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;
16use crate::kv_cache::KvCache;
17use crate::linear_core::{
18    GdnCfg, GdnWeights, ShortConvCfg, ShortConvWeights, VmfPhaseCfg, VmfPhaseWeights, gdn_forward,
19    gdn_pair, short_conv_forward, short_conv_forward_batch, short_conv_pair, vmf_phase_forward,
20    vmf_phase_pair,
21};
22use crate::pool::Pool;
23use crate::qtensor::QTensor;
24use crate::sampler::{self, SamplerConfig, SamplerScratch, SplitMix64};
25use crate::tokenizer::Tokenizer;
26use cortiq_core::mask::TaskMask;
27use cortiq_core::types::NormStyle;
28
29pub static GLOBAL_USE_GPU: std::sync::atomic::AtomicBool =
30    std::sync::atomic::AtomicBool::new(false);
31
32/// Reusable per-pipeline forward scratch: the four norm outputs the
33/// decode paths recompute every layer (single: n1/p1; pair: all four).
34/// Plain buffers, resized once — steady-state decode reuses them.
35struct ForwardScratch {
36    n1: Vec<f32>,
37    n2: Vec<f32>,
38    p1: Vec<f32>,
39    p2: Vec<f32>,
40}
41
42impl ForwardScratch {
43    fn new(hidden: usize) -> Self {
44        Self {
45            n1: vec![0.0; hidden],
46            n2: vec![0.0; hidden],
47            p1: vec![0.0; hidden],
48            p2: vec![0.0; hidden],
49        }
50    }
51}
52
53/// Complete inference pipeline state.
54pub struct Pipeline {
55    /// In-process layer split across local GPUs: (device, first layer,
56    /// last layer) per segment, in execution order. `None` = one device.
57    /// Arc so cloning the plan out of `&mut self` does not fight the
58    /// borrow checker on the hot path.
59    gpu_plan: Option<std::sync::Arc<Vec<(usize, usize, usize)>>>,
60    /// Arc: the server shares one tokenizer handle across request
61    /// handlers without borrowing a pipeline slot.
62    pub tokenizer: std::sync::Arc<Tokenizer>,
63    pub kv_cache: KvCache,
64    pub sampler_config: SamplerConfig,
65    pub weights: PipelineWeights,
66    pub hidden_size: usize,
67    pub intermediate_size: usize,
68    pub num_heads: usize,
69    pub num_kv_heads: usize,
70    pub head_dim: usize,
71    /// Total virtual layers (num_layers × num_loops for looped models).
72    pub num_layers: usize,
73    /// Physical layers in weights.layers (≤ num_layers for looped models).
74    pub physical_layers: usize,
75    /// Looped Transformer: apply final norm after each loop iteration.
76    pub loop_final_norm: bool,
77    pub vocab_size: usize,
78    pub rms_eps: f64,
79    pub rope_base: f32,
80    pub norm_style: NormStyle,
81    /// RoPE dims actually rotated (≤ head_dim; Qwen3.5 uses head_dim/4).
82    pub rotary_dim: usize,
83    /// Optional Q-head count override for each attention layer (Laguna).
84    pub attention_heads_per_layer: Option<Vec<usize>>,
85    /// Optional KV-head count of each PHYSICAL attention layer (MiMo-V2:
86    /// 4 on full-attention layers, 8 on sliding ones). Set only through
87    /// [`Pipeline::set_attn_geometry`], which also reshapes the layer
88    /// caches. None = every layer has `num_kv_heads`.
89    pub kv_heads_per_layer: Option<Vec<usize>>,
90    /// Width of each V head when it is narrower than `head_dim` (MiMo-V2:
91    /// 128 against 192). V is zero-padded to `head_dim` inside the cache
92    /// and the attention output is compacted back to nh·v_head_dim before
93    /// o_proj (see `QwenAttnCfg::v_head_dim`). None = `head_dim`.
94    pub v_head_dim: Option<usize>,
95    /// `CMF_LAYER_DUMP=<dir>` (read once at construction; tests set it
96    /// directly): the hidden state after every layer, for every position,
97    /// as raw little-endian f32 files `p{pos:06}_l{li:02}.f32` of
98    /// `hidden_size` floats each. Written by the CPU layer walks — the
99    /// batched prefill (`prefill_batch_span`) and the single-token forward
100    /// (`forward_layers_span`) — so both prompt ingest and decode can be
101    /// diffed layer by layer against an external oracle (tools/mimo_ref).
102    /// Layers a device graph runs (wgpu token/batch graph, Metal chunk or
103    /// block graphs) and the own-stack families (DeepSeek-V4/V4.1,
104    /// Qwen3.8-Flash-Next, Gemma-3n) are not dumped: run with CMF_GPU=0
105    /// for a complete set. `li` is the virtual layer index. Final logits:
106    /// `CMF_LOGIT_DUMP=<file>` (hidden + logits of the first decode step).
107    pub layer_dump: Option<std::path::PathBuf>,
108    /// GPU-graph declines already logged for this pipeline, as (graph
109    /// site, reason) — one line each, see `graph_attn_decline_reason`.
110    graph_declines: std::cell::RefCell<Vec<(&'static str, &'static str)>>,
111    /// MiMo-V2 expert placement (prefix / dynamic bank / hybrid), decided
112    /// on the first forward — see `crate::mimo_moe`.
113    pub(crate) mimo_moe: crate::mimo_moe::Slot,
114    /// Linear-core geometry (present when the model has linear layers).
115    pub vmf_cfg: Option<VmfPhaseCfg>,
116    /// GatedDeltaNet geometry (faithful vendor operator).
117    pub gdn_cfg: Option<GdnCfg>,
118    /// MiniCPM-class logit scale (tied lm_head → cannot fold into weights).
119    pub logit_multiplier: Option<f32>,
120    /// Cooperative cancel: set from any thread (FFI `cortiq_cancel`,
121    /// a dropped server connection); the generate loop checks it at
122    /// every prefill chunk and decode step and finishes with
123    /// `finish_reason: "cancelled"`. Auto-cleared when honoured.
124    pub cancel: std::sync::Arc<std::sync::atomic::AtomicBool>,
125    /// A GPU graph failure is distinct from a user/request cancellation.
126    /// Graph code sets this before raising the cooperative cancel flag so the
127    /// generation API can return an error instead of reporting a successful
128    /// `finish_reason: cancelled` result.
129    graph_failed: std::sync::atomic::AtomicBool,
130    /// Token ids currently materialized in the KV cache (the forwarded
131    /// prompt + all generated tokens except the last, which is sampled
132    /// but not yet forwarded). Lets the next generate call prefill only
133    /// the suffix when a chat app resends the whole history.
134    pub kv_history: Vec<u32>,
135    /// Owner tag of `kv_history`: the resident device graph held that
136    /// sequence (true) or the host did. Reuse continues only on the owner.
137    pub kv_history_device: bool,
138    /// KDA geometry (Kimi Linear / Kimi-K3) — shared by every Kda layer.
139    pub kda_cfg: Option<crate::linear_core::KdaCfg>,
140    /// Gemma-3n stack (AltUp/LAuReL/PLE/KV-sharing): its own forward —
141    /// weights.layers stays empty, the KV caches are the shared ones.
142    pub g3n: Option<Box<(crate::g3n::G3nGlobals, Vec<crate::g3n::G3nLayer>)>>,
143    /// DeepSeek-V4 runs its own stack too: its hidden state is `hc_mult`
144    /// copies of a vector, so no loop written for a single residual
145    /// stream can carry it.
146    pub dsv4: Option<
147        Box<(
148            crate::dsv4::Dsv4Globals,
149            Vec<crate::dsv4::Dsv4Layer>,
150            crate::dsv4::Dsv4Cfg,
151            crate::dsv4::Dsv4State,
152        )>,
153    >,
154    /// DeepSeek-V4.1 owns the shared CED/CSA2 attention state, raw Engram
155    /// lookup and four-stream mHC handoff. It cannot use the V4 cache
156    /// layout, so it has a dedicated executor and state tuple.
157    pub dsv41: Option<
158        Box<(
159            crate::dsv41::Dsv41Globals,
160            Vec<crate::dsv41::Dsv41Layer>,
161            crate::dsv41::Dsv41Cfg,
162            crate::dsv41::Dsv41State,
163        )>,
164    >,
165    /// Optional V4.1 vision tower. Text-only files leave this unset.
166    pub dsv41_vision: Option<crate::dsv41_vision::VisionModel>,
167    /// Prepared image rows consumed by the next V4.1 prefill.
168    dsv41_prefill: Option<(Vec<Option<Vec<f32>>>, Vec<bool>)>,
169    /// Qwen3.8-Flash-Next owns four residual streams plus QSA/PLE state;
170    /// the generic single-residual layer loop cannot represent it.
171    pub qwen4_exp: Option<
172        Box<(
173            crate::qwen4_exp::Globals,
174            Vec<crate::qwen4_exp::Layer>,
175            crate::qwen4_exp::Cfg,
176            crate::qwen4_exp::State,
177        )>,
178    >,
179    /// DeepSeek-V4's own speculation stack: three draft modules, each a full
180    /// layer, plus a confidence head on the last. Empty when the file has
181    /// none, which is the only signal the decode path needs.
182    pub dsv4_mtp: Vec<crate::dsv4::Dsv4Mtp>,
183    /// The draft's per-sequence state (KV rings, captured trunk hidden).
184    pub dspark: Option<crate::dsv4::DsparkState>,
185    /// Drafts awaiting their verdict: (position, proposals, still matching,
186    /// accepted so far).
187    pub dspark_pending: Vec<(usize, Vec<u32>, bool, usize)>,
188    /// Accepted prefix length of every graded draft.
189    pub dspark_hist: Vec<usize>,
190    /// The real tokens the drafts were graded against — a degenerate,
191    /// repeating output would make any acceptance number meaningless, and
192    /// the cheapest guard against believing one is to count them.
193    pub dspark_real: Vec<u32>,
194    /// The trunk's expert picks for the last few tokens, per layer. The
195    /// union over a window of them is what a batched verify would have to
196    /// read, and the ratio to the pick count is all it could save.
197    pub dspark_trunk_picks: Vec<Vec<(usize, Vec<usize>)>>,
198    /// (unique, total) expert picks per draft, trunk side and draft side.
199    pub dspark_exp: Vec<(usize, usize, usize, usize)>,
200    /// Wall time spent in the deliberately out-of-core draft. Kept separate
201    /// from trunk decode so block batching can be judged without conflating
202    /// it with GPU chain variance.
203    pub dspark_draft_ns: u128,
204    /// LFM2 short-convolution geometry (present when the model has
205    /// `ShortConv` mixer layers).
206    pub short_conv_cfg: Option<ShortConvCfg>,
207    /// Multi-token-prediction head (None = absent).
208    pub mtp: Option<MtpModule>,
209    /// MiMo-V2's draft stack (three chained MTP layers from the
210    /// `<stem>.mtp.cmf` sidecar); None = absent. Speculative greedy decode
211    /// uses it unless `CMF_MTP=0` / `CMF_MIMO_MTP=0`.
212    pub mimo_mtp: Option<mimo_mtp::MimoMtp>,
213    /// Set while the MiMo speculative verify runs `prefill_batch`: its MoE
214    /// layers take `moe_ffn_rows_exact` (each row bit-identical to decode).
215    verify_exact_moe: bool,
216    /// Speculative decode via MTP (greedy only; `CMF_MTP=0` disables).
217    pub speculative: bool,
218    /// Keep generating past end-of-sequence ids (the llama-bench contract
219    /// for a timed run). A loop flag, deliberately NOT a sampler
220    /// suppression: suppressed ids count as a penalty and switch the
221    /// speculative round and the greedy burst off, so a benchmark that
222    /// suppressed EOS never measured either.
223    pub ignore_eos: bool,
224    /// Draft-head shortlist guard: tokens left during which the draft
225    /// uses the FULL head because a recently committed id lay past the
226    /// `CMF_DRAFT_VOCAB` cut (Cyrillic and CJK ids sit above 131072 in
227    /// Qwen's table, so a prefix shortlist would draft nothing usable
228    /// there — measured on Russian prose: 2.9 → 1.6 accepted a round).
229    pub draft_full_streak: u32,
230    /// Adaptive draft depth for the speculative round (None until the
231    /// first round): grows while nearly every draft is accepted, shrinks
232    /// when fewer than half are. The verify's cost climbs with the rows on
233    /// a discrete card (RTX PRO 4000: 52 ms at 2 rows, 74 at 5, 80 at 6),
234    /// so prose wants k≈3 and code or the repetitive bench k≈5 — measured
235    /// 33.6 vs 27.6 tok/s on an essay at k=3 vs 5, 45.6 vs 38 on code.
236    /// `CMF_GRAPH_SPEC_K` pins it.
237    pub spec_k_adapt: Option<usize>,
238    /// EWMA of the accepted fraction that drives `spec_k_adapt`.
239    pub spec_acc_ewma: f32,
240    rng: SplitMix64,
241    sampler_scratch: SamplerScratch,
242    /// Speculative SAMPLING state (graph_spec_step, temperature > 0): the
243    /// correction token a rejected draft produced — committed by the loop
244    /// top in place of a fresh draw — and the per-round draft
245    /// distributions / target scratch, reused so a round allocates
246    /// nothing at the vocab size.
247    spec_forced: Option<u32>,
248    spec_q: Vec<Vec<f32>>,
249    spec_p: Vec<f32>,
250    spec_res: Vec<f32>,
251    /// The same three for the sparse chain (top-k configs).
252    spec_qs: Vec<sampler::Sparse>,
253    spec_ps: sampler::Sparse,
254    spec_ress: sampler::Sparse,
255    /// Which arm the MTP draft block runs on this generation: Some(true)
256    /// = the whole-token graph (device attention, one submit a step),
257    /// Some(false) = the per-op path; None = not decided yet. Decided
258    /// on the first draft and held, because the two arms keep the MTP
259    /// KV in different places (device mirror vs the CPU cache) and a
260    /// mid-run switch would read the wrong one.
261    mtp_graph_mode: Option<bool>,
262    /// The Metal verify graph of the round in flight, between its sync
263    /// (logits read) and the commit that replays the accepted prefix.
264    #[cfg(target_os = "macos")]
265    metal_verify: Option<MetalVerifyPending>,
266    /// Precomputed RoPE inverse frequencies [head_dim/2]. Arc: the
267    /// forward path clones a handle to escape the &mut self borrow —
268    /// cloning the table itself was a per-forward allocation.
269    pub(crate) inv_freq: std::sync::Arc<Vec<f32>>,
270    /// Reusable norm buffers for the decode hot path (roadmap §3 P0:
271    /// steady-state forward should not heap-allocate). Disjoint field
272    /// from `weights`/`kv_cache`, so split borrows keep working.
273    ws: ForwardScratch,
274    /// Persistent worker pool (None = serial; see CMF_THREADS).
275    pool: Option<std::sync::Arc<Pool>>,
276    // ── Dynamic per-token skill routing (spec §9, claim 14/16) ──
277    /// Source model, retained so a skill switch can re-resolve the
278    /// touched layers' FFN tensors (Mapped = mmap pointers, cheap).
279    pub(crate) model: Option<std::sync::Arc<cortiq_core::CmfModel>>,
280    /// Masks present → weights are dequantized f32 (rebuild path).
281    pub(crate) dyn_force_f32: bool,
282    /// Per-skill FFN layers actually replaced (derived from tensors, not
283    /// the meta `layers` field — ru2 replaces down_proj in 0..23 while
284    /// its meta says [20..23]). None = skill touches non-FFN tensors →
285    /// ineligible for cheap dynamic switching (honest refusal).
286    pub(crate) dyn_skill_layers: Vec<Option<Vec<usize>>>,
287    /// Currently overlaid skill (index into model.header.skills); None =
288    /// backbone. Set at load time to the statically-overlaid skill so
289    /// `set_active_skill(None)` correctly reverts it (else a static
290    /// skill would silently persist — the union-diff assumes dyn_active
291    /// always mirrors the live overlay). Switched by `set_active_skill`.
292    pub(crate) dyn_active: Option<usize>,
293    /// Pipeline was loaded with a soft blend (materialized working
294    /// tensors, not a single skill index) → dynamic routing refuses:
295    /// there is no single index to revert the blend from.
296    pub(crate) dyn_blend_loaded: bool,
297    /// Layer whose post-residual hidden feeds the router φ (shared by
298    /// swarm skills). None = φ capture off.
299    pub(crate) dyn_phi_layer: Option<usize>,
300    /// EMA of φ at `dyn_phi_layer` over the decode window (on-policy).
301    dyn_phi_ema: Vec<f32>,
302    dyn_phi_seen: usize,
303    /// Hysteresis router driving per-token skill switches during decode
304    /// (None = static/no dynamic routing). Taken out during generation.
305    pub dyn_router: Option<crate::swarm::DynRouter>,
306    /// O(1) Nyström attention setting (CLI/env/header-hint resolved by
307    /// the caller; None = plain cache attention everywhere).
308    o1_cfg: Option<crate::nystrom::O1Cfg>,
309    /// Bumped once per collecting→sealed transition — the GPU state mirror
310    /// re-uploads when it sees a new epoch (each fresh sealed state).
311    o1_epoch: u64,
312    /// Per-layer o1 flags derived from `o1_cfg` (Full layers only).
313    o1_flags: Vec<bool>,
314    /// Emit a structured per-token trace (B4 telemetry channel). Off by
315    /// default — the runtime is silent unless observation is requested.
316    trace: bool,
317    /// Confidence-calibration temperature (B1): reported probability is
318    /// softmax(logits / calib_temp). 1.0 = raw. Set from header.calibration.
319    calib_temp: f32,
320    /// Process-unique id keying this pipeline's device KV mirrors.
321    #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
322    graph_kv_id: u64,
323    /// Decode asks the token graph to also run final-norm + lm_head on
324    /// the device (drops the separate per-op lm_head round trip).
325    #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
326    graph_want_logits: bool,
327    /// NLL quality gates require the graph's fused head rather than silently
328    /// accepting a CPU head fallback. Generation keeps the historical
329    /// best-effort `graph_want_logits` behavior.
330    #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
331    graph_head_required: bool,
332    /// Logits the graph produced for the token just forwarded (taken by
333    /// the decode loop; None = compute on the CPU path).
334    graph_logits: Option<Vec<f32>>,
335    /// Packed Embryo graph model, built lazily on the first eligible
336    /// resident call so other architectures pay no packing cost.
337    embryo_graph: Option<std::sync::Arc<crate::gpu::EmbryoGraphModel>>,
338    /// This pipeline's token graph refused for a STRUCTURAL reason (its
339    /// own weights / layer kinds): not retried. Per pipeline, never
340    /// process-wide: a skill lane whose pack does not build must not flip
341    /// the backbone slot mid-sequence onto the host path (and a new lane
342    /// must not clear the backbone's verdict) — R4/NF-2.
343    graph_refused: std::sync::atomic::AtomicBool,
344    /// Token embeddings are multiplied by this at input (Gemma: √hidden).
345    pub embed_multiplier: f32,
346    /// Attention score scale (1/√head_dim unless the arch overrides —
347    /// Gemma's query_pre_attn_scalar).
348    pub attn_scale: f32,
349    /// Sliding-window attention: (window, every-Nth-layer-is-global
350    /// pattern) — Gemma-3.
351    pub swa: Option<(usize, usize)>,
352    /// Sliding-window tail trimming, `(slack, align)` for
353    /// `LayerKvCache::trim_window`: every layer with a window keeps only
354    /// the rows its window can still read (Spark-X2.5: 576..639 rows per
355    /// 512-window layer instead of the whole context — 27 of the 4B's 36
356    /// layers). Set at load for Spark-X2.5 only (`CMF_SWA_TRIM=0` turns
357    /// it off); None keeps every row, as every other model always has.
358    pub swa_trim: Option<(usize, usize)>,
359    /// Explicit local/global schedule for architectures that cannot be
360    /// represented by Gemma's every-Nth-global convention.
361    pub sliding_layers: Option<Vec<bool>>,
362    /// Natively bounded anchor record (`arch.anchor_core`): the file's
363    /// operator, installed once at load. `Some` = the model is
364    /// bounded-native — `--o1`/`CMF_O1*` are refused, prefix reuse and
365    /// the penalty window are bounded, and no anchor layer stores
366    /// anything per position.
367    pub anchor_core: Option<cortiq_core::AnchorCoreConfig>,
368    /// The `[W][rd/2]` relative-rotation table every bounded layer shares
369    /// (built from `inv_freq` once the RoPE setup is final).
370    bounded_rope: Option<std::sync::Arc<crate::bounded::BoundedRope>>,
371    /// Bounded prefix-reuse key (length + rolling hash + tail) of a
372    /// bounded-native model; `kv_history` stays empty there so nothing
373    /// in the pipeline grows with the dialogue.
374    pub kv_prefix: KvPrefix,
375    /// Prompt positions the last `generate*` call actually forwarded
376    /// (prefix reuse subtracts what the cache already held).
377    pub last_prefill_tokens: usize,
378    /// RoPE table of the sliding (local) layers, when they use their
379    /// own base frequency (Gemma-3: 10k local vs 1M global).
380    pub inv_freq_local: Option<std::sync::Arc<Vec<f32>>>,
381    pub rotary_dim_local: Option<usize>,
382    pub rope_scale: f32,
383    pub rope_scale_local: f32,
384    /// Gemma-4: global layers run their own geometry — (head_dim,
385    /// num_kv_heads); sliding layers keep the base fields.
386    pub global_attn: Option<(usize, usize)>,
387    /// Gemma-4: the global layers' proportional RoPE table (len
388    /// global_head_dim/2, zero-padded tail = identity rotation).
389    pub inv_freq_global: Option<std::sync::Arc<Vec<f32>>>,
390    /// Scale-less RMS normalization of V heads before caching (Gemma-4).
391    pub attn_v_norm: bool,
392    /// HunYuan dense: per-head q/k norm runs after RoPE (see the arch flag).
393    pub qk_norm_after_rope: bool,
394    /// The per-head `self_attn.g_proj` output gate uses sigmoid
395    /// (Spark-X2.5) instead of softplus (Laguna).
396    pub proj_gate_sigmoid: bool,
397    /// Final-logit soft-capping C: logits = C·tanh(logits/C) (Gemma-4).
398    pub final_softcap: Option<f32>,
399    /// Cortiq Embryo hierarchical head: cluster matrix [C, hidden]. The
400    /// flat logits h·Eᵀ are turned into the two-level log-probabilities
401    /// log softmax_c(h·Cᵀ)[c(v)] + log softmax_{s∈c(v)}(h·E_c(v)ᵀ)[v].
402    pub head_clusters: Option<std::sync::Arc<Vec<f32>>>,
403    /// Gemma-2 attention-logit soft-capping (0.0 = off).
404    pub attn_softcap: f32,
405    /// Compute per-token confidence (a full-vocab softmax each
406    /// token). On by default; `bench --core` turns it off to match
407    /// llama-bench's core timing.
408    confidence_on: bool,
409    /// Test-only one-shot forward failure, scoped to this pipeline so
410    /// parallel scoring tests cannot consume one another's injection.
411    #[cfg(test)]
412    nll_test_fail_at: Option<usize>,
413    /// Test-only route override; avoids mutating the process-wide
414    /// `CMF_PREFILL` environment variable while forcing the serial path.
415    #[cfg(test)]
416    nll_test_force_serial: bool,
417}
418
419#[cfg(target_os = "macos")]
420impl Drop for Pipeline {
421    fn drop(&mut self) {
422        // the async replay writes into `kv_cache` Vecs about to be freed
423        let _ = crate::gpu_metal::wait_replay();
424        crate::gpu::kv_mirror_drop(self.graph_kv_id);
425    }
426}
427
428#[cfg(not(target_os = "macos"))]
429impl Drop for Pipeline {
430    fn drop(&mut self) {
431        // The wgpu resident Embryo graph owns recurrent/KV buffers keyed by
432        // this pipeline's sequence id.  Release that sequence image when a
433        // pooled pipeline is dropped; model weights stay cached for reuse.
434        crate::gpu::graph_kv_reset(self.graph_kv_id);
435    }
436}
437
438/// Model weights. Matrices are `QTensor` (owned f32 for small models
439/// and tests — bit-identical to the historical paths — or quantized
440/// bytes zero-copy from the CMF mmap for big models). 1-D norms are
441/// always small and stay f32.
442pub struct PipelineWeights {
443    /// Embedding table: [vocab_size, hidden_size]
444    pub embed_tokens: QTensor,
445    /// Per-layer weights
446    pub layers: Vec<LayerWeights>,
447    /// LM head: [vocab_size, hidden_size]
448    pub lm_head: QTensor,
449    /// Final norm: [hidden_size]
450    pub final_norm: Vec<f32>,
451}
452
453/// One transformer layer: shared norms + MLP, attention by kind.
454pub struct LayerWeights {
455    pub input_norm: Vec<f32>,
456    /// The pre-FFN norm (`post_attention_layernorm` classically;
457    /// `pre_feedforward_layernorm` on Gemma-2/3 sandwich layers).
458    pub post_norm: Vec<f32>,
459    /// Gemma-2/3 sandwich: norm applied to the ATTENTION OUTPUT before
460    /// its residual add (`post_attention_layernorm` there).
461    pub attn_out_norm: Option<Vec<f32>>,
462    /// Gemma-4: the whole layer output is multiplied by this scalar.
463    pub layer_scale: Option<f32>,
464    /// Gemma-2/3 sandwich: norm applied to the FFN OUTPUT before its
465    /// residual add (`post_feedforward_layernorm`).
466    pub ffn_out_norm: Option<Vec<f32>>,
467    pub ffn: FfnKind,
468    pub attn: AttnKind,
469}
470
471/// FFN gate activation: SiLU (SwiGLU family) or tanh-GELU (Gemma's
472/// GeGLU). A property of the model, carried on every FFN triple.
473#[derive(Clone, Copy, PartialEq, Debug, Default)]
474pub enum Act {
475    #[default]
476    Silu,
477    GeluTanh,
478    /// Exact erf GELU (HF `hidden_act = "gelu"`: Spark-X2.5).
479    Gelu,
480    /// Kimi-K3 SituAndMul: BOTH halves transform —
481    /// a = β·tanh(g/β)·σ(g), up' = linβ·tanh(u/linβ) (linβ>0), out = a·up'.
482    Situ {
483        beta: f32,
484        linear_beta: f32,
485    },
486}
487
488impl Act {
489    pub fn from_arch(name: &str) -> Self {
490        if name == "gelu_tanh" {
491            Self::GeluTanh
492        } else if name == "gelu" {
493            Self::Gelu
494        } else {
495            Self::Silu
496        }
497    }
498
499    /// Arch-driven constructor (activation name + situ betas).
500    pub fn from_arch_full(arch: &cortiq_core::ModelArch) -> Self {
501        match arch.hidden_act.as_str() {
502            "situ" => Self::Situ {
503                beta: arch.activation_situ_beta.unwrap_or(1.0) as f32,
504                linear_beta: arch.activation_situ_linear_beta.unwrap_or(0.0) as f32,
505            },
506            other => Self::from_arch(other),
507        }
508    }
509
510    #[inline]
511    pub fn apply(self, x: f32) -> f32 {
512        match self {
513            Self::Silu => inference::silu(x),
514            Self::GeluTanh => inference::gelu_tanh(x),
515            Self::Gelu => inference::gelu_erf(x),
516            Self::Situ { beta, .. } => beta * (x / beta).tanh() * (1.0 / (1.0 + (-x).exp())),
517        }
518    }
519
520    /// Gated combine — the FFN contract. Situ transforms the UP half
521    /// too, so callers must use this instead of apply(g)·u.
522    #[inline]
523    pub fn combine(self, g: f32, u: f32) -> f32 {
524        match self {
525            Self::Situ { linear_beta, .. } if linear_beta > 0.0 => {
526                self.apply(g) * (linear_beta * (u / linear_beta).tanh())
527            }
528            _ => self.apply(g) * u,
529        }
530    }
531
532    /// The wgpu graphs' arm for this activation; None = no device kernel
533    /// computes it, and the graph builder must refuse the layer (the dense
534    /// graph FFN once computed SiLU for whatever the model asked for).
535    pub fn graph_act(self) -> Option<crate::gpu::GraphAct> {
536        match self {
537            Self::Silu => Some(crate::gpu::GraphAct::Silu),
538            Self::Gelu => Some(crate::gpu::GraphAct::GeluErf),
539            Self::GeluTanh => Some(crate::gpu::GraphAct::GeluTanh),
540            Self::Situ { .. } => None,
541        }
542    }
543}
544
545/// Dense gated triple — the FFN of a dense layer or of one expert.
546pub struct DenseFfn {
547    pub gate_proj: QTensor,
548    pub up_proj: QTensor,
549    pub down_proj: QTensor,
550    /// Gate activation (SiLU default; Gemma: tanh-GELU).
551    pub act: Act,
552    /// `down_proj` stored transposed (`[inter, hidden]`), when the file
553    /// carries it. Only the per-token sparse path reads it: a neuron's
554    /// down weights are a contiguous ROW there, so the token's chosen
555    /// neurons are the only bytes touched. `None` = the ordinary layout,
556    /// and the sparse path stays off.
557    pub down_t: Option<QTensor>,
558    /// Task tubes (spec: defragged task-conditional width). The three
559    /// matrices above are the CORE — the neurons every task computes;
560    /// each tube is an independently quantized slice of the SAME layer
561    /// holding the neurons only some tasks need. A tube is a normal
562    /// tensor triple, so every kernel runs it unchanged, and the bytes
563    /// of an inactive tube are never read. Empty = ordinary dense FFN.
564    pub segs: Vec<FfnSeg>,
565}
566
567/// One task tube: a contiguous slice of a layer's FFN neurons, stored
568/// as its own `[w, hidden]` / `[hidden, w]` triple. `start` is the
569/// neuron's index in the layer's FULL space (core first, then tubes in
570/// order) — the bit a task mask sets to switch this tube on.
571pub struct FfnSeg {
572    pub gate: QTensor,
573    pub up: QTensor,
574    pub down: QTensor,
575    pub start: usize,
576    pub width: usize,
577}
578
579/// FFN operator of a layer, decided by tensor presence at load time
580/// (router `mlp.gate.weight` in the directory = MoE layer).
581pub enum FfnKind {
582    Dense(DenseFfn),
583    /// Mixture-of-Experts (Qwen2-MoE / Qwen3-MoE): softmax over ALL
584    /// expert logits → top-k, optional renorm; experts stay quantized
585    /// in mmap — only the selected ones are touched per token.
586    Moe(MoeFfn),
587    /// Gemma-4 MoE: a dense MLP branch AND a routed-expert branch in
588    /// the SAME layer, each with its own norm sandwich. The dense
589    /// branch reads the pre-FFN-normed input; the expert branch (and
590    /// the router) read the RAW residual through `pre_norm_2`:
591    ///   d = post_norm_1(dense(x̂));  m = post_norm_2(Σwₑ·FFNₑ(pre_norm_2(h)))
592    ///   ffn_out = d + m   (the caller's ffn_out_norm + residual follow)
593    DenseMoe(Box<DenseMoeFfn>),
594}
595
596/// Gemma-4 dual-branch FFN (see `FfnKind::DenseMoe`).
597pub struct DenseMoeFfn {
598    pub dense: DenseFfn,
599    pub moe: MoeFfn,
600    /// post_feedforward_layernorm_1 — dense-branch output norm.
601    pub post_norm_1: Vec<f32>,
602    /// pre_feedforward_layernorm_2 — expert-branch input norm (applied
603    /// to the RAW residual, not the pre-FFN-normed activation).
604    pub pre_norm_2: Vec<f32>,
605    /// post_feedforward_layernorm_2 — expert-branch output norm.
606    pub post_norm_2: Vec<f32>,
607}
608
609pub struct MoeFfn {
610    /// Router `mlp.gate.weight` [num_experts, hidden].
611    pub router: QTensor,
612    pub experts: Vec<DenseFfn>,
613    pub top_k: usize,
614    pub norm_topk_prob: bool,
615    /// Router scores per-expert with a sigmoid (LFM2-MoE / DeepSeek-V3
616    /// `noaux_tc`) instead of a softmax over all experts (Qwen).
617    pub router_sigmoid: bool,
618    /// Per-expert selection bias `mlp.expert_bias` [num_experts]
619    /// (LFM2-MoE): added to the sigmoid scores for the top-k CHOICE only;
620    /// the gathered weights use the unbiased scores. None = no bias.
621    pub expert_bias: Option<Vec<f32>>,
622    /// Top-k weights are multiplied by this after the optional renorm
623    /// (LFM2-MoE `routed_scaling_factor`; 1.0 = off).
624    pub routed_scaling: f32,
625    /// Adaptive routing (CMF_MOE_TAU, opt-in): keep the smallest
626    /// prefix of the top-k whose renormalized mass reaches τ —
627    /// confident tokens touch 1–2 experts, flat ones keep all k.
628    /// MoE decode is memory-bound, so skipped experts are skipped
629    /// weight traffic. None = classic fixed top-k (bit-identical).
630    pub route_tau: Option<f32>,
631    /// Always-on shared expert. Qwen2-MoE carries an additional sigmoid
632    /// gate; Laguna adds the shared expert unconditionally (`None`).
633    pub shared: Option<(DenseFfn, Option<QTensor>)>,
634    /// Expert-selection counters (truncated Fisher B-field of claim 12:
635    /// routing frequency during calibration). Filled by every forward,
636    /// read by the CLI via CMF_MOE_STATS. RefCell: decode is single-threaded.
637    pub stats: std::cell::RefCell<Vec<u64>>,
638    /// Per-CHANNEL sum of squares of this FFN's input, accumulated over a
639    /// calibration run (`CMF_RMS_TRACE`). These are the RMS activation
640    /// traces AWNP needs: raw weight magnitude says every channel matters
641    /// equally, and the question AWNP asks is whether the ACTIVATIONS
642    /// disagree. Off unless the env var is set — an f64 add per channel
643    /// per token is cheap, but not free.
644    pub act_sq: std::cell::RefCell<Vec<f64>>,
645    /// Raw FFN-input rows captured for the layers named by `CMF_ACT_DUMP`
646    /// (`"9,19"`). AWNP is nullspace PROJECTION: after dropping channels the
647    /// survivors are refitted to absorb what was removed, and how much they
648    /// can absorb depends on the activation COVARIANCE, not on per-channel
649    /// RMS. Per-channel numbers can only bound the cost from above.
650    pub act_rows: std::cell::RefCell<Vec<f32>>,
651    /// Task mask over routed experts (DTG-MA over MoE, claim-12 B-field
652    /// applied): `false` experts are excluded from selection, the
653    /// softmax renormalizes over the allowed set. Built by the loader
654    /// from CMF_MOE_MASK=<stats.json> + CMF_MOE_MASK_COVER. None = all.
655    pub mask: Option<Vec<bool>>,
656    /// Gemma-4: per-expert weight scale applied AFTER the top-k renorm
657    /// (`router.per_expert_scale`). None = 1.0 everywhere.
658    pub per_expert_scale: Option<Vec<f32>>,
659    /// Gemma-4: the router reads a SCALE-LESS rms-norm of its input
660    /// (the constant gain router.scale·√hidden is folded into the
661    /// router weights at convert time).
662    pub router_input_norm: bool,
663    /// Cortiq Embryo: resonance routing (P1) — the "logits" are
664    /// bias_e − ‖(x−μ_e) − U_eᵀU_e(x−μ_e)‖², argmax = the expert whose
665    /// descriptor reconstructs the input best. `router` is a placeholder.
666    pub resonance: Option<Resonance>,
667    /// Growth records (`kind = "expert_append"`, spec §9.5.1) mounted
668    /// behind the trunk experts, one entry per grown expert IN THE ORDER
669    /// they sit in `experts` (the tail `experts[experts.len() - grown.len()..]`).
670    /// The trunk keeps `experts.len() - grown.len()` experts. Empty on a
671    /// gated MoE, on a file without records and under `CMF_GROWTH=off`.
672    pub grown: Vec<GrownExpert>,
673}
674
675/// One grown expert of an `expert_append` record as the loader mounted it.
676#[derive(Debug, Clone, PartialEq, Eq)]
677pub struct GrownExpert {
678    /// The record's skill id.
679    pub record: String,
680    /// Position of the record in `header.skills`.
681    pub record_index: usize,
682    pub layer: usize,
683    /// The index the tensor name declares (`experts.{e}` — the chain rule
684    /// of the format); the executed position may be smaller when an
685    /// earlier record of the layer is not mounted.
686    pub expert: usize,
687}
688
689/// Per-expert resonance descriptors of one MoE layer (`mlp.desc.*`).
690pub struct Resonance {
691    /// [E, hidden]
692    pub mu: Vec<f32>,
693    /// [E, k, hidden] orthonormal directions (k may be 0)
694    pub u: Vec<f32>,
695    pub k: usize,
696    /// [E] selection bias (loss-free balancing, trained online)
697    pub bias: Vec<f32>,
698    /// [E] reconstruction-error shell: an expert whose error
699    /// `‖(x−μ)⊥U‖² = d² − proj` exceeds its shell scores `−∞` (it never
700    /// wins). `+inf` = no shell — every trunk expert; a grown expert
701    /// (`expert_append`) carries the finite `desc.shell` its record stores.
702    /// Empty = no shell anywhere (legacy constructors).
703    pub shell: Vec<f32>,
704}
705
706/// `CMF_GROWTH_SHELL` state: 0 = not yet read from the environment, 1 = on,
707/// 2 = off. Process-wide, like the environment it mirrors.
708static GROWTH_SHELL: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0);
709
710/// Is the growth shell applied (`Resonance::scores` −∞ rule, the resident
711/// graph's packed shell)? `CMF_GROWTH_SHELL=off` disables it for
712/// measurement; [`set_growth_shell`] overrides the environment in-process
713/// (`growth-eval --shell`). Default: on.
714pub fn growth_shell_enabled() -> bool {
715    use std::sync::atomic::Ordering;
716    match GROWTH_SHELL.load(Ordering::Relaxed) {
717        1 => true,
718        2 => false,
719        _ => {
720            let off = std::env::var("CMF_GROWTH_SHELL")
721                .map(|v| v.eq_ignore_ascii_case("off") || v == "0")
722                .unwrap_or(false);
723            GROWTH_SHELL.store(if off { 2 } else { 1 }, Ordering::Relaxed);
724            !off
725        }
726    }
727}
728
729/// Switch the growth shell on/off for this process (`None` = re-read
730/// `CMF_GROWTH_SHELL` on the next query). A pipeline packed into the
731/// resident graph BEFORE the switch keeps the shell it was packed with —
732/// build a new pipeline after switching.
733pub fn set_growth_shell(on: Option<bool>) {
734    GROWTH_SHELL.store(
735        match on {
736            Some(true) => 1,
737            Some(false) => 2,
738            None => 0,
739        },
740        std::sync::atomic::Ordering::Relaxed,
741    );
742}
743
744impl Resonance {
745    /// Does any expert carry a finite shell (a mounted growth record)?
746    pub fn has_shell(&self) -> bool {
747        self.shell.iter().any(|s| s.is_finite())
748    }
749
750    /// The shell the runtime applies right now: the stored one, or all
751    /// `+inf` when the shell is switched off (`CMF_GROWTH_SHELL=off`).
752    pub fn effective_shell(&self, ne: usize) -> Vec<f32> {
753        let mut out = vec![f32::INFINITY; ne];
754        if growth_shell_enabled() {
755            for (o, s) in out.iter_mut().zip(&self.shell) {
756                *o = *s;
757            }
758        }
759        out
760    }
761
762    /// Routing scores for one input row (higher = better). A grown
763    /// expert whose reconstruction error lies outside its shell gets
764    /// `−∞` (unless the shell is switched off); trunk rows are the exact
765    /// bit pattern they were before growth.
766    pub fn scores(&self, x: &[f32], out: &mut [f32]) {
767        let h = x.len();
768        let ne = out.len();
769        let shell_on = growth_shell_enabled() && !self.shell.is_empty();
770        for e in 0..ne {
771            let mu = &self.mu[e * h..(e + 1) * h];
772            let mut d2 = 0.0f32;
773            for j in 0..h {
774                let d = x[j] - mu[j];
775                d2 += d * d;
776            }
777            let mut proj = 0.0f32;
778            for i in 0..self.k {
779                let u = &self.u[(e * self.k + i) * h..(e * self.k + i + 1) * h];
780                let mut p = 0.0f32;
781                for j in 0..h {
782                    p += (x[j] - mu[j]) * u[j];
783                }
784                proj += p * p;
785            }
786            let err = d2 - proj;
787            out[e] = self.bias.get(e).copied().unwrap_or(0.0) - err;
788            if shell_on && err > self.shell.get(e).copied().unwrap_or(f32::INFINITY) {
789                out[e] = f32::NEG_INFINITY;
790            }
791        }
792    }
793}
794
795/// Attention operator of a layer. Extension point: new operators are
796/// new variants here + a forward in their own module.
797pub enum AttnKind {
798    /// GQA softmax attention (+ optional Qwen3.5 qk-norm / output gate).
799    Full {
800        wq: QTensor,
801        wk: QTensor,
802        wv: QTensor,
803        wo: QTensor,
804        q_norm: Option<Vec<f32>>,
805        k_norm: Option<Vec<f32>>,
806        output_gate: bool,
807        /// Laguna: a separate softplus projection applied to the attention
808        /// output before O. The bool means one scalar per head (broadcast
809        /// across head_dim); false means one scalar per element.
810        softplus_gate: Option<(QTensor, bool)>,
811        /// Qwen2-family projection biases (q, k, v).
812        bias: Option<(Vec<f32>, Vec<f32>, Vec<f32>)>,
813    },
814    /// Canonical linear core (VMF phase attention).
815    Linear(VmfPhaseWeights),
816    /// Faithful vendor linear operator (Qwen3.5 GatedDeltaNet).
817    LinearGdn(GdnWeights),
818    /// LFM2 gated short-convolution mixer (no KV cache; conv ring state
819    /// lives in the layer's `linear_state`).
820    ShortConv(ShortConvWeights),
821    /// DeepSeek-V2 Multi-head Latent Attention. v1 executes it as
822    /// expand-to-MHA: the latent is projected per token, K/V expand to
823    /// every head and live in the ordinary cache (K head layout
824    /// [rope | nope] so the standard partial rotary covers the shared
825    /// rope key; V rows are zero-padded to the K head_dim and the pad
826    /// is sliced off before O). Latent-resident cache is a later
827    /// optimization, not a semantic change.
828    Mla(Box<MlaWeights>),
829    /// Kimi Delta Attention (Kimi Linear / Kimi-K3): per-channel decayed
830    /// delta rule, separate q/k/v short convs, sigmoid-gated output norm.
831    /// State lives in the layer's `linear_state` (no KV cache).
832    Kda(Box<crate::linear_core::KdaWeights>),
833    /// Natively bounded softmax anchor `swa_sink_v1` (Embryo-O1): ring of
834    /// the last W raw keys with relative RoPE + trained NoPE sinks, one
835    /// softmax. State is the fixed-size ring in `LayerKvCache::bounded`;
836    /// nothing is stored per position (see `crate::bounded`).
837    Bounded(Box<crate::bounded::BoundedWeights>),
838}
839
840/// DeepSeek-V2 MLA projections (see `AttnKind::Mla`).
841pub struct MlaWeights {
842    /// `[nh·(rope+nope), hidden]` (or `[…, q_lora]` when compressed) —
843    /// the converter permutes each head rope-first so rotary_dim =
844    /// qk_rope works unchanged.
845    pub q_proj: QTensor,
846    /// Compressed q (K3/V3 class): x → q_a `[q_lora, hidden]` →
847    /// rms(q_a_norm) → q_proj (= q_b). None = direct q (V2-Lite).
848    pub q_a: Option<QTensor>,
849    pub q_a_norm: Option<Vec<f32>>,
850    /// `kv_a_proj_with_mqa` `[lora + rope, hidden]` (latent first).
851    pub kv_a: QTensor,
852    /// RMS-norm weights over the latent (`kv_a_layernorm`, [lora]).
853    pub kv_a_norm: Vec<f32>,
854    /// `[nh·(nope+v), lora]` — per head [k_nope | v].
855    pub kv_b: QTensor,
856    /// `[hidden, nh·v]`.
857    pub o_proj: QTensor,
858    pub nh: usize,
859    pub qk_rope: usize,
860    pub qk_nope: usize,
861    pub v_dim: usize,
862    pub lora: usize,
863    /// Softmax scale (1/√(rope+nope), YaRN-mscale-corrected at load).
864    pub scale: f32,
865    /// Kimi Linear NoPE: skip the rotary entirely (layout unchanged).
866    pub nope: bool,
867}
868
869/// Multi-token-prediction head (DeepSeek/Qwen style, spec §2.1):
870/// `x = eh_proj·[enorm(embed(next)); hnorm(hidden)]` → one transformer
871/// block over its own KV → shared lm_head. Drafts the token after next;
872/// the main model verifies, so output is exact — MTP only buys speed.
873pub struct MtpModule {
874    pub enorm: Vec<f32>,
875    pub hnorm: Vec<f32>,
876    /// [hidden, 2·hidden]
877    pub eh_proj: QTensor,
878    pub layer: LayerWeights,
879    pub final_norm: Vec<f32>,
880    pub kv: crate::kv_cache::LayerKvCache,
881}
882
883/// A Metal verify graph after its sync: what the commit needs — the
884/// graph (per-layer replay scratch), the GDN layers in encode order (their
885/// CPU states receive the replay), and the attention layers with the CPU
886/// row count they were encoded against (the accepted rows are pulled from
887/// the mirror from there).
888/// One item of the Metal rows-graph plan.
889#[cfg(target_os = "macos")]
890enum MetalRowsItem<'a> {
891    Gdn {
892        run: Vec<crate::gpu_metal::GdnGpuLayer<'a>>,
893        first: usize,
894    },
895    Attn {
896        l: crate::gpu_metal::AttnGpuLayer<'a>,
897        li: usize,
898        q_norm: Option<&'a [f32]>,
899        k_norm: Option<&'a [f32]>,
900        output_gate: bool,
901    },
902}
903
904#[cfg(target_os = "macos")]
905struct MetalVerifyPending {
906    graph: crate::gpu_metal::VerifyGraph,
907    gdn_layers: Vec<usize>,
908    attn_layers: Vec<(usize, usize)>,
909}
910
911/// A round's batched MTP warm-up, submitted but not yet waited
912/// (`mtp_warm_batch_submit` → `mtp_warm_batch_finish`): the trunk commit's
913/// GDN replay is queued between the two.
914#[cfg(target_os = "macos")]
915struct MetalWarmPending {
916    graph: crate::gpu_metal::VerifyGraph,
917    cpu_stored: usize,
918    b: usize,
919}
920
921#[cfg(target_os = "macos")]
922enum MetalRowsRun {
923    /// Capability/preflight refusal before a command buffer was committed.
924    Declined,
925    /// A graph was admitted and then failed; callers must clear the sequence
926    /// rather than replaying it through CPU/serial state.
927    Failed,
928    Completed(MetalVerifyPending),
929}
930
931#[cfg(target_os = "macos")]
932enum MetalPrefillOutcome {
933    Declined,
934    Failed,
935    Completed(Vec<f32>),
936}
937
938#[cfg(target_os = "macos")]
939enum MetalBatchNllOutcome {
940    Declined,
941    Failed(String),
942    Completed(f64, usize),
943}
944
945/// The speculation trial's phases (see the decode loop): four timed
946/// speculative rounds, eight timed plain tokens, then the faster arm
947/// until a re-check.
948#[derive(Clone, Copy)]
949enum SpecTrial {
950    Spec {
951        t0: std::time::Instant,
952        gen0: usize,
953        rounds: usize,
954    },
955    Plain {
956        t0: std::time::Instant,
957        gen0: usize,
958    },
959    Decided {
960        spec: bool,
961        recheck_at: usize,
962    },
963}
964
965/// `CMF_GRAPH_SPEC_TIME`: 0 = off, 1 = one line per speculative round
966/// plus the host stamps of any OUTLIER round (wall > 1.4× the running
967/// median), 2 = the host stamps of every round.
968pub(crate) fn spec_time_level() -> u8 {
969    static L: std::sync::OnceLock<u8> = std::sync::OnceLock::new();
970    *L.get_or_init(|| match std::env::var("CMF_GRAPH_SPEC_TIME") {
971        Ok(v) => v.trim().parse::<u8>().map(|n| n.max(1)).unwrap_or(1),
972        Err(_) => 0,
973    })
974}
975
976/// The round's host stamps: `spec_stamp(name)` records the time since
977/// the previous stamp (the section that just ended) — from anywhere on
978/// the round's call chain (the Metal verify, the draft step, the commit),
979/// no plumbing. Off (a single atomic load) unless `CMF_GRAPH_SPEC_TIME`
980/// is set; one decode thread at a time is assumed (diagnostics).
981struct SpecStampLog {
982    t_last: std::time::Instant,
983    items: Vec<(&'static str, f32)>,
984}
985
986static SPEC_STAMPS: std::sync::Mutex<Option<SpecStampLog>> = std::sync::Mutex::new(None);
987
988pub(crate) fn spec_stamp(name: &'static str) {
989    if spec_time_level() == 0 {
990        return;
991    }
992    if let Ok(mut g) = SPEC_STAMPS.lock() {
993        if let Some(log) = g.as_mut() {
994            let now = std::time::Instant::now();
995            log.items
996                .push((name, (now - log.t_last).as_secs_f32() * 1e3));
997            log.t_last = now;
998        }
999    }
1000}
1001
1002fn spec_stamps_begin() {
1003    if spec_time_level() == 0 {
1004        return;
1005    }
1006    if let Ok(mut g) = SPEC_STAMPS.lock() {
1007        *g = Some(SpecStampLog {
1008            t_last: std::time::Instant::now(),
1009            items: Vec::with_capacity(64),
1010        });
1011    }
1012}
1013
1014fn spec_stamps_take() -> Vec<(&'static str, f32)> {
1015    SPEC_STAMPS
1016        .lock()
1017        .ok()
1018        .and_then(|mut g| g.take())
1019        .map(|l| l.items)
1020        .unwrap_or_default()
1021}
1022
1023/// One line: every stamp name in first-seen order with its total over the
1024/// round and, when it fired more than once (the draft steps), the count.
1025fn spec_stamps_format(items: &[(&'static str, f32)]) -> String {
1026    let mut agg: Vec<(&'static str, f32, u32)> = Vec::with_capacity(items.len());
1027    for &(n, ms) in items {
1028        match agg.iter_mut().find(|e| e.0 == n) {
1029            Some(e) => {
1030                e.1 += ms;
1031                e.2 += 1;
1032            }
1033            None => agg.push((n, ms, 1)),
1034        }
1035    }
1036    let mut s = String::with_capacity(agg.len() * 16);
1037    for (n, ms, k) in agg {
1038        if k > 1 {
1039            s.push_str(&format!("{n} {ms:.1}/{k} "));
1040        } else {
1041            s.push_str(&format!("{n} {ms:.1} "));
1042        }
1043    }
1044    s
1045}
1046
1047/// The speculation monitor: exponential averages of a round's wall time
1048/// and of the tokens it produced, and the plain token's wall time — the
1049/// three numbers the keep/stop rule needs. A round pays when
1050/// `tokens_per_round · plain_ms > round_ms · 1.03`. The one-shot trial
1051/// (four rounds against eight tokens) mis-called prose: the first rounds
1052/// after a prompt are formulaic and accept well, the body does not (an
1053/// essay measured 39 against a plain 44.8 with the trial saying
1054/// "speculate"), so the rule now runs on EVERY round and stops after four
1055/// consecutive losing rounds; a stopped speculation is retried 128 tokens
1056/// later.
1057///
1058/// Native Metal (`metal: true`) does not pay the eight plain tokens up
1059/// front: on the 27B a plain token is ~150 ms, so the trial alone cost
1060/// ~1.2 s of every answer. There the plain phase is (a) skipped while the
1061/// rounds land at least `SPEC_PROXY_TOKENS` tokens each — a k=7 round on
1062/// Metal costs ~1.9 plain tokens (286 against 148 ms measured on the M4),
1063/// so 3.5 tokens/round cannot lose on any Metal round/plain ratio seen —
1064/// and (b) otherwise bounded to the fewest tokens that time it: two, or
1065/// as many as fit in `SPEC_PLAIN_MIN_MS` (a 150-ms token measures itself;
1066/// a 10-ms one needs the eight). The keep/stop rule itself is unchanged:
1067/// the moment a plain rate exists, it decides.
1068#[derive(Default, Clone, Copy)]
1069struct SpecMon {
1070    round_ms: f64,
1071    tokens: f64,
1072    plain_ms: f64,
1073    n: u32,
1074    fails: u32,
1075    metal: bool,
1076}
1077
1078/// Tokens per round at or above which a Metal round pays without a plain
1079/// measurement (see `SpecMon`).
1080const SPEC_PROXY_TOKENS: f64 = 3.5;
1081/// The Metal plain phase: at least two tokens, and more until this much
1082/// wall time has been timed (up to the eight the other backends time).
1083const SPEC_PLAIN_MIN_MS: f64 = 200.0;
1084
1085impl SpecMon {
1086    fn round(&mut self, dt_ms: f64, produced: usize) {
1087        self.n += 1;
1088        if self.n == 1 {
1089            return; // round 1 pays the batch scratch and the draft mirror
1090        }
1091        let a = if self.n == 2 { 1.0 } else { 0.3 };
1092        self.round_ms += a * (dt_ms - self.round_ms);
1093        self.tokens += a * (produced as f64 - self.tokens);
1094    }
1095    fn pays(&self) -> bool {
1096        if self.plain_ms > 0.0 {
1097            self.tokens * self.plain_ms > self.round_ms * 1.03
1098        } else {
1099            self.metal && self.tokens >= SPEC_PROXY_TOKENS
1100        }
1101    }
1102    /// Has the plain phase timed enough tokens to decide?
1103    fn plain_done(&self, t0: std::time::Instant, gen0: usize, generated: usize) -> bool {
1104        let n = generated.saturating_sub(gen0);
1105        if n >= 8 {
1106            return true;
1107        }
1108        self.metal && n >= 2 && t0.elapsed().as_secs_f64() * 1e3 >= SPEC_PLAIN_MIN_MS
1109    }
1110}
1111
1112/// Ids of the consumed prefix the bounded reuse key remembers literally
1113/// (the rest is covered by the rolling hash).
1114pub const KV_PREFIX_TAIL: usize = 128;
1115
1116/// Bounded prefix-reuse key: how many ids the cache holds, a rolling
1117/// hash of ALL of them and the last [`KV_PREFIX_TAIL`] ids literally.
1118/// Answers "does this prompt strictly extend what is cached" exactly
1119/// (hash over the whole consumed prefix + literal tail) without keeping
1120/// the dialogue — the record is a fixed size whatever the session length.
1121#[derive(Debug, Clone, Default)]
1122pub struct KvPrefix {
1123    len: usize,
1124    hash: u64,
1125    tail: Vec<u32>,
1126    /// Owner of the state this key describes: the resident device graph
1127    /// (true) or the host. A turn continues the prefix only on its owner
1128    /// (R4: a host continuation of a device sequence reads an empty host
1129    /// state; a device continuation of a host sequence has no image).
1130    device: bool,
1131}
1132
1133impl KvPrefix {
1134    #[inline]
1135    fn fold(mut h: u64, ids: &[u32]) -> u64 {
1136        for &id in ids {
1137            h ^= id as u64;
1138            h = h.wrapping_mul(0x100000001b3);
1139            h ^= h >> 29;
1140        }
1141        h
1142    }
1143
1144    pub fn clear(&mut self) {
1145        self.len = 0;
1146        self.hash = 0xcbf29ce484222325;
1147        self.tail.clear();
1148        self.device = false;
1149    }
1150
1151    /// Was the prefix built on the resident device graph?
1152    pub fn on_device(&self) -> bool {
1153        self.device
1154    }
1155
1156    /// Tag the owner of the recorded prefix.
1157    pub fn set_on_device(&mut self, device: bool) {
1158        self.device = device;
1159    }
1160
1161    /// Ids the cache holds (the forwarded prefix).
1162    pub fn len(&self) -> usize {
1163        self.len
1164    }
1165
1166    pub fn is_empty(&self) -> bool {
1167        self.len == 0
1168    }
1169
1170    /// Literal tail currently kept (≤ `KV_PREFIX_TAIL`).
1171    pub fn tail_len(&self) -> usize {
1172        self.tail.len()
1173    }
1174
1175    /// Replace the key with `ids` (a fresh sequence).
1176    pub fn set(&mut self, ids: &[u32]) {
1177        self.clear();
1178        self.extend(ids);
1179    }
1180
1181    /// Append `more` to the consumed prefix (an extension-only turn).
1182    pub fn extend(&mut self, more: &[u32]) {
1183        if self.len == 0 && self.hash == 0 {
1184            self.hash = 0xcbf29ce484222325;
1185        }
1186        self.hash = Self::fold(self.hash, more);
1187        self.len += more.len();
1188        if more.len() >= KV_PREFIX_TAIL {
1189            self.tail.clear();
1190            self.tail.extend_from_slice(&more[more.len() - KV_PREFIX_TAIL..]);
1191        } else {
1192            let drop = (self.tail.len() + more.len()).saturating_sub(KV_PREFIX_TAIL);
1193            self.tail.drain(..drop);
1194            self.tail.extend_from_slice(more);
1195        }
1196    }
1197
1198    /// Cached positions when `ids` strictly extends the consumed prefix,
1199    /// 0 otherwise. The tail is compared literally first (cheap), then
1200    /// the hash over the whole prefix must agree.
1201    pub fn extension(&self, ids: &[u32]) -> usize {
1202        if self.len == 0 || ids.len() <= self.len {
1203            return 0;
1204        }
1205        let t = self.tail.len();
1206        if ids[self.len - t..self.len] != self.tail[..] {
1207            return 0;
1208        }
1209        if Self::fold(0xcbf29ce484222325, &ids[..self.len]) != self.hash {
1210            return 0;
1211        }
1212        self.len
1213    }
1214}
1215
1216/// Result of a generation call.
1217pub struct GenerateResult {
1218    pub text: String,
1219    pub token_ids: Vec<u32>,
1220    pub prompt_tokens: usize,
1221    pub tokens_generated: usize,
1222    pub finish_reason: String,
1223    /// Speculative-decode stats (0/0 when MTP is absent or inactive).
1224    pub mtp_drafted: usize,
1225    pub mtp_accepted: usize,
1226    /// Per-generated-token confidence = softmax probability of the token
1227    /// that was actually emitted (softmax probability on the chosen state). High =
1228    /// the model was sure; low = it was guessing. Same length as the
1229    /// generated slice of `token_ids`.
1230    pub token_confidence: Vec<f32>,
1231    /// Structured per-token telemetry (B4 channel). Empty unless
1232    /// `set_trace(true)`; otherwise same length as the generated slice.
1233    pub traces: Vec<TokenTrace>,
1234}
1235
1236/// One row of the structured telemetry trace (B4): the model's internal
1237/// routing state at the moment a token was emitted. Every field is a
1238/// quantity the runtime already computes — nothing is inferred or
1239/// estimated (anti-principle: only measured bytes).
1240#[derive(Clone, Debug)]
1241pub struct TokenTrace {
1242    /// 0-based index within the generated slice.
1243    pub t: usize,
1244    /// The emitted token id.
1245    pub token_id: u32,
1246    /// Softmax probability on the emitted token — how sure the model was.
1247    pub confidence: f32,
1248    /// Skill in force while this token was generated (None = backbone).
1249    pub active_skill: Option<String>,
1250    /// Recon error E = ‖r−BBᵀr‖²/‖φ‖² at the last routing eval — coherence
1251    /// with the active skill's subspace (low = coherent). None = no router
1252    /// or not yet evaluated.
1253    pub recon: Option<f32>,
1254    /// The router changed the active skill right after this token (a
1255    /// domain boundary crossed under the hysteresis barrier).
1256    pub switched: bool,
1257}
1258
1259/// Calibrated softmax probability of `id` under `logits` (the confidence on
1260/// the emitted token) — the confidence signal, cheap from logits already
1261/// computed for sampling. `temp` is the calibration temperature (B1):
1262/// softmax(logits / temp); 1.0 = raw.
1263#[cfg_attr(not(test), allow(dead_code))]
1264fn top1_prob_t(logits: &[f32], id: u32, temp: f32) -> f32 {
1265    let t = if temp > 1e-3 { temp } else { 1.0 };
1266    let max = logits.iter().fold(f32::NEG_INFINITY, |m, &v| m.max(v));
1267    let sum: f32 = logits.iter().map(|&v| ((v - max) / t).exp()).sum();
1268    if sum > 0.0 {
1269        (((logits[id as usize] - max) / t).exp()) / sum
1270    } else {
1271        0.0
1272    }
1273}
1274
1275/// prefill-GEMM enabled? (CMF_PREFILL=seq — emergency fallback to the
1276/// sequential path.)
1277fn prefill_batched() -> bool {
1278    std::env::var("CMF_PREFILL")
1279        .map(|v| v != "seq")
1280        .unwrap_or(true)
1281}
1282
1283/// Decide the graph NLL route without conflating graph quality with the
1284/// optional native-Metal fused head. A hidden-state graph remains a valid
1285/// quality route on Vulkan/Wgpu; only native Metal requires graph logits.
1286#[inline]
1287fn nll_graph_policy(
1288    unmasked: bool,
1289    prefer_graph: bool,
1290    native_metal: bool,
1291) -> (bool, bool) {
1292    let graph_quality = unmasked && prefer_graph;
1293    let fused_head_quality = graph_quality && native_metal;
1294    (graph_quality, fused_head_quality)
1295}
1296
1297/// Input to the layer-major batched span walk: token ids (embeds itself,
1298/// full-stack and coordinator prefill) or ready boundary hiddens (the
1299/// network worker's side of a split).
1300#[derive(Clone, Copy)]
1301enum PrefillIn<'a> {
1302    Ids(&'a [u32]),
1303    Hidden(&'a [f32]),
1304}
1305
1306/// The batched prefill walks `weights.layers`. Architectures that load
1307/// their own stack (gemma-3n's AltUp replicas, DeepSeek-V4's hyper-
1308/// connections) leave that empty and must go position by position — asking
1309/// otherwise indexes an empty vector, which is a panic rather than a
1310/// fallback. Every call site goes through here so the next such
1311/// architecture is one line, not four.
1312impl Pipeline {
1313    fn can_prefill_batched(&self) -> bool {
1314        #[cfg(test)]
1315        let force_serial = self.nll_test_force_serial;
1316        #[cfg(not(test))]
1317        let force_serial = false;
1318        prefill_batched() && !force_serial && !self.weights.layers.is_empty()
1319    }
1320
1321    /// The backend's automatic capacity split for a mapped transformer.
1322    /// Kept as a method so prefill and decode use the exact same boundary.
1323    fn automatic_gpu_prefix(&self) -> Option<usize> {
1324        let (model, _, _, _) = self.weights.embed_tokens.graph_weight()?;
1325        crate::gpu::automatic_layer_prefix(&model, self.num_layers, self.physical_layers)
1326    }
1327
1328    /// Positions per batched pass of the layer-stack prefill for THIS
1329    /// model on THIS backend (see [`prefill_chunk_rule`]). Pub: the network
1330    /// split must chunk exactly like the local path to reproduce it.
1331    pub fn prefill_chunk(&self) -> usize {
1332        let env = env_prefill_chunk();
1333        if env.is_some() || ChunkHost::here() != ChunkHost::Other {
1334            return prefill_chunk_rule(env, ChunkHost::here(), false);
1335        }
1336        prefill_chunk_rule(None, ChunkHost::Other, self.chunk_stack_facts().dense_on_discrete())
1337    }
1338
1339    fn chunk_stack_facts(&self) -> ChunkStackFacts {
1340        let plain_dense = !self.weights.layers.is_empty()
1341            && self.g3n.is_none()
1342            && self.dsv4.is_none()
1343            && self.dsv41.is_none()
1344            && self.qwen4_exp.is_none()
1345            && self.weights.layers.iter().all(|lw| {
1346                matches!(lw.attn, AttnKind::Full { .. }) && matches!(lw.ffn, FfnKind::Dense(_))
1347            });
1348        let gpu_on = crate::gpu::enabled();
1349        ChunkStackFacts {
1350            plain_dense,
1351            discrete: gpu_on && crate::gpu::discrete(),
1352            gpu_on,
1353            // Only asked when the rest already qualifies: it opens the
1354            // backend's capacity plan.
1355            capacity_split: std::env::var_os("CMF_GPU_LAYERS").is_some()
1356                || (plain_dense && gpu_on && self.automatic_gpu_prefix().is_some()),
1357            multi_gpu: self.gpu_plan.is_some(),
1358            o1: self.o1_active(),
1359        }
1360    }
1361}
1362
1363/// Prefill chunk (positions per batched pass), model-agnostic form. On
1364/// macOS the AMX GEMM path wants tall panels — M=48 starves the matrix
1365/// units (ggml uses ubatch 512); elsewhere the historical 48 stays.
1366/// CMF_PREFILL_CHUNK overrides. The architectures with their own stacks
1367/// (DeepSeek-V4/V4.1) chunk with this; the layer-stack prefill asks
1368/// [`Pipeline::prefill_chunk`], which also knows the model and the card.
1369/// A different chunk is a different (equally valid) generation: panel
1370/// width reorders float accumulation.
1371pub fn prefill_chunk() -> usize {
1372    prefill_chunk_rule(env_prefill_chunk(), ChunkHost::here(), false)
1373}
1374
1375fn env_prefill_chunk() -> Option<usize> {
1376    std::env::var("CMF_PREFILL_CHUNK")
1377        .ok()
1378        .and_then(|v| v.parse::<usize>().ok())
1379}
1380
1381/// The host classes the chunk width distinguishes.
1382#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1383enum ChunkHost {
1384    Macos,
1385    /// Linux/Android aarch64 (phones, SBCs).
1386    Aarch64,
1387    /// Everything else: x86-64 Linux/Windows, CPU or Vulkan/DX12.
1388    Other,
1389}
1390
1391impl ChunkHost {
1392    fn here() -> Self {
1393        if cfg!(target_os = "macos") {
1394            ChunkHost::Macos
1395        } else if cfg!(target_arch = "aarch64") {
1396            ChunkHost::Aarch64
1397        } else {
1398            ChunkHost::Other
1399        }
1400    }
1401}
1402
1403/// Chunk for a plain dense stack whose every layer lives on a discrete
1404/// card. On x86 the layer-stack prefill is host-driven: each GEMM and the
1405/// chunk attention (which re-uploads the whole KV prefix per layer) is a
1406/// separate submit + readback, so 48 positions a pass left the card idle
1407/// between them. Measured in-process on an RTX 3090 (Vulkan), 2048-token
1408/// prompt — see CHANGELOG 0.7.6 for the table.
1409const DISCRETE_DENSE_PREFILL_CHUNK: usize = 512;
1410
1411/// The chunk-width rule. `dense_on_discrete` is true only for a plain
1412/// dense transformer (full attention, dense FFN, no special stack) that
1413/// is entirely resident on one discrete card — the one case measured
1414/// here. GDN hybrids, MoE, DeepSeek stacks, capacity-split and CPU-only
1415/// runs keep the width they were tuned with.
1416fn prefill_chunk_rule(env: Option<usize>, host: ChunkHost, dense_on_discrete: bool) -> usize {
1417    if let Some(n) = env {
1418        return n.max(1);
1419    }
1420    match host {
1421        ChunkHost::Macos => 512,
1422        // Mobile: big enough to feed the batched attend (gate b ≥ 32)
1423        // and the blocked SDOT GEMM without the memory of 512.
1424        ChunkHost::Aarch64 => 256,
1425        ChunkHost::Other if dense_on_discrete => DISCRETE_DENSE_PREFILL_CHUNK,
1426        ChunkHost::Other => 48,
1427    }
1428}
1429
1430/// What the chunk rule needs to know about a loaded stack.
1431#[derive(Clone, Copy, Debug, Default)]
1432struct ChunkStackFacts {
1433    /// Every layer is `AttnKind::Full` + `FfnKind::Dense`, and no
1434    /// architecture-owned stack (g3n, DeepSeek-V4/V4.1, qwen4-exp) is set.
1435    plain_dense: bool,
1436    /// The active GPU backend is a discrete card.
1437    discrete: bool,
1438    /// The backend is up and not paused.
1439    gpu_on: bool,
1440    /// A capacity-derived device prefix: some layers run on the host.
1441    capacity_split: bool,
1442    /// An in-process multi-GPU plan is set.
1443    multi_gpu: bool,
1444    /// O(1) layers (their Q trace is recorded by the prefill).
1445    o1: bool,
1446}
1447
1448impl ChunkStackFacts {
1449    fn dense_on_discrete(self) -> bool {
1450        self.plain_dense
1451            && self.discrete
1452            && self.gpu_on
1453            && !self.capacity_split
1454            && !self.multi_gpu
1455            && !self.o1
1456    }
1457}
1458
1459/// Number of prompt rows that have a real teacher-forced next-token pair in a
1460/// prefill span.  The final prompt row has no successor token, so it must not
1461/// be handed to the MTP warm-up.  Keeping this arithmetic in one helper makes
1462/// the full-chunk and tail-chunk boundaries explicit for both the graph and
1463/// CPU implementations.
1464#[inline]
1465fn mtp_prefill_pair_count(start: usize, end: usize, input_len: usize) -> usize {
1466    if end <= start || start >= input_len {
1467        return 0;
1468    }
1469    let rows = (end.min(input_len) - start).min(input_len - start);
1470    if end < input_len {
1471        rows
1472    } else {
1473        rows.saturating_sub(1)
1474    }
1475}
1476
1477/// Callback for streaming tokens. Return `false` to cancel.
1478pub type TokenCallback = Box<dyn FnMut(&str) -> bool + Send>;
1479
1480/// One layer's cache ownership at a cross-turn KV reuse boundary.
1481#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1482pub(crate) struct ReuseLayer {
1483    /// Exact-attention layer (rows in `LayerKvCache`); otherwise a
1484    /// recurrent / latent mixer whose state cannot be rewound.
1485    pub full: bool,
1486    /// Positions the host owner cache reaches (`pos_len`: a trimmed
1487    /// sliding tail stores only the newest of them).
1488    pub host_rows: usize,
1489    /// Rows the wgpu token graph's device mirror holds (None: no mirror).
1490    pub device_rows: Option<usize>,
1491    /// A recurrent state lives on the device (advanced past the host copy).
1492    pub device_state: bool,
1493}
1494
1495/// What a reused turn must do before its tail prefill runs on the HOST.
1496#[derive(Debug, Clone, PartialEq, Eq)]
1497pub(crate) enum ReusePlan {
1498    /// Host caches already hold exactly the reused prefix.
1499    Ready,
1500    /// Copy device mirror rows `[from..to)` into the host cache of each
1501    /// listed layer (the rows decode wrote on the device only).
1502    Pull(Vec<(usize, usize, usize)>),
1503    /// The prefix cannot be continued on the host exactly: start fresh.
1504    Fresh,
1505}
1506
1507/// The wgpu whole-token graph decodes into a DEVICE K/V mirror and never
1508/// writes those rows back to the host cache, while the chunked prefill of a
1509/// pure-attention model reads (and appends to) the host cache. A reused turn
1510/// therefore found its host cache ending at the previous PROMPT, not at the
1511/// previous answer: the tail prefill attended without the model's own
1512/// answer and appended its rows at the wrong index (MiniCPM5 on Vulkan
1513/// repeated its tool call instead of reading the tool result). Every layer
1514/// must hold exactly `reuse_from` host rows before the host continues; rows
1515/// that exist only on the device are pulled back, anything else is fresh.
1516pub(crate) fn kv_reuse_plan(reuse_from: usize, layers: &[ReuseLayer]) -> ReusePlan {
1517    let mut pulls = Vec::new();
1518    for (li, l) in layers.iter().enumerate() {
1519        if !l.full {
1520            if l.device_state {
1521                return ReusePlan::Fresh;
1522            }
1523            continue;
1524        }
1525        if l.host_rows == reuse_from {
1526            continue;
1527        }
1528        if l.host_rows < reuse_from && l.device_rows.is_some_and(|d| d >= reuse_from) {
1529            pulls.push((li, l.host_rows, reuse_from));
1530            continue;
1531        }
1532        return ReusePlan::Fresh;
1533    }
1534    if pulls.is_empty() {
1535        ReusePlan::Ready
1536    } else {
1537        ReusePlan::Pull(pulls)
1538    }
1539}
1540
1541impl Pipeline {
1542    /// Clear all per-sequence state, including backend device mirrors.
1543    ///
1544    /// The host KV/history buffers are only half of the request lifecycle on
1545    /// wgpu: GDN/O(1) state and cached graph bind groups are keyed by the
1546    /// pipeline id and otherwise survive a pooled request.  Keep every fresh
1547    /// sequence entry point on this one reset path so a new request cannot
1548    /// inherit the prior request's device state.
1549    fn clear_sequence_state(&mut self) {
1550        // a replay still writing the GDN owners must land before they are
1551        // cleared or reallocated (the device holds raw pointers to them)
1552        #[cfg(target_os = "macos")]
1553        let _ = crate::gpu_metal::wait_replay();
1554        self.kv_cache.clear();
1555        // Both reuse keys (the legacy `kv_history` and the bounded
1556        // `kv_prefix`) describe the state being dropped here.
1557        self.clear_history();
1558        self.graph_logits = None;
1559        if let Some(b) = &mut self.dsv41 {
1560            b.3.clear();
1561        }
1562        crate::gpu::graph_kv_reset(self.graph_kv_id);
1563        // MTP is detached from `self` for the duration of generation, so its
1564        // device mirror is not covered by the trunk reset above.  Reset the
1565        // derived id as well: a failed/aborted warm-up must never leave a
1566        // mirror that a later request can mistake for a current MTP cache.
1567        crate::gpu::graph_kv_reset(self.mtp_kv_id());
1568    }
1569
1570    /// Make the host caches own exactly the reused prefix `[0..reuse_from)`
1571    /// before a reused turn's tail prefill runs on the host (see
1572    /// [`kv_reuse_plan`]). Returns false when the prefix cannot be continued
1573    /// exactly — the caller then starts a fresh sequence. A model whose
1574    /// prefill runs through the token graph keeps its device state as the
1575    /// authority and is left untouched.
1576    fn prepare_kv_reuse(&mut self, reuse_from: usize) -> bool {
1577        if self.graph_prefill_preferred() {
1578            return true;
1579        }
1580        let kv_id = self.graph_kv_id;
1581        let layers: Vec<ReuseLayer> = (0..self.num_layers)
1582            .map(|li| {
1583                let full = matches!(
1584                    self.weights.layers[self.phys_layer(li)].attn,
1585                    AttnKind::Full { .. }
1586                );
1587                ReuseLayer {
1588                    full,
1589                    // Absolute depth: a trimmed sliding tail stores fewer
1590                    // rows than the positions it has seen.
1591                    host_rows: self.kv_cache.layers[li].pos_len(),
1592                    device_rows: crate::gpu::graph_kv_stored(kv_id, li),
1593                    device_state: crate::gpu::graph_state_resident(kv_id, li),
1594                }
1595            })
1596            .collect();
1597        // No wgpu device state at all (CPU, Metal — whose graph appends every
1598        // decoded row to the owner cache itself): the host is the owner and
1599        // the extension check already proved the prefix.
1600        if layers
1601            .iter()
1602            .all(|l| l.device_rows.is_none() && !l.device_state)
1603        {
1604            return true;
1605        }
1606        let plan = kv_reuse_plan(reuse_from, &layers);
1607        let (what, rows, n) = match &plan {
1608            ReusePlan::Ready => ("host ready", 0, 0),
1609            ReusePlan::Fresh => ("fresh", 0, 0),
1610            ReusePlan::Pull(p) => (
1611                "pull",
1612                p.iter().map(|&(_, a, b)| b - a).max().unwrap_or(0),
1613                p.len(),
1614            ),
1615        };
1616        let t0 = std::time::Instant::now();
1617        let ok = self.apply_kv_reuse_plan(reuse_from, plan, &layers);
1618        if std::env::var("CMF_PREFILL_PROF").is_ok() {
1619            eprintln!(
1620                "kv-reuse: {what}{}: {rows} device row(s) × {n} layer(s) to the host in {:.2} ms",
1621                if ok { "" } else { " (failed → fresh)" },
1622                t0.elapsed().as_secs_f64() * 1e3
1623            );
1624        }
1625        ok
1626    }
1627
1628    fn apply_kv_reuse_plan(
1629        &mut self,
1630        reuse_from: usize,
1631        plan: ReusePlan,
1632        layers: &[ReuseLayer],
1633    ) -> bool {
1634        let kv_id = self.graph_kv_id;
1635        match plan {
1636            ReusePlan::Fresh => return false,
1637            ReusePlan::Ready => {}
1638            ReusePlan::Pull(pulls) => {
1639                // Mirrors of one uniform geometry: one batched read serves
1640                // every layer. Per-layer geometry (MiMo-V2: 4/8 KV heads,
1641                // narrow V, sliding rings) is read layer by layer in the
1642                // host layout instead.
1643                let (nkv, hd) = {
1644                    let c = &self.kv_cache.layers[pulls[0].0];
1645                    (c.num_kv_heads, c.head_dim)
1646                };
1647                let uniform = pulls.iter().all(|&(li, _, _)| {
1648                    let c = &self.kv_cache.layers[li];
1649                    (c.num_kv_heads, c.head_dim) == (nkv, hd)
1650                });
1651                let batched = if uniform {
1652                    crate::gpu::graph_kv_read_rows(kv_id, &pulls, nkv, hd)
1653                } else {
1654                    None
1655                };
1656                let rows: Vec<(Vec<f32>, Vec<f32>)> = match batched {
1657                    Some(rows) => rows,
1658                    None => {
1659                        let mut rows = Vec::with_capacity(pulls.len());
1660                        for &(li, from, to) in &pulls {
1661                            let (lnkv, lhd) = {
1662                                let c = &self.kv_cache.layers[li];
1663                                (c.num_kv_heads, c.head_dim)
1664                            };
1665                            let Some((k, v, first_valid)) =
1666                                crate::gpu::graph_kv_pull_host(kv_id, li, from, to, lnkv, lhd)
1667                            else {
1668                                return false;
1669                            };
1670                            // The host continues at `to`: a sliding layer
1671                            // reads back only its last window, a full one
1672                            // every row it lacks.
1673                            let need_from = match self.layer_window(li) {
1674                                Some(w) => from.max((to + 1).saturating_sub(w)),
1675                                None => from,
1676                            };
1677                            if first_valid > need_from {
1678                                return false;
1679                            }
1680                            rows.push((k, v));
1681                        }
1682                        rows
1683                    }
1684                };
1685                for ((li, from, to), (k, v)) in pulls.into_iter().zip(rows) {
1686                    let cache = &mut self.kv_cache.layers[li];
1687                    let row = cache.num_kv_heads * cache.head_dim;
1688                    for p in 0..to - from {
1689                        cache.append(&k[p * row..(p + 1) * row], &v[p * row..(p + 1) * row], &[]);
1690                    }
1691                    if cache.pos_len() != to {
1692                        return false;
1693                    }
1694                }
1695            }
1696        }
1697        // A mirror past the prefix (a greedy burst that ran beyond the stop)
1698        // holds rows of the OLD continuation: rewind it so the next graph
1699        // token re-syncs those positions from the host.
1700        for (li, l) in layers.iter().enumerate() {
1701            if l.full
1702                && l.device_rows.is_some_and(|d| d > reuse_from)
1703                && !crate::gpu::graph_kv_set_stored(kv_id, li, reuse_from)
1704            {
1705                return false;
1706            }
1707        }
1708        true
1709    }
1710
1711    /// Finish a generation lifecycle after the MTP/router owners were
1712    /// detached.  Every terminal path must put those owners back before the
1713    /// pooled pipeline can serve another request.  Graph side channels and
1714    /// device mirrors are cleared on errors and cancellations; a successful
1715    /// generation keeps its decode-ready host cache for KV reuse.
1716    fn finish_generation(
1717        &mut self,
1718        mtp: &mut Option<MtpModule>,
1719        router: &mut Option<crate::swarm::DynRouter>,
1720        clear_sequence: bool,
1721    ) {
1722        // A dynamic route may have switched the overlay before the terminal
1723        // path. Restore the backbone while the detached router is still
1724        // available, because set_active_skill also owns the overlay reset.
1725        if router.is_some() {
1726            let _ = self.set_active_skill(None);
1727        }
1728        // The last speculative round's replay may still be in flight on
1729        // the second queue: whoever reads the host cache after generate()
1730        // returns (session export, the network split's KV wire, a KV
1731        // reuse) must see the final states.
1732        // A replay that failed leaves the GDN owners half-written: fail
1733        // closed and drop the sequence instead of handing the cache on.
1734        #[cfg(target_os = "macos")]
1735        let clear_sequence = clear_sequence || !crate::gpu_metal::wait_replay();
1736        if clear_sequence {
1737            self.clear_sequence_state();
1738            if let Some(m) = mtp.as_mut() {
1739                // The MTP owner is detached while generation runs, so the
1740                // trunk reset above cannot clear its host cache.  Drop its
1741                // partial rows before reattaching it to the pooled pipeline;
1742                // the next request must start from the same empty anchor on
1743                // CPU and on the device mirror.
1744                m.kv.clear();
1745            }
1746            if let Some(m) = self.mtp.as_mut() {
1747                // A non-speculative request leaves the configured MTP owner
1748                // attached.  Clear that dormant cache too when a shared
1749                // generation failure/cancellation resets the sequence.
1750                m.kv.clear();
1751            }
1752        }
1753        self.graph_want_logits = false;
1754        self.graph_head_required = false;
1755        self.graph_logits = None;
1756        self.graph_failed
1757            .store(false, std::sync::atomic::Ordering::Relaxed);
1758        self.cancel
1759            .store(false, std::sync::atomic::Ordering::Relaxed);
1760        self.dyn_router = router.take().or(self.dyn_router.take());
1761        self.mtp = mtp.take().or(self.mtp.take());
1762        self.mtp_graph_mode = None;
1763        self.spec_forced = None;
1764    }
1765
1766    /// Consume a graph failure reported by a forward that returns only a
1767    /// hidden vector.  `forward_ids` is a public Result API, so it must not
1768    /// turn the graph's zero hidden sentinel into a valid lm_head result.
1769    fn check_forward_graph(&mut self, phase: &str, pos: usize) -> Result<(), String> {
1770        if self
1771            .graph_failed
1772            .swap(false, std::sync::atomic::Ordering::Relaxed)
1773        {
1774            self.cancel
1775                .store(false, std::sync::atomic::Ordering::Relaxed);
1776            self.clear_sequence_state();
1777            self.graph_logits = None;
1778            self.graph_want_logits = false;
1779            self.graph_head_required = false;
1780            return Err(format!("GPU graph failed during {phase} at position {pos}"));
1781        }
1782        Ok(())
1783    }
1784
1785    #[cfg(target_os = "macos")]
1786    fn fail_metal_graph(&mut self, reason: &str) {
1787        crate::pipeline::METAL_GRAPH_ERRORS
1788            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1789        self.clear_sequence_state();
1790        self.graph_logits = None;
1791        self.graph_failed
1792            .store(true, std::sync::atomic::Ordering::Relaxed);
1793        self.cancel
1794            .store(true, std::sync::atomic::Ordering::Relaxed);
1795        tracing::error!("native Metal TokenGraph failed closed: {reason}");
1796    }
1797
1798    /// Start an NLL/PPL request with all graph side channels in a known
1799    /// state.  A graph failure also raises the cooperative cancel bit; it is
1800    /// consumed here and that graph-induced bit is cleared so an independent
1801    /// request can be reused.  A caller-owned cancellation remains intact.
1802    fn nll_begin(&mut self) -> Result<(), String> {
1803        if self
1804            .graph_failed
1805            .swap(false, std::sync::atomic::Ordering::Relaxed)
1806        {
1807            self.cancel
1808                .store(false, std::sync::atomic::Ordering::Relaxed);
1809            self.clear_sequence_state();
1810            self.graph_logits = None;
1811            self.graph_want_logits = false;
1812            self.graph_head_required = false;
1813            return Err("GPU graph failed before NLL scoring".to_string());
1814        }
1815        self.clear_sequence_state();
1816        self.graph_logits = None;
1817        self.graph_want_logits = false;
1818        self.graph_head_required = false;
1819        Ok(())
1820    }
1821
1822    /// End an NLL/PPL request, including the side channels that are not part
1823    /// of the host KV cache.  This is intentionally explicit instead of
1824    /// relying on a tuple/sentinel return: callers must see every failure.
1825    fn nll_end(&mut self) {
1826        self.clear_sequence_state();
1827        self.graph_logits = None;
1828        self.graph_want_logits = false;
1829        self.graph_head_required = false;
1830        self.graph_failed
1831            .store(false, std::sync::atomic::Ordering::Relaxed);
1832    }
1833
1834    /// Check the graph failure channel at a scoring boundary and leave the
1835    /// pipeline reusable when the device path failed.
1836    fn nll_check_graph(&mut self, phase: &str, pos: usize) -> Result<(), String> {
1837        #[cfg(test)]
1838        if self.nll_test_fail_at == Some(pos) {
1839            self.nll_test_fail_at = None;
1840            self.graph_failed
1841                .store(true, std::sync::atomic::Ordering::Relaxed);
1842            self.cancel
1843                .store(true, std::sync::atomic::Ordering::Relaxed);
1844        }
1845        if self
1846            .graph_failed
1847            .swap(false, std::sync::atomic::Ordering::Relaxed)
1848        {
1849            self.cancel
1850                .store(false, std::sync::atomic::Ordering::Relaxed);
1851            self.clear_sequence_state();
1852            self.graph_logits = None;
1853            self.graph_want_logits = false;
1854            return Err(format!(
1855                "GPU graph failed during NLL {phase} at position {pos}"
1856            ));
1857        }
1858        Ok(())
1859    }
1860
1861    /// Map a virtual layer index to its physical weight index.
1862    /// Looped Transformer (Nanbeige 4.2): 22 physical layers × 2 loops = 44 virtual;
1863    /// virtual layer 23 maps back to physical layer 1 (23 % 22 = 1).
1864    #[inline]
1865    pub fn phys_layer(&self, virtual_idx: usize) -> usize {
1866        virtual_idx % self.physical_layers
1867    }
1868
1869    /// True when `virtual_idx` is the last layer of a loop iteration
1870    /// (used for loop_final_norm insertion).
1871    #[inline]
1872    pub fn is_loop_end(&self, virtual_idx: usize) -> bool {
1873        self.loop_final_norm && (virtual_idx + 1) % self.physical_layers == 0
1874    }
1875
1876    /// Build a pipeline from parts (used by the loader and tests).
1877    #[allow(clippy::too_many_arguments)]
1878
1879    /// Whole-block q1 token graph on the GPU (macOS/Metal): the run of
1880    /// consecutive q1 layers — GDN *and* full attention — starting at
1881    /// `start` executes as few command buffers as the CPU truly needs.
1882    /// Hidden stays device-resident across every layer; the only syncs
1883    /// are before each CPU attend (it needs q/k/v and owns the KV
1884    /// cache) and the final hidden readback. Recurrent states
1885    /// round-trip through shared memory (the CPU stays their owner, so
1886    /// every other path remains coherent). Returns the first layer
1887    /// index NOT covered (== `start` → refused, caller falls through
1888    /// to the per-layer CPU path).
1889    /// Should prefill run position-by-position through the GPU token
1890    /// graph instead of the batched CPU chunk-GEMM? True for q1 GDN
1891    /// hybrids on native Metal: their chunk prefill is walled by the
1892    /// sequential scalar recurrence, so the graph's decode rate wins.
1893    /// NOT for Looped Transformers, despite the per-chunk loop_final_norm
1894    /// sync: the chunk-GEMM amortizes each weight over the whole chunk,
1895    /// which the per-position graph cannot (Nanbeige 4.2 on M4, 512-token
1896    /// prompt: 85 tok/s chunked vs 14 through the graph).
1897    #[cfg(target_os = "macos")]
1898    fn graph_prefill_preferred(&self) -> bool {
1899        let graph_force = crate::gpu::q1_force() || crate::gpu::q2tp_gpu_opt_in();
1900        if !crate::gpu::enabled_here()
1901            || !graph_force
1902            || std::env::var("CMF_GPU_BLOCK")
1903                .map(|v| v == "0")
1904                .unwrap_or(false)
1905            // CMF_PREFILL_GRAPH=0: the chunked prefill (GEMM projections,
1906            // CPU recurrence) instead of the per-position token graph.
1907            || std::env::var("CMF_PREFILL_GRAPH").as_deref() == Ok("0")
1908        {
1909            return false;
1910        }
1911        self.weights
1912            .layers
1913            .iter()
1914            .any(|lw| {
1915                matches!(&lw.attn, AttnKind::LinearGdn(w) if w.in_proj_qkv.metal_graph_parts().is_some())
1916            })
1917    }
1918
1919    /// Prompt ingest through the batched wgpu graph in device-prefix mode:
1920    /// a MoE stack that does not fit the card runs each chunk's leading
1921    /// layers on the device (experts resident) and the rest on the host's
1922    /// batched walk. On by default for models with per-layer attention
1923    /// geometry (MiMo-V2 — its measured default); `CMF_BATCH_PREFIX=1`
1924    /// opts any other MoE model in, `=0` keeps the chunked host prefill.
1925    #[cfg(not(target_os = "macos"))]
1926    fn batch_prefix_prefill(&self) -> bool {
1927        let forced = match std::env::var("CMF_BATCH_PREFIX").as_deref() {
1928            Ok("0") => return false,
1929            Ok("1") => true,
1930            _ => false,
1931        };
1932        crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Prefill)
1933            && crate::gpu::enabled_here()
1934            && !self.graph_refused()
1935            && (forced || self.graph_attn_decline_reason().is_some())
1936            && self.wgpu_graph_attn_decline().is_none()
1937            && self.attn_softcap == 0.0
1938            && self
1939                .weights
1940                .layers
1941                .iter()
1942                .any(|lw| matches!(&lw.ffn, FfnKind::Moe(_)))
1943            && self.automatic_gpu_prefix().is_some()
1944    }
1945
1946    /// Gemma-4's prompt through the batched wgpu graph (`gemma_batch`): a
1947    /// dense stack wholly on a discrete card, whose token graph already
1948    /// decodes it. Not `graph_prefill_preferred` — the host stays the KV
1949    /// owner for reuse and scoring; only generation's ingest changes.
1950    /// `CMF_G4_BATCH=0` keeps the chunked host prefill.
1951    fn g4_batch_prefill(&self) -> bool {
1952        #[cfg(target_os = "macos")]
1953        {
1954            false
1955        }
1956        #[cfg(not(target_os = "macos"))]
1957        {
1958            (self.global_attn.is_some() || self.attn_v_norm)
1959                && crate::gpu::g4_batch_on()
1960                && crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Prefill)
1961                && crate::gpu::enabled_here()
1962                && crate::gpu::discrete()
1963                && !self.graph_refused()
1964                && !self.o1_active()
1965                && self.attn_softcap == 0.0
1966                && self.wgpu_graph_attn_decline().is_none()
1967                && self.weights.layers.iter().all(|lw| {
1968                    matches!(lw.attn, AttnKind::Full { .. }) && matches!(lw.ffn, FfnKind::Dense(_))
1969                })
1970                && self.automatic_gpu_prefix().is_none()
1971        }
1972    }
1973
1974    /// Gemma-4's batched prefill of `ids` at positions `pos0..`
1975    /// (`g4_batch_prefill`): chunks of `generation_batch_k` rows through the
1976    /// batch graph. Ok((n, rows)): `n` positions ingested; `rows` = the last
1977    /// one's hidden, or every row's (`keep_all`). A refusal stops early
1978    /// (n < ids.len()) after pulling the device rows the host walk needs.
1979    /// Err: a failure after admission — the device state is undefined.
1980    #[cfg(not(target_os = "macos"))]
1981    fn g4_batch_ingest(
1982        &mut self,
1983        ids: &[u32],
1984        pos0: usize,
1985        keep_all: bool,
1986    ) -> Result<(usize, Vec<f32>), String> {
1987        let hs = self.hidden_size;
1988        let chunk = self.generation_batch_k().max(1);
1989        let end_all = pos0 + ids.len();
1990        let mut pos = pos0;
1991        let mut out = Vec::new();
1992        while pos < end_all && !self.cancel.load(std::sync::atomic::Ordering::Relaxed) {
1993            let end = (pos + chunk).min(end_all);
1994            let bk = end - pos;
1995            let mut hiddens = vec![0f32; bk * hs];
1996            for (j, &id) in ids[pos - pos0..end - pos0].iter().enumerate() {
1997                hiddens[j * hs..(j + 1) * hs].copy_from_slice(&self.embed_single(id));
1998            }
1999            let positions: Vec<usize> = (pos..end).collect();
2000            let t_chunk = std::time::Instant::now();
2001            let outcome = self.try_batch_graph_wgpu(&mut hiddens, &positions, bk, None);
2002            if std::env::var("CMF_GRAPH_PROF").is_ok() {
2003                let ms = t_chunk.elapsed().as_secs_f64() * 1000.0;
2004                eprintln!(
2005                    "batch-chunk: phase=prompt mode=gemma4 k={bk} outcome={outcome:?} {ms:.1} ms ({:.1} tok/s)",
2006                    bk as f64 / (ms / 1000.0)
2007                );
2008            }
2009            match outcome {
2010                crate::gpu::BatchGraphOutcome::Completed => {
2011                    if keep_all {
2012                        out.extend_from_slice(&hiddens);
2013                    } else {
2014                        out = hiddens[(bk - 1) * hs..].to_vec();
2015                    }
2016                    pos = end;
2017                }
2018                crate::gpu::BatchGraphOutcome::Failed => {
2019                    return Err("Gemma-4 batched prefill failed after admission".into());
2020                }
2021                crate::gpu::BatchGraphOutcome::Declined => {
2022                    #[cfg(feature = "gpu")]
2023                    if pos > pos0 {
2024                        self.pull_lagging_host_kv(0, self.num_layers, pos);
2025                    }
2026                    break;
2027                }
2028            }
2029        }
2030        Ok((pos - pos0, out))
2031    }
2032
2033    /// Hidden rows of every position of `ids` through Gemma-4's batched
2034    /// prefill from a fresh sequence (the prefill's teacher-forced logits
2035    /// for parity gates: `logits_from_hidden` per row). None when the model
2036    /// or the card does not take that route, or it refused part way.
2037    pub fn g4_prefill_hidden_rows(&mut self, ids: &[u32]) -> Option<Vec<f32>> {
2038        #[cfg(not(target_os = "macos"))]
2039        {
2040            if ids.is_empty() || !self.g4_batch_prefill() {
2041                return None;
2042            }
2043            self.clear_sequence_state();
2044            match self.g4_batch_ingest(ids, 0, true) {
2045                Ok((n, rows)) if n == ids.len() => Some(rows),
2046                _ => {
2047                    self.clear_sequence_state();
2048                    None
2049                }
2050            }
2051        }
2052        #[cfg(target_os = "macos")]
2053        {
2054            let _ = ids;
2055            None
2056        }
2057    }
2058
2059    #[cfg(not(target_os = "macos"))]
2060    fn graph_prefill_preferred(&self) -> bool {
2061        // Discrete-GPU wgpu whole-token graph: GDN layers carry recurrent state
2062        // (conv ring + delta-rule S) resident on the GPU. A batched CPU prefill
2063        // builds that state on the CPU only, leaving the GPU buffers zeroed at
2064        // decode → garbage. Route GDN-hybrid prefill through the graph one
2065        // position at a time so the resident state is seeded exactly as decode
2066        // will read it. Pure-attention models keep the batched CPU prefill (its
2067        // KV mirror re-syncs from the CPU cache, so no seeding gap).
2068        let graph_on = crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Prefill);
2069        if !graph_on || !crate::gpu::enabled_here() {
2070            return false;
2071        }
2072        // Embryo's phase state is device-owned by the resident graph during
2073        // prefill; the batched CPU path would leave decode seeing a zeroed
2074        // device recurrence.  Route the prompt position-by-position too.
2075        if self.embryo_resident_eligible() {
2076            // The resident graph owns the phase state and anchor KV on the
2077            // device.  A prefill-only graph would leave decode on the host
2078            // with no way to import that state, so keep the whole sequence
2079            // on one owner (or use the ordinary CPU prefill/decode pair).
2080            return crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Decode);
2081        }
2082        // The descriptor-aware Prism graph now carries both the FWHT/affine
2083        // transforms and resident GDN state, so it is also the exact prefill
2084        // path for this model.  Keeping it here (rather than falling through
2085        // to the CPU chunk walk) is required for a long prompt to seed the
2086        // same device state that decode consumes.
2087        // O(1) needs the CPU prefill: the q-trace that seals the Nyström
2088        // skeleton is recorded there and nowhere else. The GDN half of
2089        // the hybrid loses nothing — the graph's first decode creates
2090        // its (ring, S) entries seeded from `cpu_state`, the same
2091        // handoff every graph run relies on when the entry is fresh.
2092        // Without this line the two designs collide on hybrids and o1
2093        // never becomes graph-portable: prefill through the graph
2094        // records no trace, so views stay None forever.
2095        if self.o1_active() {
2096            return false;
2097        }
2098        // A model the wgpu graphs decline outright would walk its prompt
2099        // one position at a time through a graph that never runs: the
2100        // batched CPU chunk prefill is the right ingest for it.
2101        if self.wgpu_graph_attn_decline().is_some() {
2102            return false;
2103        }
2104        if self
2105            .weights
2106            .layers
2107            .iter()
2108            .any(|lw| matches!(&lw.attn, AttnKind::LinearGdn(_)))
2109        {
2110            return true;
2111        }
2112        // MoE models too: the chunked CPU prefill runs every expert on the
2113        // host (Hy-MT2-30B-A3B on a Xeon: 8 tok/s of ingest against 53 of
2114        // graph decode), while the token graph — and the batched graph under
2115        // CMF_BATCH_K — keep the experts resident. Full attention in the
2116        // graph writes the KV mirror that decode reads, exactly as it does
2117        // for the hybrids' attention layers. Only when the whole stack is
2118        // resident: with a device prefix the per-position walk finishes
2119        // every token on the host, and the chunked prefill (GEMMs on the
2120        // card, the expert loop batched on the host) is the faster ingest
2121        // (the 8 GB ladder point: 7 tok/s chunked against ~1 walked).
2122        self.weights
2123            .layers
2124            .iter()
2125            .any(|lw| matches!(&lw.ffn, FfnKind::Moe(_)))
2126            && self.automatic_gpu_prefix().is_none()
2127    }
2128
2129    #[cfg(target_os = "macos")]
2130    fn q1_graph_gpu(
2131        &mut self,
2132        start: usize,
2133        upto: Option<usize>,
2134        position: usize,
2135        h: &mut [f32],
2136    ) -> usize {
2137        let _mt0 = std::time::Instant::now(); // CMF_METAL_HOSTPROF
2138        use crate::gpu::{AttnGpuLayer, GdnGpuCfg, GdnGpuLayer, GraphDims, MetalFfn, TokenGraph};
2139        let graph_force = crate::gpu::q1_force() || crate::gpu::q2tp_gpu_opt_in();
2140        if self.attn_softcap > 0.0 // capped scores: no graph kernel — CPU path
2141            || !self.metal_rope_scale_ok()
2142            || !crate::gpu::enabled_here()
2143            || !graph_force
2144            || std::env::var("CMF_GPU_BLOCK")
2145                .map(|v| v == "0")
2146                .unwrap_or(false)
2147        {
2148            if std::env::var("CMF_GRAPH_DBG").is_ok() {
2149                eprintln!(
2150                    "block-graph: front gate (softcap={} enabled_here={} graph_force={})",
2151                    self.attn_softcap > 0.0,
2152                    crate::gpu::enabled_here(),
2153                    graph_force,
2154                );
2155            }
2156            if self.graph_head_required {
2157                self.fail_metal_graph("native graph front gate refused");
2158            }
2159            return start;
2160        }
2161        // The graph encodes SiLU or exact-GELU FFNs and attention with an
2162        // explicit model scale. Sliding-window layers with their own RoPE
2163        // table / rotary width ride it too (`metal_graph_swa`): both the
2164        // device attend and the sandwich's CPU attend read each layer's
2165        // window and table. Sandwich norms and other activations fall back
2166        // to the CPU path.
2167        // Gemma-4 (`metal_gemma4`): its sliding layers ride the same door,
2168        // its global layers, V norm, sandwich norms, layer scalar and
2169        // tanh-GELU the `gpu_metal::gemma4` kernels.
2170        let gemma4 = self.metal_gemma4();
2171        let swa_graph = self.metal_graph_swa() || gemma4;
2172        if (self.swa.is_some() && !swa_graph)
2173            || (self.global_attn.is_some() && !gemma4)
2174            || self.attention_heads_per_layer.is_some()
2175            || (self.attn_v_norm && !gemma4)
2176            // per-layer KV heads, narrow V, learned sinks (MiMo-V2)
2177            || (self.graph_attn_decline_reason().is_some() && !swa_graph)
2178            || self.weights.layers.iter().any(|lw| {
2179                ((lw.attn_out_norm.is_some()
2180                    || lw.ffn_out_norm.is_some()
2181                    || lw.layer_scale.is_some())
2182                    && !gemma4)
2183                    || matches!(&lw.ffn, FfnKind::Dense(d)
2184                        if !(matches!(d.act, Act::Silu | Act::Gelu)
2185                            || (gemma4 && d.act == Act::GeluTanh)))
2186            })
2187        {
2188            // The Metal graphs' device attend has no per-layer attention
2189            // geometry (the wgpu graphs do): say so once, by name.
2190            if let Some(reason) = self.graph_attn_decline_reason().filter(|_| !swa_graph) {
2191                self.note_graph_decline("metal block graph", reason);
2192            }
2193            if std::env::var("CMF_GRAPH_DBG").is_ok() {
2194                eprintln!(
2195                    "block-graph: arch ineligible (swa={} gattn={} hpl={} vnorm={} scale_delta={:.2e})",
2196                    self.swa.is_some(),
2197                    self.global_attn.is_some(),
2198                    self.attention_heads_per_layer.is_some(),
2199                    self.attn_v_norm,
2200                    (self.attn_scale - 1.0 / (self.head_dim as f32).sqrt()).abs(),
2201                );
2202            }
2203            if self.graph_head_required {
2204                self.fail_metal_graph("native graph architecture gate refused");
2205            }
2206            return start;
2207        }
2208        // Looped Transformer: the graph covers ALL loop iterations;
2209        // encode_loop_norm is inserted on-device at each boundary.
2210        let limit = upto
2211            .map(|u| u + 1)
2212            .unwrap_or(self.num_layers)
2213            .min(self.num_layers);
2214
2215        enum Item<'a> {
2216            Gdn {
2217                run: Vec<GdnGpuLayer<'a>>,
2218                first: usize,
2219            },
2220            Attn {
2221                l: AttnGpuLayer<'a>,
2222                li: usize,
2223                q_norm: Option<&'a [f32]>,
2224                k_norm: Option<&'a [f32]>,
2225                output_gate: bool,
2226                /// `self_attn.g_proj` output gate (per-head flag): the
2227                /// sandwich projects the device-normed input on the host
2228                /// and gates the attend's output before O.
2229                proj_gate: Option<(&'a QTensor, bool)>,
2230                /// The same gate's f32 rows `[nh × hidden]` when the device
2231                /// attend can apply it (per-head sigmoid, f32 in RAM).
2232                head_gate_w: Option<&'a [f32]>,
2233                bias: Option<(&'a [f32], &'a [f32], &'a [f32])>,
2234                /// Attend on the device too (no sync): F32 KV, no
2235                /// o1/bias, dims inside the kernels' contract.
2236                full_gpu: bool,
2237            },
2238        }
2239
2240        // Device-attend KERNEL contract, shared by every Full layer. The
2241        // hd>128 default-off POLICY is applied after the scan: it was
2242        // measured on dense models, and a MoE plan inverts it — with the
2243        // experts on device each CPU-attend sandwich costs a
2244        // commit+wait, ~30 submits/token (W2 on M4: 14.7 tok/s
2245        // sandwiched vs 27.1 device-attend vs 18.8 pure CPU).
2246        let attend_mode = std::env::var("CMF_GPU_ATTEND").unwrap_or_else(|_| "auto".into());
2247        let attend_contract = attend_mode != "0"
2248            && attend_mode != "off"
2249            && self.head_dim % 4 == 0
2250            && self.head_dim <= 256
2251            && self.rotary_dim >= 2
2252            && self.rotary_dim <= self.head_dim
2253            && (self.rotary_dim / 2) % 32 == 0
2254            && self.num_kv_heads > 0
2255            && self.num_heads % self.num_kv_heads == 0;
2256
2257        let mut plan: Vec<Item> = Vec::new();
2258        let mut model_ref: Option<std::sync::Arc<cortiq_core::CmfModel>> = None;
2259        // Break-reason diagnostics ride the same env as the plan summary.
2260        let block_diag = std::env::var("CMF_GRAPH_DBG").is_ok();
2261        let mut scan = start;
2262        while scan < limit {
2263            let lw = &self.weights.layers[self.phys_layer(scan)];
2264            let ffn = match &lw.ffn {
2265                FfnKind::Dense(d) if d.segs.is_empty() => {
2266                    let (Some(g), Some(u), Some(dn)) = (
2267                        d.gate_proj.metal_graph_parts(),
2268                        d.up_proj.metal_graph_parts(),
2269                        d.down_proj.metal_graph_parts(),
2270                    ) else {
2271                        if block_diag {
2272                            eprintln!(
2273                                "block-graph: L{scan} FFN trio not graph-mappable — run ends"
2274                            );
2275                        }
2276                        break;
2277                    };
2278                    MetalFfn::Dense {
2279                        gate: g,
2280                        up: u,
2281                        down: dn,
2282                        // The front gate admitted SiLU, exact GELU and
2283                        // (Gemma-4) tanh-GELU only.
2284                        act: d.act.graph_act().map_or(0, |a| a.code() as u8),
2285                    }
2286                }
2287                FfnKind::Moe(m) => {
2288                    let Some(moe) = metal_moe_graph_parts(m, self.hidden_size) else {
2289                        if block_diag {
2290                            eprintln!(
2291                                "block-graph: L{scan} MoE outside the graph contract — run ends"
2292                            );
2293                        }
2294                        break;
2295                    };
2296                    if let QTensor::Mapped { model, .. } = &m.experts[0].gate_proj {
2297                        model_ref.get_or_insert_with(|| model.clone());
2298                    }
2299                    MetalFfn::Moe(moe)
2300                }
2301                _ => {
2302                    if block_diag {
2303                        eprintln!("block-graph: L{scan} non-graph FFN — run ends");
2304                    }
2305                    break;
2306                }
2307            };
2308            match &lw.attn {
2309                AttnKind::LinearGdn(w) if self.gdn_cfg.is_some() => {
2310                    let parts = (
2311                        w.in_proj_qkv.metal_graph_parts(),
2312                        w.in_proj_z.metal_graph_parts(),
2313                        w.in_proj_a.f32_parts(),
2314                        w.in_proj_b.f32_parts(),
2315                        w.out_proj.metal_graph_parts(),
2316                    );
2317                    let (Some(qkv), Some(z), Some(a), Some(b), Some(out)) = parts else {
2318                        if block_diag {
2319                            eprintln!(
2320                                "block-graph: L{scan} GDN parts refused (qkv={} z={} a_f32={} b_f32={} out={})",
2321                                w.in_proj_qkv.metal_graph_parts().is_some(),
2322                                w.in_proj_z.metal_graph_parts().is_some(),
2323                                w.in_proj_a.f32_parts().is_some(),
2324                                w.in_proj_b.f32_parts().is_some(),
2325                                w.out_proj.metal_graph_parts().is_some(),
2326                            );
2327                        }
2328                        break;
2329                    };
2330                    if let QTensor::Mapped { model, .. } = &w.in_proj_qkv {
2331                        model_ref.get_or_insert_with(|| model.clone());
2332                    }
2333                    let gl = GdnGpuLayer {
2334                        attn_norm: &lw.input_norm,
2335                        post_norm: &lw.post_norm,
2336                        qkv,
2337                        z,
2338                        a,
2339                        b,
2340                        out,
2341                        ffn,
2342                        conv1d: &w.conv1d,
2343                        a_log: &w.a_log,
2344                        dt_bias: &w.dt_bias,
2345                        gnorm: &w.norm,
2346                    };
2347                    match plan.last_mut() {
2348                        Some(Item::Gdn { run, .. }) => run.push(gl),
2349                        _ => plan.push(Item::Gdn {
2350                            run: vec![gl],
2351                            first: scan,
2352                        }),
2353                    }
2354                }
2355                AttnKind::Full {
2356                    wq,
2357                    wk,
2358                    wv,
2359                    wo,
2360                    q_norm,
2361                    k_norm,
2362                    output_gate,
2363                    softplus_gate,
2364                    bias,
2365                } if (!self.kv_cache.layers[scan].o1_sealed()
2366                    // Sealed o1 stays plannable when the Metal o1 port
2367                    // is on: full_gpu attends through the device state,
2368                    // and any refusal falls to the sandwich, whose CPU
2369                    // core routes sealed layers through the nystrom step.
2370                    || std::env::var("CMF_O1_METAL").as_deref() == Ok("1"))
2371                    // A projected output gate: Spark-X2.5's per-head
2372                    // sigmoid form only (Laguna's softplus gate is
2373                    // unmeasured on these paths and keeps the CPU walk).
2374                    && softplus_gate
2375                        .as_ref()
2376                        .is_none_or(|(_, per_head)| *per_head && self.proj_gate_sigmoid) =>
2377                {
2378                    let parts = (
2379                        wq.metal_graph_parts(),
2380                        wk.metal_graph_parts(),
2381                        wv.metal_graph_parts(),
2382                        wo.metal_graph_parts(),
2383                    );
2384                    let (Some(pq), Some(pk), Some(pv), Some(po)) = parts else {
2385                        break;
2386                    };
2387                    if let QTensor::Mapped { model, .. } = wq {
2388                        model_ref.get_or_insert_with(|| model.clone());
2389                    }
2390                    let cache = &self.kv_cache.layers[scan];
2391                    // O(1) layer on Metal: the device attends through the
2392                    // sealed Nystrom state (opt-in while the port proves
2393                    // itself). Unsealed -> sandwich path = the CPU o1 step.
2394                    let o1_metal = cache.o1.is_some()
2395                        && std::env::var("CMF_O1_METAL").as_deref() == Ok("1")
2396                        && cache.o1_views().is_some();
2397                    // The device attend takes the layer's window and RoPE
2398                    // table, and the per-head gate from f32 rows; a gate
2399                    // held otherwise sandwiches.
2400                    let head_gate_w = softplus_gate
2401                        .as_ref()
2402                        .and_then(|(g, _)| g.f32_parts())
2403                        .filter(|&(_, r, c)| r == self.num_heads && c == self.hidden_size)
2404                        .map(|(d, _, _)| d);
2405                    // Gemma-4's global layers are 512 wide over one KV head
2406                    // (the `gemma4` 16-slot kernels, `CMF_METAL_GEMMA4_W16`).
2407                    let (nkv_g, hd_g) = if gemma4 {
2408                        let (a, b, _) = self.layer_geom(scan);
2409                        (a, b)
2410                    } else {
2411                        (self.num_kv_heads, self.head_dim)
2412                    };
2413                    let full_gpu = attend_contract
2414                        && softplus_gate.is_none() == head_gate_w.is_none()
2415                        && cache.mode == crate::kv_cache::KvMode::F32
2416                        && (cache.o1.is_none() || o1_metal)
2417                        && bias.is_none()
2418                        && (hd_g <= 256 || crate::gpu_metal::gemma4::w16_on())
2419                        && pq.1 == self.num_heads * hd_g * (1 + *output_gate as usize)
2420                        && pk.1 == nkv_g * hd_g
2421                        && pv.1 == nkv_g * hd_g
2422                        && po.2 == self.num_heads * hd_g;
2423                    plan.push(Item::Attn {
2424                        l: AttnGpuLayer {
2425                            attn_norm: &lw.input_norm,
2426                            post_norm: &lw.post_norm,
2427                            wq: pq,
2428                            wk: pk,
2429                            wv: pv,
2430                            wo: po,
2431                            ffn,
2432                            attn_out_norm: lw.attn_out_norm.as_deref(),
2433                            ffn_out_norm: lw.ffn_out_norm.as_deref(),
2434                            layer_scale: lw.layer_scale,
2435                        },
2436                        li: scan,
2437                        q_norm: q_norm.as_deref(),
2438                        k_norm: k_norm.as_deref(),
2439                        output_gate: *output_gate,
2440                        proj_gate: softplus_gate.as_ref().map(|(g, per_head)| (g, *per_head)),
2441                        head_gate_w,
2442                        bias: bias
2443                            .as_ref()
2444                            .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
2445                        full_gpu,
2446                    });
2447                }
2448                _ => break,
2449            }
2450            scan += 1;
2451        }
2452        let Some(model) = model_ref else {
2453            if std::env::var("CMF_GRAPH_DBG").is_ok() {
2454                eprintln!("q1-graph: no model ref (start {start}, scanned to {scan})");
2455            }
2456            if self.graph_head_required {
2457                self.fail_metal_graph("native graph has no mapped model reference");
2458            }
2459            return start;
2460        };
2461        if plan.is_empty() {
2462            if std::env::var("CMF_GRAPH_DBG").is_ok() {
2463                eprintln!("q1-graph: empty plan at layer {start}");
2464            }
2465            if self.graph_head_required {
2466                self.fail_metal_graph("native graph plan is empty");
2467            }
2468            return start;
2469        }
2470        let has_moe = plan.iter().any(|it| match it {
2471            Item::Gdn { run, .. } => run.iter().any(|l| matches!(l.ffn, MetalFfn::Moe(_))),
2472            Item::Attn { l, .. } => matches!(l.ffn, MetalFfn::Moe(_)),
2473        });
2474        let has_gdn = plan.iter().any(|it| matches!(it, Item::Gdn { .. }));
2475        let dev_attend = attend_contract
2476            && (self.head_dim <= 128
2477                || has_moe
2478                // A GDN hybrid attends on a quarter of its layers: the
2479                // hd>128 caution was measured on pure-dense models where
2480                // gqa_attend dominates, and on Qwen3.8-27B (hd 256, 48
2481                // GDN + 16 attn) the sandwich costs 2x the whole decode
2482                // (1.2 vs 2.21 tok/s measured before the arena fix).
2483                || (self.head_dim <= 256 && has_gdn)
2484                // Sliding-window layers bound most attends by the window:
2485                // Spark-X2.5-1.7B q4tp on the M4 decodes 67 tok/s
2486                // device-attended against 23 sandwiched (28 syncs/token).
2487                || (self.head_dim <= 256 && swa_graph)
2488                || attend_mode == "force"
2489                || attend_mode == "256");
2490        if !dev_attend {
2491            for it in &mut plan {
2492                if let Item::Attn { li, full_gpu, .. } = it {
2493                    // The hd>128 policy is about gqa_attend; an o1 layer
2494                    // attends through its own kernel set.
2495                    let keep_o1 = self.kv_cache.layers[*li].o1.is_some()
2496                        && std::env::var("CMF_O1_METAL").as_deref() == Ok("1");
2497                    if !keep_o1 {
2498                        *full_gpu = false;
2499                    }
2500                }
2501            }
2502        }
2503        if std::env::var("CMF_GRAPH_DBG").is_ok() {
2504            use std::sync::atomic::{AtomicBool, Ordering};
2505            static SAID: AtomicBool = AtomicBool::new(false);
2506            if !SAID.swap(true, Ordering::Relaxed) {
2507                let fg = plan
2508                    .iter()
2509                    .filter(|it| matches!(it, Item::Attn { full_gpu: true, .. }))
2510                    .count();
2511                let att = plan
2512                    .iter()
2513                    .filter(|it| matches!(it, Item::Attn { .. }))
2514                    .count();
2515                eprintln!(
2516                    "q1-graph: plan of {} items from layer {start} to {scan} | dev_attend={dev_attend} full_gpu {fg}/{att} | hd={} rd={} nkv={} nh={}",
2517                    plan.len(),
2518                    self.head_dim,
2519                    self.rotary_dim,
2520                    self.num_kv_heads,
2521                    self.num_heads,
2522                );
2523            }
2524        }
2525        let dims = GraphDims {
2526            hidden: self.hidden_size,
2527            eps: self.rms_eps as f32,
2528            gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
2529        };
2530        let Some(mut graph) = TokenGraph::new(&model, dims, h) else {
2531            if self.graph_head_required {
2532                self.fail_metal_graph("native TokenGraph allocation refused");
2533            }
2534            return start;
2535        };
2536        if swa_graph && self.head_dim > 128 {
2537            // Spark-X2.5 (hd 256, 4 Q heads per KV head): the GQA-shared
2538            // split-K attend reads each K/V row once for the group instead
2539            // of once per head plus the importance re-read — at depth 1000
2540            // on the M4, 59.5 tok/s against 50.0 with the per-head kernel
2541            // up to the default 512 (every sliding layer sits at <= 512).
2542            graph.set_attend_blk_from(64);
2543        }
2544        let gcfg = self.gdn_cfg.map(|cfg| GdnGpuCfg {
2545            nv: cfg.num_v_heads,
2546            nk: cfg.num_k_heads,
2547            dk: cfg.key_head_dim,
2548            dv: cfg.value_head_dim,
2549            kk: cfg.conv_kernel,
2550            hidden: self.hidden_size,
2551            inter: self.intermediate_size,
2552            c_dim: cfg.conv_dim(),
2553            eps: cfg.rms_eps as f32,
2554            gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
2555        });
2556        // Validate the whole plan BEFORE encoding anything: after the
2557        // first sync a refused layer would leave the token
2558        // half-executed, so truncate to the provably encodable prefix.
2559        let mut valid = 0usize;
2560        let mut end = start;
2561        crate::gpu::stageprof(1, _mt0.elapsed()); // конец планирования
2562        if std::env::var("CMF_PLAN_DUMP").is_ok() {
2563            static ONCE: std::sync::Once = std::sync::Once::new();
2564            ONCE.call_once(|| {
2565                for it in &plan {
2566                    match it {
2567                        Item::Gdn { first, run } => {
2568                            eprintln!("plan: Gdn first={first} len={}", run.len())
2569                        }
2570                        Item::Attn { li, full_gpu, .. } => {
2571                            eprintln!("plan: Attn li={li} full_gpu={full_gpu}")
2572                        }
2573                    }
2574                }
2575            });
2576        }
2577        for item in &plan {
2578            let ok = match item {
2579                Item::Gdn { run, .. } => gcfg
2580                    .as_ref()
2581                    .map(|gc| run.iter().all(|l| graph.gdn_ok(l, gc)))
2582                    .unwrap_or(false),
2583                Item::Attn { l, .. } => graph.attn_ok(l),
2584            };
2585            if !ok {
2586                if block_diag {
2587                    eprintln!(
2588                        "block-graph: plan item {} ({}) failed graph preflight",
2589                        valid,
2590                        match item {
2591                            Item::Gdn { run, first } => format!("GDN run L{first}+{}", run.len()),
2592                            Item::Attn { li, .. } => format!("Attn L{li}"),
2593                        }
2594                    );
2595                }
2596                break;
2597            }
2598            valid += 1;
2599            end += match item {
2600                Item::Gdn { run, .. } => run.len(),
2601                Item::Attn { .. } => 1,
2602            };
2603        }
2604        plan.truncate(valid);
2605        if plan.is_empty() {
2606            if self.graph_head_required {
2607                self.fail_metal_graph("native graph preflight produced no valid items");
2608            }
2609            return start;
2610        }
2611
2612        if self.graph_head_required && (upto.is_some() || end != self.num_layers) {
2613            self.fail_metal_graph("fused-head NLL requires a complete 64-layer graph");
2614            return start;
2615        }
2616
2617        // Plain dense decode (every item a device-attended full-attention
2618        // layer with a dense FFN, no O(1) state): the only plan shape the
2619        // masked-nibble q4tp matvec and the concurrent layer encoder were
2620        // measured on (MiniCPM5-2B, Qwen3-0.6B on the M4). Hybrids, MoE and
2621        // o1 layers keep the historical serial path bit for bit.
2622        // Every projection must be ONE dispatch (q1t adds an overlay pass,
2623        // Prism q2tp a transform pass — dependent pairs a concurrent
2624        // encoder would race).
2625        let one_pass = |t: (usize, usize, usize)| {
2626            use cortiq_core::TensorDtype as D;
2627            matches!(
2628                model.tensors[t.0].dtype,
2629                D::Q4TiledP | D::Q4Tiled | D::Q4Block | D::Q8Row | D::Q8_2f | D::Q1
2630            )
2631        };
2632        let dense_fast = plan.iter().all(|it| match it {
2633            Item::Attn {
2634                l, li, full_gpu, ..
2635            } => {
2636                *full_gpu
2637                    && self.kv_cache.layers[*li].o1.is_none()
2638                    && [l.wq, l.wk, l.wv, l.wo].into_iter().all(one_pass)
2639                    && match l.ffn {
2640                        MetalFfn::Dense { gate, up, down, .. } => {
2641                            one_pass(gate) && one_pass(up) && one_pass(down)
2642                        }
2643                        _ => false,
2644                    }
2645            }
2646            Item::Gdn { .. } => false,
2647        });
2648        // MoE decode with every layer device-attended (Mellum2.1): the
2649        // routed experts' jobs ladder and the q8_2f attention projections
2650        // take their measured levers (`MOE_*`, `DENSE_Q8R4`).
2651        let moe_plan = !dense_fast
2652            && plan.iter().all(|it| match it {
2653                Item::Attn {
2654                    l, li, full_gpu, ..
2655                } => {
2656                    *full_gpu
2657                        && self.kv_cache.layers[*li].o1.is_none()
2658                        && matches!(l.ffn, MetalFfn::Moe(_))
2659                }
2660                Item::Gdn { .. } => false,
2661            });
2662        let ab = crate::gpu_metal::dense_ab_arm().filter(|_| dense_fast || moe_plan);
2663        if block_diag {
2664            use std::sync::atomic::{AtomicBool, Ordering};
2665            static SAID_AB: AtomicBool = AtomicBool::new(false);
2666            if !SAID_AB.swap(true, Ordering::Relaxed) {
2667                eprintln!("q1-graph: dense_fast={dense_fast} moe_plan={moe_plan} ab={ab:?}");
2668            }
2669        }
2670        let _mv_fast = match ab {
2671            Some((bits, _)) => {
2672                graph.set_dense_concurrent_raw(bits & crate::gpu_metal::DENSE_CONC != 0);
2673                crate::gpu_metal::MvFastGuard::set_raw(bits)
2674            }
2675            None if moe_plan => {
2676                graph.set_dense_concurrent(crate::gpu_metal::MOE_PLAN_BITS & crate::gpu_metal::DENSE_CONC != 0);
2677                crate::gpu_metal::MvFastGuard::set_bits(crate::gpu_metal::MOE_PLAN_BITS)
2678            }
2679            None => {
2680                graph.set_dense_concurrent(dense_fast);
2681                // Spark-X2.5 q8_2f: the four-row q8_2f matvec decodes the
2682                // 1.7B at 40.5 tok/s on the M4 against 27.7 one-row.
2683                let q8r4 = if swa_graph {
2684                    crate::gpu_metal::DENSE_Q8R4
2685                } else {
2686                    0
2687                };
2688                crate::gpu_metal::MvFastGuard::set_bits(if dense_fast {
2689                    crate::gpu_metal::DENSE_MV | crate::gpu_metal::DENSE_FUSE | q8r4
2690                } else {
2691                    0
2692                })
2693            }
2694        };
2695
2696        // RoPE table and rotary width are per layer (`layer_inv_freq`,
2697        // `layer_geom`) inside the loop below.
2698        let pool = self.pool.clone();
2699        let (nh, nkv, hd, hs, eps) = (
2700            self.num_heads,
2701            self.num_kv_heads,
2702            self.head_dim,
2703            self.hidden_size,
2704            self.rms_eps,
2705        );
2706        let norm_style = self.norm_style;
2707        let gemma = norm_style == cortiq_core::NormStyle::Gemma;
2708        let want = self.gdn_cfg.map(|c| c.state_len()).unwrap_or(0);
2709        let kv_id = self.graph_kv_id;
2710        // GDN runs whose states await readback after the next sync
2711        // (device-attended layers add no sync, so several may stack).
2712        let mut pending: Vec<(usize, usize)> = Vec::new();
2713        // Device-attended layers: their K/V/imp are pulled from the
2714        // mirror after the final sync.
2715        let mut dev_attn: Vec<(usize, usize, usize)> = Vec::new();
2716        for item in &plan {
2717            let _xt0 = std::time::Instant::now();
2718            let _xkind: u32 = match item {
2719                Item::Gdn { .. } => 2,
2720                Item::Attn { .. } => 3,
2721            };
2722            // Looped Transformer: insert on-device norm at loop boundaries.
2723            if self.loop_final_norm {
2724                let item_start = match item {
2725                    Item::Gdn { first, .. } => *first,
2726                    Item::Attn { li, .. } => *li,
2727                };
2728                if item_start > start && self.is_loop_end(item_start - 1) {
2729                    graph.encode_loop_norm(&self.weights.final_norm);
2730                }
2731            }
2732            match item {
2733                Item::Gdn { run, first } => {
2734                    for l in &mut self.kv_cache.layers[*first..*first + run.len()] {
2735                        if l.linear_state.len() != want {
2736                            l.linear_state = vec![0f32; want];
2737                        }
2738                    }
2739                    let ro: Vec<&[f32]> = self.kv_cache.layers[*first..*first + run.len()]
2740                        .iter()
2741                        .map(|l| l.linear_state.as_slice())
2742                        .collect();
2743                    let _ig = std::time::Instant::now();
2744                    if !graph.encode_gdn_run(run, &ro, gcfg.as_ref().unwrap()) {
2745                        // Unreachable: the plan was validated above.
2746                        tracing::error!("q1 graph: GDN run refused after validation");
2747                        return start;
2748                    }
2749                    // Early commit: the GPU starts the run while the
2750                    // CPU encodes the next layer (nothing to wait on).
2751                    graph.commit_kind = 2;
2752                    graph.commit();
2753                    crate::gpu::stageprof(0, _ig.elapsed());
2754                    pending.push((*first, run.len()));
2755                }
2756                Item::Attn {
2757                    l,
2758                    li,
2759                    q_norm,
2760                    k_norm,
2761                    output_gate,
2762                    proj_gate,
2763                    head_gate_w,
2764                    bias,
2765                    full_gpu,
2766                } => {
2767                    let _ia = std::time::Instant::now();
2768                    // This layer's attention geometry: its window, RoPE
2769                    // table and rotary width (Spark-X2.5 interleaves
2770                    // 512-window layers rotating all dims at θ 1e4 with
2771                    // full layers rotating a quarter at θ 5e6). Every model
2772                    // without sliding layers reads the global ones here.
2773                    let inv_freq_l = self.layer_inv_freq(*li);
2774                    let rd_l = self.layer_geom(*li).2;
2775                    let window_l = self.layer_window(*li);
2776                    let head_gate_w = *head_gate_w;
2777                    // This layer's KV heads and head width: Gemma-4's global
2778                    // layers (one 512-wide KV head) differ from the base.
2779                    let (nkv, hd) = if gemma4 {
2780                        let (a, b, _) = self.layer_geom(*li);
2781                        (a, b)
2782                    } else {
2783                        (nkv, hd)
2784                    };
2785                    // ── Fully device-resident attention: no sync at all.
2786                    if *full_gpu {
2787                        let cache = &self.kv_cache.layers[*li];
2788                        let o1p = if cache.o1.is_some() {
2789                            match cache.o1_views() {
2790                                Some(views) => Some(crate::gpu::O1AttnParams {
2791                                    views,
2792                                    epoch: self.o1_epoch,
2793                                }),
2794                                // Sealed state gone mid-run: sandwich.
2795                                None => None,
2796                            }
2797                        } else {
2798                            None
2799                        };
2800                        let o1_layer = cache.o1.is_some();
2801                        if o1_layer && o1p.is_none() {
2802                            // fall to the sandwich (CPU o1 step)
2803                        }
2804                        let cpu_k: Vec<&[f32]> = (0..nkv).map(|g| cache.head_keys(g)).collect();
2805                        let cpu_v: Vec<&[f32]> = (0..nkv).map(|g| cache.head_values(g)).collect();
2806                        let cpu_stored = if o1_layer { 0 } else { cpu_k[0].len() / hd };
2807                        // A trimmed tail always holds the window the
2808                        // device attend reads (`first = rows + 1 − w`).
2809                        debug_assert!(
2810                            cache.base() == 0 || window_l.is_some_and(|w| cpu_stored + 1 >= w)
2811                        );
2812                        let p = crate::gpu::AttnDeviceParams {
2813                            kv_id,
2814                            layer: *li,
2815                            nh,
2816                            nkv,
2817                            hd,
2818                            rd: rd_l,
2819                            position,
2820                            scale: self.attn_scale,
2821                            eps: eps as f32,
2822                            gemma,
2823                            late_qk_norm: self.qk_norm_after_rope,
2824                            output_gate: *output_gate,
2825                            q_norm: *q_norm,
2826                            k_norm: *k_norm,
2827                            inv_freq: &inv_freq_l,
2828                            rope_scale: self.layer_rope_scale(*li),
2829                            cpu_k,
2830                            cpu_v,
2831                            cpu_stored,
2832                            cpu_gen: cache.generation(),
2833                            o1: o1p,
2834                            window: window_l,
2835                            head_gate: head_gate_w,
2836                            v_norm: self.attn_v_norm,
2837                        };
2838                        let o1_bad = o1_layer && p.o1.is_none();
2839                        if !o1_bad && graph.attn_device_ok(l, &p) && graph.encode_attn_device(l, &p)
2840                        {
2841                            // o1 layers leave no mirror row to pull.
2842                            if p.o1.is_none() {
2843                                dev_attn.push((*li, nkv, hd));
2844                            }
2845                            graph.commit_kind = 3;
2846                            graph.commit();
2847                            // The footer below is skipped by `continue`:
2848                            // account the device-attn item here or its
2849                            // cost hides from the stage profile entirely.
2850                            crate::gpu::stageprof(_xkind, _xt0.elapsed());
2851                            continue;
2852                        }
2853                        // Mirror refused (nothing encoded) → sandwich.
2854                    }
2855                    graph.encode_attn_prefix(l);
2856                    if let Err(err) = graph.sync_checked() {
2857                        self.fail_metal_graph(&err);
2858                        return start;
2859                    }
2860                    if !pending.is_empty() {
2861                        let idxs: Vec<usize> =
2862                            pending.drain(..).flat_map(|(f, n)| f..f + n).collect();
2863                        let mut outs: Vec<&mut [f32]> = self
2864                            .kv_cache
2865                            .layers
2866                            .iter_mut()
2867                            .enumerate()
2868                            .filter(|(i, _)| idxs.binary_search(i).is_ok())
2869                            .map(|(_, s)| s.linear_state.as_mut_slice())
2870                            .collect();
2871                        graph.read_states(&mut outs);
2872                    }
2873                    let mut q_raw = attention::take_buf(l.wq.1);
2874                    let mut k = attention::take_buf(l.wk.1);
2875                    let mut v = attention::take_buf(l.wv.1);
2876                    graph.read_qkv(&mut q_raw, &mut k, &mut v);
2877                    // Projected output gate: g = G·norm(h) on the host, off
2878                    // the normed input the prefix left on the device.
2879                    let mut gate_raw = proj_gate.map(|(gp, _)| {
2880                        let mut normed = attention::take_buf(hs);
2881                        graph.read_normed(&mut normed);
2882                        let mut raw = attention::take_buf(gp.rows());
2883                        gp.matvec(&normed, &mut raw, pool.as_deref());
2884                        attention::recycle_buf(&mut normed);
2885                        raw
2886                    });
2887                    let cfg = QwenAttnCfg {
2888                        num_heads: nh,
2889                        num_kv_heads: nkv,
2890                        head_dim: hd,
2891                        hidden_size: hs,
2892                        position,
2893                        inv_freq: &inv_freq_l,
2894                        rotary_dim: rd_l,
2895                        scale: self.attn_scale,
2896                        softcap: self.attn_softcap,
2897                        window: window_l,
2898                        // Gemma-4 only (the front gate refuses V norm
2899                        // otherwise).
2900                        v_norm: self.attn_v_norm,
2901                        qk_norm_after_rope: self.qk_norm_after_rope,
2902                        gate_sigmoid: self.proj_gate_sigmoid,
2903                        q_norm: *q_norm,
2904                        k_norm: *k_norm,
2905                        output_gate: *output_gate,
2906                        softplus_gate: None,
2907                        rope_scale: self.layer_rope_scale(*li),
2908                        bias: *bias,
2909                        rms_eps: eps,
2910                        norm_style,
2911                        pool: pool.as_deref(),
2912                        v_head_dim: hd,
2913                    };
2914                    // CMF_ATTN_ORACLE=1: diff the device attend against
2915                    // this CPU attend on identical inputs (bring-up).
2916                    let oracle = std::env::var("CMF_ATTN_ORACLE").as_deref() == Ok("1")
2917                        || std::env::var("CMF_ATTN_DUMP").is_ok();
2918                    let _ = full_gpu;
2919                    let oracle_in = oracle.then(|| (q_raw.clone(), k.clone(), v.clone()));
2920                    let mut ao = attention::qwen_attention_core(
2921                        q_raw,
2922                        k,
2923                        v,
2924                        &mut self.kv_cache.layers[*li],
2925                        &cfg,
2926                    );
2927                    // CMF_ATTN_DUMP=<dir>: this token's rope'd Q and the layer's whole
2928                    // K/V cache as raw f32 (offline attention-statistics probes:
2929                    // block bounds, mass concentration). Needs CMF_GPU_ATTEND=0.
2930                    if let Ok(dir) = std::env::var("CMF_ATTN_DUMP") {
2931                        if let Some((qr0, k0, v0)) = oracle_in.clone() {
2932                            let (cq, _cg, _ck, _cv) =
2933                                attention::finish_projection_debug(qr0, k0, v0, &cfg, position);
2934                            let cache = &self.kv_cache.layers[*li];
2935                            let n = cache.head_keys(0).len() / hd;
2936                            let mut bytes: Vec<u8> = Vec::new();
2937                            for v in [nh as u32, nkv as u32, hd as u32, n as u32, position as u32] {
2938                                bytes.extend_from_slice(&v.to_le_bytes());
2939                            }
2940                            for v in &cq {
2941                                bytes.extend_from_slice(&v.to_le_bytes());
2942                            }
2943                            for g in 0..nkv {
2944                                for v in cache.head_keys(g) {
2945                                    bytes.extend_from_slice(&v.to_le_bytes());
2946                                }
2947                            }
2948                            for g in 0..nkv {
2949                                for v in cache.head_values(g) {
2950                                    bytes.extend_from_slice(&v.to_le_bytes());
2951                                }
2952                            }
2953                            let _ =
2954                                std::fs::write(format!("{dir}/L{li}_pos{position}.bin"), &bytes);
2955                        }
2956                    }
2957                    if let Some((qr0, k0, v0)) =
2958                        oracle_in.filter(|_| std::env::var("CMF_ATTN_ORACLE").as_deref() == Ok("1"))
2959                    {
2960                        let (cq, _cg, ck, cv) =
2961                            attention::finish_projection_debug(qr0, k0, v0, &cfg, position);
2962                        let mut h_now = vec![0f32; hs];
2963                        graph.read_h(&mut h_now);
2964                        let cache = &self.kv_cache.layers[*li];
2965                        let n_after = cache.head_keys(0).len() / hd;
2966                        // A sealed O(1) cache may have no dense current-row
2967                        // entry. The oracle is a debug probe, so let it see
2968                        // zero stored exact rows instead of underflowing.
2969                        let stored = n_after.saturating_sub(1);
2970                        let cpu_k: Vec<&[f32]> = (0..nkv)
2971                            .map(|g| &cache.head_keys(g)[..stored * hd])
2972                            .collect();
2973                        let cpu_v: Vec<&[f32]> = (0..nkv)
2974                            .map(|g| &cache.head_values(g)[..stored * hd])
2975                            .collect();
2976                        let p = crate::gpu::AttnDeviceParams {
2977                            kv_id,
2978                            layer: *li,
2979                            nh,
2980                            nkv,
2981                            hd,
2982                            // The layer's own geometry, as the CPU attend
2983                            // above used it (the projected gate is applied
2984                            // after this probe on both sides).
2985                            rd: rd_l,
2986                            position,
2987                            scale: self.attn_scale,
2988                            eps: eps as f32,
2989                            gemma,
2990                            late_qk_norm: self.qk_norm_after_rope,
2991                            output_gate: *output_gate,
2992                            q_norm: *q_norm,
2993                            k_norm: *k_norm,
2994                            inv_freq: &inv_freq_l,
2995                            rope_scale: self.layer_rope_scale(*li),
2996                            cpu_k,
2997                            cpu_v,
2998                            cpu_stored: stored,
2999                            cpu_gen: cache.generation(),
3000                            o1: None,
3001                            window: window_l,
3002                            head_gate: None,
3003                            v_norm: self.attn_v_norm,
3004                        };
3005                        if let Some((dq, dk, dv, dao)) = graph.debug_attn_device(l, &p, &h_now) {
3006                            let md = |a: &[f32], b: &[f32]| {
3007                                a.iter().zip(b).fold(0f32, |m, (x, y)| m.max((x - y).abs()))
3008                            };
3009                            let nn = |a: &[f32]| a.iter().map(|x| x * x).sum::<f32>().sqrt();
3010                            eprintln!(
3011                                "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}",
3012                                nn(&cq),
3013                                md(&cq, &dq),
3014                                nn(&ck),
3015                                md(&ck, &dk),
3016                                nn(&cv),
3017                                md(&cv, &dv),
3018                                nn(&ao),
3019                                md(&ao, &dao)
3020                            );
3021                        } else {
3022                            eprintln!("attn-oracle L{li}: device probe declined");
3023                        }
3024                    }
3025                    if let (Some(raw), Some((_, per_head))) = (gate_raw.as_deref(), *proj_gate) {
3026                        // V is as wide as the head (the front gate), so ao
3027                        // is nh·hd here, as in `qwen_attention`.
3028                        attention::apply_projected_gate(
3029                            &mut ao,
3030                            raw,
3031                            per_head,
3032                            hd,
3033                            self.proj_gate_sigmoid,
3034                        );
3035                    }
3036                    if let Some(mut raw) = gate_raw.take() {
3037                        attention::recycle_buf(&mut raw);
3038                    }
3039                    graph.encode_attn_suffix(l, &ao);
3040                    // Early commit: the GPU starts O+FFN while the CPU
3041                    // encodes the following GDN run / attention prefix.
3042                    graph.commit();
3043                    attention::recycle_buf(&mut ao);
3044                }
3045            }
3046
3047            crate::gpu::stageprof(_xkind, _xt0.elapsed());
3048        }
3049        // Ride the final norm + lm_head in the same command buffer when
3050        // this run reaches the model's end and the caller wants logits:
3051        // the separate per-op lm_head submit (a full round trip) folds
3052        // into the sync that already happens here.
3053        let mut lm_rows = None;
3054        if self.graph_want_logits
3055            && upto.is_none()
3056            && end == self.num_layers
3057            && std::env::var("CMF_GPU_LMHEAD")
3058                .map(|v| v != "0")
3059                .unwrap_or(true)
3060        {
3061            if let Some(lm) = self.weights.lm_head.metal_graph_parts() {
3062                if graph.lm_head_ok(lm) {
3063                    graph.encode_lm_head(&self.weights.final_norm, lm);
3064                    lm_rows = Some(lm.1);
3065                }
3066            }
3067        }
3068        if self.graph_head_required && lm_rows.is_none() {
3069            METAL_GRAPH_HEAD_MISS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
3070            self.fail_metal_graph("fused graph head was requested but not encodable");
3071            return start;
3072        }
3073        let _sy0 = std::time::Instant::now();
3074        if let Err(err) = graph.sync_checked() {
3075            self.fail_metal_graph(&err);
3076            return start;
3077        }
3078        let _rs0 = std::time::Instant::now();
3079        if !pending.is_empty() {
3080            let idxs: Vec<usize> = pending.drain(..).flat_map(|(f, n)| f..f + n).collect();
3081            let mut outs: Vec<&mut [f32]> = self
3082                .kv_cache
3083                .layers
3084                .iter_mut()
3085                .enumerate()
3086                .filter(|(i, _)| idxs.binary_search(i).is_ok())
3087                .map(|(_, s)| s.linear_state.as_mut_slice())
3088                .collect();
3089            graph.read_states(&mut outs);
3090        }
3091        if std::env::var("CMF_GRAPH_HOSTPROF").as_deref() == Ok("1") {
3092            use std::sync::atomic::{AtomicU64, Ordering};
3093            static SY: AtomicU64 = AtomicU64::new(0);
3094            static RS: AtomicU64 = AtomicU64::new(0);
3095            static N: AtomicU64 = AtomicU64::new(0);
3096            SY.fetch_add((_rs0 - _sy0).as_nanos() as u64, Ordering::Relaxed);
3097            RS.fetch_add(_rs0.elapsed().as_nanos() as u64, Ordering::Relaxed);
3098            let n = N.fetch_add(1, Ordering::Relaxed) + 1;
3099            if n % 100 == 0 {
3100                eprintln!(
3101                    "postprof: sync-wait {:.1} ms/ток | read_states {:.1} ms/ток ({n})",
3102                    SY.load(Ordering::Relaxed) as f64 / n as f64 / 1e6,
3103                    RS.load(Ordering::Relaxed) as f64 / n as f64 / 1e6
3104                );
3105            }
3106        }
3107        if let Some(rows) = lm_rows {
3108            crate::gpu::hostprof_encode_done(_mt0);
3109            let mut lg = attention::take_buf(rows.min(self.vocab_size));
3110            graph.read_logits(&mut lg);
3111            crate::gpu::hostprof_total(_mt0);
3112            lg.resize(self.vocab_size, 0.0);
3113            if let Some(c) = self.final_softcap {
3114                softcap_in_place(&mut lg, c, self.pool.as_deref());
3115            }
3116            self.graph_logits = Some(lg);
3117        }
3118        graph.read_h(h);
3119        if self.graph_head_required && self.graph_logits.is_none() {
3120            METAL_GRAPH_HEAD_MISS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
3121            self.fail_metal_graph("fused graph head completed without logits readback");
3122            return start;
3123        }
3124        METAL_GRAPH_TOK_OK.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
3125        METAL_GRAPH_LAYERS.fetch_add(
3126            end.saturating_sub(start) as u64,
3127            std::sync::atomic::Ordering::Relaxed,
3128        );
3129        if self.graph_head_required {
3130            METAL_GRAPH_HEAD_OK.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
3131        }
3132        // Device-attended layers: replay the CPU bookkeeping — append
3133        // the mirror's new K/V row (rope'd on the GPU) into the owner
3134        // cache, then bank this token's attention-importance mass.
3135        for (li, nkv, hd) in dev_attn {
3136            let mut krow = attention::take_buf(nkv * hd);
3137            let mut vrow = attention::take_buf(nkv * hd);
3138            if crate::gpu::kv_mirror_read_last(kv_id, li, nkv, hd, &mut krow, &mut vrow) {
3139                let cache = &mut self.kv_cache.layers[li];
3140                cache.append(&krow, &vrow, &[]);
3141                let n = cache.seq_len;
3142                let mut imp = attention::take_buf(n);
3143                crate::gpu::kv_mirror_take_imp(kv_id, li, &mut imp);
3144                cache.accumulate_imp(&imp);
3145                attention::recycle_buf(&mut imp);
3146            }
3147            attention::recycle_buf(&mut krow);
3148            attention::recycle_buf(&mut vrow);
3149        }
3150        if let Some((_, arm)) = ab {
3151            crate::gpu_metal::dense_ab_record(arm, _mt0.elapsed());
3152        }
3153        end
3154    }
3155
3156    pub fn new(
3157        tokenizer: Tokenizer,
3158        weights: PipelineWeights,
3159        hidden_size: usize,
3160        intermediate_size: usize,
3161        num_heads: usize,
3162        num_kv_heads: usize,
3163        head_dim: usize,
3164        num_layers: usize,
3165        physical_layers: usize,
3166        loop_final_norm: bool,
3167        vocab_size: usize,
3168        rms_eps: f64,
3169        rope_base: f32,
3170        norm_style: NormStyle,
3171        max_seq_len: usize,
3172        sampler_config: SamplerConfig,
3173    ) -> Self {
3174        let rng = match sampler_config.seed {
3175            Some(s) => SplitMix64::new(s),
3176            None => SplitMix64::from_entropy(),
3177        };
3178        let inv_freq = std::sync::Arc::new(attention::rope_inv_freq(head_dim, rope_base));
3179        let pool = Pool::from_env();
3180        if let Some(p) = &pool {
3181            tracing::info!("worker pool: {} threads", p.n_workers());
3182            // Keep the workers on the socket that holds the weights.
3183            if let Some(model) = weights
3184                .lm_head
3185                .model_arc()
3186                .or_else(|| weights.embed_tokens.model_arc())
3187            {
3188                let regions: Vec<&[u8]> =
3189                    model.tensors.iter().map(|t| model.entry_bytes(t)).collect();
3190                p.bind_numa(&regions);
3191            }
3192        }
3193        Self {
3194            gpu_plan: None,
3195            tokenizer: std::sync::Arc::new(tokenizer),
3196            kv_cache: KvCache::new(num_layers, num_kv_heads, head_dim, max_seq_len),
3197            sampler_config,
3198            weights,
3199            hidden_size,
3200            intermediate_size,
3201            num_heads,
3202            num_kv_heads,
3203            head_dim,
3204            num_layers,
3205            physical_layers,
3206            loop_final_norm,
3207            vocab_size,
3208            rms_eps,
3209            rope_base,
3210            norm_style,
3211            rotary_dim: head_dim,
3212            attention_heads_per_layer: None,
3213            kv_heads_per_layer: None,
3214            v_head_dim: None,
3215            layer_dump: std::env::var_os("CMF_LAYER_DUMP")
3216                .filter(|v| !v.is_empty())
3217                .map(std::path::PathBuf::from),
3218            graph_declines: std::cell::RefCell::new(Vec::new()),
3219            mimo_moe: Default::default(),
3220            vmf_cfg: None,
3221            gdn_cfg: None,
3222            kda_cfg: None,
3223            g3n: None,
3224            dsv4: None,
3225            dsv41: None,
3226            dsv41_vision: None,
3227            dsv41_prefill: None,
3228            qwen4_exp: None,
3229            dsv4_mtp: Vec::new(),
3230            dspark: None,
3231            dspark_pending: Vec::new(),
3232            dspark_hist: Vec::new(),
3233            dspark_real: Vec::new(),
3234            dspark_trunk_picks: Vec::new(),
3235            dspark_exp: Vec::new(),
3236            dspark_draft_ns: 0,
3237            logit_multiplier: None,
3238            cancel: std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)),
3239            graph_failed: std::sync::atomic::AtomicBool::new(false),
3240            kv_history: Vec::new(),
3241            kv_history_device: false,
3242            short_conv_cfg: None,
3243            mtp: None,
3244            mimo_mtp: None,
3245            verify_exact_moe: false,
3246            speculative: std::env::var("CMF_MTP").map(|v| v != "0").unwrap_or(true),
3247            ignore_eos: false,
3248            draft_full_streak: 0,
3249            spec_k_adapt: None,
3250            spec_acc_ewma: 0.7,
3251            rng,
3252            sampler_scratch: SamplerScratch::default(),
3253            spec_forced: None,
3254            spec_q: Vec::new(),
3255            spec_p: Vec::new(),
3256            spec_res: Vec::new(),
3257            spec_qs: Vec::new(),
3258            spec_ps: Vec::new(),
3259            spec_ress: Vec::new(),
3260            mtp_graph_mode: None,
3261            #[cfg(target_os = "macos")]
3262            metal_verify: None,
3263            inv_freq,
3264            ws: ForwardScratch::new(hidden_size),
3265            pool,
3266            model: None,
3267            dyn_force_f32: false,
3268            dyn_skill_layers: Vec::new(),
3269            dyn_active: None,
3270            dyn_blend_loaded: false,
3271            dyn_phi_layer: None,
3272            dyn_phi_ema: Vec::new(),
3273            dyn_phi_seen: 0,
3274            dyn_router: None,
3275            o1_cfg: None,
3276            o1_epoch: 0,
3277            o1_flags: Vec::new(),
3278            trace: false,
3279            calib_temp: 1.0,
3280            confidence_on: true,
3281            embed_multiplier: 1.0,
3282            attn_scale: 1.0 / (head_dim as f32).sqrt(),
3283            swa: None,
3284            swa_trim: None,
3285            sliding_layers: None,
3286            anchor_core: None,
3287            bounded_rope: None,
3288            kv_prefix: KvPrefix::default(),
3289            last_prefill_tokens: 0,
3290            inv_freq_local: None,
3291            rotary_dim_local: None,
3292            rope_scale: 1.0,
3293            rope_scale_local: 1.0,
3294            global_attn: None,
3295            inv_freq_global: None,
3296            attn_v_norm: false,
3297            qk_norm_after_rope: false,
3298            proj_gate_sigmoid: false,
3299            final_softcap: None,
3300            head_clusters: None,
3301            attn_softcap: 0.0,
3302            graph_want_logits: false,
3303            graph_head_required: false,
3304            graph_logits: None,
3305            embryo_graph: None,
3306            graph_refused: std::sync::atomic::AtomicBool::new(false),
3307            graph_kv_id: {
3308                static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
3309                NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
3310            },
3311            #[cfg(test)]
3312            nll_test_fail_at: None,
3313            #[cfg(test)]
3314            nll_test_force_serial: false,
3315        }
3316    }
3317
3318    /// Enable/disable per-layer O(1) Nyström attention. Only Full
3319    /// layers are eligible (a linear layer keeps its own operator).
3320    /// Applies to generation (`generate*`/`forward_ids`): the prompt
3321    /// pass stays exact, then the state seals after prefill or at the
3322    /// deferred skeleton-safe boundary for short prompts; decode runs on
3323    /// the O(1) state. Teacher-forced scoring (`ppl_ids`) intentionally
3324    /// stays exact.
3325    pub fn set_o1(&mut self, cfg: Option<crate::nystrom::O1Cfg>) {
3326        if let Err(e) = self.try_set_o1(cfg) {
3327            tracing::error!("{e}");
3328        }
3329    }
3330
3331    /// True when the file carries a native bounded anchor
3332    /// (`arch.anchor_core`): its state is a fixed record the header
3333    /// fixes, and the post-hoc O(1) override is meaningless on it.
3334    pub fn bounded_native(&self) -> bool {
3335        self.anchor_core.is_some()
3336    }
3337
3338    /// Bytes the resident device graph holds for this pipeline's sequence:
3339    /// `(recurrent state, anchor KV/ring)`; None on the host path.
3340    pub fn device_state_bytes(&self) -> Option<(u64, u64)> {
3341        crate::gpu::embryo_device_state_bytes(self.graph_kv_id)
3342    }
3343
3344    /// Why an O(1) override is refused on this pipeline, if it is.
3345    pub fn o1_refusal(&self) -> Option<String> {
3346        self.anchor_core.as_ref().map(|ac| {
3347            format!(
3348                "--o1 / CMF_O1 refused: the anchor is native bounded \
3349                 (anchor_core kind={} window={} sink={}); the file's operator \
3350                 is executed as-is and no post-hoc Nyström overlay applies",
3351                ac.kind, ac.window, ac.sink
3352            )
3353        })
3354    }
3355
3356    /// `set_o1` that reports the refusal instead of logging it.
3357    pub fn try_set_o1(&mut self, cfg: Option<crate::nystrom::O1Cfg>) -> Result<(), String> {
3358        if let Some(c) = &cfg {
3359            if let Some(why) = self.o1_refusal() {
3360                self.o1_flags = Vec::new();
3361                self.o1_cfg = None;
3362                return Err(why);
3363            }
3364            if crate::nystrom::o1_deferred_boundary(c.w, c.sink).is_none() {
3365                self.o1_flags.clear();
3366                self.o1_cfg = None;
3367                return Err(format!(
3368                    "o1 disabled: w + sink + slack + 1 overflows usize (w={}, sink={})",
3369                    c.w, c.sink
3370                ));
3371            }
3372        }
3373        self.o1_flags = match &cfg {
3374            Some(c) => {
3375                let mut flags = c.layer_flags(self.num_layers);
3376                for (li, f) in flags.iter_mut().enumerate() {
3377                    // The Nyström state replaces a full-context plain
3378                    // softmax: a sliding window or a learned sink is not
3379                    // something it can represent, and a V narrower than
3380                    // the head is not what its streaming state stores.
3381                    // Those layers keep exact cache attention.
3382                    if *f
3383                        && (!matches!(
3384                            self.weights.layers[self.phys_layer(li)].attn,
3385                            AttnKind::Full { .. }
3386                        ) || self.layer_window(li).is_some()
3387                            || self.kv_cache.layers[li].sinks.is_some()
3388                            || self.layer_v_dim(li) != self.layer_geom(li).1)
3389                    {
3390                        *f = false;
3391                    }
3392                }
3393                flags
3394            }
3395            None => Vec::new(),
3396        };
3397        if let Some(c) = &cfg {
3398            let n = self.o1_flags.iter().filter(|&&f| f).count();
3399            tracing::info!(
3400                "o1 nystrom attention: {n}/{} layer(s), m={} w={} sink={} rect={:?}",
3401                self.num_layers,
3402                c.m,
3403                c.w,
3404                c.sink,
3405                c.rect
3406            );
3407        }
3408        self.o1_cfg = cfg;
3409        Ok(())
3410    }
3411
3412    /// Install the file's bounded anchor: one fixed-size ring per
3413    /// `AttnKind::Bounded` layer (from the header, not per prompt) and
3414    /// the shared relative-rotation table. Must run after the RoPE setup
3415    /// (`set_rotary`, YaRN) so the table is built from the final
3416    /// `inv_freq`.
3417    pub fn install_bounded(
3418        &mut self,
3419        cfg: &cortiq_core::AnchorCoreConfig,
3420    ) -> Result<(), String> {
3421        if !cortiq_core::AnchorCoreConfig::KINDS.contains(&cfg.kind.as_str()) {
3422            return Err(format!(
3423                "anchor_core kind '{}' is not executable by this runtime",
3424                cfg.kind
3425            ));
3426        }
3427        if cfg.window == 0 {
3428            return Err("anchor_core.window must be >= 1".into());
3429        }
3430        let mut n = 0usize;
3431        for li in 0..self.num_layers {
3432            let pl = self.phys_layer(li);
3433            if let AttnKind::Bounded(w) = &self.weights.layers[pl].attn {
3434                if w.window != cfg.window || w.sink != cfg.sink {
3435                    return Err(format!(
3436                        "layer {li}: bounded weights (window {} sink {}) disagree with \
3437                         anchor_core (window {} sink {})",
3438                        w.window, w.sink, cfg.window, cfg.sink
3439                    ));
3440                }
3441                self.kv_cache.layers[li].install_bounded(cfg.window);
3442                n += 1;
3443            }
3444        }
3445        if n == 0 {
3446            return Err("anchor_core is present but no layer executes it".into());
3447        }
3448        let rope = crate::bounded::BoundedRope::new(cfg.window, &self.inv_freq, self.rope_scale);
3449        self.bounded_rope = Some(std::sync::Arc::new(rope));
3450        self.anchor_core = Some(cfg.clone());
3451        self.embryo_graph = None;
3452        tracing::info!(
3453            "bounded anchor {}: {n} layer(s), window {} sink {} — {} B of ring per layer",
3454            cfg.kind,
3455            cfg.window,
3456            cfg.sink,
3457            self.kv_cache.layers.iter().map(|l| l.bounded_state_bytes()).max().unwrap_or(0)
3458        );
3459        Ok(())
3460    }
3461
3462    /// Tag every layer's cache with its wire record kind and the model's
3463    /// operator identity (hash64 of `linear_core_identity` JSON) so the
3464    /// versioned state wire refuses a peer holding another operator.
3465    pub fn install_wire_identity(&mut self, identity: u64) {
3466        for li in 0..self.kv_cache.layers.len() {
3467            let pl = self.phys_layer(li);
3468            let kind = match self.weights.layers.get(pl).map(|l| &l.attn) {
3469                Some(AttnKind::Bounded(_)) => crate::kv_cache::WireKind::Bounded,
3470                Some(AttnKind::Linear(_))
3471                | Some(AttnKind::LinearGdn(_))
3472                | Some(AttnKind::ShortConv(_))
3473                | Some(AttnKind::Kda(_)) => crate::kv_cache::WireKind::Linear,
3474                _ => crate::kv_cache::WireKind::Full,
3475            };
3476            let l = &mut self.kv_cache.layers[li];
3477            l.wire_kind = kind;
3478            l.wire_identity = identity;
3479            // A per-layer geometry (`set_attn_geometry`, Gemma's global
3480            // heads) rebuilds a layer cache with index 0: re-tag it.
3481            l.wire_layer = li as u32;
3482        }
3483    }
3484
3485    /// Forget the reuse keys (legacy `kv_history` and the bounded prefix).
3486    pub fn clear_history(&mut self) {
3487        self.kv_history.clear();
3488        self.kv_history_device = false;
3489        self.kv_prefix.clear();
3490    }
3491
3492    /// Did THIS pipeline's token graph refuse for a structural reason?
3493    /// (Per pipeline: another lane's refusal, or a new pipeline of the
3494    /// same model, never changes it.)
3495    pub fn graph_refused(&self) -> bool {
3496        self.graph_refused
3497            .load(std::sync::atomic::Ordering::Relaxed)
3498    }
3499
3500    /// Remember a structural refusal of this pipeline's token graph.
3501    pub fn mark_graph_refused(&self) {
3502        if !self
3503            .graph_refused
3504            .swap(true, std::sync::atomic::Ordering::Relaxed)
3505        {
3506            tracing::info!(
3507                "token graph: unsupported for this pipeline (seq {}) — not retrying",
3508                self.graph_kv_id
3509            );
3510        }
3511    }
3512
3513    /// Position the resident Embryo graph holds for this pipeline's
3514    /// sequence (`Some(next position)`), `None` when the device holds no
3515    /// image of it (host-owned sequence, or none started).
3516    pub fn device_sequence_position(&self) -> Option<usize> {
3517        crate::gpu::embryo_device_next_position(self.graph_kv_id)
3518    }
3519
3520    /// Is the sequence this pipeline's reuse key describes owned by the
3521    /// device path it would take now? A prefix recorded on the resident
3522    /// graph continues only there (at exactly `n`), a host prefix only on
3523    /// the host; any mismatch means the next turn re-prefills from zero.
3524    fn prefix_owner_matches(&self, n: usize, recorded_on_device: bool) -> bool {
3525        let dev = self.device_sequence_position();
3526        if recorded_on_device {
3527            dev == Some(n) && self.embryo_resident_wanted()
3528        } else {
3529            dev.is_none()
3530        }
3531    }
3532
3533    /// The weights under the sequence changed (a real skill switch): every
3534    /// cached state was computed by other weights. Clear the host KV /
3535    /// ring / recurrent state AND the reuse keys — a surviving `kv_prefix`
3536    /// would let the next turn "extend" a prefix the new weights never
3537    /// saw — drop the packed resident graph (it holds the old FFN
3538    /// tensors; the next build packs the live ones under a fresh id) and
3539    /// reset its device sequence.
3540    pub(crate) fn invalidate_for_weight_change(&mut self) {
3541        self.clear_sequence_state();
3542        self.embryo_graph = None;
3543    }
3544
3545    /// Prompt positions the cache already holds when `input_ids`
3546    /// strictly EXTENDS the consumed prefix (0 otherwise). Bounded-native
3547    /// models answer from the fixed-size `kv_prefix` record; everything
3548    /// else from the legacy `kv_history` vector (which the network split
3549    /// also reads and writes).
3550    fn cached_prefix_len(&self, input_ids: &[u32]) -> usize {
3551        let (n, on_device) = if self.bounded_native() {
3552            (self.kv_prefix.extension(input_ids), self.kv_prefix.on_device())
3553        } else {
3554            let h = &self.kv_history;
3555            if !h.is_empty() && h.len() < input_ids.len() && input_ids[..h.len()] == h[..] {
3556                (h.len(), self.kv_history_device)
3557            } else {
3558                (0, false)
3559            }
3560        };
3561        // The owner tag: the state the key describes must live where this
3562        // turn will continue it (R4) — else a fresh sequence.
3563        if n > 0 && !self.prefix_owner_matches(n, on_device) {
3564            tracing::warn!(
3565                "kv-reuse refused: the cached prefix ({n} positions) was built on the {} path, \
3566                 the device now holds {:?} — re-prefilling from zero",
3567                if on_device { "resident device" } else { "host" },
3568                self.device_sequence_position()
3569            );
3570            return 0;
3571        }
3572        n
3573    }
3574
3575    /// Public view of the prefix-reuse decision for `input_ids` (positions
3576    /// the next `generate*` would take from the cache; 0 = fresh sequence).
3577    pub fn reusable_prefix_len(&self, input_ids: &[u32]) -> usize {
3578        self.cached_prefix_len(input_ids)
3579    }
3580
3581    /// Record the forwarded prefix as the next turn's reuse key. A
3582    /// bounded-native model extends the rolling record (`reused` = the
3583    /// positions this turn found cached); others keep the literal vector.
3584    /// Either way the key carries its OWNER: the resident device graph
3585    /// (it holds an image of this sequence) or the host.
3586    fn record_consumed_prefix(&mut self, consumed: &[u32], reused: usize) {
3587        let on_device = self.device_sequence_position().is_some();
3588        if self.bounded_native() {
3589            let keep = reused > 0 && reused == self.kv_prefix.len() && reused <= consumed.len();
3590            let prev_device = self.kv_prefix.on_device();
3591            self.kv_history.clear();
3592            self.kv_history_device = false;
3593            if keep && prev_device == on_device {
3594                self.kv_prefix.extend(&consumed[reused..]);
3595            } else {
3596                self.kv_prefix.set(consumed);
3597            }
3598            self.kv_prefix.set_on_device(on_device);
3599        } else {
3600            self.kv_history = consumed.to_vec();
3601            self.kv_history_device = on_device;
3602        }
3603    }
3604
3605    /// True when at least one layer runs the O(1) kernel.
3606    pub fn o1_active(&self) -> bool {
3607        self.o1_cfg.is_some() && self.o1_flags.iter().any(|&f| f)
3608    }
3609
3610    /// Whether generation's prompt ingest is routed through the whole-token
3611    /// graph.  The bench uses this to label the measured generation prefill
3612    /// honestly; keep the predicate in Pipeline so CLI labels cannot drift
3613    /// from the production route.
3614    /// Positions per batched-graph submit for the prompt: `CMF_BATCH_K`
3615    /// when set (0 = one position at a time through the token graph),
3616    /// otherwise 128 (32 for a MoE stack) on a discrete card whose prompt
3617    /// takes the graph route.
3618    /// The batched graph read a 2048-token prompt at 53 tok/s against 28.5
3619    /// one position at a time on an RTX PRO 4000 (Qwen3.8-27B q4tp: TTFT
3620    /// 39 s against 72), and its states are the speculative verify's,
3621    /// measured identical to the plain path. macOS keeps its own arm.
3622    /// 128, not 32: the q4tp prefill GEMM tiles 64 batch rows, so a
3623    /// 32-row chunk left half of every tile idle. Qwen3.8-27B q4tp on an
3624    /// RTX 3090, 1000-token prompt: 51 tok/s at 32, 82 at 64, 95 at 128,
3625    /// 97 at 256; greedy output byte-identical to the 32-row chunks
3626    /// (every GEMM row and the k-looped recurrences are per position).
3627    pub fn generation_batch_k(&self) -> usize {
3628        if let Some(k) = std::env::var("CMF_BATCH_K")
3629            .ok()
3630            .and_then(|v| v.parse::<usize>().ok())
3631        {
3632            return k;
3633        }
3634        // Gemma-4 (`g4_batch_prefill`): 128-row chunks, the GEMM tile's
3635        // two 64-row halves full.
3636        if self.g4_batch_prefill() {
3637            return 128;
3638        }
3639        #[cfg(not(target_os = "macos"))]
3640        if self.graph_prefill_preferred() && !self.o1_active() {
3641            // MoE stacks keep the 32 their expert-grouped prefill was
3642            // measured at; 128 is measured on the dense hybrids only.
3643            let moe = self
3644                .weights
3645                .layers
3646                .iter()
3647                .any(|lw| matches!(&lw.ffn, FfnKind::Moe(_)));
3648            // q8_2f experts (moe_q82's grouped prefill) read each expert
3649            // once per group of up to 16 of its entries, so the chunk that
3650            // fills those groups is the faster ingest: Mellum2.1 q8_2f on an
3651            // RTX 3090, 4000-token prompt, 503 tok/s at 64 rows, 542 at 128
3652            // (and 542 at 256).
3653            let q82 = crate::gpu::moe_q82_graph_on()
3654                && self.weights.layers.iter().any(|lw| {
3655                    matches!(&lw.ffn, FfnKind::Moe(m)
3656                        if m.experts.first().is_some_and(|e| e.gate_proj.mapped_q8_2f().is_some()))
3657                });
3658            return if q82 {
3659                128
3660            } else if moe {
3661                32
3662            } else {
3663                128
3664            };
3665        }
3666        0
3667    }
3668
3669    pub fn generation_graph_prefill(&self) -> bool {
3670        let graph = self.graph_prefill_preferred();
3671        // On wgpu, an active MTP head now consumes the trunk's graph batches
3672        // and warms its own block from those returned rows.  The selected
3673        // generation measurement is therefore the batched path, even though
3674        // the underlying GDN model still satisfies the graph-prefill
3675        // predicate.  Keep the CLI label tied to the actual route.  Native
3676        // Metal has a separate prefill-batch arm and retains its historical
3677        // label here.
3678        // A batched prompt (`generation_batch_k` > 0) is the batched graph
3679        // for every model on the graph route, not only those with an MTP
3680        // head — the label follows the route.
3681        #[cfg(not(target_os = "macos"))]
3682        if graph
3683            && self.generation_batch_k() > 0
3684            && std::env::var("CMF_MTP_CHAIN_PROBE").is_err()
3685        {
3686            return false;
3687        }
3688        graph
3689    }
3690
3691    /// Device-side O(1) mirrors currently uploaded for this pipeline's
3692    /// sequence.  The count/bytes are zero before seal or after a fresh
3693    /// reset; callers use this to distinguish logical host state from the
3694    /// GPU allocation that actually serves decode.
3695    pub fn o1_device_stats(&self) -> (usize, u64) {
3696        crate::gpu::o1_device_stats(self.graph_kv_id)
3697    }
3698
3699    /// Arm query collection on the o1 layers (fresh prompt pass).
3700    /// Reset the o1 layers to Collecting for a fresh sequence. Pub for the
3701    /// network split: each side runs the o1 lifecycle over ITS OWN layers
3702    /// (begin before prefill, seal at the prefill barrier).
3703    pub fn o1_begin(&mut self) {
3704        self.o1_begin_with_prefix(None);
3705    }
3706
3707    /// Arm collection and optionally request a positive calibration prefix.
3708    /// The effective barrier is always at least the skeleton-safe floor, so
3709    /// a short requested prefix cannot create an exact-only runtime state.
3710    pub fn o1_begin_with_prefix(&mut self, requested_prefix: Option<usize>) {
3711        if let Some(c) = &self.o1_cfg {
3712            let (m, w, sink, rect) = (c.m, c.w, c.sink, c.rect);
3713            let boundary = requested_prefix.map(|p| {
3714                p.max(
3715                    crate::nystrom::o1_deferred_boundary(w, sink)
3716                        .expect("o1 config boundary validated in set_o1"),
3717                )
3718            });
3719            for (li, &f) in self.o1_flags.iter().enumerate() {
3720                if f {
3721                    self.kv_cache.layers[li].o1_begin_with_boundary(m, w, sink, rect, boundary);
3722                }
3723            }
3724        }
3725    }
3726
3727    /// Effective deferred boundary for a positive prefix request.
3728    fn o1_effective_boundary(&self, requested_prefix: usize) -> Option<usize> {
3729        self.o1_cfg.as_ref().and_then(|c| {
3730            crate::nystrom::o1_deferred_boundary(c.w, c.sink)
3731                .map(|floor| requested_prefix.max(floor))
3732        })
3733    }
3734
3735    fn o1_note_transition(&mut self) {
3736        // Drain every layer's one-shot bit before publishing one pipeline
3737        // epoch. `any()` would short-circuit on the first layer and leak the
3738        // remaining bits into later forwards, causing one epoch per layer.
3739        let mut transitioned = false;
3740        for (li, &flagged) in self.o1_flags.iter().enumerate() {
3741            if flagged {
3742                transitioned |= self.kv_cache.layers[li].take_o1_transition();
3743            }
3744        }
3745        if transitioned {
3746            self.o1_epoch = self.o1_epoch.wrapping_add(1);
3747        }
3748    }
3749
3750    fn o1_pending(&self) -> bool {
3751        self.o1_flags.iter().enumerate().any(|(li, &f)| {
3752            f && self.kv_cache.layers[li].seq_len > 0
3753                && self.kv_cache.layers[li].o1_pending_boundary().is_some()
3754        })
3755    }
3756
3757    fn o1_fail(&mut self, err: String) {
3758        tracing::error!("o1 deferred seal failed; terminating sequence: {err}");
3759        self.clear_sequence_state();
3760        self.graph_failed
3761            .store(true, std::sync::atomic::Ordering::Relaxed);
3762        self.cancel
3763            .store(true, std::sync::atomic::Ordering::Relaxed);
3764    }
3765
3766    /// Seal participating layers while retaining the exact state when the
3767    /// prompt is below the deferred boundary. A split worker may have
3768    /// collecting layers outside its owned span; zero-depth layers remain
3769    /// armed and are intentionally skipped until their peer runs them.
3770    pub fn o1_seal_checked(&mut self) -> Result<bool, String> {
3771        if self.o1_cfg.is_none() {
3772            return Ok(false);
3773        }
3774        let mut participating = false;
3775        for li in 0..self.num_layers {
3776            if !self.o1_flags.get(li).copied().unwrap_or(false) {
3777                continue;
3778            }
3779            if let Some(err) = self.kv_cache.layers[li].take_o1_error() {
3780                return Err(err);
3781            }
3782            if self.kv_cache.layers[li].seq_len == 0 {
3783                continue;
3784            }
3785            participating = true;
3786            let num_heads = self.layer_num_heads(li);
3787            self.kv_cache.layers[li].o1_seal_checked(num_heads)?;
3788        }
3789        self.o1_note_transition();
3790        for li in 0..self.num_layers {
3791            if self.o1_flags.get(li).copied().unwrap_or(false) {
3792                if let Some(err) = self.kv_cache.layers[li].take_o1_error() {
3793                    return Err(err);
3794                }
3795            }
3796        }
3797        Ok(participating
3798            && (0..self.num_layers).all(|li| {
3799                !self.o1_flags.get(li).copied().unwrap_or(false)
3800                    || self.kv_cache.layers[li].seq_len == 0
3801                    || self.kv_cache.layers[li].o1_sealed()
3802            }))
3803    }
3804
3805    /// Complete a deferred boundary after a full position/span forward.
3806    /// This is the pipeline owner for epoch publication and failure cleanup.
3807    fn o1_progress(&mut self) {
3808        if !self.o1_active() {
3809            return;
3810        }
3811        for li in 0..self.num_layers {
3812            if self.o1_flags.get(li).copied().unwrap_or(false) {
3813                if let Some(err) = self.kv_cache.layers[li].take_o1_error() {
3814                    self.o1_fail(err);
3815                    return;
3816                }
3817            }
3818        }
3819        // A qwen_attention row can seal in the middle of a complete layer
3820        // walk. Consume its transition even though the pending boundary has
3821        // already disappeared from the cache.
3822        self.o1_note_transition();
3823        if !self.o1_pending() {
3824            return;
3825        }
3826        if let Err(err) = self.o1_seal_checked() {
3827            self.o1_fail(err);
3828        }
3829    }
3830
3831    /// Turn a deferred O(1) failure raised by a hidden-only forward into the
3832    /// Result error its public batch/span caller must return. The failure
3833    /// path already cleared host/device sequence state; consume only the
3834    /// side-channel marker here and leave the pipeline reusable.
3835    fn check_o1_progress_failure(&mut self, phase: &str) -> Result<(), String> {
3836        if self
3837            .graph_failed
3838            .swap(false, std::sync::atomic::Ordering::Relaxed)
3839        {
3840            self.cancel
3841                .store(false, std::sync::atomic::Ordering::Relaxed);
3842            self.clear_sequence_state();
3843            return Err(format!("{phase}: deferred O(1) transition failed"));
3844        }
3845        Ok(())
3846    }
3847
3848    /// Freeze landmarks + skeleton state after the prompt pass and drop
3849    /// the o1 layers' full KV; decode then runs `step()` per token.
3850    /// Pub for the network split (see `o1_begin`).
3851    pub fn o1_seal(&mut self) {
3852        if let Err(err) = self.o1_seal_checked() {
3853            self.o1_fail(err);
3854        }
3855    }
3856
3857    /// Enable/disable the structured per-token telemetry trace (B4).
3858    pub fn set_trace(&mut self, on: bool) {
3859        self.trace = on;
3860    }
3861
3862    /// Replace all request-scoped sampler options and reset the random stream.
3863    /// This is required for deterministic `seed` semantics in pooled servers.
3864    pub fn set_sampler_config(&mut self, config: SamplerConfig) {
3865        self.rng = match config.seed {
3866            Some(seed) => SplitMix64::new(seed),
3867            None => SplitMix64::from_entropy(),
3868        };
3869        self.sampler_config = config;
3870    }
3871
3872    /// Toggle the per-token confidence reduction (a full-vocab
3873    /// softmax each token). `bench --core` turns it off so the timed
3874    /// loop matches llama-bench's core contract; the result's
3875    /// `confidence` vec is empty while off.
3876    pub fn set_confidence(&mut self, on: bool) {
3877        self.confidence_on = on;
3878    }
3879
3880    /// Set the confidence-calibration temperature (B1). Values ≤0 are
3881    /// clamped to raw (1.0).
3882    pub fn set_calib_temp(&mut self, t: f32) {
3883        self.calib_temp = if t > 1e-3 { t } else { 1.0 };
3884    }
3885
3886    /// The active calibration temperature (1.0 = raw probability).
3887    pub fn calib_temp(&self) -> f32 {
3888        self.calib_temp
3889    }
3890
3891    /// The primary (full-attention) RoPE inverse-frequency table selected
3892    /// from the CMF architecture.  This is read-only diagnostic state for
3893    /// parity checks; local sliding layers may use `inv_freq_local` instead.
3894    pub fn rope_inv_freq(&self) -> &[f32] {
3895        self.inv_freq.as_slice()
3896    }
3897
3898    /// Partial rotary (Qwen3.5): rotate only the first `rotary_dim` dims;
3899    /// the frequency table is rebuilt over the rotary dims.
3900    pub fn set_rotary(&mut self, rotary_dim: usize, base: f32) {
3901        self.rotary_dim = rotary_dim.min(self.head_dim);
3902        self.inv_freq = std::sync::Arc::new(attention::rope_inv_freq(self.rotary_dim, base));
3903        // The packed resident graph owns its own inverse-frequency plane;
3904        // changing RoPE after it was built must not leave a stale device
3905        // model behind the exact host configuration.
3906        self.embryo_graph = None;
3907    }
3908
3909    fn attn_cfg(&self, position: usize) -> QwenAttnCfg<'_> {
3910        QwenAttnCfg {
3911            num_heads: self.num_heads,
3912            num_kv_heads: self.num_kv_heads,
3913            head_dim: self.head_dim,
3914            hidden_size: self.hidden_size,
3915            position,
3916            inv_freq: &self.inv_freq,
3917            rotary_dim: self.rotary_dim,
3918            scale: self.attn_scale,
3919            softcap: self.attn_softcap,
3920            window: None,
3921            v_norm: false,
3922            qk_norm_after_rope: self.qk_norm_after_rope,
3923            gate_sigmoid: self.proj_gate_sigmoid,
3924            q_norm: None,
3925            k_norm: None,
3926            output_gate: false,
3927            softplus_gate: None,
3928            rope_scale: self.rope_scale,
3929            bias: None,
3930            rms_eps: self.rms_eps,
3931            norm_style: self.norm_style,
3932            pool: self.pool.as_deref(),
3933            v_head_dim: self.v_head_dim.unwrap_or(self.head_dim),
3934        }
3935    }
3936
3937    /// Generate text from a plain-text prompt. Streams tokens via `on_token`.
3938    pub fn generate(
3939        &mut self,
3940        prompt: &str,
3941        max_tokens: usize,
3942        task_mask: Option<&TaskMask>,
3943        on_token: Option<TokenCallback>,
3944    ) -> Result<GenerateResult, String> {
3945        let input_ids = self.tokenizer.with_bos(self.tokenizer.encode(prompt));
3946        self.generate_from_ids(&input_ids, max_tokens, task_mask, on_token)
3947    }
3948
3949    /// Generate from a V4.1 multimodal prompt prepared by the vision module.
3950    /// Vision rows are encoded once and fed through the same bounded token walk as text.
3951    pub fn generate_from_vl(
3952        &mut self,
3953        input: &crate::dsv41_vision::PreparedVlInputs,
3954        max_tokens: usize,
3955        task_mask: Option<&TaskMask>,
3956        on_token: Option<TokenCallback>,
3957    ) -> Result<GenerateResult, String> {
3958        let Some(dsv41) = &self.dsv41 else {
3959            return Err("V4.1 multimodal input requires a DeepSeek-V4.1 pipeline".into());
3960        };
3961        if input.token_ids.is_empty() {
3962            return Err("empty V4.1 multimodal prompt".into());
3963        }
3964        if input.token_types.len() != input.token_ids.len() {
3965            return Err(format!(
3966                "V4.1 token type count {} != token count {}",
3967                input.token_types.len(),
3968                input.token_ids.len()
3969            ));
3970        }
3971        let dim = dsv41.2.dim;
3972        let mut embeddings = vec![None; input.token_ids.len()];
3973        let mut participates = vec![true; input.token_ids.len()];
3974        if !input.images.is_empty() {
3975            let vision = self
3976                .dsv41_vision
3977                .as_ref()
3978                .ok_or_else(|| "V4.1 image prompt has no loaded vision tower".to_string())?;
3979            for image in &input.images {
3980                let end = image.start.saturating_add(image.types.len());
3981                if end > input.token_ids.len() {
3982                    return Err(format!(
3983                        "V4.1 image span {}..{} exceeds prompt length {}",
3984                        image.start,
3985                        end,
3986                        input.token_ids.len()
3987                    ));
3988                }
3989                let mut span = vec![0.0f32; image.types.len() * dim];
3990                vision.fill_image_span(image, &mut span, self.pool.as_deref())?;
3991                for (offset, &kind) in image.types.iter().enumerate() {
3992                    let pos = image.start + offset;
3993                    if input.token_types[pos] != kind {
3994                        return Err(format!(
3995                            "V4.1 image type mismatch at position {pos}: {} != {kind}",
3996                            input.token_types[pos]
3997                        ));
3998                    }
3999                    embeddings[pos] = Some(span[offset * dim..(offset + 1) * dim].to_vec());
4000                    participates[pos] = false;
4001                }
4002            }
4003        }
4004        for (pos, &kind) in input.token_types.iter().enumerate() {
4005            if kind == crate::dsv41_vision::TEXT && embeddings[pos].is_some() {
4006                return Err(format!("V4.1 text position {pos} has an image embedding"));
4007            }
4008            if kind != crate::dsv41_vision::TEXT && embeddings[pos].is_none() {
4009                return Err(format!("V4.1 image position {pos} has no image embedding"));
4010            }
4011        }
4012        self.dsv41_prefill = Some((embeddings, participates));
4013        let result = self.generate_from_ids(&input.token_ids, max_tokens, task_mask, on_token);
4014        self.dsv41_prefill = None;
4015        result
4016    }
4017
4018    /// `None` when the mask forbids nothing (see `TaskMask::fully_open`).
4019    fn drop_open_mask<'m>(&self, m: Option<&'m TaskMask>) -> Option<&'m TaskMask> {
4020        m.filter(|m| !m.fully_open(self.intermediate_size, self.num_heads))
4021    }
4022
4023    /// Generate from prepared token ids (e.g. a chat template).
4024    ///
4025    /// With an MTP head, greedy generation without a task mask takes the
4026    /// speculative path: the MTP module drafts the token after next and
4027    /// the main model verifies both in one fused two-position forward
4028    /// (weights streamed once). The output is EXACTLY the vanilla greedy
4029    /// sequence — a rejected draft is rolled back — MTP only buys speed.
4030    pub fn generate_from_ids(
4031        &mut self,
4032        input_ids: &[u32],
4033        max_tokens: usize,
4034        task_mask: Option<&TaskMask>,
4035        on_token: Option<TokenCallback>,
4036    ) -> Result<GenerateResult, String> {
4037        self.generate_with_prompt_rows(input_ids, None, max_tokens, task_mask, on_token)
4038    }
4039
4040    /// Generate from complete prompt embeddings [token_count, hidden_size].
4041    /// Text rows can be obtained with `embed_id`; media rows replace only
4042    /// their expanded placeholder positions. Rows are already scaled and
4043    /// enter `PrefillIn::Hidden`, so a device graph must not re-embed them.
4044    /// Token-only KV reuse is disabled both into and out of this request.
4045    pub fn generate_from_embeds(
4046        &mut self,
4047        input_ids: &[u32],
4048        prompt_rows: &[f32],
4049        max_tokens: usize,
4050        task_mask: Option<&TaskMask>,
4051        on_token: Option<TokenCallback>,
4052    ) -> Result<GenerateResult, String> {
4053        if input_ids.is_empty()
4054            || input_ids.len().checked_mul(self.hidden_size) != Some(prompt_rows.len())
4055        {
4056            return Err("embedded prompt dimensions must be [tokens, hidden_size]".into());
4057        }
4058        if prompt_rows.iter().any(|x| !x.is_finite()) {
4059            return Err("embedded prompt contains non-finite values".into());
4060        }
4061        if !self.can_prefill_batched() || self.dyn_router.is_some()
4062            || self.o1_active() || self.mtp.is_some() || self.gpu_plan.is_some()
4063        {
4064            return Err("embedded prompts require the ordinary transformer path without O(1), dynamic routing, GPU splitting or a generic MTP head".into());
4065        }
4066        self.generate_with_prompt_rows(input_ids, Some(prompt_rows), max_tokens, task_mask, on_token)
4067    }
4068
4069    fn generate_with_prompt_rows(
4070        &mut self,
4071        input_ids: &[u32],
4072        prompt_rows: Option<&[f32]>,
4073        max_tokens: usize,
4074        task_mask: Option<&TaskMask>,
4075        mut on_token: Option<TokenCallback>,
4076    ) -> Result<GenerateResult, String> {
4077        #[cfg(target_os = "macos")]
4078        crate::gpu_metal::set_io_namespace(self.graph_kv_id);
4079        if std::env::var("CMF_TRACE_H").is_ok() {
4080            eprintln!("input_ids: {input_ids:?}");
4081        }
4082        if input_ids.is_empty() {
4083            return Err("empty prompt: nothing to generate from".to_string());
4084        }
4085        // A prior graph failure is terminal for that sequence but must not
4086        // poison the next independent request.  Keep this flag separate from
4087        // the externally-owned cooperative cancel bit.
4088        self.graph_failed
4089            .store(false, std::sync::atomic::Ordering::Relaxed);
4090        // A mask that forbids nothing still costs every fused path and
4091        // whole-token graph, all of which are gated on `is_none()`. A
4092        // narrowed file whose one segment is always on carries exactly
4093        // such a mask — drop it here rather than pay 5x for a no-op.
4094        let task_mask = self.drop_open_mask(task_mask);
4095
4096        // Cross-turn KV reuse: a chat app resends the whole history
4097        // every turn; when the new ids strictly EXTEND what the cache
4098        // already holds, prefill only the tail — turn latency stays
4099        // proportional to the new text instead of the whole session.
4100        // Extension-only (no rollback), so it is exact for every layer
4101        // kind including recurrent state; MTP/o1/task-mask runs keep
4102        // the fresh-sequence path. CMF_KV_REUSE=0 disables.
4103        let mut reuse_from = {
4104            let on = !std::env::var("CMF_KV_REUSE").is_ok_and(|v| v == "0");
4105            if on
4106                && prompt_rows.is_none()
4107                && task_mask.is_none()
4108                && self.mtp.is_none()
4109                && !(self.mimo_mtp.is_some() && self.speculative)
4110                && self.o1_cfg.is_none()
4111                && self.dsv41.is_none()
4112            {
4113                self.cached_prefix_len(input_ids)
4114            } else {
4115                0
4116            }
4117        };
4118        // The device may own rows the host tail prefill needs (wgpu decode
4119        // writes only its mirror): hand them to the host, or start fresh.
4120        if reuse_from > 0 && !self.prepare_kv_reuse(reuse_from) {
4121            reuse_from = 0;
4122        }
4123        self.last_prefill_tokens = input_ids.len() - reuse_from;
4124        let bounded_native = self.bounded_native();
4125        if reuse_from == 0 {
4126            // Fresh sequence — the cache holds absolute positions.
4127            self.clear_sequence_state();
4128        } else if std::env::var("CMF_PREFILL_PROF").is_ok() {
4129            eprintln!(
4130                "kv-reuse: {} of {} prompt positions already cached",
4131                reuse_from,
4132                input_ids.len()
4133            );
4134        }
4135        crate::gpu::graph_race_begin_generation();
4136        // Optional bounded calibration prefix. Keep the requested value
4137        // even when it is longer than the prompt; the collecting layer will
4138        // defer at the effective boundary and remain exact for short input.
4139        let o1_prefill = if self.o1_active() && task_mask.is_none() {
4140            std::env::var("CMF_O1_PREFILL")
4141                .ok()
4142                .and_then(|v| v.parse::<usize>().ok())
4143                .filter(|&p| p > 0)
4144        } else {
4145            None
4146        };
4147        if task_mask.is_none() {
4148            self.o1_begin_with_prefix(o1_prefill);
4149        }
4150
4151        // Speculative decode is off under o1: a rejected draft can't be
4152        // rolled back out of the far accumulators / ring window (the
4153        // Nyström insertion is irreversible by design).
4154        // The wgpu token graph owns a device K/V mirror that speculative
4155        // rollback would desync — the two are mutually exclusive.
4156        let graph_on = crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Decode);
4157        // Graph speculative decode (`CMF_GRAPH_SPEC=1`): the MTP head
4158        // drafts, ONE batched graph submit verifies the whole chain.
4159        //
4160        // It now PAYS on Qwen3.6-27B / RTX 5090 — 51.1 tok/s against a
4161        // plain 49.4 at k=3, medians of three, 89% of drafts accepted,
4162        // and the greedy continuation is byte-identical to the plain
4163        // path. That took the batch matvec sharing its nibble unpack
4164        // across the batch (`CMF_MV_BK=2`); before it, the same round
4165        // measured 43.6, an 11% LOSS, which is what the earlier note
4166        // here described.
4167        //
4168        // Still opt-in. One model's win is not a default: the verify
4169        // rides `gdn_spec_restore` and a batched frame whose numerics
4170        // are the batch kernels', and that has to be shown on more than
4171        // one architecture before every greedy decode takes it.
4172        // Greedy (with or without penalties) verifies by argmax equality.
4173        // Sampling (temperature > 0) can go through speculative SAMPLING —
4174        // draft from the MTP head's own post-chain distribution, accept
4175        // with min(1, p/q), correct from max(0, p − q); the emitted stream
4176        // is distributed exactly as the plain sampler's — but it is
4177        // OPT-IN (`CMF_GRAPH_SPEC_SAMPLE=1`): measured on Qwen3.8-27B /
4178        // RTX 5090 at the instruct row (0.7 / 0.80 / 20 / presence 1.5)
4179        // it decoded 19-22 tok/s against a plain 40 — nine post-chain
4180        // distributions a round plus a lower acceptance than greedy's,
4181        // against a verify that costs 2.7 single tokens. The greedy arms
4182        // pay +10%; the sampling arm needs a cheaper verify first.
4183        // Native Metal HAS that verify: its eight-row tile is flat in b,
4184        // so a round costs ~1.9 plain tokens and the sampling arm pays at
4185        // 2.3 accepted per round — measured on Qwen3.8-27B q4tp / M4 at
4186        // the CLI defaults (0.7 / rep 1.1 / top-k 40, seed 42), a code
4187        // prompt: 9.0 tok/s against a plain 5.4 in the same window, and
4188        // the per-round watchdog turns it off where prose loses. So on
4189        // Metal the sampling arm is ON (`CMF_GRAPH_SPEC_SAMPLE=0` opts out)
4190        // — but only for a config the SPARSE chain serves (a top-k within
4191        // `sparse_ok`): without it a round builds nine 248k-float
4192        // distributions on the host, which is the 5090's measured loss and
4193        // not a cost the round-token proxy below can see. A top-k-less
4194        // sampling config keeps the plain path unless asked for by name.
4195        #[cfg(target_os = "macos")]
4196        let metal_graph = crate::gpu::q1_force()
4197            && crate::gpu::enabled_here()
4198            && std::env::var("CMF_GPU_BLOCK")
4199                .map(|v| v != "0")
4200                .unwrap_or(true);
4201        #[cfg(not(target_os = "macos"))]
4202        let metal_graph = false;
4203        let spec_sample_env = std::env::var("CMF_GRAPH_SPEC_SAMPLE").ok();
4204        // A round whose cost is the MEASURED one: greedy (argmax rows), or
4205        // sampling through the sparse chain. Anything else pays the dense
4206        // chain's host time, which no proxy can price.
4207        let spec_cheap_round = self.sampler_config.temperature < 1e-6
4208            || sampler::sparse_ok(&self.sampler_config);
4209        let spec_sampling_ok = self.sampler_config.temperature < 1e-6
4210            || match spec_sample_env.as_deref() {
4211                Some("1") => true,
4212                Some(_) => false,
4213                None => metal_graph && spec_cheap_round,
4214            };
4215        // ON by default for greedy on the wgpu graph: with the draft on
4216        // the graph and the verify bit-exact, it measured 58.7 tok/s
4217        // against a plain 48.1 on Qwen3.8-27B q4tp / RTX 5090 (k=4) and
4218        // 51.1 against 49.4 on Qwen3.6-27B, and a round that stops
4219        // paying turns itself off below (acceptance watchdog).
4220        // `CMF_GRAPH_SPEC=0` disables; `=1` was the old opt-in spelling.
4221        // …but only where the batched verify has its register-blocked
4222        // kernel: q4tp dense FFNs (graph kind 6). q4t and q8_2f verify
4223        // through tile GEMMs today and measured a LOSS (q8_2f 22 against
4224        // 29 tok/s), the 2-bit plane the same; those stay opt-in
4225        // (`CMF_GRAPH_SPEC=1`).
4226        // …at least in nine dense FFNs of ten: a healed file carries its
4227        // last two layers at q8_2f, and two tile-GEMM verifies among 64 do
4228        // not change the arithmetic (measured: the healed q4tp file
4229        // decodes at the plain file's rate and would otherwise sit out).
4230        let (mut dense_n, mut dense_q4tp) = (0usize, 0usize);
4231        for lw in &self.weights.layers {
4232            if let FfnKind::Dense(d) = &lw.ffn {
4233                dense_n += 1;
4234                if matches!(d.gate_proj.graph_weight(), Some((_, _, 6, _)))
4235                    && matches!(d.up_proj.graph_weight(), Some((_, _, 6, _)))
4236                    && matches!(d.down_proj.graph_weight(), Some((_, _, 6, _)))
4237                {
4238                    dense_q4tp += 1;
4239                }
4240            }
4241        }
4242        let spec_default_ok = dense_n == 0 || dense_q4tp * 10 >= dense_n * 9;
4243        // Penalties break the draft head's agreement with the trunk (a
4244        // 1.1 repetition penalty measured 2 of 16 accepted): not by
4245        // default there either — off Metal that rule is untouched, and
4246        // suppressed ids keep counting as a penalty there, because no
4247        // measurement on a discrete card says otherwise.
4248        //
4249        // On native Metal the penalized arms DO pay: the draft applies
4250        // the same penalty and the verify scores the penalized rows
4251        // exactly (`greedy_pen`, the plain loop's arithmetic), so the
4252        // text is the plain path's and only the round's shape changes.
4253        // Measured on this M4 — see the report for the interleaved run.
4254        let penalized = !metal_graph
4255            && (self.sampler_config.repetition_penalty != 1.0
4256                || self.sampler_config.presence_penalty != 0.0
4257                || !self.sampler_config.suppress_tokens.is_empty());
4258        // …and not on wgpu-over-Metal: the batched verify graph there
4259        // returned 0 accepted drafts and garbage text on a GDN hybrid
4260        // (16.08, Qwen3.5-0.8B) while Vulkan is bit-exact; the Mac's
4261        // default backend is native Metal without a batch graph anyway.
4262        #[cfg(feature = "gpu")]
4263        let metal_wgpu = graph_on && crate::gpu_wgpu::wgpu_backend_is_metal();
4264        #[cfg(not(feature = "gpu"))]
4265        let metal_wgpu = false;
4266        let spec_env = std::env::var("CMF_GRAPH_SPEC").ok();
4267        let spec_wanted = match spec_env.as_deref() {
4268            Some("0") => false,
4269            Some(_) => {
4270                if metal_wgpu {
4271                    tracing::warn!(
4272                        "CMF_GRAPH_SPEC forced on wgpu/Metal: the batched verify graph is not \
4273                         verified on this backend (garbage measured on Qwen3.5-0.8B)"
4274                    );
4275                }
4276                true
4277            }
4278            None => spec_default_ok && !penalized && !metal_wgpu,
4279        };
4280        // Native Metal: the b-row verify graph (`try_batch_graph_metal`)
4281        // stands where the wgpu batch graph stands on discrete cards
4282        // (`metal_graph`, above).
4283        let graph_spec = self.speculative
4284            && (graph_on || metal_graph)
4285            && self.mtp.is_some()
4286            && task_mask.is_none()
4287            && !self.o1_active()
4288            && spec_sampling_ok
4289            && spec_wanted;
4290        // Native Metal: say the route ONCE (RUST_LOG=info), so a user can
4291        // confirm the fast path without setting a single flag — every
4292        // knob below defaults to the measured-best value on the M4.
4293        #[cfg(target_os = "macos")]
4294        if metal_graph {
4295            static SAID: std::sync::Once = std::sync::Once::new();
4296            SAID.call_once(|| {
4297                let spec = if graph_spec {
4298                    let k = std::env::var("CMF_GRAPH_SPEC_K")
4299                        .ok()
4300                        .and_then(|v| v.parse::<usize>().ok())
4301                        .filter(|&v| (1..=8).contains(&v))
4302                        .unwrap_or(7);
4303                    let arm = if self.sampler_config.temperature < 1e-6 {
4304                        "greedy"
4305                    } else {
4306                        "sampling"
4307                    };
4308                    format!(
4309                        "spec k={k} {arm} (batched verify, draft shortlist {}, trial: proxy)",
4310                        Self::draft_vocab_rows(usize::MAX)
4311                    )
4312                } else if !self.speculative {
4313                    "spec off (CMF_MTP=0)".to_string()
4314                } else if self.mtp.is_none() {
4315                    "spec off (no MTP head)".to_string()
4316                } else if !spec_sampling_ok {
4317                    if spec_cheap_round {
4318                        "spec off (CMF_GRAPH_SPEC_SAMPLE=0)".to_string()
4319                    } else {
4320                        "spec off (sampling without a top-k: the dense chain \
4321                         costs more than it saves)"
4322                            .to_string()
4323                    }
4324                } else if !spec_wanted {
4325                    "spec off (CMF_GRAPH_SPEC=0 or non-q4tp FFNs)".to_string()
4326                } else if task_mask.is_some() {
4327                    "spec off (task mask)".to_string()
4328                } else {
4329                    "spec off (O(1) attention)".to_string()
4330                };
4331                let on = |var: &str| {
4332                    if std::env::var(var).as_deref() == Ok("0") {
4333                        "off"
4334                    } else {
4335                        "on"
4336                    }
4337                };
4338                tracing::info!(
4339                    "metal native: {spec}, state4 {}, async replay {}, prefill graph {}, \
4340                     MTP graph {}, attend {}, probe {}",
4341                    if crate::gpu_metal::state4_on() { "on" } else { "off" },
4342                    if crate::gpu_metal::async_replay_on() { "on" } else { "off" },
4343                    on("CMF_METAL_PREFILL"),
4344                    on("CMF_MTP_GRAPH"),
4345                    std::env::var("CMF_GPU_ATTEND").unwrap_or_else(|_| "auto".into()),
4346                    if crate::gpu::probe_enabled() { "bypassed (q1 force)" } else { "off" },
4347                );
4348            });
4349        }
4350        // GDN hybrids sit the fused-pair speculation out by default: the
4351        // recurrence is sequential, so the pair lane cannot parallelize
4352        // (the bench's own Pair line reads fused 1.28x TWO singles on the
4353        // 35B) and the draft's full-vocab head rides on top — measured 2x
4354        // SLOWER end to end (16.1 vs 32.4 tok/s on the 48-core stand).
4355        // CMF_MTP=1 forces it back for study.
4356        let pair_pays = self.gdn_cfg.is_none() || std::env::var("CMF_MTP").as_deref() == Ok("1");
4357        let spec_active = self.speculative
4358            && self.mtp.is_some()
4359            && task_mask.is_none()
4360            && !self.o1_active()
4361            && ((!graph_on && pair_pays && self.sampler_config.temperature < 1e-6) || graph_spec);
4362        // The MTP module is detached during generation so its mutable
4363        // state does not fight the borrow on `self`.
4364        let mut mtp = if spec_active { self.mtp.take() } else { None };
4365        if std::env::var("CMF_MTP_CHAIN_PROBE").is_ok() {
4366            eprintln!(
4367                "mtp-probe gate: spec_active={spec_active} mtp={} speculative={} graph_on={graph_on} temp_ok={}",
4368                mtp.is_some(),
4369                self.speculative,
4370                self.sampler_config.temperature < 1e-6,
4371            );
4372        }
4373        if let Some(m) = &mut mtp {
4374            m.kv.clear();
4375            // The MTP block's own device mirror starts over with its cache.
4376            crate::gpu::graph_kv_reset(self.mtp_kv_id());
4377            self.mtp_graph_mode = None;
4378        }
4379        // MiMo-V2's draft stack: greedy rounds (draft K with the chained
4380        // MTP layers, verify K+1 rows in one batched forward). Sampling
4381        // decodes plain; `CMF_MTP=0` / `CMF_MIMO_MTP=0` turn it off.
4382        let mimo_spec = self.speculative
4383            && self.mimo_mtp.is_some()
4384            && task_mask.is_none()
4385            && !self.o1_active()
4386            && self.dyn_router.is_none()
4387            && self.sampler_config.temperature < 1e-6
4388            && std::env::var("CMF_MIMO_MTP").as_deref() != Ok("0");
4389        if let Some(st) = self.mimo_mtp.as_mut() {
4390            st.reset();
4391            if mimo_spec && std::env::var_os("CMF_MIMO_MTP_PROBE").is_some() {
4392                Self::mimo_mtp_hist_cap(st, input_ids.len());
4393            }
4394        }
4395        // Dynamic router detached during decode (same borrow trick as MTP).
4396        // Speculative decode and dynamic routing are mutually exclusive
4397        // for now — the fused-pair path doesn't carry per-token φ.
4398        let mut router = if mtp.is_none() {
4399            self.dyn_router.take()
4400        } else {
4401            None
4402        };
4403        let mut reuse_from = reuse_from;
4404        if let Some(r) = &mut router {
4405            r.reset(); // active=backbone, matching a fresh overlay
4406            self.dyn_phi_seen = 0; // fresh φ EMA per generation
4407            if self.dyn_active.is_some() {
4408                // A real switch back to the backbone invalidates the
4409                // cache the reuse key was computed against.
4410                let _ = self.set_active_skill(None);
4411                reuse_from = 0;
4412                self.last_prefill_tokens = input_ids.len();
4413            }
4414        }
4415
4416        let mut all_ids = input_ids.to_vec();
4417        let mut generated = 0usize;
4418        let mut finish_reason = "max_tokens".to_string();
4419        let mut drafted = 0usize;
4420        let mut accepted = 0usize;
4421        // DeepSeek-V4's draft quality is strongly content-dependent.  Two
4422        // consecutive paid rounds with no extra token put it on a bounded
4423        // cooldown; predictable text keeps batching, ordinary prose falls
4424        // back to the exact walk instead of paying a slow draft forever.
4425        // Local to one generation so one difficult request cannot poison the
4426        // next one, and deliberately automatic — this is not a user knob.
4427        let mut dsv4_spec_bad = 0usize;
4428        let mut dsv4_spec_retry_at = 0usize;
4429        let mut confidence: Vec<f32> = Vec::new();
4430        let trace_on = self.trace;
4431        let calib_temp = self.calib_temp;
4432        let mut traces: Vec<TokenTrace> = Vec::new();
4433
4434        // ── Prefill: forward each prompt token once, KEEP the last hidden.
4435        //    Dense prefill runs in fused pairs (weights streamed once per
4436        //    two positions — bit-identical to sequential, proven by the
4437        //    pair tests). With MTP: warm the draft head on
4438        //    (hidden_p, token_{p+1}) pairs.
4439        let mut hidden = vec![0.0f32; self.hidden_size];
4440        let mut pos = reuse_from;
4441        // lm_head-in-graph is only sound when the very next logits
4442        // consumer is this loop's own (MTP and skill routing interleave
4443        // other forwards / can swap lm_head between forward and sample).
4444        // CMF_GPU_LMHEAD=0 keeps lm_head off the graph: the token reads back
4445        // the 8 KB hidden instead of ~1 MB of logits, and the head runs on
4446        // the host. A probe for how much of the graph's fixed per-token cost
4447        // is the logits readback (the layer sweep puts that fixed part at
4448        // 3.88 ms of an 18.5 ms frame).
4449        let fuse_lm = mtp.is_none()
4450            && router.is_none()
4451            && std::env::var("CMF_GPU_LMHEAD").as_deref() != Ok("0");
4452        self.graph_logits = None;
4453        self.graph_want_logits = false;
4454        let _tpf = std::time::Instant::now();
4455        let batch_k = self.generation_batch_k();
4456        if let Some(rows) = prompt_rows {
4457            let hs = self.hidden_size;
4458            let chunk = self.prefill_chunk().max(1);
4459            while pos < input_ids.len() && !self.cancel.load(std::sync::atomic::Ordering::Relaxed) {
4460                let end = (pos + chunk).min(input_ids.len());
4461                let hb = match self.prefill_input_rows(
4462                    PrefillIn::Hidden(&rows[pos * hs..end * hs]), pos, task_mask,
4463                ) {
4464                    Ok(hb) => hb,
4465                    Err(err) => {
4466                        self.finish_generation(&mut mtp, &mut router, true);
4467                        return Err(err);
4468                    }
4469                };
4470                if mimo_spec { self.mimo_note_rows(&hb, pos); }
4471                hidden.copy_from_slice(&hb[hb.len() - hs..]);
4472                pos = end;
4473            }
4474        }
4475        // DeepSeek-V4 owns a separate hyper-connection stack. Route it
4476        // before the generic prefill choices: those correctly reject an
4477        // empty `weights.layers`, but their final per-position fallback used
4478        // to consume the whole prompt before `dsv4::forward_chunk` could see
4479        // it. The batch implementation therefore existed without a live
4480        // production entry point.
4481        //
4482        // Bounded chunks preserve cancellation responsiveness. Only the
4483        // prompt's final chunk asks for logits; every earlier head projection
4484        // would produce 129 280 values that no caller reads.
4485        while self.qwen4_exp.is_some()
4486            && mtp.is_none()
4487            && pos < input_ids.len()
4488            && !self.cancel.load(std::sync::atomic::Ordering::Relaxed)
4489        {
4490            // The device path takes several prompt tokens per layer frame;
4491            // the host path runs them one by one inside the same call.
4492            let end = (pos + crate::qwen4_exp::prefill_chunk()).min(input_ids.len());
4493            let want_logits = end == input_ids.len();
4494            let mut lg = Vec::new();
4495            if let Some(b) = &mut self.qwen4_exp {
4496                crate::qwen4_exp::forward_tokens(
4497                    &b.0,
4498                    &b.1,
4499                    &b.2,
4500                    &mut b.3,
4501                    &input_ids[pos..end],
4502                    pos,
4503                    &self.inv_freq,
4504                    self.pool.as_deref(),
4505                    &mut lg,
4506                    want_logits,
4507                );
4508            }
4509            if want_logits {
4510                self.graph_logits = Some(lg);
4511            }
4512            pos = end;
4513            hidden.fill(0.0);
4514        }
4515        while self.dsv4.is_some()
4516            && mtp.is_none()
4517            && pos < input_ids.len()
4518            && !self.cancel.load(std::sync::atomic::Ordering::Relaxed)
4519        {
4520            let end = (pos + prefill_chunk()).min(input_ids.len());
4521            let ids: Vec<u32> = input_ids[pos..end].to_vec();
4522            let mut lg = Vec::new();
4523            if let Some(b) = &mut self.dsv4 {
4524                let (g, layers, cfg, st) = (&b.0, &b.1, b.2, &mut b.3);
4525                crate::dsv4::forward_chunk(
4526                    g,
4527                    layers,
4528                    &cfg,
4529                    st,
4530                    &ids,
4531                    pos,
4532                    &self.inv_freq,
4533                    self.pool.as_deref(),
4534                    &mut lg,
4535                    end == input_ids.len(),
4536                );
4537            }
4538            if end == input_ids.len() {
4539                self.graph_logits = Some(lg);
4540            }
4541            pos = end;
4542            hidden = vec![0.0; self.hidden_size];
4543        }
4544        let dsv41_prefill = self.dsv41_prefill.take();
4545        while self.dsv41.is_some()
4546            && mtp.is_none()
4547            && pos < input_ids.len()
4548            && !self.cancel.load(std::sync::atomic::Ordering::Relaxed)
4549        {
4550            let end = (pos + prefill_chunk()).min(input_ids.len());
4551            let ids: Vec<u32> = input_ids[pos..end].to_vec();
4552            let mut lg = Vec::new();
4553            if let Some(b) = &mut self.dsv41 {
4554                let (g, layers, cfg, st) = (&b.0, &b.1, &b.2, &mut b.3);
4555                if let Some((embeddings, participates)) = dsv41_prefill.as_ref() {
4556                    crate::dsv41::forward_chunk_masked_with_embeddings(
4557                        g,
4558                        layers,
4559                        cfg,
4560                        st,
4561                        &ids,
4562                        pos,
4563                        &embeddings[pos..end],
4564                        &participates[pos..end],
4565                        self.pool.as_deref(),
4566                        &mut lg,
4567                    );
4568                } else {
4569                    crate::dsv41::forward_chunk(
4570                        g,
4571                        layers,
4572                        cfg,
4573                        st,
4574                        &ids,
4575                        pos,
4576                        self.pool.as_deref(),
4577                        &mut lg,
4578                    );
4579                }
4580            }
4581            if end == input_ids.len() {
4582                self.graph_logits = Some(lg);
4583            }
4584            pos = end;
4585            hidden = vec![0.0; self.hidden_size];
4586        }
4587        // With dynamic routing, prefill sequentially so the φ hook fires
4588        // over the PROMPT — the router enters decode with a warm φ (the
4589        // fused-pair path skips the per-layer φ capture). o1 layers
4590        // collect their query trace in both the single and pair paths.
4591        let dyn_prefill = router.is_some();
4592        // Optional bounded calibration prefix for generation.  The normal
4593        // O(1) path seals after the full prompt; this explicit knob instead
4594        // runs only the requested prefix through exact attention, seals the
4595        // Nyström state, and streams the rest of the prompt through the same
4596        // O(1) step used by decode.  It keeps the O(1) layers' Q trace and
4597        // temporary full KV bounded by the prefix while leaving the default
4598        // full-prompt quality profile untouched.
4599        let o1_prefill_limit = o1_prefill
4600            .and_then(|requested| self.o1_effective_boundary(requested))
4601            .map(|boundary| boundary.min(input_ids.len()));
4602        let mut o1_sealed = false;
4603        if let Some(limit) = o1_prefill_limit {
4604            // Reuse the exact batched prefix machinery when available; it
4605            // records the same per-position Q trace as the full prefill.
4606            if self.can_prefill_batched() && limit > 2 {
4607                let chunk = self.prefill_chunk();
4608                let hs = self.hidden_size;
4609                while pos < limit && !self.cancel.load(std::sync::atomic::Ordering::Relaxed) {
4610                    let end = (pos + chunk).min(limit);
4611                    let hb = self.prefill_batch(&input_ids[pos..end], pos);
4612                    hidden.copy_from_slice(&hb[(end - pos - 1) * hs..]);
4613                    pos = end;
4614                }
4615            } else {
4616                while pos < limit && !self.cancel.load(std::sync::atomic::Ordering::Relaxed) {
4617                    hidden = self.forward_layers(&self.embed_single(input_ids[pos]), pos, None);
4618                    pos += 1;
4619                }
4620            }
4621            if pos >= limit {
4622                o1_sealed = match self.o1_seal_checked() {
4623                    Ok(sealed) => sealed,
4624                    Err(err) => {
4625                        self.finish_generation(&mut mtp, &mut router, true);
4626                        return Err(err);
4627                    }
4628                };
4629                tracing::info!(
4630                    "o1 bounded prompt prefix: requested={} effective={} processed={} of {} token(s)",
4631                    o1_prefill.unwrap_or(0),
4632                    self.o1_effective_boundary(o1_prefill.unwrap_or(0))
4633                        .unwrap_or(limit),
4634                    limit,
4635                    input_ids.len()
4636                );
4637            }
4638        }
4639        // q1 hybrids on Metal: the per-position GPU token graph beats
4640        // the CPU chunk-GEMM (whose wall is the sequential scalar GDN
4641        // recurrence), so prefill goes position-by-position through the
4642        // same graph as decode. Pure-attention models keep the batched
4643        // path — there the chunk-GEMM amortization wins.
4644        let graph_prefill = self.graph_prefill_preferred();
4645        // Native Metal, q4tp GDN hybrids: the prompt through the b-row
4646        // rows graph — projections as GEMMs over up to 512 positions, the
4647        // GDN recurrence in registers on the device, K/V rows appended by
4648        // the chunk — instead of one token-graph submit per position (the
4649        // 27B: 8 tok/s → GEMM-bound). The MTP warm-up rows come out of one
4650        // batched run of the block per chunk. Any refusal leaves the rest
4651        // of the prompt to the sequential paths below.
4652        #[cfg(target_os = "macos")]
4653        if task_mask.is_none()
4654            && !dyn_prefill
4655            && (crate::gpu::q1_force() || crate::gpu::q2tp_gpu_opt_in())
4656            && crate::gpu::enabled_here()
4657            && self.gdn_cfg.is_some()
4658            && self.g3n.is_none()
4659            && input_ids.len() > 8
4660            && std::env::var("CMF_MTP_CHAIN_PROBE").is_err()
4661            && std::env::var("CMF_METAL_PREFILL").as_deref() != Ok("0")
4662        {
4663            let chunk: usize = std::env::var("CMF_METAL_PREFILL_CHUNK")
4664                .ok()
4665                .and_then(|v| v.parse().ok())
4666                .filter(|&v| (16..=512).contains(&v))
4667                .unwrap_or(256);
4668            let hs = self.hidden_size;
4669            let _tp = std::time::Instant::now();
4670            while pos < input_ids.len() && !self.cancel.load(std::sync::atomic::Ordering::Relaxed) {
4671                let end = (pos + chunk).min(input_ids.len());
4672                let hb = match self.prefill_batch_metal(&input_ids[pos..end], pos) {
4673                    MetalPrefillOutcome::Completed(hb) => hb,
4674                    MetalPrefillOutcome::Declined => break,
4675                    MetalPrefillOutcome::Failed => {
4676                        self.finish_generation(&mut mtp, &mut router, true);
4677                        return Err("ordinary Metal prefill failed after admission".into());
4678                    }
4679                };
4680                if let Some(m) = &mut mtp {
4681                    let n_pairs = if end < input_ids.len() {
4682                        end - pos
4683                    } else {
4684                        end - pos - 1
4685                    };
4686                    if n_pairs > 0 {
4687                        let pairs: Vec<(&[f32], u32)> = (0..n_pairs)
4688                            .map(|j| (&hb[j * hs..(j + 1) * hs], input_ids[pos + j + 1]))
4689                            .collect();
4690                        if !self.mtp_warm_batch_metal(m, &pairs, pos) {
4691                            for (j, (h, t)) in pairs.iter().enumerate() {
4692                                let h = h.to_vec();
4693                                let _ = self.mtp_step(m, &h, *t, pos + j);
4694                            }
4695                        }
4696                    }
4697                }
4698                hidden.copy_from_slice(&hb[(end - pos - 1) * hs..]);
4699                pos = end;
4700            }
4701            if std::env::var("CMF_PREFILL_PROF").is_ok() {
4702                eprintln!(
4703                    "metal-prefill: {} of {} tokens in {:.1} ms",
4704                    pos,
4705                    input_ids.len(),
4706                    _tp.elapsed().as_secs_f64() * 1e3
4707                );
4708            }
4709        }
4710        // Gemma-4 on a discrete card: the prompt through the batched wgpu
4711        // graph (`g4_batch_ingest`); its K/V land in the device mirrors
4712        // decode reads. A refusal leaves the rest of the prompt to the
4713        // chunked host prefill below.
4714        #[cfg(not(target_os = "macos"))]
4715        if task_mask.is_none()
4716            && !dyn_prefill
4717            && mtp.is_none()
4718            && o1_prefill.is_none()
4719            && batch_k > 0
4720            && pos + 1 < input_ids.len()
4721            && self.g4_batch_prefill()
4722        {
4723            let t_g4 = std::time::Instant::now();
4724            match self.g4_batch_ingest(&input_ids[pos..], pos, false) {
4725                Ok((n, last)) => {
4726                    if n > 0 {
4727                        hidden.copy_from_slice(&last);
4728                    }
4729                    if std::env::var("CMF_PREFILL_PROF").is_ok() {
4730                        eprintln!(
4731                            "gemma4 batch prefill: {n} of {} tokens in {:.1} ms",
4732                            input_ids.len() - pos,
4733                            t_g4.elapsed().as_secs_f64() * 1e3
4734                        );
4735                    }
4736                    pos += n;
4737                }
4738                Err(e) => {
4739                    self.finish_generation(&mut mtp, &mut router, true);
4740                    return Err(e);
4741                }
4742            }
4743        }
4744        self.mimo_moe_prepare();
4745        // A MoE stack larger than the card (MiMo-V2 q4tp on 96 GB): the
4746        // batched wgpu graph runs the device prefix of every chunk — its
4747        // experts resident — and the host's batched layer walk finishes
4748        // the chunk. Any refusal leaves the rest of the prompt to the
4749        // chunked prefill below.
4750        #[cfg(not(target_os = "macos"))]
4751        if task_mask.is_none()
4752            && !dyn_prefill
4753            && !graph_prefill
4754            && mtp.is_none()
4755            && o1_prefill.is_none()
4756            && !self.o1_active()
4757            && input_ids.len() > 2
4758            && self.batch_prefix_prefill()
4759        {
4760            let chunk = self.prefill_chunk().max(1);
4761            let hs = self.hidden_size;
4762            let t_bp = std::time::Instant::now();
4763            let pos0 = pos;
4764            while pos < input_ids.len() && !self.cancel.load(std::sync::atomic::Ordering::Relaxed) {
4765                let end = (pos + chunk).min(input_ids.len());
4766                let bk = end - pos;
4767                let mut hiddens = vec![0f32; bk * hs];
4768                for (j, &id) in input_ids[pos..end].iter().enumerate() {
4769                    hiddens[j * hs..(j + 1) * hs].copy_from_slice(&self.embed_single(id));
4770                }
4771                let positions: Vec<usize> = (pos..end).collect();
4772                let mut run = 0usize;
4773                let outcome = self.try_batch_graph_wgpu_prefix(
4774                    &mut hiddens,
4775                    &positions,
4776                    bk,
4777                    None,
4778                    Some(&mut run),
4779                );
4780                match outcome {
4781                    crate::gpu::BatchGraphOutcome::Completed => {
4782                        let hb = if run < self.num_layers {
4783                            self.prefill_batch_span(
4784                                PrefillIn::Hidden(&hiddens),
4785                                pos,
4786                                None,
4787                                run,
4788                                self.num_layers,
4789                            )
4790                        } else {
4791                            hiddens
4792                        };
4793                        if mimo_spec {
4794                            self.mimo_note_rows(&hb, pos);
4795                        }
4796                        hidden.copy_from_slice(&hb[(bk - 1) * hs..]);
4797                        pos = end;
4798                    }
4799                    crate::gpu::BatchGraphOutcome::Failed => {
4800                        self.finish_generation(&mut mtp, &mut router, true);
4801                        return Err("batched prefix prefill failed after admission".into());
4802                    }
4803                    crate::gpu::BatchGraphOutcome::Declined => {
4804                        // Earlier chunks left their prefix rows on the
4805                        // device only: the host walk below needs them.
4806                        #[cfg(feature = "gpu")]
4807                        if pos > pos0 {
4808                            self.pull_lagging_host_kv(0, self.num_layers, pos);
4809                        }
4810                        break;
4811                    }
4812                }
4813            }
4814            if std::env::var("CMF_PREFILL_PROF").is_ok() {
4815                eprintln!(
4816                    "batch-prefix prefill: {} of {} tokens in {:.1} ms",
4817                    pos - pos0,
4818                    input_ids.len(),
4819                    t_bp.elapsed().as_secs_f64() * 1e3
4820                );
4821            }
4822        }
4823        if task_mask.is_none()
4824            && !dyn_prefill
4825            && !graph_prefill
4826            && self.can_prefill_batched()
4827            && self.g3n.is_none()
4828            && o1_prefill.is_none()
4829            && input_ids.len() > 2
4830        {
4831            // Production prefill = the same chunked prefill-GEMM that
4832            // bench/PPL measure (roadmap §3 P0: generation used to warm
4833            // the prompt with the slower pair path — the published
4834            // prefill number didn't match real TTFT). MTP warm-up reads
4835            // each position's hidden straight from the chunk result.
4836            let chunk = self.prefill_chunk();
4837            let hs = self.hidden_size;
4838            while pos < input_ids.len() && !self.cancel.load(std::sync::atomic::Ordering::Relaxed) {
4839                let end = (pos + chunk).min(input_ids.len());
4840                let hb = self.prefill_batch(&input_ids[pos..end], pos);
4841                if mimo_spec {
4842                    self.mimo_note_rows(&hb, pos);
4843                }
4844                if let Some(m) = &mut mtp {
4845                    let probe: usize = std::env::var("CMF_MTP_CHAIN_PROBE")
4846                        .ok()
4847                        .and_then(|v| v.parse().ok())
4848                        .unwrap_or(0);
4849                    for p in pos..end {
4850                        if p + 1 < input_ids.len() {
4851                            if probe >= 1 && p + 2 < input_ids.len() {
4852                                // Teacher-forced chain acceptance (see the
4853                                // tail loop's twin): the warm-up row stays,
4854                                // the chain's rows roll back.
4855                                let (d1, mut hx) = self.mtp_step_h(
4856                                    m,
4857                                    &hb[(p - pos) * hs..(p - pos + 1) * hs],
4858                                    input_ids[p + 1],
4859                                    p,
4860                                );
4861                                let mut ok = d1 == input_ids[p + 2];
4862                                Self::chain_probe_note(0, ok);
4863                                let mut d_prev = d1;
4864                                let mut extra = 0usize;
4865                                for j in 1..probe {
4866                                    if p + 2 + j >= input_ids.len() {
4867                                        break;
4868                                    }
4869                                    let (dj, hj) = self.mtp_step_h(m, &hx, d_prev, p + 1 + j);
4870                                    extra += 1;
4871                                    ok = ok && dj == input_ids[p + 2 + j];
4872                                    Self::chain_probe_note(j, ok);
4873                                    d_prev = dj;
4874                                    hx = hj;
4875                                }
4876                                m.kv.truncate_last(extra);
4877                            } else {
4878                                let _ = self.mtp_step(
4879                                    m,
4880                                    &hb[(p - pos) * hs..(p - pos + 1) * hs],
4881                                    input_ids[p + 1],
4882                                    p,
4883                                );
4884                            }
4885                        }
4886                    }
4887                }
4888                hidden.copy_from_slice(&hb[(end - pos - 1) * hs..]);
4889                pos = end;
4890            }
4891        }
4892        let pair_off = std::env::var("CMF_PAIR").is_ok_and(|v| v == "0");
4893        if task_mask.is_none()
4894            && !dyn_prefill
4895            && !graph_prefill
4896            && !pair_off
4897            && self.pair_supported()
4898            && o1_prefill.is_none()
4899        {
4900            while pos + 1 < input_ids.len()
4901                && !self.cancel.load(std::sync::atomic::Ordering::Relaxed)
4902            {
4903                let e1 = self.embed_single(input_ids[pos]);
4904                let e2 = self.embed_single(input_ids[pos + 1]);
4905                let (h1, h2) = self.forward_pair(&e1, &e2, pos);
4906                if mimo_spec {
4907                    self.mimo_note_rows(&h1, pos);
4908                    self.mimo_note_rows(&h2, pos + 1);
4909                }
4910                // Both prefill tokens are real → commit lane-2 states.
4911                self.commit_linear_scratch();
4912                if let Some(m) = &mut mtp {
4913                    let _ = self.mtp_step(m, &h1, input_ids[pos + 1], pos);
4914                    if pos + 2 < input_ids.len() {
4915                        let probe: usize = std::env::var("CMF_MTP_CHAIN_PROBE")
4916                            .ok()
4917                            .and_then(|v| v.parse().ok())
4918                            .unwrap_or(0);
4919                        if probe >= 1 && pos + 3 < input_ids.len() {
4920                            // Same teacher-forced chain table as the tail
4921                            // loop below, fed from the pair path that owns
4922                            // most prefill positions.
4923                            let (d1, mut hx) = self.mtp_step_h(m, &h2, input_ids[pos + 2], pos + 1);
4924                            let mut ok = d1 == input_ids[pos + 3];
4925                            Self::chain_probe_note(0, ok);
4926                            let mut d_prev = d1;
4927                            let mut extra = 0usize;
4928                            for j in 1..probe {
4929                                if pos + 3 + j >= input_ids.len() {
4930                                    break;
4931                                }
4932                                let (dj, hj) = self.mtp_step_h(m, &hx, d_prev, pos + 2 + j);
4933                                extra += 1;
4934                                ok = ok && dj == input_ids[pos + 3 + j];
4935                                Self::chain_probe_note(j, ok);
4936                                d_prev = dj;
4937                                hx = hj;
4938                            }
4939                            m.kv.truncate_last(extra);
4940                        } else {
4941                            let _ = self.mtp_step(m, &h2, input_ids[pos + 2], pos + 1);
4942                        }
4943                    }
4944                }
4945                hidden = h2;
4946                pos += 2;
4947            }
4948        }
4949        // Batched GPU prefill for the wgpu decode graph (GDN hybrids): K prompt
4950        // positions per submit — projections/FFN as GEMMs (weight once per K),
4951        // attention/GDN looped inside — instead of one whole-graph submit per
4952        // position. Falls through to the per-position graph on any refusal.
4953        // Batched prefill is opt-in (CMF_BATCH_K>0). Default 0 = per-position
4954        // graph prefill. (Steady-state decode is provably identical either way —
4955        // token-graph submit and lm_head both unchanged — so this only trades
4956        // prefill wall.)
4957        // A bounded O(1) prefix is the one post-seal prompt interval: only
4958        // admit its batch when the device O(1) route is explicitly enabled and
4959        // every sealed layer exposes a portable view. The same batch size and
4960        // refusal behavior remain the ordinary controls/comparator.
4961        let o1_batch_ready = o1_sealed
4962            && o1_prefill.is_some()
4963            && mtp.is_none()
4964            && std::env::var("CMF_O1_GPU").as_deref() == Ok("1")
4965            && (0..self.num_layers).all(|li| {
4966                let cache = &self.kv_cache.layers[self.phys_layer(li)];
4967                cache.o1.is_none() || cache.o1_views().is_some()
4968            });
4969        // The ordinary graph-prefill route can share each completed trunk
4970        // chunk with an attached MTP head.  Keep chain probing on its
4971        // established per-position path: the probe deliberately needs every
4972        // teacher-forced draft row and its rollback table.
4973        let mtp_batch_prefill = mtp.is_some()
4974            && graph_prefill
4975            && task_mask.is_none()
4976            && !dyn_prefill
4977            && !self.o1_active()
4978            && std::env::var("CMF_MTP_CHAIN_PROBE").is_err();
4979        if batch_k > 0
4980            && (graph_prefill || o1_batch_ready)
4981            && task_mask.is_none()
4982            && (!self.o1_active() || o1_batch_ready)
4983            && (mtp.is_none() || mtp_batch_prefill)
4984            && !dyn_prefill
4985            && pos + 1 < input_ids.len()
4986        {
4987            let hs = self.hidden_size;
4988            let chunk = batch_k;
4989            while pos < input_ids.len() {
4990                let end = (pos + chunk).min(input_ids.len());
4991                let bk = end - pos;
4992                let mut hiddens = vec![0f32; bk * hs];
4993                for (j, &id) in input_ids[pos..end].iter().enumerate() {
4994                    hiddens[j * hs..(j + 1) * hs].copy_from_slice(&self.embed_single(id));
4995                }
4996                let positions: Vec<usize> = (pos..end).collect();
4997                let t_chunk = std::time::Instant::now();
4998                let outcome = self.try_batch_graph_wgpu(&mut hiddens, &positions, bk, None);
4999                let ok_b = outcome == crate::gpu::BatchGraphOutcome::Completed;
5000                if std::env::var("CMF_GRAPH_PROF").is_ok() {
5001                    let ms = t_chunk.elapsed().as_secs_f64() * 1000.0;
5002                    eprintln!(
5003                        "batch-chunk: phase=prompt mode={} k={bk} outcome={outcome:?} {ms:.1} ms ({:.1} tok/s)",
5004                        if o1_batch_ready {
5005                            "o1"
5006                        } else if mtp_batch_prefill {
5007                            "ordinary_mtp"
5008                        } else {
5009                            "ordinary"
5010                        },
5011                        bk as f64 / (ms / 1000.0)
5012                    );
5013                }
5014                {
5015                    use std::sync::atomic::{AtomicBool, Ordering};
5016                    static SAID: AtomicBool = AtomicBool::new(false);
5017                    if !SAID.swap(true, Ordering::Relaxed) {
5018                        if ok_b {
5019                            tracing::info!(
5020                                "batched prefill: ACTIVE mode={} (k={bk})",
5021                                if o1_batch_ready {
5022                                    "o1"
5023                                } else if mtp_batch_prefill {
5024                                    "ordinary_mtp"
5025                                } else {
5026                                    "ordinary"
5027                                }
5028                            );
5029                        } else {
5030                            tracing::warn!("batched prefill {:?} — per-position graph", outcome);
5031                        }
5032                    }
5033                }
5034                if ok_b {
5035                    if mimo_spec {
5036                        self.mimo_note_rows(&hiddens, pos);
5037                    }
5038                    if mtp_batch_prefill {
5039                        let n_pairs = mtp_prefill_pair_count(pos, end, input_ids.len());
5040                        if n_pairs > 0 {
5041                            // `hiddens` is owned by this chunk, so materialize
5042                            // row slices before borrowing the detached MTP
5043                            // module.  The last prompt row has no successor;
5044                            // the helper above is the single source of that
5045                            // boundary rule.
5046                            let rows: Vec<Vec<f32>> = (0..n_pairs)
5047                                .map(|j| hiddens[j * hs..(j + 1) * hs].to_vec())
5048                                .collect();
5049                            let pairs: Vec<(&[f32], u32)> = rows
5050                                .iter()
5051                                .enumerate()
5052                                .map(|(j, row)| (row.as_slice(), input_ids[pos + j + 1]))
5053                                .collect();
5054                            if std::env::var("CMF_GRAPH_PROF").is_ok() {
5055                                eprintln!(
5056                                    "mtp-warm: phase=prompt mode=ordinary_mtp first_pos={} pairs={} last_pos={}",
5057                                    pos,
5058                                    n_pairs,
5059                                    pos + n_pairs - 1,
5060                                );
5061                            }
5062                            let warm_error = if let Some(m) = mtp.as_mut() {
5063                                self.mtp_warm_prefill_pairs(m, &pairs, pos).err()
5064                            } else {
5065                                None
5066                            };
5067                            if let Some(err) = warm_error {
5068                                // The trunk batch was already admitted.  A
5069                                // failed MTP warm-up therefore clears both
5070                                // mirrors and exits; continuing would pair a
5071                                // current trunk state with a stale MTP cache.
5072                                self.finish_generation(&mut mtp, &mut router, true);
5073                                return Err(err.to_string());
5074                            }
5075                        }
5076                    }
5077                    hidden.copy_from_slice(&hiddens[(bk - 1) * hs..]);
5078                    pos = end;
5079                } else if outcome == crate::gpu::BatchGraphOutcome::Failed {
5080                    // A failed batch may have advanced a device recurrent
5081                    // state (ordinary GDN or sealed O(1)). A CPU fallback
5082                    // would then observe stale accumulators, so clear the
5083                    // request state and make the failure explicit.
5084                    self.finish_generation(&mut mtp, &mut router, true);
5085                    return Err(if o1_batch_ready {
5086                        "sealed O(1) batch graph failed after admission".to_string()
5087                    } else {
5088                        "ordinary recurrent batch graph failed after admission".to_string()
5089                    });
5090                } else {
5091                    break; // unsupported → per-position graph handles the rest
5092                }
5093            }
5094        }
5095        // Resident Embryo graph: the prompt in chunks of one submit each
5096        // instead of one whole-graph submit per position; the last chunk
5097        // carries the logits exactly as the per-position walk would.
5098        if graph_prefill
5099            && task_mask.is_none()
5100            && mtp.is_none()
5101            && !dyn_prefill
5102            && pos == 0
5103            && input_ids.len() > 1
5104            && !self.cancel.load(std::sync::atomic::Ordering::Relaxed)
5105        {
5106            if let Some(lg) = self.embryo_prefill_chunked(input_ids, 0) {
5107                self.graph_logits = Some(lg);
5108                hidden = vec![0.0; self.hidden_size];
5109                pos = input_ids.len();
5110            }
5111        }
5112        while pos < input_ids.len() && !self.cancel.load(std::sync::atomic::Ordering::Relaxed) {
5113            self.graph_want_logits = fuse_lm && pos + 1 == input_ids.len();
5114            hidden = self.forward_layers(&self.embed_single(input_ids[pos]), pos, task_mask);
5115            if mimo_spec {
5116                self.mimo_note_rows(&hidden, pos);
5117            }
5118            if let Some(m) = &mut mtp {
5119                if pos + 1 < input_ids.len() {
5120                    // `CMF_MTP_CHAIN_PROBE=k`: teacher-forced acceptance of a
5121                    // CHAINED draft — iterate the head on its own hidden k
5122                    // deep and score every depth against the prompt's real
5123                    // continuation. The economics of a k-token speculative
5124                    // round stand or fall on this table.
5125                    let probe: usize = std::env::var("CMF_MTP_CHAIN_PROBE")
5126                        .ok()
5127                        .and_then(|v| v.parse().ok())
5128                        .unwrap_or(0);
5129                    if probe >= 1 && pos + 2 < input_ids.len() {
5130                        let (d1, mut hx) = self.mtp_step_h(m, &hidden, input_ids[pos + 1], pos);
5131                        let mut ok = d1 == input_ids[pos + 2];
5132                        Self::chain_probe_note(0, ok);
5133                        let mut d_prev = d1;
5134                        let mut extra = 0usize;
5135                        for j in 1..probe {
5136                            if pos + 2 + j >= input_ids.len() {
5137                                break;
5138                            }
5139                            let (dj, hj) = self.mtp_step_h(m, &hx, d_prev, pos + 1 + j);
5140                            extra += 1;
5141                            ok = ok && dj == input_ids[pos + 2 + j];
5142                            Self::chain_probe_note(j, ok);
5143                            d_prev = dj;
5144                            hx = hj;
5145                        }
5146                        // The chain's rows are speculation, not the prompt —
5147                        // keep only the warmup row the plain path would add.
5148                        m.kv.truncate_last(extra);
5149                    } else {
5150                        let _ = self.mtp_step(m, &hidden, input_ids[pos + 1], pos);
5151                    }
5152                }
5153            }
5154            pos += 1;
5155        }
5156        if std::env::var("CMF_PREFILL_PROF").is_ok() {
5157            eprintln!(
5158                "prefill: {} tokens in {:.1} ms (batch_k={batch_k})",
5159                input_ids.len(),
5160                _tpf.elapsed().as_secs_f64() * 1000.0
5161            );
5162        }
5163        if self
5164            .graph_failed
5165            .swap(false, std::sync::atomic::Ordering::Relaxed)
5166        {
5167            // MTP is detached for speculative generation.  Restore the
5168            // module before returning the terminal graph error; otherwise a
5169            // failed request would silently remove the head from a pooled
5170            // pipeline and the next request would lose its configured route.
5171            self.finish_generation(&mut mtp, &mut router, true);
5172            return Err("GPU token graph failed during prefill".to_string());
5173        }
5174        // Cancelled mid-prefill: the cache holds a partial prompt —
5175        // drop the reuse history and return an empty generation.
5176        if self
5177            .cancel
5178            .swap(false, std::sync::atomic::Ordering::Relaxed)
5179        {
5180            // A cancelled prefill can already have advanced the device
5181            // mirror. Drop the whole partial sequence so a pooled pipeline
5182            // cannot carry that state into its next request.
5183            self.finish_generation(&mut mtp, &mut router, true);
5184            return Ok(GenerateResult {
5185                text: String::new(),
5186                token_ids: Vec::new(),
5187                prompt_tokens: input_ids.len(),
5188                tokens_generated: 0,
5189                finish_reason: "cancelled".to_string(),
5190                mtp_drafted: 0,
5191                mtp_accepted: 0,
5192                token_confidence: Vec::new(),
5193                traces: Vec::new(),
5194            });
5195        }
5196
5197        // Prompt absorbed → freeze the o1 layers' skeletons; from here
5198        // every decode step on those layers is O(W + m·dv + m²).
5199        if !o1_sealed {
5200            match self.o1_seal_checked() {
5201                Ok(_) => {}
5202                Err(err) => {
5203                    self.finish_generation(&mut mtp, &mut router, true);
5204                    return Err(err);
5205                }
5206            }
5207        }
5208
5209        // Commit one token: push, check EOS, stream. Returns false = stop.
5210        macro_rules! commit {
5211            ($id:expr) => {{
5212                all_ids.push($id);
5213                generated += 1;
5214                self.note_draft_id($id);
5215                if self.tokenizer.is_eos($id) && !self.ignore_eos {
5216                    finish_reason = "stop".to_string();
5217                    false
5218                } else {
5219                    let token_text = self.tokenizer.decode_token($id);
5220                    let mut go = true;
5221                    if let Some(ref mut cb) = on_token {
5222                        if !cb(&token_text) {
5223                            finish_reason = "cancelled".to_string();
5224                            go = false;
5225                        }
5226                    }
5227                    go
5228                }
5229            }};
5230        }
5231
5232        // Speculation is decided by MEASUREMENT, not by an acceptance
5233        // model. A k=4 round costs ~3.8 plain tokens on the 5090 (draft
5234        // 6.6 + verify 66.6 + commit 4.8 ms against a 20.6 ms token), so it
5235        // pays only when the head lands ~2.8 of 4 — predictable text (code,
5236        // structured output) does, free prose often does not, and the
5237        // ratio at which the two cross depends on the card and the context
5238        // depth. So: four speculative rounds timed, then eight plain
5239        // tokens timed, and the faster arm runs until a re-check 256
5240        // tokens later (context growth moves the balance). The trial
5241        // costs at most a few tokens of the slower arm per 256.
5242        let mut spec_trial = SpecTrial::Spec {
5243            t0: std::time::Instant::now(),
5244            gen0: generated,
5245            rounds: 0,
5246        };
5247        // The token-count proxy prices a round at ~1.9 plain tokens. That
5248        // holds for the Metal rounds whose cost was measured — greedy and
5249        // the sparse sampling chain — so an expensive round (the dense
5250        // chain, reachable only by `CMF_GRAPH_SPEC_SAMPLE=1`) still times
5251        // the plain path before it decides.
5252        let mut spec_mon = SpecMon {
5253            metal: graph_spec && crate::gpu::q1_force() && spec_cheap_round,
5254            ..SpecMon::default()
5255        };
5256        let mut spec_watchdog_off = false;
5257        // CMF_GRAPH_SPEC_TIME: the round walls so far (round 1 excluded —
5258        // it pays the scratch), for the outlier test on each new one
5259        let mut spec_walls: Vec<f32> = Vec::new();
5260        // ... and the end of the last round: the host time between rounds
5261        // (token commits, streaming, the loop top) is printed at level 2
5262        let mut spec_round_end: Option<std::time::Instant> = None;
5263        if mimo_spec {
5264            if let Ok(path) = std::env::var("CMF_MIMO_MTP_PROBE") {
5265                if let Some(mut st) = self.mimo_mtp.take() {
5266                    self.mimo_mtp_probe(&mut st, input_ids, &path);
5267                    self.mimo_mtp = Some(st);
5268                }
5269            }
5270        }
5271        // ── Decode ──
5272        let mut next_pos = input_ids.len();
5273        'decode: while generated < max_tokens {
5274            if self
5275                .graph_failed
5276                .swap(false, std::sync::atomic::Ordering::Relaxed)
5277            {
5278                // Keep the detached MTP module attached after a terminal
5279                // graph error so the pipeline can be reused for a fresh
5280                // sequence.  `clear_sequence_state` only clears mirrors and
5281                // host KV; it cannot recover a module dropped here.
5282                self.finish_generation(&mut mtp, &mut router, true);
5283                return Err("GPU token graph failed during decode".to_string());
5284            }
5285            if self
5286                .cancel
5287                .swap(false, std::sync::atomic::Ordering::Relaxed)
5288            {
5289                finish_reason = "cancelled".to_string();
5290                break 'decode;
5291            }
5292            // A rejected speculative draft already drew this position's
5293            // token from the residual distribution (graph_spec_step); it
5294            // is committed as-is — sampling again from the row's logits
5295            // would bias the stream toward the target's mode.
5296            if mimo_spec && next_pos > 0 {
5297                // Every path leaves `hidden` = the backbone output at
5298                // next_pos-1; the draft layers read it (idempotent).
5299                self.mimo_note_rows(&hidden, next_pos - 1);
5300            }
5301            let forced = self.spec_forced.take();
5302            let mut logits = match (forced, self.graph_logits.take()) {
5303                (Some(_), _) => Vec::new(),
5304                (None, Some(lg)) => lg,
5305                (None, None) => {
5306                    let _prof = crate::cpuprof::time(crate::cpuprof::Slot::Head);
5307                    inference::rms_norm_into(
5308                        &hidden,
5309                        &self.weights.final_norm,
5310                        self.rms_eps,
5311                        self.norm_style,
5312                        &mut self.ws.n1,
5313                    );
5314                    self.lm_head_forward(&self.ws.n1)
5315                }
5316            };
5317            // CMF_LOGIT_DUMP=<path>: the first decode step's hidden + logits
5318            // as raw f32 (hidden first) — cross-backend numerics diffing.
5319            if generated
5320                == std::env::var("CMF_LOGIT_DUMP_STEP")
5321                    .ok()
5322                    .and_then(|v| v.parse().ok())
5323                    .unwrap_or(0)
5324            {
5325                if let Ok(path) = std::env::var("CMF_LOGIT_DUMP") {
5326                    let mut bytes: Vec<u8> = Vec::with_capacity((hidden.len() + logits.len()) * 4);
5327                    for v in hidden.iter().chain(logits.iter()) {
5328                        bytes.extend_from_slice(&v.to_le_bytes());
5329                    }
5330                    if let Err(e) = std::fs::write(&path, &bytes) {
5331                        eprintln!("logit dump: failed to write {path}: {e}");
5332                        self.finish_generation(&mut mtp, &mut router, true);
5333                        return Err(format!("logit dump write failed: {e}"));
5334                    }
5335                }
5336            }
5337            // CMF_LOGIT_DUMP_ALL=<dir>: every decode step's logits as raw
5338            // f32, `<dir>/step{n:05}.f32` — step-by-step backend diffing
5339            // (a greedy run on two backends compares until they diverge).
5340            if let Ok(dir) = std::env::var("CMF_LOGIT_DUMP_ALL") {
5341                if !logits.is_empty() {
5342                    let path = std::path::Path::new(&dir).join(format!("step{generated:05}.f32"));
5343                    let bytes: Vec<u8> = logits.iter().flat_map(|v| v.to_le_bytes()).collect();
5344                    if let Err(e) =
5345                        std::fs::create_dir_all(&dir).and_then(|_| std::fs::write(&path, &bytes))
5346                    {
5347                        eprintln!("logit dump: failed to write {}: {e}", path.display());
5348                    }
5349                }
5350            }
5351            let t_next = match forced.or_else(|| forced_ids_at(generated)) {
5352                Some(c) => c,
5353                None => {
5354                    let _prof = crate::cpuprof::time(crate::cpuprof::Slot::Sampler);
5355                    sampler::sample_with_scratch_pool(
5356                        &logits,
5357                        &self.sampler_config,
5358                        self.sampler_config.penalty_past(&all_ids, bounded_native),
5359                        &mut self.rng,
5360                        &mut self.sampler_scratch,
5361                        self.pool.as_deref(),
5362                    )
5363                }
5364            };
5365            if self.confidence_on {
5366                confidence.push(if logits.is_empty() {
5367                    0.0
5368                } else {
5369                    sampler::top1_prob_pool(
5370                        self.pool.as_deref(),
5371                        &mut self.sampler_scratch,
5372                        &logits,
5373                        t_next,
5374                        calib_temp,
5375                    )
5376                });
5377            }
5378            if !logits.is_empty() {
5379                attention::recycle_buf(&mut logits);
5380            }
5381            if trace_on {
5382                // active_skill = the overlay in force while this token was
5383                // generated; recon/switched are filled after the post-emit
5384                // routing eval below (freshest coherence for this token).
5385                let skill = router.as_ref().and_then(|r| r.active_id());
5386                traces.push(TokenTrace {
5387                    t: generated,
5388                    token_id: t_next,
5389                    confidence: confidence.last().copied().unwrap_or(0.0),
5390                    active_skill: skill,
5391                    recon: None,
5392                    switched: false,
5393                });
5394            }
5395            if !commit!(t_next) {
5396                break 'decode;
5397            }
5398            if generated >= max_tokens {
5399                break 'decode;
5400            }
5401
5402            // Catch-all for decode paths that bypass the walks' own trim
5403            // (a graph step, a speculative round).
5404            self.swa_trim_tails();
5405            if self.dsv41.is_none() && self.kv_cache.needs_eviction() {
5406                // Say it ONCE, loudly: past this point the model keeps
5407                // talking but has lost half its context, and on a GDN
5408                // hybrid the graph's device state goes stale on top. The
5409                // Qwen3.8 bring-up spent a day reading this cliff as
5410                // three different model bugs.
5411                static SAID: std::sync::Once = std::sync::Once::new();
5412                SAID.call_once(|| {
5413                    tracing::warn!(
5414                        "KV cache full at {} positions — evicting half; quality \
5415                         will degrade. Raise CMF_MAX_SEQ.",
5416                        self.kv_cache.max_seq_len,
5417                    );
5418                });
5419                let keep = (self.kv_cache.max_seq_len / 2).max(1);
5420                self.kv_cache.evict(keep);
5421            }
5422
5423            // Advance the speculation trial: plain-phase accounting and
5424            // the periodic re-check happen here, on every token.
5425            if graph_spec {
5426                match spec_trial {
5427                    SpecTrial::Plain { t0, gen0 } if spec_mon.plain_done(t0, gen0, generated) => {
5428                        spec_mon.plain_ms =
5429                            t0.elapsed().as_secs_f64() * 1e3 / (generated - gen0) as f64;
5430                        let keep = spec_mon.pays();
5431                        tracing::info!(
5432                            "speculation trial: {:.2} tok/round in {:.1} ms vs plain {:.1} ms/tok — {}",
5433                            spec_mon.tokens,
5434                            spec_mon.round_ms,
5435                            spec_mon.plain_ms,
5436                            if keep { "speculating" } else { "plain" }
5437                        );
5438                        spec_mon.fails = 0;
5439                        spec_trial = SpecTrial::Decided {
5440                            spec: keep,
5441                            recheck_at: if keep { usize::MAX } else { generated + 128 },
5442                        };
5443                    }
5444                    SpecTrial::Decided { recheck_at, .. } if generated >= recheck_at => {
5445                        spec_mon.n = 0;
5446                        spec_trial = SpecTrial::Spec {
5447                            t0: std::time::Instant::now(),
5448                            gen0: generated,
5449                            rounds: 0,
5450                        };
5451                    }
5452                    _ => {}
5453                }
5454                spec_watchdog_off = matches!(
5455                    spec_trial,
5456                    SpecTrial::Plain { .. } | SpecTrial::Decided { spec: false, .. }
5457                );
5458            }
5459            // ── MiMo-V2 draft stack: draft K, verify K+1 rows in one batch ──
5460            if mimo_spec && generated + 1 < max_tokens && next_pos > 0 {
5461                let budget = max_tokens - generated - 1;
5462                if let Some(mut st) = self.mimo_mtp.take() {
5463                    let k = st.depth.min(budget);
5464                    let r = self.mimo_spec_round(&mut st, next_pos, &all_ids, k);
5465                    self.mimo_mtp = Some(st);
5466                    let r = match r {
5467                        Ok(r) => r,
5468                        Err(err) => {
5469                            self.finish_generation(&mut mtp, &mut router, true);
5470                            return Err(err);
5471                        }
5472                    };
5473                    if let Some(r) = r {
5474                        drafted += r.drafted;
5475                        accepted += r.accepted.len();
5476                        let mut stopped = false;
5477                        for &id in &r.accepted {
5478                            if self.confidence_on {
5479                                confidence.push(0.0);
5480                            }
5481                            if !commit!(id) {
5482                                stopped = true;
5483                                break;
5484                            }
5485                        }
5486                        if stopped {
5487                            break 'decode;
5488                        }
5489                        next_pos += r.accepted.len() + 1;
5490                        hidden = r.hidden;
5491                        // The loop top chooses the round's own token from
5492                        // these logits — the same sampler, same history.
5493                        self.graph_logits = Some(r.logits);
5494                        continue 'decode;
5495                    }
5496                }
5497            }
5498            // ── Qwen3.8-Flash-Next draft head: k greedy drafts from the MTP
5499            //    sidecar, one batched verify window on the device ──
5500            #[cfg(feature = "gpu")]
5501            if self.speculative
5502                && self.qwen4_exp.is_some()
5503                && task_mask.is_none()
5504                && self.sampler_config.temperature < 1e-6
5505                && generated + 1 < max_tokens
5506                && next_pos > 0
5507                && std::env::var("CMF_QWEN_MTP").as_deref() != Ok("0")
5508            {
5509                let r = match &mut self.qwen4_exp {
5510                    Some(b) => crate::qwen4_exp::spec_round(
5511                        &b.0,
5512                        &b.1,
5513                        &b.2,
5514                        &mut b.3,
5515                        next_pos,
5516                        &all_ids,
5517                        &self.inv_freq,
5518                        self.pool.as_deref(),
5519                    ),
5520                    None => None,
5521                };
5522                if let Some(r) = r {
5523                    drafted += r.drafted;
5524                    accepted += r.accepted.len();
5525                    let mut stopped = false;
5526                    for &id in &r.accepted {
5527                        if self.confidence_on {
5528                            confidence.push(0.0);
5529                        }
5530                        if !commit!(id) {
5531                            stopped = true;
5532                            break;
5533                        }
5534                    }
5535                    if stopped {
5536                        break 'decode;
5537                    }
5538                    next_pos += r.accepted.len() + 1;
5539                    hidden.fill(0.0);
5540                    self.graph_logits = Some(r.logits);
5541                    continue 'decode;
5542                }
5543            }
5544            match &mut mtp {
5545                // ── Graph speculation: chain-draft, batch-verify on device ──
5546                #[cfg(feature = "gpu")]
5547                Some(m)
5548                    if graph_spec
5549                        && !spec_watchdog_off
5550                        && generated + 1 < max_tokens
5551                        && next_pos > 0 =>
5552                {
5553                    let t_round = std::time::Instant::now();
5554                    if spec_time_level() >= 2 {
5555                        if let Some(t) = spec_round_end.take() {
5556                            eprintln!(
5557                                "spec-gap {:.2} ms (host between rounds)",
5558                                t.elapsed().as_secs_f64() * 1e3
5559                            );
5560                        }
5561                    }
5562                    spec_stamps_begin();
5563                    // device buffers allocated during this round: a
5564                    // first-touch Shared allocation is zero-filled inside
5565                    // the command buffer that uses it, which is what the
5566                    // long outlier rounds were
5567                    #[cfg(target_os = "macos")]
5568                    let allocs0 = crate::gpu_metal::IO_BUF_ALLOCS
5569                        .load(std::sync::atomic::Ordering::Relaxed);
5570                    #[cfg(not(target_os = "macos"))]
5571                    let allocs0 = 0u64;
5572                    if let Some((extra, n_pos, new_h)) = self.graph_spec_step(
5573                        m,
5574                        &hidden,
5575                        t_next,
5576                        next_pos,
5577                        &mut drafted,
5578                        &mut accepted,
5579                        &mut all_ids,
5580                        max_tokens - generated,
5581                    ) {
5582                        next_pos = n_pos;
5583                        hidden = new_h;
5584                        let level = spec_time_level();
5585                        if level > 0 {
5586                            let wall = t_round.elapsed().as_secs_f32() * 1e3;
5587                            let stamps = spec_stamps_take();
5588                            // the running median of the rounds before this
5589                            // one (round 1 pays the scratch: not a sample)
5590                            let median = if spec_walls.len() >= 3 {
5591                                let mut s = spec_walls.clone();
5592                                s.sort_by(|a, b| a.partial_cmp(b).unwrap());
5593                                Some(s[s.len() / 2])
5594                            } else {
5595                                None
5596                            };
5597                            let outlier = median.is_some_and(|m| wall > 1.4 * m);
5598                            #[cfg(target_os = "macos")]
5599                            let allocs = crate::gpu_metal::IO_BUF_ALLOCS
5600                                .load(std::sync::atomic::Ordering::Relaxed)
5601                                - allocs0;
5602                            #[cfg(not(target_os = "macos"))]
5603                            let allocs = allocs0;
5604                            eprintln!(
5605                                "spec-round wall {wall:.1} ms → {} tokens{}{}",
5606                                extra.len() + 1,
5607                                if allocs > 0 {
5608                                    format!(" [{allocs} new device buffers]")
5609                                } else {
5610                                    String::new()
5611                                },
5612                                match (outlier, median) {
5613                                    (true, Some(m)) => format!(" OUTLIER (median {m:.1})"),
5614                                    _ => String::new(),
5615                                }
5616                            );
5617                            if level >= 2 || outlier {
5618                                let sum: f32 = stamps.iter().map(|s| s.1).sum();
5619                                eprintln!(
5620                                    "spec-stamps: {}| untracked {:.1}",
5621                                    spec_stamps_format(&stamps),
5622                                    wall - sum
5623                                );
5624                            }
5625                            if spec_mon.n >= 1 {
5626                                spec_walls.push(wall);
5627                            }
5628                        }
5629                        // One speculative round done: the monitor counts it
5630                        // (round 1 untimed — it pays the batch scratch and
5631                        // the draft mirror), and the trial advances.
5632                        spec_mon.round(t_round.elapsed().as_secs_f64() * 1e3, extra.len() + 1);
5633                        // the round's tokens land in `generated` below; the
5634                        // plain phase must start counting AFTER them
5635                        spec_trial = Self::spec_trial_round(
5636                            spec_trial,
5637                            &mut spec_mon,
5638                            generated + extra.len() + 1,
5639                        );
5640                        let mut stopped = false;
5641                        for &id in &extra {
5642                            if self.confidence_on {
5643                                confidence.push(0.0);
5644                            }
5645                            if !commit!(id) {
5646                                stopped = true;
5647                                break;
5648                            }
5649                        }
5650                        if stopped {
5651                            break 'decode;
5652                        }
5653                        if spec_time_level() >= 2 {
5654                            spec_round_end = Some(std::time::Instant::now());
5655                        }
5656                        continue 'decode;
5657                    }
5658                    if self
5659                        .graph_failed
5660                        .swap(false, std::sync::atomic::Ordering::Relaxed)
5661                    {
5662                        // `graph_spec_step` may have detached MTP while a
5663                        // warm-up was in flight.  Do not reinterpret its
5664                        // terminal device failure as a plain decode step;
5665                        // restore the head, clear both mirrors, and surface
5666                        // one explicit error to the caller.
5667                        self.finish_generation(&mut mtp, &mut router, true);
5668                        return Err("GPU MTP graph failed during speculative decode".to_string());
5669                    }
5670                    // Declined (batch graph refused): plain forward below —
5671                    // and a round that produced one token for the trial's
5672                    // ledger, so a graph that keeps refusing is measured out
5673                    // like a head that keeps missing (it was spinning
5674                    // forever on a file whose batch graph declines).
5675                    // A declined round is not a cheap one-token round — it
5676                    // is a verify that does not exist for this file (a
5677                    // healed q8_2f tail measured 760 drafts, 0 accepted, 33
5678                    // against 48.8 tok/s while the monitor called the draft
5679                    // alone "paying"). Count it as the losing streak in one.
5680                    spec_mon.round(t_round.elapsed().as_secs_f64() * 1e3, 1);
5681                    spec_mon.tokens = 0.0;
5682                    spec_mon.fails = 3;
5683                    spec_trial = Self::spec_trial_round(spec_trial, &mut spec_mon, generated + 1);
5684                    hidden = self.forward_layers(&self.embed_single(t_next), next_pos, task_mask);
5685                    next_pos += 1;
5686                    continue 'decode;
5687                }
5688                // ── Speculative: draft t+2, verify in a fused pair ──
5689                Some(m) if !graph_spec && generated + 1 < max_tokens => {
5690                    let draft = self.mtp_step(m, &hidden, t_next, next_pos - 1);
5691                    drafted += 1;
5692                    let emb1 = self.embed_single(t_next);
5693                    let emb2 = self.embed_single(draft);
5694                    let (h1, h2) = self.forward_pair(&emb1, &emb2, next_pos);
5695
5696                    inference::rms_norm_into(
5697                        &h1,
5698                        &self.weights.final_norm,
5699                        self.rms_eps,
5700                        self.norm_style,
5701                        &mut self.ws.n1,
5702                    );
5703                    let mut logits1 = self.lm_head_forward(&self.ws.n1);
5704                    let t_after = sampler::sample_with_scratch_pool(
5705                        &logits1,
5706                        &self.sampler_config,
5707                        self.sampler_config.penalty_past(&all_ids, bounded_native),
5708                        &mut self.rng,
5709                        &mut self.sampler_scratch,
5710                        self.pool.as_deref(),
5711                    );
5712                    if self.confidence_on {
5713                        confidence.push(sampler::top1_prob_pool(
5714                            self.pool.as_deref(),
5715                            &mut self.sampler_scratch,
5716                            &logits1,
5717                            t_after,
5718                            calib_temp,
5719                        ));
5720                    }
5721                    attention::recycle_buf(&mut logits1);
5722                    if trace_on {
5723                        // Speculative decode is mutually exclusive with
5724                        // dynamic routing (router is None here) — no skill.
5725                        traces.push(TokenTrace {
5726                            t: generated,
5727                            token_id: t_after,
5728                            confidence: confidence.last().copied().unwrap_or(0.0),
5729                            active_skill: None,
5730                            recon: None,
5731                            switched: false,
5732                        });
5733                    }
5734                    let stop = !commit!(t_after);
5735
5736                    if t_after == draft {
5737                        accepted += 1;
5738                        self.commit_linear_scratch();
5739                        let _ = self.mtp_step(m, &h1, t_after, next_pos);
5740                        hidden = h2;
5741                        next_pos += 2;
5742                    } else {
5743                        // The draft lane is wrong: roll its KV entry back.
5744                        for layer in &mut self.kv_cache.layers {
5745                            layer.truncate_last(1);
5746                        }
5747                        if !stop {
5748                            let _ = self.mtp_step(m, &h1, t_after, next_pos);
5749                            hidden = self.forward_layers(
5750                                &self.embed_single(t_after),
5751                                next_pos + 1,
5752                                None,
5753                            );
5754                        }
5755                        next_pos += 2;
5756                    }
5757                    if stop {
5758                        break 'decode;
5759                    }
5760                }
5761                // ── Vanilla: forward the sampled token ──
5762                _ => {
5763                    // ── DeepSeek-V4 speculative decode (CMF_DSV4_SPEC=1):
5764                    // draft five on the card, verify batched, commit the
5765                    // accepted prefix. Greedy only; a rejected token's state
5766                    // is restored and replayed, so output equals the walk. ──
5767                    #[cfg(feature = "gpu")]
5768                    if Self::dsv4_spec_on() && self.dsv4.is_some() {
5769                        static SAID: std::sync::Once = std::sync::Once::new();
5770                        SAID.call_once(|| {
5771                            eprintln!(
5772                                "dsv4-spec гейт: mtp={} mask={} router={} trace={} temp={} rep={} ",
5773                                !self.dsv4_mtp.is_empty(),
5774                                task_mask.is_none(),
5775                                router.is_none(),
5776                                !trace_on,
5777                                self.sampler_config.temperature < 1e-6,
5778                                self.sampler_config.repetition_penalty == 1.0,
5779                            );
5780                        });
5781                    }
5782                    #[cfg(feature = "gpu")]
5783                    if Self::dsv4_spec_on()
5784                        && self.dsv4.is_some()
5785                        && !self.dsv4_mtp.is_empty()
5786                        && task_mask.is_none()
5787                        && router.is_none()
5788                        && !trace_on
5789                        && self.sampler_config.temperature < 1e-6
5790                        && self.sampler_config.repetition_penalty == 1.0
5791                        && generated + 1 < max_tokens
5792                        && all_ids.len() >= 2
5793                        && generated >= dsv4_spec_retry_at
5794                    {
5795                        let tip_token = all_ids[all_ids.len() - 2];
5796                        let drafted0 = drafted;
5797                        let round = self.dsv4_spec_step(
5798                            tip_token,
5799                            t_next,
5800                            next_pos,
5801                            max_tokens.saturating_sub(generated),
5802                            &mut drafted,
5803                            &mut accepted,
5804                        );
5805                        if drafted > drafted0 {
5806                            let useful = round.as_ref().is_some_and(|(extra, _)| !extra.is_empty());
5807                            if useful {
5808                                dsv4_spec_bad = 0;
5809                            } else {
5810                                dsv4_spec_bad += 1;
5811                                if dsv4_spec_bad >= 2 {
5812                                    dsv4_spec_bad = 0;
5813                                    dsv4_spec_retry_at = generated.saturating_add(32);
5814                                    tracing::info!(
5815                                        "dsv4: draft не окупился дважды — точный walk на 32 токена"
5816                                    );
5817                                }
5818                            }
5819                        }
5820                        if let Some((extra, n_pos)) = round {
5821                            next_pos = n_pos;
5822                            let mut stopped = false;
5823                            for &id in &extra {
5824                                if self.confidence_on {
5825                                    confidence.push(0.0);
5826                                }
5827                                if !commit!(id) {
5828                                    stopped = true;
5829                                    break;
5830                                }
5831                            }
5832                            if stopped {
5833                                break 'decode;
5834                            }
5835                            continue 'decode;
5836                        }
5837                    }
5838                    self.graph_want_logits = fuse_lm;
5839                    // Greedy burst (CMF_MULTISTEP, default 8, 1 = off): while
5840                    // nothing observes per-token state — pure argmax sampling,
5841                    // no router/trace/confidence/mask — decode k tokens per
5842                    // submit and commit them wholesale. The trailing normal
5843                    // forward leaves logits for the loop top, as always.
5844                    let mut t_fwd = t_next;
5845                    let pure_greedy = self.sampler_config.temperature < 1e-6
5846                        && self.sampler_config.repetition_penalty == 1.0
5847                        && self.sampler_config.suppress_tokens.is_empty();
5848                    // Off by default: at every k the burst measured at or
5849                    // below the plain path on this graph shape (k=1 loses
5850                    // the argmax dispatches vs a 1 MB readback, k>=8 loses
5851                    // inter-step drains vs the saved sync). Experimental.
5852                    let burst_k = std::env::var("CMF_MULTISTEP")
5853                        .ok()
5854                        .and_then(|v| v.parse::<usize>().ok())
5855                        .unwrap_or(0);
5856                    if pure_greedy
5857                        && burst_k >= 1
5858                        && fuse_lm
5859                        && task_mask.is_none()
5860                        && router.is_none()
5861                        && !trace_on
5862                        && !self.confidence_on
5863                    {
5864                        let mut stopped = false;
5865                        loop {
5866                            let room = max_tokens.saturating_sub(generated);
5867                            if room <= 2 {
5868                                break;
5869                            }
5870                            let k = burst_k.min(room - 1);
5871                            if k < 1 {
5872                                break;
5873                            }
5874                            let Some(ids) = self.try_multi_burst(t_fwd, next_pos, k) else {
5875                                if self
5876                                    .graph_failed
5877                                    .swap(false, std::sync::atomic::Ordering::Relaxed)
5878                                {
5879                                    self.finish_generation(&mut mtp, &mut router, true);
5880                                    return Err(
5881                                        "GPU token graph failed during greedy burst".to_string()
5882                                    );
5883                                }
5884                                break;
5885                            };
5886                            next_pos += k;
5887                            for &id in &ids {
5888                                if !commit!(id) {
5889                                    stopped = true;
5890                                    break;
5891                                }
5892                            }
5893                            if stopped {
5894                                break;
5895                            }
5896                            t_fwd = *ids.last().unwrap();
5897                        }
5898                        if stopped {
5899                            break 'decode;
5900                        }
5901                    }
5902                    // Metal: keep the draft head's cache in step through
5903                    // the trial's plain phase and a paused speculation —
5904                    // the pair (hidden, t_fwd) at next_pos−1, the step the
5905                    // round's draft 0 would take. Without it the head's
5906                    // cache lagged the trunk by every plain token for the
5907                    // rest of the generation: the batched warm-up declined
5908                    // every later round and its rows went one by one (a
5909                    // whole MTP step per accepted token), and the drafts
5910                    // attended a context with those tokens missing.
5911                    #[cfg(target_os = "macos")]
5912                    if graph_spec
5913                        && spec_watchdog_off
5914                        && next_pos > 0
5915                        && self.mtp_graph_mode == Some(true)
5916                        && crate::gpu::q1_force()
5917                    {
5918                        if let Some(m) = mtp.as_mut() {
5919                            let _ = self.mtp_step_metal(m, &hidden, t_fwd, next_pos - 1, false);
5920                        }
5921                    }
5922                    hidden = self.forward_layers(&self.embed_single(t_fwd), next_pos, task_mask);
5923                    next_pos += 1;
5924                    // Dynamic routing: the forward updated φ; ask the
5925                    // router whether to switch skills before the next token.
5926                    if let Some(r) = &mut router {
5927                        let phi = self.dyn_phi_ema.clone();
5928                        let decision = r.step(&phi, generated);
5929                        if let Some(new_active) = decision {
5930                            let _ = self.set_active_skill(new_active);
5931                        }
5932                        // Backfill this token's coherence + switch flag from
5933                        // the just-run eval (freshest measured values).
5934                        if trace_on {
5935                            if let Some(last) = traces.last_mut() {
5936                                let e = r.last_best_e();
5937                                last.recon = e.is_finite().then_some(e);
5938                                last.switched = decision.is_some();
5939                            }
5940                        }
5941                    }
5942                }
5943            }
5944        }
5945
5946        let cancelled = finish_reason == "cancelled";
5947        // A generation during which the router switched weights holds no
5948        // state any single overlay would produce (each switch cleared the
5949        // cache mid-sequence), so it leaves no reuse key behind either.
5950        let dyn_switched = router.as_ref().is_some_and(|r| !r.switches.is_empty());
5951        if mimo_spec {
5952            if let Some(st) = self.mimo_mtp.as_ref() {
5953                let line = st.stats.line();
5954                tracing::info!("{line}");
5955                if std::env::var_os("CMF_MIMO_MTP_STATS").is_some() {
5956                    eprintln!("{line}");
5957                }
5958            }
5959        }
5960        self.finish_generation(&mut mtp, &mut router, cancelled);
5961
5962        let output_ids = &all_ids[input_ids.len()..];
5963        // Forwarded = prompt + all generated but the LAST sampled token
5964        // (emitted without being fed back). Exact only without MTP —
5965        // reuse is gated off when MTP is active.
5966        let forwarded = input_ids.len() + output_ids.len().saturating_sub(1);
5967        // A MiMo speculative round that stopped on an accepted draft (EOS,
5968        // cancel) leaves verify rows past the committed stream in the cache:
5969        // never offer that cache for reuse. Neither does a router that
5970        // switched weights mid-sequence (no single overlay produced it).
5971        let consumed = std::mem::take(&mut all_ids);
5972        if dyn_switched {
5973            self.clear_sequence_state();
5974        } else if cancelled || mimo_spec || prompt_rows.is_some() {
5975            self.clear_history();
5976        } else {
5977            self.record_consumed_prefix(&consumed[..forwarded.min(consumed.len())], reuse_from);
5978        }
5979        all_ids = consumed;
5980        let output_ids = &all_ids[input_ids.len()..];
5981        confidence.truncate(output_ids.len()); // guard against any overshoot
5982        traces.truncate(output_ids.len());
5983        Ok(GenerateResult {
5984            text: self.tokenizer.decode(output_ids),
5985            token_ids: output_ids.to_vec(),
5986            prompt_tokens: input_ids.len(),
5987            tokens_generated: generated,
5988            finish_reason,
5989            mtp_drafted: drafted,
5990            mtp_accepted: accepted,
5991            token_confidence: confidence,
5992            traces,
5993        })
5994    }
5995
5996    /// One MTP step: feed `(hidden_p, token_{p+1})` into the draft head,
5997    /// advance its KV cache at position `p`, return the drafted token
5998    /// for position `p+2`.
5999    fn mtp_step(
6000        &mut self,
6001        m: &mut MtpModule,
6002        hidden: &[f32],
6003        next_token: u32,
6004        position: usize,
6005    ) -> u32 {
6006        self.mtp_step_h(m, hidden, next_token, position).0
6007    }
6008
6009    /// Tally for `CMF_MTP_CHAIN_PROBE`: per depth, how often the CHAIN is
6010    /// still an exact prefix of the real continuation. Printed every 128
6011    /// depth-0 samples so a killed run still shows its table.
6012    fn chain_probe_note(depth: usize, prefix_ok: bool) {
6013        use std::sync::Mutex;
6014        static T: Mutex<Vec<(u64, u64)>> = Mutex::new(Vec::new());
6015        let mut t = T.lock().unwrap();
6016        if t.len() <= depth {
6017            t.resize(depth + 1, (0, 0));
6018        }
6019        t[depth].0 += 1;
6020        t[depth].1 += prefix_ok as u64;
6021        if depth == 0 && t[0].0 % 128 == 0 {
6022            let line: Vec<String> = t
6023                .iter()
6024                .enumerate()
6025                .map(|(d, (n, k))| {
6026                    format!(
6027                        "d{}={:.0}%({n})",
6028                        d + 1,
6029                        100.0 * *k as f64 / (*n).max(1) as f64
6030                    )
6031                })
6032                .collect();
6033            eprintln!("mtp-chain: {}", line.join(" "));
6034        }
6035    }
6036
6037    /// `mtp_step` that also hands back the block's own output hidden — the
6038    /// state a CHAINED draft feeds the next step, the way a multi-token
6039    /// speculative round iterates the head on itself.
6040    /// One MTP block step from (trunk hidden, token): the head's LOGITS
6041    /// and the block's own hidden for chaining. The draft is argmax of the
6042    /// logits on the greedy path and a draw from their post-chain
6043    /// distribution on the sampling path.
6044    fn mtp_step_hl(
6045        &mut self,
6046        m: &mut MtpModule,
6047        hidden: &[f32],
6048        next_token: u32,
6049        position: usize,
6050    ) -> (Vec<f32>, Vec<f32>) {
6051        // The graph arm: the MTP block as a one-layer token graph with the
6052        // head fused — device attention over the block's own KV mirror,
6053        // one submit for block + head, hidden and logits back together.
6054        // Decided once per generation (see `mtp_graph_mode`).
6055        #[cfg(target_os = "macos")]
6056        if self.mtp_graph_mode != Some(false) && crate::gpu::q1_force() {
6057            if let Some(r) = self.mtp_step_metal(m, hidden, next_token, position, true) {
6058                self.mtp_graph_mode = Some(true);
6059                return r;
6060            }
6061            if self.mtp_graph_mode == Some(true) {
6062                tracing::error!("mtp Metal graph failed after admission");
6063                self.clear_sequence_state();
6064                self.graph_failed
6065                    .store(true, std::sync::atomic::Ordering::Relaxed);
6066                self.cancel
6067                    .store(true, std::sync::atomic::Ordering::Relaxed);
6068                return (Vec::new(), Vec::new());
6069            }
6070            self.mtp_graph_mode = Some(false);
6071        }
6072        #[cfg(feature = "gpu")]
6073        if self.mtp_graph_mode != Some(false) {
6074            if !self.mtp_graph_ok(m) {
6075                if self.mtp_graph_mode == Some(true) {
6076                    // A mirror was already admitted, so a capability change
6077                    // cannot safely switch this request to the stale CPU
6078                    // cache.  Keep the same terminal contract as a failed
6079                    // token graph.
6080                    tracing::error!("mtp graph became unavailable after admission");
6081                    self.clear_sequence_state();
6082                    self.graph_failed
6083                        .store(true, std::sync::atomic::Ordering::Relaxed);
6084                    self.cancel
6085                        .store(true, std::sync::atomic::Ordering::Relaxed);
6086                    return (Vec::new(), Vec::new());
6087                }
6088                self.mtp_graph_mode = Some(false);
6089            } else {
6090                if let Some(r) = self.mtp_step_graph(m, hidden, next_token, position) {
6091                    self.mtp_graph_mode = Some(true);
6092                    return r;
6093                }
6094                if self.graph_failed.load(std::sync::atomic::Ordering::Relaxed) {
6095                    // A token graph can have admitted a persistent MTP/GDN
6096                    // mirror before its readback failed.  The CPU MTP cache
6097                    // is not a valid continuation in that state; leave the
6098                    // flag set so the generation caller returns through its
6099                    // terminal error path instead of silently switching
6100                    // arithmetic.
6101                    return (Vec::new(), Vec::new());
6102                }
6103                // `mtp_graph_ok` was true, so a None here means a refusal or
6104                // failure after graph admission.  Do not fall through to a
6105                // CPU cache whose rows may lag the device mirror.
6106                tracing::error!("mtp graph failed or declined after admission");
6107                self.clear_sequence_state();
6108                self.graph_failed
6109                    .store(true, std::sync::atomic::Ordering::Relaxed);
6110                self.cancel
6111                    .store(true, std::sync::atomic::Ordering::Relaxed);
6112                return (Vec::new(), Vec::new());
6113            }
6114        }
6115        // fc concat order is [enorm(embed); hnorm(hidden)] — EMBEDDING
6116        // FIRST. Verified by the oracle (converter/mtp_oracle.py):
6117        // [emb;hid] → 45.8% acceptance, [hid;emb] → 0.00%.
6118        let e = self.embed_single(next_token);
6119        let mut cat = vec![0.0f32; 2 * self.hidden_size];
6120        let (cat_e, cat_h) = cat.split_at_mut(self.hidden_size);
6121        inference::rms_norm_into(&e, &m.enorm, self.rms_eps, self.norm_style, cat_e);
6122        inference::rms_norm_into(hidden, &m.hnorm, self.rms_eps, self.norm_style, cat_h);
6123        let mut x = vec![0.0f32; self.hidden_size];
6124        m.eh_proj.matvec(&cat, &mut x, self.pool.as_deref());
6125
6126        // One standard transformer block over the MTP's own cache.
6127        let lw = &m.layer;
6128        inference::rms_norm_into(
6129            &x,
6130            &lw.input_norm,
6131            self.rms_eps,
6132            self.norm_style,
6133            &mut self.ws.n1,
6134        );
6135        let attn = match &lw.attn {
6136            // MLA models carry no MTP head; this path cannot see them.
6137            AttnKind::Mla(_) => unreachable!("MLA has no MTP/pair path"),
6138            AttnKind::Kda(_) => unreachable!("KDA has no MTP/pair path"),
6139            AttnKind::Full {
6140                wq,
6141                wk,
6142                wv,
6143                wo,
6144                q_norm,
6145                k_norm,
6146                output_gate,
6147                softplus_gate,
6148                bias,
6149            } => {
6150                let mut cfg = self.attn_cfg(position);
6151                cfg.q_norm = q_norm.as_deref();
6152                cfg.k_norm = k_norm.as_deref();
6153                cfg.output_gate = *output_gate;
6154                cfg.softplus_gate = softplus_gate
6155                    .as_ref()
6156                    .map(|(gate, per_head)| (gate, *per_head));
6157                cfg.bias = bias
6158                    .as_ref()
6159                    .map(|(q, k, v)| (q.as_slice(), k.as_slice(), v.as_slice()));
6160                attention::qwen_attention(&self.ws.n1, wq, wk, wv, wo, &mut m.kv, &cfg)
6161            }
6162            AttnKind::Linear(_)
6163            | AttnKind::LinearGdn(_)
6164            | AttnKind::ShortConv(_)
6165            | AttnKind::Bounded(_) => {
6166                unreachable!("MTP block is full attention")
6167            }
6168        };
6169        for (i, &a) in attn.iter().enumerate() {
6170            x[i] += a;
6171        }
6172        inference::rms_norm_into(
6173            &x,
6174            &lw.post_norm,
6175            self.rms_eps,
6176            self.norm_style,
6177            &mut self.ws.p1,
6178        );
6179        let ffn = ffn_forward(&lw.ffn, &self.ws.p1, self.pool.as_deref(), None);
6180        for (i, &f) in ffn.iter().enumerate() {
6181            x[i] += f;
6182        }
6183
6184        inference::rms_norm_into(
6185            &x,
6186            &m.final_norm,
6187            self.rms_eps,
6188            self.norm_style,
6189            &mut self.ws.n1,
6190        );
6191        let lg = self.lm_head_forward(&self.ws.n1);
6192        (lg, x)
6193    }
6194
6195    /// `mtp_step_hl` reduced to the greedy draft: argmax of the head.
6196    fn mtp_step_h(
6197        &mut self,
6198        m: &mut MtpModule,
6199        hidden: &[f32],
6200        next_token: u32,
6201        position: usize,
6202    ) -> (u32, Vec<f32>) {
6203        let (mut lg, x) = self.mtp_step_hl(m, hidden, next_token, position);
6204        let draft = sampler::argmax(&lg);
6205        attention::recycle_buf(&mut lg);
6206        (draft, x)
6207    }
6208
6209    /// One speculative round for the trial: rounds 1..5 of a `Spec` phase
6210    /// advance it (the monitor already averaged this round); after five,
6211    /// the plain phase runs (once — a known plain rate decides at once);
6212    /// a decided speculation keeps re-checking the rule every round and
6213    /// stops after four losing rounds in a row.
6214    fn spec_trial_round(trial: SpecTrial, mon: &mut SpecMon, generated: usize) -> SpecTrial {
6215        match trial {
6216            SpecTrial::Spec { t0, gen0, rounds } => {
6217                let rounds = rounds + 1;
6218                if rounds >= 5 {
6219                    if mon.plain_ms > 0.0 {
6220                        let keep = mon.pays();
6221                        mon.fails = 0;
6222                        tracing::info!(
6223                            "speculation re-check: {:.2} tok/round in {:.1} ms vs plain {:.1} ms/tok — {}",
6224                            mon.tokens,
6225                            mon.round_ms,
6226                            mon.plain_ms,
6227                            if keep { "speculating" } else { "plain" }
6228                        );
6229                        SpecTrial::Decided {
6230                            spec: keep,
6231                            recheck_at: if keep { usize::MAX } else { generated + 128 },
6232                        }
6233                    } else if mon.pays() {
6234                        // Metal: the rounds land enough tokens each that no
6235                        // plain measurement is needed — keep speculating,
6236                        // and re-check every round (a losing streak sends
6237                        // the loop to the plain phase, below).
6238                        mon.fails = 0;
6239                        tracing::info!(
6240                            "speculation trial: {:.2} tok/round in {:.1} ms — speculating (plain not timed)",
6241                            mon.tokens,
6242                            mon.round_ms,
6243                        );
6244                        SpecTrial::Decided {
6245                            spec: true,
6246                            recheck_at: usize::MAX,
6247                        }
6248                    } else {
6249                        SpecTrial::Plain {
6250                            t0: std::time::Instant::now(),
6251                            gen0: generated,
6252                        }
6253                    }
6254                } else {
6255                    SpecTrial::Spec { t0, gen0, rounds }
6256                }
6257            }
6258            SpecTrial::Decided { spec: true, .. } => {
6259                if mon.pays() {
6260                    mon.fails = 0;
6261                    trial
6262                } else {
6263                    mon.fails += 1;
6264                    if mon.fails >= 4 {
6265                        if mon.plain_ms <= 0.0 {
6266                            // Metal, plain never timed: four doubtful rounds
6267                            // buy the (bounded) plain measurement, and the
6268                            // exact rule decides from it.
6269                            tracing::info!(
6270                                "speculation doubtful: {:.2} tok/round in {:.1} ms — timing plain",
6271                                mon.tokens,
6272                                mon.round_ms,
6273                            );
6274                            return SpecTrial::Plain {
6275                                t0: std::time::Instant::now(),
6276                                gen0: generated,
6277                            };
6278                        }
6279                        tracing::info!(
6280                            "speculation stopped: {:.2} tok/round in {:.1} ms vs plain {:.1} ms/tok",
6281                            mon.tokens,
6282                            mon.round_ms,
6283                            mon.plain_ms
6284                        );
6285                        SpecTrial::Decided {
6286                            spec: false,
6287                            recheck_at: generated + 128,
6288                        }
6289                    } else {
6290                        trial
6291                    }
6292                }
6293            }
6294            other => other,
6295        }
6296    }
6297
6298    /// The MTP block's device-mirror id: the trunk's id with a high bit,
6299    /// so the (kv_id, layer) mirror keys never collide.
6300    fn mtp_kv_id(&self) -> u64 {
6301        self.graph_kv_id | (1u64 << 40)
6302    }
6303
6304    /// The MTP block's mirror layer index: 0 — its own kv_id keeps it
6305    /// apart from the trunk, and the BATCH graph (the warm-up path) keys
6306    /// its mirrors at layer 0 with no base of its own, so the draft's
6307    /// token graph must key the same slot.
6308    const MTP_LAYER_BASE: usize = 0;
6309
6310    /// The wgpu MTP draft writes speculative rows straight into its device
6311    /// mirror while the CPU owner retains only the real prompt/decode anchor.
6312    /// After verification, move that mirror cursor back to the anchor before
6313    /// replaying accepted pairs.  The next graph append then sees the same
6314    /// contiguous position as the CPU/Metal path without uploading stale
6315    /// speculative rows.
6316    #[cfg(feature = "gpu")]
6317    fn rewind_mtp_graph_mirror(&self, stored: usize) -> bool {
6318        self.mtp_graph_mode != Some(true)
6319            || crate::gpu::graph_kv_set_stored(self.mtp_kv_id(), Self::MTP_LAYER_BASE, stored)
6320    }
6321
6322    /// A speculative verify graph appends the full `k+1` trunk rows before
6323    /// the acceptance count is known.  GDN state already has a snapshot
6324    /// restore; Full-attention mirrors need the matching logical cursor
6325    /// rewind so the next graph call does not reject an ahead-of-position KV
6326    /// cache after a partial acceptance.
6327    #[cfg(feature = "gpu")]
6328    fn rewind_trunk_graph_mirrors(&self, stored: usize) -> bool {
6329        let mut ok = true;
6330        let mut expected = false;
6331        for li in 0..self.num_layers {
6332            if matches!(
6333                self.weights.layers[self.phys_layer(li)].attn,
6334                AttnKind::Full { .. }
6335            ) {
6336                expected = true;
6337                ok &= crate::gpu::graph_kv_set_stored(self.graph_kv_id, li, stored);
6338            }
6339        }
6340        !expected || ok
6341    }
6342
6343    /// Count the recurrent layers participating in the trunk verify graph.
6344    /// Snapshot restore is all-or-nothing across that set; deriving the count
6345    /// from the model keeps the restore contract valid for looped models too.
6346    fn graph_gdn_layer_count(&self) -> usize {
6347        (0..self.num_layers)
6348            .filter(|&li| {
6349                matches!(
6350                    &self.weights.layers[self.phys_layer(li)].attn,
6351                    AttnKind::LinearGdn(_)
6352                )
6353            })
6354            .count()
6355    }
6356
6357    /// The block's input from (trunk hidden, token): eh_proj · [enorm(e);
6358    /// hnorm(h)] — the same arithmetic the per-op path starts with.
6359    fn mtp_block_input(&mut self, m: &MtpModule, hidden: &[f32], next_token: u32) -> Vec<f32> {
6360        let e = self.embed_single(next_token);
6361        let mut cat = vec![0.0f32; 2 * self.hidden_size];
6362        let (cat_e, cat_h) = cat.split_at_mut(self.hidden_size);
6363        inference::rms_norm_into(&e, &m.enorm, self.rms_eps, self.norm_style, cat_e);
6364        inference::rms_norm_into(hidden, &m.hnorm, self.rms_eps, self.norm_style, cat_h);
6365        let mut x = vec![0.0f32; self.hidden_size];
6366        m.eh_proj.matvec(&cat, &mut x, self.pool.as_deref());
6367        x
6368    }
6369
6370    /// Is the MTP block graphable at all (device up, full attention
6371    /// without softplus, dense FFN)? The plan itself is built per call.
6372    #[cfg(feature = "gpu")]
6373    fn mtp_block_graph_ok(&self, m: &MtpModule) -> bool {
6374        if std::env::var("CMF_MTP_GRAPH").as_deref() == Ok("0") {
6375            return false;
6376        }
6377        if !crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Decode)
6378            || !crate::gpu::enabled_here()
6379            || self.attn_softcap > 0.0
6380            || self.attention_heads_per_layer.is_some()
6381            // The block graph caches V as wide as K and feeds o_proj
6382            // nh·head_dim; a narrow-V model keeps its MTP block per-op.
6383            || self.v_head_dim.is_some()
6384        {
6385            return false;
6386        }
6387        matches!(
6388            &m.layer.attn,
6389            AttnKind::Full {
6390                softplus_gate: None,
6391                ..
6392            }
6393        ) && matches!(&m.layer.ffn, FfnKind::Dense(_))
6394    }
6395
6396    /// Full MTP token-graph eligibility, including the fused lm-head and all
6397    /// block projection weights.  Keep this distinct from the block-only
6398    /// check: prompt warm-up does not need the head, while a draft step does.
6399    #[cfg(feature = "gpu")]
6400    fn mtp_graph_ok(&self, m: &MtpModule) -> bool {
6401        if !self.mtp_block_graph_ok(m) {
6402            return false;
6403        }
6404        let AttnKind::Full { wq, wk, wv, wo, .. } = &m.layer.attn else {
6405            return false;
6406        };
6407        let FfnKind::Dense(d) = &m.layer.ffn else {
6408            return false;
6409        };
6410        d.segs.is_empty()
6411            && wq.graph_weight().is_some()
6412            && wk.graph_weight().is_some()
6413            && wv.graph_weight().is_some()
6414            && wo.graph_weight().is_some()
6415            && d.gate_proj.graph_weight().is_some()
6416            && d.up_proj.graph_weight().is_some()
6417            && d.down_proj.graph_weight().is_some()
6418            && self.weights.lm_head.graph_weight().is_some()
6419    }
6420
6421    /// One MTP block step on the wgpu token graph: block + fused head in
6422    /// one submit, the block hidden and the logits read back together.
6423    /// None = the graph cannot take this block (softplus gate, non-dense
6424    /// FFN, unquantized head, no device) — the caller keeps the per-op
6425    /// path for the whole generation.
6426    #[cfg(feature = "gpu")]
6427    fn mtp_step_graph(
6428        &mut self,
6429        m: &mut MtpModule,
6430        hidden: &[f32],
6431        next_token: u32,
6432        position: usize,
6433    ) -> Option<(Vec<f32>, Vec<f32>)> {
6434        if !self.mtp_graph_ok(m) {
6435            return None;
6436        }
6437        let lw = &m.layer;
6438        let AttnKind::Full {
6439            wq,
6440            wk,
6441            wv,
6442            wo,
6443            q_norm,
6444            k_norm,
6445            output_gate,
6446            softplus_gate,
6447            bias,
6448        } = &lw.attn
6449        else {
6450            return None;
6451        };
6452        if softplus_gate.is_some() {
6453            return None;
6454        }
6455        let FfnKind::Dense(d) = &lw.ffn else {
6456            return None;
6457        };
6458        if !d.segs.is_empty() {
6459            return None; // tube layers run on the segmented path
6460        }
6461        // The block's input first: it borrows `self` mutably (embed scratch,
6462        // pool), the plan below borrows the weights immutably.
6463        let mut x = self.mtp_block_input(m, hidden, next_token);
6464        fn gw(t: &QTensor) -> Option<crate::gpu::GraphW<'_>> {
6465            let (_, i, kind, rs) = t.graph_weight()?;
6466            Some(crate::gpu::GraphW {
6467                idx: i,
6468                kind,
6469                row_scale: rs,
6470                data: &[],
6471                prism: crate::gpu::GraphPrismOp::None,
6472                affine: false,
6473            })
6474        }
6475        let (model, _, _, _) = wq.graph_weight()?;
6476        let model = model.clone();
6477        let (lm_gw, lm_rows) = {
6478            let (_, i, kind, rs) = self.weights.lm_head.graph_weight()?;
6479            // The draft's head over the CMF_DRAFT_VOCAB shortlist (the same
6480            // cut the native Metal draft takes): 662 MB a step on Qwen3.8
6481            // becomes 170 MB at 65536; the verify keeps the full head.
6482            let rows = if kind == 6 {
6483                self.draft_head_rows(self.weights.lm_head.rows())
6484            } else {
6485                self.weights.lm_head.rows()
6486            };
6487            (
6488                crate::gpu::GraphW {
6489                    idx: i,
6490                    kind,
6491                    row_scale: rs,
6492                    data: &[],
6493                    prism: crate::gpu::GraphPrismOp::None,
6494                    affine: false,
6495                },
6496                rows,
6497            )
6498        };
6499        let layer = crate::gpu::GraphLayer {
6500            input_norm: &lw.input_norm,
6501            attn: crate::gpu::GraphAttn::Full {
6502                wq: gw(wq)?,
6503                wk: gw(wk)?,
6504                wv: gw(wv)?,
6505                wo: gw(wo)?,
6506                q_norm: q_norm.as_deref(),
6507                k_norm: k_norm.as_deref(),
6508                late_qk_norm: self.qk_norm_after_rope,
6509                bias: bias
6510                    .as_ref()
6511                    .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
6512                output_gate: *output_gate,
6513                cpu_k: m.kv.k_heads(),
6514                cpu_v: m.kv.v_heads(),
6515                cpu_base: m.kv.base(),
6516                geom: None,
6517                head_gate: None,
6518            },
6519            post_norm: &lw.post_norm,
6520            attn_out_norm: None,
6521            ffn_out_norm: None,
6522            layer_scale: None,
6523            ffn: crate::gpu::GraphFfn::Dense {
6524                gate: gw(&d.gate_proj)?,
6525                up: gw(&d.up_proj)?,
6526                down: gw(&d.down_proj)?,
6527                act: d.act.graph_act()?,
6528            },
6529        };
6530        let nh = self.num_heads;
6531        let (nkv, hd, rd) = self.layer_geom(0);
6532        let gemma = self.norm_style == cortiq_core::NormStyle::Gemma;
6533        let mut logits = Vec::new();
6534        let ok = crate::gpu::forward_token_graph(
6535            &model,
6536            self.mtp_kv_id(),
6537            std::slice::from_ref(&layer),
6538            &[None],
6539            self.o1_epoch,
6540            &self.inv_freq,
6541            &mut x,
6542            nh,
6543            nkv,
6544            hd,
6545            self.attn_scale,
6546            rd,
6547            self.hidden_size,
6548            self.intermediate_size,
6549            position,
6550            self.kv_cache.max_seq_len,
6551            gemma,
6552            self.rms_eps as f32,
6553            Some((&lm_gw, lm_rows)),
6554            &m.final_norm,
6555            &mut logits,
6556            &[],
6557            1,
6558            None,
6559            None,
6560            None,
6561            Self::MTP_LAYER_BASE,
6562            true,
6563        );
6564        match ok {
6565            crate::gpu::TokenGraphOutcome::Completed => {}
6566            crate::gpu::TokenGraphOutcome::Declined => return None,
6567            crate::gpu::TokenGraphOutcome::Failed => {
6568                // The backend has already admitted persistent state.  Keep
6569                // this distinct from a capability refusal so the caller
6570                // cannot switch to the stale CPU MTP cache.
6571                self.clear_sequence_state();
6572                self.graph_failed
6573                    .store(true, std::sync::atomic::Ordering::Relaxed);
6574                self.cancel
6575                    .store(true, std::sync::atomic::Ordering::Relaxed);
6576                return None;
6577            }
6578        }
6579        logits.resize(self.vocab_size, 0.0);
6580        Some((logits, x))
6581    }
6582
6583    /// The warm-ups of one speculative round on the device: every accepted
6584    /// (hidden, token) pair as ONE batched graph run over the MTP block
6585    /// (no head) — its kv_append lands the pairs in the block's mirror.
6586    /// `pairs` are consecutive positions from `first_pos`.  The tri-state
6587    /// result is intentional: a refusal before admission may use the
6588    /// per-row/CPU route, while a failure after admission must terminate the
6589    /// sequence rather than fall through to a stale CPU cache.
6590    #[cfg(feature = "gpu")]
6591    fn mtp_warm_graph(
6592        &mut self,
6593        m: &mut MtpModule,
6594        pairs: &[(&[f32], u32)],
6595        first_pos: usize,
6596    ) -> crate::gpu::BatchGraphOutcome {
6597        if pairs.is_empty() {
6598            return crate::gpu::BatchGraphOutcome::Completed;
6599        }
6600        if !self.mtp_block_graph_ok(m) {
6601            return crate::gpu::BatchGraphOutcome::Declined;
6602        }
6603        let hs = self.hidden_size;
6604        // Block inputs for every pair (eh_proj on the per-op path, one
6605        // matvec each — the plan's own prologue).
6606        let mut hiddens = Vec::with_capacity(pairs.len() * hs);
6607        for (h, t) in pairs {
6608            hiddens.extend_from_slice(&self.mtp_block_input(m, h, *t));
6609        }
6610        let lw = &m.layer;
6611        let AttnKind::Full {
6612            wq,
6613            wk,
6614            wv,
6615            wo,
6616            q_norm,
6617            k_norm,
6618            output_gate,
6619            bias,
6620            ..
6621        } = &lw.attn
6622        else {
6623            return crate::gpu::BatchGraphOutcome::Declined;
6624        };
6625        let FfnKind::Dense(d) = &lw.ffn else {
6626            return crate::gpu::BatchGraphOutcome::Declined;
6627        };
6628        if !d.segs.is_empty() {
6629            return crate::gpu::BatchGraphOutcome::Declined; // tube layers run on the segmented path
6630        }
6631        // The graph has no arm for a projected output gate, and only the
6632        // activations `graph_act` names.
6633        let (AttnKind::Full { softplus_gate: None, .. }, Some(gact)) =
6634            (&lw.attn, d.act.graph_act())
6635        else {
6636            return crate::gpu::BatchGraphOutcome::Declined;
6637        };
6638        fn gw(t: &QTensor) -> Option<crate::gpu::GraphW<'_>> {
6639            let (_, i, kind, rs) = t.graph_weight()?;
6640            Some(crate::gpu::GraphW {
6641                idx: i,
6642                kind,
6643                row_scale: rs,
6644                data: &[],
6645                prism: crate::gpu::GraphPrismOp::None,
6646                affine: false,
6647            })
6648        }
6649        let Some((model, _, _, _)) = wq.graph_weight() else {
6650            return crate::gpu::BatchGraphOutcome::Declined;
6651        };
6652        let model = model.clone();
6653        let (Some(gwq), Some(gwk), Some(gwv), Some(gwo), Some(gg), Some(gu), Some(gd)) = (
6654            gw(wq),
6655            gw(wk),
6656            gw(wv),
6657            gw(wo),
6658            gw(&d.gate_proj),
6659            gw(&d.up_proj),
6660            gw(&d.down_proj),
6661        ) else {
6662            return crate::gpu::BatchGraphOutcome::Declined;
6663        };
6664        let layer = crate::gpu::GraphLayer {
6665            input_norm: &lw.input_norm,
6666            attn: crate::gpu::GraphAttn::Full {
6667                wq: gwq,
6668                wk: gwk,
6669                wv: gwv,
6670                wo: gwo,
6671                q_norm: q_norm.as_deref(),
6672                k_norm: k_norm.as_deref(),
6673                late_qk_norm: self.qk_norm_after_rope,
6674                bias: bias
6675                    .as_ref()
6676                    .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
6677                output_gate: *output_gate,
6678                cpu_k: m.kv.k_heads(),
6679                cpu_v: m.kv.v_heads(),
6680                cpu_base: m.kv.base(),
6681                geom: None,
6682                head_gate: None,
6683            },
6684            post_norm: &lw.post_norm,
6685            attn_out_norm: None,
6686            ffn_out_norm: None,
6687            layer_scale: None,
6688            ffn: crate::gpu::GraphFfn::Dense {
6689                gate: gg,
6690                up: gu,
6691                down: gd,
6692                act: gact,
6693            },
6694        };
6695        let positions: Vec<usize> = (first_pos..first_pos + pairs.len()).collect();
6696        let nh = self.num_heads;
6697        let (nkv, hd, rd) = self.layer_geom(0);
6698        let gemma = self.norm_style == cortiq_core::NormStyle::Gemma;
6699        crate::gpu::forward_batch_graph(
6700            &model,
6701            self.mtp_kv_id(),
6702            std::slice::from_ref(&layer),
6703            &self.inv_freq,
6704            &mut hiddens,
6705            nh,
6706            nkv,
6707            hd,
6708            rd,
6709            hs,
6710            self.intermediate_size,
6711            &positions,
6712            self.kv_cache.max_seq_len,
6713            gemma,
6714            self.rms_eps as f32,
6715            self.attn_scale,
6716            pairs.len(),
6717            &[],
6718            0,
6719            None,
6720            None,
6721        )
6722    }
6723
6724    /// Complete an MTP warm-up after the batched graph has refused.  A
6725    /// graphable block is retried one row at a time; once any device row has
6726    /// been admitted, a CPU fallback would observe a stale mirror, so every
6727    /// token-graph refusal is terminal.  If the block is not graphable and no
6728    /// mirror exists yet, warming on the CPU is safe and records the CPU mode
6729    /// for the rest of the generation.
6730    #[cfg(feature = "gpu")]
6731    fn mtp_warm_graph_fallback(
6732        &mut self,
6733        m: &mut MtpModule,
6734        pairs: &[(&[f32], u32)],
6735        first_pos: usize,
6736    ) -> bool {
6737        if pairs.is_empty() {
6738            return true;
6739        }
6740        let graphable = self.mtp_block_graph_ok(m);
6741        if !graphable {
6742            // A previously admitted mirror cannot be made coherent by
6743            // appending to the host cache.  The caller turns this into a
6744            // terminal generation error and clears both mirrors.
6745            if self.mtp_graph_mode == Some(true) {
6746                return false;
6747            }
6748            self.mtp_graph_mode = Some(false);
6749            for (j, (h, t)) in pairs.iter().enumerate() {
6750                self.mtp_warm(m, h, *t, first_pos + j);
6751            }
6752            return true;
6753        }
6754
6755        // The batch refusal is recoverable only through the same device
6756        // state.  Keep rows owned until each token graph has completed; a
6757        // None is treated as unsafe because the token-graph API deliberately
6758        // collapses its backend refusal/failure into that result.
6759        for (j, (h, t)) in pairs.iter().enumerate() {
6760            if self.mtp_step_graph(m, h, *t, first_pos + j).is_none() {
6761                return false;
6762            }
6763        }
6764        self.mtp_graph_mode = Some(true);
6765        true
6766    }
6767
6768    /// Warm a contiguous set of MTP pairs using the existing graph seam, with
6769    /// an all-or-nothing error contract for callers that already admitted the
6770    /// trunk batch.  The non-GPU build keeps the same pair accounting while
6771    /// using the established CPU warm path.
6772    #[cfg(feature = "gpu")]
6773    fn mtp_warm_prefill_pairs(
6774        &mut self,
6775        m: &mut MtpModule,
6776        pairs: &[(&[f32], u32)],
6777        first_pos: usize,
6778    ) -> Result<(), &'static str> {
6779        // Keep unsupported token-graph heads on the established CPU MTP
6780        // route before admitting any block mirror.  Once a device mirror is
6781        // active, the same condition is terminal because CPU rows cannot
6782        // repair its state.
6783        if self.mtp_graph_mode == Some(false) || !self.mtp_graph_ok(m) {
6784            if self.mtp_graph_mode == Some(true) {
6785                return Err("MTP token graph became unavailable after admission");
6786            }
6787            self.mtp_graph_mode = Some(false);
6788            for (j, (h, t)) in pairs.iter().enumerate() {
6789                self.mtp_warm(m, h, *t, first_pos + j);
6790            }
6791            return Ok(());
6792        }
6793        match self.mtp_warm_graph(m, pairs, first_pos) {
6794            crate::gpu::BatchGraphOutcome::Completed => {
6795                if !pairs.is_empty() {
6796                    self.mtp_graph_mode = Some(true);
6797                }
6798                Ok(())
6799            }
6800            crate::gpu::BatchGraphOutcome::Declined => {
6801                if self.mtp_warm_graph_fallback(m, pairs, first_pos) {
6802                    Ok(())
6803                } else {
6804                    Err("MTP warm-up fallback failed after device admission")
6805                }
6806            }
6807            crate::gpu::BatchGraphOutcome::Failed => {
6808                Err("MTP warm batch graph failed after admission")
6809            }
6810        }
6811    }
6812
6813    #[cfg(not(feature = "gpu"))]
6814    fn mtp_warm_prefill_pairs(
6815        &mut self,
6816        m: &mut MtpModule,
6817        pairs: &[(&[f32], u32)],
6818        first_pos: usize,
6819    ) -> Result<(), &'static str> {
6820        for (j, (h, t)) in pairs.iter().enumerate() {
6821            self.mtp_warm(m, h, *t, first_pos + j);
6822        }
6823        Ok(())
6824    }
6825
6826    /// The MTP block alone — advance its KV with a (hidden, token) pair the
6827    /// verify just proved, without paying the head. What keeps the draft's
6828    /// attention context warm between speculative rounds.
6829    fn mtp_warm(&mut self, m: &mut MtpModule, hidden: &[f32], next_token: u32, position: usize) {
6830        let e = self.embed_single(next_token);
6831        let mut cat = vec![0.0f32; 2 * self.hidden_size];
6832        let (cat_e, cat_h) = cat.split_at_mut(self.hidden_size);
6833        inference::rms_norm_into(&e, &m.enorm, self.rms_eps, self.norm_style, cat_e);
6834        inference::rms_norm_into(hidden, &m.hnorm, self.rms_eps, self.norm_style, cat_h);
6835        let mut x = vec![0.0f32; self.hidden_size];
6836        m.eh_proj.matvec(&cat, &mut x, self.pool.as_deref());
6837        inference::rms_norm_into(
6838            &x,
6839            &m.layer.input_norm,
6840            self.rms_eps,
6841            self.norm_style,
6842            &mut self.ws.n1,
6843        );
6844        let attn = match &m.layer.attn {
6845            AttnKind::Full {
6846                wq,
6847                wk,
6848                wv,
6849                wo,
6850                q_norm,
6851                k_norm,
6852                output_gate,
6853                softplus_gate,
6854                bias,
6855            } => {
6856                let mut cfg = self.attn_cfg(position);
6857                cfg.q_norm = q_norm.as_deref();
6858                cfg.k_norm = k_norm.as_deref();
6859                cfg.output_gate = *output_gate;
6860                cfg.softplus_gate = softplus_gate.as_ref().map(|(g, p)| (g, *p));
6861                cfg.bias = bias
6862                    .as_ref()
6863                    .map(|(q, k, v)| (q.as_slice(), k.as_slice(), v.as_slice()));
6864                attention::qwen_attention(&self.ws.n1, wq, wk, wv, wo, &mut m.kv, &cfg)
6865            }
6866            _ => return,
6867        };
6868        let _ = attn;
6869    }
6870
6871    /// Speculative decode ON the wgpu whole-token graph: draft k with the
6872    /// MTP head, verify all of them plus the tip in ONE batched graph
6873    /// submit whose tail folds the head, commit the accepted prefix and
6874    /// roll the GDN state back to the last real position. Greedy only —
6875    /// output equals the plain graph's token for token, the way the DSV4
6876    /// verify equals the walk.
6877    #[cfg(feature = "gpu")]
6878    #[allow(clippy::too_many_arguments)]
6879    fn graph_spec_step(
6880        &mut self,
6881        m: &mut MtpModule,
6882        hidden: &[f32],
6883        t_next: u32,
6884        next_pos: usize,
6885        drafted: &mut usize,
6886        accepted: &mut usize,
6887        // The committed stream (prompt + generated so far, `t_next`
6888        // included): the sampler chain's penalties read it, and the
6889        // sampling arm extends it with the drafts position by position.
6890        all_ids: &mut Vec<u32>,
6891        // Tokens left before `max_tokens`. A round commits up to k
6892        // accepted drafts, and those positions are already in the cache,
6893        // so the depth is capped here — trimming the output afterwards
6894        // would leave cache rows the committed stream does not have.
6895        room: usize,
6896    ) -> Option<(Vec<u32>, usize, Vec<f32>)> {
6897        // 3 is the measured optimum on Qwen3.6-27B / RTX 5090 (medians
6898        // of three, greedy): 51.1 tok/s against a plain 49.4, where k=2
6899        // gives 46.1, k=4 50.0, k=5 47.4, k=6 45.2. Acceptance is 89-91%
6900        // throughout — what turns the curve over is the verify, which
6901        // costs ~7.4 ms per extra position, and the draft ~3 ms a step.
6902        // 4 since the draft moved onto the graph (Qwen3.8-27B / 5090:
6903        // k=3 51.2, k=4 51.8 with the per-op draft; the graph draft
6904        // halves the draft cost, so the extra draft is cheaper still).
6905        // 5 with the int8 verify (the default: measured 76.5 against
6906        // k=4's 72-74 and k=6's 74 on the 5090), 4 with the f32 one.
6907        #[cfg(target_os = "macos")]
6908        let metal_native = crate::gpu::q1_force();
6909        #[cfg(not(target_os = "macos"))]
6910        let metal_native = false;
6911        #[cfg(feature = "gpu")]
6912        let k_default = if metal_native {
6913            // the Metal verify's GEMM tile is 8 rows wide and flat in b:
6914            // seven drafts + the tip fill it for free
6915            7
6916        } else if crate::gpu_wgpu::verify_i8_on() {
6917            5
6918        } else {
6919            4
6920        };
6921        #[cfg(not(feature = "gpu"))]
6922        let k_default = 4;
6923        let k_env: Option<usize> = std::env::var("CMF_GRAPH_SPEC_K")
6924            .ok()
6925            .and_then(|v| v.parse().ok())
6926            .filter(|&v| (1..=8).contains(&v));
6927        // Adaptive depth: start below the card's flat-verify optimum and
6928        // let the accepted fraction move it — predictable text climbs to
6929        // the old default within a few rounds, prose settles at 2-3 where
6930        // the shorter verify pays.
6931        let (k_start, k_max) = if metal_native { (7, 7) } else { (3, k_default.max(5)) };
6932        let k_full: usize = k_env.unwrap_or_else(|| self.spec_k_adapt.unwrap_or(k_start));
6933        let k_spec = k_full.min(room).max(1);
6934        // a tail round cut short by `room` says nothing about the text:
6935        // it must not move the adaptive depth the next request starts at
6936        let k_capped = k_spec < k_full;
6937        if next_pos == 0 {
6938            return None;
6939        }
6940        let t_round = std::time::Instant::now();
6941        // Submissions per phase — and they say where the round's money is.
6942        // Qwen3.6-27B on an RTX 5090, k=3:
6943        //
6944        //   draft   9.3 ms / 12 submissions   (four per MTP step)
6945        //   verify 52.8 ms /  1               (the batched graph)
6946        //   commit  5.4 ms /  6               (two per warm)
6947        //
6948        // The verify is already one submit. The draft's own work is 834 MB
6949        // a step — 0.8 ms at this card's measured 1056 GB/s — against 3.1
6950        // ms measured, so ~0.58 ms of every step is round trip, not
6951        // arithmetic, and the same holds for the warms. Eighteen round
6952        // trips a round at roughly half a millisecond each is ~11 ms of a
6953        // 68 ms round: fusing the MTP block into ONE submit the way the
6954        // trunk already is projects to ~64 tok/s against today's 50.9.
6955        // That is the largest measured item left on this path.
6956        let subs = || crate::gpu_wgpu::SUBMITS.load(std::sync::atomic::Ordering::Relaxed);
6957        let sub0 = subs();
6958        // Greedy without penalties verifies by argmax equality (bit-exact
6959        // against the plain path). Anything else is speculative SAMPLING:
6960        // each draft is a DRAW from the MTP head's post-chain distribution
6961        // q_j, kept for the accept test; the verify's rows give p_j.
6962        let cfg = self.sampler_config.clone();
6963        let penalized = !(cfg.repetition_penalty == 1.0
6964            && cfg.presence_penalty == 0.0
6965            && cfg.suppress_tokens.is_empty());
6966        // Three verify regimes: plain greedy (argmax of the raw rows),
6967        // greedy WITH penalties (argmax of the penalized rows — a single
6968        // pass each, no distributions), and sampling (draw / accept /
6969        // correct on post-chain distributions).
6970        let greedy_pen = cfg.temperature < 1e-6 && penalized;
6971        let sampling = cfg.temperature >= 1e-6;
6972        // Sampling with a top-k goes through the SPARSE chain: the dense
6973        // one builds nine 248k-float distributions a round (four drafts,
6974        // five verify rows) and measured 19-22 tok/s against a plain 40 —
6975        // the host, not the card. Sparse, the same nine cost tens of
6976        // microseconds each.
6977        let sparse = sampling && sampler::sparse_ok(&cfg);
6978        let base_len = all_ids.len();
6979        if sampling && !sparse && self.spec_q.len() < k_spec {
6980            self.spec_q.resize_with(k_spec, Vec::new);
6981        }
6982        if sparse && self.spec_qs.len() < k_spec {
6983            self.spec_qs.resize_with(k_spec, Vec::new);
6984        }
6985        // Draft the chain: first from the trunk's tip hidden, then the head
6986        // iterating on itself. Rows land in the MTP KV; the chain rows past
6987        // the first are speculation over speculative state and roll back
6988        // below, replaced by verified pairs.
6989        let mut drafts = Vec::with_capacity(k_spec);
6990        let mut hx = hidden.to_vec();
6991        // CMF_SPEC_DBG=1: draft 0 through BOTH MTP arms (graph and per-op)
6992        // from the same inputs — are the arms the difference, or the inputs?
6993        let spec_dbg = std::env::var("CMF_SPEC_DBG").is_ok();
6994        spec_stamp("pro");
6995        // Plain greedy on native Metal: the whole chain as one command
6996        // buffer (device argmax + embedding gather between the steps).
6997        // A decline before commit hands the round to the per-step loop
6998        // below; a failure after commit is terminal, like any graph
6999        // failure after admission.
7000        #[cfg(target_os = "macos")]
7001        if metal_native && !sampling && !greedy_pen && self.mtp_graph_mode != Some(false) {
7002            match self.mtp_draft_chain_metal(m, hidden, t_next, next_pos - 1, k_spec) {
7003                Ok(ids) => {
7004                    self.mtp_graph_mode = Some(true);
7005                    drafts = ids;
7006                }
7007                Err(true) => {
7008                    tracing::error!("mtp Metal draft chain failed after commit");
7009                    self.clear_sequence_state();
7010                    self.graph_failed
7011                        .store(true, std::sync::atomic::Ordering::Relaxed);
7012                    self.cancel
7013                        .store(true, std::sync::atomic::Ordering::Relaxed);
7014                    return None;
7015                }
7016                Err(false) => {}
7017            }
7018        }
7019        for j in drafts.len()..k_spec {
7020            let tok_in = if j == 0 { t_next } else { drafts[j - 1] };
7021            let mut dbg_ref: Option<(Vec<f32>, Vec<f32>)> = None;
7022            if spec_dbg {
7023                let saved = self.mtp_graph_mode;
7024                self.mtp_graph_mode = Some(false);
7025                let r = self.mtp_step_hl(m, &hx, tok_in, next_pos - 1 + j);
7026                self.mtp_graph_mode = saved;
7027                if self.graph_failed.load(std::sync::atomic::Ordering::Relaxed) {
7028                    return None;
7029                }
7030                m.kv.truncate_last(1);
7031                dbg_ref = Some(r);
7032            }
7033            let (mut lg, hj) = self.mtp_step_hl(m, &hx, tok_in, next_pos - 1 + j);
7034            if self.graph_failed.load(std::sync::atomic::Ordering::Relaxed) {
7035                return None;
7036            }
7037            if let Some((lg_cpu, h_cpu)) = dbg_ref {
7038                let n = |v: &[f32]| v.iter().map(|x| x * x).sum::<f32>().sqrt();
7039                let dl = lg
7040                    .iter()
7041                    .zip(&lg_cpu)
7042                    .fold(0f32, |m, (a, b)| m.max((a - b).abs()));
7043                let dh = hj
7044                    .iter()
7045                    .zip(&h_cpu)
7046                    .fold(0f32, |m, (a, b)| m.max((a - b).abs()));
7047                eprintln!(
7048                    "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 {}",
7049                    next_pos - 1 + j,
7050                    sampler::argmax(&lg_cpu),
7051                    sampler::argmax(&lg),
7052                    n(&h_cpu),
7053                    n(&hj),
7054                    m.kv.seq_len
7055                );
7056            }
7057            let dj = if sparse {
7058                let mut q = std::mem::take(&mut self.spec_qs[j]);
7059                let ok = sampler::sparse_distribution_into(
7060                    &lg,
7061                    &cfg,
7062                    all_ids,
7063                    &mut self.sampler_scratch,
7064                    self.pool.as_deref(),
7065                    &mut q,
7066                );
7067                let d = if ok {
7068                    sampler::draw_sparse(&q, &mut self.rng)
7069                } else {
7070                    // everything filtered: the dense chain's greedy fallback
7071                    let t = sampler::argmax(&lg);
7072                    q.clear();
7073                    q.push((t, 1.0));
7074                    t
7075                };
7076                self.spec_qs[j] = q;
7077                all_ids.push(d);
7078                d
7079            } else if sampling {
7080                let mut q = std::mem::take(&mut self.spec_q[j]);
7081                sampler::distribution_into(
7082                    &lg,
7083                    &cfg,
7084                    all_ids,
7085                    &mut self.sampler_scratch,
7086                    self.pool.as_deref(),
7087                    &mut q,
7088                );
7089                let d = sampler::draw(&q, &mut self.rng);
7090                self.spec_q[j] = q;
7091                all_ids.push(d); // the next draft's penalties see this one
7092                d
7093            } else if greedy_pen {
7094                let d = sampler::argmax_penalized(
7095                    &lg,
7096                    &cfg,
7097                    all_ids,
7098                    &mut self.sampler_scratch,
7099                    self.pool.as_deref(),
7100                );
7101                all_ids.push(d);
7102                d
7103            } else {
7104                sampler::argmax(&lg)
7105            };
7106            attention::recycle_buf(&mut lg);
7107            drafts.push(dj);
7108            hx = hj;
7109            spec_stamp("d.pick");
7110        }
7111        all_ids.truncate(base_len);
7112        *drafted += k_spec;
7113        let t_draft = t_round.elapsed();
7114        let sub_draft = subs();
7115        // Verify batch: [t_next, d1 .. d_{k-1}] at next_pos.. — every row's
7116        // logits come back from the graph's own head.
7117        let b = k_spec + 1;
7118        let mut hiddens = vec![0.0f32; b * self.hidden_size];
7119        for (i, &t) in std::iter::once(&t_next).chain(drafts.iter()).enumerate() {
7120            let e = self.embed_single(t);
7121            hiddens[i * self.hidden_size..(i + 1) * self.hidden_size].copy_from_slice(&e);
7122        }
7123        let positions: Vec<usize> = (next_pos..next_pos + b).collect();
7124        spec_stamp("v.emb");
7125        let (lm_gw, lm_rows) = {
7126            let (_, i, kind, rs) = self.weights.lm_head.graph_weight()?;
7127            (
7128                crate::gpu::GraphW {
7129                    idx: i,
7130                    kind,
7131                    row_scale: rs,
7132                    data: &[],
7133                    prism: crate::gpu::GraphPrismOp::None,
7134                    affine: false,
7135                },
7136                self.weights.lm_head.rows(),
7137            )
7138        };
7139        let mut logits = Vec::new();
7140        let final_norm = self.weights.final_norm.clone();
7141        // Plain greedy on Metal: the b argmaxes come from the device
7142        // (`argmax_rows` after the head) and the 7.9 MB logits plane is
7143        // never read back — the round's decision needs only the ids, and
7144        // the loop top takes the last verified id as `spec_forced`, which
7145        // is exactly what its argmax of the row would give. The full rows
7146        // stay for anything that reads them: sampling, penalties,
7147        // confidence, the verify oracle, the logit dump.
7148        // `CMF_METAL_DEV_ARGMAX=0` keeps the host path.
7149        #[cfg(target_os = "macos")]
7150        let greedy_dev = metal_native
7151            && !sampling
7152            && !greedy_pen
7153            && !self.confidence_on
7154            && self.final_softcap.is_none()
7155            // The host acceptance argmax scans the WHOLE head row
7156            // (`lm_rows`), the sampler's own row only `vocab_size`: they
7157            // coincide exactly when the head has no padding rows, and
7158            // only then is the device argmax (which scores `vocab_size`)
7159            // bit-identical to both.
7160            && self.vocab_size == lm_rows
7161            && std::env::var_os("CMF_METAL_VERIFY_CHECK").is_none()
7162            && std::env::var_os("CMF_LOGIT_DUMP").is_none()
7163            && std::env::var("CMF_METAL_DEV_ARGMAX").as_deref() != Ok("0");
7164        #[cfg(not(target_os = "macos"))]
7165        let greedy_dev = false;
7166        let mut dev_ids: Vec<u32> = Vec::new();
7167        #[cfg(target_os = "macos")]
7168        let verify_outcome = if metal_native {
7169            let lm = self.weights.lm_head.q1_parts()?;
7170            let n_score = self.vocab_size.min(lm_rows);
7171            self.try_batch_graph_metal(
7172                &mut hiddens,
7173                &positions,
7174                b,
7175                Some((lm, &final_norm, &mut logits)),
7176                if greedy_dev {
7177                    Some((n_score, &mut dev_ids))
7178                } else {
7179                    None
7180                },
7181            )
7182        } else {
7183            self.try_batch_graph_wgpu(
7184                &mut hiddens,
7185                &positions,
7186                b,
7187                Some(crate::gpu::SpecTail {
7188                    lm: lm_gw,
7189                    lm_rows,
7190                    final_norm: &final_norm,
7191                    logits_out: &mut logits,
7192                }),
7193            )
7194        };
7195        #[cfg(not(target_os = "macos"))]
7196        let verify_outcome = self.try_batch_graph_wgpu(
7197            &mut hiddens,
7198            &positions,
7199            b,
7200            Some(crate::gpu::SpecTail {
7201                lm: lm_gw,
7202                lm_rows,
7203                final_norm: &final_norm,
7204                logits_out: &mut logits,
7205            }),
7206        );
7207        match verify_outcome {
7208            crate::gpu::BatchGraphOutcome::Completed => {}
7209            crate::gpu::BatchGraphOutcome::Declined => {
7210                // The verifier refused before admission.  Its draft MTP
7211                // rows are still device-resident, so rewind the separate
7212                // mirror before the caller takes the exact one-token path.
7213                m.kv.truncate_last(k_spec);
7214                if !metal_native && !self.rewind_mtp_graph_mirror(next_pos) {
7215                    self.clear_sequence_state();
7216                    self.graph_failed
7217                        .store(true, std::sync::atomic::Ordering::Relaxed);
7218                    self.cancel
7219                        .store(true, std::sync::atomic::Ordering::Relaxed);
7220                    tracing::error!("MTP graph mirror rewind failed after verify decline");
7221                }
7222                return None;
7223            }
7224            crate::gpu::BatchGraphOutcome::Failed => {
7225                // A failed batch may have advanced trunk/GDN state.  Clear
7226                // both mirrors and preserve the terminal outcome rather than
7227                // falling through to stale CPU state.
7228                self.clear_sequence_state();
7229                self.graph_failed
7230                    .store(true, std::sync::atomic::Ordering::Relaxed);
7231                self.cancel
7232                    .store(true, std::sync::atomic::Ordering::Relaxed);
7233                tracing::error!("MTP verify batch graph failed after admission");
7234                return None;
7235            }
7236        }
7237        // `CMF_METAL_VERIFY_CHECK=1`: run the same b tokens through the
7238        // plain per-token path and compare each row's argmax + logits with
7239        // the verify's — the bring-up oracle for the batched graph. The
7240        // plain forwards mutate the CPU state; it is snapshotted and put
7241        // back, and the K/V mirrors re-pointed, before the round goes on.
7242        #[cfg(target_os = "macos")]
7243        if metal_native && std::env::var("CMF_METAL_VERIFY_CHECK").as_deref() == Ok("1") {
7244            let snap: Vec<Vec<f32>> = self
7245                .kv_cache
7246                .layers
7247                .iter()
7248                .map(|l| l.linear_state.clone())
7249                .collect();
7250            let attn_lens: Vec<usize> = self.kv_cache.layers.iter().map(|l| l.seq_len).collect();
7251            let toks: Vec<u32> = std::iter::once(t_next)
7252                .chain(drafts.iter().copied())
7253                .collect();
7254            let want_save = self.graph_want_logits;
7255            self.graph_want_logits = false;
7256            for (i, &t) in toks.iter().enumerate() {
7257                let hi = self.forward_layers(&self.embed_single(t), next_pos + i, None);
7258                let _ = self.graph_logits.take();
7259                // CMF_SPEC_PLAIN_HIDDEN=1: the next round drafts from the
7260                // plain path's hidden instead of the verify's (an experiment
7261                // on the chain's sensitivity to the half-GEMM noise)
7262                if std::env::var("CMF_SPEC_PLAIN_HIDDEN").as_deref() == Ok("1") {
7263                    hiddens[i * self.hidden_size..(i + 1) * self.hidden_size].copy_from_slice(&hi);
7264                }
7265                let ref_lg = self.logits_from_hidden(&hi);
7266                let row = &logits[i * lm_rows..(i + 1) * lm_rows];
7267                let ra = sampler::argmax(&ref_lg);
7268                let va = sampler::argmax(row);
7269                let mut md = 0f32;
7270                let mut rms = 0f64;
7271                for j in 0..lm_rows.min(ref_lg.len()) {
7272                    let d = (ref_lg[j] - row[j]).abs();
7273                    md = md.max(d);
7274                    rms += (d as f64) * (d as f64);
7275                }
7276                let mut hd = 0f32;
7277                for j in 0..self.hidden_size {
7278                    hd = hd.max((hi[j] - hiddens[i * self.hidden_size + j]).abs());
7279                }
7280                eprintln!(
7281                    "verify-check row {i} tok {t} pos {}: ref argmax {ra} verify argmax {va} {} | max|dlogit| {md:.3} rms {:.4} | max|dhidden| {hd:.4}",
7282                    next_pos + i,
7283                    if ra == va { "OK" } else { "MISMATCH" },
7284                    (rms / lm_rows as f64).sqrt()
7285                );
7286            }
7287            self.graph_want_logits = want_save;
7288            // restore IN PLACE: the pending verify graph wraps these very
7289            // allocations (zero-copy) — replacing the Vec would strand it
7290            for (l, st) in self.kv_cache.layers.iter_mut().zip(snap) {
7291                if l.linear_state.len() == st.len() {
7292                    l.linear_state.copy_from_slice(&st);
7293                } else {
7294                    l.linear_state = st;
7295                }
7296            }
7297            for (li, (l, n0)) in self.kv_cache.layers.iter_mut().zip(attn_lens).enumerate() {
7298                let extra = l.seq_len.saturating_sub(n0);
7299                if extra > 0 {
7300                    l.truncate_last(extra);
7301                    crate::gpu_metal::kv_mirror_set_stored(self.graph_kv_id, li, n0);
7302                }
7303            }
7304        }
7305        let t_verify = t_round.elapsed();
7306        let sub_verify = subs();
7307        // Acceptance. Greedy: row i's argmax is the trunk's token after
7308        // input i. Sampling: accept draft i with min(1, p_i/q_i), and on
7309        // the first rejection draw the correction from max(0, p_i − q_i)
7310        // — that token is committed by the loop top as-is (spec_forced).
7311        let mut a = 0usize;
7312        let mut forced: Option<u32> = None;
7313        let ids: Vec<u32> = if sparse {
7314            let mut p = std::mem::take(&mut self.spec_ps);
7315            let mut res = std::mem::take(&mut self.spec_ress);
7316            while a < k_spec {
7317                let ok = sampler::sparse_distribution_into(
7318                    &logits[a * lm_rows..(a + 1) * lm_rows],
7319                    &cfg,
7320                    all_ids,
7321                    &mut self.sampler_scratch,
7322                    self.pool.as_deref(),
7323                    &mut p,
7324                );
7325                if !ok {
7326                    let t = sampler::argmax(&logits[a * lm_rows..(a + 1) * lm_rows]);
7327                    p.clear();
7328                    p.push((t, 1.0));
7329                }
7330                match sampler::spec_accept_or_correct_sparse(
7331                    &p,
7332                    &self.spec_qs[a],
7333                    drafts[a],
7334                    &mut self.rng,
7335                    &mut res,
7336                ) {
7337                    None => {
7338                        all_ids.push(drafts[a]);
7339                        a += 1;
7340                    }
7341                    Some(c) => {
7342                        forced = Some(c);
7343                        break;
7344                    }
7345                }
7346            }
7347            all_ids.truncate(base_len);
7348            self.spec_ps = p;
7349            self.spec_ress = res;
7350            drafts.clone()
7351        } else if sampling {
7352            let mut p = std::mem::take(&mut self.spec_p);
7353            let mut res = std::mem::take(&mut self.spec_res);
7354            while a < k_spec {
7355                sampler::distribution_into(
7356                    &logits[a * lm_rows..(a + 1) * lm_rows],
7357                    &cfg,
7358                    all_ids,
7359                    &mut self.sampler_scratch,
7360                    self.pool.as_deref(),
7361                    &mut p,
7362                );
7363                match sampler::spec_accept_or_correct(
7364                    &p,
7365                    &self.spec_q[a],
7366                    drafts[a],
7367                    &mut self.rng,
7368                    &mut res,
7369                    self.pool.as_deref(),
7370                ) {
7371                    None => {
7372                        all_ids.push(drafts[a]);
7373                        a += 1;
7374                    }
7375                    Some(c) => {
7376                        forced = Some(c);
7377                        break;
7378                    }
7379                }
7380            }
7381            all_ids.truncate(base_len);
7382            self.spec_p = p;
7383            self.spec_res = res;
7384            // the accepted drafts ARE the verified tokens after inputs 0..a
7385            drafts.clone()
7386        } else if greedy_pen {
7387            // Row i's penalized argmax, penalties over the stream that
7388            // includes the accepted drafts before it — the plain loop's
7389            // exact arithmetic, one pass per row, no working copy.
7390            let mut ids: Vec<u32> = Vec::with_capacity(b);
7391            for i in 0..b {
7392                let t = sampler::argmax_penalized(
7393                    &logits[i * lm_rows..(i + 1) * lm_rows],
7394                    &cfg,
7395                    all_ids,
7396                    &mut self.sampler_scratch,
7397                    self.pool.as_deref(),
7398                );
7399                ids.push(t);
7400                if i < k_spec && t == drafts[i] {
7401                    all_ids.push(t);
7402                } else {
7403                    break;
7404                }
7405            }
7406            all_ids.truncate(base_len);
7407            while a < k_spec && a < ids.len() && ids[a] == drafts[a] {
7408                a += 1;
7409            }
7410            // rows past the first mismatch were never scored; the loop
7411            // top re-samples the last verified row itself.
7412            ids
7413        } else if greedy_dev && dev_ids.len() == b {
7414            let ids = std::mem::take(&mut dev_ids);
7415            while a < k_spec && ids[a] == drafts[a] {
7416                a += 1;
7417            }
7418            ids
7419        } else {
7420            if logits.len() < b * lm_rows {
7421                // the device argmax was asked for and came back short:
7422                // no rows to fall back on — terminal like a failed batch
7423                self.clear_sequence_state();
7424                self.graph_failed
7425                    .store(true, std::sync::atomic::Ordering::Relaxed);
7426                self.cancel
7427                    .store(true, std::sync::atomic::Ordering::Relaxed);
7428                tracing::error!("Metal verify returned neither logits nor argmax ids");
7429                return None;
7430            }
7431            let ids: Vec<u32> = (0..b)
7432                .map(|i| sampler::argmax(&logits[i * lm_rows..(i + 1) * lm_rows]))
7433                .collect();
7434            while a < k_spec && ids[a] == drafts[a] {
7435                a += 1;
7436            }
7437            ids
7438        };
7439        spec_stamp("acc");
7440        if spec_dbg {
7441            eprintln!(
7442                "spec-dbg round: t_next {t_next} drafts {:?} verified {:?} accepted {a}",
7443                drafts, ids
7444            );
7445        }
7446        // CMF_METAL_VERIFY_CHECK=2: the commit oracle — plain-forward the
7447        // a+1 accepted tokens from a snapshot, then diff the replayed GDN
7448        // states and the appended K/V rows against that.
7449        #[cfg(target_os = "macos")]
7450        let commit_ref: Option<(Vec<Vec<f32>>, Vec<(usize, Vec<f32>, Vec<f32>)>)> = if metal_native
7451            && std::env::var("CMF_METAL_VERIFY_CHECK").as_deref() == Ok("2")
7452        {
7453            let snap: Vec<Vec<f32>> = self
7454                .kv_cache
7455                .layers
7456                .iter()
7457                .map(|l| l.linear_state.clone())
7458                .collect();
7459            let attn_lens: Vec<usize> = self.kv_cache.layers.iter().map(|l| l.seq_len).collect();
7460            let toks: Vec<u32> = std::iter::once(t_next)
7461                .chain(drafts.iter().copied())
7462                .collect();
7463            let want_save = self.graph_want_logits;
7464            self.graph_want_logits = false;
7465            for (i, &t) in toks.iter().take(a + 1).enumerate() {
7466                let _ = self.forward_layers(&self.embed_single(t), next_pos + i, None);
7467                let _ = self.graph_logits.take();
7468            }
7469            self.graph_want_logits = want_save;
7470            let plain_states: Vec<Vec<f32>> = self
7471                .kv_cache
7472                .layers
7473                .iter()
7474                .map(|l| l.linear_state.clone())
7475                .collect();
7476            let (nkv, hd) = (self.num_kv_heads, self.head_dim);
7477            let mut rows = Vec::new();
7478            for (li, (l, n0)) in self
7479                .kv_cache
7480                .layers
7481                .iter_mut()
7482                .zip(attn_lens.iter())
7483                .enumerate()
7484            {
7485                let extra = l.seq_len.saturating_sub(*n0);
7486                if extra > 0 {
7487                    let mut kk = Vec::new();
7488                    let mut vv = Vec::new();
7489                    for g in 0..nkv {
7490                        kk.extend_from_slice(&l.head_keys(g)[n0 * hd..]);
7491                        vv.extend_from_slice(&l.head_values(g)[n0 * hd..]);
7492                    }
7493                    rows.push((li, kk, vv));
7494                    l.truncate_last(extra);
7495                    crate::gpu_metal::kv_mirror_set_stored(self.graph_kv_id, li, *n0);
7496                }
7497            }
7498            for (l, st) in self.kv_cache.layers.iter_mut().zip(snap) {
7499                if l.linear_state.len() == st.len() {
7500                    l.linear_state.copy_from_slice(&st);
7501                } else {
7502                    l.linear_state = st;
7503                }
7504            }
7505            Some((plain_states, rows))
7506        } else {
7507            None
7508        };
7509        let warm_off = std::env::var("CMF_SPEC_WARM").is_ok_and(|v| v == "0");
7510        // Metal: the MTP cache cut and the round's warm-up SUBMIT come
7511        // BEFORE the trunk commit, so the warm-up's command buffer is
7512        // queued ahead of the GDN replay (second queue) and its wait
7513        // below no longer sits behind the replay — measured: the warm-up's
7514        // wait grew with the accepted count exactly like the replay does
7515        // (8 ms at a=1, 17 ms at a=3, 25 ms at a=5 for ~2 ms of its own
7516        // work). The replay now overlaps the warm-up's readback, the
7517        // round's return and the next draft chain.
7518        #[cfg(target_os = "macos")]
7519        let mut warm_pending: Option<MetalWarmPending> = None;
7520        #[cfg(target_os = "macos")]
7521        if metal_native {
7522            m.kv.truncate_last(k_spec.saturating_sub(1));
7523            if self.mtp_graph_mode == Some(true) {
7524                // the mirror rows below the cut are the CPU rows: re-point,
7525                // no re-upload
7526                crate::gpu_metal::kv_mirror_set_stored(
7527                    self.mtp_kv_id(),
7528                    Self::MTP_LAYER_BASE,
7529                    m.kv.seq_len,
7530                );
7531                if !warm_off && a > 0 {
7532                    let pairs: Vec<(&[f32], u32)> = (0..a)
7533                        .map(|j| {
7534                            (
7535                                &hiddens[j * self.hidden_size..(j + 1) * self.hidden_size],
7536                                ids[j],
7537                            )
7538                        })
7539                        .collect();
7540                    warm_pending = self.mtp_warm_batch_submit(m, &pairs, next_pos);
7541                }
7542            }
7543            spec_stamp("c.wsub");
7544        }
7545        // a fully-accepted round needs no restore: every input was real.
7546        #[cfg(target_os = "macos")]
7547        if metal_native {
7548            // the Metal verify never wrote its states: the commit replays the
7549            // accepted prefix into the CPU owners and appends the K/V rows
7550            if !self.metal_verify_commit(a) {
7551                self.clear_sequence_state();
7552                self.graph_failed
7553                    .store(true, std::sync::atomic::Ordering::Relaxed);
7554                self.cancel
7555                    .store(true, std::sync::atomic::Ordering::Relaxed);
7556                tracing::error!("Metal verify state/KV handoff failed after admission");
7557                return None;
7558            }
7559            if let Some((plain_states, rows)) = commit_ref {
7560                crate::gpu_metal::queue_fence();
7561                // the commit's replay runs on the second queue: collect it
7562                // before the oracle reads the CPU owners it writes into
7563                let _ = crate::gpu_metal::wait_replay();
7564                let (nkv, hd) = (self.num_kv_heads, self.head_dim);
7565                let mut worst_s = 0f32;
7566                let mut worst_li = 0usize;
7567                for (li, (l, ps)) in self.kv_cache.layers.iter().zip(&plain_states).enumerate() {
7568                    if l.linear_state.len() != ps.len() || ps.is_empty() {
7569                        continue;
7570                    }
7571                    let d = l
7572                        .linear_state
7573                        .iter()
7574                        .zip(ps)
7575                        .fold(0f32, |m, (x, y)| m.max((x - y).abs()));
7576                    let n = ps.iter().fold(0f32, |m, y| m.max(y.abs()));
7577                    let rel = d / n.max(1e-6);
7578                    if rel > worst_s {
7579                        worst_s = rel;
7580                        worst_li = li;
7581                    }
7582                }
7583                let mut worst_k = 0f32;
7584                for (li, kk, vv) in &rows {
7585                    let l = &self.kv_cache.layers[*li];
7586                    let n0 = l.seq_len - (kk.len() / (nkv * hd));
7587                    let mut ck = Vec::new();
7588                    let mut cv = Vec::new();
7589                    for g in 0..nkv {
7590                        ck.extend_from_slice(&l.head_keys(g)[n0 * hd..]);
7591                        cv.extend_from_slice(&l.head_values(g)[n0 * hd..]);
7592                    }
7593                    if ck.len() == kk.len() {
7594                        let dk = ck
7595                            .iter()
7596                            .zip(kk)
7597                            .fold(0f32, |m, (x, y)| m.max((x - y).abs()));
7598                        let dv = cv
7599                            .iter()
7600                            .zip(vv)
7601                            .fold(0f32, |m, (x, y)| m.max((x - y).abs()));
7602                        worst_k = worst_k.max(dk).max(dv);
7603                    } else {
7604                        eprintln!(
7605                            "commit-check L{li}: kv row count mismatch {} vs {}",
7606                            ck.len(),
7607                            kk.len()
7608                        );
7609                    }
7610                }
7611                eprintln!(
7612                    "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}"
7613                );
7614            }
7615        }
7616        if !metal_native && a + 1 < b {
7617            let expected_gdn_layers = self.graph_gdn_layer_count();
7618            if expected_gdn_layers > 0
7619                && !crate::gpu::gdn_spec_restore(self.graph_kv_id, a, next_pos, expected_gdn_layers)
7620            {
7621                self.clear_sequence_state();
7622                self.graph_failed
7623                    .store(true, std::sync::atomic::Ordering::Relaxed);
7624                self.cancel
7625                    .store(true, std::sync::atomic::Ordering::Relaxed);
7626                tracing::error!("GDN speculative restore failed after verify");
7627                return None;
7628            }
7629        }
7630        if !metal_native && !self.rewind_trunk_graph_mirrors(next_pos + a + 1) {
7631            // The verify graph committed the full batch, but one of its
7632            // persistent Full-attention mirrors could not be re-pointed to
7633            // the accepted prefix.  Treat that as terminal state failure;
7634            // an exact CPU fallback would otherwise consume stale GDN/KV.
7635            self.clear_sequence_state();
7636            self.graph_failed
7637                .store(true, std::sync::atomic::Ordering::Relaxed);
7638            self.cancel
7639                .store(true, std::sync::atomic::Ordering::Relaxed);
7640            tracing::error!("trunk graph KV rewind failed after speculative verify");
7641            return None;
7642        }
7643        *accepted += a;
7644        // MTP cache: keep the first draft row (its inputs were real), drop
7645        // the chain's, then append the verified pairs the round produced.
7646        // Each of those is a whole MTP block on the per-op path and they
7647        // cost 5.8 ms of a 69 ms round at k=3 — a third of what the
7648        // round's own draft costs. PRICED, and they earn it: skipping
7649        // them (`CMF_SPEC_WARM=0`) drops acceptance from 89% to 81% at
7650        // k=3 and 85% to 74% at k=4, and the tok/s goes nowhere at k=3
7651        // (50.3 against 50.5) and backwards at k=4 (48.1 against 50.1).
7652        // The knob stays so the next person can re-price it after the
7653        // warms are batched instead of assuming either way.
7654        if !metal_native {
7655            // (Metal cut its MTP cache before the trunk commit, above)
7656            m.kv.truncate_last(k_spec.saturating_sub(1));
7657        }
7658        spec_stamp("c.trunc");
7659        if !metal_native
7660            && self.mtp_graph_mode == Some(true)
7661            && !self.rewind_mtp_graph_mirror(next_pos)
7662        {
7663            // The graph draft was admitted, so inability to move its cursor
7664            // back to the real anchor is a state failure, not a capability
7665            // refusal.  Do not warm or continue with a stale mirror.
7666            self.clear_sequence_state();
7667            self.graph_failed
7668                .store(true, std::sync::atomic::Ordering::Relaxed);
7669            self.cancel
7670                .store(true, std::sync::atomic::Ordering::Relaxed);
7671            tracing::error!("MTP graph mirror rewind failed after verify commit");
7672            return None;
7673        }
7674        if !warm_off && a > 0 {
7675            // Graph arm: all accepted pairs in ONE batched run over the
7676            // MTP block; the token graph one by one if the batch declines.
7677            let mut warmed = false;
7678            #[cfg(target_os = "macos")]
7679            if metal_native && self.mtp_graph_mode == Some(true) {
7680                // the batched warm-up was submitted before the trunk
7681                // commit: collect it here; one by one on the token graph
7682                // if it declined (or failed)
7683                warmed = match warm_pending.take() {
7684                    Some(p) => self.mtp_warm_batch_finish(m, p),
7685                    None => false,
7686                };
7687                if !warmed {
7688                    warmed = true;
7689                    for j in 0..a {
7690                        let row =
7691                            hiddens[j * self.hidden_size..(j + 1) * self.hidden_size].to_vec();
7692                        if self
7693                            .mtp_step_metal(m, &row, ids[j], next_pos + j, false)
7694                            .is_none()
7695                        {
7696                            warmed = false;
7697                            break;
7698                        }
7699                    }
7700                }
7701            }
7702            if !warmed && self.mtp_graph_mode != Some(false) && !metal_native {
7703                let rows: Vec<Vec<f32>> = (0..a)
7704                    .map(|j| hiddens[j * self.hidden_size..(j + 1) * self.hidden_size].to_vec())
7705                    .collect();
7706                let pairs: Vec<(&[f32], u32)> = rows
7707                    .iter()
7708                    .zip(ids.iter())
7709                    .map(|(r, &t)| (r.as_slice(), t))
7710                    .collect();
7711                match self.mtp_warm_prefill_pairs(m, &pairs, next_pos) {
7712                    Ok(()) => warmed = true,
7713                    Err(err) => {
7714                        // A warm-up failure after graph admission cannot
7715                        // fall back to `mtp_warm`: the detached CPU cache is
7716                        // not authoritative for the device mirror.  Mark it
7717                        // terminal so the generation caller clears state and
7718                        // returns instead of drafting from stale attention.
7719                        tracing::error!("{err}");
7720                        self.clear_sequence_state();
7721                        self.graph_failed
7722                            .store(true, std::sync::atomic::Ordering::Relaxed);
7723                        self.cancel
7724                            .store(true, std::sync::atomic::Ordering::Relaxed);
7725                        return None;
7726                    }
7727                }
7728            }
7729            if !warmed {
7730                for j in 0..a {
7731                    let row = &hiddens[j * self.hidden_size..(j + 1) * self.hidden_size];
7732                    let row = row.to_vec();
7733                    self.mtp_warm(m, &row, ids[j], next_pos + j);
7734                }
7735            }
7736        }
7737        // The sampler's contract: logits of the LAST verified position —
7738        // unless a rejected draft already drew the correction, in which
7739        // case the loop top commits that token and samples nothing.
7740        spec_stamp("c.warm");
7741        if let Some(c) = forced {
7742            self.spec_forced = Some(c);
7743            self.graph_logits = None;
7744        } else if greedy_dev && logits.is_empty() {
7745            // the row's argmax IS the token the loop top would pick from
7746            // it (plain greedy, no penalties): commit it as forced
7747            self.spec_forced = Some(ids[a]);
7748            self.graph_logits = None;
7749        } else {
7750            let mut row = logits[a * lm_rows..(a + 1) * lm_rows].to_vec();
7751            row.resize(self.vocab_size, 0.0);
7752            if let Some(c) = self.final_softcap {
7753                for l in row.iter_mut() {
7754                    *l = c * (*l / c).tanh();
7755                }
7756            }
7757            self.graph_logits = Some(row);
7758        }
7759        let new_hidden = hiddens[a * self.hidden_size..(a + 1) * self.hidden_size].to_vec();
7760        spec_stamp("c.row");
7761        // Three phases, not two. The round's wall clock was 4 ms longer
7762        // than draft+verify and the difference had nowhere to be seen:
7763        // the accepted prefix re-runs the MTP block once per token to
7764        // keep the draft head's attention cache warm, and the GDN state
7765        // rolls back on any rejection. Both live here, after the verify.
7766        if std::env::var("CMF_GRAPH_SPEC_TIME").is_ok() {
7767            let end = subs();
7768            eprintln!(
7769                "spec-round: draft {:.1} ms/{} sub | verify {:.1} ms/{} sub | \
7770                 commit {:.1} ms/{} sub (accepted {a} of {k_spec}, full-head streak {})",
7771                t_draft.as_secs_f64() * 1e3,
7772                sub_draft - sub0,
7773                (t_verify - t_draft).as_secs_f64() * 1e3,
7774                sub_verify - sub_draft,
7775                (t_round.elapsed() - t_verify).as_secs_f64() * 1e3,
7776                end - sub_verify,
7777                self.draft_full_streak,
7778            );
7779        }
7780        // Native Metal's verify tile is flat in b (eight rows for the price
7781        // of one), so a shorter round only forfeits tokens — measured on
7782        // the M4: an essay round at k=2 still verified in 260 ms. The
7783        // adaptation is for cards whose verify grows with the rows.
7784        if k_env.is_none() && !metal_native && !k_capped {
7785            // Slow average and a wide band: a fast one oscillated 2↔3 on
7786            // an essay every other round (measured), which forfeits the
7787            // draft it just paid for.
7788            let f = a as f32 / k_spec.max(1) as f32;
7789            self.spec_acc_ewma += 0.2 * (f - self.spec_acc_ewma);
7790            let mut k_next = k_spec;
7791            if self.spec_acc_ewma >= 0.75 && k_spec < k_max {
7792                k_next = k_spec + 1;
7793            } else if self.spec_acc_ewma < 0.4 && k_spec > 2 {
7794                k_next = k_spec - 1;
7795            }
7796            if k_next != k_spec {
7797                self.spec_acc_ewma = 0.6;
7798                if std::env::var("CMF_GRAPH_SPEC_TIME").is_ok() {
7799                    eprintln!("spec-k: {k_spec} → {k_next}");
7800                }
7801            }
7802            self.spec_k_adapt = Some(k_next);
7803        }
7804        spec_stamp("end");
7805        Some((drafts[..a].to_vec(), next_pos + a + 1, new_hidden))
7806    }
7807
7808    /// Micro-benchmark: two single-position forwards vs one fused pair
7809    /// from the current cache state (KV rewound after each probe).
7810    /// Returns (two_singles_ms, fused_pair_ms) per probe, or the (0, 0)
7811    /// sentinel when this model has no pair path to measure — the same
7812    /// answer the o1 arm gives, and the bench prints it the same way.
7813    /// (An architecture that loads its own layers leaves `weights.layers`
7814    /// empty; walking it here was an index panic, found by `bench` on
7815    /// deepseek_v4.)
7816    pub fn measure_pair_fusion(&mut self, iters: usize) -> (f64, f64) {
7817        if !self.pair_supported() {
7818            return (0.0, 0.0);
7819        }
7820        // This is a host-side pair micro-benchmark. It truncates the host KV
7821        // after every probe, so letting the whole-token graph participate
7822        // would leave its device GDN/KV mirror ahead of the next probe and
7823        // poison the process-wide graph verdict before the real generation
7824        // benchmark starts. Keep the existing per-op/GPU arithmetic while
7825        // suppressing only the stateful token graph for this measurement.
7826        let graph_env = std::env::var_os("CMF_GPU_WGPU_GRAPH");
7827        unsafe { std::env::set_var("CMF_GPU_WGPU_GRAPH", "0") };
7828        let emb1 = self.embed_single(1);
7829        let emb2 = self.embed_single(2);
7830        let pos = self.kv_cache.seq_len();
7831
7832        let t0 = std::time::Instant::now();
7833        for _ in 0..iters {
7834            let _ = self.forward_layers(&emb1, pos, None);
7835            let _ = self.forward_layers(&emb2, pos + 1, None);
7836            for l in &mut self.kv_cache.layers {
7837                l.truncate_last(2);
7838            }
7839        }
7840        let singles_ms = t0.elapsed().as_secs_f64() * 1000.0 / iters as f64;
7841
7842        let t1 = std::time::Instant::now();
7843        for _ in 0..iters {
7844            let _ = self.forward_pair(&emb1, &emb2, pos);
7845            for l in &mut self.kv_cache.layers {
7846                l.truncate_last(2);
7847            }
7848        }
7849        let pair_ms = t1.elapsed().as_secs_f64() * 1000.0 / iters as f64;
7850        match graph_env {
7851            Some(value) => unsafe { std::env::set_var("CMF_GPU_WGPU_GRAPH", value) },
7852            None => unsafe { std::env::remove_var("CMF_GPU_WGPU_GRAPH") },
7853        }
7854        (singles_ms, pair_ms)
7855    }
7856
7857    /// Fused two-position forward: weight rows are streamed from memory
7858    /// once per layer for both positions. Full layers → fused GQA pair;
7859    /// linear layers → vmf_phase pair (lane 2 state is tentative in the
7860    /// per-layer scratch until the draft is accepted).
7861    /// Whether the fused two-position path covers every layer kind in
7862    /// this model. MLA and KDA run per position (their pair arms are
7863    /// unreachable); the seq prefill falls back to singles for them.
7864    fn pair_supported(&self) -> bool {
7865        // An EMPTY layer stack means the architecture loaded its own and
7866        // this path has nothing to walk. Checking that directly, rather
7867        // than naming each such architecture, is what makes the guard hold
7868        // for the next one: `any()` over no layers is false, so a
7869        // feature-by-feature test says "supported" for a model that has no
7870        // layers here at all.
7871        !self.weights.layers.is_empty()
7872            && self.g3n.is_none()
7873            && !self
7874                .weights
7875                .layers
7876                .iter()
7877                .any(|lw| matches!(&lw.attn, AttnKind::Mla(_) | AttnKind::Kda(_)))
7878    }
7879
7880    fn forward_pair(
7881        &mut self,
7882        emb1: &[f32],
7883        emb2: &[f32],
7884        position: usize,
7885    ) -> (Vec<f32>, Vec<f32>) {
7886        // A two-token prompt starts here, not in the layer walk: decide the
7887        // MiMo placement before the pair's per-op MoE uploads any expert.
7888        self.mimo_moe_prepare();
7889        let mut h1 = emb1.to_vec();
7890        let mut h2 = emb2.to_vec();
7891        let (_nkv, _hd, hs, _rd, eps) = (
7892            self.num_kv_heads,
7893            self.head_dim,
7894            self.hidden_size,
7895            self.rotary_dim,
7896            self.rms_eps,
7897        );
7898        let pool = self.pool.clone();
7899
7900        for li in 0..self.num_layers {
7901            let lw = &self.weights.layers[self.phys_layer(li)];
7902            // Norms into pipeline scratch (4 allocs/layer on the MTP
7903            // decode hot path before this).
7904            inference::rms_norm_into(
7905                &h1,
7906                &lw.input_norm,
7907                self.rms_eps,
7908                self.norm_style,
7909                &mut self.ws.n1,
7910            );
7911            inference::rms_norm_into(
7912                &h2,
7913                &lw.input_norm,
7914                self.rms_eps,
7915                self.norm_style,
7916                &mut self.ws.n2,
7917            );
7918
7919            let (a1, a2) = match &lw.attn {
7920                AttnKind::Mla(_) => unreachable!("MLA has no MTP/pair path"),
7921                AttnKind::Kda(_) => unreachable!("KDA has no MTP/pair path"),
7922                AttnKind::Bounded(w) => {
7923                    // Two sequential positions of the bounded operator
7924                    // (the ring's causal order is the pair's order).
7925                    let rope = self
7926                        .bounded_rope
7927                        .clone()
7928                        .expect("bounded layer without an installed rotation table");
7929                    let cfg = crate::bounded::BoundedAttnCfg {
7930                        num_heads: self.num_heads,
7931                        num_kv_heads: self.num_kv_heads,
7932                        head_dim: self.head_dim,
7933                        hidden_size: hs,
7934                        scale: self.attn_scale,
7935                        rope: &rope,
7936                        pool: pool.as_deref(),
7937                    };
7938                    let a1 = crate::bounded::bounded_attention(
7939                        &self.ws.n1,
7940                        w,
7941                        &mut self.kv_cache.layers[li],
7942                        &cfg,
7943                    );
7944                    let a2 = crate::bounded::bounded_attention(
7945                        &self.ws.n2,
7946                        w,
7947                        &mut self.kv_cache.layers[li],
7948                        &cfg,
7949                    );
7950                    (a1, a2)
7951                }
7952                AttnKind::Linear(w) => {
7953                    let cfg = self.vmf_cfg.expect("linear layer without vmf_cfg");
7954                    let layer = &mut self.kv_cache.layers[li];
7955                    let (state, scratch) = (&mut layer.linear_state, &mut layer.linear_scratch);
7956                    vmf_phase_pair(
7957                        &self.ws.n1,
7958                        &self.ws.n2,
7959                        w,
7960                        &cfg,
7961                        state,
7962                        scratch,
7963                        self.pool.as_deref(),
7964                    )
7965                }
7966                AttnKind::LinearGdn(w) => {
7967                    let cfg = self.gdn_cfg.expect("gdn layer without gdn_cfg");
7968                    let layer = &mut self.kv_cache.layers[li];
7969                    let (state, scratch) = (&mut layer.linear_state, &mut layer.linear_scratch);
7970                    gdn_pair(
7971                        &self.ws.n1,
7972                        &self.ws.n2,
7973                        w,
7974                        &cfg,
7975                        state,
7976                        scratch,
7977                        self.pool.as_deref(),
7978                    )
7979                }
7980                AttnKind::ShortConv(w) => {
7981                    let cfg = self
7982                        .short_conv_cfg
7983                        .expect("short-conv layer without short_conv_cfg");
7984                    let layer = &mut self.kv_cache.layers[li];
7985                    let (state, scratch) = (&mut layer.linear_state, &mut layer.linear_scratch);
7986                    short_conv_pair(
7987                        &self.ws.n1,
7988                        &self.ws.n2,
7989                        w,
7990                        &cfg,
7991                        state,
7992                        scratch,
7993                        self.pool.as_deref(),
7994                    )
7995                }
7996                AttnKind::Full {
7997                    wq,
7998                    wk,
7999                    wv,
8000                    wo,
8001                    q_norm,
8002                    k_norm,
8003                    output_gate,
8004                    softplus_gate,
8005                    bias,
8006                } => {
8007                    let inv_freq_l = self.layer_inv_freq(li);
8008                    let (nkv_l, hd_l, rd_l) = self.layer_geom(li);
8009                    let cfg = QwenAttnCfg {
8010                        num_heads: self.layer_num_heads(li),
8011                        num_kv_heads: nkv_l,
8012                        head_dim: hd_l,
8013                        hidden_size: hs,
8014                        position,
8015                        inv_freq: &inv_freq_l,
8016                        rotary_dim: rd_l,
8017                        scale: self.attn_scale,
8018                        softcap: self.attn_softcap,
8019                        window: self.layer_window(li),
8020                        v_norm: self.attn_v_norm,
8021                        qk_norm_after_rope: self.qk_norm_after_rope,
8022                        gate_sigmoid: self.proj_gate_sigmoid,
8023                        q_norm: q_norm.as_deref(),
8024                        k_norm: k_norm.as_deref(),
8025                        output_gate: *output_gate,
8026                        softplus_gate: softplus_gate
8027                            .as_ref()
8028                            .map(|(gate, per_head)| (gate, *per_head)),
8029                        rope_scale: self.layer_rope_scale(li),
8030                        bias: bias
8031                            .as_ref()
8032                            .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
8033                        rms_eps: eps,
8034                        norm_style: self.norm_style,
8035                        pool: pool.as_deref(),
8036                        v_head_dim: self.layer_v_dim(li),
8037                    };
8038                    attention::qwen_attention_pair(
8039                        &self.ws.n1,
8040                        &self.ws.n2,
8041                        wq,
8042                        wk,
8043                        wv,
8044                        wo,
8045                        &mut self.kv_cache.layers[li],
8046                        &cfg,
8047                    )
8048                }
8049            };
8050            let (a1, a2) = match &self.weights.layers[self.phys_layer(li)].attn_out_norm {
8051                Some(w) => (
8052                    inference::rms_norm(&a1, w, self.rms_eps, self.norm_style),
8053                    inference::rms_norm(&a2, w, self.rms_eps, self.norm_style),
8054                ),
8055                None => (a1, a2),
8056            };
8057            for i in 0..self.hidden_size {
8058                h1[i] += a1[i];
8059                h2[i] += a2[i];
8060            }
8061            let (mut a1, mut a2) = (a1, a2);
8062            attention::recycle_buf(&mut a1);
8063            attention::recycle_buf(&mut a2);
8064
8065            let lw = &self.weights.layers[self.phys_layer(li)];
8066            inference::rms_norm_into(
8067                &h1,
8068                &lw.post_norm,
8069                self.rms_eps,
8070                self.norm_style,
8071                &mut self.ws.p1,
8072            );
8073            inference::rms_norm_into(
8074                &h2,
8075                &lw.post_norm,
8076                self.rms_eps,
8077                self.norm_style,
8078                &mut self.ws.p2,
8079            );
8080            let (f1, f2) = match &lw.ffn {
8081                // Dual-branch layers need the raw residuals — run the
8082                // two positions through the same fn decode uses.
8083                FfnKind::DenseMoe(dm) => (
8084                    dense_moe_ffn(
8085                        dm,
8086                        &self.ws.p1,
8087                        &h1,
8088                        self.rms_eps,
8089                        self.norm_style,
8090                        self.pool.as_deref(),
8091                    ),
8092                    dense_moe_ffn(
8093                        dm,
8094                        &self.ws.p2,
8095                        &h2,
8096                        self.rms_eps,
8097                        self.norm_style,
8098                        self.pool.as_deref(),
8099                    ),
8100                ),
8101                FfnKind::Moe(m) if self.mimo_moe.is_dynamic(li, false) => (
8102                    moe_ffn_banked(&mut self.mimo_moe, li, m, &self.ws.p1, self.pool.as_deref()),
8103                    moe_ffn_banked(&mut self.mimo_moe, li, m, &self.ws.p2, self.pool.as_deref()),
8104                ),
8105                _ => ffn_forward_pair(
8106                    &lw.ffn,
8107                    &self.ws.p1,
8108                    &self.ws.p2,
8109                    self.pool.as_deref(),
8110                    None,
8111                ),
8112            };
8113            let (f1, f2) = match &self.weights.layers[self.phys_layer(li)].ffn_out_norm {
8114                Some(w) => (
8115                    inference::rms_norm(&f1, w, self.rms_eps, self.norm_style),
8116                    inference::rms_norm(&f2, w, self.rms_eps, self.norm_style),
8117                ),
8118                None => (f1, f2),
8119            };
8120            for i in 0..self.hidden_size {
8121                h1[i] += f1[i];
8122                h2[i] += f2[i];
8123            }
8124            let (mut f1, mut f2) = (f1, f2);
8125            attention::recycle_buf(&mut f1);
8126            attention::recycle_buf(&mut f2);
8127            if let Some(sc) = self.weights.layers[self.phys_layer(li)].layer_scale {
8128                for i in 0..self.hidden_size {
8129                    h1[i] *= sc;
8130                    h2[i] *= sc;
8131                }
8132            }
8133            // Looped Transformer: apply final norm at the end of each loop iteration.
8134            if self.is_loop_end(li) && li + 1 < self.num_layers {
8135                h1 = inference::rms_norm(
8136                    &h1,
8137                    &self.weights.final_norm,
8138                    self.rms_eps,
8139                    self.norm_style,
8140                );
8141                h2 = inference::rms_norm(
8142                    &h2,
8143                    &self.weights.final_norm,
8144                    self.rms_eps,
8145                    self.norm_style,
8146                );
8147            }
8148        }
8149        // Real O(1) prefill pairs may also carry tentative lane-2 recurrent
8150        // state. Commit it before publishing the transition epoch so the
8151        // next serial/device row cannot observe a new attention epoch with an
8152        // old GDN state. Speculative pairs run only when O(1) is inactive and
8153        // retain their existing caller-controlled commit/rollback semantics.
8154        if self.o1_active() {
8155            self.commit_linear_scratch();
8156        }
8157        self.o1_progress();
8158        self.swa_trim_tails();
8159        (h1, h2)
8160    }
8161
8162    /// Commit lane-2 linear states after an accepted draft.
8163    fn commit_linear_scratch(&mut self) {
8164        for layer in &mut self.kv_cache.layers {
8165            if !layer.linear_scratch.is_empty() {
8166                std::mem::swap(&mut layer.linear_state, &mut layer.linear_scratch);
8167                layer.linear_scratch.clear();
8168            }
8169        }
8170    }
8171
8172    /// Forward a full id sequence from a fresh cache and return the
8173    /// logits after the last position (golden-parity harness, bench).
8174    pub fn forward_ids(
8175        &mut self,
8176        ids: &[u32],
8177        task_mask: Option<&TaskMask>,
8178    ) -> Result<Vec<f32>, String> {
8179        #[cfg(target_os = "macos")]
8180        crate::gpu_metal::set_io_namespace(self.graph_kv_id);
8181        if ids.is_empty() {
8182            return Err("empty id sequence".to_string());
8183        }
8184        self.clear_sequence_state();
8185        self.check_forward_graph("forward_ids setup", 0)?;
8186        if task_mask.is_none() {
8187            self.o1_begin();
8188        }
8189        let mut hidden = vec![0.0f32; self.hidden_size];
8190        let mut pos = 0usize;
8191        if let Some(b) = &mut self.dsv41 {
8192            let pool = self.pool.clone();
8193            let mut logits = Vec::new();
8194            crate::dsv41::forward_chunk(
8195                &b.0,
8196                &b.1,
8197                &b.2,
8198                &mut b.3,
8199                ids,
8200                0,
8201                pool.as_deref(),
8202                &mut logits,
8203            );
8204            if let Err(err) = self.o1_seal_checked() {
8205                self.clear_sequence_state();
8206                return Err(err);
8207            }
8208            return Ok(logits);
8209        }
8210        // Same routing predicate generation uses. Two reasons it must be
8211        // the same one: (1) a GDN hybrid's recurrent state is GPU-
8212        // resident, and a batched CPU prefill would build it on the host
8213        // only — decode then reads buffers the prefill never wrote;
8214        // (2) bench times THIS function and calls the result "prefill",
8215        // so a different path here reports a number production never
8216        // sees (W2 on 2×5090: 8.7 tok/s reported against 125 real).
8217        // Gemma-4: generation's batched-graph ingest (`g4_batch_ingest`),
8218        // so bench and the parity tools time and score the route run/serve
8219        // take; a refusal leaves the rest to the chunked host prefill.
8220        #[cfg(not(target_os = "macos"))]
8221        if task_mask.is_none() && ids.len() > 2 && self.g4_batch_prefill() {
8222            let (n, last) = match self.g4_batch_ingest(ids, 0, false) {
8223                Ok(r) => r,
8224                Err(e) => {
8225                    self.clear_sequence_state();
8226                    return Err(e);
8227                }
8228            };
8229            if n > 0 {
8230                hidden.copy_from_slice(&last);
8231                pos = n;
8232            }
8233        }
8234        if self.can_prefill_batched() && !self.graph_prefill_preferred() && ids.len() > 2 {
8235            // prefill-GEMM in chunks; only the last position's hidden is
8236            // needed. (o1-compatible: the batch path attends per position
8237            // through qwen_attention, which carries the collection hook.)
8238            let chunk = self.prefill_chunk();
8239            let hs = self.hidden_size;
8240            while pos < ids.len() {
8241                let end = (pos + chunk).min(ids.len());
8242                let hb = self.prefill_rows(&ids[pos..end], pos, task_mask)?;
8243                self.check_forward_graph("forward_ids batched prefill", end - 1)?;
8244                hidden.copy_from_slice(&hb[(end - pos - 1) * hs..]);
8245                pos = end;
8246            }
8247        }
8248        // Same guards as generation's prefill — INCLUDING the graph one.
8249        // The CPU pair walk was intercepting positions that the resident
8250        // token graph would have run itself: on a GDN hybrid over wgpu
8251        // that is 89 ms of host forward against 7 ms of device submit,
8252        // and it made prefill look 12× slower than it is (W2 on an RTX
8253        // 5090, ctx 512: 11.2 tok/s with the walk, 136.6 without).
8254        // CMF_PAIR=0 opts out; a model whose layers live outside
8255        // `weights.layers` has no pair walk to take.
8256        if task_mask.is_none()
8257            && !self.graph_prefill_preferred()
8258            && !std::env::var("CMF_PAIR").is_ok_and(|v| v == "0")
8259            && self.pair_supported()
8260        {
8261            while pos + 1 < ids.len() {
8262                let e1 = self.embed_single(ids[pos]);
8263                let e2 = self.embed_single(ids[pos + 1]);
8264                let (_, h2) = self.forward_pair(&e1, &e2, pos);
8265                self.check_forward_graph("forward_ids pair", pos + 1)?;
8266                self.commit_linear_scratch();
8267                hidden = h2;
8268                pos += 2;
8269            }
8270        }
8271        // Resident Embryo graph: the prompt in chunks of one submit each
8272        // (the same device state and logits as the per-position walk).
8273        if task_mask.is_none() && pos == 0 && ids.len() > 1 {
8274            if let Some(lg) = self.embryo_prefill_chunked(ids, 0) {
8275                self.graph_logits = Some(lg);
8276                hidden = vec![0.0; self.hidden_size];
8277                pos = ids.len();
8278            }
8279        }
8280        while pos < ids.len() {
8281            hidden = self.forward_layers(&self.embed_single(ids[pos]), pos, task_mask);
8282            self.check_forward_graph("forward_ids", pos)?;
8283            pos += 1;
8284        }
8285        if let Some(logits) = self.graph_logits.take() {
8286            // Resident stacks already applied final norm and their head in
8287            // the same submit; do not run a second norm/head over the zero
8288            // hidden sentinel returned by forward_layers_span.
8289            if let Err(err) = self.o1_seal_checked() {
8290                self.clear_sequence_state();
8291                return Err(err);
8292            }
8293            return Ok(logits);
8294        }
8295        // Harness contract: after forward_ids the cache is decode-ready —
8296        // under o1 that means sealed (bench measures the seal as part of
8297        // prefill, honestly).
8298        if let Err(err) = self.o1_seal_checked() {
8299            self.clear_sequence_state();
8300            return Err(err);
8301        }
8302        let normed = inference::rms_norm(
8303            &hidden,
8304            &self.weights.final_norm,
8305            self.rms_eps,
8306            self.norm_style,
8307        );
8308        Ok(self.lm_head_forward(&normed))
8309    }
8310
8311    /// Run the V4.1 stack one token at a time and retain logits for every
8312    /// position. This is a diagnostic surface for comparing a converted
8313    /// checkpoint with a tokenwise reference implementation.
8314    #[doc(hidden)]
8315    pub fn dsv41_serial_logits(&mut self, ids: &[u32]) -> Result<Vec<Vec<f32>>, String> {
8316        #[cfg(target_os = "macos")]
8317        crate::gpu_metal::set_io_namespace(self.graph_kv_id);
8318        if ids.is_empty() {
8319            return Err("empty id sequence".to_string());
8320        }
8321        self.clear_sequence_state();
8322        self.dsv41
8323            .as_ref()
8324            .ok_or_else(|| "dsv41 serial logits require a DeepSeek-V4.1 model".to_string())?;
8325        self.o1_begin();
8326        let rows = {
8327            let pool = self.pool.clone();
8328            let b = self
8329                .dsv41
8330                .as_mut()
8331                .expect("dsv41 checked above; state cannot change during forward");
8332            let mut rows = Vec::with_capacity(ids.len());
8333            for (position, &id) in ids.iter().enumerate() {
8334                let mut logits = Vec::new();
8335                crate::dsv41::forward_token(
8336                    &b.0,
8337                    &b.1,
8338                    &b.2,
8339                    &mut b.3,
8340                    id,
8341                    position,
8342                    pool.as_deref(),
8343                    &mut logits,
8344                );
8345                rows.push(logits);
8346            }
8347            rows
8348        };
8349        self.o1_seal();
8350        Ok(rows)
8351    }
8352
8353    /// Teacher-forced perplexity over a token sequence (phase-C gate:
8354    /// honest quant comparisons instead of prompt vibes).
8355    ///
8356    /// Attention is EXACT even on a model whose layers are flagged for
8357    /// the O(1) kernel — scoring the backbone is the default on purpose
8358    /// (it is the yardstick). `nll_ids_o1` scores the CONVERTED model.
8359    pub fn ppl_ids(&mut self, ids: &[u32]) -> Result<f64, String> {
8360        let (nll, cnt) = self.nll_ids_from(ids, 0)?;
8361        Ok((nll / cnt.max(1) as f64).exp())
8362    }
8363
8364    /// DTG-MA calibration pass (Patent 2): run `ids` through the model
8365    /// (CPU path, per position) and return each layer's per-neuron
8366    /// activation mass Σ|silu(gate)·up| — the statistic the task-guided
8367    /// FFN mask is derived from.
8368    pub fn probe_ffn_mass(&mut self, ids: &[u32]) -> Vec<Vec<f64>> {
8369        self.clear_sequence_state();
8370        FFN_PROBE.with(|p| {
8371            *p.borrow_mut() = Some(vec![vec![0f64; self.intermediate_size]; self.num_layers]);
8372        });
8373        crate::gpu::cpu_scope(|| {
8374            for (pos, &id) in ids.iter().enumerate() {
8375                let emb = self.embed_single(id);
8376                let _ = self.forward_layers(&emb, pos, None);
8377            }
8378        });
8379        self.clear_sequence_state();
8380        FFN_PROBE
8381            .with(|p| p.borrow_mut().take())
8382            .unwrap_or_default()
8383    }
8384
8385    /// `probe_ffn_mass` over the BATCHED prefill: same accumulator, one
8386    /// sweep instead of one forward per token. What makes the statistic
8387    /// affordable on a 27B.
8388    pub fn probe_ffn_mass_batch(&mut self, ids: &[u32]) -> Result<Vec<Vec<f64>>, String> {
8389        if let Err(err) = self.nll_begin() {
8390            // A recorder can be left by a caller that was interrupted before
8391            // this request entered its scoring block.  Consume it even when
8392            // the preflight failure prevents initialization of a new one.
8393            let _ = FFN_PROBE.with(|p| p.borrow_mut().take());
8394            self.nll_end();
8395            return Err(err);
8396        }
8397        FFN_PROBE.with(|p| {
8398            *p.borrow_mut() = Some(vec![vec![0f64; self.intermediate_size]; self.num_layers]);
8399        });
8400        let result: Result<(), String> = (|| {
8401            for chunk in ids.chunks(256) {
8402                if chunk.len() < 2 {
8403                    continue;
8404                }
8405                self.nll_ids_masked(chunk, 0, None)?;
8406            }
8407            Ok(())
8408        })();
8409        self.nll_end();
8410        let probe = FFN_PROBE
8411            .with(|p| p.borrow_mut().take())
8412            .unwrap_or_default();
8413        match result {
8414            Ok(()) => Ok(probe),
8415            Err(err) => {
8416                drop(probe);
8417                Err(err)
8418            }
8419        }
8420    }
8421
8422    /// Teacher-forced PPL with a task mask active (sparse execution) —
8423    /// the quality gate for a DTG-MA-masked skill. Sequential per
8424    /// position: the batched prefill path is dense-only.
8425    pub fn ppl_ids_masked(&mut self, ids: &[u32], mask: &TaskMask) -> Result<f64, String> {
8426        self.nll_begin()?;
8427        let result: Result<f64, String> = (|| {
8428            let mut nll = 0f64;
8429            let mut cnt = 0usize;
8430            let mut hidden = vec![0f32; self.hidden_size];
8431            for (pos, &id) in ids.iter().enumerate() {
8432                if pos > 0 {
8433                    inference::rms_norm_into(
8434                        &hidden,
8435                        &self.weights.final_norm,
8436                        self.rms_eps,
8437                        self.norm_style,
8438                        &mut self.ws.n1,
8439                    );
8440                    let mut logits = self.lm_head_forward(&self.ws.n1);
8441                    let max = logits.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
8442                    let sum: f64 = logits.iter().map(|&v| ((v - max) as f64).exp()).sum();
8443                    let p = ((logits[id as usize] - max) as f64).exp() / sum.max(1e-300);
8444                    nll -= p.max(1e-300).ln();
8445                    cnt += 1;
8446                    attention::recycle_buf(&mut logits);
8447                }
8448                let emb = self.embed_single(id);
8449                hidden = self.forward_layers(&emb, pos, Some(mask));
8450                self.nll_check_graph("masked serial forward", pos)?;
8451                // Consume a possible graph logits side channel before the
8452                // next row.  Masked scoring normally disables that route,
8453                // but stale channel state must never survive a request.
8454                let _ = self.graph_logits.take();
8455            }
8456            Ok((nll / cnt.max(1) as f64).exp())
8457        })();
8458        self.nll_end();
8459        result
8460    }
8461
8462    /// Teacher-forced NLL sum + scored-token count over positions
8463    /// `start..len-1`, attention EXACT. Positions below `start` still
8464    /// run — they are the context — they are just not scored, so this
8465    /// pairs with `nll_ids_o1(ids, start)` over the very same tokens.
8466    ///
8467    /// Returning (nll, cnt) rather than a ppl is what lets a windowed
8468    /// caller combine windows before the exp, so every scored token
8469    /// weighs the same regardless of how the windows are cut.
8470    /// `nll_ids_from` with a task mask held active at every position.
8471    ///
8472    /// The batched prefill path does not thread masks, so this walks the
8473    /// per-position forward — slower, but it scores the file exactly the
8474    /// way `run --task` will serve it, which is the point of the gate
8475    /// that calls it. With `None` it defers to the fast path.
8476    /// Masked scoring rides the SAME batched sweep as unmasked scoring —
8477    /// the masked-inference fast path: `prefill_batch_masked` lands the
8478    /// per-visit FFN rows on the activations inside the fused arms. The
8479    /// per-position loop below remains only as the no-batch fallback.
8480    pub fn nll_ids_masked(
8481        &mut self,
8482        ids: &[u32],
8483        start: usize,
8484        task_mask: Option<&TaskMask>,
8485    ) -> Result<(f64, usize), String> {
8486        let task_mask = self.drop_open_mask(task_mask);
8487        self.nll_ids_inner(ids, start, task_mask)
8488    }
8489
8490    pub fn nll_ids_from(&mut self, ids: &[u32], start: usize) -> Result<(f64, usize), String> {
8491        self.nll_ids_inner(ids, start, None)
8492    }
8493
8494    fn nll_ids_inner(
8495        &mut self,
8496        ids: &[u32],
8497        start: usize,
8498        task_mask: Option<&TaskMask>,
8499    ) -> Result<(f64, usize), String> {
8500        self.nll_begin()?;
8501        let result: Result<(f64, usize), String> = (|| {
8502            let mut nll = 0f64;
8503            let mut cnt = 0usize;
8504            // An unmasked quality run with the resident wgpu graph must score
8505            // the same stateful path used by generation.  The layer-major
8506            // GEMM prefill below is a valid CPU/GEMM oracle, but it seeds
8507            // neither the graph's device GDN state nor its device KV mirrors;
8508            // using it here would silently score a different execution.  Keep
8509            // masked scoring on the exact per-position path as before, and
8510            // let the serial arm below drive the graph-aware scorer.
8511            // Only native Metal has a fused graph lm_head contract.  Vulkan
8512            // and other graph backends may expose hidden state without the
8513            // optional logits side channel; preserve their established CPU
8514            // norm/head fallback instead of turning that valid route into a
8515            // hard missing-logits error.
8516            let (graph_quality, fused_head_quality) = nll_graph_policy(
8517                task_mask.is_none(),
8518                self.graph_prefill_preferred(),
8519                crate::gpu::q1_force(),
8520            );
8521            self.graph_head_required = fused_head_quality;
8522            self.graph_want_logits = fused_head_quality;
8523            #[cfg(target_os = "macos")]
8524            if graph_quality && std::env::var("CMF_METAL_BATCH_NLL").as_deref() != Ok("0") {
8525                match self.nll_batch_metal(ids, start) {
8526                    MetalBatchNllOutcome::Completed(nll, count) => {
8527                        return Ok((nll, count));
8528                    }
8529                    MetalBatchNllOutcome::Declined => {}
8530                    MetalBatchNllOutcome::Failed(err) => return Err(err),
8531                }
8532            }
8533            // CMF_NLL_SERIAL=1 scores position by position through the
8534            // decode path (the wgpu token graph where it admits the model)
8535            // — the perplexity gate of a decode kernel, which the batched
8536            // prefill arm below never runs.
8537            let force_serial = std::env::var("CMF_NLL_SERIAL").as_deref() == Ok("1");
8538            if self.can_prefill_batched() && !graph_quality && !force_serial {
8539                // prefill-GEMM: layer-major position chunks, lm_head batched
8540                // (254MB lm_head read once per chunk, not per position).
8541                // The layer chunk is large (grouping positions by MoE experts
8542                // wins with size), lm_head in sub-blocks (logit buffer
8543                // 32×vocab ≈ 32MB instead of 128×).
8544                const CHUNK: usize = 128;
8545                const LM_SUB: usize = 32;
8546                let n = ids.len().saturating_sub(1);
8547                let hs = self.hidden_size;
8548                let rows = self.weights.lm_head.rows();
8549                let mut pos = 0usize;
8550                let state_trace = std::env::var("CMF_STATE_TRACE").is_ok();
8551                while pos < n {
8552                    let end = (pos + CHUNK).min(n);
8553                    let bsz = end - pos;
8554                    let hb = self.prefill_rows(&ids[pos..end], pos, task_mask)?;
8555                    self.nll_check_graph("batched prefill", pos)?;
8556                    if state_trace && end % 256 == 0 {
8557                        self.trace_recurrent_state(end);
8558                    }
8559                    let mut k0 = 0usize;
8560                    while k0 < bsz {
8561                        let k1 = (k0 + LM_SUB).min(bsz);
8562                        let sb = k1 - k0;
8563                        // Sub-block entirely below the scored range: the KV
8564                        // it just built is all this pass needed from it.
8565                        if pos + k1 <= start {
8566                            k0 = k1;
8567                            continue;
8568                        }
8569                        let mut normed = vec![0.0f32; sb * hs];
8570                        for k in 0..sb {
8571                            let r = inference::rms_norm(
8572                                &hb[(k0 + k) * hs..(k0 + k + 1) * hs],
8573                                &self.weights.final_norm,
8574                                self.rms_eps,
8575                                self.norm_style,
8576                            );
8577                            normed[k * hs..(k + 1) * hs].copy_from_slice(&r);
8578                        }
8579                        let mut logits = vec![0.0f32; sb * rows];
8580                        self.weights
8581                            .lm_head
8582                            .matmat(&normed, sb, &mut logits, self.pool.as_deref());
8583                        for k in 0..sb {
8584                            if pos + k0 + k < start {
8585                                continue;
8586                            }
8587                            self.nll_check_graph("batched score row", pos + k0 + k)?;
8588                            let lg = &mut logits[k * rows..k * rows + self.vocab_size.min(rows)];
8589                            if let Some(mu) = self.logit_multiplier {
8590                                for v in lg.iter_mut() {
8591                                    *v *= mu;
8592                                }
8593                            }
8594                            // Gemma-class final-logit soft-capping: the
8595                            // decode paths apply it; scoring must too, or
8596                            // the uncapped softmax misprices every token.
8597                            if let Some(c) = self.final_softcap {
8598                                softcap_in_place(lg, c, self.pool.as_deref());
8599                            }
8600                            // Cortiq Embryo hierarchical head: same correction
8601                            // the decode path applies (lm_head_forward).
8602                            if let Some(cm) = self.head_clusters.clone() {
8603                                self.hierarchical_head_logprobs(
8604                                    &normed[k * hs..(k + 1) * hs],
8605                                    &cm,
8606                                    lg,
8607                                );
8608                            }
8609                            let lg = &logits[k * rows..k * rows + self.vocab_size.min(rows)];
8610                            let target = ids[pos + k0 + k + 1] as usize;
8611                            let max = lg.iter().fold(f32::NEG_INFINITY, |m, &v| m.max(v));
8612                            let lse: f64 = lg
8613                                .iter()
8614                                .map(|&v| ((v - max) as f64).exp())
8615                                .sum::<f64>()
8616                                .ln()
8617                                + max as f64;
8618                            nll += lse - lg[target] as f64;
8619                            cnt += 1;
8620                            if std::env::var("CMF_PPL_TRACE").is_ok() {
8621                                let top = lg
8622                                    .iter()
8623                                    .enumerate()
8624                                    .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
8625                                    .map(|(i, _)| i)
8626                                    .unwrap_or(0);
8627                                eprintln!(
8628                                    "BTRACE pos {} target {} nll {:.4} top {} lg_t {:.3} lg_top {:.3}",
8629                                    pos + k0 + k,
8630                                    target,
8631                                    lse - lg[target] as f64,
8632                                    top,
8633                                    lg[target],
8634                                    lg[top]
8635                                );
8636                            }
8637                        }
8638                        k0 = k1;
8639                    }
8640                    pos = end;
8641                }
8642                return Ok((nll, cnt));
8643            }
8644            for pos in 0..ids.len().saturating_sub(1) {
8645                let hidden = self.forward_layers(&self.embed_single(ids[pos]), pos, task_mask);
8646                self.nll_check_graph("serial forward", pos)?;
8647                // Architectures whose head lives inside their own stack return
8648                // the logits out of band and a zero hidden — DeepSeek-V4 folds
8649                // its hyper-connection copies between the last layer and the
8650                // norm, so it cannot hand back a vector this loop could use.
8651                // Scoring the zeros gave a perplexity of exactly the vocabulary
8652                // size, which is a uniform distribution reported as a
8653                // measurement. `generate` already reads this channel.
8654                let out_of_band = self.graph_logits.take();
8655                if self.graph_head_required && out_of_band.is_none() {
8656                    METAL_GRAPH_HEAD_MISS.fetch_add(
8657                        1,
8658                        std::sync::atomic::Ordering::Relaxed,
8659                    );
8660                    return Err(format!(
8661                        "fused Metal graph head did not complete at NLL position {pos}"
8662                    ));
8663                }
8664                if pos < start {
8665                    continue;
8666                }
8667                let logits = match out_of_band {
8668                    Some(lg) => lg,
8669                    None => {
8670                        let normed = inference::rms_norm(
8671                            &hidden,
8672                            &self.weights.final_norm,
8673                            self.rms_eps,
8674                            self.norm_style,
8675                        );
8676                        // lm_head_forward applies the final-logit softcap itself
8677                        // — capping again here double-squashed gemma-class
8678                        // logits (tanh∘tanh) and reported a flattered ppl.
8679                        self.lm_head_forward(&normed)
8680                    }
8681                };
8682                let target = ids[pos + 1] as usize;
8683                let max = logits.iter().fold(f32::NEG_INFINITY, |m, &v| m.max(v));
8684                let lse: f64 = logits
8685                    .iter()
8686                    .map(|&v| ((v - max) as f64).exp())
8687                    .sum::<f64>()
8688                    .ln()
8689                    + max as f64;
8690                let tok_nll = lse - logits[target] as f64;
8691                if std::env::var("CMF_PPL_TRACE").is_ok() && pos < 48 {
8692                    let top = logits
8693                        .iter()
8694                        .enumerate()
8695                        .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
8696                        .map(|(i, _)| i)
8697                        .unwrap_or(0);
8698                    eprintln!(
8699                        "pos {pos:3} tgt {target:6} nll {tok_nll:7.3} | top1 {top:6} lg[t]={:.2} lg[top]={:.2}",
8700                        logits[target], logits[top]
8701                    );
8702                }
8703                nll += tok_nll;
8704                cnt += 1;
8705            }
8706            Ok((nll, cnt))
8707        })();
8708        self.nll_end();
8709        result
8710    }
8711
8712    /// Score one post-layer hidden with the same final norm/head path used by
8713    /// decode. Keeping this in one helper is important for the production
8714    /// batch scorer: its rows stop before the final norm, just like the
8715    /// per-position O(1) path below.
8716    fn nll_from_hidden(&mut self, hidden: &[f32], target: u32, pos: usize) -> f64 {
8717        let normed = inference::rms_norm(
8718            hidden,
8719            &self.weights.final_norm,
8720            self.rms_eps,
8721            self.norm_style,
8722        );
8723        // lm_head_forward applies the final-logit softcap itself — capping
8724        // again here double-squashed gemma-class logits in earlier scorers.
8725        let mut logits = self.lm_head_forward(&normed);
8726        let target = target as usize;
8727        let max = logits.iter().fold(f32::NEG_INFINITY, |m, &v| m.max(v));
8728        let lse: f64 = logits
8729            .iter()
8730            .map(|&v| ((v - max) as f64).exp())
8731            .sum::<f64>()
8732            .ln()
8733            + max as f64;
8734        let tok_nll = lse - logits[target] as f64;
8735        if std::env::var("CMF_PPL_TRACE").is_ok() && pos < 48 {
8736            let top = logits
8737                .iter()
8738                .enumerate()
8739                .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
8740                .map(|(i, _)| i)
8741                .unwrap_or(0);
8742            eprintln!(
8743                "pos {pos:3} tgt {target:6} nll {tok_nll:7.3} | top1 {top:6} lg[t]={:.2} lg[top]={:.2}",
8744                logits[target], logits[top]
8745            );
8746        }
8747        attention::recycle_buf(&mut logits);
8748        tok_nll
8749    }
8750
8751    /// `CMF_STATE_TRACE`: per-layer magnitude of the recurrent record at
8752    /// position `pos` — the whole `linear_state` (vmf: S then the conv
8753    /// ring; GDN: conv ring then S), its recurrent S part alone, the
8754    /// bounded ring, and the last per-position KV row count.  The tool
8755    /// that separated the ~4k perplexity cliff of the 500-step exports
8756    /// (a state that keeps climbing past the trained window) from a
8757    /// runtime boundary; one line per layer, `STATE pos=… layer=…`.
8758    fn trace_recurrent_state(&self, pos: usize) {
8759        let stats = |v: &[f32]| -> (f64, f64) {
8760            if v.is_empty() {
8761                return (0.0, 0.0);
8762            }
8763            let (mut ss, mut mx) = (0f64, 0f64);
8764            for &x in v {
8765                ss += (x as f64) * (x as f64);
8766                mx = mx.max((x as f64).abs());
8767            }
8768            ((ss / v.len() as f64).sqrt(), mx)
8769        };
8770        for (li, l) in self.kv_cache.layers.iter().enumerate() {
8771            let lw = &self.weights.layers[self.phys_layer(li)];
8772            let (kind, s_len) = match &lw.attn {
8773                AttnKind::Linear(_) => (
8774                    "vmf",
8775                    self.vmf_cfg.map(|c| c.state_len()).unwrap_or(0),
8776                ),
8777                AttnKind::LinearGdn(_) => (
8778                    "gdn",
8779                    self.gdn_cfg.map(|c| c.state_len()).unwrap_or(0),
8780                ),
8781                AttnKind::Bounded(_) => ("bounded", 0),
8782                AttnKind::Full { .. } => ("full", 0),
8783                _ => ("other", 0),
8784            };
8785            let (rms, max) = stats(&l.linear_state);
8786            let s_part = if kind == "vmf" {
8787                &l.linear_state[..s_len.min(l.linear_state.len())]
8788            } else if kind == "gdn" {
8789                let ring = l.linear_state.len().saturating_sub(s_len).min(l.linear_state.len());
8790                &l.linear_state[ring..]
8791            } else {
8792                &l.linear_state[..0]
8793            };
8794            let (s_rms, s_max) = stats(s_part);
8795            let (ring_rms, ring_len) = match &l.bounded {
8796                Some(b) => (stats(&b.ring_k).0, b.ring_k.len()),
8797                None => (0.0, 0),
8798            };
8799            eprintln!(
8800                "STATE pos={pos} layer={li} kind={kind} state_len={} rms={rms:.5} max={max:.4} \
8801                 S_rms={s_rms:.5} S_max={s_max:.4} ring_k_rms={ring_rms:.5} ring_len={ring_len} kv_rows={}",
8802                l.linear_state.len(),
8803                l.seq_len
8804            );
8805        }
8806    }
8807
8808    /// Teacher-forced NLL of the CONVERTED model: the O(1) Nyström path
8809    /// is ACTIVE over the scored positions. Returns `Ok((nll sum, scored
8810    /// count))` over `prefill..len-1` and surfaces a post-mutation batch
8811    /// failure instead of returning a partial score.
8812    ///
8813    /// Runtime discipline, deliberately NOT the matrix probe's: the
8814    /// requested prefix plus any required deferred lead-in run the exact
8815    /// prompt pass — that pass is what freezes the landmarks and M — and
8816    /// every post-seal scored position goes through `NystromState::step()`,
8817    /// the same code decode runs.
8818    /// So the landmarks are PREFILL-frozen (what ships), not
8819    /// full-sequence oracles (what the published probe measured). When the
8820    /// requested prefix is shorter than the bounded transition, rows in the
8821    /// exact lead-in are still scored so the shifted target range is stable.
8822    ///
8823    /// Pair with `nll_ids_from(ids, prefill)` for the exact baseline
8824    /// over the identical token set — that ratio is the honest one.
8825    pub fn nll_ids_o1(&mut self, ids: &[u32], prefill: usize) -> Result<(f64, usize), String> {
8826        // This scorer consumes host hiddens, so never request the optional
8827        // token-graph lm_head side channel. `nll_begin` also consumes a
8828        // prior graph failure and clears only the cancel bit that failure
8829        // raised, leaving a caller-owned cancellation observable.
8830        self.nll_begin()?;
8831        let requested_prefix = (prefill > 0).then_some(prefill);
8832        self.o1_begin_with_prefix(requested_prefix);
8833        let n = ids.len().saturating_sub(1);
8834        let requested_start = prefill.min(n);
8835        // The exact prefix must reach the deferred boundary before a
8836        // collecting layer can convert. Rows between the requested start and
8837        // that boundary remain part of the public NLL range and are scored
8838        // from the same hidden pass below.
8839        let exact_end = if self.o1_active() {
8840            match requested_prefix {
8841                Some(requested) => self.o1_effective_boundary(requested),
8842                None => self
8843                    .o1_cfg
8844                    .as_ref()
8845                    .and_then(|c| crate::nystrom::o1_deferred_boundary(c.w, c.sink)),
8846            }
8847            .unwrap_or(requested_start)
8848            .min(n)
8849        } else {
8850            requested_start
8851        };
8852        let mut nll = 0f64;
8853        let mut cnt = 0usize;
8854
8855        // Exact prompt pass over ids[..exact_end]: the seal consumes its
8856        // q/k/v. Rows at or after requested_start are scored here when the
8857        // bounded lead-in is longer than the caller's requested prefix.
8858        let mut pos = 0usize;
8859        if self.can_prefill_batched() {
8860            const CHUNK: usize = 128;
8861            while pos < exact_end {
8862                let end = (pos + CHUNK).min(exact_end);
8863                let hiddens = self.prefill_batch(&ids[pos..end], pos);
8864                if self
8865                    .graph_failed
8866                    .swap(false, std::sync::atomic::Ordering::Relaxed)
8867                {
8868                    self.cancel
8869                        .store(false, std::sync::atomic::Ordering::Relaxed);
8870                    self.nll_end();
8871                    return Err("GPU graph failed during O(1) NLL prefix".into());
8872                }
8873                for row in 0..end - pos {
8874                    let score_pos = pos + row;
8875                    if score_pos >= requested_start && score_pos < n {
8876                        nll += self.nll_from_hidden(
8877                            &hiddens[row * self.hidden_size..(row + 1) * self.hidden_size],
8878                            ids[score_pos + 1],
8879                            score_pos,
8880                        );
8881                        cnt += 1;
8882                    }
8883                }
8884                pos = end;
8885            }
8886        } else {
8887            while pos < exact_end {
8888                let hidden = self.forward_layers(&self.embed_single(ids[pos]), pos, None);
8889                if self
8890                    .graph_failed
8891                    .swap(false, std::sync::atomic::Ordering::Relaxed)
8892                {
8893                    self.cancel
8894                        .store(false, std::sync::atomic::Ordering::Relaxed);
8895                    self.nll_end();
8896                    return Err("GPU graph failed during O(1) NLL prefix".into());
8897                }
8898                if pos >= requested_start && pos < n {
8899                    nll += self.nll_from_hidden(&hidden, ids[pos + 1], pos);
8900                    cnt += 1;
8901                }
8902                pos += 1;
8903            }
8904        }
8905        self.o1_seal_checked().map_err(|err| {
8906            self.nll_end();
8907            err
8908        })?;
8909
8910        // Reuse the production whole-token batch graph for the post-seal
8911        // suffix when the caller explicitly enabled both routes. This is a
8912        // teacher-forced scorer, so every row is ids[pos] and its target is
8913        // ids[pos + 1]; no speculative tail or rollback state is involved.
8914        // A first Declined is safe to handle with the established serial O(1)
8915        // path. Once a chunk completes, however, the device recurrent state
8916        // owns the sequence and a later decline must be terminal rather than
8917        // falling back to stale CPU state.
8918        let batch_k = std::env::var("CMF_BATCH_K")
8919            .ok()
8920            .and_then(|v| v.parse::<usize>().ok())
8921            .unwrap_or(0);
8922        let batch_admitted = batch_k > 0
8923            && self.can_prefill_batched()
8924            && self.o1_active()
8925            && std::env::var("CMF_O1_GPU").as_deref() == Ok("1")
8926            && (0..self.num_layers).all(|li| {
8927                let cache = &self.kv_cache.layers[self.phys_layer(li)];
8928                cache.o1.is_none() || cache.o1_views().is_some()
8929            });
8930        if std::env::var("CMF_GRAPH_PROF").is_ok() {
8931            eprintln!(
8932                "nll-batch: phase=post-seal admission={} requested_k={} scored_rows={}",
8933                batch_admitted,
8934                batch_k,
8935                n.saturating_sub(exact_end),
8936            );
8937        }
8938        let mut batch_completed = false;
8939        if batch_admitted && exact_end < n {
8940            let hs = self.hidden_size;
8941            let mut batch_pos = exact_end;
8942            while batch_pos < n {
8943                let end = (batch_pos + batch_k).min(n);
8944                let bk = end - batch_pos;
8945                let mut hiddens = vec![0.0f32; bk * hs];
8946                for (row, &id) in ids[batch_pos..end].iter().enumerate() {
8947                    hiddens[row * hs..(row + 1) * hs].copy_from_slice(&self.embed_single(id));
8948                }
8949                let positions: Vec<usize> = (batch_pos..end).collect();
8950                let t_batch = std::time::Instant::now();
8951                let outcome = self.try_batch_graph_wgpu(&mut hiddens, &positions, bk, None);
8952                if std::env::var("CMF_GRAPH_PROF").is_ok() {
8953                    let ms = t_batch.elapsed().as_secs_f64() * 1000.0;
8954                    eprintln!(
8955                        "nll-batch: phase=post-seal mode=o1 k={bk} pos={}..{} outcome={outcome:?} {ms:.1} ms ({:.1} tok/s)",
8956                        batch_pos,
8957                        end.saturating_sub(1),
8958                        bk as f64 / (ms / 1000.0),
8959                    );
8960                }
8961                if let Err(err) = self.nll_check_graph("batch graph", batch_pos) {
8962                    self.nll_end();
8963                    return Err(err);
8964                }
8965                match outcome {
8966                    crate::gpu::BatchGraphOutcome::Completed => {
8967                        batch_completed = true;
8968                        for row in 0..bk {
8969                            nll += self.nll_from_hidden(
8970                                &hiddens[row * hs..(row + 1) * hs],
8971                                ids[batch_pos + row + 1],
8972                                batch_pos + row,
8973                            );
8974                            cnt += 1;
8975                        }
8976                        batch_pos = end;
8977                    }
8978                    crate::gpu::BatchGraphOutcome::Declined => {
8979                        if batch_completed {
8980                            self.nll_end();
8981                            return Err(format!(
8982                                "O(1) NLL batch declined after completed chunk at position {batch_pos}"
8983                            ));
8984                        }
8985                        break;
8986                    }
8987                    crate::gpu::BatchGraphOutcome::Failed => {
8988                        self.nll_end();
8989                        return Err(format!(
8990                            "O(1) NLL batch graph failed after admission at position {batch_pos}"
8991                        ));
8992                    }
8993                }
8994            }
8995            if batch_completed && cnt == n.saturating_sub(requested_start) {
8996                self.nll_end();
8997                return Ok((nll, cnt));
8998            }
8999        }
9000
9001        // Serial O(1) fallback/reference. It is intentionally retained when
9002        // batch admission declines before mutation; callers must label this
9003        // CMF_BATCH_K=0/per-position path separately from the production
9004        // whole-token batch route.
9005        for pos in exact_end..n {
9006            let hidden = self.forward_layers(&self.embed_single(ids[pos]), pos, None);
9007            if self
9008                .graph_failed
9009                .swap(false, std::sync::atomic::Ordering::Relaxed)
9010            {
9011                self.cancel
9012                    .store(false, std::sync::atomic::Ordering::Relaxed);
9013                self.nll_end();
9014                return Err(format!(
9015                    "GPU graph failed during O(1) NLL serial scoring at position {pos}"
9016                ));
9017            }
9018            nll += self.nll_from_hidden(&hidden, ids[pos + 1], pos);
9019            cnt += 1;
9020        }
9021        self.nll_end();
9022        Ok((nll, cnt))
9023    }
9024
9025    /// Teacher-forced calibration data (B1): for each position, whether the
9026    /// argmax equals the actual next token, and the top-1 softmax prob
9027    /// (top-1 probability) under EACH temperature in `temps` — all from ONE forward
9028    /// pass (argmax/correctness are temperature-invariant; only p_max
9029    /// reshapes). Feeds `cortiq calibrate` (reliability/ECE + temperature
9030    /// fit): is the model's confidence a true property, or does it need a
9031    /// measured scaling?
9032    pub fn calib_ids(&mut self, ids: &[u32], temps: &[f32]) -> (Vec<bool>, Vec<Vec<f32>>) {
9033        self.clear_sequence_state();
9034        let n = ids.len().saturating_sub(1);
9035        let mut correct = Vec::with_capacity(n);
9036        let mut pmax = Vec::with_capacity(n);
9037        for pos in 0..n {
9038            let emb = self.embed_single(ids[pos]);
9039            let hidden = self.forward_layers(&emb, pos, None);
9040            let logits = if let Some(logits) = self.graph_logits.take() {
9041                logits
9042            } else {
9043                let normed = inference::rms_norm(
9044                    &hidden,
9045                    &self.weights.final_norm,
9046                    self.rms_eps,
9047                    self.norm_style,
9048                );
9049                // lm_head_forward applies the final-logit softcap itself —
9050                // capping again here double-squashed gemma-class logits
9051                // (tanh∘tanh) and reported a flattered ppl.
9052                self.lm_head_forward(&normed)
9053            };
9054            let target = ids[pos + 1] as usize;
9055            let (mut amax, mut mval) = (0usize, f32::NEG_INFINITY);
9056            for (i, &v) in logits.iter().enumerate() {
9057                if v > mval {
9058                    mval = v;
9059                    amax = i;
9060                }
9061            }
9062            correct.push(amax == target);
9063            let row: Vec<f32> = temps
9064                .iter()
9065                .map(|&t| {
9066                    let tt = t.max(1e-3);
9067                    let s: f32 = logits.iter().map(|&v| ((v - mval) / tt).exp()).sum();
9068                    1.0 / s.max(1e-12) // numerator at the max is exp(0)=1
9069                })
9070                .collect();
9071            pmax.push(row);
9072        }
9073        self.clear_sequence_state();
9074        (correct, pmax)
9075    }
9076
9077    /// Teacher-forced PPL with the dynamic router driving per-window
9078    /// skill switches (VMF experiment №2 measurement). Sequential (φ
9079    /// must update per token), returns (ppl, switch_count). The router
9080    /// must be enabled (`enable_dynamic_routing`); else this equals
9081    /// plain `ppl_ids`. The active skill when scoring token t shapes the
9082    /// logits for t+1 — on-policy over the held-out text itself.
9083    pub fn ppl_ids_dynamic(&mut self, ids: &[u32]) -> Result<(f64, usize), String> {
9084        if self.dyn_router.is_none() {
9085            return Ok((self.ppl_ids(ids)?, 0));
9086        }
9087        self.nll_begin()?;
9088        let saved_active = self.dyn_active;
9089        let mut router = self
9090            .dyn_router
9091            .take()
9092            .ok_or_else(|| "dynamic router disappeared before PPL scoring".to_string())?;
9093        router.reset();
9094        self.dyn_phi_seen = 0;
9095        let _ = self.set_active_skill(None);
9096
9097        let result: Result<(f64, usize), String> = (|| {
9098            let mut nll = 0f64;
9099            let mut cnt = 0usize;
9100            for pos in 0..ids.len().saturating_sub(1) {
9101                let hidden = self.forward_layers(&self.embed_single(ids[pos]), pos, None);
9102                self.nll_check_graph("dynamic serial forward", pos)?;
9103                let out_of_band = self.graph_logits.take();
9104                let mut logits = match out_of_band {
9105                    Some(lg) => lg,
9106                    None => {
9107                        let normed = inference::rms_norm(
9108                            &hidden,
9109                            &self.weights.final_norm,
9110                            self.rms_eps,
9111                            self.norm_style,
9112                        );
9113                        // lm_head_forward applies the final-logit softcap itself —
9114                        // capping again here double-squashed gemma-class logits
9115                        // and reported a flattered ppl.
9116                        self.lm_head_forward(&normed)
9117                    }
9118                };
9119                let target = ids[pos + 1] as usize;
9120                let max = logits.iter().fold(f32::NEG_INFINITY, |m, &v| m.max(v));
9121                let lse: f64 = logits
9122                    .iter()
9123                    .map(|&v| ((v - max) as f64).exp())
9124                    .sum::<f64>()
9125                    .ln()
9126                    + max as f64;
9127                let tok_nll = lse - logits[target] as f64;
9128                if std::env::var("CMF_PPL_TRACE").is_ok() && pos < 48 {
9129                    let top = logits
9130                        .iter()
9131                        .enumerate()
9132                        .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
9133                        .map(|(i, _)| i)
9134                        .unwrap_or(0);
9135                    eprintln!(
9136                        "pos {pos:3} tgt {target:6} nll {tok_nll:7.3} | top1 {top:6} lg[t]={:.2} lg[top]={:.2}",
9137                        logits[target], logits[top]
9138                    );
9139                }
9140                nll += tok_nll;
9141                cnt += 1;
9142                attention::recycle_buf(&mut logits);
9143                // Route on the evolving phi (drives the NEXT token's skill).
9144                let phi = self.dyn_phi_ema.clone();
9145                if let Some(new_active) = router.step(&phi, pos) {
9146                    let _ = self.set_active_skill(new_active);
9147                }
9148            }
9149            Ok(((nll / cnt.max(1) as f64).exp(), router.switches.len()))
9150        })();
9151
9152        // Restore the detached router and the active overlay on both success
9153        // and failure. The scoring state is cleared independently below.
9154        let _ = self.set_active_skill(saved_active);
9155        self.dyn_router = Some(router);
9156        self.nll_end();
9157        result
9158    }
9159
9160    /// Routing probe φ (spec §9): mean-pooled hidden after `layer`.
9161    pub fn probe_phi(&mut self, ids: &[u32], layer: usize) -> Vec<f32> {
9162        self.clear_sequence_state();
9163        let mut acc = vec![0f32; self.hidden_size];
9164        for (pos, &id) in ids.iter().enumerate() {
9165            let h = self.forward_layers_upto(&self.embed_single(id), pos, None, Some(layer));
9166            for (a, v) in acc.iter_mut().zip(&h) {
9167                *a += v;
9168            }
9169        }
9170        let n = ids.len().max(1) as f32;
9171        for a in acc.iter_mut() {
9172            *a /= n;
9173        }
9174        self.clear_sequence_state();
9175        acc
9176    }
9177
9178    /// Router-v2 φ probe (spec §9.4, `phi.pool = "span_mean"`): the hidden
9179    /// AFTER `layer` — the same per-position walk and the same quantity as
9180    /// [`Self::probe_phi`] — averaged over the positions in `span` only
9181    /// (the user text between the template's prefix and suffix ids), NOT
9182    /// unit-normalized (the decision normalizes). The walk stops at
9183    /// `span.end`: causality makes the later positions irrelevant, so the
9184    /// result is bit-identical to probing `ids[..span.end]`. An empty span
9185    /// gives the zero vector, which the decision treats as degenerate.
9186    ///
9187    /// Every sequence state is reset before and after — the host KV/ring/
9188    /// recurrent state, the reuse keys (`kv_history`, `kv_prefix`) and the
9189    /// device graph's sequence — so run it on a pipeline that does not
9190    /// also serve a conversation (its prefix reuse would be lost).
9191    pub fn probe_phi_span(
9192        &mut self,
9193        ids: &[u32],
9194        layer: usize,
9195        span: std::ops::Range<usize>,
9196    ) -> Vec<f32> {
9197        #[cfg(target_os = "macos")]
9198        crate::gpu_metal::set_io_namespace(self.graph_kv_id);
9199        let end = span.end.min(ids.len());
9200        let start = span.start.min(end);
9201        let reset = |p: &mut Self| p.clear_sequence_state();
9202        reset(self);
9203        let mut acc = vec![0f32; self.hidden_size];
9204        for (pos, &id) in ids[..end].iter().enumerate() {
9205            let h = self.forward_layers_upto(&self.embed_single(id), pos, None, Some(layer));
9206            if pos >= start {
9207                for (a, v) in acc.iter_mut().zip(&h) {
9208                    *a += v;
9209                }
9210            }
9211        }
9212        let n = end - start;
9213        if n > 0 {
9214            let n = n as f32;
9215            for a in acc.iter_mut() {
9216                *a /= n;
9217            }
9218        }
9219        reset(self);
9220        acc
9221    }
9222
9223    /// One decode step of the current sequence: forward `token` at
9224    /// `position` (the cache holds positions `[0, position)`, e.g. after
9225    /// [`Self::forward_ids`]) and return the next-token logits — the same
9226    /// forward and head the generation loop runs (resident-graph logits
9227    /// when the graph ran, final norm + lm_head otherwise). The logit-dump
9228    /// tools drive greedy decoding with it so every position is observable.
9229    pub fn decode_step_logits(&mut self, token: u32, position: usize) -> Vec<f32> {
9230        #[cfg(target_os = "macos")]
9231        crate::gpu_metal::set_io_namespace(self.graph_kv_id);
9232        self.graph_logits = None;
9233        let hidden = self.forward_layers(&self.embed_single(token), position, None);
9234        if let Some(logits) = self.graph_logits.take() {
9235            return logits;
9236        }
9237        inference::rms_norm_into(
9238            &hidden,
9239            &self.weights.final_norm,
9240            self.rms_eps,
9241            self.norm_style,
9242            &mut self.ws.n1,
9243        );
9244        self.lm_head_forward(&self.ws.n1)
9245    }
9246
9247    /// Layer-major batched prefill (prefill-GEMM): full-attention —
9248    /// per-position with the existing operators (KV grows naturally,
9249    /// causality preserved), GDN projections / FFN / MoE — batched
9250    /// (a weight row is read from DRAM once per chunk, not per
9251    /// position). Returns the hidden of all positions [b × hidden].
9252    fn prefill_batch(&mut self, ids: &[u32], start_pos: usize) -> Vec<f32> {
9253        self.prefill_batch_masked(ids, start_pos, None)
9254    }
9255
9256    /// `prefill_batch` with a task mask honored on the dense-FFN panels
9257    /// (the masked-inference fast path: full fused compute, mask lands on
9258    /// the activations). The whole-chunk GPU graph is skipped for masked
9259    /// layers by the callers' arms; the per-GEMM device paths stay in
9260    /// play because the zeroing happens on the host between them.
9261    fn prefill_batch_masked(
9262        &mut self,
9263        ids: &[u32],
9264        start_pos: usize,
9265        task_mask: Option<&TaskMask>,
9266    ) -> Vec<f32> {
9267        self.prefill_batch_span(PrefillIn::Ids(ids), start_pos, task_mask, 0, usize::MAX)
9268    }
9269
9270    /// One prompt chunk through the whole stack, post-stack rows out (no
9271    /// final norm) — the ingest generation uses, shared by scoring and
9272    /// `forward_ids` so they measure the same execution: the batched wgpu
9273    /// graph's device prefix plus the host's batched walk for the rest when
9274    /// `batch_prefix_prefill` holds and the graph admits the chunk, else
9275    /// the host's chunked prefill. Err only when a graph that had mutated
9276    /// device state failed.
9277    fn prefill_rows(
9278        &mut self,
9279        ids: &[u32],
9280        pos: usize,
9281        task_mask: Option<&TaskMask>,
9282    ) -> Result<Vec<f32>, String> {
9283        self.prefill_input_rows(PrefillIn::Ids(ids), pos, task_mask)
9284    }
9285
9286    fn prefill_input_rows(
9287        &mut self,
9288        input: PrefillIn<'_>,
9289        pos: usize,
9290        task_mask: Option<&TaskMask>,
9291    ) -> Result<Vec<f32>, String> {
9292        self.mimo_moe_prepare();
9293        let hs = self.hidden_size;
9294        let bk = match input {
9295            PrefillIn::Ids(ids) => ids.len(),
9296            PrefillIn::Hidden(rows) => rows.len() / hs,
9297        };
9298        #[cfg(not(target_os = "macos"))]
9299        if task_mask.is_none()
9300            && !self.o1_active()
9301            && bk > 1
9302            && (self.batch_prefix_prefill()
9303                || (self.verify_exact_moe
9304                    && crate::gpu::enabled_here()
9305                    && crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Decode)))
9306        {
9307            let mut hiddens = match input {
9308                PrefillIn::Hidden(rows) => rows.to_vec(),
9309                PrefillIn::Ids(ids) => ids.iter().flat_map(|&id| self.embed_single(id)).collect(),
9310            };
9311            let positions: Vec<usize> = (pos..pos + bk).collect();
9312            let mut run = 0usize;
9313            match self.try_batch_graph_wgpu_prefix(
9314                &mut hiddens,
9315                &positions,
9316                bk,
9317                None,
9318                Some(&mut run),
9319            ) {
9320                crate::gpu::BatchGraphOutcome::Completed => {
9321                    let out = if run < self.num_layers {
9322                        self.prefill_batch_span(
9323                            PrefillIn::Hidden(&hiddens),
9324                            pos,
9325                            None,
9326                            run,
9327                            self.num_layers,
9328                        )
9329                    } else {
9330                        hiddens
9331                    };
9332                    return if self.graph_failed.load(std::sync::atomic::Ordering::Relaxed) {
9333                        Err("MiMo attention graph failed after admission".into())
9334                    } else { Ok(out) };
9335                }
9336                crate::gpu::BatchGraphOutcome::Failed => {
9337                    return Err("batched prefix prefill failed after admission".into());
9338                }
9339                crate::gpu::BatchGraphOutcome::Declined => {
9340                    // Rows an earlier chunk left on the device only.
9341                    #[cfg(feature = "gpu")]
9342                    self.pull_lagging_host_kv(0, self.num_layers, pos);
9343                }
9344            }
9345        }
9346        let out = self.prefill_batch_span(input, pos, task_mask, 0, usize::MAX);
9347        if self.graph_failed.load(std::sync::atomic::Ordering::Relaxed) {
9348            Err("batch tail graph failed after admission".into())
9349        } else { Ok(out) }
9350    }
9351
9352    /// The layer-major batched walk over a layer span [from..upto_excl):
9353    /// the whole prefill machinery (chunk graph, batched attends, GEMM
9354    /// panels) for a PARTIAL stack — the network split's prefill rides
9355    /// the same canon as the local one. Input is token ids (embeds
9356    /// itself, coordinator side) or ready boundary hiddens (worker side).
9357    fn prefill_batch_span(
9358        &mut self,
9359        input: PrefillIn<'_>,
9360        start_pos: usize,
9361        task_mask: Option<&TaskMask>,
9362        from: usize,
9363        upto_excl: usize,
9364    ) -> Vec<f32> {
9365        let hs = self.hidden_size;
9366        let b = match input {
9367            PrefillIn::Ids(ids) => ids.len(),
9368            PrefillIn::Hidden(hb) => hb.len() / hs,
9369        };
9370        let upto_excl = upto_excl.min(self.num_layers);
9371        // The CPU embed is deferred: when the chunk graph takes the run
9372        // from layer 0 it gathers the embeddings on the device instead.
9373        // A hidden input is ready by definition.
9374        let mut h: Vec<f32>;
9375        let mut h_ready;
9376        match input {
9377            PrefillIn::Ids(_) => {
9378                h = vec![0.0; b * hs];
9379                h_ready = false;
9380            }
9381            PrefillIn::Hidden(hb) => {
9382                h = hb.to_vec();
9383                h_ready = true;
9384            }
9385        }
9386        let fill_h = |h: &mut Vec<f32>, me: &Self| {
9387            if let PrefillIn::Ids(ids) = input {
9388                for (bi, &id) in ids.iter().enumerate() {
9389                    let e = me.embed_single(id);
9390                    h[bi * hs..(bi + 1) * hs].copy_from_slice(&e);
9391                }
9392                if let Ok(tp) = std::env::var("CMF_TRACE_POS") {
9393                    if let Ok(t) = tp.parse::<usize>() {
9394                        if t >= start_pos && t < start_pos + ids.len() {
9395                            let bi = t - start_pos;
9396                            let row = &h[bi * hs..(bi + 1) * hs];
9397                            let n: f32 = row.iter().map(|x| x * x).sum::<f32>().sqrt();
9398                            eprintln!(
9399                                "BATCH pos {t} embed: id {} |h| = {n:.6} h0 {:.6} h1 {:.6} | b={} start={start_pos} ids[..8]={:?}",
9400                                ids[bi],
9401                                row[0],
9402                                row[1],
9403                                ids.len(),
9404                                &ids[..ids.len().min(8)]
9405                            );
9406                        }
9407                    }
9408                }
9409            }
9410        };
9411        let (_nkv, _hd, _rd, eps) = (
9412            self.num_kv_heads,
9413            self.head_dim,
9414            self.rotary_dim,
9415            self.rms_eps,
9416        );
9417        let pool = self.pool.clone();
9418        let norm_style = self.norm_style;
9419        self.mimo_moe_prepare();
9420        let automatic_gpu_prefix = self.automatic_gpu_prefix();
9421
9422        #[cfg(target_os = "macos")]
9423        let mut chunk_skip_until = 0usize;
9424        for li in from..upto_excl {
9425            let _capacity_tail = automatic_gpu_prefix
9426                .filter(|&prefix| {
9427                    li >= prefix && !(self.verify_exact_moe && self.mimo_moe.is_dynamic(li, false))
9428                })
9429                .map(|_| crate::gpu::enter_cpu_scope());
9430            crate::gpu::set_layer(li as i64); // layer-split GPU/CPU
9431            // Spark-X2.5: the layer's int8 matrix-unit GEMMs take the
9432            // `zi_mm` tile, bit-identical to the 64x64 kernel and about
9433            // three times its rate (`gpu_wgpu::zi_gemm_route`).
9434            #[cfg(feature = "gpu")]
9435            let _fast_gemm = self
9436                .proj_gate_sigmoid
9437                .then(crate::gpu::enter_prefill_fast_gemm);
9438            // GPU chunk graph (default-on under CMF_GPU=1): a run of
9439            // consecutive eligible layers for the whole chunk in ONE
9440            // Metal submission — norm, QKV, RoPE with fused mirror
9441            // append, causal attend, O, FFN, hidden device-resident
9442            // across the run. Any refusal falls through to the CPU path.
9443            #[cfg(target_os = "macos")]
9444            if task_mask.is_none() {
9445                if li < chunk_skip_until {
9446                    continue;
9447                }
9448                // Device-side embedding needs a q8_row embedding matrix;
9449                // with any other layout the CPU fills `h` first and the
9450                // graph starts from a ready hidden (refusing the whole
9451                // run over the embedding alone kept q4t models — the
9452                // whole Nanbeige/Bonsai class — on the CPU prefill).
9453                if !h_ready && li == 0 && self.weights.embed_tokens.q8_row_parts().is_none() {
9454                    fill_h(&mut h, self);
9455                    h_ready = true;
9456                }
9457                let ids_for_embed = match input {
9458                    PrefillIn::Ids(ids) => (!h_ready && li == 0).then_some(ids),
9459                    PrefillIn::Hidden(_) => None,
9460                };
9461                let end = self.chunk_run_gpu(li, &mut h, b, start_pos, ids_for_embed, upto_excl);
9462                if end > li {
9463                    h_ready = true;
9464                    chunk_skip_until = end;
9465                    // Looped Transformer: the graph stopped at a loop
9466                    // boundary — apply final norm before the next iteration.
9467                    if self.is_loop_end(end - 1) && end < self.num_layers {
9468                        for bi in 0..b {
9469                            let normed = inference::rms_norm(
9470                                &h[bi * hs..(bi + 1) * hs],
9471                                &self.weights.final_norm,
9472                                eps,
9473                                norm_style,
9474                            );
9475                            h[bi * hs..(bi + 1) * hs].copy_from_slice(&normed);
9476                        }
9477                    }
9478                    continue;
9479                }
9480            }
9481            if !h_ready {
9482                fill_h(&mut h, self);
9483                h_ready = true;
9484            }
9485            if task_mask.is_none() && self.verify_exact_moe {
9486                let positions: Vec<_> = (start_pos..start_pos + b).collect();
9487                match self.mimo_graph_layer_rows(li, &mut h, &positions) {
9488                    crate::gpu::BatchGraphOutcome::Completed => continue,
9489                    crate::gpu::BatchGraphOutcome::Failed => return h,
9490                    crate::gpu::BatchGraphOutcome::Declined => {},
9491                }
9492            }
9493            #[cfg(feature = "gpu")]
9494            self.pull_lagging_host_kv(li, li + 1, start_pos);
9495            let lw = &self.weights.layers[self.phys_layer(li)];
9496            let t_attn = std::time::Instant::now();
9497            // ── attention ──
9498            match &lw.attn {
9499                AttnKind::Kda(w) => {
9500                    // Projections batched, recurrence sequential.
9501                    let cfg = self.kda_cfg.expect("kda layer without kda_cfg");
9502                    let mut normed = vec![0.0f32; b * hs];
9503                    for bi in 0..b {
9504                        inference::rms_norm_into(
9505                            &h[bi * hs..(bi + 1) * hs],
9506                            &lw.input_norm,
9507                            eps,
9508                            norm_style,
9509                            &mut normed[bi * hs..(bi + 1) * hs],
9510                        );
9511                    }
9512                    let attn = crate::linear_core::kda_forward_batch(
9513                        &normed,
9514                        b,
9515                        w,
9516                        &cfg,
9517                        &mut self.kv_cache.layers[li].linear_state,
9518                        pool.as_deref(),
9519                    );
9520                    for (dst, &a) in h.iter_mut().zip(&attn) {
9521                        *dst += a;
9522                    }
9523                }
9524                AttnKind::LinearGdn(w) => {
9525                    // Projections batched, recurrence sequential.
9526                    let cfg = self.gdn_cfg.expect("gdn layer without gdn_cfg");
9527                    let mut normed = vec![0.0f32; b * hs];
9528                    for bi in 0..b {
9529                        let r = inference::rms_norm(
9530                            &h[bi * hs..(bi + 1) * hs],
9531                            &lw.input_norm,
9532                            eps,
9533                            norm_style,
9534                        );
9535                        normed[bi * hs..(bi + 1) * hs].copy_from_slice(&r);
9536                    }
9537                    let attn = crate::linear_core::gdn_forward_batch(
9538                        &normed,
9539                        b,
9540                        w,
9541                        &cfg,
9542                        &mut self.kv_cache.layers[li].linear_state,
9543                        pool.as_deref(),
9544                    );
9545                    for (dst, &a) in h.iter_mut().zip(&attn) {
9546                        *dst += a;
9547                    }
9548                }
9549                AttnKind::ShortConv(w) => {
9550                    // Projections batched over the chunk; the conv walks the
9551                    // contiguous positions in order (same ring as decode).
9552                    let cfg = self
9553                        .short_conv_cfg
9554                        .expect("short-conv layer without short_conv_cfg");
9555                    let mut normed = vec![0.0f32; b * hs];
9556                    for bi in 0..b {
9557                        inference::rms_norm_into(
9558                            &h[bi * hs..(bi + 1) * hs],
9559                            &lw.input_norm,
9560                            eps,
9561                            norm_style,
9562                            &mut normed[bi * hs..(bi + 1) * hs],
9563                        );
9564                    }
9565                    let attn = short_conv_forward_batch(
9566                        &normed,
9567                        b,
9568                        w,
9569                        &cfg,
9570                        &mut self.kv_cache.layers[li].linear_state,
9571                        pool.as_deref(),
9572                    );
9573                    for (dst, &a) in h.iter_mut().zip(&attn) {
9574                        *dst += a;
9575                    }
9576                }
9577                AttnKind::Mla(w) => {
9578                    // Per-position prefill (correctness first; latent
9579                    // batching is a later optimization).
9580                    let inv_freq_l = self.layer_inv_freq(li);
9581                    let rs = self.layer_rope_scale(li);
9582                    let mut normed = vec![0.0f32; hs];
9583                    for bi in 0..b {
9584                        inference::rms_norm_into(
9585                            &h[bi * hs..(bi + 1) * hs],
9586                            &lw.input_norm,
9587                            eps,
9588                            norm_style,
9589                            &mut normed,
9590                        );
9591                        let ao = mla_attention(
9592                            w,
9593                            &normed,
9594                            &mut self.kv_cache.layers[li],
9595                            start_pos + bi,
9596                            &inv_freq_l,
9597                            rs,
9598                            eps,
9599                            pool.as_deref(),
9600                        );
9601                        for (dst, &a) in h[bi * hs..(bi + 1) * hs].iter_mut().zip(&ao) {
9602                            *dst += a;
9603                        }
9604                    }
9605                }
9606                AttnKind::Full {
9607                    wq,
9608                    wk,
9609                    wv,
9610                    wo,
9611                    q_norm,
9612                    k_norm,
9613                    output_gate,
9614                    softplus_gate,
9615                    bias,
9616                } => {
9617                    // Chunk-GEMM QKV/O; per-position causal attention
9618                    // inside (roadmap §3 P0 — full-attention prefill no
9619                    // longer re-reads the projection weights b times).
9620                    let mut normed = vec![0.0f32; b * hs];
9621                    for bi in 0..b {
9622                        inference::rms_norm_into(
9623                            &h[bi * hs..(bi + 1) * hs],
9624                            &lw.input_norm,
9625                            eps,
9626                            norm_style,
9627                            &mut normed[bi * hs..(bi + 1) * hs],
9628                        );
9629                    }
9630                    let inv_freq_l = self.layer_inv_freq(li);
9631                    let (nkv_l, hd_l, rd_l) = self.layer_geom(li);
9632                    let cfg = QwenAttnCfg {
9633                        num_heads: self.layer_num_heads(li),
9634                        num_kv_heads: nkv_l,
9635                        head_dim: hd_l,
9636                        hidden_size: hs,
9637                        position: start_pos,
9638                        inv_freq: &inv_freq_l,
9639                        rotary_dim: rd_l,
9640                        scale: self.attn_scale,
9641                        softcap: self.attn_softcap,
9642                        window: self.layer_window(li),
9643                        v_norm: self.attn_v_norm,
9644                        qk_norm_after_rope: self.qk_norm_after_rope,
9645                        gate_sigmoid: self.proj_gate_sigmoid,
9646                        q_norm: q_norm.as_deref(),
9647                        k_norm: k_norm.as_deref(),
9648                        output_gate: *output_gate,
9649                        softplus_gate: softplus_gate
9650                            .as_ref()
9651                            .map(|(gate, per_head)| (gate, *per_head)),
9652                        rope_scale: self.layer_rope_scale(li),
9653                        bias: bias
9654                            .as_ref()
9655                            .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
9656                        rms_eps: eps,
9657                        norm_style,
9658                        pool: pool.as_deref(),
9659                        v_head_dim: self.layer_v_dim(li),
9660                    };
9661                    #[cfg(feature = "gpu")]
9662                    let _mirror = self
9663                        .prefill_mirror_target(li)
9664                        .map(crate::gpu::enter_prefill_mirror);
9665                    let mut attn = attention::qwen_attention_batch(
9666                        &normed,
9667                        b,
9668                        wq,
9669                        wk,
9670                        wv,
9671                        wo,
9672                        &mut self.kv_cache.layers[li],
9673                        &cfg,
9674                    );
9675                    if let Some(w) = &lw.attn_out_norm {
9676                        for bi in 0..b {
9677                            inference::rms_norm_into(
9678                                &attn[bi * hs..(bi + 1) * hs],
9679                                w,
9680                                eps,
9681                                norm_style,
9682                                &mut normed[bi * hs..(bi + 1) * hs],
9683                            );
9684                        }
9685                        attn.copy_from_slice(&normed);
9686                    }
9687                    for (dst, &a) in h.iter_mut().zip(&attn) {
9688                        *dst += a;
9689                    }
9690                }
9691                AttnKind::Bounded(w) => {
9692                    // Chunk-GEMM projections, the bounded operator per
9693                    // position over ring + chunk — never a growing KV.
9694                    let mut normed = vec![0.0f32; b * hs];
9695                    for bi in 0..b {
9696                        inference::rms_norm_into(
9697                            &h[bi * hs..(bi + 1) * hs],
9698                            &lw.input_norm,
9699                            eps,
9700                            norm_style,
9701                            &mut normed[bi * hs..(bi + 1) * hs],
9702                        );
9703                    }
9704                    let rope = self
9705                        .bounded_rope
9706                        .clone()
9707                        .expect("bounded layer without an installed rotation table");
9708                    let cfg = crate::bounded::BoundedAttnCfg {
9709                        num_heads: self.num_heads,
9710                        num_kv_heads: self.num_kv_heads,
9711                        head_dim: self.head_dim,
9712                        hidden_size: hs,
9713                        scale: self.attn_scale,
9714                        rope: &rope,
9715                        pool: pool.as_deref(),
9716                    };
9717                    let mut attn = crate::bounded::bounded_attention_batch(
9718                        &normed,
9719                        b,
9720                        w,
9721                        &mut self.kv_cache.layers[li],
9722                        &cfg,
9723                    );
9724                    if let Some(wn) = &lw.attn_out_norm {
9725                        for bi in 0..b {
9726                            inference::rms_norm_into(
9727                                &attn[bi * hs..(bi + 1) * hs],
9728                                wn,
9729                                eps,
9730                                norm_style,
9731                                &mut normed[bi * hs..(bi + 1) * hs],
9732                            );
9733                        }
9734                        attn.copy_from_slice(&normed);
9735                    }
9736                    for (dst, &a) in h.iter_mut().zip(&attn) {
9737                        *dst += a;
9738                    }
9739                    attention::recycle_buf(&mut attn);
9740                }
9741                AttnKind::Linear(w) => {
9742                    for bi in 0..b {
9743                        let normed = inference::rms_norm(
9744                            &h[bi * hs..(bi + 1) * hs],
9745                            &lw.input_norm,
9746                            eps,
9747                            norm_style,
9748                        );
9749                        vmf_phase_forward(
9750                            &normed,
9751                            w,
9752                            &self.vmf_cfg.expect("linear layer without vmf_cfg"),
9753                            &mut self.kv_cache.layers[li].linear_state,
9754                            pool.as_deref(),
9755                        )
9756                        .iter()
9757                        .enumerate()
9758                        .for_each(|(i, &a)| h[bi * hs + i] += a);
9759                    }
9760                }
9761            }
9762
9763            // ── FFN batched ──
9764            let lw = &self.weights.layers[self.phys_layer(li)];
9765            let mut post = vec![0.0f32; b * hs];
9766            for bi in 0..b {
9767                let r =
9768                    inference::rms_norm(&h[bi * hs..(bi + 1) * hs], &lw.post_norm, eps, norm_style);
9769                post[bi * hs..(bi + 1) * hs].copy_from_slice(&r);
9770            }
9771            // A restrictive per-visit FFN row lands on the activations
9772            // inside the dense arm; an all-open row costs nothing.
9773            let mask_row = task_mask
9774                .filter(|m| m.ffn_active_count(li) < self.intermediate_size)
9775                .and_then(|m| m.ffn_masks.get(li))
9776                .map(|v| v.as_slice());
9777            let attn_ns = t_attn.elapsed().as_nanos() as u64;
9778            let t_ffn = std::time::Instant::now();
9779            let mut ffn = match &lw.ffn {
9780                FfnKind::Dense(d) if !d.segs.is_empty() => {
9781                    tube_ffn(d, &post, b, pool.as_deref(), mask_row)
9782                }
9783                FfnKind::Dense(d) => dense_ffn_batch(d, &post, b, pool.as_deref(), mask_row),
9784                FfnKind::Moe(m) if self.verify_exact_moe && self.mimo_moe.is_dynamic(li, false) => {
9785                    moe_ffn_banked_rows(&mut self.mimo_moe, li, m, &post, b, hs, pool.as_deref())
9786                }
9787                FfnKind::Moe(m) if self.verify_exact_moe => {
9788                    moe_ffn_rows_exact(m, &post, b, hs, pool.as_deref())
9789                }
9790                // Keep prompt expert panels off the projection arena and
9791                // use their routes to prime the model-wide bank.
9792                FfnKind::Moe(m) if self.mimo_moe.is_dynamic(li, false) => {
9793                    let before = m.stats.borrow().clone();
9794                    let out = crate::gpu::cpu_scope(|| {
9795                        moe_ffn_batch(m, &post, b, hs, pool.as_deref(), None)
9796                    });
9797                    self.mimo_moe.prime(li, m, &before);
9798                    out
9799                }
9800                FfnKind::Moe(m) => moe_ffn_batch(m, &post, b, hs, pool.as_deref(), None),
9801                // Dual-branch layers run per position (the expert branch
9802                // reads the raw residual — nothing to batch yet).
9803                FfnKind::DenseMoe(dm) => {
9804                    let mut out = vec![0.0f32; b * hs];
9805                    for bi in 0..b {
9806                        let r = dense_moe_ffn(
9807                            dm,
9808                            &post[bi * hs..(bi + 1) * hs],
9809                            &h[bi * hs..(bi + 1) * hs],
9810                            eps,
9811                            norm_style,
9812                            pool.as_deref(),
9813                        );
9814                        out[bi * hs..(bi + 1) * hs].copy_from_slice(&r);
9815                    }
9816                    out
9817                }
9818            };
9819            if prefill_prof_on() {
9820                PREFILL_SPLIT[0].fetch_add(attn_ns, std::sync::atomic::Ordering::Relaxed);
9821                PREFILL_SPLIT[1].fetch_add(t_ffn.elapsed().as_nanos() as u64, std::sync::atomic::Ordering::Relaxed);
9822            }
9823            if let Some(w) = &lw.ffn_out_norm {
9824                for bi in 0..b {
9825                    inference::rms_norm_into(
9826                        &ffn[bi * hs..(bi + 1) * hs],
9827                        w,
9828                        eps,
9829                        norm_style,
9830                        &mut post[bi * hs..(bi + 1) * hs],
9831                    );
9832                }
9833                ffn.copy_from_slice(&post);
9834            }
9835            for (dst, &f) in h.iter_mut().zip(&ffn) {
9836                *dst += f;
9837            }
9838            if let Some(sc) = lw.layer_scale {
9839                for v in h.iter_mut() {
9840                    *v *= sc;
9841                }
9842            }
9843            // CMF_LAYER_DUMP: every position's hidden after layer li.
9844            if self.layer_dump.is_some() {
9845                for bi in 0..b {
9846                    self.dump_layer_row(start_pos + bi, li, &h[bi * hs..(bi + 1) * hs]);
9847                }
9848            }
9849            if let Ok(tp) = std::env::var("CMF_TRACE_POS") {
9850                if let Ok(t) = tp.parse::<usize>() {
9851                    if t >= start_pos && t < start_pos + b {
9852                        let bi = t - start_pos;
9853                        let row = &h[bi * hs..(bi + 1) * hs];
9854                        let n: f32 = row.iter().map(|x| x * x).sum::<f32>().sqrt();
9855                        eprintln!(
9856                            "BATCH pos {t} after layer {li}: |h| = {n:.6} h0 {:.6} h1 {:.6}",
9857                            row[0], row[1]
9858                        );
9859                    }
9860                }
9861            }
9862            // CMF_DEBUG_LAYERS=1: per-layer hidden-state health of the
9863            // LAST prompt position — the knife for "which layer type
9864            // breaks first" on a new architecture.
9865            if std::env::var("CMF_DEBUG_LAYERS").is_ok() {
9866                let row = &h[(b - 1) * hs..b * hs];
9867                let rms =
9868                    (row.iter().map(|&v| (v as f64) * (v as f64)).sum::<f64>() / hs as f64).sqrt();
9869                let mx = row.iter().fold(0f32, |m, &v| m.max(v.abs()));
9870                eprintln!(
9871                    "layer {li:>3} {:>10} ffn={:<5} rms={rms:>12.4} max={mx:>12.4}",
9872                    match &self.weights.layers[self.phys_layer(li)].attn {
9873                        AttnKind::LinearGdn(_) => "gdn",
9874                        AttnKind::Linear(_) => "vmf",
9875                        AttnKind::ShortConv(_) => "conv",
9876                        _ => "attn",
9877                    },
9878                    match &lw.ffn {
9879                        FfnKind::Moe(_) => "moe",
9880                        FfnKind::Dense(_) => "dense",
9881                        FfnKind::DenseMoe(_) => "dense+moe",
9882                    },
9883                );
9884            }
9885            // Looped Transformer: apply final norm at the end of each loop iteration.
9886            if self.is_loop_end(li) && li + 1 < self.num_layers {
9887                for bi in 0..b {
9888                    let normed = inference::rms_norm(
9889                        &h[bi * hs..(bi + 1) * hs],
9890                        &self.weights.final_norm,
9891                        eps,
9892                        norm_style,
9893                    );
9894                    h[bi * hs..(bi + 1) * hs].copy_from_slice(&normed);
9895                }
9896            }
9897            if std::env::var("CMF_TRACE_H").is_ok() {
9898                let n = h[..hs].iter().map(|v| v.abs()).sum::<f32>() / hs as f32;
9899                let mx = h[..hs].iter().fold(0.0f32, |a, &v| a.max(v.abs()));
9900                eprintln!(
9901                    "layer {li}: mean|h|={n:.4} max|h|={mx:.2} scale={:?}",
9902                    lw.layer_scale
9903                );
9904            }
9905        }
9906        crate::gpu::set_layer(-1); // lm_head/final ops outside layer-split
9907        if prefill_prof_on() {
9908            let ms = |a: &std::sync::atomic::AtomicU64| {
9909                a.load(std::sync::atomic::Ordering::Relaxed) as f64 / 1e6
9910            };
9911            let sp = &attention::ATTN_SPLIT;
9912            let (qkv_enq, qkv_wait, ca_wait, zi) = wgpu_prefill_counters();
9913            eprintln!(
9914                "prefill-split: attention {:.1} ms (proj {:.1} [qkv enqueue {:.1} wait {:.1}, \
9915                 host gate {:.1}], host loop {:.1}, attend {:.1} \
9916                 [q pack {:.1}, device {:.1} of which readback {:.1}], o-proj {:.1}), \
9917                 ffn {:.1} ms (cumulative; zi_mm GEMMs {zi})",
9918                ms(&PREFILL_SPLIT[0]),
9919                ms(&sp[0]),
9920                qkv_enq,
9921                qkv_wait,
9922                ms(&sp[6]),
9923                ms(&sp[1]),
9924                ms(&sp[2]),
9925                ms(&sp[4]),
9926                ms(&sp[5]),
9927                ca_wait,
9928                ms(&sp[3]),
9929                ms(&PREFILL_SPLIT[1]),
9930            );
9931            let [calls, attended, wo, ring, reseed, f16] = wgpu_mirror_counters();
9932            eprintln!(
9933                "prefill-mirror: {calls} calls, {attended} attended ({wo} with O on the card), \
9934                 {ring} ring refusals, {reseed} reseeds, {f16} f16 fallbacks, {} upload \
9935                 fallbacks (cumulative)",
9936                attention::MIRROR_UPLOADS.load(std::sync::atomic::Ordering::Relaxed),
9937            );
9938        }
9939        // A batched span owns a complete set of positions. Publish any
9940        // collecting→sealed transition only after every layer has finished;
9941        // callers that cross into serial/device work must see the new epoch
9942        // before this function returns.
9943        self.o1_progress();
9944        // Every layer of the chunk has appended and attended its rows.
9945        self.swa_trim_tails();
9946        h
9947    }
9948
9949    /// Embed a single token.
9950    fn embed_single(&self, id: u32) -> Vec<f32> {
9951        let mut out = vec![0.0f32; self.hidden_size];
9952        if (id as usize) < self.weights.embed_tokens.rows() {
9953            self.weights.embed_tokens.row_f32(id as usize, &mut out);
9954        }
9955        if self.embed_multiplier != 1.0 {
9956            for v in out.iter_mut() {
9957                *v *= self.embed_multiplier;
9958            }
9959        }
9960        // DeepSeek-V4's hash layers route by TOKEN ID, so the id has to
9961        // reach the forward. It rides in slot 0 (the forward re-reads the
9962        // real embedding itself from the table).
9963        if self.dsv4.is_some()
9964            || self.dsv41.is_some()
9965            || self.qwen4_exp.is_some()
9966        {
9967            let mut v = vec![0.0f32; self.hidden_size.max(1)];
9968            v[0] = id as f32;
9969            return v;
9970        }
9971        // Gemma-3n: the per-layer-embedding half needs the token ID, so
9972        // it rides appended to the embedding; the g3n forward splits it.
9973        if let Some(b) = &self.g3n {
9974            return b.0.extend_embedding(id, &out, self.pool.as_deref());
9975        }
9976        out
9977    }
9978
9979    /// A run of consecutive prefill layers on the GPU for the whole
9980    /// chunk (default-on under CMF_GPU=1; CMF_GPU_CHUNK=0 disables).
9981    /// Eligibility per layer: q8_row weights, plain full attention
9982    /// (no output gate), F32 KV, no o1/masks/gemma extras. Returns the
9983    /// first layer index NOT processed (== `li0` when the run is empty).
9984    #[cfg(target_os = "macos")]
9985    fn chunk_run_gpu(
9986        &mut self,
9987        li0: usize,
9988        h: &mut [f32],
9989        b: usize,
9990        pos0: usize,
9991        embed_ids: Option<&[u32]>,
9992        cap: usize,
9993    ) -> usize {
9994        // (The old streaming attend needed a depth bound at ~1k; the
9995        // GEMM attention scales like the CPU path and lifted it.)
9996        // CMF_GPU_CHUNK=0 disables the graph.
9997        // Gemma-4 (`metal_gemma4`) brings its own kernels for the global
9998        // layers, V norm, attention scale and sandwich (`ChunkGemma4`).
9999        // `CMF_METAL_GEMMA4_CHUNK=0` keeps its prompt on the host walk.
10000        let gemma4 = self.metal_gemma4()
10001            && std::env::var("CMF_METAL_GEMMA4_CHUNK").as_deref() != Ok("0");
10002        if !crate::gpu::enabled_here()
10003            || std::env::var("CMF_GPU_CHUNK")
10004                .map(|v| v == "0")
10005                .unwrap_or(false)
10006            || b < 32
10007            // Sliding windows with per-layer RoPE ride the chunk graph
10008            // (`metal_graph_swa`: causal_softmax_win, per-layer tables).
10009            || (self.swa.is_some() && !self.metal_graph_swa() && !gemma4)
10010            || (self.global_attn.is_some() && !gemma4)
10011            // per-layer KV heads, narrow V, learned sinks (MiMo-V2)
10012            || (self.graph_attn_decline_reason().is_some() && !self.metal_graph_swa() && !gemma4)
10013            // Collection owns the exact Q trace and boundary conversion;
10014            // this chunk graph appends dense KV without feeding that trace.
10015            || self.o1_active()
10016            || (self.attn_v_norm && !gemma4)
10017            || !self.metal_rope_scale_ok()
10018            || ((self.attn_scale - 1.0 / (self.head_dim as f32).sqrt()).abs() > 1e-9 && !gemma4)
10019        {
10020            return li0;
10021        }
10022        let Some(model) = self.model.clone() else {
10023            return li0;
10024        };
10025        let (nh, nkv, hd, hs) = (
10026            self.num_heads,
10027            self.num_kv_heads,
10028            self.head_dim,
10029            self.hidden_size,
10030        );
10031        // Collect the longest run of consecutive eligible layers.
10032        // Looped Transformer: stop at the loop boundary so the CPU can
10033        // apply loop_final_norm between iterations.
10034        let loop_end = if self.loop_final_norm {
10035            ((li0 / self.physical_layers) + 1) * self.physical_layers
10036        } else {
10037            self.num_layers
10038        };
10039        let run_end = self.num_layers.min(loop_end).min(cap);
10040        // Each layer's own RoPE table (Spark-X2.5: sliding and full layers
10041        // differ); the global one for every model without sliding layers.
10042        // Declared before `layers`, which borrows it (and drops a routing
10043        // closure on MoE layers).
10044        let tables: Vec<std::sync::Arc<Vec<f32>>> =
10045            (li0..run_end).map(|li| self.layer_inv_freq(li)).collect();
10046        let mut layers: Vec<crate::gpu_metal::ChunkLayer> = Vec::new();
10047        let mut stored_at: Vec<usize> = Vec::new();
10048        for li in li0..run_end {
10049            let lw = &self.weights.layers[self.phys_layer(li)];
10050            if (lw.attn_out_norm.is_some() || lw.ffn_out_norm.is_some() || lw.layer_scale.is_some())
10051                && !gemma4
10052            {
10053                break;
10054            }
10055            let AttnKind::Full {
10056                wq,
10057                wk,
10058                wv,
10059                wo,
10060                q_norm,
10061                k_norm,
10062                output_gate: false,
10063                softplus_gate,
10064                bias,
10065            } = &lw.attn
10066            else {
10067                break;
10068            };
10069            // Spark-X2.5's per-head sigmoid g_proj gate, from f32 rows.
10070            let head_gate = match softplus_gate {
10071                None => None,
10072                Some((g, true)) if self.proj_gate_sigmoid => match g.f32_parts() {
10073                    Some((d, r, c)) if r == nh && c == hs => Some(d),
10074                    _ => break,
10075                },
10076                Some(_) => break,
10077            };
10078            // The FFN: a dense SiLU/GELU trio, or routed q4tp experts
10079            // (Mellum2.1: softmax router, no shared expert) whose first
10080            // trio stands in for the shape checks.
10081            let (gate_t, up_t, down_t, ffn_act, moe) = match &lw.ffn {
10082                FfnKind::Dense(d)
10083                    if (matches!(d.act, Act::Silu | Act::Gelu)
10084                        || (gemma4 && d.act == Act::GeluTanh))
10085                        && d.segs.is_empty() =>
10086                {
10087                    (&d.gate_proj, &d.up_proj, &d.down_proj, d.act, None)
10088                }
10089                FfnKind::Moe(m) => match chunk_moe_parts(m, hs) {
10090                    Some((router, experts, q8)) => {
10091                        let e0 = &m.experts[0];
10092                        let route = Box::new(move |lg: &[f32]| {
10093                            // `moe_ffn_batch`'s routing and weights, row by row.
10094                            let (idx, p, wsum) = moe_route(lg, m, None);
10095                            let mut st = m.stats.borrow_mut();
10096                            if st.len() < m.experts.len() {
10097                                st.resize(m.experts.len(), 0);
10098                            }
10099                            for &e in &idx {
10100                                st[e] += 1;
10101                            }
10102                            let w = idx.iter().map(|&e| p[e] / wsum).collect();
10103                            (idx, w)
10104                        });
10105                        (
10106                            &e0.gate_proj,
10107                            &e0.up_proj,
10108                            &e0.down_proj,
10109                            Act::Silu,
10110                            Some(crate::gpu_metal::ChunkMoe {
10111                                router,
10112                                experts,
10113                                q8,
10114                                route,
10115                            }),
10116                        )
10117                    }
10118                    None => break,
10119                },
10120                _ => break,
10121            };
10122            // q8_row (row_scale populated), q8_2f (plus its input field),
10123            // or q4_tiled / q4tp (row_scale empty — their scales are in the
10124            // payload). Mixing across the seven projections of one layer is
10125            // fine; the encoder branches per weight on the tensor's dtype.
10126            // Anything else refuses.
10127            fn cw(t: &QTensor) -> Option<(usize, usize, usize, &[f32])> {
10128                t.q8_row_parts()
10129                    .or_else(|| t.q8_2f_parts().map(|(i, r, c, rs, _)| (i, r, c, rs)))
10130                    .or_else(|| t.q4t_parts().map(|(i, r, c)| (i, r, c, &[][..])))
10131                    .or_else(|| t.q4tp_parts().map(|(i, r, c)| (i, r, c, &[][..])))
10132            }
10133            fn cf(t: &QTensor) -> &[f32] {
10134                t.q8_2f_parts().map_or(&[][..], |(_, _, _, _, cf)| cf)
10135            }
10136            let col = [cf(wq), cf(wk), cf(wv), cf(wo)];
10137            // q8_2f projections ride the chunk graph on the MoE layers it
10138            // was measured on (Mellum2.1); dense q8_2f stacks keep the host
10139            // walk until they are measured too. That includes a q8_2f
10140            // gate/up/down behind q4/q8_row attention: the chunk FFN has no
10141            // x·col staging, so it would run those weights without their
10142            // input field (0.8.14 refused every q8_2f weight here).
10143            // Gemma-4 reads every q8_2f weight through the blob GEMM with its
10144            // own field (`ChunkGemma4`), so its dense q8_2f stack rides.
10145            if moe.is_none()
10146                && !gemma4
10147                && (col.iter().any(|c| !c.is_empty())
10148                    || [gate_t, up_t, down_t].iter().any(|t| !cf(t).is_empty()))
10149            {
10150                break;
10151            }
10152            let parts = (
10153                cw(wq),
10154                cw(wk),
10155                cw(wv),
10156                cw(wo),
10157                cw(gate_t),
10158                cw(up_t),
10159                cw(down_t),
10160            );
10161            let (Some(pq), Some(pk), Some(pv), Some(po), Some(pg), Some(pu), Some(pd)) = parts
10162            else {
10163                break;
10164            };
10165            let layer = &self.kv_cache.layers[li];
10166            if layer.mode != crate::kv_cache::KvMode::F32 || layer.o1.is_some() {
10167                break;
10168            }
10169            // A trimmed tail holds every row the chunk's first query reads.
10170            debug_assert!(
10171                layer.base() == 0
10172                    || self
10173                        .layer_window(li)
10174                        .is_some_and(|w| layer.head_len(0) + 1 >= w)
10175            );
10176            stored_at.push(layer.head_len(0));
10177            // Per-layer geometry for Gemma-4 (512-wide global layers over
10178            // one KV head); every other model keeps the pipeline-wide one.
10179            let (nkv, hd) = if gemma4 {
10180                let (a, bb, _) = self.layer_geom(li);
10181                (a, bb)
10182            } else {
10183                (nkv, hd)
10184            };
10185            layers.push(crate::gpu_metal::ChunkLayer {
10186                model: &model,
10187                kv_id: self.graph_kv_id,
10188                layer: li,
10189                wq: pq,
10190                wk: pk,
10191                wv: pv,
10192                wo: po,
10193                gate: pg,
10194                up: pu,
10195                down: pd,
10196                input_norm: &lw.input_norm,
10197                post_norm: &lw.post_norm,
10198                bias: bias
10199                    .as_ref()
10200                    .map(|(a, bb, cc)| (a.as_slice(), bb.as_slice(), cc.as_slice())),
10201                q_norm: q_norm.as_deref(),
10202                k_norm: k_norm.as_deref(),
10203                inv_freq: &tables[li - li0],
10204                rope_scale: self.layer_rope_scale(li),
10205                rd: self.layer_geom(li).2,
10206                nh,
10207                nkv,
10208                hd,
10209                hs,
10210                inter: gate_t.rows(),
10211                gemma: matches!(self.norm_style, cortiq_core::NormStyle::Gemma),
10212                late_qk_norm: self.qk_norm_after_rope,
10213                eps: self.rms_eps as f32,
10214                window: self.layer_window(li),
10215                head_gate,
10216                act: ffn_act.graph_act().map_or(0, |a| a.code() as u8),
10217                g4: gemma4.then(|| crate::gpu_metal::ChunkGemma4 {
10218                    v_norm: self.attn_v_norm,
10219                    scale: self.attn_scale,
10220                    attn_out_norm: lw.attn_out_norm.as_deref(),
10221                    ffn_out_norm: lw.ffn_out_norm.as_deref(),
10222                    layer_scale: lw.layer_scale,
10223                }),
10224                col,
10225                moe,
10226            });
10227        }
10228        if layers.is_empty() {
10229            return li0;
10230        }
10231        // KV row width per layer (uniform but for Gemma-4).
10232        let rows_kv: Vec<(usize, usize)> = layers.iter().map(|l| (l.nkv, l.nkv * l.hd)).collect();
10233        let mut store: Vec<(Vec<f32>, Vec<f32>, Vec<f32>)> = stored_at
10234            .iter()
10235            .zip(&rows_kv)
10236            .map(|(&st, &(_, row))| (vec![0f32; b * row], vec![0f32; b * row], vec![0f32; st + b]))
10237            .collect();
10238        let mut io: Vec<crate::gpu_metal::ChunkIo> = Vec::with_capacity(layers.len());
10239        for (i, (ok, ov, oi)) in store.iter_mut().enumerate() {
10240            let li = layers[i].layer;
10241            let layer = &self.kv_cache.layers[li];
10242            let nkv = rows_kv[i].0;
10243            io.push(crate::gpu_metal::ChunkIo {
10244                cpu_stored: stored_at[i],
10245                cpu_gen: layer.generation(),
10246                cpu_k: (0..nkv).map(|g| layer.head_keys(g)).collect(),
10247                cpu_v: (0..nkv).map(|g| layer.head_values(g)).collect(),
10248                out_k: ok,
10249                out_v: ov,
10250                imp: oi,
10251            });
10252        }
10253        let n_run = layers.len();
10254        let last = layers.last().map(|l| l.layer + 1).unwrap_or(li0);
10255        // Device-side embedding when the run starts the model and the
10256        // embedding matrix is q8_row-mapped.
10257        let ep = embed_ids.and_then(|ids| {
10258            self.weights
10259                .embed_tokens
10260                .q8_row_parts()
10261                .map(|(idx, rows, _c, rs)| crate::gpu_metal::ChunkEmbed {
10262                    idx,
10263                    rows,
10264                    row_scale: rs,
10265                    ids,
10266                    mult: self.embed_multiplier,
10267                })
10268        });
10269        if embed_ids.is_some() && ep.is_none() {
10270            return li0;
10271        }
10272        if !crate::gpu_metal::chunk_run_gpu(&layers, &mut io, h, b, pos0, ep.as_ref()) {
10273            return li0;
10274        }
10275        drop(io);
10276        drop(layers);
10277        // CPU caches stay the owners of record: append the chunk rows
10278        // and bank the importance masses per layer.
10279        for (i, (ok, ov, oi)) in store.iter().enumerate().take(n_run) {
10280            let li = li0 + i;
10281            let row = rows_kv[i].1;
10282            let layer = &mut self.kv_cache.layers[li];
10283            for bi in 0..b {
10284                layer.append(
10285                    &ok[bi * row..(bi + 1) * row],
10286                    &ov[bi * row..(bi + 1) * row],
10287                    &[],
10288                );
10289            }
10290            layer.accumulate_imp(oi);
10291        }
10292        last
10293    }
10294
10295    /// Is layer `li` a sliding-window (local-RoPE) layer? Gemma-3:
10296    /// every `pattern`-th layer is global, the rest are local.
10297    fn layer_is_local(&self, li: usize) -> bool {
10298        if let Some(layers) = &self.sliding_layers {
10299            return layers.get(li).copied().unwrap_or(false);
10300        }
10301        match self.swa {
10302            Some((_, pattern)) => (li + 1) % pattern.max(1) != 0,
10303            None => false,
10304        }
10305    }
10306
10307    /// The RoPE table for layer `li` (local layers may have their own;
10308    /// Gemma-4 global layers use the proportional padded table).
10309    fn layer_inv_freq(&self, li: usize) -> std::sync::Arc<Vec<f32>> {
10310        if self.layer_is_local(li) {
10311            if let Some(f) = &self.inv_freq_local {
10312                return f.clone();
10313            }
10314        } else if let Some(f) = &self.inv_freq_global {
10315            return f.clone();
10316        }
10317        self.inv_freq.clone()
10318    }
10319
10320    /// The attend window for layer `li` (None = full context).
10321    fn layer_window(&self, li: usize) -> Option<usize> {
10322        self.swa
10323            .and_then(|(w, _)| self.layer_is_local(li).then_some(w))
10324    }
10325
10326    /// Drop every sliding layer's rows its window can no longer read
10327    /// (`LayerKvCache::trim_window`). Call only at a safe point — after a
10328    /// complete position / pair / chunk walk, never between a layer's
10329    /// appends and its attend. One comparison per layer below the trigger.
10330    ///
10331    /// Off with an MTP head: its verify oracles and rollbacks snapshot and
10332    /// compare per-layer row counts as positions (`CMF_METAL_VERIFY_CHECK`,
10333    /// the MTP caches), which a trimmed tail would break.
10334    fn swa_trim_tails(&mut self) {
10335        let Some((slack, align)) = self.swa_trim else {
10336            return;
10337        };
10338        if self.mtp.is_some() || self.mimo_mtp.is_some() || !self.dsv4_mtp.is_empty() {
10339            return;
10340        }
10341        for li in 0..self.num_layers.min(self.kv_cache.layers.len()) {
10342            if let Some(w) = self.layer_window(li) {
10343                self.kv_cache.layers[li].trim_window(w, slack, align);
10344            }
10345        }
10346    }
10347
10348    fn layer_num_heads(&self, li: usize) -> usize {
10349        self.attention_heads_per_layer
10350            .as_ref()
10351            .and_then(|v| v.get(li).copied())
10352            .unwrap_or(self.num_heads)
10353    }
10354
10355    fn layer_rope_scale(&self, li: usize) -> f32 {
10356        if self.layer_is_local(li) {
10357            self.rope_scale_local
10358        } else {
10359            self.rope_scale
10360        }
10361    }
10362
10363    /// Attention geometry of layer `li`: (num_kv_heads, head_dim,
10364    /// rotary_dim). Gemma-4 global layers override all three.
10365    fn layer_geom(&self, li: usize) -> (usize, usize, usize) {
10366        if !self.layer_is_local(li) {
10367            if let Some((ghd, gkv)) = self.global_attn {
10368                return (gkv, ghd, ghd);
10369            }
10370        }
10371        (
10372            self.layer_num_kv_heads(li),
10373            self.head_dim,
10374            if self.layer_is_local(li) {
10375                self.rotary_dim_local.unwrap_or(self.rotary_dim)
10376            } else {
10377                self.rotary_dim
10378            },
10379        )
10380    }
10381
10382    /// KV heads of layer `li` (virtual index): the per-layer count when the
10383    /// model has one (MiMo-V2), else the uniform `num_kv_heads`.
10384    fn layer_num_kv_heads(&self, li: usize) -> usize {
10385        self.kv_heads_per_layer
10386            .as_ref()
10387            .and_then(|v| v.get(self.phys_layer(li)).copied())
10388            .unwrap_or(self.num_kv_heads)
10389    }
10390
10391    /// V head width of layer `li` (≤ its head_dim).
10392    fn layer_v_dim(&self, li: usize) -> usize {
10393        let (_, hd, _) = self.layer_geom(li);
10394        self.v_head_dim.unwrap_or(hd).min(hd)
10395    }
10396
10397    /// Install a per-layer KV geometry: KV heads per PHYSICAL layer and/or
10398    /// a V head width narrower than `head_dim` (MiMo-V2). Validates it and
10399    /// reshapes the caches of every layer whose KV head count differs from
10400    /// `num_kv_heads`. The loader and the tests share this one path, so a
10401    /// hand-built pipeline cannot hold a geometry the loader would refuse.
10402    /// Call before the first forward (it drops cached rows of reshaped
10403    /// layers). Refuses combinations whose paths would read it wrong:
10404    /// Gemma-4 global layers and MLA carry their own geometry.
10405    pub fn set_attn_geometry(
10406        &mut self,
10407        kv_heads_per_layer: Option<Vec<usize>>,
10408        v_head_dim: Option<usize>,
10409    ) -> Result<(), String> {
10410        if kv_heads_per_layer.is_some() || v_head_dim.is_some() {
10411            if self.global_attn.is_some() {
10412                return Err(
10413                    "per-layer KV heads / v_head_dim cannot combine with Gemma-4 global \
10414                     attention geometry"
10415                        .into(),
10416                );
10417            }
10418            if self
10419                .weights
10420                .layers
10421                .iter()
10422                .any(|lw| matches!(lw.attn, AttnKind::Mla(_)))
10423            {
10424                return Err("per-layer KV heads / v_head_dim cannot combine with MLA".into());
10425            }
10426        }
10427        if let Some(vd) = v_head_dim {
10428            if vd == 0 || vd > self.head_dim {
10429                return Err(format!(
10430                    "v_head_dim {vd} must be in 1..={} (head_dim)",
10431                    self.head_dim
10432                ));
10433            }
10434        }
10435        if let Some(v) = &kv_heads_per_layer {
10436            if v.len() != self.physical_layers {
10437                return Err(format!(
10438                    "kv_heads_per_layer has {} entries, expected {} layers",
10439                    v.len(),
10440                    self.physical_layers
10441                ));
10442            }
10443            for (li, &nkv) in v.iter().enumerate() {
10444                let is_attn = matches!(
10445                    self.weights.layers.get(li).map(|lw| &lw.attn),
10446                    Some(AttnKind::Full { .. }) | None
10447                );
10448                if !is_attn {
10449                    continue;
10450                }
10451                let nh = self
10452                    .attention_heads_per_layer
10453                    .as_ref()
10454                    .and_then(|h| h.get(li).copied())
10455                    .unwrap_or(self.num_heads);
10456                if nkv == 0 || nh % nkv != 0 {
10457                    return Err(format!(
10458                        "layer {li}: {nkv} KV heads must be nonzero and divide {nh} Q heads"
10459                    ));
10460                }
10461            }
10462        }
10463        self.kv_heads_per_layer = kv_heads_per_layer;
10464        self.v_head_dim = v_head_dim.filter(|&vd| vd != self.head_dim);
10465        if self.kv_heads_per_layer.is_some() {
10466            for li in 0..self.kv_cache.layers.len() {
10467                let full = matches!(
10468                    self.weights
10469                        .layers
10470                        .get(self.phys_layer(li))
10471                        .map(|lw| &lw.attn),
10472                    Some(AttnKind::Full { .. })
10473                );
10474                let nkv = self.layer_num_kv_heads(li);
10475                let cache = &self.kv_cache.layers[li];
10476                if full && (cache.num_kv_heads != nkv || cache.head_dim != self.head_dim) {
10477                    let sinks = cache.sinks.clone();
10478                    self.kv_cache.layers[li] =
10479                        crate::kv_cache::LayerKvCache::new(nkv, self.head_dim);
10480                    self.kv_cache.layers[li].sinks = sinks;
10481                }
10482            }
10483        }
10484        Ok(())
10485    }
10486
10487    /// Attach learned attention-sink logits (one per Q head) to PHYSICAL
10488    /// layer `phys` — every virtual layer that runs it. The loader calls
10489    /// this for each `model.layers.N.self_attn.sinks` tensor.
10490    pub fn set_layer_sinks(&mut self, phys: usize, sinks: Vec<f32>) -> Result<(), String> {
10491        let Some(lw) = self.weights.layers.get(phys) else {
10492            return Err(format!("sinks for layer {phys}: no such layer"));
10493        };
10494        if !matches!(lw.attn, AttnKind::Full { .. }) {
10495            return Err(format!(
10496                "sinks for layer {phys}: only softmax (Full) attention layers take sinks"
10497            ));
10498        }
10499        let nh = self
10500            .attention_heads_per_layer
10501            .as_ref()
10502            .and_then(|h| h.get(phys).copied())
10503            .unwrap_or(self.num_heads);
10504        if sinks.len() != nh {
10505            return Err(format!(
10506                "sinks for layer {phys}: {} values, expected one per Q head ({nh})",
10507                sinks.len()
10508            ));
10509        }
10510        if let Some(bad) = sinks.iter().find(|v| !v.is_finite()) {
10511            return Err(format!("sinks for layer {phys}: non-finite value {bad}"));
10512        }
10513        for li in 0..self.kv_cache.layers.len() {
10514            if self.phys_layer(li) == phys {
10515                self.kv_cache.layers[li].sinks = Some(sinks.clone());
10516            }
10517        }
10518        Ok(())
10519    }
10520
10521    /// Why the GPU attention graphs cannot serve this model, if they
10522    /// cannot: the wgpu whole-token and batched graphs, the greedy
10523    /// multi-burst, the q1 attention dropin and the Metal block/chunk/rows
10524    /// graphs all assume ONE (num_kv_heads, head_dim) geometry, V heads as
10525    /// wide as K, a single RoPE table, full-context attention and a plain
10526    /// softmax. A model outside that contract runs on the CPU layer walk
10527    /// (and the per-op GPU matvecs) until a graph learns it — never on a
10528    /// graph that would read it wrong. None = no attention-level reason
10529    /// (the graph builders still check weights and layer kinds).
10530    pub fn graph_attn_decline_reason(&self) -> Option<&'static str> {
10531        if self.kv_heads_per_layer.is_some() {
10532            return Some("per-layer KV head counts");
10533        }
10534        if self.v_head_dim.is_some_and(|vd| vd != self.head_dim) {
10535            return Some("V heads narrower than Q/K heads");
10536        }
10537        if self.kv_cache.layers.iter().any(|l| l.sinks.is_some()) {
10538            return Some("learned attention sinks");
10539        }
10540        if self.swa.is_some() || self.sliding_layers.is_some() {
10541            return Some("sliding-window layers");
10542        }
10543        None
10544    }
10545
10546    /// Can the Metal kernels apply this model's RoPE amplitude? Every
10547    /// rotation kernel (`attn_rope_qkn[_b]`, `chunk_rope_kv`) multiplies the
10548    /// rotated pair by the layer's `rope_scale` (YaRN `attention_factor`,
10549    /// Mellum2.1's global layers) exactly where the CPU's
10550    /// `rope_rotate_scaled` does. With the Q/K norm AFTER RoPE the kernels'
10551    /// shared sum of squares would be the unscaled one, so that pairing
10552    /// (no known model) declines.
10553    #[cfg(target_os = "macos")]
10554    fn metal_rope_scale_ok(&self) -> bool {
10555        !(self.qk_norm_after_rope && (0..self.num_layers).any(|li| self.layer_rope_scale(li) != 1.0))
10556    }
10557
10558    /// Can the Metal block graph carry this model's sliding-window layers?
10559    /// Both of its attention forms take a layer's own window, RoPE table
10560    /// and rotary width: the device attend (`AttnDeviceParams::window`, the
10561    /// layer's `inv_freq`/`rd`) and the sandwich's host attend (exactly the
10562    /// CPU path's attention). True when the sliding window is the model's
10563    /// only attention-level decline (Spark-X2.5); per-layer KV heads,
10564    /// narrow V, sinks and Gemma-4 global geometry still decline; a YaRN
10565    /// amplitude per layer rides along (`metal_rope_scale_ok`).
10566    ///
10567    /// Per-layer Q heads (Laguna) and capped scores (Gemma-2) decline here
10568    /// too, with V norm (Gemma-4): the chunk prefill's own gate checks
10569    /// neither of the first two, and before this door opened its `swa`
10570    /// refusal kept every sliding model with them off the chunk graph.
10571    #[cfg(target_os = "macos")]
10572    fn metal_graph_swa(&self) -> bool {
10573        (self.swa.is_some() || self.sliding_layers.is_some())
10574            && self.attention_heads_per_layer.is_none()
10575            && !self.attn_v_norm
10576            && self.attn_softcap == 0.0
10577            && self.kv_heads_per_layer.is_none()
10578            && self.v_head_dim.map_or(true, |vd| vd == self.head_dim)
10579            && !self.kv_cache.layers.iter().any(|l| l.sinks.is_some())
10580            && self.global_attn.is_none()
10581            && self.inv_freq_global.is_none()
10582            && self.metal_rope_scale_ok()
10583            && !(0..self.num_layers).any(|li| {
10584                self.layer_is_local(li)
10585                    && self.inv_freq_local.is_none()
10586                    && self.rotary_dim_local.is_some_and(|r| r != self.rotary_dim)
10587            })
10588    }
10589
10590    /// Can the Metal graphs carry this Gemma-4 model (`gpu_metal::gemma4`)?
10591    /// Its sliding layers (window, local RoPE table, 256-wide heads)
10592    /// device-attend like Spark-X2.5's; the 512-wide global layers (one KV
10593    /// head, proportional RoPE in the zero-padded table) take the 16-slot
10594    /// kernels; the V norm, sandwich norms, layer scalar and tanh-GELU come
10595    /// from the Gemma-4 library. Dense FFNs only (no Gemma-4 MoE measured).
10596    /// `CMF_METAL_GEMMA4=0` declines it as before (CPU attention).
10597    #[cfg(target_os = "macos")]
10598    fn metal_gemma4(&self) -> bool {
10599        crate::gpu_metal::gemma4::enabled()
10600            && (self.global_attn.is_some() || self.attn_v_norm)
10601            && self.attention_heads_per_layer.is_none()
10602            && self.kv_heads_per_layer.is_none()
10603            && self.v_head_dim.is_none()
10604            && !self.kv_cache.layers.iter().any(|l| l.sinks.is_some())
10605            && self.attn_softcap == 0.0
10606            && !self.qk_norm_after_rope
10607            && self.metal_rope_scale_ok()
10608            && self
10609                .weights
10610                .layers
10611                .iter()
10612                .all(|lw| matches!(&lw.ffn, FfnKind::Dense(d) if d.segs.is_empty()))
10613            && !(0..self.num_layers).any(|li| {
10614                self.layer_is_local(li)
10615                    && self.inv_freq_local.is_none()
10616                    && self.rotary_dim_local.is_some_and(|r| r != self.rotary_dim)
10617            })
10618    }
10619
10620    /// Why the WGPU graphs (whole-token, batched prefill, greedy burst)
10621    /// cannot run this model's attention, if they cannot. Per-layer KV
10622    /// heads, V narrower than K, learned sinks and sliding windows ride
10623    /// their per-layer geometry (`GraphAttnGeom`, the ATTEND_X kernels);
10624    /// what that geometry does not express keeps the decline, by name.
10625    /// None for every model with one attention geometry.
10626    pub fn wgpu_graph_attn_decline(&self) -> Option<&'static str> {
10627        // Keep the ordinary (uniform) fast arms off YaRN until they carry a
10628        // scale field too. A non-uniform geometry gets a `GraphAttnGeom` and
10629        // is handled below by the generic graph kernels.
10630        let geometry_reason = self.graph_attn_decline_reason();
10631        let has_scaled_rope = (0..self.num_layers).any(|li| self.layer_rope_scale(li) != 1.0);
10632        if has_scaled_rope && geometry_reason.is_none() {
10633            return Some("scaled RoPE without per-layer geometry");
10634        }
10635        // For Q/K norm after partial RoPE, the scale changes only the rotary
10636        // slice's RMS denominator. The generic graph deliberately declines
10637        // that uncommon combination until it carries a separate rotary sum;
10638        // Mellum has q/k norm before RoPE, so it remains on this path.
10639        if has_scaled_rope && self.qk_norm_after_rope {
10640            return Some("scaled RoPE with post-RoPE Q/K norm");
10641        }
10642        geometry_reason?;
10643        // Gemma-4: global layers 512 wide over 256-wide sliding ones
10644        // (`GraphAttnGeom::hd`), the scale-less V norm (`v_norm`), and the
10645        // sandwich norms + layer scalar (`GraphLayer`) ride the TOKEN graph.
10646        // The batch graph has none of the three and refuses them itself
10647        // (`batch_graph_gemma_decline`).
10648        if self.attention_heads_per_layer.is_some() {
10649            return Some("per-layer Q head counts with per-layer geometry");
10650        }
10651        // Sandwich norms are measured on Gemma-4 only (the humanizer port,
10652        // against HF transformers); Gemma-2/3 keep the per-op path until
10653        // they are measured the same way.
10654        let gemma4 = self.global_attn.is_some() || self.attn_v_norm;
10655        if !gemma4
10656            && self.weights.layers.iter().any(|lw| {
10657                lw.attn_out_norm.is_some() || lw.ffn_out_norm.is_some() || lw.layer_scale.is_some()
10658            })
10659        {
10660            return Some("sandwich norms / layer scale with per-layer geometry");
10661        }
10662        // `GraphAttnGeom::rope_scale` carries YaRN's post-rotation
10663        // attention-amplitude factor to the WGPU RoPE kernel. It is not an
10664        // angular position scale, so it must not be folded into `invf`.
10665        if self.weights.layers.iter().any(|lw| {
10666            matches!(
10667                &lw.attn,
10668                AttnKind::Full {
10669                    output_gate: true,
10670                    ..
10671                }
10672            )
10673        }) && self.v_head_dim.is_some()
10674        {
10675            return Some("gated attention with V narrower than K");
10676        }
10677        if (0..self.num_layers).any(|li| {
10678            self.layer_is_local(li)
10679                && self.inv_freq_local.is_none()
10680                && self.rotary_dim_local.is_some_and(|r| r != self.rotary_dim)
10681        }) {
10682            return Some("local rotary width without a local RoPE table");
10683        }
10684        None
10685    }
10686
10687    /// What the wgpu BATCH graph (and the attention-only singleton graphs)
10688    /// cannot run of Gemma-4, which the token graph does run: per-layer
10689    /// head widths, the V norm, sandwich norms and the layer scalar.
10690    fn batch_graph_gemma_decline(&self) -> Option<&'static str> {
10691        // Gemma-4 rides `gemma_batch` (per-layer head width, V norm,
10692        // sandwich norms, layer scalar) unless CMF_G4_BATCH=0.
10693        if (self.global_attn.is_some() || self.attn_v_norm) && crate::gpu::g4_batch_on() {
10694            return None;
10695        }
10696        if self.global_attn.is_some() {
10697            return Some("per-layer head width (Gemma-4 global layers)");
10698        }
10699        if self.attn_v_norm {
10700            return Some("V norm");
10701        }
10702        if self.weights.layers.iter().any(|lw| {
10703            lw.attn_out_norm.is_some() || lw.ffn_out_norm.is_some() || lw.layer_scale.is_some()
10704        }) {
10705            return Some("sandwich norms / layer scale");
10706        }
10707        None
10708    }
10709
10710    /// The wgpu graphs' attention geometry for layer `li` (virtual index):
10711    /// Some only for a model whose layers do not share one (MiMo-V2) — KV
10712    /// heads, V width, rotary width and RoPE table, window and sinks of
10713    /// THIS layer, exactly what the CPU attention reads for it.
10714    fn graph_attn_geom(&self, li: usize) -> Option<crate::gpu::GraphAttnGeom<'_>> {
10715        self.graph_attn_decline_reason()?;
10716        let (nkv, hd, rd) = self.layer_geom(li);
10717        let invf: &[f32] = if self.layer_is_local(li) {
10718            match &self.inv_freq_local {
10719                Some(f) => f.as_slice(),
10720                None => self.inv_freq.as_slice(),
10721            }
10722        } else {
10723            match &self.inv_freq_global {
10724                Some(f) => f.as_slice(),
10725                None => self.inv_freq.as_slice(),
10726            }
10727        };
10728        Some(crate::gpu::GraphAttnGeom {
10729            nkv,
10730            hd,
10731            dv: self.layer_v_dim(li),
10732            rd,
10733            invf,
10734            rope_scale: self.layer_rope_scale(li),
10735            window: self.layer_window(li),
10736            sink: self.kv_cache.layers[li].sinks.as_deref(),
10737            v_norm: self.attn_v_norm,
10738        })
10739    }
10740
10741    /// The decode graph's device K/V mirror that layer `li`'s batched
10742    /// prefill appends its chunk to and attends against (wgpu), instead
10743    /// of uploading the layer's whole K/V prefix every chunk and the whole
10744    /// cache again at the first decode token. Only where the token graph
10745    /// will read that very mirror: the decode graph on, not refused, and
10746    /// this layer's geometry what it requests (`graph_attn_geom`: KV heads,
10747    /// V as wide as K, the window as a ring, no sink).
10748    ///
10749    /// Spark-X2.5 only (`proj_gate_sigmoid`), the one architecture whose
10750    /// output was measured identical on it. `CMF_PREFILL_MIRROR=0` keeps
10751    /// the host upload (A/B).
10752    #[cfg(feature = "gpu")]
10753    fn prefill_mirror_target(&self, li: usize) -> Option<crate::gpu::PrefillMirror> {
10754        if !self.proj_gate_sigmoid
10755            || std::env::var("CMF_PREFILL_MIRROR").as_deref() == Ok("0")
10756            || !crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Decode)
10757            || self.graph_refused()
10758            || self.o1_active()
10759            || self.attn_softcap > 0.0
10760            || self.wgpu_graph_attn_decline().is_some()
10761        {
10762            return None;
10763        }
10764        let g = self.graph_attn_geom(li)?;
10765        let (nkv, hd, _) = self.layer_geom(li);
10766        // The token graph passes the call-wide head width (layer 0's).
10767        if g.nkv != nkv || g.dv != hd || g.sink.is_some() || hd != self.layer_geom(0).1 {
10768            return None;
10769        }
10770        Some(crate::gpu::PrefillMirror {
10771            kv_id: self.graph_kv_id,
10772            layer: li,
10773            limit: self.kv_cache.max_seq_len,
10774        })
10775    }
10776
10777    /// Bring the host KV cache of every Full-attention layer in
10778    /// `[from, upto)` up to `position` rows from the wgpu mirrors, where a
10779    /// device graph advanced a layer that the host is about to run: a
10780    /// device prefix that shrank since the prompt (or a batched prefill
10781    /// prefix longer than the decode one). A layer whose mirror does not
10782    /// hold the missing rows is left alone. Rows a sliding layer's ring
10783    /// no longer holds come back as zeros — outside every window that
10784    /// will read them.
10785    #[cfg(feature = "gpu")]
10786    fn pull_lagging_host_kv(&mut self, from: usize, upto: usize, position: usize) {
10787        let kv_id = self.graph_kv_id;
10788        for li in from..upto.min(self.num_layers) {
10789            if !matches!(
10790                self.weights.layers[self.phys_layer(li)].attn,
10791                AttnKind::Full { .. }
10792            ) {
10793                continue;
10794            }
10795            // Absolute: a trimmed sliding tail holds positions base.. only.
10796            let host = self.kv_cache.layers[li].pos_len();
10797            if host >= position {
10798                continue;
10799            }
10800            let Some(dev) = crate::gpu::graph_kv_stored(kv_id, li) else {
10801                continue;
10802            };
10803            let to = dev.min(position);
10804            if to <= host {
10805                continue;
10806            }
10807            let (nkv, hd) = {
10808                let c = &self.kv_cache.layers[li];
10809                (c.num_kv_heads, c.head_dim)
10810            };
10811            let Some((k, v, first_valid)) =
10812                crate::gpu::graph_kv_pull_host(kv_id, li, host, to, nkv, hd)
10813            else {
10814                continue;
10815            };
10816            // A sliding layer only ever reads its last `window` rows; a
10817            // full-context layer needs every row it did not have.
10818            let need_from = match self.layer_window(li) {
10819                Some(w) => host.max((position + 1).saturating_sub(w)),
10820                None => host,
10821            };
10822            if first_valid > need_from {
10823                tracing::warn!(
10824                    "layer {li}: device KV rows {host}..{to} no longer resident \
10825                     (from {first_valid}); host attention will miss them"
10826                );
10827            }
10828            let row = nkv * hd;
10829            let cache = &mut self.kv_cache.layers[li];
10830            for p in 0..to - host {
10831                cache.append(&k[p * row..(p + 1) * row], &v[p * row..(p + 1) * row], &[]);
10832            }
10833        }
10834    }
10835
10836    /// Log (once per graph site and pipeline) that `site` declined for
10837    /// `reason`. The lines are kept so a caller or a test can read them.
10838    fn note_graph_decline(&self, site: &'static str, reason: &'static str) {
10839        let mut seen = self.graph_declines.borrow_mut();
10840        if !seen.iter().any(|&(s, r)| s == site && r == reason) {
10841            tracing::warn!("{site} declined: {reason} (CPU attention path)");
10842            seen.push((site, reason));
10843        }
10844    }
10845
10846    /// The GPU-graph declines this pipeline has logged so far, as the
10847    /// logged lines.
10848    pub fn graph_declines(&self) -> Vec<String> {
10849        self.graph_declines
10850            .borrow()
10851            .iter()
10852            .map(|(site, reason)| format!("{site} declined: {reason} (CPU attention path)"))
10853            .collect()
10854    }
10855
10856    /// Does layer `li` have the plain attention geometry the historical
10857    /// head-masked f32 path (`multi_head_attention`) assumes — pipeline-wide
10858    /// KV heads / head_dim / RoPE table, full context, no sink, V as wide
10859    /// as K? Anything else runs the dense `qwen_attention` instead.
10860    /// `CMF_LAYER_DUMP` writer (see `Pipeline::layer_dump`): one position's
10861    /// hidden after layer `li` as raw little-endian f32 into
10862    /// `<dir>/p{pos:06}_l{li:02}.f32`. A failed write is reported once and
10863    /// never stops the forward — the dump is a diagnostic.
10864    fn dump_layer_row(&self, pos: usize, li: usize, row: &[f32]) {
10865        let Some(dir) = &self.layer_dump else {
10866            return;
10867        };
10868        let mut bytes = Vec::with_capacity(row.len() * 4);
10869        for v in row {
10870            bytes.extend_from_slice(&v.to_le_bytes());
10871        }
10872        let path = dir.join(format!("p{pos:06}_l{li:02}.f32"));
10873        if let Err(e) = std::fs::create_dir_all(dir).and_then(|_| std::fs::write(&path, &bytes)) {
10874            use std::sync::atomic::{AtomicBool, Ordering};
10875            static SAID: AtomicBool = AtomicBool::new(false);
10876            if !SAID.swap(true, Ordering::Relaxed) {
10877                tracing::error!("CMF_LAYER_DUMP: cannot write {}: {e}", path.display());
10878            }
10879        }
10880    }
10881
10882    /// Decide the MiMo-V2 expert placement once (`crate::mimo_moe`). Any
10883    /// other model turns the slot off on the first call.
10884    fn mimo_moe_prepare(&mut self) {
10885        if !self.mimo_moe.is_undecided() {
10886            return;
10887        }
10888        let slot = {
10889            let layers: Vec<(usize, &MoeFfn)> = (0..self.num_layers)
10890                .filter_map(
10891                    |li| match &self.weights.layers.get(self.phys_layer(li))?.ffn {
10892                        FfnKind::Moe(m) => Some((li, m)),
10893                        _ => None,
10894                    },
10895                )
10896                .collect();
10897            // One bank lives on one device: an in-process multi-GPU split
10898            // keeps the whole-layer path.
10899            if layers.is_empty()
10900                || self.physical_layers != self.num_layers
10901                || self.gpu_plan.is_some()
10902            {
10903                crate::mimo_moe::Slot::Off
10904            } else {
10905                // Whether a whole-token graph could run this model's layers
10906                // (then a whole-layer prefix is one submit, not per-layer
10907                // fences).
10908                let graph_prefix =
10909                    self.wgpu_graph_attn_decline().is_none() && crate::gpu::wgpu_graph_default();
10910                crate::mimo_moe::Slot::decide(&layers, self.num_layers, graph_prefix)
10911            }
10912        };
10913        self.mimo_moe = slot;
10914    }
10915
10916    #[cfg(test)]
10917    pub(crate) fn test_graph_kv_id(&self) -> u64 {
10918        self.graph_kv_id
10919    }
10920
10921    /// Dynamic MiMo layer: one device attention graph, followed by a bank
10922    /// frame. Both decode and short verification use this same attention
10923    /// path and absolute layer key; the host KV may intentionally lag.
10924    pub(crate) fn mimo_graph_layer_rows(
10925        &mut self,
10926        li: usize,
10927        h: &mut [f32],
10928        positions: &[usize],
10929    ) -> crate::gpu::BatchGraphOutcome {
10930        use crate::gpu::BatchGraphOutcome as Out;
10931        let b = positions.len();
10932        if !(1..=4).contains(&b)
10933            || h.len() != b * self.hidden_size
10934            || !self.mimo_moe.is_dynamic(li, true)
10935            || !crate::gpu::enabled_here()
10936            || !crate::gpu::wgpu_active()
10937            || self.o1_active()
10938            || self.physical_layers != self.num_layers
10939            // The pair-fusion diagnostic (and an explicit graph-off run)
10940            // rewinds only host KV. A hidden singleton attention graph here
10941            // would leave device mirrors ahead of the next host position.
10942            || std::env::var("CMF_GPU_WGPU_GRAPH").as_deref() == Ok("0")
10943            || std::env::var("CMF_MIMO_ATTN_GRAPH").as_deref() == Ok("0")
10944            || self.wgpu_graph_attn_decline().is_some()
10945        {
10946            return Out::Declined;
10947        }
10948        let attn_started = std::time::Instant::now();
10949        let outcome = {
10950            let lw = &self.weights.layers[li];
10951            if lw.attn_out_norm.is_some() || lw.ffn_out_norm.is_some() || lw.layer_scale.is_some() {
10952                return Out::Declined;
10953            }
10954            let FfnKind::Moe(m) = &lw.ffn else {
10955                return Out::Declined;
10956            };
10957            let AttnKind::Full {
10958                wq,
10959                wk,
10960                wv,
10961                wo,
10962                q_norm,
10963                k_norm,
10964                output_gate,
10965                softplus_gate,
10966                bias,
10967            } = &lw.attn
10968            else {
10969                return Out::Declined;
10970            };
10971            if *output_gate || softplus_gate.is_some() {
10972                return Out::Declined;
10973            }
10974            let Some(model) = wq.graph_weight().map(|(m, ..)| m.clone()).or_else(|| {
10975                m.experts
10976                    .first()?
10977                    .gate_proj
10978                    .mapped_q4tp()
10979                    .map(|(m, _)| m.clone())
10980            }) else {
10981                return Out::Declined;
10982            };
10983            fn gw<'a>(
10984                t: &'a QTensor,
10985                owner: &std::sync::Arc<cortiq_core::CmfModel>,
10986            ) -> Option<crate::gpu::GraphW<'a>> {
10987                if let Some((m, idx, kind, rs)) = t.graph_weight() {
10988                    if m.uid() != owner.uid() || t.has_prism_contract() {
10989                        return None;
10990                    }
10991                    return Some(crate::gpu::GraphW {
10992                        idx,
10993                        kind,
10994                        row_scale: rs,
10995                        data: &[],
10996                        prism: crate::gpu::GraphPrismOp::None,
10997                        affine: false,
10998                    });
10999                }
11000                t.as_f32().map(|data| crate::gpu::GraphW {
11001                    idx: 0,
11002                    kind: 4,
11003                    row_scale: &[],
11004                    data,
11005                    prism: crate::gpu::GraphPrismOp::None,
11006                    affine: false,
11007                })
11008            }
11009            let (Some(q), Some(k), Some(v), Some(o)) = (
11010                gw(wq, &model),
11011                gw(wk, &model),
11012                gw(wv, &model),
11013                gw(wo, &model),
11014            ) else {
11015                return Out::Declined;
11016            };
11017            let layer = crate::gpu::GraphLayer {
11018                input_norm: &lw.input_norm,
11019                post_norm: &lw.post_norm,
11020                attn_out_norm: None,
11021                ffn_out_norm: None,
11022                layer_scale: None,
11023                ffn: crate::gpu::GraphFfn::AttentionOnly,
11024                attn: crate::gpu::GraphAttn::Full {
11025                    wq: q,
11026                    wk: k,
11027                    wv: v,
11028                    wo: o,
11029                    q_norm: q_norm.as_deref(),
11030                    k_norm: k_norm.as_deref(),
11031                    late_qk_norm: self.qk_norm_after_rope,
11032                    bias: bias
11033                        .as_ref()
11034                        .map(|(q, k, v)| (q.as_slice(), k.as_slice(), v.as_slice())),
11035                    output_gate: false,
11036                    cpu_k: self.kv_cache.layers[li].k_heads(),
11037                    cpu_v: self.kv_cache.layers[li].v_heads(),
11038                    cpu_base: self.kv_cache.layers[li].base(),
11039                    geom: self.graph_attn_geom(li),
11040                    head_gate: None,
11041                },
11042            };
11043            let (nkv, hd, rd) = self.layer_geom(li);
11044            crate::gpu::forward_batch_graph_at(
11045                &model,
11046                self.graph_kv_id,
11047                li,
11048                &[layer],
11049                &self.inv_freq,
11050                h,
11051                self.layer_num_heads(li),
11052                nkv,
11053                hd,
11054                rd,
11055                self.hidden_size,
11056                1,
11057                positions,
11058                self.kv_cache.max_seq_len,
11059                self.norm_style == cortiq_core::NormStyle::Gemma,
11060                self.rms_eps as f32,
11061                self.attn_scale,
11062                b,
11063                &[],
11064                self.o1_epoch,
11065                None,
11066                None,
11067            )
11068        };
11069        match outcome {
11070            Out::Completed => {}
11071            Out::Declined => return Out::Declined,
11072            Out::Failed => {
11073                self.graph_failed
11074                    .store(true, std::sync::atomic::Ordering::Relaxed);
11075                return Out::Failed;
11076            }
11077        }
11078        let attn_ns = attn_started.elapsed().as_nanos() as u64;
11079        let hs = self.hidden_size;
11080        let lw = &self.weights.layers[li];
11081        let FfnKind::Moe(m) = &lw.ffn else {
11082            unreachable!()
11083        };
11084        let mut post = vec![0.0; h.len()];
11085        for (x, y) in h.chunks_exact(hs).zip(post.chunks_exact_mut(hs)) {
11086            inference::rms_norm_into(x, &lw.post_norm, self.rms_eps, self.norm_style, y);
11087        }
11088        let mut ffn = if b == 1 {
11089            moe_ffn_banked(&mut self.mimo_moe, li, m, &post, self.pool.as_deref())
11090        } else {
11091            moe_ffn_banked_rows(
11092                &mut self.mimo_moe,
11093                li,
11094                m,
11095                &post,
11096                b,
11097                hs,
11098                self.pool.as_deref(),
11099            )
11100        };
11101        for (x, &f) in h.iter_mut().zip(&ffn) {
11102            *x += f;
11103        }
11104        attention::recycle_buf(&mut ffn);
11105        if self.layer_dump.is_some() {
11106            for (&pos, row) in positions.iter().zip(h.chunks_exact(hs)) {
11107                self.dump_layer_row(pos, li, row);
11108            }
11109        }
11110        crate::mimo_moe::note_attention_graph(b, attn_ns);
11111        Out::Completed
11112    }
11113
11114    fn layer_attn_plain(&self, li: usize) -> bool {
11115        self.kv_heads_per_layer.is_none()
11116            && self.v_head_dim.is_none()
11117            && self.global_attn.is_none()
11118            && self.layer_window(li).is_none()
11119            && self.kv_cache.layers[li].sinks.is_none()
11120    }
11121
11122    /// Forward one position through all layers (hybrid dispatch).
11123    fn forward_layers(
11124        &mut self,
11125        hidden: &[f32],
11126        position: usize,
11127        task_mask: Option<&TaskMask>,
11128    ) -> Vec<f32> {
11129        let out = self.forward_layers_upto(hidden, position, task_mask, None);
11130        self.o1_progress();
11131        self.swa_trim_tails();
11132        out
11133    }
11134
11135    // ── Network pipeline-split building blocks (coordinator/worker) ──
11136    // A remote worker owns layers [from ..= upto] and their KV; the
11137    // coordinator owns the rest plus embed / final norm / head. Attention
11138    // causality is per-layer, so a whole prompt's boundary hiddens ship
11139    // as one batch and decode ships one vector per token.
11140
11141    /// Embed one token id (embed multiplier applied).
11142    pub fn embed_id(&self, id: u32) -> Vec<f32> {
11143        self.embed_single(id)
11144    }
11145
11146    /// Refuse the archs/modes whose forward cannot be cut at a layer
11147    /// boundary. Loud by design: a split that silently changed the math
11148    /// would be a chimera.
11149    pub fn split_supported(&self) -> Result<(), String> {
11150        if self.dsv4.is_some() {
11151            return Err(
11152                "network split: DeepSeek-V4 runs its own fused stack (not splittable yet)".into(),
11153            );
11154        }
11155        if self.dsv41.is_some() {
11156            return Err(
11157                "network split: DeepSeek-V4.1 owns the shared CED/CSA2 state (not splittable)"
11158                    .into(),
11159            );
11160        }
11161        if self.qwen4_exp.is_some() {
11162            return Err(
11163                "network split: Qwen3.8-Flash-Next hyper/QSA stack is not splittable yet".into(),
11164            );
11165        }
11166        if self.g3n.is_some() {
11167            return Err(
11168                "network split: Gemma-3n runs its own AltUp stack (not splittable yet)".into(),
11169            );
11170        }
11171        Ok(())
11172    }
11173
11174    /// Forward `hidden` through layers [from ..= upto] at `position`,
11175    /// appending those layers' KV/state. Both split sides call this
11176    /// over their own range; a task mask applies to the span's own
11177    /// layers (each side masks what it runs).
11178    pub fn forward_span(
11179        &mut self,
11180        hidden: &[f32],
11181        position: usize,
11182        from: usize,
11183        upto: usize,
11184        task_mask: Option<&TaskMask>,
11185    ) -> Result<Vec<f32>, String> {
11186        self.split_supported()?;
11187        if from > upto || upto >= self.num_layers {
11188            return Err(format!(
11189                "forward_span: layer range {from}..={upto} outside 0..{}",
11190                self.num_layers
11191            ));
11192        }
11193        if hidden.len() != self.hidden_size {
11194            return Err(format!(
11195                "forward_span: hidden len {} ≠ hidden_size {}",
11196                hidden.len(),
11197                self.hidden_size
11198            ));
11199        }
11200        let out = self.forward_layers_span(hidden, position, task_mask, from, Some(upto));
11201        self.o1_progress();
11202        self.swa_trim_tails();
11203        if self
11204            .graph_failed
11205            .swap(false, std::sync::atomic::Ordering::Relaxed)
11206        {
11207            self.cancel
11208                .store(false, std::sync::atomic::Ordering::Relaxed);
11209            self.clear_sequence_state();
11210            return Err("forward_span: deferred O(1) transition failed".into());
11211        }
11212        Ok(out)
11213    }
11214
11215    /// Final norm + lm_head over a boundary hidden (the final-logit
11216    /// softcap is applied by lm_head_forward itself).
11217    pub fn logits_from_hidden(&mut self, hidden: &[f32]) -> Vec<f32> {
11218        let normed = inference::rms_norm(
11219            hidden,
11220            &self.weights.final_norm,
11221            self.rms_eps,
11222            self.norm_style,
11223        );
11224        self.lm_head_forward(&normed)
11225    }
11226
11227    /// Sample the next token with this pipeline's sampler state.
11228    pub fn sample_next(&mut self, logits: &[f32], past_tokens: &[u32]) -> u32 {
11229        sampler::sample_with_scratch(
11230            logits,
11231            &self.sampler_config,
11232            past_tokens,
11233            &mut self.rng,
11234            &mut self.sampler_scratch,
11235        )
11236    }
11237
11238    /// Fresh sequence: clear KV, reuse history and device mirrors.
11239    pub fn reset_session(&mut self) {
11240        self.clear_sequence_state();
11241    }
11242
11243    /// Batched span prefill from token ids (coordinator side): embed +
11244    /// layers [0 ..= upto]; returns the boundary hiddens of ALL positions
11245    /// (ids.len() × hidden). Rides the same layer-major machinery as the
11246    /// local prefill; falls back to the per-position walk under
11247    /// CMF_PREFILL=seq.
11248    pub fn prefill_span_ids(
11249        &mut self,
11250        ids: &[u32],
11251        start_pos: usize,
11252        upto: usize,
11253        task_mask: Option<&TaskMask>,
11254    ) -> Result<Vec<f32>, String> {
11255        self.split_supported()?;
11256        if upto >= self.num_layers {
11257            return Err(format!(
11258                "prefill_span_ids: upto {upto} outside 0..{}",
11259                self.num_layers
11260            ));
11261        }
11262        // Same predicate as the whole-stack prefill: a span whose GDN
11263        // state lives on the device must walk positions through the
11264        // graph, not through the batched CPU span.
11265        if self.can_prefill_batched() && !self.graph_prefill_preferred() {
11266            let out =
11267                self.prefill_batch_span(PrefillIn::Ids(ids), start_pos, task_mask, 0, upto + 1);
11268            self.check_o1_progress_failure("prefill_span_ids")?;
11269            Ok(out)
11270        } else {
11271            let hs = self.hidden_size;
11272            let mut out = Vec::with_capacity(ids.len() * hs);
11273            for (i, &id) in ids.iter().enumerate() {
11274                let emb = self.embed_id(id);
11275                out.extend_from_slice(&self.forward_span(
11276                    &emb,
11277                    start_pos + i,
11278                    0,
11279                    upto,
11280                    task_mask,
11281                )?);
11282            }
11283            Ok(out)
11284        }
11285    }
11286
11287    /// Batched span prefill from boundary hiddens (worker side): layers
11288    /// [from ..= upto] for every position in the batch; returns the batch.
11289    pub fn prefill_span_hidden(
11290        &mut self,
11291        hidden: &[f32],
11292        start_pos: usize,
11293        from: usize,
11294        upto: usize,
11295        task_mask: Option<&TaskMask>,
11296    ) -> Result<Vec<f32>, String> {
11297        self.split_supported()?;
11298        let hs = self.hidden_size;
11299        if hidden.is_empty() || hidden.len() % hs != 0 {
11300            return Err(format!(
11301                "prefill_span_hidden: {} floats is not a multiple of hidden {hs}",
11302                hidden.len()
11303            ));
11304        }
11305        if from > upto || upto >= self.num_layers {
11306            return Err(format!(
11307                "prefill_span_hidden: layer range {from}..={upto} outside 0..{}",
11308                self.num_layers
11309            ));
11310        }
11311        if self.can_prefill_batched() && !self.graph_prefill_preferred() {
11312            let out = self.prefill_batch_span(
11313                PrefillIn::Hidden(hidden),
11314                start_pos,
11315                task_mask,
11316                from,
11317                upto + 1,
11318            );
11319            self.check_o1_progress_failure("prefill_span_hidden")?;
11320            Ok(out)
11321        } else {
11322            let b = hidden.len() / hs;
11323            let mut out = Vec::with_capacity(hidden.len());
11324            for i in 0..b {
11325                let h = self.forward_span(
11326                    &hidden[i * hs..(i + 1) * hs],
11327                    start_pos + i,
11328                    from,
11329                    upto,
11330                    task_mask,
11331                )?;
11332                out.extend_from_slice(&h);
11333            }
11334            Ok(out)
11335        }
11336    }
11337
11338    /// Build the whole-token wgpu graph for a pure-attention q1 model (every
11339    /// layer Full q1 + dense q1 FFN, no gate/bias). Returns the post-stack
11340    /// hidden (caller does final norm + lm_head), or None to fall back.
11341    fn try_token_graph_wgpu(
11342        &self,
11343        hidden: &[f32],
11344        position: usize,
11345        logits_out: &mut Vec<f32>,
11346        layers_run: &mut usize,
11347    ) -> Option<Result<Vec<f32>, ()>> {
11348        self.try_token_graph_wgpu_steps(
11349            hidden,
11350            position,
11351            logits_out,
11352            1,
11353            None,
11354            Some(layers_run),
11355            0,
11356            self.num_layers,
11357        )
11358    }
11359
11360    /// The span twin (network split): the graph covers [from..upto_excl)
11361    /// — one submit per SEGMENT per token. lm_head folds in only when
11362    /// the span reaches the last layer.
11363    fn try_token_graph_wgpu_span(
11364        &self,
11365        hidden: &[f32],
11366        position: usize,
11367        logits_out: &mut Vec<f32>,
11368        from: usize,
11369        upto_excl: usize,
11370        layers_run: &mut usize,
11371    ) -> Option<Result<Vec<f32>, ()>> {
11372        self.try_token_graph_wgpu_steps(
11373            hidden,
11374            position,
11375            logits_out,
11376            1,
11377            None,
11378            Some(layers_run),
11379            from,
11380            upto_excl,
11381        )
11382    }
11383
11384    /// Greedy burst: forward `t_next` and let the device pick + re-embed
11385    /// the next k−1 tokens — k frames, ONE submit, k ids back. The ZML
11386    /// trade, on wgpu. None ⇒ caller keeps the per-token path.
11387    fn try_multi_burst(&self, t_next: u32, position: usize, k: usize) -> Option<Vec<u32>> {
11388        if self.o1_active() || self.attn_softcap > 0.0 {
11389            return None;
11390        }
11391        // The burst builds the whole-token graph; attention the graph's
11392        // per-layer geometry cannot express keeps the per-token path.
11393        if let Some(reason) = self.wgpu_graph_attn_decline() {
11394            self.note_graph_decline("wgpu multi-burst", reason);
11395            return None;
11396        }
11397        let graph_on = crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Decode);
11398        if !graph_on || self.graph_refused() {
11399            // Same memo as the decode site: this path builds the very
11400            // same graph, so a model it cannot build for must not be
11401            // walked again here either. Missing this guard was worth
11402            // 2.5x on an Adreno — 0.361 tok/s against 0.905 — because
11403            // the burst retried per token what decode had already given
11404            // up on.
11405            return None;
11406        }
11407        let emb = self.embed_single(t_next);
11408        let mut lg = Vec::new();
11409        let mut ids = Vec::new();
11410        match self.try_token_graph_wgpu_steps(
11411            &emb,
11412            position,
11413            &mut lg,
11414            k,
11415            Some(&mut ids),
11416            None,
11417            0,
11418            self.num_layers,
11419        ) {
11420            Some(Ok(_)) => {}
11421            Some(Err(())) => {
11422                // Preserve the backend's post-admission failure through the
11423                // Option-based burst API.  The decode caller consumes this
11424                // flag and clears the sequence instead of falling through
11425                // to a stale CPU recurrent state.
11426                self.graph_failed
11427                    .store(true, std::sync::atomic::Ordering::Relaxed);
11428                return None;
11429            }
11430            None => return None,
11431        }
11432        (ids.len() == k).then_some(ids)
11433    }
11434
11435    /// Multi-step greedy: k whole frames in ONE submit, argmax and re-embed
11436    /// on the device. `ids_out` receives the k winner ids; the hidden/logits
11437    /// outputs are NOT produced in that mode.
11438    fn try_token_graph_wgpu_steps(
11439        &self,
11440        hidden: &[f32],
11441        position: usize,
11442        logits_out: &mut Vec<f32>,
11443        steps: usize,
11444        ids_out: Option<&mut Vec<u32>>,
11445        layers_run: Option<&mut usize>,
11446        from: usize,
11447        upto_excl: usize,
11448    ) -> Option<Result<Vec<f32>, ()>> {
11449        // The bank has already reserved its VRAM. Never build a second
11450        // all-expert arena across bank-owned layers (including bursts).
11451        let upto_excl = match self.mimo_moe.graph_prefix_end() {
11452            Some(end) if end < upto_excl => {
11453                if steps != 1 || layers_run.is_none() || from >= end {
11454                    return None;
11455                }
11456                end
11457            }
11458            _ => upto_excl,
11459        };
11460        // O(1) Nyström decode runs off the sealed state, not the KV cache the
11461        // graph mirrors — never take the graph while o1 is active.
11462        let o1_gpu = std::env::var("CMF_O1_GPU").as_deref() == Ok("1");
11463        if (self.o1_active() && !o1_gpu) || self.attn_softcap > 0.0 {
11464            // Softcapped scores have no graph kernel yet — CPU owns them.
11465            // o1 rides the graph only behind CMF_O1_GPU=1 while the port
11466            // proves itself; without it the CPU path owns o1 as before.
11467            return None;
11468        }
11469        // Per-layer KV heads, narrow V, sinks and sliding windows ride
11470        // `GraphAttn::Full::geom` (the ATTEND_X kernels). Anything that
11471        // geometry cannot express declines here, by name — before the
11472        // per-layer gate existed a sliding/sink model ran the graph as
11473        // full-context attention, fluent and wrong. The caller memoizes
11474        // the refusal.
11475        if let Some(reason) = self.wgpu_graph_attn_decline() {
11476            self.note_graph_decline("wgpu token graph", reason);
11477            return None;
11478        }
11479        // Per-layer sealed o1 state for the graph. During prefill the
11480        // state is still Collecting -> views are None -> the graph
11481        // refuses below and the CPU prefill records the q trace and
11482        // seals, exactly as the o1 design requires.
11483        let o1_views: Vec<Option<Vec<crate::nystrom::O1DeviceView<'_>>>> = (from..upto_excl)
11484            .map(|li| {
11485                if !o1_gpu {
11486                    return None;
11487                }
11488                self.kv_cache.layers[self.phys_layer(li)].o1_views()
11489            })
11490            .collect();
11491        if self.o1_active() && o1_gpu {
11492            // Any o1 layer not sealed (or degenerate exact-only) keeps the
11493            // whole token on the CPU: half-graph forwards would desync.
11494            let want: usize = (from..upto_excl)
11495                .filter(|li| self.kv_cache.layers[self.phys_layer(*li)].o1.is_some())
11496                .count();
11497            let have = o1_views.iter().filter(|v| v.is_some()).count();
11498            if want == 0 || have != want {
11499                // The silent twin of the gpu-side o1 gates, found the
11500                // same way: a 15x decode drop with an empty log. Views
11501                // stay None until the layer's state SEALS, so `have`
11502                // lagging `want` early in a run is the o1 design working
11503                // — but it must say so, or the next reader spends a
11504                // night proving the kernels innocent.
11505                // On CHANGE, not once: the first decline is the legal
11506                // unsealed prefill, and a once-print buries the state
11507                // that matters — what the count reads AFTER the seal.
11508                use std::sync::atomic::{AtomicUsize, Ordering};
11509                static LAST: AtomicUsize = AtomicUsize::new(usize::MAX);
11510                let code = have * 1000 + want;
11511                if LAST.swap(code, Ordering::Relaxed) != code {
11512                    tracing::warn!(
11513                        "o1 graph: {have} of {want} layers sealed — per-op until all seal"
11514                    );
11515                }
11516                return None;
11517            }
11518        }
11519        let nh = self.num_heads;
11520        let (nkv, hd, rd) = self.layer_geom(0);
11521        let gemma = self.norm_style == cortiq_core::NormStyle::Gemma;
11522        let mut layers = Vec::with_capacity(upto_excl - from);
11523        let mut model = None;
11524        let dbg = std::env::var("CMF_GRAPH_DEBUG").is_ok();
11525        fn gw(t: &QTensor) -> Option<crate::gpu::GraphW<'_>> {
11526            if let Some((m, i, kind, rs)) = t
11527                .graph_weight()
11528                .or_else(|| t.graph_weight_descriptor())
11529            {
11530                let name = &m.tensors[i].name;
11531                let prism = if crate::prism::is_inverse_embedding(m, name) {
11532                    crate::gpu::GraphPrismOp::InverseEmbedding
11533                } else if crate::prism::is_forward_weight(m, name) {
11534                    crate::gpu::GraphPrismOp::Forward
11535                } else {
11536                    crate::gpu::GraphPrismOp::None
11537                };
11538                return Some(crate::gpu::GraphW {
11539                    idx: i,
11540                    kind,
11541                    row_scale: rs,
11542                    data: &[],
11543                    prism,
11544                    affine: crate::prism::is_affine_target(m, name),
11545                });
11546            }
11547            // Small unquantized projections (GDN in_proj_a/b) stay f32.
11548            match t.as_f32() {
11549                Some(d) => Some(crate::gpu::GraphW {
11550                    idx: 0,
11551                    kind: 4,
11552                    row_scale: &[],
11553                    data: d,
11554                    prism: crate::gpu::GraphPrismOp::None,
11555                    affine: false,
11556                }),
11557                None => {
11558                    if std::env::var_os("CMF_BATCH_DEBUG").is_some() {
11559                        eprintln!("batch graph: weight has no graph/f32 representation");
11560                    }
11561                    None
11562                }
11563            }
11564        }
11565        for li in from..upto_excl {
11566            let lw = &self.weights.layers[self.phys_layer(li)];
11567            if dbg {
11568                let ak = match &lw.attn {
11569                    AttnKind::Mla(_) => "Mla".into(),
11570                    AttnKind::Full {
11571                        output_gate, bias, ..
11572                    } => format!("Full gate={output_gate} bias={}", bias.is_some()),
11573                    AttnKind::LinearGdn(_) => "LinearGdn".into(),
11574                    AttnKind::Kda(_) => "Kda".into(),
11575                    AttnKind::Linear(_) => "Linear".into(),
11576                    AttnKind::ShortConv(_) => "ShortConv".into(),
11577                    AttnKind::Bounded(_) => "Bounded".into(),
11578                };
11579                let fk = match &lw.ffn {
11580                    FfnKind::Dense(_) => "Dense",
11581                    FfnKind::Moe(_) => "Moe",
11582                    FfnKind::DenseMoe(_) => "DenseMoe",
11583                };
11584                eprintln!("graph L{li}: attn={ak} ffn={fk}");
11585            }
11586            let gffn = match &lw.ffn {
11587                FfnKind::DenseMoe(_) => return None, // dual branch: CPU path
11588                // A tube layer is several matrices, not one — the
11589                // whole-layer graph has no shape for it yet.
11590                FfnKind::Dense(d) if !d.segs.is_empty() => return None,
11591                FfnKind::Dense(d) => {
11592                    // An activation the graph has no kernel arm for keeps
11593                    // the CPU/per-op owner, by name — the dense graph FFN
11594                    // used to compute SiLU for whatever the model asked.
11595                    let Some(act) = d.act.graph_act() else {
11596                        self.note_graph_decline(
11597                            "wgpu token graph",
11598                            "dense FFN activation without a graph kernel",
11599                        );
11600                        return None;
11601                    };
11602                    crate::gpu::GraphFfn::Dense {
11603                        gate: gw(&d.gate_proj)?,
11604                        up: gw(&d.up_proj)?,
11605                        down: gw(&d.down_proj)?,
11606                        act,
11607                    }
11608                }
11609                FfnKind::Moe(m) => {
11610                    // Adaptive τ and expert masks keep the CPU path, where
11611                    // they are implemented. Sigmoid routing with a selection
11612                    // bias (LFM2-MoE / DeepSeek noaux_tc), a routed scale ≠ 1
11613                    // and an UNGATED shared expert (HunYuan hy_v3: ×2.826 on
11614                    // the routed mix, the shared expert at weight 1) are all
11615                    // graphed — before, every such token fell to the per-op
11616                    // path whole (145 submits/token on Hy-MT2-30B-A3B).
11617                    if m.route_tau.is_some() || m.mask.is_some() {
11618                        return None;
11619                    }
11620                    let shared = m.shared.as_ref();
11621                    let has_shared = shared.is_some();
11622                    let shared_gated = matches!(shared, Some((_, Some(_))));
11623                    let sgate = match shared {
11624                        Some((_, Some(sg))) => gw(sg)?,
11625                        // No gate (hy_v3) or no shared expert at all: the
11626                        // router weight stands in so the plumbing stays
11627                        // total; the select kernels pin weight 1 or skip.
11628                        _ => gw(&m.router)?,
11629                    };
11630                    let router = gw(&m.router)?;
11631                    // The resident MoE kernels do not yet carry the
11632                    // descriptor-aware transform through router/shared-gate
11633                    // selection.  Refuse the complete layer instead of
11634                    // scoring with an untransformed Prism plane (the dense
11635                    // path has an explicit FWHT boundary below).
11636                    if router.prism != crate::gpu::GraphPrismOp::None
11637                        || sgate.prism != crate::gpu::GraphPrismOp::None
11638                        || router.affine
11639                        || sgate.affine
11640                    {
11641                        tracing::warn!(
11642                            "resident MoE declined: Prism/affine router or shared gate transform is not implemented"
11643                        );
11644                        return None;
11645                    }
11646                    let inter = m.experts.first()?.gate_proj.rows();
11647                    let mut experts = Vec::with_capacity(m.experts.len() + 1);
11648                    // q4t or q4tp, but not both in one layer — the kernels
11649                    // are picked per layer, not per expert.
11650                    let mut q4tp: Option<bool> = None;
11651                    // The mixed 2-bit profile: q2tp gate/up over a q4tp
11652                    // down. Uniform across the layer, like `q4tp` itself.
11653                    let mut gu_q2: Option<bool> = None;
11654                    // q8_2f experts (gate, up and down): their own kernels.
11655                    let mut q82: Option<bool> = None;
11656                    for e in m.experts.iter().chain(shared.map(|(se, _)| se)) {
11657                        if !matches!(e.act, Act::Silu)
11658                            || e.gate_proj.rows() != inter
11659                            || e.up_proj.rows() != inter
11660                        {
11661                            return None;
11662                        }
11663                        // Expert tensors are packed into one resident buffer
11664                        // and the MoE kernels have no transform slot per
11665                        // expert.  Keep the CPU/per-op owner for Prism or
11666                        // affine experts rather than silently using raw bytes.
11667                        for expert_weight in [&e.gate_proj, &e.up_proj, &e.down_proj] {
11668                            let Some((em, ei, _, _)) = expert_weight
11669                                .graph_weight()
11670                                .or_else(|| expert_weight.graph_weight_descriptor())
11671                            else {
11672                                return None;
11673                            };
11674                            let name = &em.tensors[ei].name;
11675                            if crate::prism::is_forward_weight(em, name)
11676                                || crate::prism::is_inverse_embedding(em, name)
11677                                || crate::prism::is_affine_target(em, name)
11678                            {
11679                                tracing::warn!(
11680                                    "resident MoE declined: expert Prism/affine transform is not implemented"
11681                                );
11682                                return None;
11683                            }
11684                        }
11685                        let (mm, gi, ui, di, is_p, is_q2, is_8) = match e.gate_proj.mapped_q4t() {
11686                            Some((mm, gi)) => (
11687                                mm,
11688                                gi,
11689                                e.up_proj.mapped_q4t()?.1,
11690                                e.down_proj.mapped_q4t()?.1,
11691                                false,
11692                                false,
11693                                false,
11694                            ),
11695                            None => match e.gate_proj.mapped_q2tp() {
11696                                Some((mm, gi)) => (
11697                                    mm,
11698                                    gi,
11699                                    e.up_proj.mapped_q2tp()?.1,
11700                                    e.down_proj.mapped_q4tp()?.1,
11701                                    true,
11702                                    true,
11703                                    false,
11704                                ),
11705                                None => match e.gate_proj.mapped_q4tp() {
11706                                    Some((mm, gi)) => (
11707                                        mm,
11708                                        gi,
11709                                        e.up_proj.mapped_q4tp()?.1,
11710                                        e.down_proj.mapped_q4tp()?.1,
11711                                        true,
11712                                        false,
11713                                        false,
11714                                    ),
11715                                    // q8_2f experts: all three projections,
11716                                    // each with its own column field.
11717                                    None => {
11718                                        if !crate::gpu::moe_q82_graph_on() {
11719                                            return None;
11720                                        }
11721                                        let (mm, gi) = e.gate_proj.mapped_q8_2f()?;
11722                                        (
11723                                            mm,
11724                                            gi,
11725                                            e.up_proj.mapped_q8_2f()?.1,
11726                                            e.down_proj.mapped_q8_2f()?.1,
11727                                            false,
11728                                            false,
11729                                            true,
11730                                        )
11731                                    }
11732                                },
11733                            },
11734                        };
11735                        if *q4tp.get_or_insert(is_p) != is_p
11736                            || *gu_q2.get_or_insert(is_q2) != is_q2
11737                            || *q82.get_or_insert(is_8) != is_8
11738                        {
11739                            // The shared expert rides in the same packed
11740                            // buffer as the routed ones, so a layer that
11741                            // mixes layouts cannot be indexed by one stride.
11742                            // Say so: the symptom is a whole model quietly
11743                            // running its MoE on the CPU.
11744                            tracing::warn!(
11745                                "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."
11746                            );
11747                            return None;
11748                        }
11749                        model.get_or_insert_with(|| mm.clone());
11750                        experts.push((gi, ui, di));
11751                    }
11752                    crate::gpu::GraphFfn::Moe {
11753                        router,
11754                        shared_gate: sgate,
11755                        experts,
11756                        n_exp: m.experts.len(),
11757                        // CMF_TOPK_PROBE: timing probe only — output is WRONG.
11758                        // Fewer experts shrink the MoE arithmetic while the
11759                        // dispatch count stays identical, which is the only
11760                        // clean way to tell a launch-bound decode from a
11761                        // compute-bound one.
11762                        top_k: std::env::var("CMF_TOPK_PROBE")
11763                            .ok()
11764                            .and_then(|v| v.parse::<usize>().ok())
11765                            .filter(|k| *k > 0 && *k <= m.top_k)
11766                            .unwrap_or(m.top_k),
11767                        inter,
11768                        norm_topk: m.norm_topk_prob,
11769                        q4tp: q4tp?,
11770                        gu_q2: gu_q2.unwrap_or(false),
11771                        q82: q82.unwrap_or(false),
11772                        sigmoid: m.router_sigmoid,
11773                        bias: m.expert_bias.as_deref(),
11774                        has_shared,
11775                        shared_gated,
11776                        route_scale: m.routed_scaling,
11777                    }
11778                }
11779            };
11780            let attn = match &lw.attn {
11781                AttnKind::Full {
11782                    wq,
11783                    wk,
11784                    wv,
11785                    wo,
11786                    q_norm,
11787                    k_norm,
11788                    output_gate,
11789                    softplus_gate,
11790                    bias,
11791                } => {
11792                    if self.attention_heads_per_layer.is_some() {
11793                        return None;
11794                    }
11795                    // A projected output gate rides the graph in one form:
11796                    // Spark-X2.5's head-wise sigmoid (one g_proj row per Q
11797                    // head). Laguna's softplus gate keeps the CPU path.
11798                    let head_gate = match softplus_gate {
11799                        None => None,
11800                        Some((g, true)) if self.proj_gate_sigmoid && !*output_gate => {
11801                            Some(gw(g)?)
11802                        }
11803                        Some(_) => {
11804                            self.note_graph_decline(
11805                                "wgpu token graph",
11806                                "projected softplus / per-element output gate",
11807                            );
11808                            return None;
11809                        }
11810                    };
11811                    let (m, _, _, _) = wq
11812                        .graph_weight()
11813                        .or_else(|| wq.graph_weight_descriptor())?;
11814                    model = Some(m.clone());
11815                    crate::gpu::GraphAttn::Full {
11816                        wq: gw(wq)?,
11817                        wk: gw(wk)?,
11818                        wv: gw(wv)?,
11819                        wo: gw(wo)?,
11820                        q_norm: q_norm.as_deref(),
11821                        k_norm: k_norm.as_deref(),
11822                        late_qk_norm: self.qk_norm_after_rope,
11823                        bias: bias
11824                            .as_ref()
11825                            .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
11826                        output_gate: *output_gate,
11827                        cpu_k: self.kv_cache.layers[li].k_heads(),
11828                        cpu_v: self.kv_cache.layers[li].v_heads(),
11829                        cpu_base: self.kv_cache.layers[li].base(),
11830                        geom: self.graph_attn_geom(li),
11831                        head_gate,
11832                    }
11833                }
11834                AttnKind::LinearGdn(w) => {
11835                    let cfg = self.gdn_cfg?;
11836                    let (m, _, _, _) = w
11837                        .in_proj_qkv
11838                        .graph_weight()
11839                        .or_else(|| w.in_proj_qkv.graph_weight_descriptor())?;
11840                    model = Some(m.clone());
11841                    crate::gpu::GraphAttn::Gdn {
11842                        qkv: gw(&w.in_proj_qkv)?,
11843                        z: gw(&w.in_proj_z)?,
11844                        a: gw(&w.in_proj_a)?,
11845                        b: gw(&w.in_proj_b)?,
11846                        out: gw(&w.out_proj)?,
11847                        conv1d: &w.conv1d,
11848                        a_log: &w.a_log,
11849                        dt_bias: &w.dt_bias,
11850                        norm: &w.norm,
11851                        nv: cfg.num_v_heads,
11852                        nk: cfg.num_k_heads,
11853                        dk: cfg.key_head_dim,
11854                        dv: cfg.value_head_dim,
11855                        kk: cfg.conv_kernel,
11856                        cpu_state: &self.kv_cache.layers[self.phys_layer(li)].linear_state,
11857                    }
11858                }
11859                AttnKind::ShortConv(w) => {
11860                    let cfg = self.short_conv_cfg?;
11861                    let (m, _, _, _) = w
11862                        .in_proj
11863                        .graph_weight()
11864                        .or_else(|| w.in_proj.graph_weight_descriptor())?;
11865                    model = Some(m.clone());
11866                    crate::gpu::GraphAttn::ShortConv {
11867                        inp: gw(&w.in_proj)?,
11868                        out: gw(&w.out_proj)?,
11869                        taps: &w.conv,
11870                        kernel: cfg.kernel,
11871                        cpu_state: &self.kv_cache.layers[self.phys_layer(li)].linear_state,
11872                    }
11873                }
11874                _ => return None,
11875            };
11876            layers.push(crate::gpu::GraphLayer {
11877                input_norm: &lw.input_norm,
11878                attn,
11879                post_norm: &lw.post_norm,
11880                attn_out_norm: lw.attn_out_norm.as_deref(),
11881                ffn_out_norm: lw.ffn_out_norm.as_deref(),
11882                layer_scale: lw.layer_scale,
11883                ffn: gffn,
11884            });
11885        }
11886        let model = model?;
11887        // Fold final-norm + lm_head into the graph when this call wants logits
11888        // and the lm_head is a graphable (quantized) weight — the graph then
11889        // reads back logits (into logits_out) instead of the hidden, dropping
11890        // the separate CPU/GPU lm_head op + its sync. Never the f32 fallback:
11891        // an unquantized lm_head is vocab·hidden and must not be uploaded.
11892        let lm_gw = if upto_excl == self.num_layers
11893            && self.graph_want_logits
11894            && std::env::var("CMF_GPU_LMHEAD")
11895                .map(|v| v != "0")
11896                .unwrap_or(true)
11897        {
11898            self.weights
11899                .lm_head
11900                .graph_weight()
11901                .or_else(|| self.weights.lm_head.graph_weight_descriptor())
11902                .map(|(m, i, kind, rs)| {
11903                let name = &m.tensors[i].name;
11904                let prism = if crate::prism::is_inverse_embedding(m, name) {
11905                    crate::gpu::GraphPrismOp::InverseEmbedding
11906                } else if crate::prism::is_forward_weight(m, name) {
11907                    crate::gpu::GraphPrismOp::Forward
11908                } else {
11909                    crate::gpu::GraphPrismOp::None
11910                };
11911                (
11912                    crate::gpu::GraphW {
11913                        idx: i,
11914                        kind,
11915                        row_scale: rs,
11916                        data: &[],
11917                        prism,
11918                        affine: crate::prism::is_affine_target(m, name),
11919                    },
11920                    self.weights.lm_head.rows(),
11921                )
11922            })
11923        } else {
11924            None
11925        };
11926        let lm = lm_gw.as_ref().map(|(gw, rows)| (gw, *rows));
11927        // Multi-step re-embeds the winner on the device.
11928        let emb_gw = if steps > 1 {
11929            self.weights
11930                .embed_tokens
11931                .graph_weight()
11932                .or_else(|| self.weights.embed_tokens.graph_weight_descriptor())
11933                .map(|(m, i, kind, rs)| {
11934                    let name = &m.tensors[i].name;
11935                    let prism = if crate::prism::is_inverse_embedding(m, name) {
11936                        crate::gpu::GraphPrismOp::InverseEmbedding
11937                    } else if crate::prism::is_forward_weight(m, name) {
11938                        crate::gpu::GraphPrismOp::Forward
11939                    } else {
11940                        crate::gpu::GraphPrismOp::None
11941                    };
11942                    (
11943                        crate::gpu::GraphW {
11944                            idx: i,
11945                            kind,
11946                            row_scale: rs,
11947                            data: &[],
11948                            prism,
11949                            affine: crate::prism::is_affine_target(m, name),
11950                        },
11951                        self.weights.embed_tokens.rows(),
11952                        self.embed_multiplier,
11953                    )
11954                })
11955        } else {
11956            None
11957        };
11958
11959        // Loop boundaries: virtual layer indices after which final_norm is
11960        // applied (mid-stack only; the GLOBAL last layer's norm folds into
11961        // lm_head). Span-relative — the executor compares its enumerate
11962        // index. A span ending mid-stack keeps its boundary norm even when
11963        // it is the span's own last layer.
11964        let loop_norm_at: Vec<usize> = if self.loop_final_norm {
11965            (from..upto_excl.min(self.num_layers - 1))
11966                .filter(|&li| (li + 1) % self.physical_layers == 0)
11967                .map(|li| li - from)
11968                .collect()
11969        } else {
11970            Vec::new()
11971        };
11972        let mut h = hidden.to_vec();
11973        // The normal decode path only needs the fused lm-head logits.  A
11974        // CMF_LOGIT_DUMP diagnostic, however, promises a prompt-boundary
11975        // post-stack hidden alongside those logits; request the existing
11976        // second readback only for that explicit probe instead of dumping
11977        // the input copy left in `h` by a folded-head graph.
11978        let dump_hidden = std::env::var_os("CMF_LOGIT_DUMP").is_some();
11979        let outcome = crate::gpu::forward_token_graph(
11980            &model,
11981            self.graph_kv_id,
11982            &layers,
11983            &o1_views,
11984            self.o1_epoch,
11985            &self.inv_freq,
11986            &mut h,
11987            nh,
11988            nkv,
11989            hd,
11990            self.attn_scale,
11991            rd,
11992            self.hidden_size,
11993            self.intermediate_size,
11994            position,
11995            self.kv_cache.max_seq_len,
11996            gemma,
11997            self.rms_eps as f32,
11998            lm,
11999            &self.weights.final_norm,
12000            logits_out,
12001            &loop_norm_at,
12002            steps,
12003            emb_gw.as_ref().map(|(gw, rows, m)| (gw, *rows, *m)),
12004            ids_out,
12005            layers_run,
12006            from,
12007            dump_hidden,
12008        );
12009        match outcome {
12010            crate::gpu::TokenGraphOutcome::Completed => Some(Ok(h)),
12011            crate::gpu::TokenGraphOutcome::Failed => Some(Err(())),
12012            crate::gpu::TokenGraphOutcome::Declined => None,
12013        }
12014    }
12015
12016    /// Batched prefill: k contiguous prompt positions through the whole wgpu
12017    /// graph in ONE submit (projections/FFN as GEMMs). `hiddens` is [k·hidden]
12018    /// in/out (embeddings in, layer output out); KV mirror / GDN state advance.
12019    /// false ⇒ unsupported → caller keeps the per-position graph.
12020    /// The b-row Metal graph plan for the whole model: every layer as a
12021    /// GDN run or a full-attention item, all-or-nothing (a layer outside the
12022    /// graph's contract → None, the caller runs plain). Shared by the
12023    /// speculative verify and the batched prefill.
12024    #[cfg(target_os = "macos")]
12025    #[allow(clippy::type_complexity)]
12026    fn metal_rows_plan(
12027        &self,
12028    ) -> Option<(
12029        Vec<MetalRowsItem<'_>>,
12030        std::sync::Arc<cortiq_core::CmfModel>,
12031        Option<crate::gpu_metal::GdnGpuCfg>,
12032    )> {
12033        use crate::gpu_metal::{AttnGpuLayer, GdnGpuCfg, GdnGpuLayer, MetalFfn};
12034        let graph_force = crate::gpu::q1_force() || crate::gpu::q2tp_gpu_opt_in();
12035        if !graph_force
12036            || !crate::gpu::enabled_here()
12037            || std::env::var("CMF_GPU_BLOCK")
12038                .map(|v| v == "0")
12039                .unwrap_or(false)
12040            || self.attn_softcap > 0.0
12041            || self.o1_active()
12042            || self.swa.is_some()
12043            || self.global_attn.is_some()
12044            || self.attention_heads_per_layer.is_some()
12045            // per-layer KV heads, narrow V, learned sinks (MiMo-V2)
12046            || self.graph_attn_decline_reason().is_some()
12047            || self.attn_v_norm
12048            || self.loop_final_norm
12049            || !self.metal_rope_scale_ok()
12050        {
12051            return None;
12052        }
12053        let attend_contract = self.head_dim % 4 == 0
12054            && self.head_dim <= 256
12055            && self.rotary_dim >= 2
12056            && self.rotary_dim <= self.head_dim
12057            && (self.rotary_dim / 2) % 32 == 0
12058            && self.num_kv_heads > 0
12059            && self.num_heads % self.num_kv_heads == 0;
12060        if !attend_contract {
12061            return None;
12062        }
12063        let mut plan: Vec<MetalRowsItem> = Vec::new();
12064        let mut model_ref: Option<std::sync::Arc<cortiq_core::CmfModel>> = None;
12065        for li in 0..self.num_layers {
12066            let lw = &self.weights.layers[self.phys_layer(li)];
12067            if lw.attn_out_norm.is_some() || lw.ffn_out_norm.is_some() || lw.layer_scale.is_some() {
12068                return None;
12069            }
12070            let ffn = match &lw.ffn {
12071                FfnKind::Dense(d) if d.act == Act::Silu && d.segs.is_empty() => {
12072                    let (Some(g), Some(u), Some(dn)) = (
12073                        d.gate_proj.metal_graph_parts(),
12074                        d.up_proj.metal_graph_parts(),
12075                        d.down_proj.metal_graph_parts(),
12076                    ) else {
12077                        return None;
12078                    };
12079                    MetalFfn::Dense {
12080                        gate: g,
12081                        up: u,
12082                        down: dn,
12083                        act: 0, // SiLU only (the arm above)
12084                    }
12085                }
12086                _ => return None,
12087            };
12088            match &lw.attn {
12089                AttnKind::LinearGdn(w) if self.gdn_cfg.is_some() => {
12090                    let (Some(qkv), Some(z), Some(a), Some(bb), Some(out)) = (
12091                        w.in_proj_qkv.metal_graph_parts(),
12092                        w.in_proj_z.metal_graph_parts(),
12093                        w.in_proj_a.f32_parts(),
12094                        w.in_proj_b.f32_parts(),
12095                        w.out_proj.metal_graph_parts(),
12096                    ) else {
12097                        return None;
12098                    };
12099                    if let QTensor::Mapped { model, .. } = &w.in_proj_qkv {
12100                        model_ref.get_or_insert_with(|| model.clone());
12101                    }
12102                    let gl = GdnGpuLayer {
12103                        attn_norm: &lw.input_norm,
12104                        post_norm: &lw.post_norm,
12105                        qkv,
12106                        z,
12107                        a,
12108                        b: bb,
12109                        out,
12110                        ffn,
12111                        conv1d: &w.conv1d,
12112                        a_log: &w.a_log,
12113                        dt_bias: &w.dt_bias,
12114                        gnorm: &w.norm,
12115                    };
12116                    match plan.last_mut() {
12117                        Some(MetalRowsItem::Gdn { run, .. }) => run.push(gl),
12118                        _ => plan.push(MetalRowsItem::Gdn {
12119                            run: vec![gl],
12120                            first: li,
12121                        }),
12122                    }
12123                }
12124                AttnKind::Full {
12125                    wq,
12126                    wk,
12127                    wv,
12128                    wo,
12129                    q_norm,
12130                    k_norm,
12131                    output_gate,
12132                    softplus_gate: None,
12133                    bias: None,
12134                } => {
12135                    let (Some(pq), Some(pk), Some(pv), Some(po)) =
12136                        (
12137                            wq.metal_graph_parts(),
12138                            wk.metal_graph_parts(),
12139                            wv.metal_graph_parts(),
12140                            wo.metal_graph_parts(),
12141                        )
12142                    else {
12143                        return None;
12144                    };
12145                    if let QTensor::Mapped { model, .. } = wq {
12146                        model_ref.get_or_insert_with(|| model.clone());
12147                    }
12148                    let cache = &self.kv_cache.layers[li];
12149                    if cache.mode != crate::kv_cache::KvMode::F32 || cache.o1.is_some() {
12150                        return None;
12151                    }
12152                    plan.push(MetalRowsItem::Attn {
12153                        l: AttnGpuLayer {
12154                            attn_norm: &lw.input_norm,
12155                            post_norm: &lw.post_norm,
12156                            wq: pq,
12157                            wk: pk,
12158                            wv: pv,
12159                            wo: po,
12160                            ffn,
12161                            attn_out_norm: None,
12162                            ffn_out_norm: None,
12163                            layer_scale: None,
12164                        },
12165                        li,
12166                        q_norm: q_norm.as_deref(),
12167                        k_norm: k_norm.as_deref(),
12168                        output_gate: *output_gate,
12169                    });
12170                }
12171                _ => return None,
12172            }
12173        }
12174        let model = model_ref?;
12175        let gcfg = self.gdn_cfg.map(|cfg| GdnGpuCfg {
12176            nv: cfg.num_v_heads,
12177            nk: cfg.num_k_heads,
12178            dk: cfg.key_head_dim,
12179            dv: cfg.value_head_dim,
12180            kk: cfg.conv_kernel,
12181            hidden: self.hidden_size,
12182            inter: self.intermediate_size,
12183            c_dim: cfg.conv_dim(),
12184            eps: cfg.rms_eps as f32,
12185            gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
12186        });
12187        Some((plan, model, gcfg))
12188    }
12189
12190    /// `AttnDeviceParams` for a plan item over the CPU cache as it stands.
12191    #[cfg(target_os = "macos")]
12192    #[allow(clippy::too_many_arguments)]
12193    fn metal_attn_params<'a>(
12194        li: usize,
12195        cache: &'a crate::kv_cache::LayerKvCache,
12196        q_norm: Option<&'a [f32]>,
12197        k_norm: Option<&'a [f32]>,
12198        output_gate: bool,
12199        inv_freq: &'a [f32],
12200        rope_scale: f32,
12201        geom: (usize, usize, usize, usize),
12202        pos0: usize,
12203        kv_id: u64,
12204        scale: f32,
12205        eps: f32,
12206        gemma: bool,
12207        late_qk_norm: bool,
12208    ) -> (crate::gpu_metal::AttnDeviceParams<'a>, usize) {
12209        let (nh, nkv, hd, rd) = geom;
12210        let cpu_k: Vec<&[f32]> = (0..nkv).map(|g| cache.head_keys(g)).collect();
12211        let cpu_v: Vec<&[f32]> = (0..nkv).map(|g| cache.head_values(g)).collect();
12212        let cpu_stored = cpu_k[0].len() / hd;
12213        (
12214            crate::gpu_metal::AttnDeviceParams {
12215                kv_id,
12216                layer: li,
12217                nh,
12218                nkv,
12219                hd,
12220                rd,
12221                position: pos0,
12222                scale,
12223                eps,
12224                gemma,
12225                late_qk_norm,
12226                output_gate,
12227                q_norm,
12228                k_norm,
12229                inv_freq,
12230                rope_scale,
12231                cpu_k,
12232                cpu_v,
12233                cpu_stored,
12234                cpu_gen: cache.generation(),
12235                o1: None,
12236                window: None,
12237                head_gate: None,
12238                v_norm: false,
12239            },
12240            cpu_stored,
12241        )
12242    }
12243
12244    /// Run the rows plan over `hiddens` (b rows at `pos0..`): validate,
12245    /// encode every item, optionally the head, sync. Returns the graph
12246    /// (for the commit / state finish) plus the GDN layer indices and the
12247    /// attention layers with the row count they were encoded against.
12248    #[cfg(target_os = "macos")]
12249    #[allow(clippy::type_complexity)]
12250    fn metal_rows_run(
12251        &mut self,
12252        hiddens: &mut [f32],
12253        pos0: usize,
12254        b: usize,
12255        prefill: bool,
12256        spec: Option<((usize, usize, usize), &[f32], &mut Vec<f32>)>,
12257        // Greedy verify: (row length scored, the b argmax ids out) — the
12258        // head's argmax runs on the device and the logits plane is NOT
12259        // read back (`spec.2` stays empty).
12260        mut argmax_out: Option<(usize, &mut Vec<u32>)>,
12261    ) -> MetalRowsRun {
12262        use crate::gpu_metal::{GraphDims, VerifyGraph};
12263        // The previous round's commit may still be replaying into the
12264        // trunk GDN owners on the second queue: this graph reads them
12265        // (zero-copy wraps) and may reallocate them below — collect the
12266        // replay first. Normally already complete (the draft chain ran
12267        // in between); a failed replay is terminal like a failed commit.
12268        if !crate::gpu_metal::wait_replay() {
12269            tracing::error!("Metal rows graph: the pending async replay failed");
12270            return MetalRowsRun::Failed;
12271        }
12272        spec_stamp("v.wait");
12273        // Seed the GDN recurrent records the rows graph reads — GDN layers
12274        // ONLY (the record the CPU path would allocate anyway).  Sizing
12275        // every layer here planted a zero GDN-sized record on a bounded
12276        // anchor of a natively bounded file this path then refused, and
12277        // that record counted as recurrent state on macOS.
12278        let want = self.gdn_cfg.map(|c| c.state_len()).unwrap_or(0);
12279        if want > 0 {
12280            let phys = self.physical_layers.max(1);
12281            for (li, l) in self.kv_cache.layers.iter_mut().enumerate() {
12282                let is_gdn = self
12283                    .weights
12284                    .layers
12285                    .get(li % phys)
12286                    .is_some_and(|lw| matches!(lw.attn, AttnKind::LinearGdn(_)));
12287                if is_gdn && l.linear_state.len() != want {
12288                    l.linear_state = vec![0f32; want];
12289                }
12290            }
12291        }
12292        let Some((plan, model, gcfg)) = self.metal_rows_plan() else {
12293            return MetalRowsRun::Declined;
12294        };
12295        spec_stamp("v.plan");
12296        let dims = GraphDims {
12297            hidden: self.hidden_size,
12298            eps: self.rms_eps as f32,
12299            gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
12300        };
12301        let Some(mut graph) = (if prefill {
12302            VerifyGraph::new_prefill(&model, dims, hiddens, b)
12303        } else {
12304            VerifyGraph::new(&model, dims, hiddens, b)
12305        }) else {
12306            return MetalRowsRun::Declined;
12307        };
12308        let geom = (
12309            self.num_heads,
12310            self.num_kv_heads,
12311            self.head_dim,
12312            self.rotary_dim,
12313        );
12314        let gemma = self.norm_style == cortiq_core::NormStyle::Gemma;
12315        let eps = self.rms_eps as f32;
12316        let kv_id = self.graph_kv_id;
12317        let inv_freq = self.inv_freq.clone();
12318        for item in &plan {
12319            let ok = match item {
12320                MetalRowsItem::Gdn { run, .. } => gcfg
12321                    .as_ref()
12322                    .map(|gc| run.iter().all(|l| graph.gdn_ok(l, gc)))
12323                    .unwrap_or(false),
12324                MetalRowsItem::Attn {
12325                    l,
12326                    li,
12327                    q_norm,
12328                    k_norm,
12329                    output_gate,
12330                } => {
12331                    let (p, _) = Self::metal_attn_params(
12332                        *li,
12333                        &self.kv_cache.layers[*li],
12334                        *q_norm,
12335                        *k_norm,
12336                        *output_gate,
12337                        &inv_freq,
12338                        self.layer_rope_scale(*li),
12339                        geom,
12340                        pos0,
12341                        kv_id,
12342                        self.attn_scale,
12343                        eps,
12344                        gemma,
12345                        self.qk_norm_after_rope,
12346                    );
12347                    graph.attn_ok(l, &p)
12348                }
12349            };
12350            if !ok {
12351                use std::sync::atomic::{AtomicBool, Ordering};
12352                static SAID: AtomicBool = AtomicBool::new(false);
12353                if !SAID.swap(true, Ordering::Relaxed) {
12354                    tracing::warn!("metal rows graph: a layer failed preflight — declining");
12355                }
12356                return MetalRowsRun::Declined;
12357            }
12358        }
12359        let lm = match &spec {
12360            Some((lm, _, _)) => {
12361                if !graph.lm_head_ok(*lm) {
12362                    return MetalRowsRun::Declined;
12363                }
12364                Some(*lm)
12365            }
12366            None => None,
12367        };
12368        let mut gdn_layers = Vec::new();
12369        let mut attn_layers = Vec::new();
12370        for item in &plan {
12371            match item {
12372                MetalRowsItem::Gdn { run, first } => {
12373                    let ro: Vec<&[f32]> = self.kv_cache.layers[*first..*first + run.len()]
12374                        .iter()
12375                        .map(|l| l.linear_state.as_slice())
12376                        .collect();
12377                    if !graph.encode_gdn_run_b(run, &ro, gcfg.as_ref().unwrap()) {
12378                        return MetalRowsRun::Declined;
12379                    }
12380                    gdn_layers.extend(*first..*first + run.len());
12381                }
12382                MetalRowsItem::Attn {
12383                    l,
12384                    li,
12385                    q_norm,
12386                    k_norm,
12387                    output_gate,
12388                } => {
12389                    let (p, cpu_stored) = Self::metal_attn_params(
12390                        *li,
12391                        &self.kv_cache.layers[*li],
12392                        *q_norm,
12393                        *k_norm,
12394                        *output_gate,
12395                        &inv_freq,
12396                        self.layer_rope_scale(*li),
12397                        geom,
12398                        pos0,
12399                        kv_id,
12400                        self.attn_scale,
12401                        eps,
12402                        gemma,
12403                        self.qk_norm_after_rope,
12404                    );
12405                    if !graph.encode_attn_b(l, &p) {
12406                        return MetalRowsRun::Declined;
12407                    }
12408                    attn_layers.push((*li, cpu_stored));
12409                }
12410            }
12411        }
12412        if let (Some(lm), Some((_, final_norm, _))) = (lm, spec.as_ref()) {
12413            if !graph.encode_lm_head_b(final_norm, lm) {
12414                return MetalRowsRun::Declined;
12415            }
12416            // The device argmax is an OPTIMISATION, never a reason to
12417            // decline the round: if it will not encode, drop it and read
12418            // the logits plane back the old way (the head is encoded
12419            // either way, so the rows are there).
12420            if let Some((n, _)) = argmax_out.as_ref() {
12421                if !graph.encode_argmax_b(*n) {
12422                    argmax_out = None;
12423                }
12424            }
12425        }
12426        spec_stamp("v.enc");
12427        if !graph.sync() {
12428            return MetalRowsRun::Failed;
12429        }
12430        spec_stamp("v.gpu");
12431        match (spec, argmax_out) {
12432            (Some(_), Some((_, ids))) => {
12433                ids.resize(b, 0);
12434                if !graph.read_argmax(ids) {
12435                    return MetalRowsRun::Failed;
12436                }
12437                spec_stamp("v.am");
12438            }
12439            (Some((lm, _, logits)), None) => {
12440                logits.resize(b * lm.1, 0.0);
12441                if !graph.read_logits(logits) {
12442                    return MetalRowsRun::Failed;
12443                }
12444                spec_stamp("v.lg");
12445            }
12446            (None, _) => {}
12447        }
12448        if !graph.read_hidden(hiddens) {
12449            return MetalRowsRun::Failed;
12450        }
12451        spec_stamp("v.hid");
12452        MetalRowsRun::Completed(MetalVerifyPending {
12453            graph,
12454            gdn_layers,
12455            attn_layers,
12456        })
12457    }
12458
12459    /// Native-Metal twin of `try_batch_graph_wgpu`: the b rows through the
12460    /// whole model on the `VerifyGraph` (one submit), the head folded in
12461    /// when `spec` asks; `hiddens` come back as the last layer's output
12462    /// rows, `spec.2` as `[b][lm_rows]` logits. The graph is parked in
12463    /// `metal_verify` for `metal_verify_commit`.
12464    #[cfg(target_os = "macos")]
12465    fn try_batch_graph_metal(
12466        &mut self,
12467        hiddens: &mut [f32],
12468        positions: &[usize],
12469        b: usize,
12470        spec: Option<((usize, usize, usize), &[f32], &mut Vec<f32>)>,
12471        argmax_out: Option<(usize, &mut Vec<u32>)>,
12472    ) -> crate::gpu::BatchGraphOutcome {
12473        let _t0 = std::time::Instant::now();
12474        if positions.len() != b
12475            || positions.windows(2).any(|w| w[1] != w[0] + 1)
12476            || hiddens.len() != b * self.hidden_size
12477        {
12478            return crate::gpu::BatchGraphOutcome::Declined;
12479        }
12480        let pending = match self.metal_rows_run(hiddens, positions[0], b, false, spec, argmax_out) {
12481            MetalRowsRun::Declined => return crate::gpu::BatchGraphOutcome::Declined,
12482            MetalRowsRun::Failed => return crate::gpu::BatchGraphOutcome::Failed,
12483            MetalRowsRun::Completed(pending) => pending,
12484        };
12485        if std::env::var("CMF_GRAPH_SPEC_TIME").is_ok() {
12486            eprintln!(
12487                "metal-verify: {:.1} ms | b={b}",
12488                _t0.elapsed().as_secs_f64() * 1e3
12489            );
12490        }
12491        self.metal_verify = Some(pending);
12492        crate::gpu::BatchGraphOutcome::Completed
12493    }
12494
12495    /// Batched prefill on the Metal rows graph: `ids` (≤ 512) at
12496    /// `start_pos..`, states written in place, K/V rows appended to the
12497    /// CPU caches; optional final norm/head logits are returned in `spec`.
12498    /// Declined means no command buffer was admitted; Failed is terminal.
12499    #[cfg(target_os = "macos")]
12500    fn prefill_rows_metal(
12501        &mut self,
12502        ids: &[u32],
12503        start_pos: usize,
12504        spec: Option<((usize, usize, usize), &[f32], &mut Vec<f32>)>,
12505    ) -> MetalPrefillOutcome {
12506        let b = ids.len();
12507        if b == 0 || b > 512 {
12508            return MetalPrefillOutcome::Declined;
12509        }
12510        METAL_PREFILL_CHUNKS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
12511        let with_head = spec.is_some();
12512        let hs = self.hidden_size;
12513        let mut hiddens = vec![0f32; b * hs];
12514        for (j, &id) in ids.iter().enumerate() {
12515            let e = self.embed_single(id);
12516            hiddens[j * hs..(j + 1) * hs].copy_from_slice(&e);
12517        }
12518        let mut pending = match self.metal_rows_run(&mut hiddens, start_pos, b, true, spec, None) {
12519            MetalRowsRun::Declined => return MetalPrefillOutcome::Declined,
12520            MetalRowsRun::Failed => {
12521                METAL_PREFILL_ERRORS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
12522                return MetalPrefillOutcome::Failed;
12523            }
12524            MetalRowsRun::Completed(pending) => pending,
12525        };
12526        // states are final: copy them to the owners
12527        let idxs = pending.gdn_layers.clone();
12528        let mut outs: Vec<&mut [f32]> = self
12529            .kv_cache
12530            .layers
12531            .iter_mut()
12532            .enumerate()
12533            .filter(|(i, _)| idxs.binary_search(i).is_ok())
12534            .map(|(_, l)| l.linear_state.as_mut_slice())
12535            .collect();
12536        if !pending.graph.finish_states(&mut outs) {
12537            METAL_PREFILL_ERRORS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
12538            return MetalPrefillOutcome::Failed;
12539        }
12540        let (nkv, hd) = (self.num_kv_heads, self.head_dim);
12541        // Read every layer before mutating any CPU cache.  A missing mirror
12542        // row is a terminal graph failure, not a reason to append a partial
12543        // prefix and replay the remainder serially.
12544        let mut rows = Vec::with_capacity(pending.attn_layers.len());
12545        for (li, cpu_stored) in &pending.attn_layers {
12546            let mut kbuf = vec![0f32; b * nkv * hd];
12547            let mut vbuf = vec![0f32; b * nkv * hd];
12548            if !crate::gpu_metal::kv_mirror_read_rows(
12549                self.graph_kv_id,
12550                *li,
12551                nkv,
12552                hd,
12553                *cpu_stored,
12554                b,
12555                &mut kbuf,
12556                &mut vbuf,
12557            ) {
12558                METAL_PREFILL_ERRORS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
12559                return MetalPrefillOutcome::Failed;
12560            }
12561            rows.push((*li, *cpu_stored, kbuf, vbuf));
12562        }
12563        for (li, cpu_stored, kbuf, vbuf) in rows {
12564            let cache = &mut self.kv_cache.layers[li];
12565            for r in 0..b {
12566                cache.append(
12567                    &kbuf[r * nkv * hd..(r + 1) * nkv * hd],
12568                    &vbuf[r * nkv * hd..(r + 1) * nkv * hd],
12569                    &[],
12570                );
12571            }
12572            crate::gpu_metal::kv_mirror_set_stored(self.graph_kv_id, li, cpu_stored + b);
12573        }
12574        METAL_PREFILL_ROWS.fetch_add(b as u64, std::sync::atomic::Ordering::Relaxed);
12575        if with_head {
12576            METAL_PREFILL_HEAD_ROWS.fetch_add(b as u64, std::sync::atomic::Ordering::Relaxed);
12577        }
12578        MetalPrefillOutcome::Completed(hiddens)
12579    }
12580
12581    #[cfg(target_os = "macos")]
12582    fn prefill_batch_metal(&mut self, ids: &[u32], start_pos: usize) -> MetalPrefillOutcome {
12583        self.prefill_rows_metal(ids, start_pos, None)
12584    }
12585
12586    /// Exact teacher-forced NLL through the ordinary Metal rows graph.  This
12587    /// is intentionally separate from the serial TokenGraph scorer: every
12588    /// chunk owns a real b-row graph/head completion and the recurrent/KV
12589    /// handoff is committed before the next chunk begins.
12590    #[cfg(target_os = "macos")]
12591    fn nll_batch_metal(&mut self, ids: &[u32], start: usize) -> MetalBatchNllOutcome {
12592        if ids.len() < 2 || self.o1_active() || self.head_clusters.is_some() {
12593            return MetalBatchNllOutcome::Declined;
12594        }
12595        let Some(lm) = self.weights.lm_head.metal_graph_parts() else {
12596            return MetalBatchNllOutcome::Declined;
12597        };
12598        let chunk = std::env::var("CMF_METAL_PREFILL_CHUNK")
12599            .ok()
12600            .and_then(|v| v.parse::<usize>().ok())
12601            .filter(|&v| (1..=512).contains(&v))
12602            .unwrap_or(32);
12603        let final_norm = self.weights.final_norm.clone();
12604        let mut nll = 0.0f64;
12605        let mut count = 0usize;
12606        let mut pos = 0usize;
12607        let mut completed = 0usize;
12608        while pos < ids.len() {
12609            let end = (pos + chunk).min(ids.len());
12610            let mut logits = Vec::new();
12611            let outcome = self.prefill_rows_metal(
12612                &ids[pos..end],
12613                pos,
12614                Some((lm, &final_norm, &mut logits)),
12615            );
12616            match outcome {
12617                MetalPrefillOutcome::Declined => {
12618                    return if completed == 0 {
12619                        MetalBatchNllOutcome::Declined
12620                    } else {
12621                        MetalBatchNllOutcome::Failed(format!(
12622                            "ordinary Metal NLL batch declined after {completed} chunks"
12623                        ))
12624                    };
12625                }
12626                MetalPrefillOutcome::Failed => {
12627                    return MetalBatchNllOutcome::Failed(
12628                        "ordinary Metal NLL batch failed after admission".to_string(),
12629                    );
12630                }
12631                MetalPrefillOutcome::Completed(_) => {}
12632            }
12633            completed += 1;
12634            let vocab = self.vocab_size.min(lm.1);
12635            if logits.len() != (end - pos) * lm.1 || vocab == 0 {
12636                return MetalBatchNllOutcome::Failed(
12637                    "ordinary Metal NLL head returned an invalid shape".to_string(),
12638                );
12639            }
12640            for row in 0..(end - pos) {
12641                let absolute = pos + row;
12642                if absolute < start || absolute + 1 >= ids.len() {
12643                    continue;
12644                }
12645                let lg = &mut logits[row * lm.1..row * lm.1 + vocab];
12646                if let Some(mu) = self.logit_multiplier {
12647                    for v in lg.iter_mut() {
12648                        *v *= mu;
12649                    }
12650                }
12651                if let Some(c) = self.final_softcap {
12652                    softcap_in_place(lg, c, self.pool.as_deref());
12653                }
12654                let target = ids[absolute + 1] as usize;
12655                if target >= vocab {
12656                    return MetalBatchNllOutcome::Failed(format!(
12657                        "target token {target} exceeds Metal head rows {vocab}"
12658                    ));
12659                }
12660                let max = lg.iter().fold(f32::NEG_INFINITY, |m, &v| m.max(v));
12661                let lse: f64 = lg
12662                    .iter()
12663                    .map(|&v| ((v - max) as f64).exp())
12664                    .sum::<f64>()
12665                    .ln()
12666                    + max as f64;
12667                nll += lse - lg[target] as f64;
12668                count += 1;
12669            }
12670            pos = end;
12671        }
12672        MetalBatchNllOutcome::Completed(nll, count)
12673    }
12674
12675    /// Commit a Metal verify round: replay the GDN recurrences over the
12676    /// `a + 1` accepted positions into the CPU states, append the accepted
12677    /// K/V rows from the mirrors to the CPU caches, re-point the mirrors.
12678    #[cfg(target_os = "macos")]
12679    fn metal_verify_commit(&mut self, a: usize) -> bool {
12680        let Some(mut pending) = self.metal_verify.take() else {
12681            return false;
12682        };
12683        let n = a + 1;
12684        // encode order == ascending layer order (the plan walks 0..layers)
12685        let idxs = pending.gdn_layers.clone();
12686        let mut outs: Vec<&mut [f32]> = self
12687            .kv_cache
12688            .layers
12689            .iter_mut()
12690            .enumerate()
12691            .filter(|(i, _)| idxs.binary_search(i).is_ok())
12692            .map(|(_, l)| l.linear_state.as_mut_slice())
12693            .collect();
12694        if !pending.graph.commit(n, &mut outs) {
12695            return false;
12696        }
12697        spec_stamp("c.replay");
12698        let (nkv, hd) = (self.num_kv_heads, self.head_dim);
12699        // Read every layer before mutating any CPU cache.  Missing rows are
12700        // terminal after the replay has executed; never append a partial KV
12701        // prefix and continue on a serial path.
12702        let mut rows = Vec::with_capacity(pending.attn_layers.len());
12703        for (li, cpu_stored) in &pending.attn_layers {
12704            let mut kbuf = vec![0f32; n * nkv * hd];
12705            let mut vbuf = vec![0f32; n * nkv * hd];
12706            if !crate::gpu_metal::kv_mirror_read_rows(
12707                self.graph_kv_id,
12708                *li,
12709                nkv,
12710                hd,
12711                *cpu_stored,
12712                n,
12713                &mut kbuf,
12714                &mut vbuf,
12715            ) {
12716                return false;
12717            }
12718            rows.push((*li, *cpu_stored, kbuf, vbuf));
12719        }
12720        for (li, cpu_stored, kbuf, vbuf) in rows {
12721            let cache = &mut self.kv_cache.layers[li];
12722            for r in 0..n {
12723                cache.append(
12724                    &kbuf[r * nkv * hd..(r + 1) * nkv * hd],
12725                    &vbuf[r * nkv * hd..(r + 1) * nkv * hd],
12726                    &[],
12727                );
12728            }
12729            crate::gpu_metal::kv_mirror_set_stored(self.graph_kv_id, li, cpu_stored + n);
12730        }
12731        spec_stamp("c.kv");
12732        true
12733    }
12734
12735    /// The round's warm-ups as ONE b-row graph run over the MTP block on
12736    /// Metal: `pairs` = (trunk hidden, next token) at consecutive positions
12737    /// from `first_pos`; the block's input projection is folded in. This
12738    /// half encodes and SUBMITS (no wait); `mtp_warm_batch_finish` waits
12739    /// and pulls the appended K/V rows into the CPU MTP cache. None = the
12740    /// graph declined (nothing submitted, nothing appended).
12741    #[cfg(target_os = "macos")]
12742    fn mtp_warm_batch_submit(
12743        &mut self,
12744        m: &mut MtpModule,
12745        pairs: &[(&[f32], u32)],
12746        first_pos: usize,
12747    ) -> Option<MetalWarmPending> {
12748        use crate::gpu_metal::{AttnDeviceParams, AttnGpuLayer, GraphDims, MetalFfn, VerifyGraph};
12749        let b = pairs.len();
12750        if b == 0 || b > 512 || m.kv.mode != crate::kv_cache::KvMode::F32 || m.kv.o1.is_some() {
12751            return None;
12752        }
12753        let AttnKind::Full {
12754            wq,
12755            wk,
12756            wv,
12757            wo,
12758            q_norm,
12759            k_norm,
12760            output_gate,
12761            softplus_gate: None,
12762            bias: None,
12763        } = &m.layer.attn
12764        else {
12765            return None;
12766        };
12767        let FfnKind::Dense(d) = &m.layer.ffn else {
12768            return None;
12769        };
12770        if !d.segs.is_empty() {
12771            return None;
12772        }
12773        let (Some(pq), Some(pk), Some(pv), Some(po)) =
12774            (wq.q1_parts(), wk.q1_parts(), wv.q1_parts(), wo.q1_parts())
12775        else {
12776            return None;
12777        };
12778        let (Some(g), Some(u), Some(dn)) = (
12779            d.gate_proj.q1_parts(),
12780            d.up_proj.q1_parts(),
12781            d.down_proj.q1_parts(),
12782        ) else {
12783            return None;
12784        };
12785        let Some(eh) = m.eh_proj.q1_parts() else {
12786            return None;
12787        };
12788        let QTensor::Mapped { model, .. } = wq else {
12789            return None;
12790        };
12791        let model = model.clone();
12792        let hs = self.hidden_size;
12793        // [enorm(embed(tok)); hnorm(hidden)] rows
12794        let mut cat = vec![0f32; b * 2 * hs];
12795        for (j, (h, tok)) in pairs.iter().enumerate() {
12796            let e = self.embed_single(*tok);
12797            let (ce, ch) = cat[j * 2 * hs..(j + 1) * 2 * hs].split_at_mut(hs);
12798            inference::rms_norm_into(&e, &m.enorm, self.rms_eps, self.norm_style, ce);
12799            inference::rms_norm_into(h, &m.hnorm, self.rms_eps, self.norm_style, ch);
12800        }
12801        let dims = GraphDims {
12802            hidden: hs,
12803            eps: self.rms_eps as f32,
12804            gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
12805        };
12806        spec_stamp("w.cat");
12807        let Some(mut graph) = VerifyGraph::new_via_proj(&model, dims, eh, &cat, b) else {
12808            return None;
12809        };
12810        spec_stamp("w.new");
12811        let l = AttnGpuLayer {
12812            attn_norm: &m.layer.input_norm,
12813            post_norm: &m.layer.post_norm,
12814            wq: pq,
12815            wk: pk,
12816            wv: pv,
12817            wo: po,
12818            ffn: MetalFfn::Dense {
12819                gate: g,
12820                up: u,
12821                down: dn,
12822                act: (d.act == Act::Gelu) as u8,
12823            },
12824            attn_out_norm: None,
12825            ffn_out_norm: None,
12826            layer_scale: None,
12827        };
12828        let (nh, nkv, hd, rd) = (
12829            self.num_heads,
12830            self.num_kv_heads,
12831            self.head_dim,
12832            self.rotary_dim,
12833        );
12834        let inv_freq = self.inv_freq.clone();
12835        let cpu_stored;
12836        {
12837            let cache = &m.kv;
12838            let cpu_k: Vec<&[f32]> = (0..nkv).map(|g| cache.head_keys(g)).collect();
12839            let cpu_v: Vec<&[f32]> = (0..nkv).map(|g| cache.head_values(g)).collect();
12840            cpu_stored = cpu_k[0].len() / hd;
12841            // The cache may LAG the position (rows nobody warmed): the
12842            // pairs land at cpu_stored.. with their true RoPE positions
12843            // first_pos.., exactly what the one-by-one warm does. A cache
12844            // AHEAD of the position is a real inconsistency.
12845            if cpu_stored > first_pos {
12846                spec_stamp("w.decl");
12847                return None;
12848            }
12849            let p = AttnDeviceParams {
12850                kv_id: self.mtp_kv_id(),
12851                layer: Self::MTP_LAYER_BASE,
12852                nh,
12853                nkv,
12854                hd,
12855                rd,
12856                position: first_pos,
12857                scale: self.attn_scale,
12858                eps: self.rms_eps as f32,
12859                gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
12860                late_qk_norm: self.qk_norm_after_rope,
12861                output_gate: *output_gate,
12862                q_norm: q_norm.as_deref(),
12863                k_norm: k_norm.as_deref(),
12864                inv_freq: &inv_freq,
12865                rope_scale: self.rope_scale,
12866                cpu_k,
12867                cpu_v,
12868                cpu_stored,
12869                cpu_gen: cache.generation(),
12870                o1: None,
12871                window: None,
12872                head_gate: None,
12873                v_norm: false,
12874            };
12875            if !graph.attn_ok(&l, &p) || !graph.encode_attn_b(&l, &p) {
12876                return None;
12877            }
12878        }
12879        spec_stamp("w.enc");
12880        if !graph.submit() {
12881            return None;
12882        }
12883        spec_stamp("w.sub");
12884        Some(MetalWarmPending {
12885            graph,
12886            cpu_stored,
12887            b,
12888        })
12889    }
12890
12891    /// Submit and finish in one call (the prefill's MTP warm-up, where
12892    /// nothing runs in between).
12893    #[cfg(target_os = "macos")]
12894    fn mtp_warm_batch_metal(
12895        &mut self,
12896        m: &mut MtpModule,
12897        pairs: &[(&[f32], u32)],
12898        first_pos: usize,
12899    ) -> bool {
12900        match self.mtp_warm_batch_submit(m, pairs, first_pos) {
12901            Some(p) => self.mtp_warm_batch_finish(m, p),
12902            None => false,
12903        }
12904    }
12905
12906    /// Second half of the batched warm-up: wait for the submitted graph,
12907    /// pull its b appended K/V rows into the CPU MTP cache, re-point the
12908    /// mirror. False = the command buffer failed or the rows are missing
12909    /// (nothing appended; the caller falls back to the one-by-one warm).
12910    #[cfg(target_os = "macos")]
12911    fn mtp_warm_batch_finish(&mut self, m: &mut MtpModule, pending: MetalWarmPending) -> bool {
12912        let MetalWarmPending {
12913            mut graph,
12914            cpu_stored,
12915            b,
12916        } = pending;
12917        let (nkv, hd) = (self.num_kv_heads, self.head_dim);
12918        if !graph.sync() {
12919            return false;
12920        }
12921        spec_stamp("w.gpu");
12922        let mut kbuf = vec![0f32; b * nkv * hd];
12923        let mut vbuf = vec![0f32; b * nkv * hd];
12924        if !crate::gpu_metal::kv_mirror_read_rows(
12925            self.mtp_kv_id(),
12926            Self::MTP_LAYER_BASE,
12927            nkv,
12928            hd,
12929            cpu_stored,
12930            b,
12931            &mut kbuf,
12932            &mut vbuf,
12933        ) {
12934            return false;
12935        }
12936        for r in 0..b {
12937            m.kv.append(
12938                &kbuf[r * nkv * hd..(r + 1) * nkv * hd],
12939                &vbuf[r * nkv * hd..(r + 1) * nkv * hd],
12940                &[],
12941            );
12942        }
12943        crate::gpu_metal::kv_mirror_set_stored(
12944            self.mtp_kv_id(),
12945            Self::MTP_LAYER_BASE,
12946            cpu_stored + b,
12947        );
12948        spec_stamp("w.kv");
12949        true
12950    }
12951
12952    /// A committed token id from the high table (Cyrillic, CJK and the
12953    /// like sit above 131072 in Qwen's vocabulary; Latin subwords past
12954    /// the 65536 cut are rare enough to lose as rejected drafts) switches
12955    /// the draft to the full head for the next 16 tokens; other ids count
12956    /// down. On an M4 the full 660 MB head costs 5.5 ms a draft step
12957    /// against 1.4 for the shortlist, so the streak is kept short.
12958    pub(crate) fn note_draft_id(&mut self, id: u32) {
12959        let cut = Self::draft_vocab_rows(usize::MAX).max(131_072);
12960        if (id as usize) >= cut {
12961            self.draft_full_streak = 16;
12962        } else {
12963            self.draft_full_streak = self.draft_full_streak.saturating_sub(1);
12964        }
12965    }
12966
12967    /// The draft head's rows for the next step: the shortlist, or the full
12968    /// head while `draft_full_streak` runs.
12969    fn draft_head_rows(&self, head_rows: usize) -> usize {
12970        if self.draft_full_streak > 0 {
12971            head_rows
12972        } else {
12973            Self::draft_vocab_rows(head_rows)
12974        }
12975    }
12976
12977    /// Draft-head shortlist size: `CMF_DRAFT_VOCAB` rows (default 65536,
12978    /// capped at the head; 0 = full head).
12979    fn draft_vocab_rows(head_rows: usize) -> usize {
12980        static N: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
12981        let n = *N.get_or_init(|| {
12982            std::env::var("CMF_DRAFT_VOCAB")
12983                .ok()
12984                .and_then(|v| v.parse().ok())
12985                .unwrap_or(65536)
12986        });
12987        if n == 0 { head_rows } else { n.min(head_rows) }
12988    }
12989
12990    /// One MTP block step on the native Metal token graph: block input on
12991    /// the host, the attention layer + FFN device-resident over the MTP
12992    /// mirror, the head folded in when `want_logits`. The appended K/V row
12993    /// is pulled into the CPU MTP cache (owner of record) after the sync.
12994    #[cfg(target_os = "macos")]
12995    fn mtp_step_metal(
12996        &mut self,
12997        m: &mut MtpModule,
12998        hidden: &[f32],
12999        next_token: u32,
13000        position: usize,
13001        want_logits: bool,
13002    ) -> Option<(Vec<f32>, Vec<f32>)> {
13003        use crate::gpu_metal::{AttnDeviceParams, AttnGpuLayer, GraphDims, MetalFfn, TokenGraph};
13004        if std::env::var("CMF_MTP_GRAPH").as_deref() == Ok("0")
13005            || !crate::gpu::q1_force()
13006            || !crate::gpu::enabled_here()
13007            || self.attn_softcap > 0.0
13008            || self.attention_heads_per_layer.is_some()
13009            || m.kv.mode != crate::kv_cache::KvMode::F32
13010            || m.kv.o1.is_some()
13011        {
13012            return None;
13013        }
13014        let AttnKind::Full {
13015            wq,
13016            wk,
13017            wv,
13018            wo,
13019            q_norm,
13020            k_norm,
13021            output_gate,
13022            softplus_gate: None,
13023            bias: None,
13024        } = &m.layer.attn
13025        else {
13026            return None;
13027        };
13028        let FfnKind::Dense(d) = &m.layer.ffn else {
13029            return None;
13030        };
13031        if d.act != Act::Silu || !d.segs.is_empty() {
13032            return None;
13033        }
13034        let (pq, pk, pv, po) = (
13035            wq.q1_parts()?,
13036            wk.q1_parts()?,
13037            wv.q1_parts()?,
13038            wo.q1_parts()?,
13039        );
13040        let (g, u, dn) = (
13041            d.gate_proj.q1_parts()?,
13042            d.up_proj.q1_parts()?,
13043            d.down_proj.q1_parts()?,
13044        );
13045        let QTensor::Mapped { model, .. } = wq else {
13046            return None;
13047        };
13048        let model = model.clone();
13049        let lm = if want_logits {
13050            Some(self.weights.lm_head.q1_parts()?)
13051        } else {
13052            None
13053        };
13054        let dims = GraphDims {
13055            hidden: self.hidden_size,
13056            eps: self.rms_eps as f32,
13057            gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
13058        };
13059        // The block input `eh_proj · [enorm(e); hnorm(h)]` rides in the
13060        // graph (one submit a step); the host per-op matvec if it cannot.
13061        let hs = self.hidden_size;
13062        let mut x = vec![0f32; hs];
13063        let mut graph = TokenGraph::new(&model, dims, &x)?;
13064        let mut folded = false;
13065        if let Some(eh) = m.eh_proj.q1_parts() {
13066            let e = self.embed_single(next_token);
13067            let mut cat = vec![0.0f32; 2 * hs];
13068            let (cat_e, cat_h) = cat.split_at_mut(hs);
13069            inference::rms_norm_into(&e, &m.enorm, self.rms_eps, self.norm_style, cat_e);
13070            inference::rms_norm_into(hidden, &m.hnorm, self.rms_eps, self.norm_style, cat_h);
13071            folded = graph.encode_input_proj(eh, &cat);
13072        }
13073        if !folded {
13074            x = self.mtp_block_input(m, hidden, next_token);
13075            graph = TokenGraph::new(&model, dims, &x)?;
13076        }
13077        spec_stamp("d.in");
13078        let l = AttnGpuLayer {
13079            attn_norm: &m.layer.input_norm,
13080            post_norm: &m.layer.post_norm,
13081            wq: pq,
13082            wk: pk,
13083            wv: pv,
13084            wo: po,
13085            ffn: MetalFfn::Dense {
13086                gate: g,
13087                up: u,
13088                down: dn,
13089                act: (d.act == Act::Gelu) as u8,
13090            },
13091            attn_out_norm: None,
13092            ffn_out_norm: None,
13093            layer_scale: None,
13094        };
13095        let (nh, nkv, hd, rd) = (
13096            self.num_heads,
13097            self.num_kv_heads,
13098            self.head_dim,
13099            self.rotary_dim,
13100        );
13101        let inv_freq = self.inv_freq.clone();
13102        {
13103            let cache = &m.kv;
13104            let cpu_k: Vec<&[f32]> = (0..nkv).map(|g| cache.head_keys(g)).collect();
13105            let cpu_v: Vec<&[f32]> = (0..nkv).map(|g| cache.head_values(g)).collect();
13106            let cpu_stored = cpu_k[0].len() / hd;
13107            let p = AttnDeviceParams {
13108                kv_id: self.mtp_kv_id(),
13109                layer: Self::MTP_LAYER_BASE,
13110                nh,
13111                nkv,
13112                hd,
13113                rd,
13114                position,
13115                scale: self.attn_scale,
13116                eps: self.rms_eps as f32,
13117                gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
13118                late_qk_norm: self.qk_norm_after_rope,
13119                output_gate: *output_gate,
13120                q_norm: q_norm.as_deref(),
13121                k_norm: k_norm.as_deref(),
13122                inv_freq: &inv_freq,
13123                rope_scale: self.rope_scale,
13124                cpu_k,
13125                cpu_v,
13126                cpu_stored,
13127                cpu_gen: cache.generation(),
13128                o1: None,
13129                window: None,
13130                head_gate: None,
13131                v_norm: false,
13132            };
13133            if !graph.attn_device_ok(&l, &p) || !graph.encode_attn_device(&l, &p) {
13134                return None;
13135            }
13136        }
13137        // The draft's head over a vocabulary SHORTLIST (the first
13138        // CMF_DRAFT_VOCAB rows — BPE ids run roughly by merge rank, so the
13139        // low ids carry the mass): the verify keeps the full head, so a true
13140        // token past the cut is only a rejected draft, never a wrong token.
13141        // 662 MB a step on Qwen3.8 becomes 170 MB at 65536.
13142        let draft_rows = if let Some(lm) = lm {
13143            self.draft_head_rows(lm.1)
13144        } else {
13145            0
13146        };
13147        if let Some(lm) = lm {
13148            if !graph.lm_head_ok(lm) {
13149                return None;
13150            }
13151            if draft_rows < lm.1 {
13152                if !graph.encode_lm_head_part(&m.final_norm, lm, draft_rows) {
13153                    return None;
13154                }
13155            } else {
13156                graph.encode_lm_head(&m.final_norm, lm);
13157            }
13158        }
13159        spec_stamp("d.enc");
13160        if graph.sync_checked().is_err() {
13161            return None;
13162        }
13163        spec_stamp("d.gpu");
13164        let mut logits = Vec::new();
13165        if let Some(lm) = lm {
13166            let n_read = draft_rows.min(lm.1).min(self.vocab_size);
13167            logits = attention::take_buf(n_read);
13168            graph.read_logits(&mut logits);
13169            // ids past the shortlist: never drafted (−∞ in every chain)
13170            logits.resize(self.vocab_size, f32::NEG_INFINITY);
13171        }
13172        graph.finish(&mut x);
13173        let mut krow = attention::take_buf(nkv * hd);
13174        let mut vrow = attention::take_buf(nkv * hd);
13175        if crate::gpu_metal::kv_mirror_read_last(
13176            self.mtp_kv_id(),
13177            Self::MTP_LAYER_BASE,
13178            nkv,
13179            hd,
13180            &mut krow,
13181            &mut vrow,
13182        ) {
13183            m.kv.append(&krow, &vrow, &[]);
13184        }
13185        attention::recycle_buf(&mut krow);
13186        attention::recycle_buf(&mut vrow);
13187        spec_stamp("d.rd");
13188        Some((logits, x))
13189    }
13190
13191    /// `CMF_MTP_CHAIN=0` keeps the per-step draft (one submit and one
13192    /// host round trip per MTP step); the default drafts the whole chain
13193    /// in one command buffer when the round is plain greedy.
13194    ///
13195    /// Measured on an M4 (24 GB), Qwen3.8-27B q4tp, P3 at 160 tokens,
13196    /// k=7, six runs per arm alternating inside one lock window — the
13197    /// round's draft phase (median over the 34 rounds of a run) is
13198    /// 34.5 ms per round old against 30.1 new, i.e. 4.93 → 4.31 ms per
13199    /// draft step. That is the whole prize: the 7 submits cost ~0.6 ms
13200    /// each in host and submit latency and nothing else changes —
13201    /// acceptance (3.41 of 7) and tokens per round (4.41) are identical,
13202    /// and the round is 289 → 285 ms, decode 13.8 → 14.0 tok/s.
13203    fn mtp_chain_on() -> bool {
13204        static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
13205        *ON.get_or_init(|| std::env::var("CMF_MTP_CHAIN").as_deref() != Ok("0"))
13206    }
13207
13208    /// The round's k greedy drafts as ONE command buffer on Metal: the MTP
13209    /// block k times back to back, each step's token embedding gathered
13210    /// on the device from the argmax the step before it wrote, the head
13211    /// over the round's shortlist (or the full head during a full-head
13212    /// streak — decided once, before the chain, exactly as the per-step
13213    /// path decides it per step, since `draft_full_streak` only moves on
13214    /// a commit). One wait, then the k ids and the k appended K/V rows
13215    /// come back; the CPU MTP cache ends where k `mtp_step_metal` calls
13216    /// would have left it. `Err(false)` = declined before anything was
13217    /// committed (the per-step path takes the round); `Err(true)` = the
13218    /// command buffer failed after commit.
13219    #[cfg(target_os = "macos")]
13220    fn mtp_draft_chain_metal(
13221        &mut self,
13222        m: &mut MtpModule,
13223        hidden: &[f32],
13224        t_next: u32,
13225        position: usize,
13226        k: usize,
13227    ) -> Result<Vec<u32>, bool> {
13228        use crate::gpu_metal::{AttnDeviceParams, AttnGpuLayer, GraphDims, MetalFfn, TokenGraph};
13229        if k == 0
13230            || k > 64
13231            || !Self::mtp_chain_on()
13232            || std::env::var("CMF_MTP_GRAPH").as_deref() == Ok("0")
13233            || !crate::gpu::q1_force()
13234            || !crate::gpu::enabled_here()
13235            || self.attn_softcap > 0.0
13236            || self.attention_heads_per_layer.is_some()
13237            || m.kv.mode != crate::kv_cache::KvMode::F32
13238            || m.kv.o1.is_some()
13239            // the chain gathers embeddings itself: only the plain table
13240            || self.dsv4.is_some()
13241            || self.dsv41.is_some()
13242            || self.qwen4_exp.is_some()
13243            || self.g3n.is_some()
13244        {
13245            return Err(false);
13246        }
13247        let AttnKind::Full {
13248            wq,
13249            wk,
13250            wv,
13251            wo,
13252            q_norm,
13253            k_norm,
13254            output_gate,
13255            softplus_gate: None,
13256            bias: None,
13257        } = &m.layer.attn
13258        else {
13259            return Err(false);
13260        };
13261        let FfnKind::Dense(d) = &m.layer.ffn else {
13262            return Err(false);
13263        };
13264        if d.act != Act::Silu || !d.segs.is_empty() {
13265            return Err(false);
13266        }
13267        let (Some(pq), Some(pk), Some(pv), Some(po)) =
13268            (wq.q1_parts(), wk.q1_parts(), wv.q1_parts(), wo.q1_parts())
13269        else {
13270            return Err(false);
13271        };
13272        let (Some(g), Some(u), Some(dn)) = (
13273            d.gate_proj.q1_parts(),
13274            d.up_proj.q1_parts(),
13275            d.down_proj.q1_parts(),
13276        ) else {
13277            return Err(false);
13278        };
13279        let (Some(eh), Some(lm)) = (m.eh_proj.q1_parts(), self.weights.lm_head.q1_parts()) else {
13280            return Err(false);
13281        };
13282        let QTensor::Mapped { model, .. } = wq else {
13283            return Err(false);
13284        };
13285        let model = model.clone();
13286        // the embedding table: a q4tp tensor of the SAME blob, no Prism
13287        // inverse-embedding post-pass
13288        let QTensor::Mapped {
13289            model: em,
13290            idx: eidx,
13291            dtype: cortiq_core::TensorDtype::Q4TiledP,
13292            ..
13293        } = &self.weights.embed_tokens
13294        else {
13295            return Err(false);
13296        };
13297        if !std::sync::Arc::ptr_eq(em, &model)
13298            || crate::prism::is_inverse_embedding(&model, &model.tensors[*eidx].name)
13299        {
13300            return Err(false);
13301        }
13302        let embed = (
13303            *eidx,
13304            self.weights.embed_tokens.rows(),
13305            self.weights.embed_tokens.cols(),
13306        );
13307        if embed.2 != self.hidden_size || hidden.len() != self.hidden_size {
13308            return Err(false);
13309        }
13310        let dims = GraphDims {
13311            hidden: self.hidden_size,
13312            eps: self.rms_eps as f32,
13313            gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
13314        };
13315        let Some(mut graph) = TokenGraph::new(&model, dims, hidden) else {
13316            return Err(false);
13317        };
13318        if !graph.chain_embed_ok(embed) || !graph.lm_head_ok(lm) {
13319            return Err(false);
13320        }
13321        let l = AttnGpuLayer {
13322            attn_norm: &m.layer.input_norm,
13323            post_norm: &m.layer.post_norm,
13324            wq: pq,
13325            wk: pk,
13326            wv: pv,
13327            wo: po,
13328            ffn: MetalFfn::Dense {
13329                gate: g,
13330                up: u,
13331                down: dn,
13332                act: (d.act == Act::Gelu) as u8,
13333            },
13334            attn_out_norm: None,
13335            ffn_out_norm: None,
13336            layer_scale: None,
13337        };
13338        let (nh, nkv, hd, rd) = (
13339            self.num_heads,
13340            self.num_kv_heads,
13341            self.head_dim,
13342            self.rotary_dim,
13343        );
13344        let inv_freq = self.inv_freq.clone();
13345        let draft_rows = self.draft_head_rows(lm.1);
13346        let n_arg = draft_rows.min(lm.1).min(self.vocab_size);
13347        if n_arg == 0 {
13348            return Err(false);
13349        }
13350        // `CMF_MTP_CHAIN_SPLIT=1` commits each step as it is encoded, so
13351        // the GPU starts on step 0 while the host is still encoding step
13352        // 1 — a probe for whether the host encode is on the critical
13353        // path. It is not: three runs each, draft 30.0 ms per round split
13354        // against 30.1 whole, and the whole chain's host encode measures
13355        // 0.3 ms against a 29.7 ms wait. Kept as a probe, off by default.
13356        let split = std::env::var("CMF_MTP_CHAIN_SPLIT").as_deref() == Ok("1");
13357        let t_chain = std::time::Instant::now();
13358        graph.chain_ids_init(t_next, k);
13359        let cpu_stored;
13360        {
13361            let cache = &m.kv;
13362            let cpu_k: Vec<&[f32]> = (0..nkv).map(|g| cache.head_keys(g)).collect();
13363            let cpu_v: Vec<&[f32]> = (0..nkv).map(|g| cache.head_values(g)).collect();
13364            cpu_stored = cpu_k[0].len() / hd;
13365            for j in 0..k {
13366                if !graph.encode_chain_input(
13367                    embed,
13368                    j as u32,
13369                    &m.enorm,
13370                    &m.hnorm,
13371                    self.embed_multiplier,
13372                    eh,
13373                ) {
13374                    return Err(false);
13375                }
13376                // step j's mirror row: the mirror is re-pointed at the CPU
13377                // rows before step 0 and advances by one per step; its
13378                // resync (never taken past step 0) reads the CPU rows
13379                let p = AttnDeviceParams {
13380                    kv_id: self.mtp_kv_id(),
13381                    layer: Self::MTP_LAYER_BASE,
13382                    nh,
13383                    nkv,
13384                    hd,
13385                    rd,
13386                    position: position + j,
13387                    scale: self.attn_scale,
13388                    eps: self.rms_eps as f32,
13389                    gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
13390                    late_qk_norm: self.qk_norm_after_rope,
13391                    output_gate: *output_gate,
13392                    q_norm: q_norm.as_deref(),
13393                    k_norm: k_norm.as_deref(),
13394                    inv_freq: &inv_freq,
13395                    rope_scale: self.rope_scale,
13396                    cpu_k: cpu_k.clone(),
13397                    cpu_v: cpu_v.clone(),
13398                    cpu_stored: cpu_stored + j,
13399                    cpu_gen: cache.generation(),
13400                    o1: None,
13401                    window: None,
13402                    head_gate: None,
13403                    v_norm: false,
13404                };
13405                if !graph.attn_device_ok(&l, &p) || !graph.encode_attn_device(&l, &p) {
13406                    return Err(false);
13407                }
13408                if draft_rows < lm.1 {
13409                    if !graph.encode_lm_head_part(&m.final_norm, lm, draft_rows) {
13410                        return Err(false);
13411                    }
13412                } else {
13413                    graph.encode_lm_head(&m.final_norm, lm);
13414                }
13415                if !graph.encode_argmax(n_arg, j as u32 + 1) {
13416                    return Err(false);
13417                }
13418                if split {
13419                    // CMF_MTP_CHAIN_SPLIT=1: commit every step so the GPU
13420                    // starts on step 0 while the host encodes the rest
13421                    graph.commit();
13422                }
13423            }
13424        }
13425        let t_enc = t_chain.elapsed();
13426        if graph.sync_checked().is_err() {
13427            return Err(true);
13428        }
13429        if std::env::var_os("CMF_GRAPH_SPEC_TIME").is_some() {
13430            eprintln!(
13431                "mtp-chain: encode {:.1} ms | wait {:.1} ms (k={k}, head rows {draft_rows}{})",
13432                t_enc.as_secs_f64() * 1e3,
13433                (t_chain.elapsed() - t_enc).as_secs_f64() * 1e3,
13434                if split { ", split" } else { "" }
13435            );
13436        }
13437        let mut ids = vec![0u32; k];
13438        if !graph.chain_ids_read(&mut ids) {
13439            return Err(true);
13440        }
13441        let mut kbuf = vec![0f32; k * nkv * hd];
13442        let mut vbuf = vec![0f32; k * nkv * hd];
13443        if !crate::gpu_metal::kv_mirror_read_rows(
13444            self.mtp_kv_id(),
13445            Self::MTP_LAYER_BASE,
13446            nkv,
13447            hd,
13448            cpu_stored,
13449            k,
13450            &mut kbuf,
13451            &mut vbuf,
13452        ) {
13453            return Err(true);
13454        }
13455        for r in 0..k {
13456            m.kv.append(
13457                &kbuf[r * nkv * hd..(r + 1) * nkv * hd],
13458                &vbuf[r * nkv * hd..(r + 1) * nkv * hd],
13459                &[],
13460            );
13461        }
13462        Ok(ids)
13463    }
13464
13465    fn try_batch_graph_wgpu(
13466        &self,
13467        hiddens: &mut [f32],
13468        positions: &[usize],
13469        k: usize,
13470        spec: Option<crate::gpu::SpecTail<'_>>,
13471    ) -> crate::gpu::BatchGraphOutcome {
13472        self.try_batch_graph_wgpu_prefix(hiddens, positions, k, spec, None)
13473    }
13474
13475    /// `try_batch_graph_wgpu` with the device-prefix mode: `layers_run`
13476    /// Some lets a stack that does not fit run its leading layers (the
13477    /// token graph's prefix rule) and reports how many; `hiddens` then
13478    /// holds the boundary rows and the caller runs the rest on the host.
13479    fn try_batch_graph_wgpu_prefix(
13480        &self,
13481        hiddens: &mut [f32],
13482        positions: &[usize],
13483        k: usize,
13484        spec: Option<crate::gpu::SpecTail<'_>>,
13485        layers_run: Option<&mut usize>,
13486    ) -> crate::gpu::BatchGraphOutcome {
13487        let graph_end = match self.mimo_moe.graph_prefix_end() {
13488            Some(end) if end < self.num_layers => {
13489                if layers_run.is_none() || spec.is_some() || end == 0 {
13490                    return crate::gpu::BatchGraphOutcome::Declined;
13491                }
13492                end
13493            }
13494            _ => self.num_layers,
13495        };
13496        let _tb = std::time::Instant::now();
13497        let batch_debug = std::env::var_os("CMF_BATCH_DEBUG").is_some();
13498        if self.attn_softcap > 0.0 {
13499            return crate::gpu::BatchGraphOutcome::Declined; // capped scores: no graph kernel — CPU path
13500        }
13501        // Same attention contract as the token graph: per-layer geometry
13502        // rides `geom`, anything it cannot express declines by name.
13503        if let Some(reason) = self
13504            .wgpu_graph_attn_decline()
13505            .or_else(|| self.batch_graph_gemma_decline())
13506        {
13507            self.note_graph_decline("wgpu batch graph", reason);
13508            return crate::gpu::BatchGraphOutcome::Declined;
13509        }
13510        let nh = self.num_heads;
13511        let (nkv, hd, rd) = self.layer_geom(0);
13512        let gemma = self.norm_style == cortiq_core::NormStyle::Gemma;
13513        fn gw(t: &QTensor) -> Option<crate::gpu::GraphW<'_>> {
13514            if let Some((m, i, kind, rs)) = t
13515                .graph_weight()
13516                .or_else(|| t.graph_weight_descriptor())
13517            {
13518                let name = &m.tensors[i].name;
13519                let prism = if crate::prism::is_inverse_embedding(m, name) {
13520                    crate::gpu::GraphPrismOp::InverseEmbedding
13521                } else if crate::prism::is_forward_weight(m, name) {
13522                    crate::gpu::GraphPrismOp::Forward
13523                } else {
13524                    crate::gpu::GraphPrismOp::None
13525                };
13526                return Some(crate::gpu::GraphW {
13527                    idx: i,
13528                    kind,
13529                    row_scale: rs,
13530                    data: &[],
13531                    prism,
13532                    affine: crate::prism::is_affine_target(m, name),
13533                });
13534            }
13535            if std::env::var_os("CMF_BATCH_DEBUG").is_some() {
13536                eprintln!(
13537                    "batch graph: tensor has no graph descriptor/f32 fallback rows={} cols={}",
13538                    t.rows(),
13539                    t.cols()
13540                );
13541            }
13542            t.as_f32().map(|d| crate::gpu::GraphW {
13543                idx: 0,
13544                kind: 4,
13545                row_scale: &[],
13546                data: d,
13547                prism: crate::gpu::GraphPrismOp::None,
13548                affine: false,
13549            })
13550        }
13551        let built: Option<(
13552            Vec<crate::gpu::GraphLayer<'_>>,
13553            std::sync::Arc<cortiq_core::CmfModel>,
13554        )> = (|| {
13555            let mut layers = Vec::with_capacity(graph_end);
13556            let mut model = None;
13557            for li in 0..graph_end {
13558                let lw = &self.weights.layers[self.phys_layer(li)];
13559                // MoE routes per token, so its experts are encoded token by
13560                // token inside the batched submit while attention and the
13561                // projections stay GEMMs. Refusing MoE here is what left
13562                // prefill running one position at a time: 33 tok/s against
13563                // 54 on decode, i.e. reading the prompt was slower than
13564                // writing the answer.
13565                let gffn = match &lw.ffn {
13566                    FfnKind::Dense(d) if !d.segs.is_empty() => {
13567                        if batch_debug {
13568                            eprintln!("batch graph: dense segmented FFN at layer {li}");
13569                        }
13570                        return None;
13571                    }
13572                    FfnKind::Dense(d) => {
13573                        let Some(act) = d.act.graph_act() else {
13574                            if batch_debug {
13575                                eprintln!(
13576                                    "batch graph: dense FFN activation {:?} without a graph kernel at layer {li}",
13577                                    d.act
13578                                );
13579                            }
13580                            return None;
13581                        };
13582                        crate::gpu::GraphFfn::Dense {
13583                            gate: gw(&d.gate_proj)?,
13584                            up: gw(&d.up_proj)?,
13585                            down: gw(&d.down_proj)?,
13586                            act,
13587                        }
13588                    }
13589                    FfnKind::Moe(m) => {
13590                        // Adaptive τ and expert masks stay on the CPU path.
13591                        // Sigmoid scores, the selection bias, a routed scale
13592                        // ≠ 1 and an ungated shared expert (hy_v3) ride the
13593                        // same flags word as the token graph — before, this
13594                        // refusal sent every Hy-MT2-30B prompt to the chunked
13595                        // fallback (8 tok/s of ingest against 53 of decode).
13596                        if m.route_tau.is_some() || m.mask.is_some() {
13597                            return None;
13598                        }
13599                        // A shared expert rides as slot top_k (gated or
13600                        // not is a flag on the select kernel); without one
13601                        // (MiMo-V2, LFM2-MoE) the kernels run top_k slots.
13602                        let shared = m.shared.as_ref();
13603                        let has_shared = shared.is_some();
13604                        let shared_gated = matches!(shared, Some((_, Some(_))));
13605                        let sgate = match shared {
13606                            Some((_, Some(sg))) => gw(sg)?,
13607                            // Ungated or absent: the router plane stands in
13608                            // so the plumbing stays total; the kernel pins
13609                            // weight 1 or never reads it.
13610                            _ => gw(&m.router)?,
13611                        };
13612                        let router = gw(&m.router)?;
13613                        // The batch MoE kernels still consume raw per-token
13614                        // rows and do not carry the descriptor-aware Prism
13615                        // transform/affine bit for router or shared-gate
13616                        // planes.  Refuse rather than route an untransformed
13617                        // source activation.
13618                        if router.prism != crate::gpu::GraphPrismOp::None
13619                            || router.affine
13620                            || sgate.prism != crate::gpu::GraphPrismOp::None
13621                            || sgate.affine
13622                        {
13623                            return None;
13624                        }
13625                        let inter = m.experts.first()?.gate_proj.rows();
13626                        let mut experts = Vec::with_capacity(m.experts.len() + 1);
13627                        let mut q4tp: Option<bool> = None;
13628                        let mut gu_q2: Option<bool> = None;
13629                        let mut q82: Option<bool> = None;
13630                        for e in m.experts.iter().chain(shared.map(|(se, _)| se)) {
13631                            if !matches!(e.act, Act::Silu)
13632                                || e.gate_proj.rows() != inter
13633                                || e.up_proj.rows() != inter
13634                            {
13635                                return None;
13636                            }
13637                            // Same ladder as the token graph: q4t → q2tp
13638                            // (mixed profile: 2-bit gate/up over a q4tp
13639                            // down) → q4tp → q8_2f. Uniform across the layer.
13640                            let (mm, gi, ui, di, is_p, is_q2, is_8) = match e.gate_proj.mapped_q4t()
13641                            {
13642                                Some((mm, gi)) => (
13643                                    mm,
13644                                    gi,
13645                                    e.up_proj.mapped_q4t()?.1,
13646                                    e.down_proj.mapped_q4t()?.1,
13647                                    false,
13648                                    false,
13649                                    false,
13650                                ),
13651                                None => match e.gate_proj.mapped_q2tp() {
13652                                    Some((mm, gi)) => (
13653                                        mm,
13654                                        gi,
13655                                        e.up_proj.mapped_q2tp()?.1,
13656                                        e.down_proj.mapped_q4tp()?.1,
13657                                        true,
13658                                        true,
13659                                        false,
13660                                    ),
13661                                    None => match e.gate_proj.mapped_q4tp() {
13662                                        Some((mm, gi)) => (
13663                                            mm,
13664                                            gi,
13665                                            e.up_proj.mapped_q4tp()?.1,
13666                                            e.down_proj.mapped_q4tp()?.1,
13667                                            true,
13668                                            false,
13669                                            false,
13670                                        ),
13671                                        None => {
13672                                            if !crate::gpu::moe_q82_graph_on() {
13673                                                return None;
13674                                            }
13675                                            let (mm, gi) = e.gate_proj.mapped_q8_2f()?;
13676                                            (
13677                                                mm,
13678                                                gi,
13679                                                e.up_proj.mapped_q8_2f()?.1,
13680                                                e.down_proj.mapped_q8_2f()?.1,
13681                                                false,
13682                                                false,
13683                                                true,
13684                                            )
13685                                        }
13686                                    },
13687                                },
13688                            };
13689                            if *q4tp.get_or_insert(is_p) != is_p
13690                                || *gu_q2.get_or_insert(is_q2) != is_q2
13691                                || *q82.get_or_insert(is_8) != is_8
13692                            {
13693                                return None;
13694                            }
13695                            if [gi, ui, di].into_iter().any(|idx| {
13696                                mm.tensors
13697                                    .get(idx)
13698                                    .is_some_and(|t| {
13699                                        crate::prism::is_forward_weight(mm, &t.name)
13700                                            || crate::prism::is_affine_target(mm, &t.name)
13701                                    })
13702                            }) {
13703                                return None;
13704                            }
13705                            model.get_or_insert_with(|| mm.clone());
13706                            experts.push((gi, ui, di));
13707                        }
13708                        crate::gpu::GraphFfn::Moe {
13709                            router,
13710                            shared_gate: sgate,
13711                            experts,
13712                            n_exp: m.experts.len(),
13713                            top_k: m.top_k,
13714                            inter,
13715                            norm_topk: m.norm_topk_prob,
13716                            q4tp: q4tp?,
13717                            gu_q2: gu_q2.unwrap_or(false),
13718                            q82: q82.unwrap_or(false),
13719                            sigmoid: m.router_sigmoid,
13720                            bias: m.expert_bias.as_deref(),
13721                            has_shared,
13722                            shared_gated,
13723                            route_scale: m.routed_scaling,
13724                        }
13725                    }
13726                    _ => return None,
13727                };
13728                let attn = match &lw.attn {
13729                    AttnKind::Full {
13730                        wq,
13731                        wk,
13732                        wv,
13733                        wo,
13734                        q_norm,
13735                        k_norm,
13736                        output_gate,
13737                        softplus_gate,
13738                        bias,
13739                    } => {
13740                        if softplus_gate.is_some() || self.attention_heads_per_layer.is_some() {
13741                            if batch_debug {
13742                                eprintln!(
13743                                    "batch graph: unsupported Full attention gate at layer {li} softplus={} heads={}",
13744                                    softplus_gate.is_some(),
13745                                    self.attention_heads_per_layer.is_some()
13746                                );
13747                            }
13748                            return None;
13749                        }
13750                        let (m, _, _, _) = wq
13751                            .graph_weight()
13752                            .or_else(|| wq.graph_weight_descriptor())?;
13753                        model = Some(m.clone());
13754                        crate::gpu::GraphAttn::Full {
13755                            wq: gw(wq)?,
13756                            wk: gw(wk)?,
13757                            wv: gw(wv)?,
13758                            wo: gw(wo)?,
13759                            q_norm: q_norm.as_deref(),
13760                            k_norm: k_norm.as_deref(),
13761                            late_qk_norm: self.qk_norm_after_rope,
13762                            bias: bias
13763                                .as_ref()
13764                                .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
13765                            output_gate: *output_gate,
13766                            cpu_k: self.kv_cache.layers[li].k_heads(),
13767                            cpu_v: self.kv_cache.layers[li].v_heads(),
13768                            cpu_base: self.kv_cache.layers[li].base(),
13769                            geom: self.graph_attn_geom(li),
13770                            // The batched graph has no head-gate arm; a
13771                            // gated layer is refused above.
13772                            head_gate: None,
13773                        }
13774                    }
13775                    AttnKind::LinearGdn(w) => {
13776                        let Some(cfg) = self.gdn_cfg else {
13777                            if batch_debug {
13778                                eprintln!("batch graph: no GDN config at layer {li}");
13779                            }
13780                            return None;
13781                        };
13782                        let (m, _, _, _) = w
13783                            .in_proj_qkv
13784                            .graph_weight()
13785                            .or_else(|| w.in_proj_qkv.graph_weight_descriptor())?;
13786                        model = Some(m.clone());
13787                        crate::gpu::GraphAttn::Gdn {
13788                            qkv: gw(&w.in_proj_qkv)?,
13789                            z: gw(&w.in_proj_z)?,
13790                            a: gw(&w.in_proj_a)?,
13791                            b: gw(&w.in_proj_b)?,
13792                            out: gw(&w.out_proj)?,
13793                            conv1d: &w.conv1d,
13794                            a_log: &w.a_log,
13795                            dt_bias: &w.dt_bias,
13796                            norm: &w.norm,
13797                            nv: cfg.num_v_heads,
13798                            nk: cfg.num_k_heads,
13799                            dk: cfg.key_head_dim,
13800                            dv: cfg.value_head_dim,
13801                            kk: cfg.conv_kernel,
13802                            cpu_state: &self.kv_cache.layers[self.phys_layer(li)].linear_state,
13803                        }
13804                    }
13805                    _ => return None,
13806                };
13807                // Gemma-4's sandwich norms and layer scalar (`gemma_batch`);
13808                // every other model hands the graph none, as before.
13809                let g4 = self.global_attn.is_some() || self.attn_v_norm;
13810                layers.push(crate::gpu::GraphLayer {
13811                    input_norm: &lw.input_norm,
13812                    attn,
13813                    post_norm: &lw.post_norm,
13814                    attn_out_norm: if g4 { lw.attn_out_norm.as_deref() } else { None },
13815                    ffn_out_norm: if g4 { lw.ffn_out_norm.as_deref() } else { None },
13816                    layer_scale: if g4 { lw.layer_scale } else { None },
13817                    ffn: gffn,
13818                });
13819            }
13820            Some((layers, model?))
13821        })();
13822        let Some((layers, model)) = built else {
13823            {
13824                use std::sync::atomic::{AtomicBool, Ordering};
13825                static SAID: AtomicBool = AtomicBool::new(false);
13826                if !SAID.swap(true, Ordering::Relaxed) {
13827                    tracing::warn!("batch graph: BUILDER refused (layer weights/kinds)");
13828                }
13829            }
13830            return crate::gpu::BatchGraphOutcome::Declined;
13831        };
13832        if std::env::var("CMF_GRAPH_SPEC_TIME").is_ok() {
13833            eprintln!("batch-build: {:.1} ms", _tb.elapsed().as_secs_f64() * 1e3);
13834        }
13835        crate::gpu::forward_batch_graph(
13836            &model,
13837            self.graph_kv_id,
13838            &layers,
13839            &self.inv_freq,
13840            hiddens,
13841            nh,
13842            nkv,
13843            hd,
13844            rd,
13845            self.hidden_size,
13846            self.intermediate_size,
13847            positions,
13848            self.kv_cache.max_seq_len,
13849            gemma,
13850            self.rms_eps as f32,
13851            self.attn_scale,
13852            k,
13853            &(0..graph_end)
13854                .map(|li| self.kv_cache.layers[self.phys_layer(li)].o1_views())
13855                .collect::<Vec<_>>(),
13856            self.o1_epoch,
13857            spec,
13858            layers_run,
13859        )
13860    }
13861
13862    /// Same, stopping after layer `upto` inclusive (routing probe φ).
13863    /// `CMF_DSV4_DRAFT_PROBE=1` — grade the draft against what the trunk goes on
13864    /// to produce. Off by default; it runs a whole draft per decoded token.
13865    fn draft_probe() -> bool {
13866        static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
13867        *ON.get_or_init(|| std::env::var("CMF_DSV4_DRAFT_PROBE").is_ok_and(|v| v != "0"))
13868    }
13869
13870    /// `CMF_DSV4_DRAFT_PROBE=1`: measure how much of the draft the trunk
13871    /// would have agreed with, WITHOUT verifying or rolling anything back.
13872    ///
13873    /// The number this produces decides the whole speculation design — at
13874    /// acceptance a, a block of B positions yields 1 + a + a² + ... tokens
13875    /// per trunk pass — so it is worth measuring before any of the machinery
13876    /// that would exploit it exists. Each draft is parked with the position
13877    /// it was made at, and graded as the real tokens arrive.
13878    /// `CMF_DSV4_SPEC=1` — the DeepSeek-V4 speculative decode: draft five
13879    /// on the card, verify them in one batched trunk pass, commit the
13880    /// accepted prefix, roll the rest back.
13881    #[cfg(feature = "gpu")]
13882    fn dsv4_spec_on() -> bool {
13883        static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
13884        *ON.get_or_init(|| {
13885            // Test-only runtime gate: model loading still performs the same
13886            // reservation and trunk packing, which gives rollback parity a
13887            // topology-identical non-speculative control arm.
13888            if let Ok(v) = std::env::var("CMF_DSV4_SPEC_RUN") {
13889                return v != "0";
13890            }
13891            // An explicit value is a diagnostic force/escape hatch.  With no
13892            // knob, speculation is eligible only when model loading reserved
13893            // its bounded pack.  On small q4tp cards the geometric reserve
13894            // gate deliberately leaves this at zero: trying to build DSpark
13895            // after the exact trunk filled VRAM is both slower and a device
13896            // OOM (measured on A40).
13897            std::env::var("CMF_DSV4_SPEC")
13898                .map(|v| v != "0")
13899                .unwrap_or_else(|_| {
13900                    crate::gpu_wgpu::DRAFT_RESERVE.load(std::sync::atomic::Ordering::Relaxed) > 0
13901                })
13902        })
13903    }
13904
13905    /// One speculative round at the decode tip. `t_next` is the token the
13906    /// sampler just committed for `next_pos`. Returns the EXTRA accepted
13907    /// tokens (possibly none) and the new position, with `graph_logits`
13908    /// left holding the last accepted position's logits — exactly what the
13909    /// loop top expects. `None` means "speculate not this round": nothing
13910    /// was committed, the caller forwards normally.
13911    #[cfg(feature = "gpu")]
13912    fn dsv4_spec_step(
13913        &mut self,
13914        tip_token: u32,
13915        t_next: u32,
13916        next_pos: usize,
13917        max_extra: usize,
13918        drafted: &mut usize,
13919        accepted_ctr: &mut usize,
13920    ) -> Option<(Vec<u32>, usize)> {
13921        let t_all = std::time::Instant::now();
13922        if std::env::var("CMF_DSV4_SPEC_TIME").is_ok() {
13923            thread_local! {
13924                static LAST: std::cell::Cell<Option<std::time::Instant>> =
13925                    const { std::cell::Cell::new(None) };
13926            }
13927            LAST.with(|l| {
13928                if let Some(prev) = l.get() {
13929                    eprintln!(
13930                        "между раундами {:.1} мс",
13931                        prev.elapsed().as_secs_f64() * 1e3
13932                    );
13933                }
13934                l.set(Some(std::time::Instant::now()));
13935            });
13936        }
13937        if std::env::var("CMF_DSV4_SPEC_DEBUG").is_ok() {
13938            eprintln!("spec_step: вход pos={next_pos}");
13939        }
13940        let n_layers = self.dsv4.as_ref().map(|b| b.1.len())?;
13941        let cfg = self.dsv4.as_ref().map(|b| b.2)?;
13942        // The draft state and its capture, armed exactly as the probe does.
13943        if self.dspark.is_none() {
13944            let t = crate::dsv4::dspark_targets(&self.dsv4_mtp, &cfg, n_layers);
13945            if t.is_empty() {
13946                return None;
13947            }
13948            crate::dsv4::dspark_arm(&t, cfg.dim);
13949            self.dspark = Some(crate::dsv4::DsparkState::new(
13950                self.dsv4_mtp.len(),
13951                &cfg,
13952                t.len(),
13953            ));
13954        }
13955        let targets = crate::dsv4::dspark_targets(&self.dsv4_mtp, &cfg, n_layers);
13956        let pack = crate::dsv4::dspark_pack_get(&self.dsv4_mtp, &cfg);
13957        if pack.is_none() && std::env::var("CMF_DSV4_SPEC_DEBUG").is_ok() {
13958            eprintln!("spec_step: пак не построился (targets {targets:?})");
13959        }
13960        let pack = pack?;
13961        let block = crate::dsv4::dspark_block();
13962        let b_box = self.dsv4.as_mut()?;
13963        let (g, layers, st) = (&b_box.0, &b_box.1, &mut b_box.3);
13964        let ds = self.dspark.as_mut()?;
13965        // The tip's captures: either this token ran on a normal path that
13966        // filled the thread-local, or the previous spec round left them.
13967        let dbg = std::env::var("CMF_DSV4_SPEC_DEBUG").is_ok();
13968        if !crate::dsv4::dspark_take(&mut ds.main_hidden) && !ds.have_hidden {
13969            if dbg {
13970                eprintln!("spec_step: нет захвата");
13971            }
13972            return None;
13973        }
13974        ds.have_hidden = true;
13975        let tip_pos = next_pos.checked_sub(1)?;
13976        let draft_started = std::time::Instant::now();
13977        let mut conf = Vec::new();
13978        let props = crate::dsv4::dspark_draft_gpu(
13979            g,
13980            &self.dsv4_mtp,
13981            &cfg,
13982            ds,
13983            pack,
13984            st.kv_id,
13985            tip_token,
13986            tip_pos,
13987            self.pool.as_deref(),
13988            &mut conf,
13989        );
13990        self.dspark_draft_ns += draft_started.elapsed().as_nanos();
13991        *drafted += block;
13992        if props.is_empty() || props[0] != t_next {
13993            if dbg {
13994                eprintln!(
13995                    "spec_step: черновик {} (props0={:?} t_next={t_next})",
13996                    if props.is_empty() {
13997                        "пуст"
13998                    } else {
13999                        "мимо"
14000                    },
14001                    props.first()
14002                );
14003            }
14004            return None;
14005        }
14006        // `fed[0]` is `t_next`, which the outer loop has already committed;
14007        // only `fed[1..]` become additional output tokens. Cap the verify
14008        // transaction itself to the caller's remaining output budget instead
14009        // of merely truncating the returned vector: otherwise the KV/state
14010        // would advance past `max_tokens` and a 64-token request could return
14011        // 66 tokens (and poison a reused session with two invisible steps).
14012        let mut k_verify = crate::dsv4::dspark_verify_k()
14013            .min(props.len())
14014            .min(max_extra.saturating_add(1));
14015        // Adaptive depth: positions the draft itself doubts are paid for on
14016        // every verify and delivered almost never (natural-text survival
14017        // [.67 .50 .29 .08 .04]). `CMF_DSPARK_CONF_MIN=p` trims the fed
14018        // prefix at the first proposal whose confidence drops below p; on
14019        // predictable text the confidences stay high and nothing changes.
14020        let conf_min = {
14021            static M: std::sync::OnceLock<f32> = std::sync::OnceLock::new();
14022            *M.get_or_init(|| {
14023                std::env::var("CMF_DSPARK_CONF_MIN")
14024                    .ok()
14025                    .and_then(|v| v.parse().ok())
14026                    .unwrap_or(0.0)
14027            })
14028        };
14029        if conf_min > 0.0 && conf.len() >= props.len() {
14030            let mut keep = 1usize;
14031            while keep < k_verify && conf.get(keep).copied().unwrap_or(0.0) >= conf_min {
14032                keep += 1;
14033            }
14034            k_verify = k_verify.min(keep.max(2));
14035        }
14036        if k_verify < 2 {
14037            return None;
14038        }
14039        let mut fed = Vec::with_capacity(k_verify);
14040        fed.push(t_next);
14041        fed.extend_from_slice(&props[1..k_verify]);
14042        let mut argmax = Vec::new();
14043        let mut logits_all = Vec::new();
14044        let mut walked = Vec::new();
14045        let txn = crate::dsv4::dsv4_verify_chunk(
14046            g,
14047            layers,
14048            &cfg,
14049            st,
14050            &fed,
14051            next_pos,
14052            &self.inv_freq,
14053            self.pool.as_deref(),
14054            &targets,
14055            &mut argmax,
14056            &mut logits_all,
14057            &mut walked,
14058        );
14059        if txn.is_none() && dbg {
14060            eprintln!("spec_step: verify отказал");
14061        }
14062        let txn = txn?;
14063        let spec_gpu_end = txn.gpu_end;
14064        let b = fed.len();
14065        let mut accepted = 1usize;
14066        while accepted < b && fed[accepted] == argmax[accepted - 1] {
14067            accepted += 1;
14068        }
14069        // `CMF_DSV4_SPEC_FORCE_REJECT=1` — accept nothing beyond the known
14070        // token, every round: the pure rollback exerciser. The output must
14071        // stay byte-identical to the plain walk; anything else is a
14072        // transaction bug, isolated from the acceptance logic.
14073        if std::env::var("CMF_DSV4_SPEC_FORCE_REJECT").is_ok_and(|v| v != "0") {
14074            accepted = 1;
14075        }
14076        if std::env::var("CMF_DSV4_SPEC_TRACE").is_ok() {
14077            eprintln!("spec@{next_pos}: fed={fed:?} argmax={argmax:?} accepted={accepted}");
14078        }
14079        let t_fin = std::time::Instant::now();
14080        if !crate::dsv4::dsv4_spec_finish(
14081            g,
14082            layers,
14083            &cfg,
14084            st,
14085            txn,
14086            accepted,
14087            &fed,
14088            &self.inv_freq,
14089            self.pool.as_deref(),
14090        ) {
14091            tracing::warn!("dsv4: спекулятивный откат не удался — состояние подозрительно");
14092            return None;
14093        }
14094        if std::env::var("CMF_DSV4_SPEC_TIME").is_ok() {
14095            eprintln!(
14096                "finish(k={accepted}): {:.1} мс",
14097                t_fin.elapsed().as_secs_f64() * 1e3
14098            );
14099        }
14100        *accepted_ctr += accepted - 1;
14101        // Captures per accepted token: device targets photographed by the
14102        // batch, host targets from the verify's own walk. The last one
14103        // becomes the new tip's draft input; every one owes the ring an
14104        // entry for its position.
14105        let (hc, dim) = (cfg.hc_mult, cfg.dim);
14106        // Complete-chain layers are photographed by the fused submission;
14107        // partial device layers overwrite that slot after exact host cold-
14108        // expert correction.  Thus every target in the contiguous device
14109        // prefix has a valid per-token capture.
14110        let dev_caps: Vec<usize> = targets
14111            .iter()
14112            .copied()
14113            .filter(|&t| t < spec_gpu_end)
14114            .collect();
14115        let mut caps_all = vec![0.0f32; dev_caps.len() * b * hc * dim];
14116        if !crate::gpu_wgpu::dsv4_spec_cap_read_all(b, dev_caps.len(), hc * dim, &mut caps_all) {
14117            return None;
14118        }
14119        for t in 0..accepted {
14120            let tip = t + 1 == accepted;
14121            for (slot, &tl) in targets.iter().enumerate() {
14122                if let Some(di) = dev_caps.iter().position(|&d| d == tl) {
14123                    let lo = (di * b + t) * hc * dim;
14124                    crate::dsv4::dspark_capture(
14125                        &caps_all[lo..lo + hc * dim],
14126                        &cfg,
14127                        slot,
14128                        &mut ds.main_hidden,
14129                    );
14130                } else if tip
14131                    && crate::dsv4::dspark_peek_slot(slot, dim, {
14132                        let lo = slot * dim;
14133                        &mut ds.main_hidden[lo..lo + dim]
14134                    })
14135                {
14136                    // The tip's host-layer captures are the walk's own
14137                    // per-layer notes — exact. (The walk that ran last ended
14138                    // on exactly this token, on both the accept-all and the
14139                    // rollback path.)
14140                } else {
14141                    // Intermediate tokens: the post-tail state stands in for
14142                    // the per-layer capture on host targets below the last
14143                    // layer. Ring-entry quality only; the tip is exact.
14144                    crate::dsv4::dspark_capture(
14145                        &walked[t * hc * dim..(t + 1) * hc * dim],
14146                        &cfg,
14147                        slot,
14148                        &mut ds.main_hidden,
14149                    );
14150                }
14151            }
14152            crate::dsv4::dspark_ring_append(
14153                g,
14154                &self.dsv4_mtp,
14155                &cfg,
14156                ds,
14157                next_pos + t,
14158                self.pool.as_deref(),
14159            );
14160        }
14161        let row = logits_all[(accepted - 1) * cfg.vocab..accepted * cfg.vocab].to_vec();
14162        self.graph_logits = Some(row);
14163        // The speculative loop never runs the probe, so the trunk tally has
14164        // no other place to cycle. Armed only when someone asked for the
14165        // dump; the host tail is the only tallying path here, which is
14166        // precisely the population a partial pack would serve.
14167        if std::env::var("CMF_DSV4_TRUNK_PICK_DUMP").is_ok() {
14168            crate::dsv4::trunk_freq_note(&crate::dsv4::pick_tally_take());
14169            crate::dsv4::pick_tally_arm();
14170        }
14171        if std::env::var("CMF_DSV4_SPEC_TIME").is_ok() {
14172            eprintln!(
14173                "spec_step total {:.1} мс (k={accepted})",
14174                t_all.elapsed().as_secs_f64() * 1e3
14175            );
14176        }
14177        Some((fed[1..accepted].to_vec(), next_pos + accepted))
14178    }
14179
14180    fn dspark_probe(&mut self, position: usize, token_id: u32) {
14181        if self.dsv4_mtp.is_empty() || !Self::draft_probe() {
14182            return;
14183        }
14184        // What the trunk just routed to, for this token.
14185        let trunk_now = crate::dsv4::pick_tally_take();
14186        crate::dsv4::trunk_freq_note(&trunk_now);
14187        if !trunk_now.is_empty() {
14188            self.dspark_trunk_picks.push(trunk_now);
14189            let keep = crate::dsv4::dspark_block();
14190            if self.dspark_trunk_picks.len() > keep {
14191                self.dspark_trunk_picks.remove(0);
14192            }
14193        }
14194        // Grade whatever is waiting: the token just decoded sits at
14195        // `position`, so it answers the draft made at `position - 1 - i`.
14196        for p in std::mem::take(&mut self.dspark_pending) {
14197            let Some(i) = position.checked_sub(p.0 + 1) else {
14198                continue;
14199            };
14200            let mut p = p;
14201            if i < p.1.len() {
14202                if p.2 && p.1[i] == token_id {
14203                    p.3 = i + 1;
14204                } else {
14205                    p.2 = false;
14206                }
14207                if i + 1 < p.1.len() {
14208                    self.dspark_pending.push(p);
14209                    continue;
14210                }
14211            }
14212            self.dspark_hist.push(p.3);
14213            self.dspark_real.push(token_id);
14214        }
14215        let Some(b) = &mut self.dsv4 else { return };
14216        let (g, layers, cfg) = (&b.0, &b.1, b.2);
14217        let n_layers = layers.len();
14218        if self.dspark.is_none() {
14219            let t = crate::dsv4::dspark_targets(&self.dsv4_mtp, &cfg, n_layers);
14220            if t.is_empty() {
14221                return;
14222            }
14223            eprintln!(
14224                "DSpark: захват со слоёв {t:?}, блок {}",
14225                crate::dsv4::dspark_block()
14226            );
14227            crate::dsv4::dspark_arm(&t, cfg.dim);
14228            self.dspark = Some(crate::dsv4::DsparkState::new(
14229                self.dsv4_mtp.len(),
14230                &cfg,
14231                t.len(),
14232            ));
14233        }
14234        let ds = self.dspark.as_mut().unwrap();
14235        if !crate::dsv4::dspark_take(&mut ds.main_hidden) {
14236            return; // this token ran on a path that captures nothing
14237        }
14238        let mut conf = Vec::new();
14239        crate::dsv4::pick_tally_arm();
14240        // The trunk has already consumed the adaptive VRAM budget. Until the
14241        // draft owns an explicit bounded device pack, its tensors are an
14242        // out-of-core CPU/disk tier by contract: never let per-op probes try
14243        // to squeeze another multi-gigabyte MTP expert cache onto the card.
14244        let draft_started = std::time::Instant::now();
14245        #[cfg(feature = "gpu")]
14246        let gpu_draft = crate::dsv4::dspark_gpu_on();
14247        #[cfg(not(feature = "gpu"))]
14248        let gpu_draft = false;
14249        let props = if gpu_draft {
14250            #[cfg(feature = "gpu")]
14251            {
14252                let kv_id = b.3.kv_id;
14253                match crate::dsv4::dspark_pack_get(&self.dsv4_mtp, &cfg) {
14254                    Some(pk) => crate::dsv4::dspark_draft_gpu(
14255                        g,
14256                        &self.dsv4_mtp,
14257                        &cfg,
14258                        ds,
14259                        pk,
14260                        kv_id,
14261                        token_id,
14262                        position,
14263                        self.pool.as_deref(),
14264                        &mut conf,
14265                    ),
14266                    None => Vec::new(),
14267                }
14268            }
14269            #[cfg(not(feature = "gpu"))]
14270            Vec::new()
14271        } else {
14272            crate::gpu::cpu_scope(|| {
14273                crate::dsv4::dspark_draft(
14274                    g,
14275                    &self.dsv4_mtp,
14276                    &cfg,
14277                    ds,
14278                    token_id,
14279                    position,
14280                    self.pool.as_deref(),
14281                    &mut conf,
14282                )
14283            })
14284        };
14285        self.dspark_draft_ns += draft_started.elapsed().as_nanos();
14286        let draft_picks = crate::dsv4::pick_tally_take();
14287        crate::dsv4::dspark_freq_note(&draft_picks);
14288        // Re-arm for the NEXT trunk token; the probe runs after the forward,
14289        // so this is the only place that can.
14290        crate::dsv4::pick_tally_arm();
14291        if !props.is_empty() {
14292            // Two ratios, side by side: what a batched verify over the trunk
14293            // would read against what it asks for, and the same for the
14294            // draft's three stages. Near 1.0 means a batch amortises nothing.
14295            let (tu, tt) = {
14296                let flat: Vec<(usize, Vec<usize>)> = self
14297                    .dspark_trunk_picks
14298                    .iter()
14299                    .flat_map(|v| v.iter().cloned())
14300                    .collect();
14301                // Per layer, across the window of tokens.
14302                let mut per: std::collections::HashMap<usize, Vec<usize>> =
14303                    std::collections::HashMap::new();
14304                for (li, picks) in flat {
14305                    per.entry(li).or_default().extend(picks);
14306                }
14307                let n = per.len().max(1);
14308                let mut u = 0usize;
14309                let mut t = 0usize;
14310                for (_, v) in per {
14311                    t += v.len();
14312                    u += v.iter().collect::<std::collections::HashSet<_>>().len();
14313                }
14314                (u / n, t / n)
14315            };
14316            let (du, dt) = crate::dsv4::tally_unique(&draft_picks);
14317            self.dspark_exp.push((tu, tt, du, dt));
14318            self.dspark_pending.push((position, props, true, 0));
14319        }
14320        if self.dspark_hist.len() >= 8 && self.dspark_hist.len() % 8 == 0 {
14321            let n = self.dspark_hist.len() as f32;
14322            let mean: f32 = self.dspark_hist.iter().sum::<usize>() as f32 / n;
14323            let block = crate::dsv4::dspark_block();
14324            let mut at = vec![0usize; block + 1];
14325            for &k in &self.dspark_hist {
14326                at[k] += 1;
14327            }
14328            // Prefix survival: S_i = P(the first i positions all held).
14329            let mut surv = Vec::with_capacity(block);
14330            for i in 1..=block {
14331                let k = at[i..].iter().sum::<usize>() as f32 / n;
14332                surv.push(format!("{k:.2}"));
14333            }
14334            let distinct = self
14335                .dspark_real
14336                .iter()
14337                .collect::<std::collections::HashSet<_>>()
14338                .len();
14339            let (tu, tt, du, dt) = self.dspark_exp.iter().fold((0, 0, 0, 0), |a, b| {
14340                (a.0 + b.0, a.1 + b.1, a.2 + b.2, a.3 + b.3)
14341            });
14342            let m = self.dspark_exp.len().max(1);
14343            eprintln!(
14344                "DSpark: черновиков {}, принято в среднем {mean:.2} из {block} \
14345                 (токенов за проход {:.2}), распределение {at:?}, выживание [{}]",
14346                self.dspark_hist.len(),
14347                mean + 1.0,
14348                surv.join(" ")
14349            );
14350            eprintln!(
14351                "DSpark: разных токенов {distinct} из {} (вырожденность), \
14352                 эксперты ствол {}/{} на слой за {block} токенов, \
14353                 черновик {}/{} за блок, draft {:.2} мс/блок",
14354                self.dspark_real.len(),
14355                tu / m,
14356                tt / m,
14357                du / m,
14358                dt / m,
14359                self.dspark_draft_ns as f64 / self.dspark_exp.len().max(1) as f64 / 1e6
14360            );
14361        }
14362    }
14363
14364    fn forward_layers_upto(
14365        &mut self,
14366        hidden: &[f32],
14367        position: usize,
14368        task_mask: Option<&TaskMask>,
14369        upto: Option<usize>,
14370    ) -> Vec<f32> {
14371        // In-process multi-GPU: each segment runs pinned to its card,
14372        // and the only thing crossing the boundary is one hidden vector
14373        // that never leaves this address space. Same layer split the
14374        // network mode does, minus the second process, the socket, the
14375        // serialization and the dir_hash handshake.
14376        if let Some(plan) = self.gpu_plan.clone() {
14377            if upto.is_none() && plan.len() > 1 {
14378                let mut h = hidden.to_vec();
14379                for &(dev, from, upto_incl) in plan.iter() {
14380                    h = crate::gpu::with_device(dev, || {
14381                        self.forward_layers_span(&h, position, task_mask, from, Some(upto_incl))
14382                    });
14383                }
14384                return h;
14385            }
14386        }
14387        self.forward_layers_span(hidden, position, task_mask, 0, upto)
14388    }
14389
14390    /// Split this pipeline's layer stack across local GPUs: segment i
14391    /// runs on `devices[i]`. Contiguous and even by layer count — the
14392    /// VRAM-weighted planner is the next step, and an uneven card pair
14393    /// is why it will be needed. `None` clears the plan.
14394    pub fn set_gpu_plan(&mut self, devices: Option<&[usize]>) -> Result<(), String> {
14395        self.set_gpu_plan_at(devices, None)
14396    }
14397
14398    /// The same, with an explicit first boundary (`--peer-split`): card
14399    /// 0 takes layers `[0..at)`, the rest split what remains. Uneven
14400    /// cards, or an attention-heavy head, are why this knob exists.
14401    pub fn set_gpu_plan_at(
14402        &mut self,
14403        devices: Option<&[usize]>,
14404        at: Option<usize>,
14405    ) -> Result<(), String> {
14406        let Some(devs) = devices.filter(|d| d.len() > 1) else {
14407            self.gpu_plan = None;
14408            return Ok(());
14409        };
14410        self.split_supported()?;
14411        let n = self.num_layers;
14412        if devs.len() > n {
14413            return Err(format!("{} devices for {n} layers", devs.len()));
14414        }
14415        if let Some(k) = at {
14416            if k == 0 || k >= n {
14417                return Err(format!("split at {k}: the model has {n} layers"));
14418            }
14419            if devs.len() == 2 {
14420                self.gpu_plan = Some(std::sync::Arc::new(vec![
14421                    (devs[0], 0, k - 1),
14422                    (devs[1], k, n - 1),
14423                ]));
14424                return Ok(());
14425            }
14426            return Err(format!(
14427                "an explicit split point takes exactly 2 devices, got {}",
14428                devs.len()
14429            ));
14430        }
14431        let per = n.div_ceil(devs.len());
14432        let mut plan = Vec::with_capacity(devs.len());
14433        let mut from = 0usize;
14434        for &d in devs {
14435            if from >= n {
14436                break;
14437            }
14438            let upto = (from + per - 1).min(n - 1);
14439            plan.push((d, from, upto));
14440            from = upto + 1;
14441        }
14442        self.gpu_plan = Some(std::sync::Arc::new(plan));
14443        Ok(())
14444    }
14445
14446    /// The active in-process split, if any: (device, first layer, last).
14447    pub fn gpu_plan(&self) -> Option<Vec<(usize, usize, usize)>> {
14448        self.gpu_plan.as_ref().map(|p| p.as_ref().clone())
14449    }
14450
14451    /// Layer span [from ..= upto] (upto None = last layer): the building
14452    /// block the network pipeline-split rides on. `from > 0` skips the
14453    /// arch escape hatches (the pub `forward_span` refuses those archs
14454    /// first) and the whole-token graph — the plain per-layer loop is
14455    /// the canonical executor for a partial stack.
14456    fn embryo_qtensor_f32(t: &QTensor) -> Vec<f32> {
14457        if let Some(x) = t.as_f32() {
14458            return x.to_vec();
14459        }
14460        let mut out = vec![0.0; t.rows() * t.cols()];
14461        for r in 0..t.rows() {
14462            t.row_f32(r, &mut out[r * t.cols()..(r + 1) * t.cols()]);
14463        }
14464        out
14465    }
14466
14467    fn embryo_resident_eligible(&self) -> bool {
14468        // One mixer family per file: vmf_phase (kind 0/1) or
14469        // gated_delta_net (kind 4); the anchors are full (2) or bounded (3).
14470        if (self.vmf_cfg.is_none() && self.gdn_cfg.is_none())
14471            || self.num_layers != self.physical_layers
14472            || self.loop_final_norm
14473            || self.weights.layers.len() != self.num_layers
14474            || self.head_clusters.is_none()
14475            || self.final_softcap.is_some()
14476            || self.logit_multiplier.is_some()
14477            || self.attn_softcap != 0.0
14478            || self.mtp.is_some()
14479            || self.g3n.is_some()
14480            || self.dsv4.is_some()
14481            || self.dsv41.is_some()
14482            || self.qwen4_exp.is_some()
14483            // Dynamic routing swaps FFN weights mid-sequence under the
14484            // packed graph; a blend has no single overlay. A STATIC skill
14485            // (`from_model_with_skill`) is fine: the pack reads the live
14486            // `weights.layers[*].ffn`, i.e. the skill's tensors, and a
14487            // later `set_active_skill` change drops the pack
14488            // (`invalidate_for_weight_change`).
14489            || self.dyn_router.is_some()
14490            || self.dyn_phi_layer.is_some()
14491            || self.dyn_blend_loaded
14492            || self.o1_cfg.is_some()
14493            || self.swa.is_some()
14494            || self.sliding_layers.is_some()
14495            || self.global_attn.is_some()
14496            || self.attention_heads_per_layer.is_some()
14497            || self.attn_v_norm
14498            || self
14499                .kv_cache
14500                .layers
14501                .iter()
14502                .any(|l| l.mode != crate::kv_cache::KvMode::F32)
14503            || self.rope_scale != 1.0
14504            || self.rope_scale_local != 1.0
14505            || self.attn_scale != 1.0 / (self.head_dim as f32).sqrt()
14506            || self.hidden_size == 0
14507            || self.hidden_size > 1024
14508            || self.intermediate_size > 1024
14509            || self.num_heads == 0
14510            || self.num_kv_heads == 0
14511            || self.num_heads % self.num_kv_heads != 0
14512            || self.num_heads.saturating_mul(self.head_dim) > 1024
14513            || self.num_kv_heads.saturating_mul(self.head_dim) > 1024
14514            || self.vocab_size == 0
14515            || self.kv_cache.max_seq_len == 0
14516            || self.rotary_dim == 0
14517            || self.rotary_dim > self.head_dim
14518            || self.rotary_dim % 2 != 0
14519            || self.inv_freq.len() < self.rotary_dim / 2
14520        {
14521            return false;
14522        }
14523        // The resident shader is deliberately an f32 profile.  Dequantizing
14524        // a Q4/Q8 tensor into the packed buffer would silently change the
14525        // operator relative to the CPU quantized path, so quantized CMFs
14526        // retain the exact ordinary executor instead of claiming parity.
14527        // Measured consequence (RTX PRO 4000, S4 bounded export requantized
14528        // with `cortiq requant --quant q4tp-quantize`): `eligible=false`,
14529        // the generic wgpu whole-token graph refuses too, and the per-op
14530        // path decodes at ~73 tok/s against ~200 tok/s on the CPU q4tp
14531        // path — a q4tp Embryo-O1 file is a CPU artifact today; the
14532        // resident graph serves the f32 export.
14533        if self.weights.lm_head.as_f32().is_none()
14534            || self.weights.embed_tokens.as_f32().is_none()
14535            || self.weights.lm_head.rows() < self.vocab_size
14536            || self.weights.lm_head.cols() != self.hidden_size
14537            || self.weights.embed_tokens.rows() < self.vocab_size
14538            || self.weights.embed_tokens.cols() != self.hidden_size
14539            || self.weights.final_norm.len() != self.hidden_size
14540        {
14541            return false;
14542        }
14543        if let Some(cfg) = self.vmf_cfg {
14544            if cfg.num_heads.saturating_mul(cfg.nphase) > 1024
14545                || cfg.num_heads.saturating_mul(cfg.nphase.saturating_add(1)) > 1024
14546                || cfg.num_heads.saturating_mul(cfg.value_head_dim) > 1024
14547                || cfg.state_len() == 0
14548            {
14549                return false;
14550            }
14551        }
14552        if let Some(g) = self.gdn_cfg {
14553            // The resident GDN kernels (gpu_wgpu.rs `embryo_core_gdn_*`):
14554            // fused projection ≤ 2048 rows, nv·dv ≤ 1024, dk ≤ 128 lanes,
14555            // dv ≤ 256 lanes in vec4 rows, SiLU output gate (the Embryo
14556            // export), same rms eps as the stack.
14557            if g.num_v_heads == 0
14558                || g.num_k_heads == 0
14559                || g.num_v_heads % g.num_k_heads != 0
14560                || g.key_head_dim == 0
14561                || g.key_head_dim > 128
14562                || g.value_head_dim == 0
14563                || g.value_head_dim > 256
14564                || g.value_head_dim % 4 != 0
14565                || g.conv_kernel == 0
14566                || g.num_v_heads > 512
14567                || g.num_v_heads.saturating_mul(g.value_head_dim) > 1024
14568                || g.conv_dim() > 2048
14569                || g.conv_dim() % 4 != 0
14570                || g.hidden_size != self.hidden_size
14571                || g.output_gate_sigmoid
14572                || g.rms_eps != self.rms_eps
14573                || g.state_len() == 0
14574            {
14575                return false;
14576            }
14577        }
14578        let mut full_seen = false;
14579        for lw in &self.weights.layers {
14580            if lw.attn_out_norm.is_some() || lw.ffn_out_norm.is_some() || lw.layer_scale.is_some() {
14581                return false;
14582            }
14583            match &lw.attn {
14584                AttnKind::LinearGdn(w) => {
14585                    let Some(g) = self.gdn_cfg else {
14586                        return false;
14587                    };
14588                    let (nv, dv, kk) = (g.num_v_heads, g.value_head_dim, g.conv_kernel);
14589                    if w.in_proj_qkv.rows() != g.conv_dim()
14590                        || w.in_proj_qkv.cols() != self.hidden_size
14591                        || w.in_proj_qkv.as_f32().is_none()
14592                        || w.in_proj_z.rows() != nv * dv
14593                        || w.in_proj_z.cols() != self.hidden_size
14594                        || w.in_proj_z.as_f32().is_none()
14595                        || w.in_proj_a.rows() != nv
14596                        || w.in_proj_a.cols() != self.hidden_size
14597                        || w.in_proj_a.as_f32().is_none()
14598                        || w.in_proj_b.rows() != nv
14599                        || w.in_proj_b.cols() != self.hidden_size
14600                        || w.in_proj_b.as_f32().is_none()
14601                        || w.conv1d.len() != g.conv_dim() * kk
14602                        || w.a_log.len() != nv
14603                        || w.dt_bias.len() != nv
14604                        || w.norm.len() != dv
14605                        || w.out_proj.rows() != self.hidden_size
14606                        || w.out_proj.cols() != nv * dv
14607                        || w.out_proj.as_f32().is_none()
14608                    {
14609                        return false;
14610                    }
14611                }
14612                AttnKind::Linear(w) => {
14613                    let Some(cfg) = self.vmf_cfg else {
14614                        return false;
14615                    };
14616                    if w.thq.rows() != cfg.num_heads * cfg.nphase
14617                        || w.thq.cols() != self.hidden_size
14618                        || w.thq.as_f32().is_none()
14619                        || w.thk.rows() != cfg.num_heads * cfg.nphase
14620                        || w.thk.cols() != self.hidden_size
14621                        || w.thk.as_f32().is_none()
14622                        || w.v_proj.rows() != cfg.num_heads * cfg.value_head_dim
14623                        || w.v_proj.cols() != self.hidden_size
14624                        || w.v_proj.as_f32().is_none()
14625                        || w.out_proj.rows() != self.hidden_size
14626                        || w.out_proj.cols() != cfg.num_heads * cfg.value_head_dim
14627                        || w.out_proj.as_f32().is_none()
14628                        || w.decay.len() != cfg.num_heads * 2 * cfg.nphase
14629                    {
14630                        return false;
14631                    }
14632                    if let Some((kg, kb)) = &w.k_gate {
14633                        if kg.rows() != cfg.num_heads
14634                            || kg.cols() != self.hidden_size
14635                            || kg.as_f32().is_none()
14636                            || kb.len() != cfg.num_heads
14637                        {
14638                            return false;
14639                        }
14640                    }
14641                    if let Some(conv) = &w.conv {
14642                        if conv.len() % self.hidden_size != 0 || conv.len() / self.hidden_size < 2 {
14643                            return false;
14644                        }
14645                    }
14646                }
14647                AttnKind::Full {
14648                    wq,
14649                    wk,
14650                    wv,
14651                    wo,
14652                    q_norm,
14653                    k_norm,
14654                    output_gate,
14655                    softplus_gate,
14656                    bias,
14657                } => {
14658                    if full_seen
14659                        || q_norm.is_some()
14660                        || k_norm.is_some()
14661                        || *output_gate
14662                        || softplus_gate.is_some()
14663                        || bias.is_some()
14664                        || wq.as_f32().is_none()
14665                        || wk.as_f32().is_none()
14666                        || wv.as_f32().is_none()
14667                        || wo.as_f32().is_none()
14668                        || wq.rows() != self.num_heads * self.head_dim
14669                        || wk.rows() != self.num_kv_heads * self.head_dim
14670                        || wv.rows() != self.num_kv_heads * self.head_dim
14671                        || wq.cols() != self.hidden_size
14672                        || wk.cols() != self.hidden_size
14673                        || wv.cols() != self.hidden_size
14674                        || wo.rows() != self.hidden_size
14675                        || wo.cols() != self.num_heads * self.head_dim
14676                    {
14677                        return false;
14678                    }
14679                    full_seen = true;
14680                }
14681                AttnKind::Bounded(w) => {
14682                    // The resident bounded attend scores S + W lanes in one
14683                    // 256-lane chunk; the format caps S + W at 160.
14684                    let Some(ac) = self.anchor_core.as_ref() else {
14685                        return false;
14686                    };
14687                    if self.bounded_rope.is_none()
14688                        || w.window != ac.window
14689                        || w.sink != ac.sink
14690                        || w.window == 0
14691                        || w.window + w.sink > 256
14692                        || w.sink_k.len() != self.num_kv_heads * w.sink * self.head_dim
14693                        || w.sink_v.len() != self.num_kv_heads * w.sink * self.head_dim
14694                        || w.wq.as_f32().is_none()
14695                        || w.wk.as_f32().is_none()
14696                        || w.wv.as_f32().is_none()
14697                        || w.wo.as_f32().is_none()
14698                        || w.wq.rows() != self.num_heads * self.head_dim
14699                        || w.wk.rows() != self.num_kv_heads * self.head_dim
14700                        || w.wv.rows() != self.num_kv_heads * self.head_dim
14701                        || w.wq.cols() != self.hidden_size
14702                        || w.wk.cols() != self.hidden_size
14703                        || w.wv.cols() != self.hidden_size
14704                        || w.wo.rows() != self.hidden_size
14705                        || w.wo.cols() != self.num_heads * self.head_dim
14706                    {
14707                        return false;
14708                    }
14709                }
14710                _ => return false,
14711            }
14712            match &lw.ffn {
14713                FfnKind::Dense(d) => {
14714                    if d.act != Act::Silu
14715                        || !d.segs.is_empty()
14716                        || d.gate_proj.as_f32().is_none()
14717                        || d.up_proj.as_f32().is_none()
14718                        || d.down_proj.as_f32().is_none()
14719                        || d.gate_proj.rows() != self.intermediate_size
14720                        || d.gate_proj.cols() != self.hidden_size
14721                        || d.up_proj.rows() != self.intermediate_size
14722                        || d.up_proj.cols() != self.hidden_size
14723                        || d.down_proj.rows() != self.hidden_size
14724                        || d.down_proj.cols() != self.intermediate_size
14725                    {
14726                        return false;
14727                    }
14728                }
14729                FfnKind::Moe(m) => {
14730                    if m.resonance.is_none()
14731                        || m.top_k != 1
14732                        || m.router_sigmoid
14733                        || !m.norm_topk_prob
14734                        || m.expert_bias.is_some()
14735                        || m.routed_scaling != 1.0
14736                        || m.route_tau.is_some()
14737                        || m.shared.is_none()
14738                        || m.mask.is_some()
14739                        || m.per_expert_scale.is_some()
14740                        || m.router_input_norm
14741                        || m.experts.is_empty()
14742                        || m.experts.len() > 8
14743                    {
14744                        return false;
14745                    }
14746                    let r = m.resonance.as_ref().unwrap();
14747                    if r.mu.len() != m.experts.len() * self.hidden_size
14748                        || r.bias.len() != m.experts.len()
14749                        || r.u.len() != m.experts.len() * r.k * self.hidden_size
14750                        || r.k > 128
14751                    {
14752                        return false;
14753                    }
14754                    let Some((shared, gate)) = &m.shared else {
14755                        return false;
14756                    };
14757                    if gate.is_some() || shared.act != Act::Silu || !shared.segs.is_empty() {
14758                        return false;
14759                    }
14760                    if shared.gate_proj.as_f32().is_none()
14761                        || shared.up_proj.as_f32().is_none()
14762                        || shared.down_proj.as_f32().is_none()
14763                        || shared.gate_proj.rows() != self.intermediate_size
14764                        || shared.gate_proj.cols() != self.hidden_size
14765                        || shared.up_proj.rows() != self.intermediate_size
14766                        || shared.up_proj.cols() != self.hidden_size
14767                        || shared.down_proj.rows() != self.hidden_size
14768                        || shared.down_proj.cols() != self.intermediate_size
14769                    {
14770                        return false;
14771                    }
14772                    for e in &m.experts {
14773                        if e.act != Act::Silu
14774                            || !e.segs.is_empty()
14775                            || e.gate_proj.as_f32().is_none()
14776                            || e.up_proj.as_f32().is_none()
14777                            || e.down_proj.as_f32().is_none()
14778                            || e.gate_proj.rows() != self.intermediate_size
14779                            || e.gate_proj.cols() != self.hidden_size
14780                            || e.up_proj.rows() != self.intermediate_size
14781                            || e.up_proj.cols() != self.hidden_size
14782                            || e.down_proj.rows() != self.hidden_size
14783                            || e.down_proj.cols() != self.intermediate_size
14784                        {
14785                            return false;
14786                        }
14787                    }
14788                }
14789                FfnKind::DenseMoe(_) => return false,
14790            }
14791            if lw.input_norm.len() != self.hidden_size || lw.post_norm.len() != self.hidden_size {
14792                return false;
14793            }
14794        }
14795        if full_seen && self.anchor_core.is_some() {
14796            return false;
14797        }
14798        full_seen || self.num_layers > 0
14799    }
14800
14801    /// The resident Embryo graph is the owner of this pipeline's forward:
14802    /// the same gate `forward_layers_span` applies before handing a token
14803    /// to `forward_embryo_graph` (both graph phases on, the explicit
14804    /// opt-in, a wgpu device, no earlier refusal, an eligible stack).
14805    fn embryo_resident_wanted(&self) -> bool {
14806        crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Decode)
14807            && crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Prefill)
14808            && matches!(
14809                std::env::var("CMF_EMBRYO_RESIDENT").as_deref(),
14810                Ok("1") | Ok("parallel")
14811            )
14812            && crate::gpu::enabled_here()
14813            && !self.graph_refused()
14814            && self.embryo_resident_eligible()
14815    }
14816
14817    /// Chunked prefill on the resident graph: `ids` from `start` in
14818    /// chunks of `EMBRYO_CHUNK_MAX`, one submit each, projections/FFN as
14819    /// chunk GEMMs and the recurrent layers walked in time on the device.
14820    /// Returns the last position's logits when the whole span ran there.
14821    /// `None` = refused before any device work (the per-position path
14822    /// takes the span).  `CMF_EMBRYO_CHUNK=0` keeps the per-position
14823    /// prefill (A/B and the parity reference).
14824    fn embryo_prefill_chunked(&mut self, ids: &[u32], start: usize) -> Option<Vec<f32>> {
14825        if ids.len() < 2
14826            || std::env::var("CMF_EMBRYO_CHUNK").as_deref() == Ok("0")
14827            || !self.embryo_resident_wanted()
14828        {
14829            return None;
14830        }
14831        let model = self.ensure_embryo_graph()?;
14832        let cmax = std::env::var("CMF_EMBRYO_CHUNK")
14833            .ok()
14834            .and_then(|v| v.parse::<usize>().ok())
14835            .filter(|&v| v >= 1)
14836            .unwrap_or(crate::gpu::EMBRYO_CHUNK_MAX)
14837            .min(crate::gpu::EMBRYO_CHUNK_MAX);
14838        let hs = self.hidden_size;
14839        let n = ids.len();
14840        let mut pos = start;
14841        let mut last = None;
14842        let mut rows = Vec::with_capacity(cmax * hs);
14843        while pos < n {
14844            let end = (pos + cmax).min(n);
14845            rows.clear();
14846            for &id in &ids[pos..end] {
14847                rows.extend_from_slice(&self.embed_single(id));
14848            }
14849            let mut lg = Vec::new();
14850            if !crate::gpu::forward_embryo_graph_chunk(
14851                &model,
14852                self.graph_kv_id,
14853                &rows,
14854                pos,
14855                end - pos,
14856                &mut lg,
14857            ) {
14858                if pos == start {
14859                    if std::env::var("CMF_EMBRYO_DBG").as_deref() == Ok("1") {
14860                        eprintln!("embryo-dbg: chunk prefill refused at position {pos}");
14861                    }
14862                    return None;
14863                }
14864                // The device sequence advanced through the earlier chunks;
14865                // a host continuation would mix two owners of the state.
14866                // Fail this sequence alone, leaving no stale state or key.
14867                self.kv_cache.clear();
14868                self.clear_history();
14869                crate::gpu::graph_kv_reset(self.graph_kv_id);
14870                panic!(
14871                    "resident Embryo chunk prefill refused at position {pos}; refusing an in-flight host fallback"
14872                );
14873            }
14874            last = Some(lg);
14875            pos = end;
14876        }
14877        if std::env::var("CMF_EMBRYO_DBG").as_deref() == Ok("1") {
14878            eprintln!(
14879                "embryo-dbg: chunk prefill {} ids from {start} in {} submits (chunk {cmax})",
14880                n - start,
14881                (n - start).div_ceil(cmax)
14882            );
14883        }
14884        last
14885    }
14886
14887    fn ensure_embryo_graph(&mut self) -> Option<std::sync::Arc<crate::gpu::EmbryoGraphModel>> {
14888        if self.embryo_graph.is_none() && self.embryo_resident_eligible() {
14889            const UMAX: u32 = u32::MAX;
14890            const HEADER: usize = crate::gpu::EMBRYO_META_HEADER;
14891            const REC: usize = 64;
14892            struct Pack {
14893                data: Vec<f32>,
14894            }
14895            impl Pack {
14896                fn put(&mut self, x: &[f32]) -> u32 {
14897                    if x.is_empty() {
14898                        return u32::MAX;
14899                    }
14900                    let off = self.data.len();
14901                    self.data.extend_from_slice(x);
14902                    off as u32
14903                }
14904            }
14905            // The mixer family of the file: vmf_phase geometry fills the
14906            // phase header words, gated_delta_net fills words 24..29.  A
14907            // file has exactly one linear core, so at most one is live.
14908            let vmf = self.vmf_cfg;
14909            let gdn = self.gdn_cfg;
14910            let mut pack = Pack { data: Vec::new() };
14911            let mut meta = vec![0u32; HEADER];
14912            meta[0] = self.hidden_size as u32;
14913            meta[1] = self.intermediate_size as u32;
14914            meta[2] = self.vocab_size as u32;
14915            meta[3] = self.num_layers as u32;
14916            meta[4] = vmf.map(|c| c.num_heads).unwrap_or(0) as u32;
14917            meta[5] = vmf.map(|c| c.nphase).unwrap_or(0) as u32;
14918            meta[6] = vmf.map(|c| c.value_head_dim).unwrap_or(0) as u32;
14919            if let Some(g) = gdn {
14920                meta[24] = g.num_v_heads as u32;
14921                meta[25] = g.num_k_heads as u32;
14922                meta[26] = g.key_head_dim as u32;
14923                meta[27] = g.value_head_dim as u32;
14924                meta[28] = g.conv_kernel as u32;
14925                meta[29] = g.conv_dim() as u32;
14926            }
14927            meta[7] = self.num_heads as u32;
14928            meta[8] = self.num_kv_heads as u32;
14929            meta[9] = self.head_dim as u32;
14930            meta[10] = self.kv_cache.max_seq_len as u32;
14931            let clusters = self.head_clusters.as_ref().unwrap();
14932            let cluster_count = clusters.len() / self.hidden_size;
14933            if clusters.len() % self.hidden_size != 0
14934                || cluster_count == 0
14935                || cluster_count > 1024
14936                || self.vocab_size % cluster_count != 0
14937                || self.weights.lm_head.rows() < self.vocab_size
14938                || self.weights.final_norm.len() != self.hidden_size
14939            {
14940                return None;
14941            }
14942            meta[11] = cluster_count as u32;
14943            meta[12] = (self.vocab_size / cluster_count) as u32;
14944            meta[13] = vmf.map(|c| c.state_len()).unwrap_or(0) as u32;
14945            meta[16] = self.rotary_dim as u32;
14946            meta[17] = matches!(self.norm_style, NormStyle::Gemma) as u32;
14947            meta[19] = (self.rms_eps as f32).to_bits();
14948            let max_conv = self
14949                .weights
14950                .layers
14951                .iter()
14952                .filter_map(|lw| match &lw.attn {
14953                    AttnKind::Linear(w) => w.conv.as_ref().map(|v| v.len() / self.hidden_size),
14954                    _ => None,
14955                })
14956                .max()
14957                .unwrap_or(1);
14958            // One state slot per recurrent layer: the phase state plus its
14959            // hidden-wide conv ring, or the GDN record `[conv ring | S]`
14960            // (`GdnCfg::state_len`).  A file carries one mixer family, so
14961            // the stride is exactly that family's record and the device
14962            // state buffer equals the header's recurrent bytes.
14963            let phase_stride = vmf
14964                .map(|c| c.state_len() + max_conv.saturating_sub(1) * self.hidden_size)
14965                .unwrap_or(0);
14966            let gdn_stride = gdn.map(|g| g.state_len()).unwrap_or(0);
14967            let state_stride = phase_stride.max(gdn_stride);
14968            // Bounded genome: the KV plane of an anchor is its ring
14969            // `[kvh][W][hd]` K + V, and only anchors own one.  Legacy full
14970            // anchors keep the `max_seq` planes indexed by layer.
14971            let bounded = self.anchor_core.clone();
14972            let (anchor_window, anchor_sink) = bounded
14973                .as_ref()
14974                .map(|ac| (ac.window, ac.sink))
14975                .unwrap_or((0, 0));
14976            let kv_stride = if bounded.is_some() {
14977                2usize
14978                    .saturating_mul(self.num_kv_heads)
14979                    .saturating_mul(anchor_window)
14980                    .saturating_mul(self.head_dim)
14981            } else {
14982                2usize
14983                    .saturating_mul(self.num_kv_heads)
14984                    .saturating_mul(self.kv_cache.max_seq_len)
14985                    .saturating_mul(self.head_dim)
14986            };
14987            meta[14] = state_stride as u32;
14988            meta[15] = kv_stride as u32;
14989            meta[18] = anchor_window as u32;
14990            meta[20] = anchor_sink as u32;
14991            meta[21] = match &self.bounded_rope {
14992                Some(rope) => {
14993                    // [W][half] cos then [W][half] sin, one contiguous table.
14994                    let off = pack.put(&rope.cos);
14995                    let _ = pack.put(&rope.sin);
14996                    off
14997                }
14998                None => UMAX,
14999            };
15000            let mut full_seen = false;
15001            let mut bounded_seen = 0usize;
15002            // Recurrent state slots belong to mixer layers only (phase or
15003            // GDN): an anchor owns a ring, not a state stride, so the
15004            // device state buffer is exactly the header's recurrent bytes.
15005            let mut phase_seen = 0usize;
15006            let mut gdn_seen = 0usize;
15007            for (li, lw) in self.weights.layers.iter().enumerate() {
15008                let base = meta.len();
15009                meta.resize(base + REC, UMAX);
15010                meta[base] = match &lw.attn {
15011                    AttnKind::Linear(w) if w.phase_delta => 1,
15012                    AttnKind::Linear(_) => 0,
15013                    AttnKind::Full { .. } => 2,
15014                    AttnKind::Bounded(_) => 3,
15015                    AttnKind::LinearGdn(_) => 4,
15016                    _ => UMAX,
15017                };
15018                meta[base + 1] = pack.put(&lw.input_norm);
15019                meta[base + 2] = pack.put(&lw.post_norm);
15020                meta[base + 25] = match &lw.attn {
15021                    AttnKind::Linear(_) => {
15022                        let off = ((phase_seen + gdn_seen) * state_stride) as u32;
15023                        phase_seen += 1;
15024                        off
15025                    }
15026                    AttnKind::LinearGdn(_) => {
15027                        let off = ((phase_seen + gdn_seen) * state_stride) as u32;
15028                        gdn_seen += 1;
15029                        off
15030                    }
15031                    _ => UMAX,
15032                };
15033                match &lw.attn {
15034                    AttnKind::LinearGdn(w) => {
15035                        // Layer record words 56..63 + 29, as the resident
15036                        // kernels read them (gpu_wgpu.rs `embryo_core_gdn_*`).
15037                        meta[base + 56] = pack.put(&Self::embryo_qtensor_f32(&w.in_proj_qkv));
15038                        meta[base + 57] = pack.put(&Self::embryo_qtensor_f32(&w.in_proj_z));
15039                        meta[base + 58] = pack.put(&Self::embryo_qtensor_f32(&w.in_proj_a));
15040                        meta[base + 59] = pack.put(&Self::embryo_qtensor_f32(&w.in_proj_b));
15041                        meta[base + 60] = pack.put(&w.conv1d);
15042                        meta[base + 61] = pack.put(&w.a_log);
15043                        meta[base + 62] = pack.put(&w.dt_bias);
15044                        meta[base + 63] = pack.put(&w.norm);
15045                        meta[base + 29] = pack.put(&Self::embryo_qtensor_f32(&w.out_proj));
15046                        meta[base + 24] = 0;
15047                    }
15048                    AttnKind::Linear(w) => {
15049                        meta[base + 3] = pack.put(&Self::embryo_qtensor_f32(&w.thq));
15050                        meta[base + 4] = pack.put(&Self::embryo_qtensor_f32(&w.thk));
15051                        meta[base + 5] = pack.put(&Self::embryo_qtensor_f32(&w.v_proj));
15052                        meta[base + 6] = pack.put(&Self::embryo_qtensor_f32(&w.out_proj));
15053                        let decay: Vec<f32> = w.decay.iter().map(|&x| x as f32).collect();
15054                        meta[base + 7] = pack.put(&decay);
15055                        if let Some((kg, kb)) = &w.k_gate {
15056                            meta[base + 8] = pack.put(&Self::embryo_qtensor_f32(kg));
15057                            meta[base + 9] = pack.put(kb);
15058                        }
15059                        if let Some(conv) = &w.conv {
15060                            meta[base + 10] = pack.put(conv);
15061                            meta[base + 24] = (conv.len() / self.hidden_size) as u32;
15062                        } else {
15063                            meta[base + 24] = 0;
15064                        }
15065                    }
15066                    AttnKind::Full { wq, wk, wv, wo, .. } => {
15067                        full_seen = true;
15068                        meta[base + 11] = pack.put(&Self::embryo_qtensor_f32(wq));
15069                        meta[base + 12] = pack.put(&Self::embryo_qtensor_f32(wk));
15070                        meta[base + 13] = pack.put(&Self::embryo_qtensor_f32(wv));
15071                        meta[base + 14] = pack.put(&Self::embryo_qtensor_f32(wo));
15072                        meta[base + 26] = (li * kv_stride) as u32;
15073                    }
15074                    AttnKind::Bounded(w) => {
15075                        meta[base + 11] = pack.put(&Self::embryo_qtensor_f32(&w.wq));
15076                        meta[base + 12] = pack.put(&Self::embryo_qtensor_f32(&w.wk));
15077                        meta[base + 13] = pack.put(&Self::embryo_qtensor_f32(&w.wv));
15078                        meta[base + 14] = pack.put(&Self::embryo_qtensor_f32(&w.wo));
15079                        // Ring slot of this anchor (anchors only, packed).
15080                        meta[base + 26] = (bounded_seen * kv_stride) as u32;
15081                        meta[base + 27] = pack.put(&w.sink_k);
15082                        meta[base + 28] = pack.put(&w.sink_v);
15083                        bounded_seen += 1;
15084                    }
15085                    _ => return None,
15086                }
15087                match &lw.ffn {
15088                    FfnKind::Dense(d) => {
15089                        meta[base + 15] = 0;
15090                        meta[base + 16] = 0;
15091                        meta[base + 21] = pack.put(&Self::embryo_qtensor_f32(&d.gate_proj));
15092                        meta[base + 22] = pack.put(&Self::embryo_qtensor_f32(&d.up_proj));
15093                        meta[base + 23] = pack.put(&Self::embryo_qtensor_f32(&d.down_proj));
15094                    }
15095                    FfnKind::Moe(m) => {
15096                        let r = m.resonance.as_ref().unwrap();
15097                        let (shared, _) = m.shared.as_ref().unwrap();
15098                        meta[base + 15] = 1;
15099                        meta[base + 16] = m.experts.len() as u32;
15100                        meta[base + 17] = pack.put(&r.mu);
15101                        meta[base + 18] = pack.put(&r.u);
15102                        meta[base + 19] = pack.put(&r.bias);
15103                        meta[base + 20] = r.k as u32;
15104                        // Word 30: the growth shell as the runtime applies
15105                        // it now (`+inf` on trunk rows, the stored finite
15106                        // shell on grown rows, all `+inf` under
15107                        // `CMF_GROWTH_SHELL=off`), followed by one `−∞`
15108                        // sentinel at index E the kernel writes as the
15109                        // score of an expert outside its shell — WGSL has
15110                        // no infinity literal, so the value travels as
15111                        // data (`embryo_core_route_finalize`).
15112                        let mut shell = r.effective_shell(m.experts.len());
15113                        shell.push(f32::NEG_INFINITY);
15114                        meta[base + 30] = pack.put(&shell);
15115                        meta[base + 21] = pack.put(&Self::embryo_qtensor_f32(&shared.gate_proj));
15116                        meta[base + 22] = pack.put(&Self::embryo_qtensor_f32(&shared.up_proj));
15117                        meta[base + 23] = pack.put(&Self::embryo_qtensor_f32(&shared.down_proj));
15118                        for (e, ex) in m.experts.iter().enumerate() {
15119                            meta[base + 32 + e * 3] =
15120                                pack.put(&Self::embryo_qtensor_f32(&ex.gate_proj));
15121                            meta[base + 33 + e * 3] =
15122                                pack.put(&Self::embryo_qtensor_f32(&ex.up_proj));
15123                            meta[base + 34 + e * 3] =
15124                                pack.put(&Self::embryo_qtensor_f32(&ex.down_proj));
15125                        }
15126                    }
15127                    FfnKind::DenseMoe(_) => return None,
15128                }
15129            }
15130            if !full_seen && self.num_layers == 0 {
15131                return None;
15132            }
15133            let id = {
15134                static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
15135                NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
15136            };
15137            let model = crate::gpu::EmbryoGraphModel {
15138                id,
15139                hidden: self.hidden_size,
15140                intermediate: self.intermediate_size,
15141                vocab: self.vocab_size,
15142                layers: self.num_layers,
15143                phase_heads: vmf.map(|c| c.num_heads).unwrap_or(0),
15144                nphase: vmf.map(|c| c.nphase).unwrap_or(0),
15145                phase_dv: vmf.map(|c| c.value_head_dim).unwrap_or(0),
15146                anchor_q_heads: self.num_heads,
15147                anchor_kv_heads: self.num_kv_heads,
15148                anchor_head_dim: self.head_dim,
15149                rotary_dim: self.rotary_dim,
15150                max_seq: self.kv_cache.max_seq_len,
15151                cluster_count,
15152                cluster_size: self.vocab_size / cluster_count,
15153                phase_state_len: vmf.map(|c| c.state_len()).unwrap_or(0),
15154                state_stride,
15155                kv_stride,
15156                norm_gemma: matches!(self.norm_style, NormStyle::Gemma),
15157                phase_mass: vmf.map(|c| c.phase_mass).unwrap_or(0.0),
15158                weights: pack.data,
15159                meta,
15160                lm_head: Self::embryo_qtensor_f32(&self.weights.lm_head),
15161                clusters: clusters.as_ref().clone(),
15162                final_norm: self.weights.final_norm.clone(),
15163                inv_freq: self.inv_freq.as_ref().clone(),
15164                bounded: bounded.is_some(),
15165                kv_layers: if bounded.is_some() {
15166                    bounded_seen
15167                } else {
15168                    self.num_layers
15169                },
15170                state_layers: phase_seen + gdn_seen,
15171                anchor_window,
15172                anchor_sink,
15173                phase_layers: phase_seen,
15174                gdn_layers: gdn_seen,
15175                gdn_heads: gdn.map(|g| g.num_v_heads).unwrap_or(0),
15176                gdn_k_heads: gdn.map(|g| g.num_k_heads).unwrap_or(0),
15177                gdn_dk: gdn.map(|g| g.key_head_dim).unwrap_or(0),
15178                gdn_dv: gdn.map(|g| g.value_head_dim).unwrap_or(0),
15179                gdn_kk: gdn.map(|g| g.conv_kernel).unwrap_or(0),
15180            };
15181            self.embryo_graph = Some(std::sync::Arc::new(model));
15182        }
15183        self.embryo_graph.clone()
15184    }
15185
15186    fn forward_layers_span(
15187        &mut self,
15188        hidden: &[f32],
15189        position: usize,
15190        task_mask: Option<&TaskMask>,
15191        from: usize,
15192        upto: Option<usize>,
15193    ) -> Vec<f32> {
15194        debug_assert!(
15195            from == 0
15196                || (self.dsv4.is_none()
15197                    && self.dsv41.is_none()
15198                    && self.qwen4_exp.is_none()
15199                    && self.g3n.is_none())
15200        );
15201        // Every plain forward — the whole-token Metal graph (`q1_graph_gpu`
15202        // wraps the GDN owners zero-copy and reallocates them on a size
15203        // change) and the CPU layer loop (reads/swaps `linear_state`) —
15204        // must see the previous speculative commit's asynchronous replay
15205        // complete. One mutex probe when nothing is pending.
15206        #[cfg(target_os = "macos")]
15207        if !crate::gpu_metal::wait_replay() {
15208            self.fail_metal_graph("the pending async replay failed before a plain forward");
15209            return vec![0.0; self.hidden_size];
15210        }
15211        if let Some(b) = &mut self.qwen4_exp {
15212            let _ = (task_mask, upto);
15213            let token_id = hidden.first().copied().unwrap_or(0.0) as u32;
15214            let mut logits = Vec::new();
15215            crate::qwen4_exp::forward_token(
15216                &b.0,
15217                &b.1,
15218                &b.2,
15219                &mut b.3,
15220                token_id,
15221                position,
15222                &self.inv_freq,
15223                self.pool.as_deref(),
15224                &mut logits,
15225                true,
15226            );
15227            self.graph_logits = Some(logits);
15228            return vec![0.0; self.hidden_size];
15229        }
15230        // DeepSeek-V4 runs its own stack: the state is hc_mult copies, and
15231        // the forward returns LOGITS, not a hidden — the head is inside it
15232        // (the final fold sits between the last layer and the norm). The
15233        // token id rides in `hidden[0]`, written by embed_single, because
15234        // the hash layers route by id rather than by content.
15235        if let Some(b) = &mut self.dsv4 {
15236            let _ = (task_mask, upto);
15237            let token_id = hidden.first().copied().unwrap_or(0.0) as u32;
15238            let (g, layers, cfg, st) = (&b.0, &b.1, b.2, &mut b.3);
15239            st.pos = position;
15240            let mut logits = Vec::new();
15241            crate::dsv4::forward_token(
15242                g,
15243                layers,
15244                &cfg,
15245                st,
15246                token_id,
15247                &self.inv_freq,
15248                self.pool.as_deref(),
15249                &mut logits,
15250            );
15251            self.graph_logits = Some(logits);
15252            self.dspark_probe(position, token_id);
15253            // The caller expects a hidden; the logits went out of band, as
15254            // with the fused lm_head path.
15255            return vec![0.0; self.hidden_size];
15256        }
15257        // DeepSeek-V4.1 owns its complete stack and emits logits out of band.
15258        if let Some(b) = &mut self.dsv41 {
15259            let _ = (task_mask, upto);
15260            let token_id = hidden.first().copied().unwrap_or(0.0) as u32;
15261            let mut logits = Vec::new();
15262            crate::dsv41::forward_token(
15263                &b.0,
15264                &b.1,
15265                &b.2,
15266                &mut b.3,
15267                token_id,
15268                position,
15269                self.pool.as_deref(),
15270                &mut logits,
15271            );
15272            self.graph_logits = Some(logits);
15273            return vec![0.0; self.hidden_size];
15274        }
15275        // Gemma-3n runs its own stack (4 AltUp replicas don't fit this
15276        // loop); `hidden` is the extended embedding from embed_single.
15277        if let Some(b) = &self.g3n {
15278            let _ = (task_mask, upto);
15279            return crate::g3n::g3n_forward(
15280                &b.0,
15281                &b.1,
15282                hidden,
15283                position,
15284                &mut self.kv_cache.layers,
15285                self.num_heads,
15286                self.num_kv_heads,
15287                self.head_dim,
15288                self.pool.as_deref(),
15289            );
15290        }
15291        // Cortiq Embryo owns a separate resident graph: phase recurrent
15292        // state, resonance routing, the GQA anchor KV and hierarchical head
15293        // all execute in one Vulkan submit. It is limited to a complete
15294        // unmasked stack; spans and task masks retain the exact host path.
15295        // `CMF_EMBRYO_DBG=1` names the gate that keeps a token off the
15296        // resident graph — every refusal below is otherwise silent.
15297        if from == 0
15298            && upto.is_none()
15299            && task_mask.is_none()
15300            && self.anchor_core.is_some()
15301            && std::env::var("CMF_EMBRYO_DBG").as_deref() == Ok("1")
15302        {
15303            static ONCE: std::sync::Once = std::sync::Once::new();
15304            ONCE.call_once(|| {
15305                eprintln!(
15306                    "embryo-dbg: graph_on decode={} prefill={} resident_env={:?} enabled_here={} \
15307                     unsupported={} eligible={}",
15308                    crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Decode),
15309                    crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Prefill),
15310                    std::env::var("CMF_EMBRYO_RESIDENT").ok(),
15311                    crate::gpu::enabled_here(),
15312                    self.graph_refused(),
15313                    self.embryo_resident_eligible(),
15314                );
15315            });
15316        }
15317        if from == 0
15318            && upto.is_none()
15319            && task_mask.is_none()
15320            // Embryo's recurrent/KV state has no host import path.  Do not
15321            // seed it for a prefill-only graph and then silently decode from
15322            // an empty CPU cache; both phases must select the resident owner.
15323            && crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Decode)
15324            && crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Prefill)
15325            // This whole-token Embryo path remains explicitly opt-in.
15326            // `CMF_GPU_WGPU_GRAPH=1` still enables the mature generic graph,
15327            // but must not silently select this model-specific resident path.
15328            && matches!(
15329                std::env::var("CMF_EMBRYO_RESIDENT").as_deref(),
15330                Ok("1") | Ok("parallel")
15331            )
15332            && crate::gpu::enabled_here()
15333            && !self.graph_refused()
15334            // Sequence owner: past position zero the device continues only
15335            // a sequence it holds. A host-owned sequence (the graph refused
15336            // at its start, or its prefix was prefilled on the host) keeps
15337            // the host path to its end — never a device attempt at p > 0
15338            // over an empty device image.
15339            && (position == 0 || self.device_sequence_position().is_some())
15340            && self.embryo_resident_eligible()
15341            && let Some(model) = self.ensure_embryo_graph()
15342        {
15343            let mut lg = Vec::new();
15344            if crate::gpu::forward_embryo_graph(&model, self.graph_kv_id, hidden, position, &mut lg)
15345            {
15346                self.graph_logits = Some(lg);
15347                return vec![0.0; self.hidden_size];
15348            }
15349            if std::env::var("CMF_EMBRYO_DBG").as_deref() == Ok("1") {
15350                eprintln!("embryo-dbg: forward_embryo_graph refused at position {position}");
15351            }
15352            // The refusal is THIS pipeline's: falling through once is safe
15353            // at position zero (the host owns the sequence from here),
15354            // while a refusal at a later position of a device-owned
15355            // sequence would mix a host KV/state path with a partial
15356            // device sequence.
15357            self.mark_graph_refused();
15358            if position != 0 {
15359                // Fail this sequence alone and leave nothing stale behind:
15360                // no reuse key, no host or device state for the next
15361                // request on this slot to "extend".
15362                self.kv_cache.clear();
15363                self.clear_history();
15364                crate::gpu::graph_kv_reset(self.graph_kv_id);
15365                panic!(
15366                    "resident Embryo graph refused at position {position}; refusing an in-flight host fallback"
15367                );
15368            }
15369        }
15370        let mut h = hidden.to_vec();
15371        // MiMo-V2 expert placement: decided before the graph or the per-op
15372        // arena can claim the budget the expert bank needs.
15373        self.mimo_moe_prepare();
15374        let _mimo_q8 = self.mimo_moe.is_on()
15375            .then(crate::qtensor::enter_full_gpu_q8_scope);
15376        // Split borrows: copy scalars / clone handles so the per-layer
15377        // cfg does not hold `&self` while the KV cache is `&mut`.
15378        let (nh, _nkv, _hd, hs, _rd, eps) = (
15379            self.num_heads,
15380            self.num_kv_heads,
15381            self.head_dim,
15382            self.hidden_size,
15383            self.rotary_dim,
15384            self.rms_eps,
15385        );
15386        let pool = self.pool.clone();
15387        // Opt-in wgpu token-graph attention (discrete Vulkan/DX12): the whole
15388        // attention sub-block runs resident in one submit. Off by default.
15389        // Whole-token wgpu graph: eligibility + arbitration.
15390        //  - explicit CMF_GPU_WGPU_GRAPH forces it on/off;
15391        //  - discrete adapters (4090: decode 76 -> 137 tok/s) and GDN
15392        //    hybrids (recurrent state device-resident, no CPU twin to
15393        //    race) TRUST it;
15394        //  - integrated/mobile adapters RACE it against the normal path
15395        //    at generation granularity (gpu::graph_race_*) — tiled
15396        //    mobile GPUs can turn the ~300-dispatch graph into seconds
15397        //    per token, while a fast phone GPU keeps its win.
15398        let graph_env = std::env::var("CMF_GPU_WGPU_GRAPH").ok();
15399        let graph_on = match graph_env.as_deref() {
15400            Some("0") => false,
15401            Some("prefill") => false, // decode keeps the per-op path
15402            Some(_) => true,
15403            // Unset: same discrete-only default as every other graph
15404            // site. "Is the GPU on" used to stand in here — which made
15405            // the 0.2 tok/s whole-token graph race-eligible on mobile
15406            // adapters and cost 12-14× on first tokens (cmfmobile
15407            // TUNING.md); integrated GPUs keep the per-op probe path.
15408            None => crate::gpu::wgpu_graph_default(),
15409        };
15410        let graph_trusted =
15411            graph_env.is_some() || crate::gpu::wgpu_graph_default() || self.gdn_cfg.is_some();
15412        let race_eligible = graph_on
15413            && upto.is_none()
15414            && task_mask.is_none()
15415            && from == 0
15416            && !self.graph_refused();
15417        let mut tail_start = 0usize;
15418        if race_eligible && crate::gpu::graph_race_use_graph(graph_trusted) {
15419            let t_graph = std::time::Instant::now();
15420            let mut lg = Vec::new();
15421            let mut gl = 0usize;
15422            let built = self.try_token_graph_wgpu(hidden, position, &mut lg, &mut gl);
15423            let declined = built.is_none();
15424            let built = match built {
15425                Some(Ok(hh)) => Some(hh),
15426                Some(Err(())) => {
15427                    // O(1) state was admitted before the device failure; the
15428                    // CPU mirrors are stale by construction.  Clear the whole
15429                    // sequence and stop rather than walking that stale state.
15430                    self.clear_sequence_state();
15431                    self.graph_failed
15432                        .store(true, std::sync::atomic::Ordering::Relaxed);
15433                    self.cancel
15434                        .store(true, std::sync::atomic::Ordering::Relaxed);
15435                    tracing::error!("token graph failed after admission; sequence state cleared");
15436                    return vec![0.0; self.hidden_size];
15437                }
15438                None => None,
15439            };
15440            // Past the transient guards (o1 still collecting, a softcap)
15441            // a refusal is about the weights and will never change —
15442            // remember it instead of walking every layer again next
15443            // token.
15444            if declined && !self.o1_active() && self.attn_softcap == 0.0 {
15445                self.mark_graph_refused();
15446            }
15447            graph_note(built.is_some(), gl, self.num_layers);
15448            if let Some(hh) = built {
15449                let dur = t_graph.elapsed();
15450                if std::env::var("CMF_GRAPH_PROF").is_ok() {
15451                    eprintln!("graph-call: {:.2} ms total", dur.as_secs_f64() * 1000.0);
15452                }
15453                if gl > 0 && gl < self.num_layers {
15454                    // Device prefix: the graph ran layers 0..gl and handed
15455                    // back the boundary hidden — the loop below owns the
15456                    // tail. The prefix layers' KV/state advanced on the
15457                    // device; the tail's advances on the host below. One
15458                    // boundary crossing per token.
15459                    h = hh;
15460                    tail_start = gl;
15461                } else if graph_trusted || !crate::gpu::graph_race_first_token_hopeless(dur) {
15462                    if !graph_trusted {
15463                        crate::gpu::graph_race_record(true, dur);
15464                    }
15465                    if !lg.is_empty() {
15466                        // Graph produced logits (final-norm + lm_head folded in) —
15467                        // pad/cap to vocab and hand them to the sampler directly.
15468                        lg.resize(self.vocab_size, 0.0);
15469                        if let Some(c) = self.final_softcap {
15470                            softcap_in_place(&mut lg, c, self.pool.as_deref());
15471                        }
15472                        self.graph_logits = Some(lg);
15473                    }
15474                    return hh;
15475                }
15476                // Hopeless first graph token: discard it and fall through
15477                // to the normal path. Safe exactly here — the prompt KV is
15478                // still CPU-owned (chunked prefill), so recomputing this
15479                // position is exact; the mirror's extra row is never read
15480                // (the race just settled on the normal path).
15481            }
15482        }
15483        // KIMI-LINEAR HAS NO SPLIT BUG. The 2.6× reported from the
15484        // model rotation (12.2 tok/s on one card against 4.6 on two)
15485        // was a single measurement of a model whose arm arbitration is
15486        // borderline, and it did not survive repetition. Three runs an
15487        // arm, same binary, back to back:
15488        //   probe on : 1 GPU 9.5 / 5.7 / 5.9   2 GPU 7.8 / 13.0 / 13.3
15489        //   pinned   : 1 GPU 5.6 / 5.3 / 5.2   2 GPU 3.5 / 4.2 / 3.4
15490        // With the arms pinned the split costs about 1.45×, which is
15491        // what a layer split costs. With the probe free, TWO CARDS RUN
15492        // FASTER — because for this model the CPU arm wins some op
15493        // classes and the probe finds that.
15494        //
15495        // Two things do stand, and both are measured. The token graph
15496        // builds NOTHING here (`covered 0 of 14 layers [0..14)`), so
15497        // every layer walks per-op on either arm — that is where the
15498        // headroom is, not in the split. And this model's benchmark is
15499        // unusable without `CMF_GPU_PROBE=0`: the arbitration alone
15500        // moves it by more than 2×.
15501        //
15502        // Span runs (network split): the graph covers exactly [from..=upto]
15503        // — one submit per SEGMENT per token. No race: its state is global
15504        // and calibrated on full stacks, so spans take the graph only where
15505        // it is trusted by default (discrete adapters / CMF_GPU_WGPU_GRAPH).
15506        let span = from > 0 || upto.is_some();
15507        if span && graph_on && task_mask.is_none() && graph_trusted {
15508            let upto_excl = upto.map_or(self.num_layers, |u| u + 1);
15509            let mut lg = Vec::new();
15510            let mut gl = 0usize;
15511            let span_res =
15512                self.try_token_graph_wgpu_span(hidden, position, &mut lg, from, upto_excl, &mut gl);
15513            let span_res = match span_res {
15514                Some(Ok(hh)) => Some(hh),
15515                Some(Err(())) => {
15516                    self.clear_sequence_state();
15517                    self.graph_failed
15518                        .store(true, std::sync::atomic::Ordering::Relaxed);
15519                    self.cancel
15520                        .store(true, std::sync::atomic::Ordering::Relaxed);
15521                    tracing::error!(
15522                        "span token graph failed after admission; sequence state cleared"
15523                    );
15524                    return vec![0.0; self.hidden_size];
15525                }
15526                None => None,
15527            };
15528            graph_note(span_res.is_some(), gl, upto_excl - from);
15529            if std::env::var("CMF_GPU_DEBUG").is_ok() {
15530                // How much of the span the graph actually covered. A
15531                // prefix of nothing means every layer walks per-op and
15532                // the split's extra cost is elsewhere.
15533                static SEEN: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
15534                if SEEN.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 4 {
15535                    eprintln!(
15536                        "span graph: covered {gl} of {} layers [{from}..{upto_excl}) res={}",
15537                        upto_excl - from,
15538                        span_res.is_some()
15539                    );
15540                }
15541            }
15542            if let Some(hh) = span_res {
15543                if gl == upto_excl - from {
15544                    if !lg.is_empty() {
15545                        lg.resize(self.vocab_size, 0.0);
15546                        if let Some(c) = self.final_softcap {
15547                            softcap_in_place(&mut lg, c, self.pool.as_deref());
15548                        }
15549                        self.graph_logits = Some(lg);
15550                    }
15551                    crate::gpu::set_layer(-1);
15552                    return hh;
15553                }
15554                // Partial device prefix of the span: CPU owns the tail.
15555                h = hh;
15556                tail_start = from + gl;
15557            }
15558        }
15559        // Layers the host is about to run whose device mirror moved ahead
15560        // of the host cache (a device prefix that shrank since the prompt,
15561        // a batched-prefill prefix longer than this token's): bring their
15562        // rows over first. One comparison per layer when nothing lags.
15563        let t_race_cpu = (race_eligible && !graph_trusted).then(std::time::Instant::now);
15564
15565        // A partial graph is an explicit GPU-prefix / CPU-tail split. Keep
15566        // the tail PURE host-side: letting its QTensor hooks re-enter the
15567        // residency arena streams every omitted layer through Vulkan and the
15568        // driver's freed-allocation cache can grow to the full model size
15569        // (25.4 GiB observed with a 14 GiB budget on Granite 30B Q8_2F).
15570        // With a MiMo expert bank the tail is not a whole-layer host
15571        // stream: its experts run from the bank (never the arena) and its
15572        // projections stay per-op on the device, which the bank's placement
15573        // left room for.
15574        let host_tail = tail_start > from;
15575        let _host_tail = (host_tail && !self.mimo_moe.is_on()).then(crate::gpu::enter_cpu_scope);
15576        let automatic_gpu_prefix = self.automatic_gpu_prefix();
15577
15578        let _prof_layers = crate::cpuprof::time(crate::cpuprof::Slot::Layers);
15579        #[cfg(target_os = "macos")]
15580        let mut gpu_skip_until = 0usize;
15581        for li in tail_start.max(from)..self.num_layers {
15582            let _capacity_tail = automatic_gpu_prefix
15583                .filter(|&prefix| li >= prefix && !self.mimo_moe.is_dynamic(li, host_tail))
15584                .map(|_| crate::gpu::enter_cpu_scope());
15585            crate::gpu::set_layer(li as i64); // layer-split GPU/CPU (CMF_GPU_LAYERS)
15586            if let Some(u) = upto {
15587                if li > u {
15588                    break;
15589                }
15590            }
15591            if let Some(mask) = task_mask {
15592                if !mask.layer_alive(li) {
15593                    continue; // dead layer: residual pass-through
15594                }
15595            }
15596            // Whole-block q1 token graph: a run of consecutive q1
15597            // layers — GDN and full attention — executes with one sync
15598            // per CPU attend instead of per op (macOS/Metal).
15599            #[cfg(target_os = "macos")]
15600            {
15601                if li < gpu_skip_until {
15602                    continue;
15603                }
15604                if task_mask.is_none() {
15605                    let end = self.q1_graph_gpu(li, upto, position, &mut h);
15606                    if self
15607                        .graph_failed
15608                        .load(std::sync::atomic::Ordering::Relaxed)
15609                    {
15610                        // The graph may have mutated device state before a
15611                        // command-buffer error. Never continue with a CPU
15612                        // tail or read a stale host mirror after admission.
15613                        return vec![0.0; self.hidden_size];
15614                    }
15615                    if end > li {
15616                        gpu_skip_until = end;
15617                        // Looped Transformer: the graph stopped at a loop
15618                        // boundary — apply final norm before the next iteration.
15619                        if self.is_loop_end(end - 1) && end < self.num_layers {
15620                            h = inference::rms_norm(
15621                                &h,
15622                                &self.weights.final_norm,
15623                                self.rms_eps,
15624                                self.norm_style,
15625                            );
15626                        }
15627                        continue;
15628                    }
15629                }
15630            }
15631
15632            if task_mask.is_none() {
15633                match self.mimo_graph_layer_rows(li, &mut h, &[position]) {
15634                    crate::gpu::BatchGraphOutcome::Completed => continue,
15635                    crate::gpu::BatchGraphOutcome::Failed => return vec![0.0; self.hidden_size],
15636                    crate::gpu::BatchGraphOutcome::Declined => {},
15637                }
15638            }
15639            #[cfg(feature = "gpu")]
15640            self.pull_lagging_host_kv(li, li + 1, position);
15641            let lw = &self.weights.layers[self.phys_layer(li)];
15642            if let Ok(tp) = std::env::var("CMF_TRACE_POS") {
15643                if tp.parse::<usize>().ok() == Some(position) {
15644                    let n: f32 = h.iter().map(|x| x * x).sum::<f32>().sqrt();
15645                    eprintln!(
15646                        "TRACE pos {position} layer {li}: |h| = {n:.6} h0 {:.6} h1 {:.6}",
15647                        h[0], h[1]
15648                    );
15649                }
15650            }
15651            // Norm into the pipeline scratch — the returning rms_norm
15652            // allocated twice per layer per token (roadmap §3 P0).
15653            let prof = crate::cpuprof::time(crate::cpuprof::Slot::Norms);
15654            inference::rms_norm_into(
15655                &h,
15656                &lw.input_norm,
15657                self.rms_eps,
15658                self.norm_style,
15659                &mut self.ws.n1,
15660            );
15661            drop(prof);
15662
15663            let attn_out = match &lw.attn {
15664                AttnKind::Mla(w) => {
15665                    let inv_freq_l = self.layer_inv_freq(li);
15666                    let rs = self.layer_rope_scale(li);
15667                    let eps = self.rms_eps;
15668                    let pool = self.pool.clone();
15669                    mla_attention(
15670                        w,
15671                        &self.ws.n1,
15672                        &mut self.kv_cache.layers[li],
15673                        position,
15674                        &inv_freq_l,
15675                        rs,
15676                        eps,
15677                        pool.as_deref(),
15678                    )
15679                }
15680                AttnKind::Linear(w) => {
15681                    let cfg = self.vmf_cfg.expect("linear layer without vmf_cfg");
15682                    vmf_phase_forward(
15683                        &self.ws.n1,
15684                        w,
15685                        &cfg,
15686                        &mut self.kv_cache.layers[li].linear_state,
15687                        self.pool.as_deref(),
15688                    )
15689                }
15690                AttnKind::Kda(w) => {
15691                    let cfg = self.kda_cfg.expect("kda layer without kda_cfg");
15692                    crate::linear_core::kda_forward(
15693                        &self.ws.n1,
15694                        w,
15695                        &cfg,
15696                        &mut self.kv_cache.layers[li].linear_state,
15697                        self.pool.as_deref(),
15698                    )
15699                }
15700                AttnKind::LinearGdn(w) => {
15701                    let cfg = self.gdn_cfg.expect("gdn layer without gdn_cfg");
15702                    gdn_forward(
15703                        &self.ws.n1,
15704                        w,
15705                        &cfg,
15706                        &mut self.kv_cache.layers[li].linear_state,
15707                        self.pool.as_deref(),
15708                    )
15709                }
15710                AttnKind::ShortConv(w) => {
15711                    let cfg = self
15712                        .short_conv_cfg
15713                        .expect("short-conv layer without short_conv_cfg");
15714                    short_conv_forward(
15715                        &self.ws.n1,
15716                        w,
15717                        &cfg,
15718                        &mut self.kv_cache.layers[li].linear_state,
15719                        self.pool.as_deref(),
15720                    )
15721                }
15722                AttnKind::Bounded(w) => {
15723                    // Natively bounded anchor: insert into the ring, attend
15724                    // over sinks ∪ window. No position, nothing appended.
15725                    let rope = self
15726                        .bounded_rope
15727                        .clone()
15728                        .expect("bounded layer without an installed rotation table");
15729                    let cfg = crate::bounded::BoundedAttnCfg {
15730                        num_heads: self.num_heads,
15731                        num_kv_heads: self.num_kv_heads,
15732                        head_dim: self.head_dim,
15733                        hidden_size: hs,
15734                        scale: self.attn_scale,
15735                        rope: &rope,
15736                        pool: pool.as_deref(),
15737                    };
15738                    crate::bounded::bounded_attention(
15739                        &self.ws.n1,
15740                        w,
15741                        &mut self.kv_cache.layers[li],
15742                        &cfg,
15743                    )
15744                }
15745                AttnKind::Full {
15746                    wq,
15747                    wk,
15748                    wv,
15749                    wo,
15750                    q_norm,
15751                    k_norm,
15752                    output_gate,
15753                    softplus_gate,
15754                    bias,
15755                } if self.kv_cache.layers[li].o1_sealed() => {
15756                    // O(1) override: decode on the sealed Nyström state
15757                    // instead of the growing KV cache.
15758                    let inv_freq_l = self.layer_inv_freq(li);
15759                    let (nkv_l, hd_l, rd_l) = self.layer_geom(li);
15760                    let cfg = QwenAttnCfg {
15761                        num_heads: self.layer_num_heads(li),
15762                        num_kv_heads: nkv_l,
15763                        head_dim: hd_l,
15764                        hidden_size: hs,
15765                        position,
15766                        inv_freq: &inv_freq_l,
15767                        rotary_dim: rd_l,
15768                        scale: self.attn_scale,
15769                        softcap: self.attn_softcap,
15770                        window: None,
15771                        v_norm: self.attn_v_norm,
15772                        qk_norm_after_rope: self.qk_norm_after_rope,
15773                        gate_sigmoid: self.proj_gate_sigmoid,
15774                        q_norm: q_norm.as_deref(),
15775                        k_norm: k_norm.as_deref(),
15776                        output_gate: *output_gate,
15777                        softplus_gate: softplus_gate
15778                            .as_ref()
15779                            .map(|(gate, per_head)| (gate, *per_head)),
15780                        rope_scale: self.layer_rope_scale(li),
15781                        bias: bias
15782                            .as_ref()
15783                            .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
15784                        rms_eps: eps,
15785                        norm_style: self.norm_style,
15786                        pool: pool.as_deref(),
15787                        v_head_dim: self.layer_v_dim(li),
15788                    };
15789                    attention::qwen_attention_nystrom(
15790                        &self.ws.n1,
15791                        wq,
15792                        wk,
15793                        wv,
15794                        wo,
15795                        &mut self.kv_cache.layers[li],
15796                        &cfg,
15797                    )
15798                }
15799                AttnKind::Full {
15800                    wq,
15801                    wk,
15802                    wv,
15803                    wo,
15804                    q_norm,
15805                    k_norm,
15806                    output_gate,
15807                    softplus_gate,
15808                    bias,
15809                } => 'attn: {
15810                    // wgpu token-graph attention (opt-in): whole sub-block in
15811                    // one submit, device K/V mirror. q1 only, no gate/bias/mask.
15812                    // Its kernel has no window, sink or narrow-V slot and
15813                    // one mirror geometry: such models stay on the CPU attend.
15814                    let dropin_reason =
15815                        graph_on.then(|| self.graph_attn_decline_reason()).flatten();
15816                    if let Some(reason) = dropin_reason {
15817                        self.note_graph_decline("wgpu attn dropin", reason);
15818                    }
15819                    if graph_on
15820                        && dropin_reason.is_none()
15821                        && !*output_gate
15822                        && softplus_gate.is_none()
15823                        && self.attention_heads_per_layer.is_none()
15824                        && bias.is_none()
15825                        && task_mask.is_none()
15826                    {
15827                        let inv_freq_l = self.layer_inv_freq(li);
15828                        let (nkv_l, hd_l, rd_l) = self.layer_geom(li);
15829                        let gemma = self.norm_style == cortiq_core::NormStyle::Gemma;
15830                        if let (Some((gm, qi)), Some((_, ki)), Some((_, vi)), Some((_, oi))) = (
15831                            wq.mapped_q1(),
15832                            wk.mapped_q1(),
15833                            wv.mapped_q1(),
15834                            wo.mapped_q1(),
15835                        ) {
15836                            let gm = gm.clone();
15837                            let mut out = vec![0f32; hs];
15838                            let cache = &self.kv_cache.layers[li];
15839                            if crate::gpu::attn_dropin(
15840                                &gm,
15841                                self.graph_kv_id,
15842                                li,
15843                                &self.ws.n1,
15844                                qi,
15845                                ki,
15846                                vi,
15847                                oi,
15848                                q_norm.as_deref(),
15849                                k_norm.as_deref(),
15850                                self.qk_norm_after_rope,
15851                                &inv_freq_l,
15852                                nh,
15853                                nkv_l,
15854                                hd_l,
15855                                rd_l,
15856                                hs,
15857                                position,
15858                                self.kv_cache.max_seq_len,
15859                                gemma,
15860                                eps as f32,
15861                                cache.k_heads(),
15862                                cache.v_heads(),
15863                                &mut out,
15864                            ) {
15865                                break 'attn out;
15866                            }
15867                        }
15868                    }
15869                    let masked = task_mask
15870                        .map(|m| m.head_flags(li, self.num_heads).iter().any(|&a| !a))
15871                        .unwrap_or(false);
15872                    let f32_view = (wq.as_f32(), wk.as_f32(), wv.as_f32(), wo.as_f32());
15873                    // The masked kernel knows one pipeline-wide geometry and
15874                    // RoPE table, no window and no sink.
15875                    let plain = self.layer_attn_plain(li);
15876                    match (masked, f32_view) {
15877                        // Historical masked path (f32 slices; the loader
15878                        // keeps masked models in f32).
15879                        (true, (Some(q), Some(k), Some(v), Some(o))) if plain => {
15880                            let active_heads = task_mask.unwrap().head_flags(li, self.num_heads);
15881                            attention::multi_head_attention(
15882                                &self.ws.n1,
15883                                q,
15884                                k,
15885                                v,
15886                                o,
15887                                &mut self.kv_cache.layers[li],
15888                                self.num_heads,
15889                                self.num_kv_heads,
15890                                self.head_dim,
15891                                self.hidden_size,
15892                                position,
15893                                &active_heads,
15894                                &self.inv_freq,
15895                            )
15896                        }
15897                        (masked, _) => {
15898                            if masked {
15899                                tracing::warn!(
15900                                    "layer {li}: head mask on quantized weights or on a \
15901                                     window/sink/per-layer-geometry layer not supported \
15902                                     yet — executing dense"
15903                                );
15904                            }
15905                            let inv_freq_l = self.layer_inv_freq(li);
15906                            let (nkv_l, hd_l, rd_l) = self.layer_geom(li);
15907                            let cfg = QwenAttnCfg {
15908                                num_heads: self.layer_num_heads(li),
15909                                num_kv_heads: nkv_l,
15910                                head_dim: hd_l,
15911                                hidden_size: hs,
15912                                position,
15913                                inv_freq: &inv_freq_l,
15914                                rotary_dim: rd_l,
15915                                scale: self.attn_scale,
15916                                softcap: self.attn_softcap,
15917                                window: self.layer_window(li),
15918                                v_norm: self.attn_v_norm,
15919                                qk_norm_after_rope: self.qk_norm_after_rope,
15920                                gate_sigmoid: self.proj_gate_sigmoid,
15921                                q_norm: q_norm.as_deref(),
15922                                k_norm: k_norm.as_deref(),
15923                                output_gate: *output_gate,
15924                                softplus_gate: softplus_gate
15925                                    .as_ref()
15926                                    .map(|(gate, per_head)| (gate, *per_head)),
15927                                rope_scale: self.layer_rope_scale(li),
15928                                bias: bias
15929                                    .as_ref()
15930                                    .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
15931                                rms_eps: eps,
15932                                norm_style: self.norm_style,
15933                                pool: pool.as_deref(),
15934                                v_head_dim: self.layer_v_dim(li),
15935                            };
15936                            attention::qwen_attention(
15937                                &self.ws.n1,
15938                                wq,
15939                                wk,
15940                                wv,
15941                                wo,
15942                                &mut self.kv_cache.layers[li],
15943                                &cfg,
15944                            )
15945                        }
15946                    }
15947                }
15948            };
15949            // Gemma sandwich norm: normalize the attention branch before
15950            // it joins the residual stream.
15951            let attn_out = match &self.weights.layers[self.phys_layer(li)].attn_out_norm {
15952                Some(w) => inference::rms_norm(&attn_out, w, self.rms_eps, self.norm_style),
15953                None => attn_out,
15954            };
15955            let lw = &self.weights.layers[self.phys_layer(li)];
15956            let prof = crate::cpuprof::time(crate::cpuprof::Slot::Norms);
15957            inference::add_rmsnorm_fused_into(
15958                &mut h,
15959                &attn_out,
15960                &lw.post_norm,
15961                self.rms_eps,
15962                self.norm_style,
15963                &mut self.ws.p1,
15964            );
15965            drop(prof);
15966            let mut attn_out = attn_out;
15967            attention::recycle_buf(&mut attn_out);
15968            let post_normed = &self.ws.p1;
15969
15970            let ffn_masked = task_mask
15971                .map(|m| m.ffn_active_count(li) < self.intermediate_size)
15972                .unwrap_or(false);
15973            // One masked dense CONTRACT, dispatched by cost. The
15974            // activation-zeroing arm (the batched sweep's, validated
15975            // against the replica to 0.8%) computes the FULL fused FFN
15976            // and zeroes the dead — right whenever most neurons live.
15977            // The sparse arm reads ONLY active rows and down columns —
15978            // per-row dots are slower per element than the fused kernel,
15979            // so it pays only once the mask is deep enough. The 0.5
15980            // crossover is first-principles (fused kernels run ~2x the
15981            // per-row dot throughput); a shallow specialist (95% alive)
15982            // stays fused, a --target-sparsity bake flips arms on its
15983            // own weight.
15984            let ffn_out = match (ffn_masked, &lw.ffn) {
15985                // A defragged tube layer answers its own mask: the core
15986                // always runs, each tube runs when its bit is on, and
15987                // the tubes that are off are never read from the mmap.
15988                (_, FfnKind::Dense(d)) if !d.segs.is_empty() => {
15989                    let row = task_mask
15990                        .and_then(|tm| tm.ffn_masks.get(li))
15991                        .map(|v| v.as_slice());
15992                    tube_ffn(d, post_normed, 1, self.pool.as_deref(), row)
15993                }
15994                (true, FfnKind::Dense(d)) => {
15995                    let tm = task_mask.unwrap();
15996                    let alive = tm.ffn_active_count(li);
15997                    let deep = alive * 2 <= self.intermediate_size;
15998                    if deep && d.down_proj.sparse_col_ok() && !d.gate_proj.has_prism_contract() {
15999                        let active = tm.ffn_active_indices(li);
16000                        sparse_ffn_quant(
16001                            d,
16002                            post_normed,
16003                            &active,
16004                            self.hidden_size,
16005                            self.pool.as_deref(),
16006                        )
16007                    } else if deep
16008                        && let (Some(g), Some(u), Some(dn)) = (
16009                            d.gate_proj.as_f32(),
16010                            d.up_proj.as_f32(),
16011                            d.down_proj.as_f32(),
16012                        )
16013                    {
16014                        let active = tm.ffn_active_indices(li);
16015                        inference::sparse_ffn_forward(
16016                            post_normed,
16017                            g,
16018                            u,
16019                            dn,
16020                            self.hidden_size,
16021                            self.intermediate_size,
16022                            &active,
16023                            self.pool.as_deref(),
16024                        )
16025                    } else {
16026                        let row = tm.ffn_masks.get(li).map(|v| v.as_slice());
16027                        dense_ffn_batch(d, post_normed, 1, self.pool.as_deref(), row)
16028                    }
16029                }
16030                (true, FfnKind::Moe(m)) => {
16031                    // MoE is sparse by expert selection; a task mask
16032                    // narrows the ROUTABLE set via its expert fields
16033                    // (spec §5) when it carries them.
16034                    let allowed = task_mask.and_then(|tm| tm.expert_flags(li, m.experts.len()));
16035                    ffn_forward(
16036                        &lw.ffn,
16037                        post_normed,
16038                        self.pool.as_deref(),
16039                        allowed.as_deref(),
16040                    )
16041                }
16042                (true, FfnKind::DenseMoe(dm)) => dense_moe_ffn(
16043                    dm,
16044                    post_normed,
16045                    &h,
16046                    self.rms_eps,
16047                    self.norm_style,
16048                    self.pool.as_deref(),
16049                ),
16050                (false, _) => match &lw.ffn {
16051                    FfnKind::DenseMoe(dm) => dense_moe_ffn(
16052                        dm,
16053                        post_normed,
16054                        &h,
16055                        self.rms_eps,
16056                        self.norm_style,
16057                        self.pool.as_deref(),
16058                    ),
16059                    FfnKind::Moe(m)
16060                        if task_mask.is_none() && self.mimo_moe.is_dynamic(li, host_tail) =>
16061                    {
16062                        moe_ffn_banked(&mut self.mimo_moe, li, m, post_normed, self.pool.as_deref())
16063                    }
16064                    _ => {
16065                        let allowed = match (&lw.ffn, task_mask) {
16066                            (FfnKind::Moe(m), Some(tm)) => tm.expert_flags(li, m.experts.len()),
16067                            _ => None,
16068                        };
16069                        ffn_forward(
16070                            &lw.ffn,
16071                            post_normed,
16072                            self.pool.as_deref(),
16073                            allowed.as_deref(),
16074                        )
16075                    }
16076                },
16077            };
16078            let ffn_out = match &self.weights.layers[self.phys_layer(li)].ffn_out_norm {
16079                Some(w) => inference::rms_norm(&ffn_out, w, self.rms_eps, self.norm_style),
16080                None => ffn_out,
16081            };
16082            for (i, &f) in ffn_out.iter().enumerate() {
16083                h[i] += f;
16084            }
16085            let mut ffn_out = ffn_out;
16086            attention::recycle_buf(&mut ffn_out);
16087
16088            // Gemma-4: the layer output is scaled by a learned scalar.
16089            if let Some(sc) = self.weights.layers[self.phys_layer(li)].layer_scale {
16090                for v in h.iter_mut() {
16091                    *v *= sc;
16092                }
16093            }
16094            // CMF_LAYER_DUMP: this position's hidden after layer li.
16095            if self.layer_dump.is_some() {
16096                self.dump_layer_row(position, li, &h);
16097            }
16098
16099            // Looped Transformer: apply final norm at the end of each loop iteration.
16100            // Nanbeige 4.2: after layer 21 (virtual), apply norm before looping back to layer 0.
16101            if self.is_loop_end(li) && li + 1 < self.num_layers {
16102                h = inference::rms_norm(
16103                    &h,
16104                    &self.weights.final_norm,
16105                    self.rms_eps,
16106                    self.norm_style,
16107                );
16108            }
16109
16110            // Dynamic routing φ capture (on-policy): the
16111            // EMA of the post-residual hidden at the router's phi_layer,
16112            // updated as the context evolves during decode.
16113            if self.dyn_phi_layer == Some(li) {
16114                self.update_dyn_phi(&h);
16115            }
16116        }
16117        crate::gpu::set_layer(-1); // layers done — lm_head outside layer-split
16118        if let Some(t) = t_race_cpu {
16119            crate::gpu::graph_race_record(false, t.elapsed());
16120        }
16121
16122        h
16123    }
16124
16125    /// EMA of φ at the router layer (rolling, weight 0.2 = ~5-token
16126    /// horizon). First observation seeds it exactly.
16127    fn update_dyn_phi(&mut self, h: &[f32]) {
16128        const A: f32 = 0.2;
16129        if self.dyn_phi_ema.len() != h.len() {
16130            self.dyn_phi_ema = vec![0.0; h.len()];
16131            self.dyn_phi_seen = 0;
16132        }
16133        if self.dyn_phi_seen == 0 {
16134            self.dyn_phi_ema.copy_from_slice(h);
16135        } else {
16136            for (e, &v) in self.dyn_phi_ema.iter_mut().zip(h) {
16137                *e = (1.0 - A) * *e + A * v;
16138            }
16139        }
16140        self.dyn_phi_seen += 1;
16141    }
16142
16143    /// Current router φ (EMA at phi_layer); empty until first capture.
16144    pub fn dyn_phi(&self) -> &[f32] {
16145        &self.dyn_phi_ema
16146    }
16147
16148    /// Enable/disable φ capture at the router layer, reset the EMA.
16149    pub fn set_dyn_phi_layer(&mut self, layer: Option<usize>) {
16150        self.dyn_phi_layer = layer;
16151        self.dyn_phi_ema.clear();
16152        self.dyn_phi_seen = 0;
16153    }
16154
16155    /// Skills eligible for dynamic switching: (index, id, phi_layer).
16156    pub fn dynamic_skills(&self) -> Vec<(usize, String, usize)> {
16157        let Some(model) = &self.model else {
16158            return Vec::new();
16159        };
16160        model
16161            .header
16162            .skills
16163            .iter()
16164            .enumerate()
16165            .filter_map(|(i, sk)| {
16166                // A v2 record routes only through the request-level
16167                // backbone-gated decision (its status and gate are
16168                // checked there), never per token.
16169                if sk.is_v2() {
16170                    return None;
16171                }
16172                let ok = matches!(self.dyn_skill_layers.get(i), Some(Some(_)));
16173                let sel = sk.selection.as_ref()?;
16174                (ok).then(|| (i, sk.id.clone(), sel.phi_layer))
16175            })
16176            .collect()
16177    }
16178
16179    /// Index of the currently overlaid skill (None = backbone).
16180    pub fn active_skill(&self) -> Option<usize> {
16181        self.dyn_active
16182    }
16183
16184    /// Enable dynamic per-token skill routing: build the hysteresis
16185    /// router from the container's routable skills, start φ capture at
16186    /// their (shared) phi_layer. Returns the number of routable skills
16187    /// (0 = nothing to route; router stays off). Idempotent.
16188    pub fn enable_dynamic_routing(&mut self) -> usize {
16189        use crate::swarm::{DynRouter, RoutableSkill};
16190        let Some(model) = self.model.clone() else {
16191            return 0;
16192        };
16193        // Router policy v2 (spec §9.4) routes per REQUEST: the backbone is
16194        // the default and only the backbone-gated decision may pick a
16195        // skill. A per-token switch would bypass that gate (and change
16196        // the O(1) state mid-sequence), so the hysteresis router never
16197        // runs on such a file; the caller keeps the request-level
16198        // decision.
16199        if let Some(r) = &model.header.router {
16200            tracing::warn!(
16201                "dynamic routing disabled: this file declares router policy '{}' with \
16202                 granularity \"{}\" — the request-level decision applies instead",
16203                r.policy,
16204                r.granularity
16205            );
16206            return 0;
16207        }
16208        // Format-v2 skill records (bit SKILLS_V2) without a router policy:
16209        // their status/gate contract ("auto-routing requires active +
16210        // measured") lives in the backbone-gated decision only — the
16211        // hysteresis router would switch into a quarantined record
16212        // (fail-open). Refuse the whole file, not just its v2 records.
16213        if model.required_features & cortiq_core::format::features::SKILLS_V2 != 0
16214            || model.header.skills.iter().any(|s| s.is_v2())
16215        {
16216            tracing::warn!(
16217                "dynamic routing disabled: this file carries format-v2 skill records \
16218                 (SKILLS_V2) — they route per request through a router policy only"
16219            );
16220            return 0;
16221        }
16222        // A blend materialized f32 working tensors into the layers; there
16223        // is no single skill index to revert from → refuse (honest).
16224        if self.dyn_blend_loaded {
16225            tracing::warn!("dynamic routing unavailable on a blend-loaded pipeline");
16226            return 0;
16227        }
16228        // A statically-overlaid skill that is NOT FFN-eligible can't be
16229        // cheaply reverted at generation start → refuse rather than
16230        // silently keep it overlaid.
16231        if let Some(a) = self.dyn_active {
16232            if !matches!(self.dyn_skill_layers.get(a), Some(Some(_))) {
16233                tracing::warn!("loaded skill is not FFN-eligible — dynamic routing unavailable");
16234                return 0;
16235            }
16236        }
16237        let hidden = self.hidden_size;
16238        let mut skills = Vec::new();
16239        for (idx, id, _phi) in self.dynamic_skills() {
16240            if let Some(sel) = model.header.skills[idx].selection.as_ref() {
16241                if let Some(rs) = RoutableSkill::from_descriptor(idx, id, sel, hidden) {
16242                    skills.push(rs);
16243                }
16244            }
16245        }
16246        if skills.is_empty() {
16247            return 0;
16248        }
16249        // Skills should share a phi_layer; warn (not fail) if they don't.
16250        let phi = skills[0].phi_layer;
16251        if skills.iter().any(|s| s.phi_layer != phi) {
16252            tracing::warn!("routable skills disagree on phi_layer; using {phi}");
16253        }
16254        let n = skills.len();
16255        self.set_dyn_phi_layer(Some(phi));
16256        self.dyn_router = Some(DynRouter::new(skills));
16257        n
16258    }
16259
16260    /// Human-readable switch log from the last dynamic-routed generation.
16261    pub fn route_switches(&self) -> Vec<(usize, Option<String>, Option<String>)> {
16262        self.dyn_router
16263            .as_ref()
16264            .map(|r| r.switches.clone())
16265            .unwrap_or_default()
16266    }
16267
16268    /// LM head: hidden → logits [vocab_size]. The dominant matvec of
16269    /// every decode step — row-parallel on the worker pool.
16270    fn lm_head_forward(&self, hidden: &[f32]) -> Vec<f32> {
16271        let _mimo_q8 = self.mimo_moe.is_on()
16272            .then(crate::qtensor::enter_full_gpu_q8_scope);
16273        let rows = self.weights.lm_head.rows();
16274        let mut logits = attention::take_buf(rows.min(self.vocab_size));
16275        // Banked MiMo uses the same exact projection family for the
16276        // plain/draft head and the batched verification head. Read both
16277        // scale planes in-place instead of preparing per-op scale buffers.
16278        let served = self.mimo_moe.is_on() && crate::gpu::mimo_q8_short_enabled()
16279            && rows == self.vocab_size && !self.weights.lm_head.has_prism_contract()
16280            && self.weights.lm_head.graph_weight().is_some_and(|(model, idx, kind, _)| {
16281                kind == 7 && crate::gpu::q82_short_rows(model, idx, hidden, 1,
16282                    rows, self.hidden_size, &mut logits)
16283            });
16284        if !served {
16285            self.weights.lm_head.matvec(hidden, &mut logits, self.pool.as_deref());
16286        }
16287        logits.resize(self.vocab_size, 0.0);
16288        if let Some(m) = self.logit_multiplier {
16289            for l in logits.iter_mut() {
16290                *l *= m;
16291            }
16292        }
16293        if let Some(c) = self.final_softcap {
16294            softcap_in_place(&mut logits, c, self.pool.as_deref());
16295        }
16296        if let Some(cm) = self.head_clusters.as_ref() {
16297            self.hierarchical_head_logprobs(hidden, cm, &mut logits);
16298        }
16299        logits
16300    }
16301
16302    /// Two-level head (Cortiq Embryo): in place, logits[v] ← log p(v) =
16303    /// (lc[c] − lse(lc)) + (logit[v] − lse over v's cluster block), c = v / S.
16304    fn hierarchical_head_logprobs(&self, hidden: &[f32], cm: &[f32], logits: &mut [f32]) {
16305        let h = hidden.len();
16306        let ncl = cm.len() / h.max(1);
16307        if ncl == 0 || logits.len() % ncl != 0 {
16308            return;
16309        }
16310        let cs = logits.len() / ncl;
16311        // cluster logits + log-softmax
16312        let mut lc = vec![0.0f32; ncl];
16313        for c in 0..ncl {
16314            let row = &cm[c * h..(c + 1) * h];
16315            let mut s = 0.0f32;
16316            for j in 0..h {
16317                s += row[j] * hidden[j];
16318            }
16319            lc[c] = s;
16320        }
16321        let mx = lc.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
16322        let lse: f32 = mx + lc.iter().map(|v| (v - mx).exp()).sum::<f32>().ln();
16323        for c in 0..ncl {
16324            let blk = &mut logits[c * cs..(c + 1) * cs];
16325            let bm = blk.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
16326            let bl: f32 = bm + blk.iter().map(|v| (v - bm).exp()).sum::<f32>().ln();
16327            let add = lc[c] - lse - bl;
16328            for v in blk.iter_mut() {
16329                *v += add;
16330            }
16331        }
16332    }
16333
16334    /// Prefill `ids` and return the next-token logits — what the model
16335    /// would predict next, WITHOUT committing to generation (introspection
16336    /// for `cortiq explain`). Clears and repopulates the KV cache; leaves
16337    /// the active overlay untouched.
16338    pub fn prefill_next_logits(&mut self, ids: &[u32], task_mask: Option<&TaskMask>) -> Vec<f32> {
16339        #[cfg(target_os = "macos")]
16340        crate::gpu_metal::set_io_namespace(self.graph_kv_id);
16341        self.clear_sequence_state();
16342        // This helper is used by the pooled classification endpoint, where
16343        // every request is a fresh sequence. The shared reset also clears the
16344        // wgpu token graph's device-side recurrent state.
16345        crate::gpu::graph_race_begin_generation();
16346        if task_mask.is_none() {
16347            self.o1_begin();
16348        }
16349        let mut hidden = vec![0.0f32; self.hidden_size];
16350        for (pos, &id) in ids.iter().enumerate() {
16351            let emb = self.embed_single(id);
16352            hidden = self.forward_layers(&emb, pos, task_mask);
16353        }
16354        if let Err(err) = self.o1_seal_checked() {
16355            self.o1_fail(err);
16356        }
16357        // Stacks that own their head (V4, V4.1, Qwen3.8-Flash-Next, GLM-5)
16358        // return a zero hidden and hand the logits out of band.
16359        if let Some(logits) = self.graph_logits.take() {
16360            return logits;
16361        }
16362        inference::rms_norm_into(
16363            &hidden,
16364            &self.weights.final_norm,
16365            self.rms_eps,
16366            self.norm_style,
16367            &mut self.ws.n1,
16368        );
16369        self.lm_head_forward(&self.ws.n1)
16370    }
16371}
16372
16373/// Convenience: deterministic tiny pipeline for tests.
16374pub fn create_test_pipeline(
16375    hidden_size: usize,
16376    intermediate_size: usize,
16377    num_heads: usize,
16378    num_kv_heads: usize,
16379    head_dim: usize,
16380    num_layers: usize,
16381    vocab_size: usize,
16382) -> Pipeline {
16383    // Small pseudo-random weights: constant weights make attention
16384    // degenerate and hide indexing bugs.
16385    let synth = |n: usize, salt: usize| -> Vec<f32> {
16386        (0..n)
16387            .map(|i| (((i * 31 + salt * 17 + 7) % 97) as f32 / 97.0 - 0.5) * 0.2)
16388            .collect()
16389    };
16390    let qt = |rows: usize, cols: usize, salt: usize| -> QTensor {
16391        QTensor::from_f32(synth(rows * cols, salt), rows, cols)
16392    };
16393    let layer_weights: Vec<LayerWeights> = (0..num_layers)
16394        .map(|li| LayerWeights {
16395            input_norm: vec![1.0; hidden_size],
16396            post_norm: vec![1.0; hidden_size],
16397            attn_out_norm: None,
16398            ffn_out_norm: None,
16399            layer_scale: None,
16400            ffn: FfnKind::Dense(DenseFfn {
16401                gate_proj: qt(intermediate_size, hidden_size, li * 10 + 5),
16402                up_proj: qt(intermediate_size, hidden_size, li * 10 + 6),
16403                down_proj: qt(hidden_size, intermediate_size, li * 10 + 7),
16404                act: Act::Silu,
16405                down_t: None,
16406                segs: Vec::new(),
16407            }),
16408            attn: AttnKind::Full {
16409                bias: None,
16410                wq: qt(num_heads * head_dim, hidden_size, li * 10 + 1),
16411                wk: qt(num_kv_heads * head_dim, hidden_size, li * 10 + 2),
16412                wv: qt(num_kv_heads * head_dim, hidden_size, li * 10 + 3),
16413                wo: qt(hidden_size, num_heads * head_dim, li * 10 + 4),
16414                q_norm: None,
16415                k_norm: None,
16416                output_gate: false,
16417                softplus_gate: None,
16418            },
16419        })
16420        .collect();
16421
16422    Pipeline::new(
16423        Tokenizer::byte_level(),
16424        PipelineWeights {
16425            embed_tokens: qt(vocab_size, hidden_size, 100),
16426            layers: layer_weights,
16427            lm_head: qt(vocab_size, hidden_size, 200),
16428            final_norm: vec![1.0; hidden_size],
16429        },
16430        hidden_size,
16431        intermediate_size,
16432        num_heads,
16433        num_kv_heads,
16434        head_dim,
16435        num_layers,
16436        num_layers, // physical_layers = num_layers (non-looped)
16437        false,      // loop_final_norm
16438        vocab_size,
16439        1e-6,
16440        10_000.0,
16441        NormStyle::Qwen,
16442        4096,
16443        SamplerConfig {
16444            seed: Some(42),
16445            ..Default::default()
16446        },
16447    )
16448}
16449
16450/// Batched dense-FFN: gate/up/down via matmat (element-wise the same
16451/// math as b × dense_ffn — the same dot kernels).
16452/// One mask bit, LSB-first per byte — `TaskMask::ffn_active_indices`'s
16453/// convention.
16454#[inline]
16455fn mask_bit(row: &[u8], j: usize) -> bool {
16456    (row.get(j >> 3).copied().unwrap_or(0) >> (j & 7)) & 1 != 0
16457}
16458
16459/// Zero the CLOSED neurons' activations in a [rows × inter] panel — the
16460/// masked-inference fast path's whole trick: full fused quant compute,
16461/// then the mask lands on the ACTIVATIONS, which is arithmetically the
16462/// pruned network without touching a quantized weight byte. Whole open
16463/// bytes (0xFF = 8 open neurons) skip in one test.
16464/// `CMF_FFN_MASK_GAIN` — Patent 12 FIG. 4, variance-preserving
16465/// rescaling: truncation removes a share of the layer's output energy,
16466/// so the survivors are scaled up to put the variance back where the
16467/// downstream norm expects it. A scalar here; per layer it is
16468/// `sqrt(total energy / kept energy)`.
16469fn mask_gain() -> f32 {
16470    static G: std::sync::OnceLock<f32> = std::sync::OnceLock::new();
16471    *G.get_or_init(|| {
16472        std::env::var("CMF_FFN_MASK_GAIN")
16473            .ok()
16474            .and_then(|v| v.parse().ok())
16475            .unwrap_or(1.0)
16476    })
16477}
16478
16479fn zero_masked_cols(g: &mut [f32], rows: usize, inter: usize, row: &[u8]) {
16480    // With CMF_FFN_MEANFILL a closed neuron contributes its average
16481    // instead of nothing — same bytes read, one constant restored.
16482    let fill = meanfill().and_then(|(i, v)| {
16483        let li = crate::gpu::cur_layer();
16484        (*i == inter && li >= 0).then(|| &v[li as usize * inter..(li as usize + 1) * inter])
16485    });
16486    for r in 0..rows {
16487        let base = r * inter;
16488        for (bi, &byte) in row.iter().enumerate() {
16489            if byte == 0xFF {
16490                continue;
16491            }
16492            let j0 = bi * 8;
16493            for bit in 0..8 {
16494                let j = j0 + bit;
16495                if j < inter && byte & (1 << bit) == 0 {
16496                    g[base + j] = fill.map_or(0.0, |f| f[j]);
16497                }
16498            }
16499        }
16500    }
16501    let gain = mask_gain();
16502    if gain != 1.0 {
16503        for v in g[..rows * inter].iter_mut() {
16504            *v *= gain;
16505        }
16506    }
16507}
16508
16509/// True when neuron `i`'s bit is set (no mask = everything runs).
16510#[inline]
16511fn tube_bit(row: Option<&[u8]>, i: usize) -> bool {
16512    row.is_none_or(|r| mask_bit(r, i))
16513}
16514
16515/// Every bit below `n` set — the common case for a tube file's CORE,
16516/// where only the tube bits vary per task.
16517fn all_bits_on(row: &[u8], n: usize) -> bool {
16518    (0..n).all(|i| mask_bit(row, i))
16519}
16520
16521/// `CMF_TUBE_TOPK` — how many tubes a TOKEN may open (0 = the task mask
16522/// decides alone). This is the dense FFN read as a mixture: the tubes
16523/// are the experts a k-means over `gate_proj` rows found, and the token
16524/// picks among them. `CMF_TUBE_SCORE=gate` scores a tube by its own
16525/// gate (realizable: only `up`/`down` of the losers go unread),
16526/// `=oracle` scores by the true `silu(gate)·up` mass (the ceiling —
16527/// only `down` is saved, and the selection has read what it predicts).
16528fn tube_topk() -> usize {
16529    static K: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
16530    *K.get_or_init(|| {
16531        std::env::var("CMF_TUBE_TOPK")
16532            .ok()
16533            .and_then(|v| v.parse().ok())
16534            .unwrap_or(0)
16535    })
16536}
16537
16538fn tube_score_oracle() -> bool {
16539    static O: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
16540    *O.get_or_init(|| std::env::var("CMF_TUBE_SCORE").is_ok_and(|v| v == "oracle"))
16541}
16542
16543/// The routed arm of `tube_ffn`: a token opens only its best `k` tubes.
16544/// At `b == 1` (decode) the losers are genuinely never read — that is
16545/// the speed. At `b > 1` (the scoring sweep) every tube is computed and
16546/// the losers' activations are zeroed instead: same arithmetic, so the
16547/// perplexity is the routed model's, measured without a per-token
16548/// gather in the middle of a GEMM.
16549fn tube_ffn_routed(
16550    d: &DenseFfn,
16551    xs: &[f32],
16552    b: usize,
16553    pool: Option<&Pool>,
16554    mask_row: Option<&[u8]>,
16555    k: usize,
16556) -> Vec<f32> {
16557    let hidden = d.down_proj.rows();
16558    let core = d.gate_proj.rows();
16559    let core_full = mask_row.is_none_or(|r| all_bits_on(r, core));
16560    let mut out = match (b, core_full, mask_row) {
16561        (1, true, _) => dense_ffn(d, xs, pool),
16562        (1, false, Some(row)) => dense_ffn_masked(d, xs, pool, row),
16563        (_, true, _) => dense_ffn_batch(d, xs, b, pool, None),
16564        (_, false, row) => dense_ffn_batch(d, xs, b, pool, row),
16565    };
16566    let cand: Vec<usize> = (0..d.segs.len())
16567        .filter(|&i| tube_bit(mask_row, d.segs[i].start))
16568        .collect();
16569    if cand.is_empty() {
16570        return out;
16571    }
16572    // gate (and, where the score or the batch needs it, up) per tube.
16573    // The SCORE is taken at the point the serving path could take it:
16574    // off the gate alone, or off the finished activation for the oracle.
16575    let oracle = tube_score_oracle();
16576    let mut acts: Vec<Vec<f32>> = Vec::with_capacity(cand.len());
16577    let mut scores = vec![0f32; b * cand.len()];
16578    for (ci, &i) in cand.iter().enumerate() {
16579        let seg = &d.segs[i];
16580        let w = seg.width;
16581        let mut g = vec![0.0f32; b * w];
16582        if b == 1 {
16583            seg.gate.matvec(xs, &mut g, pool);
16584        } else {
16585            seg.gate.matmat(xs, b, &mut g, pool);
16586        }
16587        for v in g.iter_mut() {
16588            *v = Act::Silu.combine(*v, 1.0);
16589        }
16590        if !oracle {
16591            for t in 0..b {
16592                scores[t * cand.len() + ci] =
16593                    g[t * w..(t + 1) * w].iter().map(|v| v * v).sum::<f32>();
16594            }
16595        }
16596        if oracle || b > 1 {
16597            let mut u = vec![0.0f32; b * w];
16598            if b == 1 {
16599                seg.up.matvec(xs, &mut u, pool);
16600            } else {
16601                seg.up.matmat(xs, b, &mut u, pool);
16602            }
16603            for (a, &v) in g.iter_mut().zip(u.iter()) {
16604                *a *= v;
16605            }
16606            if oracle {
16607                for t in 0..b {
16608                    scores[t * cand.len() + ci] =
16609                        g[t * w..(t + 1) * w].iter().map(|v| v * v).sum::<f32>();
16610                }
16611            }
16612        }
16613        acts.push(g);
16614    }
16615    // per-token scores and the winners
16616    let keep = k.min(cand.len());
16617    let mut scratch: Vec<f32> = Vec::new();
16618    for t in 0..b {
16619        let mut sc: Vec<(f32, usize)> = (0..cand.len())
16620            .map(|ci| (scores[t * cand.len() + ci], ci))
16621            .collect();
16622        sc.sort_unstable_by(|x, y| y.0.total_cmp(&x.0));
16623        let mut alive = vec![false; cand.len()];
16624        for &(_, ci) in sc.iter().take(keep) {
16625            alive[ci] = true;
16626        }
16627        if b > 1 {
16628            for (ci, a) in acts.iter_mut().enumerate() {
16629                if !alive[ci] {
16630                    let w = d.segs[cand[ci]].width;
16631                    a[t * w..(t + 1) * w].fill(0.0);
16632                }
16633            }
16634        } else {
16635            // decode: finish only the winners — the losers' up/down
16636            // (and, with the gate score, everything but their gate)
16637            // are never touched.
16638            for (ci, &i) in cand.iter().enumerate() {
16639                if !alive[ci] {
16640                    continue;
16641                }
16642                let seg = &d.segs[i];
16643                let w = seg.width;
16644                let g = &mut acts[ci];
16645                if !tube_score_oracle() {
16646                    scratch.clear();
16647                    scratch.resize(w, 0.0);
16648                    seg.up.matvec(xs, &mut scratch, pool);
16649                    for (a, &v) in g.iter_mut().zip(scratch.iter()) {
16650                        *a *= v;
16651                    }
16652                }
16653                let mut acc = vec![0.0f32; hidden];
16654                seg.down.matvec(g, &mut acc, pool);
16655                for (o, a) in out.iter_mut().zip(&acc) {
16656                    *o += *a;
16657                }
16658            }
16659        }
16660    }
16661    if b > 1 {
16662        for (ci, &i) in cand.iter().enumerate() {
16663            let seg = &d.segs[i];
16664            let mut acc = vec![0.0f32; b * hidden];
16665            seg.down.matmat(&acts[ci], b, &mut acc, pool);
16666            for (o, a) in out.iter_mut().zip(&acc) {
16667                *o += *a;
16668            }
16669        }
16670    }
16671    out
16672}
16673
16674/// FFN of a defragged tube layer: the always-on core plus the tubes the
16675/// task mask switches on. Each tube is a normal tensor triple, so the
16676/// same kernels run it and an inactive tube's bytes are never read —
16677/// that is the whole point of the defrag (a scattered mask cannot skip
16678/// bytes; a contiguous one is just a smaller matrix).
16679fn tube_ffn(
16680    d: &DenseFfn,
16681    xs: &[f32],
16682    b: usize,
16683    pool: Option<&Pool>,
16684    mask_row: Option<&[u8]>,
16685) -> Vec<f32> {
16686    if tube_topk() > 0 {
16687        return tube_ffn_routed(d, xs, b, pool, mask_row, tube_topk());
16688    }
16689    let hidden = d.down_proj.rows();
16690    let core = d.gate_proj.rows();
16691    let core_full = mask_row.is_none_or(|r| all_bits_on(r, core));
16692    let mut out = match (b, core_full, mask_row) {
16693        (1, true, _) => dense_ffn(d, xs, pool),
16694        (1, false, Some(row)) => dense_ffn_masked(d, xs, pool, row),
16695        (_, true, _) => dense_ffn_batch(d, xs, b, pool, None),
16696        (_, false, row) => dense_ffn_batch(d, xs, b, pool, row),
16697    };
16698    TUBE_SCRATCH.with(|sc| {
16699        let mut sc = sc.borrow_mut();
16700        let [g, u, acc] = &mut *sc;
16701        for seg in &d.segs {
16702            if !tube_bit(mask_row, seg.start) {
16703                continue;
16704            }
16705            let w = seg.width;
16706            g.resize(b * w, 0.0);
16707            if b == 1
16708                && d.act == Act::Silu
16709                && QTensor::matvec_silu_mul(&seg.gate, &seg.up, xs, g, pool)
16710            {
16711                // g holds silu(gate)·up.
16712            } else {
16713                u.resize(b * w, 0.0);
16714                if b == 1 {
16715                    QTensor::matvec_many([&seg.gate, &seg.up], xs, [g, u], pool);
16716                } else {
16717                    seg.gate.matmat(xs, b, g, pool);
16718                    seg.up.matmat(xs, b, u, pool);
16719                }
16720                for i in 0..b * w {
16721                    g[i] = d.act.combine(g[i], u[i]);
16722                }
16723            }
16724            acc.resize(b * hidden, 0.0);
16725            acc.fill(0.0);
16726            if b == 1 {
16727                seg.down.matvec(g, acc, pool);
16728            } else {
16729                seg.down.matmat(g, b, acc, pool);
16730            }
16731            for (o, a) in out.iter_mut().zip(acc.iter()) {
16732                *o += *a;
16733            }
16734        }
16735        out
16736    })
16737}
16738
16739thread_local! {
16740    /// gate / up / down-accumulator scratch for the tube loop — a tube
16741    /// runs once per layer per token, and a fresh Vec each time is a
16742    /// malloc per tube per layer per token.
16743    static TUBE_SCRATCH: std::cell::RefCell<[Vec<f32>; 3]> =
16744        const { std::cell::RefCell::new([Vec::new(), Vec::new(), Vec::new()]) };
16745}
16746
16747/// CMF_PREFILL_PROF: cumulative ns of the batched walk's attention and
16748/// FFN halves (all layers, all chunks).
16749static PREFILL_SPLIT: [std::sync::atomic::AtomicU64; 2] =
16750    [std::sync::atomic::AtomicU64::new(0), std::sync::atomic::AtomicU64::new(0)];
16751
16752/// The wgpu side of `CMF_PREFILL_PROF`: q|k|v GEMMs' enqueue and readback
16753/// wait, the chunk attend's readback wait (ms, cumulative) and the int8
16754/// GEMMs the `zi_mm` route took.
16755fn wgpu_prefill_counters() -> (f64, f64, f64, u64) {
16756    #[cfg(feature = "gpu")]
16757    {
16758        use std::sync::atomic::Ordering::Relaxed;
16759        let ms = |a: &std::sync::atomic::AtomicU64| a.load(Relaxed) as f64 / 1e6;
16760        (
16761            ms(&crate::gpu_wgpu::GEMM_MANY_NS[0]),
16762            ms(&crate::gpu_wgpu::GEMM_MANY_NS[1]),
16763            ms(&crate::gpu_wgpu::CHUNK_ATTEND_WAIT_NS),
16764            crate::gpu_wgpu::ZI_GEMM_CALLS.load(Relaxed),
16765        )
16766    }
16767    #[cfg(not(feature = "gpu"))]
16768    (0.0, 0.0, 0.0, 0)
16769}
16770
16771/// The wgpu prefill mirror attend's outcome counters
16772/// (`gpu_wgpu::MIRROR_EVENTS`).
16773fn wgpu_mirror_counters() -> [u64; 6] {
16774    #[cfg(feature = "gpu")]
16775    {
16776        crate::gpu_wgpu::MIRROR_EVENTS
16777            .each_ref()
16778            .map(|a| a.load(std::sync::atomic::Ordering::Relaxed))
16779    }
16780    #[cfg(not(feature = "gpu"))]
16781    [0; 6]
16782}
16783
16784fn prefill_prof_on() -> bool {
16785    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
16786    *ON.get_or_init(|| std::env::var_os("CMF_PREFILL_PROF").is_some())
16787}
16788
16789fn dense_ffn_batch(
16790    d: &DenseFfn,
16791    xs: &[f32],
16792    b: usize,
16793    pool: Option<&Pool>,
16794    mask_row: Option<&[u8]>,
16795) -> Vec<f32> {
16796    let inter = d.gate_proj.rows();
16797    let hidden = d.down_proj.rows();
16798    // Fused on-device SwiGLU when the device is in play: three separate
16799    // `matmat` calls are three round trips per layer, and the gate/up
16800    // panels (b × inter — 22 MB each at a 512-token chunk) cross the bus
16801    // twice for nothing. The kernel already existed for the image DiT;
16802    // the LLM prefill was simply never wired to it. A task mask needs the
16803    // activations on the host between the halves, so it keeps the CPU
16804    // arm below.
16805    // SiLU and the exact GELU (Spark-X2.5) have device arms; `q4_ffn_act`
16806    // hands SiLU to the very entry points this used to call.
16807    let fused_act = d.act.graph_act();
16808    if mask_row.is_none()
16809        && fused_act.is_some()
16810        && b >= 32
16811        && crate::gpu::enabled_here()
16812        && !crate::gpu::mm_killed()
16813        // The refit pass needs this layer's activations on the host; the
16814        // fused chain keeps them on the device. Refusing it here costs
16815        // one round trip and keeps every GEMM on the card — the
16816        // alternative was running the whole calibration on the CPU.
16817        && refit_dir().is_none()
16818        // Same for the mass/hit probes. The accumulator at the bottom of
16819        // this function only sees `g` when `g` came back to the host, so
16820        // a fused batch would leave it summing nothing — a probe that
16821        // reports zeros rather than failing, which is worse.
16822        && !ffn_probe_active()
16823    {
16824        if let (Some((model, w1)), Some((_, w3)), Some((_, w2))) = (
16825            d.gate_proj.mapped_q4t(),
16826            d.up_proj.mapped_q4t(),
16827            d.down_proj.mapped_q4t(),
16828        ) {
16829            let mut out = vec![0.0f32; b * hidden];
16830            let act = fused_act.expect("checked above");
16831            if crate::gpu::q4_ffn_act(model, w1, w3, w2, xs, b, hidden, inter, false, act, &mut out)
16832            {
16833                return out;
16834            }
16835        }
16836        // The q4tp twin (same kernel family, scale from the row ladder) —
16837        // the DiT has run it in production since the pipeline containers;
16838        // the LLM prefill was simply never wired to it, so a q4tp model's
16839        // prefill panels stayed on the CPU.
16840        if let (Some((model, w1)), Some((_, w3)), Some((_, w2))) = (
16841            d.gate_proj.mapped_q4tp(),
16842            d.up_proj.mapped_q4tp(),
16843            d.down_proj.mapped_q4tp(),
16844        ) {
16845            let mut out = vec![0.0f32; b * hidden];
16846            let act = fused_act.expect("checked above");
16847            if crate::gpu::q4_ffn_act(model, w1, w3, w2, xs, b, hidden, inter, true, act, &mut out)
16848            {
16849                return out;
16850            }
16851        }
16852        // Every other device codec — int8, or gate/up and down in different
16853        // codecs: the three GEMMs with the panels kept on the card. Without
16854        // it a q8_2f prefill read both b·inter panels home, folded them on
16855        // one host thread, and sent the result back for down.
16856        // CMF_FFN_KEEP=0 keeps the per-GEMM path (A/B).
16857        if let (true, Some((model, w1)), Some((_, w3)), Some((_, w2))) = (
16858            std::env::var("CMF_FFN_KEEP").as_deref() != Ok("0"),
16859            d.gate_proj.mapped_device_gemm(),
16860            d.up_proj.mapped_device_gemm(),
16861            d.down_proj.mapped_device_gemm(),
16862        ) {
16863            let mut out = vec![0.0f32; b * hidden];
16864            let act = fused_act.expect("checked above");
16865            if crate::gpu::ffn_act_keep(model, w1, w3, w2, xs, b, hidden, inter, act, &mut out) {
16866                return out;
16867            }
16868        }
16869    }
16870    let mut g = vec![0.0f32; b * inter];
16871    d.gate_proj.matmat(xs, b, &mut g, pool);
16872    let mut u = vec![0.0f32; b * inter];
16873    d.up_proj.matmat(xs, b, &mut u, pool);
16874    if gate_topk() > 0 && d.act == Act::Silu {
16875        for t in 0..b {
16876            let row = &mut g[t * inter..(t + 1) * inter];
16877            for v in row.iter_mut() {
16878                *v = Act::Silu.combine(*v, 1.0);
16879            }
16880            keep_top_k(row, gate_topk());
16881        }
16882        for i in 0..b * inter {
16883            g[i] *= u[i];
16884        }
16885    } else {
16886        for i in 0..b * inter {
16887            g[i] = d.act.combine(g[i], u[i]);
16888        }
16889    }
16890    if let Some(row) = mask_row {
16891        zero_masked_cols(&mut g, b, inter, row);
16892    }
16893    if oracle_topk() > 0 {
16894        for t in 0..b {
16895            keep_top_k(&mut g[t * inter..(t + 1) * inter], oracle_topk());
16896        }
16897    }
16898    let mut out = vec![0.0f32; b * hidden];
16899    d.down_proj.matmat(&g, b, &mut out, pool);
16900    if refit_dir().is_some() {
16901        let li = crate::gpu::cur_layer();
16902        if li >= 0 {
16903            refit_accumulate(li as usize, &g, b, inter, &out, hidden, pool);
16904        }
16905    }
16906    // The DTG-MA probe, on the batched path: one prefill sweep gives the
16907    // same per-neuron statistic the per-position probe does, and on a 27B
16908    // that is minutes instead of hours.
16909    FFN_PROBE.with(|pr| {
16910        if let Some(acc) = pr.borrow_mut().as_mut() {
16911            let li = crate::gpu::cur_layer();
16912            if li < 0 {
16913                return;
16914            }
16915            let Some(row) = acc.get_mut(li as usize) else {
16916                return;
16917            };
16918            let sq = probe_sq();
16919            for t in 0..b {
16920                for (a, &v) in row.iter_mut().zip(&g[t * inter..(t + 1) * inter]) {
16921                    *a += if sq {
16922                        (v as f64) * (v as f64)
16923                    } else {
16924                        (v as f64).abs()
16925                    };
16926                }
16927            }
16928        }
16929    });
16930    out
16931}
16932
16933/// Batched MoE-FFN: router batched, positions are GROUPED by expert —
16934/// an expert's weights are read once for all its positions in the chunk
16935/// (the main prefill-GEMM win on MoE: 960MB/token of 35B experts).
16936/// Accumulate per-channel activation energy for `CMF_RMS_TRACE`.
16937fn accumulate_act(m: &MoeFfn, xs: &[f32], b: usize) {
16938    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
16939    static DUMP: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
16940    let on = *ON.get_or_init(|| std::env::var("CMF_RMS_TRACE").is_ok());
16941    let dump = *DUMP.get_or_init(|| std::env::var("CMF_ACT_DUMP").is_ok());
16942    if (!on && !dump) || b == 0 {
16943        return;
16944    }
16945    let hidden = xs.len() / b;
16946    if on {
16947        let mut acc = m.act_sq.borrow_mut();
16948        if acc.len() < hidden {
16949            acc.resize(hidden, 0.0);
16950        }
16951        for t in 0..b {
16952            let row = &xs[t * hidden..(t + 1) * hidden];
16953            for (a, &v) in acc.iter_mut().zip(row) {
16954                *a += (v as f64) * (v as f64);
16955            }
16956        }
16957    }
16958    if dump {
16959        // Cap the capture: the covariance needs a few thousand rows, and a
16960        // whole prefill of every layer would be gigabytes for no extra rank.
16961        let cap: usize = std::env::var("CMF_ACT_DUMP_ROWS")
16962            .ok()
16963            .and_then(|v| v.parse().ok())
16964            .unwrap_or(4096);
16965        let mut rows = m.act_rows.borrow_mut();
16966        if rows.len() < cap * hidden {
16967            let take = b.min((cap * hidden - rows.len()) / hidden.max(1));
16968            rows.extend_from_slice(&xs[..take * hidden]);
16969        }
16970    }
16971}
16972
16973/// Send-able cursor over a Vec-of-Vecs: each pool worker writes only its
16974/// own slots (disjoint by construction in the caller).
16975#[derive(Clone, Copy)]
16976struct SendVecs(*mut Vec<f32>);
16977unsafe impl Send for SendVecs {}
16978unsafe impl Sync for SendVecs {}
16979impl SendVecs {
16980    #[inline]
16981    fn at(self, i: usize) -> *mut Vec<f32> {
16982        unsafe { self.0.add(i) }
16983    }
16984}
16985
16986fn moe_ffn_batch(
16987    m: &MoeFfn,
16988    xs: &[f32],
16989    b: usize,
16990    hidden: usize,
16991    pool: Option<&Pool>,
16992    allowed: Option<&[bool]>,
16993) -> Vec<f32> {
16994    accumulate_act(m, xs, b);
16995    let ne = m.experts.len();
16996    let mut logits = vec![0.0f32; b * ne];
16997    match &m.resonance {
16998        Some(r) => {
16999            let hdim = xs.len() / b.max(1);
17000            for bi in 0..b {
17001                r.scores(
17002                    &xs[bi * hdim..(bi + 1) * hdim],
17003                    &mut logits[bi * ne..(bi + 1) * ne],
17004                );
17005            }
17006        }
17007        None => m.router.matmat(xs, b, &mut logits, pool),
17008    }
17009
17010    // Assignments: expert → [(position, weight)] — same routing as
17011    // moe_ffn, per position (see `moe_route`).
17012    let mut assign: Vec<Vec<(usize, f32)>> = vec![Vec::new(); ne];
17013    {
17014        let mut st = m.stats.borrow_mut();
17015        if st.len() < ne {
17016            st.resize(ne, 0);
17017        }
17018        for bi in 0..b {
17019            let (idx, p, wsum) = moe_route(&logits[bi * ne..(bi + 1) * ne], m, allowed);
17020            for &e in &idx {
17021                st[e] += 1;
17022                assign[e].push((bi, p[e] / wsum));
17023            }
17024        }
17025    }
17026
17027    let mut out = vec![0.0f32; b * hidden];
17028    let cols = m.experts[0].gate_proj.cols();
17029    let run_expert = |d: &DenseFfn, list: &[(usize, f32)], out: &mut [f32]| {
17030        let sb = list.len();
17031        let mut sub = vec![0.0f32; sb * cols];
17032        for (k, &(bi, _)) in list.iter().enumerate() {
17033            sub[k * cols..(k + 1) * cols].copy_from_slice(&xs[bi * cols..(bi + 1) * cols]);
17034        }
17035        let eo = dense_ffn_batch(d, &sub, sb, pool, None);
17036        for (k, &(bi, w)) in list.iter().enumerate() {
17037            for i in 0..hidden {
17038                out[bi * hidden + i] += w * eo[k * hidden + i];
17039            }
17040        }
17041    };
17042    // Routed experts: the panels are TINY (b·top_k spread over every
17043    // expert — a few positions each), so a pool dispatch per expert is
17044    // pure barrier cost. Invert the parallelism: workers take WHOLE
17045    // experts (serial math inside), then one deterministic scatter in
17046    // expert order — the exact accumulation order the serial loop had.
17047    let active: Vec<usize> = (0..ne).filter(|&e| !assign[e].is_empty()).collect();
17048    if pool.is_some() && active.len() >= 8 {
17049        let mut panels: Vec<Vec<f32>> = vec![Vec::new(); active.len()];
17050        {
17051            let panel_ptr = SendVecs(panels.as_mut_ptr());
17052            // Capture only the expert table: `m` itself carries RefCell
17053            // stats and must not cross the pool boundary.
17054            let experts = &m.experts;
17055            let (active_r, assign_r) = (&active, &assign);
17056            let inherit_cpu = crate::gpu::inherit_cpu_scope();
17057            let run = |start: usize, end: usize| {
17058                let _cpu_scope = inherit_cpu();
17059                // Native Metal: the per-op device helpers (`q4tp_ffn`,
17060                // `q8_matmat`, …) stage through size-keyed shared io
17061                // buffers and are not re-entrant — two workers whose experts
17062                // drew the same row count wrote one buffer at once, so a
17063                // Mellum2.1 prompt came out different on every run. The
17064                // workers keep whole experts on the CPU there; the device
17065                // prefill is the MoE chunk graph.
17066                #[cfg(target_os = "macos")]
17067                let _metal_cpu = crate::gpu::enter_cpu_scope();
17068                for ai in start..end {
17069                    let e = active_r[ai];
17070                    let list = &assign_r[e];
17071                    let sb = list.len();
17072                    let mut sub = vec![0.0f32; sb * cols];
17073                    for (k, &(bi, _)) in list.iter().enumerate() {
17074                        sub[k * cols..(k + 1) * cols]
17075                            .copy_from_slice(&xs[bi * cols..(bi + 1) * cols]);
17076                    }
17077                    // SAFETY: each worker owns a disjoint panels[ai].
17078                    unsafe {
17079                        *panel_ptr.at(ai) = dense_ffn_batch(&experts[e], &sub, sb, None, None);
17080                    }
17081                }
17082            };
17083            match pool {
17084                Some(p) => p.run_rows(active.len(), &run),
17085                None => run(0, active.len()),
17086            }
17087        }
17088        for (ai, &e) in active.iter().enumerate() {
17089            for (k, &(bi, w)) in assign[e].iter().enumerate() {
17090                let eo = &panels[ai][k * hidden..(k + 1) * hidden];
17091                for i in 0..hidden {
17092                    out[bi * hidden + i] += w * eo[i];
17093                }
17094            }
17095        }
17096    } else {
17097        for &e in &active {
17098            run_expert(&m.experts[e], &assign[e], &mut out);
17099        }
17100    }
17101    if let Some((se, gate)) = &m.shared {
17102        let all: Vec<(usize, f32)> = if let Some(gate) = gate {
17103            let mut gl = vec![0.0f32; b];
17104            gate.matmat(xs, b, &mut gl, pool);
17105            (0..b)
17106                .map(|bi| (bi, 1.0 / (1.0 + (-gl[bi]).exp())))
17107                .collect()
17108        } else {
17109            (0..b).map(|bi| (bi, 1.0)).collect()
17110        };
17111        run_expert(se, &all, &mut out);
17112    }
17113    out
17114}
17115
17116/// Decode-exact multi-token MoE — the MiMo speculative verify's FFN. Row
17117/// `r` of the result is bit-identical to `moe_ffn(m, x_r)` on the CPU
17118/// (`moe_ffn_cpu` → `moe_ffn_cpu_batched`): router matvec per row, the same
17119/// routing, the same int8 gate/up/SiLU and down terms
17120/// (`QTensor::moe_gate_up_rows` / `moe_down_rows`), and the row's experts
17121/// summed in ITS route order from 0. What the rows share is the weight
17122/// traffic: each routed expert is read once for every row that picked it.
17123/// (`moe_ffn_batch`, the prompt path, groups the same way but sums in
17124/// expert-index order and runs blocked kernels on wide groups — close, not
17125/// bit-equal to decode.) Any layer the kernels do not cover, or a device
17126/// that could answer `moe_ffn` itself, walks `moe_ffn` row by row.
17127fn moe_ffn_rows_exact(
17128    m: &MoeFfn,
17129    xs: &[f32],
17130    b: usize,
17131    hidden: usize,
17132    pool: Option<&Pool>,
17133) -> Vec<f32> {
17134    let mut out = vec![0.0f32; b * hidden];
17135    let per_row = |out: &mut [f32]| {
17136        for r in 0..b {
17137            let o = moe_ffn(m, &xs[r * hidden..(r + 1) * hidden], pool, None);
17138            out[r * hidden..(r + 1) * hidden].copy_from_slice(&o);
17139        }
17140    };
17141    let covered = !crate::gpu::enabled_here()
17142        && moe_batch_enabled()
17143        && m.shared.is_none()
17144        && m.resonance.is_none()
17145        && FFN_PROBE.with(|pr| pr.borrow().is_none())
17146        && m.experts.iter().all(|d| d.act == Act::Silu);
17147    if !covered {
17148        per_row(&mut out);
17149        return out;
17150    }
17151    let ne = m.experts.len();
17152    // Routing, row by row, exactly as `moe_ffn`.
17153    let mut routes: Vec<(Vec<usize>, Vec<f32>)> = Vec::with_capacity(b);
17154    for r in 0..b {
17155        let x = &xs[r * hidden..(r + 1) * hidden];
17156        accumulate_act(m, x, 1);
17157        let mut logits = vec![0.0f32; ne];
17158        m.router.matvec(x, &mut logits, pool);
17159        let (idx, p, wsum) = moe_route(&logits, m, None);
17160        {
17161            let mut st = m.stats.borrow_mut();
17162            if st.len() < ne {
17163                st.resize(ne, 0);
17164            }
17165            for &e in &idx {
17166                st[e] += 1;
17167            }
17168        }
17169        let w: Vec<f32> = idx
17170            .iter()
17171            .map(|&e| p[e] / wsum * m.per_expert_scale.as_ref().map_or(1.0, |v| v[e]))
17172            .collect();
17173        routes.push((idx, w));
17174    }
17175    if routes.iter().any(|(idx, _)| idx.is_empty()) {
17176        per_row(&mut out);
17177        return out;
17178    }
17179    // Group the (row, expert) picks by expert, in first-seen order.
17180    let mut experts: Vec<usize> = Vec::new();
17181    let mut groups: Vec<Vec<usize>> = Vec::new();
17182    for (r, (idx, _)) in routes.iter().enumerate() {
17183        for &e in idx {
17184            match experts.iter().position(|&x| x == e) {
17185                Some(g) => groups[g].push(r),
17186                None => {
17187                    experts.push(e);
17188                    groups.push(vec![r]);
17189                }
17190            }
17191        }
17192    }
17193    let n_pairs: usize = groups.iter().map(|g| g.len()).sum();
17194    let inter = m.experts[experts[0]].gate_proj.rows();
17195    let pairs: Vec<(&QTensor, &QTensor)> = experts
17196        .iter()
17197        .map(|&e| (&m.experts[e].gate_proj, &m.experts[e].up_proj))
17198        .collect();
17199    let mut gs: Vec<Vec<f32>> = (0..n_pairs).map(|_| vec![0f32; inter]).collect();
17200    if !QTensor::moe_gate_up_rows(&pairs, &groups, xs, &mut gs, pool) {
17201        per_row(&mut out);
17202        return out;
17203    }
17204    let downs: Vec<&QTensor> = experts.iter().map(|&e| &m.experts[e].down_proj).collect();
17205    let lens: Vec<usize> = groups.iter().map(|g| g.len()).collect();
17206    let mut ds: Vec<Vec<f32>> = (0..n_pairs).map(|_| vec![0f32; hidden]).collect();
17207    if !QTensor::moe_down_rows(&downs, &lens, &gs, &mut ds, pool) {
17208        per_row(&mut out);
17209        return out;
17210    }
17211    // Where each (row, expert) term landed in the flat pair list.
17212    let mut slot = std::collections::HashMap::with_capacity(n_pairs);
17213    let mut p = 0usize;
17214    for (g, &e) in experts.iter().enumerate() {
17215        for &r in &groups[g] {
17216            slot.insert((r, e), p);
17217            p += 1;
17218        }
17219    }
17220    for (r, (idx, w)) in routes.iter().enumerate() {
17221        let terms: Vec<(&[f32], f32)> = idx
17222            .iter()
17223            .zip(w)
17224            .map(|(&e, &we)| (ds[slot[&(r, e)]].as_slice(), we))
17225            .collect();
17226        let row = &mut out[r * hidden..(r + 1) * hidden];
17227        for (i, dst) in row.iter_mut().enumerate() {
17228            // `moe_down_many`'s per-row sum: from 0, in route order.
17229            let mut acc = 0f32;
17230            for (d, we) in &terms {
17231                acc += we * d[i];
17232            }
17233            *dst = acc;
17234        }
17235    }
17236    out
17237}
17238
17239/// Gemma's final-logit soft-capping, `c·tanh(x/c)`, over a whole vocab row —
17240/// element for element the serial expression, split across the pool: at a
17241/// 262k vocab the serial `tanh` pass was ~5 ms of a 36 ms decode token.
17242pub(crate) fn softcap_in_place(v: &mut [f32], c: f32, pool: Option<&Pool>) {
17243    match pool {
17244        Some(p) if v.len() >= 1 << 15 => {
17245            let base = v.as_mut_ptr() as usize;
17246            p.run_rows(v.len(), &|s, e| {
17247                // SAFETY: run_rows hands out disjoint [s, e) ranges of `v`,
17248                // which outlives the (blocking) dispatch.
17249                let sl = unsafe { std::slice::from_raw_parts_mut((base as *mut f32).add(s), e - s) };
17250                for x in sl {
17251                    *x = c * (*x / c).tanh();
17252                }
17253            });
17254        }
17255        _ => {
17256            for x in v.iter_mut() {
17257                *x = c * (*x / c).tanh();
17258            }
17259        }
17260    }
17261}
17262
17263thread_local! {
17264    /// gate/up activation scratch for the dense FFN paths (single uses
17265    /// two slots, the fused pair all four) — these were fresh
17266    /// intermediate-size Vecs on every layer of every token.
17267    static FFN_SCRATCH: std::cell::RefCell<[Vec<f32>; 4]> =
17268        const { std::cell::RefCell::new([Vec::new(), Vec::new(), Vec::new(), Vec::new()]) };
17269}
17270
17271/// Dense SwiGLU FFN through QTensor matvecs (any storage).
17272fn dense_ffn(d: &DenseFfn, x: &[f32], pool: Option<&Pool>) -> Vec<f32> {
17273    // Per-token sparsity, when the file was built for it: gate first,
17274    // then only the chosen neurons' up/down rows leave the mmap.
17275    if gate_topk() > 0
17276        && let Some(out) = dense_ffn_dynamic(d, x, pool, gate_topk())
17277    {
17278        return out;
17279    }
17280    // Whole-FFN GPU submit (этап 4.2 increment): gate → silu·up → down
17281    // chained in ONE command buffer with the intermediate activations
17282    // resident on the device — 3 per-op polls become 1 per layer. The
17283    // moe_block backend already implements exactly this chain; a dense
17284    // FFN is one expert with weight 1. Runtime probe: the chain still
17285    // pays one submit+poll per layer — alternate it against the pure-CPU
17286    // FFN and keep whichever is faster on this machine.
17287    // q1 FFNs offload at any practical size: the q1 CPU kernel is
17288    // compute-bound, so the UMA threshold logic does not apply — the
17289    // probe measures and decides either way.
17290    // The fused GPU block has no descriptor-aware Prism path: it would either
17291    // consume an unrotated activation or decline after inspecting the mixed
17292    // q2tp/q4tp tensors.  Do not let that structural refusal enter the FFN
17293    // probe's CPU_ONLY scope; the ordinary body below dispatches each matrix
17294    // through QTensor::matvec, which owns the signed FWHT + affine q2tp route.
17295    let prism_body = d.gate_proj.has_prism_contract()
17296        || d.up_proj.has_prism_contract()
17297        || d.down_proj.has_prism_contract();
17298    if !prism_body
17299        && crate::gpu::enabled_here()
17300        && (d.gate_proj.rows() >= crate::gpu::min_rows() || d.gate_proj.is_q1())
17301    {
17302        let arm = if d.gate_proj.is_q1() && crate::gpu::q1_force() {
17303            crate::gpu::ProbeArm::Gpu
17304        } else {
17305            crate::gpu::probe_arm(crate::gpu::OpClass::Ffn)
17306        };
17307        match arm {
17308            crate::gpu::ProbeArm::Gpu => {
17309                let t0 = std::time::Instant::now();
17310                if let Some(out) = dense_ffn_gpu(d, x, pool) {
17311                    crate::gpu::probe_record(crate::gpu::OpClass::Ffn, true, t0.elapsed());
17312                    return out;
17313                }
17314                // Declined: no timing exists, so say so. Silence here is
17315                // what left `ffn` undecided for 9000 calls and cost a
17316                // failed device attempt on half of them.
17317                crate::gpu::probe_note_decline(crate::gpu::OpClass::Ffn);
17318            }
17319            crate::gpu::ProbeArm::CpuTimed => {
17320                let t0 = std::time::Instant::now();
17321                let out = crate::gpu::cpu_scope(|| dense_ffn_cpu(d, x, pool));
17322                crate::gpu::probe_record(crate::gpu::OpClass::Ffn, false, t0.elapsed());
17323                return out;
17324            }
17325            crate::gpu::ProbeArm::Cpu => {
17326                return crate::gpu::cpu_scope(|| dense_ffn_cpu(d, x, pool));
17327            }
17328        }
17329    }
17330    dense_ffn_cpu(d, x, pool)
17331}
17332
17333/// The pure-CPU dense-FFN body (also the fallback of every GPU refusal).
17334fn dense_ffn_cpu(d: &DenseFfn, x: &[f32], pool: Option<&Pool>) -> Vec<f32> {
17335    let inter = d.gate_proj.rows();
17336    FFN_SCRATCH.with(|s| {
17337        let mut s = s.borrow_mut();
17338        let [g, u, ..] = &mut *s;
17339        g.resize(inter, 0.0);
17340        // Fused gate+up+silu: one dispatch, no separate silu pass.
17341        // Falls back to matvec_many + silu loop for unsupported dtypes.
17342        if gate_topk() > 0 {
17343            // Gate first, select, and only then pay for `up`: the
17344            // measurement arm computes both and zeroes the losers, which
17345            // is the same arithmetic.
17346            u.resize(inter, 0.0);
17347            QTensor::matvec_many([&d.gate_proj, &d.up_proj], x, [g, u], pool);
17348            for i in 0..inter {
17349                g[i] = Act::Silu.combine(g[i], 1.0);
17350            }
17351            keep_top_k(g, gate_topk());
17352            for i in 0..inter {
17353                g[i] *= u[i];
17354            }
17355        } else if d.act == Act::Silu && {
17356            let _prof = crate::cpuprof::time(crate::cpuprof::Slot::FfnGateUp);
17357            QTensor::matvec_silu_mul(&d.gate_proj, &d.up_proj, x, g, pool)
17358        } {
17359            // g now holds silu(gate)·up directly.
17360        } else {
17361            u.resize(inter, 0.0);
17362            // Multi-matrix job: gate+up under one pool dispatch.
17363            let _prof = crate::cpuprof::time(crate::cpuprof::Slot::FfnGateUp);
17364            QTensor::matvec_many([&d.gate_proj, &d.up_proj], x, [g, u], pool);
17365            for i in 0..inter {
17366                g[i] = d.act.combine(g[i], u[i]);
17367            }
17368        }
17369        // DTG-MA bake probe (Patent 2): accumulate this layer's
17370        // per-neuron activation mass while a probe pass is active.
17371        // `CMF_FFN_PROBE_TOPK=k` switches the statistic from mass to a
17372        // HIT COUNT — how many tokens rank the neuron in their own top
17373        // k. Mass asks "how loud is this neuron overall", the count
17374        // asks "how often does this task actually need it", and the two
17375        // rank neurons differently whenever a few tokens are loud.
17376        FFN_PROBE.with(|pr| {
17377            if let Some(acc) = pr.borrow_mut().as_mut() {
17378                let li = crate::gpu::cur_layer();
17379                if li >= 0 {
17380                    if let Some(row) = acc.get_mut(li as usize) {
17381                        match probe_topk() {
17382                            0 if probe_sq() => {
17383                                for (a, &v) in row.iter_mut().zip(g.iter()) {
17384                                    *a += (v as f64) * (v as f64);
17385                                }
17386                            }
17387                            0 if probe_signed() => {
17388                                for (a, &v) in row.iter_mut().zip(g.iter()) {
17389                                    *a += v as f64;
17390                                }
17391                            }
17392                            0 => {
17393                                for (a, &v) in row.iter_mut().zip(g.iter()) {
17394                                    *a += (v as f64).abs();
17395                                }
17396                            }
17397                            k => {
17398                                let n = g.len();
17399                                let k = k.min(n);
17400                                let mut mag: Vec<f32> = g.iter().map(|v| v.abs()).collect();
17401                                let (_, kth, _) = mag.select_nth_unstable_by(k - 1, |a, b| {
17402                                    b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal)
17403                                });
17404                                let thr = *kth;
17405                                for (a, &v) in row.iter_mut().zip(g.iter()) {
17406                                    if v.abs() >= thr {
17407                                        *a += 1.0;
17408                                    }
17409                                }
17410                            }
17411                        }
17412                    }
17413                }
17414            }
17415        });
17416        if oracle_topk() > 0 {
17417            keep_top_k(g, oracle_topk());
17418        }
17419        {
17420            let li = crate::gpu::cur_layer();
17421            if li >= 0 {
17422                adump_row(li as usize, g);
17423            }
17424        }
17425        let mut out = attention::take_buf(d.down_proj.rows());
17426        let _prof = crate::cpuprof::time(crate::cpuprof::Slot::FfnDown);
17427        d.down_proj.matvec(g, &mut out, pool);
17428        out
17429    })
17430}
17431
17432/// Online accumulators for the AWNP refit of a narrowed FFN.
17433///
17434/// The refit needs `Gss = A_SᵀA_S` and `YA = YᵀA_S` per layer, where `A_S`
17435/// are the calibration activations of the KEPT neurons and `Y` the full
17436/// FFN output. Both are small enough to hold; the thing that is not is
17437/// the activations they are built from — a 27B layer would dump a
17438/// gigabyte per thousand tokens. So they are accumulated as the
17439/// calibration runs and written once at the end.
17440///
17441/// `CMF_FFN_REFIT=<dir>` holds `support.<L>.u32` (a u32 count then the
17442/// kept indices) for every layer to accumulate; `CMF_FFN_REFIT_FROM/TO`
17443/// bound the layer span so the accumulators fit in RAM.
17444pub struct RefitAcc {
17445    pub support: Vec<u32>,
17446    pub gss: Vec<f32>,
17447    pub ya: Vec<f32>,
17448    pub hidden: usize,
17449    pub tokens: u64,
17450    /// Activations staged transposed ([ns, t] and [hidden, t]) until the
17451    /// batch is worth a GEMM. The product costs `ns²` to move and add
17452    /// REGARDLESS of how many tokens went into it, so folding 16 chunks
17453    /// into one call cuts that cost 16× — it was 15 TB of traffic per
17454    /// calibration pass at one call per 256 tokens.
17455    pub buf_g: Vec<f32>,
17456    pub buf_o: Vec<f32>,
17457    pub buf_t: usize,
17458}
17459
17460/// The product buffer is SHARED across layers — one 473 MB allocation,
17461/// not one per layer (that was 30 GB of nothing on a 64-layer model).
17462/// It lives under the same lock as the accumulators.
17463type RefitState = (std::collections::HashMap<usize, RefitAcc>, Vec<f32>);
17464
17465static REFIT: std::sync::OnceLock<Option<(String, std::sync::Mutex<RefitState>)>> =
17466    std::sync::OnceLock::new();
17467
17468/// Is an FFN probe accumulator installed on this thread? The fused GPU
17469/// FFN must decline while one is, or the probe silently measures zero.
17470fn ffn_probe_active() -> bool {
17471    FFN_PROBE.with(|p| p.borrow().is_some())
17472}
17473
17474fn refit_dir() -> Option<&'static (String, std::sync::Mutex<RefitState>)> {
17475    REFIT
17476        .get_or_init(|| {
17477            std::env::var("CMF_FFN_REFIT").ok().map(|d| {
17478                (
17479                    d,
17480                    std::sync::Mutex::new((std::collections::HashMap::new(), Vec::new())),
17481                )
17482            })
17483        })
17484        .as_ref()
17485}
17486
17487/// Accumulate one prefill panel into the layer's refit statistics.
17488fn refit_accumulate(
17489    li: usize,
17490    g: &[f32],
17491    b: usize,
17492    inter: usize,
17493    out: &[f32],
17494    hidden: usize,
17495    pool: Option<&Pool>,
17496) {
17497    let Some((dir, map)) = refit_dir() else {
17498        return;
17499    };
17500    static SPAN: std::sync::OnceLock<(usize, usize)> = std::sync::OnceLock::new();
17501    let (from, to) = *SPAN.get_or_init(|| {
17502        let g = |k: &str, d: usize| {
17503            std::env::var(k)
17504                .ok()
17505                .and_then(|v| v.parse().ok())
17506                .unwrap_or(d)
17507        };
17508        (
17509            g("CMF_FFN_REFIT_FROM", 0),
17510            g("CMF_FFN_REFIT_TO", usize::MAX),
17511        )
17512    });
17513    if li < from || li > to {
17514        return;
17515    }
17516    let mut guard = map.lock().unwrap();
17517    let (map, shared) = &mut *guard;
17518    let acc = match map.entry(li) {
17519        std::collections::hash_map::Entry::Occupied(e) => e.into_mut(),
17520        std::collections::hash_map::Entry::Vacant(e) => {
17521            let path = format!("{dir}/support.{li}.u32");
17522            let Ok(bytes) = std::fs::read(&path) else {
17523                eprintln!("refit: no {path} — layer {li} skipped");
17524                return;
17525            };
17526            let n = u32::from_le_bytes(bytes[0..4].try_into().unwrap()) as usize;
17527            let support: Vec<u32> = bytes[4..4 + n * 4]
17528                .chunks_exact(4)
17529                .map(|c| u32::from_le_bytes([c[0], c[1], c[2], c[3]]))
17530                .collect();
17531            eprintln!(
17532                "refit: layer {li} support {n} ({:.0} MB of accumulator)",
17533                (n * n + hidden * n) as f64 * 4.0 / 1e6
17534            );
17535            e.insert(RefitAcc {
17536                gss: vec![0.0; n * n],
17537                ya: vec![0.0; hidden * n],
17538                buf_g: Vec::new(),
17539                buf_o: Vec::new(),
17540                buf_t: 0,
17541                support,
17542                hidden,
17543                tokens: 0,
17544            })
17545        }
17546    };
17547    let ns = acc.support.len();
17548    // Stage this chunk transposed; the GEMM fires once the batch is full.
17549    let cap = refit_batch();
17550    if acc.buf_g.is_empty() {
17551        acc.buf_g = vec![0.0; ns * cap];
17552        acc.buf_o = vec![0.0; hidden * cap];
17553    }
17554    let take = b.min(cap - acc.buf_t);
17555    for t in 0..take {
17556        let col = acc.buf_t + t;
17557        for (j, &n) in acc.support.iter().enumerate() {
17558            acc.buf_g[j * cap + col] = g[t * inter + n as usize];
17559        }
17560        for h in 0..hidden {
17561            acc.buf_o[h * cap + col] = out[t * hidden + h];
17562        }
17563    }
17564    acc.buf_t += take;
17565    acc.tokens += take as u64;
17566    if acc.buf_t < cap {
17567        return;
17568    }
17569    let bt = acc.buf_t;
17570    acc.buf_t = 0;
17571    // The GEMM WRITES its C (it zeroes the accumulators it uses), so the
17572    // chunk product lands in scratch and is added on — the one thing that
17573    // silently turns a Gram over 13 000 tokens into a Gram over 256.
17574    // Both products are `C[n, m] += X[n, b] · Yᵀ[b, m]` with X and Y
17575    // stored row-major [·, b] — exactly `gemm_nt_f32`'s shape, so the
17576    // card does them when it is up (this is the whole calibration's
17577    // cost: O(|S|²) per token, 2.9 PFLOP for a 27B pass). The tiled CPU
17578    // loop stays as the fallback. Neither accumulates, so the product
17579    // lands in scratch and is added on.
17580    let RefitAcc {
17581        gss,
17582        ya,
17583        buf_g,
17584        buf_o,
17585        ..
17586    } = acc;
17587    let need = (ns * ns).max(hidden * ns);
17588    if shared.len() < need {
17589        shared.resize(need, 0.0);
17590    }
17591    let scratch = &mut shared[..];
17592    let _ = bt;
17593    if crate::gpu::gemm_nt_f32_transient(buf_g, buf_g, &mut scratch[..ns * ns], ns, cap, ns) {
17594        add_into(gss, &scratch[..ns * ns], pool);
17595        if crate::gpu::gemm_nt_f32_transient(
17596            buf_o,
17597            buf_g,
17598            &mut scratch[..hidden * ns],
17599            hidden,
17600            cap,
17601            ns,
17602        ) {
17603            add_into(ya, &scratch[..hidden * ns], pool);
17604        } else {
17605            accum_outer_t(ya, hidden, ns, cap, buf_o, buf_g, pool);
17606        }
17607    } else {
17608        accum_outer_t(gss, ns, ns, cap, buf_g, buf_g, pool);
17609        accum_outer_t(ya, hidden, ns, cap, buf_o, buf_g, pool);
17610    }
17611    // No zeroing: the batch is always filled exactly (cap is a multiple
17612    // of the prefill chunk), and a memset of 178 MB a layer would cost
17613    // more than the GEMM.
17614}
17615
17616/// `CMF_FFN_REFIT_BATCH` — tokens staged before each GEMM (default 4096).
17617fn refit_batch() -> usize {
17618    static B: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
17619    *B.get_or_init(|| {
17620        std::env::var("CMF_FFN_REFIT_BATCH")
17621            .ok()
17622            .and_then(|v| v.parse().ok())
17623            .unwrap_or(4096)
17624    })
17625}
17626
17627/// `c[m, n] += Σ_t left[m, t]·right[n, t]` — both operands transposed,
17628/// the CPU fallback for the staged batch.
17629fn accum_outer_t(
17630    c: &mut [f32],
17631    m: usize,
17632    n: usize,
17633    b: usize,
17634    left: &[f32],
17635    right: &[f32],
17636    pool: Option<&Pool>,
17637) {
17638    let ptr = SendMut(c.as_mut_ptr());
17639    let body = |i: usize| {
17640        let ptr = &ptr;
17641        let row = unsafe { std::slice::from_raw_parts_mut(ptr.0.add(i * n), n) };
17642        for t in 0..b {
17643            let a = left[i * b + t];
17644            if a == 0.0 {
17645                continue;
17646            }
17647            for (j, o) in row.iter_mut().enumerate() {
17648                *o += a * right[j * b + t];
17649            }
17650        }
17651    };
17652    match pool {
17653        Some(p) if m > 1 => p.run_rows(m, &|s, e| {
17654            for i in s..e {
17655                body(i);
17656            }
17657        }),
17658        _ => {
17659            for i in 0..m {
17660                body(i);
17661            }
17662        }
17663    }
17664}
17665
17666/// `dst += src`, spread over the pool — at 118 M floats a layer this is
17667/// not a loop to leave on one core.
17668fn add_into(dst: &mut [f32], src: &[f32], pool: Option<&Pool>) {
17669    let n = dst.len().min(src.len());
17670    match pool {
17671        Some(p) if n >= 1 << 16 => {
17672            let ptr = SendMut(dst.as_mut_ptr());
17673            let f = |s: usize, e: usize| {
17674                let ptr = &ptr;
17675                for blk in s..e {
17676                    let (a, b) = (blk * 4096, ((blk + 1) * 4096).min(n));
17677                    for i in a..b {
17678                        unsafe { *ptr.0.add(i) += src[i] };
17679                    }
17680                }
17681            };
17682            p.run_rows(n.div_ceil(4096), &f);
17683        }
17684        _ => {
17685            for (d, v) in dst.iter_mut().zip(&src[..n]) {
17686                *d += *v;
17687            }
17688        }
17689    }
17690}
17691
17692/// `c[m, n] += Σ_t left[t, m]·right[t, n]`, with `left` stored [m, t] and
17693/// `right` [t, n]. Tiled over the rows of `c` so a tile stays in cache
17694/// while each token's `right` row streams past it once, and parallel
17695/// over tiles.
17696fn accum_outer(
17697    c: &mut [f32],
17698    m: usize,
17699    n: usize,
17700    b: usize,
17701    left: &[f32],
17702    right: &[f32],
17703    pool: Option<&Pool>,
17704) {
17705    const TILE: usize = 32;
17706    let tiles = m.div_ceil(TILE);
17707    let cp = SendMut(c.as_mut_ptr());
17708    let body = |ti: usize| {
17709        let cp = &cp;
17710        let i0 = ti * TILE;
17711        let i1 = (i0 + TILE).min(m);
17712        for t in 0..b {
17713            let r = &right[t * n..t * n + n];
17714            for i in i0..i1 {
17715                let a = left[i * b + t];
17716                if a == 0.0 {
17717                    continue;
17718                }
17719                // SAFETY: tiles partition c's rows; workers never overlap.
17720                let row = unsafe { std::slice::from_raw_parts_mut(cp.0.add(i * n), n) };
17721                for (o, v) in row.iter_mut().zip(r) {
17722                    *o += a * *v;
17723                }
17724            }
17725        }
17726    };
17727    match pool {
17728        Some(p) if tiles > 1 => p.run_rows(tiles, &|s, e| {
17729            for ti in s..e {
17730                body(ti);
17731            }
17732        }),
17733        _ => {
17734            for ti in 0..tiles {
17735                body(ti);
17736            }
17737        }
17738    }
17739}
17740
17741/// Write what the calibration accumulated: `gss.<L>.f32` and `ya.<L>.f32`.
17742pub fn refit_flush() -> usize {
17743    let Some((dir, map)) = refit_dir() else {
17744        return 0;
17745    };
17746    let guard = map.lock().unwrap();
17747    let mut n = 0;
17748    for (li, acc) in guard.0.iter() {
17749        // A silently truncated write here is a Gram that reshapes to
17750        // nothing an hour later — say it out loud instead.
17751        let w = |name: &str, v: &[f32]| {
17752            let path = format!("{dir}/{name}.{li}.f32");
17753            let bytes: Vec<u8> = v.iter().flat_map(|x| x.to_le_bytes()).collect();
17754            match std::fs::write(&path, &bytes) {
17755                Ok(()) => {}
17756                Err(e) => eprintln!(
17757                    "refit: FAILED to write {path} ({} MB): {e}",
17758                    bytes.len() / 1_000_000
17759                ),
17760            }
17761        };
17762        w("gss", &acc.gss);
17763        w("ya", &acc.ya);
17764        println!(
17765            "refit L{li}: {} support, {} tokens, hidden {}",
17766            acc.support.len(),
17767            acc.tokens,
17768            acc.hidden
17769        );
17770        n += 1;
17771    }
17772    n
17773}
17774
17775/// `CMF_FFN_ADUMP=<prefix>` — append every probed token's FFN activation
17776/// row to `<prefix>.<layer>.f16`. The co-activation record: which
17777/// neurons fire together, which is what a tube has to group if a token
17778/// is ever going to open one tube instead of sixteen.
17779fn adump_row(li: usize, g: &[f32]) {
17780    use std::io::Write as _;
17781    static FILES: std::sync::OnceLock<
17782        Option<(
17783            String,
17784            std::sync::Mutex<std::collections::HashMap<usize, std::fs::File>>,
17785        )>,
17786    > = std::sync::OnceLock::new();
17787    let Some((prefix, map)) = FILES
17788        .get_or_init(|| {
17789            std::env::var("CMF_FFN_ADUMP")
17790                .ok()
17791                .map(|p| (p, std::sync::Mutex::new(std::collections::HashMap::new())))
17792        })
17793        .as_ref()
17794    else {
17795        return;
17796    };
17797    // `CMF_FFN_ADUMP_FROM/_TO` narrow the dump to a layer span, so a big
17798    // calibration run fits on disk in a few passes instead of one.
17799    static SPAN: std::sync::OnceLock<(usize, usize)> = std::sync::OnceLock::new();
17800    let (from, to) = *SPAN.get_or_init(|| {
17801        let g = |k: &str, d: usize| {
17802            std::env::var(k)
17803                .ok()
17804                .and_then(|v| v.parse().ok())
17805                .unwrap_or(d)
17806        };
17807        (
17808            g("CMF_FFN_ADUMP_FROM", 0),
17809            g("CMF_FFN_ADUMP_TO", usize::MAX),
17810        )
17811    });
17812    if li < from || li > to {
17813        return;
17814    }
17815    let mut map = map.lock().unwrap();
17816    let f = map.entry(li).or_insert_with(|| {
17817        std::fs::File::create(format!("{prefix}.{li}.f16")).expect("adump file")
17818    });
17819    let mut bytes = Vec::with_capacity(g.len() * 2);
17820    for v in g {
17821        bytes.extend_from_slice(&cortiq_core::quant::f32_to_f16(*v).to_le_bytes());
17822    }
17823    let _ = f.write_all(&bytes);
17824}
17825
17826/// `CMF_FFN_ORACLE_TOPK` — keep only the k largest |silu(g)·u| of each
17827/// token and zero the rest. Not a serving mode: it is the CEILING of
17828/// contextual sparsity — what a per-token router would be chasing —
17829/// measured by cheating, since the selection reads the very activations
17830/// it would have to predict.
17831fn oracle_topk() -> usize {
17832    static K: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
17833    *K.get_or_init(|| {
17834        std::env::var("CMF_FFN_ORACLE_TOPK")
17835            .ok()
17836            .and_then(|v| v.parse().ok())
17837            .unwrap_or(0)
17838    })
17839}
17840
17841/// `CMF_FFN_GATE_TOPK` — the REALIZABLE cousin of the oracle: rank the
17842/// neurons by their gate alone (which the kernel has computed anyway
17843/// before it reads `up`), keep the k best, and drop the rest. Every
17844/// dropped neuron's `up` row and `down` column stay unread, so this is
17845/// the sparsity a serving path can actually take without a router.
17846fn gate_topk() -> usize {
17847    static K: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
17848    *K.get_or_init(|| {
17849        std::env::var("CMF_FFN_GATE_TOPK")
17850            .ok()
17851            .and_then(|v| v.parse().ok())
17852            .unwrap_or(0)
17853    })
17854}
17855
17856/// `CMF_FFN_GATE_BLOCK` — select in blocks of B neurons instead of one
17857/// by one. A scattered per-neuron choice cannot be read efficiently (a
17858/// row at a time, no prefetch runway); a block of 32 is a contiguous
17859/// 32-row slab of `up` and of the transposed `down`, which the ordinary
17860/// kernels stream. The question the measurement answers is what the
17861/// block costs in quality.
17862fn gate_block() -> usize {
17863    static B: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
17864    *B.get_or_init(|| {
17865        std::env::var("CMF_FFN_GATE_BLOCK")
17866            .ok()
17867            .and_then(|v| v.parse().ok())
17868            .unwrap_or(1)
17869    })
17870}
17871
17872/// Zero all but the `k` largest BLOCKS (by summed square) of a row.
17873fn keep_top_blocks(g: &mut [f32], keep_n: usize, block: usize) {
17874    let n = g.len();
17875    let nb = n.div_ceil(block);
17876    let kb = (keep_n.div_ceil(block)).clamp(1, nb);
17877    if kb >= nb {
17878        return;
17879    }
17880    let mut score: Vec<f32> = (0..nb)
17881        .map(|b| {
17882            g[b * block..((b + 1) * block).min(n)]
17883                .iter()
17884                .map(|v| v * v)
17885                .sum::<f32>()
17886        })
17887        .collect();
17888    let mut ord = score.clone();
17889    let (_, kth, _) = ord.select_nth_unstable_by(kb - 1, |a, b| {
17890        b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal)
17891    });
17892    let thr = *kth;
17893    for b in 0..nb {
17894        if score[b] < thr {
17895            g[b * block..((b + 1) * block).min(n)].fill(0.0);
17896        }
17897    }
17898    score.clear();
17899}
17900
17901/// Zero all but the `k` largest magnitudes of one token's activation row.
17902fn keep_top_k(g: &mut [f32], k: usize) {
17903    if gate_block() > 1 {
17904        return keep_top_blocks(g, k, gate_block());
17905    }
17906    let n = g.len();
17907    if k == 0 || k >= n {
17908        return;
17909    }
17910    let mut mag: Vec<f32> = g.iter().map(|v| v.abs()).collect();
17911    let (_, kth, _) = mag.select_nth_unstable_by(k - 1, |a, b| {
17912        b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal)
17913    });
17914    let thr = *kth;
17915    for v in g.iter_mut() {
17916        if v.abs() < thr {
17917            *v = 0.0;
17918        }
17919    }
17920}
17921
17922/// `CMF_FFN_PROBE_SQ` — accumulate Σa², so the dump divided by the token
17923/// count and square-rooted is the RMS activation trace Patent 12 weights
17924/// its matrices by.
17925fn probe_sq() -> bool {
17926    static S: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
17927    *S.get_or_init(|| std::env::var("CMF_FFN_PROBE_SQ").is_ok())
17928}
17929
17930/// `CMF_FFN_PROBE_SIGNED` — accumulate the SIGNED activation sum
17931/// instead of its magnitude: what a dropped neuron contributes ON
17932/// AVERAGE, which is the bias a narrowed FFN can add back for free.
17933fn probe_signed() -> bool {
17934    static S: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
17935    *S.get_or_init(|| std::env::var("CMF_FFN_PROBE_SIGNED").is_ok())
17936}
17937
17938/// `CMF_FFN_MEANFILL=<file>` — a masked-out neuron contributes its MEAN
17939/// activation instead of zero (`u32 layers, u32 inter, f32[…]`, the mass
17940/// dump layout, holding per-neuron means). Dropping a neuron outright
17941/// also drops its average contribution, which shifts the layer output by
17942/// a constant; filling the mean back is one add per layer and costs no
17943/// bytes off the bus. This is the measurement arm — in a tube file the
17944/// same correction ships as a per-task bias vector.
17945fn meanfill() -> Option<&'static (usize, Vec<f32>)> {
17946    static M: std::sync::OnceLock<Option<(usize, Vec<f32>)>> = std::sync::OnceLock::new();
17947    M.get_or_init(|| {
17948        let p = std::env::var("CMF_FFN_MEANFILL").ok()?;
17949        let b = std::fs::read(&p).ok()?;
17950        let inter = u32::from_le_bytes(b[4..8].try_into().ok()?) as usize;
17951        let vals: Vec<f32> = b[8..]
17952            .chunks_exact(4)
17953            .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]]))
17954            .collect();
17955        eprintln!("meanfill: {} value(s), inter {inter}", vals.len());
17956        Some((inter, vals))
17957    })
17958    .as_ref()
17959}
17960
17961/// `CMF_FFN_PROBE_TOPK` — 0 (default) = accumulate mass, k>0 = count
17962/// how often a neuron lands in a token's top k.
17963fn probe_topk() -> usize {
17964    static K: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
17965    *K.get_or_init(|| {
17966        std::env::var("CMF_FFN_PROBE_TOPK")
17967            .ok()
17968            .and_then(|v| v.parse().ok())
17969            .unwrap_or(0)
17970    })
17971}
17972
17973thread_local! {
17974    /// DTG-MA activation probe: per-layer per-neuron Σ|silu(g)·u|
17975    /// accumulator, alive only during `Pipeline::probe_ffn_mass`.
17976    static FFN_PROBE: std::cell::RefCell<Option<Vec<Vec<f64>>>> =
17977        const { std::cell::RefCell::new(None) };
17978}
17979
17980/// Per-token structured sparsity, paid for in bytes.
17981///
17982/// The gate is the cheapest third of an FFN and it already says which
17983/// neurons matter: `silu(gate)` near zero means the neuron contributes
17984/// nothing whatever `up` says. So compute every gate, keep the `k`
17985/// loudest, and read ONLY those neurons' `up` rows and `down` rows —
17986/// the latter needs `down_proj` stored transposed, otherwise a neuron's
17987/// down weights are a strided column and "reading only those" costs a
17988/// full cache line each.
17989///
17990/// Returns `None` when the file has no transposed `down` (the caller
17991/// then runs the ordinary dense path).
17992fn dense_ffn_dynamic(d: &DenseFfn, x: &[f32], pool: Option<&Pool>, k: usize) -> Option<Vec<f32>> {
17993    // The scatter path reads individual rows/columns and cannot express the
17994    // per-matrix signed FWHT boundary.  Let the descriptor-aware dense path
17995    // handle Prism files rather than silently running an unrotated sparse
17996    // approximation.
17997    if d.gate_proj.has_prism_contract()
17998        || d.up_proj.has_prism_contract()
17999        || d.down_proj.has_prism_contract()
18000    {
18001        return None;
18002    }
18003    let dt = d.down_t.as_ref()?;
18004    let inter = d.gate_proj.rows();
18005    let hidden = dt.cols();
18006    if k == 0 || k >= inter || d.act != Act::Silu {
18007        return None;
18008    }
18009    DYN_SCRATCH.with(|sc| {
18010        let mut sc = sc.borrow_mut();
18011        let DynScratch {
18012            g,
18013            mag,
18014            live,
18015            parts,
18016        } = &mut *sc;
18017        g.resize(inter, 0.0);
18018        d.gate_proj.matvec(x, g, pool);
18019        for v in g.iter_mut() {
18020            *v = inference::silu(*v);
18021        }
18022        // The k-th largest |silu(gate)| is the threshold; ties keep more,
18023        // which is the safe side.
18024        mag.clear();
18025        mag.extend(g.iter().map(|v| v.abs()));
18026        let (_, kth, _) = mag.select_nth_unstable_by(k - 1, |a, b| {
18027            b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal)
18028        });
18029        let thr = *kth;
18030        live.clear();
18031        live.extend((0..inter as u32).filter(|&n| g[n as usize].abs() >= thr));
18032        let mut out = vec![0.0f32; hidden];
18033        match pool {
18034            Some(p) if live.len() >= 64 => {
18035                let nw = p.n_workers() + 1;
18036                parts.clear();
18037                parts.resize(nw * hidden, 0.0);
18038                let ptr = SendMut(parts.as_mut_ptr());
18039                let n = live.len();
18040                let live_ref: &[u32] = live;
18041                let g_ref: &[f32] = g;
18042                p.run(&|w, workers| {
18043                    let chunk = n.div_ceil(workers);
18044                    let (s, e) = (w * chunk, ((w + 1) * chunk).min(n));
18045                    if s >= e {
18046                        return;
18047                    }
18048                    WORKER_SCRATCH.with(|ws| {
18049                        let mut ws = ws.borrow_mut();
18050                        let [scratch, acc] = &mut *ws;
18051                        scratch.resize(hidden.max(x.len()), 0.0);
18052                        acc.clear();
18053                        acc.resize(hidden, 0.0);
18054                        for (o, &nrm) in live_ref[s..e].iter().enumerate() {
18055                            // One neuron of runway: the next row's lines
18056                            // start moving while this one is multiplied.
18057                            if let Some(&nx) = live_ref[s..e].get(o + 1) {
18058                                d.up_proj.prefetch_row(nx as usize);
18059                                dt.prefetch_row(nx as usize);
18060                            }
18061                            let idx = nrm as usize;
18062                            let up = d.up_proj.row_dot(idx, x, scratch);
18063                            let a = g_ref[idx] * up;
18064                            if a != 0.0 {
18065                                dt.add_row_scaled(idx, a, acc, scratch);
18066                            }
18067                        }
18068                        for (j, v) in acc.iter().enumerate() {
18069                            unsafe { *ptr.at(w * hidden + j) = *v };
18070                        }
18071                    });
18072                });
18073                for w in 0..nw {
18074                    for (j, o) in out.iter_mut().enumerate() {
18075                        *o += parts[w * hidden + j];
18076                    }
18077                }
18078            }
18079            _ => {
18080                WORKER_SCRATCH.with(|ws| {
18081                    let mut ws = ws.borrow_mut();
18082                    let [scratch, _acc] = &mut *ws;
18083                    scratch.resize(hidden.max(x.len()), 0.0);
18084                    for &nrm in live.iter() {
18085                        let idx = nrm as usize;
18086                        let up = d.up_proj.row_dot(idx, x, scratch);
18087                        let a = g[idx] * up;
18088                        if a != 0.0 {
18089                            dt.add_row_scaled(idx, a, &mut out, scratch);
18090                        }
18091                    }
18092                });
18093            }
18094        }
18095        Some(out)
18096    })
18097}
18098
18099/// Caller-side scratch of the dynamic path — one allocation per thread,
18100/// not one per layer per token (that alone cost a third of the decode).
18101struct DynScratch {
18102    g: Vec<f32>,
18103    mag: Vec<f32>,
18104    live: Vec<u32>,
18105    parts: Vec<f32>,
18106}
18107
18108thread_local! {
18109    static DYN_SCRATCH: std::cell::RefCell<DynScratch> = const {
18110        std::cell::RefCell::new(DynScratch {
18111            g: Vec::new(),
18112            mag: Vec::new(),
18113            live: Vec::new(),
18114            parts: Vec::new(),
18115        })
18116    };
18117    /// Pool-worker scratch: the row buffer and this worker's partial sum.
18118    static WORKER_SCRATCH: std::cell::RefCell<[Vec<f32>; 2]> =
18119        const { std::cell::RefCell::new([Vec::new(), Vec::new()]) };
18120}
18121
18122/// `dense_ffn_cpu` with a per-visit mask landing on the activations —
18123/// the masked-inference fast path's decode arm. Full fused quant
18124/// compute, closed neurons zeroed before down: arithmetically the
18125/// pruned network, no dequant, no weight bytes touched.
18126fn dense_ffn_masked(d: &DenseFfn, x: &[f32], pool: Option<&Pool>, mask_row: &[u8]) -> Vec<f32> {
18127    let inter = d.gate_proj.rows();
18128    FFN_SCRATCH.with(|s| {
18129        let mut s = s.borrow_mut();
18130        let [g, u, ..] = &mut *s;
18131        g.resize(inter, 0.0);
18132        if d.act == Act::Silu && QTensor::matvec_silu_mul(&d.gate_proj, &d.up_proj, x, g, pool) {
18133            // g holds silu(gate)·up.
18134        } else {
18135            u.resize(inter, 0.0);
18136            QTensor::matvec_many([&d.gate_proj, &d.up_proj], x, [g, u], pool);
18137            for i in 0..inter {
18138                g[i] = d.act.combine(g[i], u[i]);
18139            }
18140        }
18141        zero_masked_cols(g, 1, inter, mask_row);
18142        let mut out = attention::take_buf(d.down_proj.rows());
18143        d.down_proj.matvec(g, &mut out, pool);
18144        out
18145    })
18146}
18147
18148/// Dense FFN as one GPU submission via the MoE block path (single
18149/// expert, weight 1.0): gate → silu·up → down chained in one command
18150/// buffer, intermediate activations device-resident. None → weights
18151/// not q8-mapped in the primary shard / over the VRAM budget / backend
18152/// refusal → honest CPU path.
18153fn dense_ffn_gpu(d: &DenseFfn, x: &[f32], _pool: Option<&Pool>) -> Option<Vec<f32>> {
18154    if d.gate_proj.has_prism_contract()
18155        || d.up_proj.has_prism_contract()
18156        || d.down_proj.has_prism_contract()
18157    {
18158        return None;
18159    }
18160    // The GPU block applies SiLU, exact GELU and tanh-GELU (Gemma); other
18161    // activations stay on the CPU.
18162    d.act.graph_act()?;
18163    // Threshold: tiny FFNs are not worth a submission (q1 excepted —
18164    // see the caller's gate).
18165    if d.gate_proj.rows() < crate::gpu::min_rows() && !d.gate_proj.is_q1() {
18166        return None;
18167    }
18168    let mut jobs: Vec<crate::gpu::MoeJob> = Vec::with_capacity(1);
18169    let mut model_ref = None;
18170    moe_push_job(d, x, 1.0, &mut jobs, &mut model_ref)?;
18171    let model = model_ref?;
18172    let hidden = jobs[0].down.1;
18173    let mut out = attention::take_buf(hidden);
18174    if crate::gpu::moe_block(&model, &jobs, &mut out) {
18175        Some(out)
18176    } else {
18177        let mut out = out;
18178        attention::recycle_buf(&mut out);
18179        None
18180    }
18181}
18182
18183/// q8-mapped primary-shard tensor parts for a GPU job: q8_2f carries
18184/// its column field, q8_row runs with empty col slices (the backend
18185/// skips the multiply). Shared by the MoE block and the dense-FFN
18186/// single-job path.
18187#[allow(clippy::type_complexity)]
18188#[allow(clippy::type_complexity)]
18189pub(crate) fn moe_parts(
18190    t: &QTensor,
18191) -> Option<(
18192    &std::sync::Arc<cortiq_core::CmfModel>,
18193    usize,
18194    usize,
18195    usize,
18196    &[f32],
18197    &[f32],
18198    bool,
18199    bool,
18200    bool,
18201)> {
18202    match t {
18203        QTensor::Mapped {
18204            model,
18205            idx,
18206            dtype: dt @ (cortiq_core::TensorDtype::Q8_2f | cortiq_core::TensorDtype::Q8Row),
18207            rows,
18208            cols,
18209            row_scale,
18210            col_field,
18211            ..
18212        } if (*dt == cortiq_core::TensorDtype::Q8Row) || !col_field.is_empty() => Some((
18213            model, *idx, *rows, *cols, row_scale, col_field, false, false, false,
18214        )),
18215        // q1: tile-embedded scales — empty rs/col slices, raw xs.
18216        QTensor::Mapped {
18217            model,
18218            idx,
18219            dtype: cortiq_core::TensorDtype::Q1,
18220            rows,
18221            cols,
18222            ..
18223        } => Some((
18224            model,
18225            *idx,
18226            *rows,
18227            *cols,
18228            &[][..],
18229            &[][..],
18230            true,
18231            false,
18232            false,
18233        )),
18234        // q4_tiled: 18-byte tiles with embedded f16 scales — raw xs.
18235        QTensor::Mapped {
18236            model,
18237            idx,
18238            dtype: cortiq_core::TensorDtype::Q4Tiled,
18239            rows,
18240            cols,
18241            ..
18242        } => Some((
18243            model,
18244            *idx,
18245            *rows,
18246            *cols,
18247            &[][..],
18248            &[][..],
18249            false,
18250            true,
18251            false,
18252        )),
18253        // q4tp: same raw-xs contract, different stride and scale plane.
18254        QTensor::Mapped {
18255            model,
18256            idx,
18257            dtype: cortiq_core::TensorDtype::Q4TiledP,
18258            rows,
18259            cols,
18260            ..
18261        } => Some((
18262            model,
18263            *idx,
18264            *rows,
18265            *cols,
18266            &[][..],
18267            &[][..],
18268            false,
18269            true,
18270            false,
18271        )),
18272        // q2tp: the 2-bit expert plane of the mixed profile — q4 family
18273        // for stride bookkeeping, flagged q2 so the trio validation can
18274        // demand a q4tp down.
18275        QTensor::Mapped {
18276            model,
18277            idx,
18278            dtype: cortiq_core::TensorDtype::Q2TiledP,
18279            rows,
18280            cols,
18281            ..
18282        } => Some((
18283            model,
18284            *idx,
18285            *rows,
18286            *cols,
18287            &[][..],
18288            &[][..],
18289            false,
18290            true,
18291            true,
18292        )),
18293        _ => None,
18294    }
18295}
18296
18297/// The routed experts of a MoE layer as the Metal chunk prefill takes
18298/// them: f32 router rows and every expert's q4tp (gate, up, down) tensor
18299/// indices, uniform SiLU shapes. Only the plain routed form — no shared
18300/// expert, no per-expert scale, no resonance or τ router, no router input
18301/// norm; the batched host walk keeps the rest. The routing itself stays the
18302/// host's (`moe_route`), so mask and selection bias need nothing here.
18303#[cfg(target_os = "macos")]
18304#[allow(clippy::type_complexity)]
18305fn chunk_moe_parts(m: &MoeFfn, hidden: usize) -> Option<(&[f32], Vec<(usize, usize, usize)>, bool)> {
18306    if m.shared.is_some()
18307        || m.per_expert_scale.is_some()
18308        || m.resonance.is_some()
18309        || m.route_tau.is_some()
18310        || m.router_input_norm
18311        || m.experts.is_empty()
18312        || m.top_k == 0
18313        || std::env::var("CMF_RMS_TRACE").is_ok()
18314        || std::env::var("CMF_ACT_DUMP").is_ok()
18315        || std::env::var("CMF_METAL_MOE_PREFILL").as_deref() == Ok("0")
18316    {
18317        return None;
18318    }
18319    let (rf, rr, rc) = m.router.f32_parts()?;
18320    if rr != m.experts.len() || rc != hidden {
18321        return None;
18322    }
18323    let inter = m.experts[0].gate_proj.rows();
18324    // All-q8_2f experts (Mellum2.1 q8_2f) ride the `moe_q8` GEMMs;
18325    // `CMF_METAL_MOE_Q8=0` keeps them on the host walk.
18326    let q8 = crate::gpu_metal::moe_q8::enabled()
18327        && m.experts[0].gate_proj.q8_2f_parts().is_some();
18328    let experts = m
18329        .experts
18330        .iter()
18331        .map(|e| {
18332            if e.act != Act::Silu
18333                || !e.segs.is_empty()
18334                || e.gate_proj.rows() != inter
18335                || e.up_proj.rows() != inter
18336                || e.down_proj.cols() != inter
18337                || e.gate_proj.cols() != hidden
18338                || e.down_proj.rows() != hidden
18339            {
18340                return None;
18341            }
18342            if q8 {
18343                let q8i = |t: &QTensor| t.q8_2f_parts().map(|p| p.0);
18344                return Some((q8i(&e.gate_proj)?, q8i(&e.up_proj)?, q8i(&e.down_proj)?));
18345            }
18346            Some((
18347                e.gate_proj.mapped_q4tp()?.1,
18348                e.up_proj.mapped_q4tp()?.1,
18349                e.down_proj.mapped_q4tp()?.1,
18350            ))
18351        })
18352        .collect::<Option<Vec<_>>>()?;
18353    Some((rf, experts, q8))
18354}
18355
18356/// Map a MoE onto the Metal token graph's contract: f32 router, a
18357/// shared expert (gated — Qwen — or ungated at weight 1 — DeepSeek-V3 /
18358/// HunYuan hy_v3), softmax or sigmoid scores with an optional selection
18359/// bias and routed scale, experts uniformly q4tp (or the mixed profile:
18360/// q2tp gate/up over a q4tp down). τ routers, masks, per-expert scales
18361/// and Gemma's router-input norm refuse here — those semantics stay on
18362/// the CPU path.
18363#[cfg(target_os = "macos")]
18364fn metal_moe_graph_parts(m: &MoeFfn, hidden: usize) -> Option<crate::gpu::GpuMoe<'_>> {
18365    if m.router_input_norm
18366        || m.route_tau.is_some()
18367        || m.mask.is_some()
18368        || m.per_expert_scale.is_some()
18369        || m.experts.is_empty()
18370        || m.top_k == 0
18371        || m.resonance.is_some()
18372    {
18373        return None;
18374    }
18375    // Without a shared expert the select kernel fills the k routed jobs
18376    // only (flag 32). Measured on Mellum2.1's softmax router; the sigmoid /
18377    // biased routers without one (LFM2-MoE) keep the CPU path until they
18378    // are measured too.
18379    let (sh, sg) = match &m.shared {
18380        Some((sh, sg)) => (Some(sh), sg.as_ref()),
18381        None if !m.router_sigmoid && m.expert_bias.is_none() => (None, None),
18382        None => return None,
18383    };
18384    let (rf, rr, rc) = m.router.f32_parts()?;
18385    if rr != m.experts.len() || rc != hidden {
18386        return None;
18387    }
18388    let shared_gated = sg.is_some();
18389    let sf = match sg {
18390        Some(sg) => {
18391            let (sf, sr, sc) = sg.f32_parts()?;
18392            if sr * sc != hidden {
18393                return None;
18394            }
18395            sf
18396        }
18397        // Ungated: the router's first row stands in for the gate matvec
18398        // (its logit is never read — the kernel pins weight 1).
18399        None => &rf[..hidden],
18400    };
18401    if let Some(b) = &m.expert_bias {
18402        if b.len() != m.experts.len() {
18403            return None;
18404        }
18405    }
18406    let inter = m.experts[0].gate_proj.rows();
18407    // The first expert's gate decides the profile; every trio (shared
18408    // included) must agree — the jobs ladder flips ONE kernel for all.
18409    let gu_q2 = m.experts[0].gate_proj.mapped_q2tp().is_some();
18410    // All-q8_2f routed experts (Mellum2.1 q8_2f: no shared expert, the
18411    // contract they were measured on) take the `moe_q8` kernels;
18412    // `CMF_METAL_MOE_Q8=0` keeps them off the graph (per-op path).
18413    let q8 = crate::gpu_metal::moe_q8::enabled()
18414        && m.shared.is_none()
18415        && m.experts[0].gate_proj.q8_2f_parts().is_some();
18416    let trio = |e: &DenseFfn| -> Option<(usize, usize, usize)> {
18417        if e.act != Act::Silu
18418            || e.gate_proj.rows() != inter
18419            || e.gate_proj.cols() != hidden
18420            || e.up_proj.rows() != inter
18421            || e.up_proj.cols() != hidden
18422            || e.down_proj.rows() != hidden
18423            || e.down_proj.cols() != inter
18424        {
18425            return None;
18426        }
18427        if q8 {
18428            let q8i = |t: &QTensor| t.q8_2f_parts().map(|p| p.0);
18429            return Some((q8i(&e.gate_proj)?, q8i(&e.up_proj)?, q8i(&e.down_proj)?));
18430        }
18431        let pick = |t: &QTensor| -> Option<usize> {
18432            if gu_q2 {
18433                t.mapped_q2tp().map(|(_, i)| i)
18434            } else {
18435                t.mapped_q4tp().map(|(_, i)| i)
18436            }
18437        };
18438        Some((
18439            pick(&e.gate_proj)?,
18440            pick(&e.up_proj)?,
18441            e.down_proj.mapped_q4tp().map(|(_, i)| i)?,
18442        ))
18443    };
18444    let experts = m.experts.iter().map(trio).collect::<Option<Vec<_>>>()?;
18445    let shared = match sh {
18446        Some(sh) => Some(trio(sh)?),
18447        None => None,
18448    };
18449    Some(crate::gpu::GpuMoe {
18450        router: rf,
18451        sgate: sf,
18452        experts,
18453        shared,
18454        n_exp: m.experts.len(),
18455        top_k: m.top_k,
18456        inter,
18457        norm_topk: m.norm_topk_prob,
18458        route_scale: m.routed_scaling,
18459        gu_q2,
18460        q8,
18461        sigmoid: m.router_sigmoid,
18462        bias: m.expert_bias.as_deref(),
18463        shared_gated,
18464    })
18465}
18466
18467/// `CMF_FORCE_IDS=<file>` (diagnostics): decode step n commits the n-th
18468/// whitespace-separated token id of the file instead of sampling, while the
18469/// logits are still computed (and dumped by `CMF_LOGIT_DUMP_ALL`) — one
18470/// backend teacher-forced along another's greedy sequence, so top-1
18471/// agreement can be counted past a near-tie divergence.
18472fn forced_ids_at(step: usize) -> Option<u32> {
18473    static IDS: std::sync::OnceLock<Vec<u32>> = std::sync::OnceLock::new();
18474    IDS.get_or_init(|| {
18475        std::env::var("CMF_FORCE_IDS")
18476            .ok()
18477            .and_then(|p| std::fs::read_to_string(p).ok())
18478            .map(|s| s.split_whitespace().filter_map(|t| t.parse().ok()).collect())
18479            .unwrap_or_default()
18480    })
18481    .get(step)
18482    .copied()
18483}
18484
18485/// Build one gate/up/down GPU job from three tensors. `moe_push_job` is the
18486/// DenseFfn-shaped caller; architectures that keep their experts in their own
18487/// structs (DeepSeek-V4) come here directly.
18488pub(crate) fn moe_push_job_parts<'a>(
18489    gate: &'a QTensor,
18490    up: &'a QTensor,
18491    down: &'a QTensor,
18492    x: &[f32],
18493    w: f32,
18494    swiglu_limit: f32,
18495    jobs: &mut Vec<crate::gpu::MoeJob<'a>>,
18496    model_ref: &mut Option<std::sync::Arc<cortiq_core::CmfModel>>,
18497) -> Option<()> {
18498    use crate::qtensor::prescale;
18499    let (gm, gi, gr, gc, grs, gcf, gq1, gq4, gq2) = moe_parts(gate)?;
18500    let (_, ui, ur, uc, urs, ucf, uq1, uq4, uq2) = moe_parts(up)?;
18501    let (_, di, dr, dc, drs, dcf, dq1, dq4, dq2) = moe_parts(down)?;
18502    if gq1 != uq1 || uq1 != dq1 || gq4 != uq4 || uq4 != dq4 || gq2 != uq2 {
18503        return None; // mixed-dtype trio — honest CPU path
18504    }
18505    // The 2-bit profile is gate/up q2tp over a PLAIN q4tp down; any other
18506    // 2-bit arrangement stays on the CPU.
18507    if gq2 && (dq2 || !dq4 || down.mapped_q4tp().is_none()) {
18508        return None;
18509    }
18510    if !gq2 && dq2 {
18511        return None;
18512    }
18513    model_ref.get_or_insert_with(|| gm.clone());
18514    let dt = |cf: &[f32]| {
18515        if cf.is_empty() {
18516            cortiq_core::TensorDtype::Q8Row
18517        } else {
18518            cortiq_core::TensorDtype::Q8_2f
18519        }
18520    };
18521    jobs.push(crate::gpu::MoeJob {
18522        gate: (gi, gr, gc, grs),
18523        up: (ui, ur, uc, urs),
18524        down: (di, dr, dc, drs),
18525        xs_gate: prescale(x, gcf, dt(gcf)).into_owned(),
18526        xs_up: prescale(x, ucf, dt(ucf)).into_owned(),
18527        down_col: dcf,
18528        w,
18529        q1: gq1,
18530        q4t: gq4 && !gq2 && gate.mapped_q4tp().is_none(),
18531        q4tp: gq4 && (gq2 || gate.mapped_q4tp().is_some()),
18532        gu_q2: gq2,
18533        swiglu_limit,
18534        act: 0,
18535    });
18536    Some(())
18537}
18538
18539/// Build one gate/up/down GPU job (see `moe_parts`).
18540fn moe_push_job<'a>(
18541    d: &'a DenseFfn,
18542    x: &[f32],
18543    w: f32,
18544    jobs: &mut Vec<crate::gpu::MoeJob<'a>>,
18545    model_ref: &mut Option<std::sync::Arc<cortiq_core::CmfModel>>,
18546) -> Option<()> {
18547    use crate::qtensor::prescale;
18548    // The block's act(gate)·up pass takes the activation's code (SiLU,
18549    // exact GELU, tanh-GELU); anything without a device form stays home.
18550    let act = d.act.graph_act()?.code();
18551    let (gm, gi, gr, gc, grs, gcf, gq1, gq4, gq2) = moe_parts(&d.gate_proj)?;
18552    let (_, ui, ur, uc, urs, ucf, uq1, uq4, uq2) = moe_parts(&d.up_proj)?;
18553    let (_, di, dr, dc, drs, dcf, dq1, dq4, dq2) = moe_parts(&d.down_proj)?;
18554    if gq1 != uq1 || uq1 != dq1 || gq4 != uq4 || uq4 != dq4 || gq2 != uq2 {
18555        return None; // mixed-dtype trio — honest CPU path
18556    }
18557    if gq2 && (dq2 || !dq4 || d.down_proj.mapped_q4tp().is_none()) {
18558        return None;
18559    }
18560    if !gq2 && dq2 {
18561        return None;
18562    }
18563    model_ref.get_or_insert_with(|| gm.clone());
18564    let gdt = if gcf.is_empty() {
18565        cortiq_core::TensorDtype::Q8Row
18566    } else {
18567        cortiq_core::TensorDtype::Q8_2f
18568    };
18569    let udt = if ucf.is_empty() {
18570        cortiq_core::TensorDtype::Q8Row
18571    } else {
18572        cortiq_core::TensorDtype::Q8_2f
18573    };
18574    jobs.push(crate::gpu::MoeJob {
18575        gate: (gi, gr, gc, grs),
18576        up: (ui, ur, uc, urs),
18577        down: (di, dr, dc, drs),
18578        xs_gate: prescale(x, gcf, gdt).into_owned(),
18579        xs_up: prescale(x, ucf, udt).into_owned(),
18580        down_col: dcf,
18581        w,
18582        q1: gq1,
18583        q4t: gq4 && !gq2 && d.gate_proj.mapped_q4tp().is_none(),
18584        q4tp: gq4 && (gq2 || d.gate_proj.mapped_q4tp().is_some()),
18585        gu_q2: gq2,
18586        swiglu_limit: 0.0,
18587        act,
18588    });
18589    Some(())
18590}
18591
18592/// Sparse dense-FFN directly on QUANTIZED weights (mask × mmap): reads
18593/// ONLY the active neurons' gate/up rows and down columns from the mmap
18594/// — no full-matrix dequant, no f32 model copy. This is what lets a
18595/// masked big model run at quantized RSS (the historical mask path
18596/// forced the whole model to f32). Semantics identical to the f32
18597/// sparse path within quant tolerance.
18598fn sparse_ffn_quant(
18599    d: &DenseFfn,
18600    x: &[f32],
18601    active: &[u16],
18602    hidden: usize,
18603    pool: Option<&Pool>,
18604) -> Vec<f32> {
18605    let n = active.len();
18606    let inter = d.gate_proj.rows();
18607    let mut act = vec![0.0f32; n];
18608    // Scratch is needed if EITHER projection is group-packed (q4/vbit);
18609    // gate/up normally share a dtype but sizing on both is robust.
18610    let need_scratch = !(d.gate_proj.sparse_col_ok() && d.up_proj.sparse_col_ok());
18611    let compute = |ai: usize| -> f32 {
18612        let idx = active[ai] as usize;
18613        if idx >= inter {
18614            return 0.0; // defensive parity with the f32 sparse path
18615        }
18616        let mut s = if need_scratch {
18617            vec![0.0f32; hidden]
18618        } else {
18619            Vec::new()
18620        };
18621        let gate = d.gate_proj.row_dot(idx, x, &mut s);
18622        let up = d.up_proj.row_dot(idx, x, &mut s);
18623        d.act.combine(gate, up)
18624    };
18625    match pool {
18626        Some(p) if n >= 256 => {
18627            let ptr = SendMut(act.as_mut_ptr());
18628            p.run(&|widx, nw| {
18629                let chunk = n.div_ceil(nw);
18630                let (s, e) = (widx * chunk, ((widx + 1) * chunk).min(n));
18631                for ai in s..e {
18632                    unsafe { *ptr.at(ai) = compute(ai) };
18633                }
18634            });
18635        }
18636        _ => {
18637            for (ai, a) in act.iter_mut().enumerate() {
18638                *a = compute(ai);
18639            }
18640        }
18641    }
18642    // Scatter through active down columns (reads only those columns).
18643    let mut out = vec![0.0f32; hidden];
18644    for (ai, &idx) in active.iter().enumerate() {
18645        let w = act[ai];
18646        if w.abs() >= 1e-12 && (idx as usize) < inter {
18647            d.down_proj.add_col_scaled(idx as usize, w, &mut out);
18648        }
18649    }
18650    out
18651}
18652
18653/// Test-only re-export of the private sparse-quant FFN (mask × mmap gate).
18654#[doc(hidden)]
18655pub fn sparse_ffn_quant_for_test(
18656    d: &DenseFfn,
18657    x: &[f32],
18658    active: &[u16],
18659    hidden: usize,
18660) -> Vec<f32> {
18661    sparse_ffn_quant(d, x, active, hidden, None)
18662}
18663
18664/// Dequantize a DenseFfn's three matrices to f32 (transient; only the
18665/// q4/vbit-masked fallback uses it — the memory-lean path is
18666/// sparse_ffn_quant). Reuses row_f32 row-by-row.
18667fn dequant_dense_f32(d: &DenseFfn) -> (Vec<f32>, Vec<f32>, Vec<f32>) {
18668    let deq = |t: &QTensor| -> Vec<f32> {
18669        let (rows, cols) = (t.rows(), t.cols());
18670        let mut out = vec![0.0f32; rows * cols];
18671        for r in 0..rows {
18672            t.row_f32(r, &mut out[r * cols..(r + 1) * cols]);
18673        }
18674        out
18675    };
18676    (deq(&d.gate_proj), deq(&d.up_proj), deq(&d.down_proj))
18677}
18678
18679/// Pointer wrapper for the worker-pool scatter (same pattern as qtensor).
18680struct SendMut(*mut f32);
18681unsafe impl Send for SendMut {}
18682unsafe impl Sync for SendMut {}
18683impl SendMut {
18684    #[inline]
18685    // Deliberate unsynchronized scatter: pool workers write disjoint indices
18686    // in parallel, so returning `&mut` from `&self` is intentional here.
18687    #[allow(clippy::mut_from_ref)]
18688    unsafe fn at(&self, i: usize) -> &mut f32 {
18689        unsafe { &mut *self.0.add(i) }
18690    }
18691}
18692
18693/// Router → (selected experts in torch.topk order, per-expert score
18694/// vector, normalizer). The final weight of expert `e` is `p[e] / wsum`.
18695///
18696/// Two regimes share this. Qwen: softmax over ALL experts, top-k of the
18697/// probabilities, optional renorm — `router_sigmoid=false`, no bias,
18698/// scale 1 → bit-identical to the historical path. LFM2-MoE /
18699/// DeepSeek-V3 `noaux_tc`: per-expert sigmoid scores, an optional
18700/// selection bias (top-k CHOICE only; weights stay unbiased), a 1e-6 renorm
18701/// floor and a routed scale. Architectures whose reference uses a different
18702/// sigmoid denominator floor (for example GLM-5's `1e-20`) call
18703/// [`moe_route_with_eps`] directly; the historical generic path remains
18704/// unchanged.
18705pub(crate) fn moe_route(
18706    logits: &[f32],
18707    m: &MoeFfn,
18708    allowed: Option<&[bool]>,
18709) -> (Vec<usize>, Vec<f32>, f32) {
18710    moe_route_with_eps(logits, m, allowed, 1e-6)
18711}
18712
18713/// Router implementation with an explicit sigmoid renormalization floor.
18714///
18715/// GLM-5.3's source computes `sum(selected_scores) + 1e-20`; using the
18716/// generic 1e-6 floor there is not a harmless tolerance difference when all
18717/// logits are very negative: it collapses the routed branch toward zero
18718/// instead of normalizing the selected experts. Keeping the epsilon parameter
18719/// here avoids changing the established Qwen/LFM2 contract while allowing
18720/// each architecture to preserve its own numerical semantics.
18721pub(crate) fn moe_route_with_eps(
18722    logits: &[f32],
18723    m: &MoeFfn,
18724    allowed: Option<&[bool]>,
18725    sigmoid_denom_eps: f32,
18726) -> (Vec<usize>, Vec<f32>, f32) {
18727    let ne = logits.len();
18728    // Expert restriction: the static env mask (CMF_MOE_MASK) AND the
18729    // active task mask's expert fields (spec §5) both narrow the
18730    // candidate set; selection happens over the admitted experts only.
18731    // With norm_topk the kept weights renormalize below; without it
18732    // the excluded mass is honestly dropped.
18733    let admit = |e: usize| {
18734        m.mask.as_ref().is_none_or(|mk| mk[e])
18735            && allowed.is_none_or(|a| a.get(e).copied().unwrap_or(false))
18736    };
18737    // The resonance router (spec §9.5.1) selects by the RAW score: the
18738    // trainer (`resonance_winner`) and the resident graph
18739    // (`embryo_core_route_pick`) take the first maximum of the scores
18740    // and run the winner with weight 1.0. Selecting through the softmax
18741    // instead is not the same decision: `exp(l − max)` rounds two scores
18742    // closer than 2^-25 (possible below |score| 0.25) to the same 1.0, and
18743    // the lower index would take a token whose score is strictly smaller
18744    // — the trainer's trace and the graph would disagree with this path.
18745    // `−∞` (outside the shell) never wins; with no finite admitted expert
18746    // the generic path below degrades to uniform. The winner's
18747    // probability is 1.0 by construction (a one-hot `p`), so its
18748    // renormalized weight is `routed_scaling` on both norm_topk settings.
18749    if m.resonance.is_some() && m.top_k == 1 {
18750        let mut best: Option<usize> = None;
18751        for e in (0..ne).filter(|&e| admit(e)) {
18752            let l = logits[e];
18753            if l == f32::NEG_INFINITY || l.is_nan() {
18754                continue;
18755            }
18756            if best.is_none_or(|b| l > logits[b]) {
18757                best = Some(e);
18758            }
18759        }
18760        if let Some(b) = best {
18761            let mut p = vec![0.0f32; ne];
18762            p[b] = 1.0;
18763            return (vec![b], p, 1.0 / m.routed_scaling);
18764        }
18765    }
18766    // A `−∞` logit (a grown expert outside its shell, `Resonance::scores`)
18767    // takes probability 0 on both paths: sigmoid(−∞) = 0, exp(−∞ − max) = 0
18768    // — top-1 is the best FINITE expert, its renormalized weight exactly
18769    // 1.0. Every expert at −∞ cannot happen (trunk experts have no shell);
18770    // should it, the softmax would be NaN, so it degrades to uniform.
18771    let p: Vec<f32> = if m.router_sigmoid {
18772        logits.iter().map(|&l| 1.0 / (1.0 + (-l).exp())).collect()
18773    } else {
18774        let mx = logits.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
18775        if mx == f32::NEG_INFINITY {
18776            vec![1.0 / ne.max(1) as f32; ne]
18777        } else {
18778            let mut e: Vec<f32> = logits.iter().map(|&l| (l - mx).exp()).collect();
18779            let s: f32 = e.iter().sum();
18780            for v in &mut e {
18781                *v /= s;
18782            }
18783            e
18784        }
18785    };
18786    let mut idx: Vec<usize> = (0..ne).filter(|&e| admit(e)).collect();
18787    // Descending by selection score, lower index wins ties (torch.topk).
18788    match &m.expert_bias {
18789        Some(b) => idx.sort_unstable_by(|&x, &y| {
18790            (p[y] + b[y])
18791                .partial_cmp(&(p[x] + b[x]))
18792                .unwrap()
18793                .then(x.cmp(&y))
18794        }),
18795        None => idx.sort_unstable_by(|&x, &y| p[y].partial_cmp(&p[x]).unwrap().then(x.cmp(&y))),
18796    }
18797    idx.truncate(m.top_k);
18798    // Adaptive τ-routing: trim the tail experts once the kept mass is
18799    // enough. wsum below renormalizes over the KEPT set, so the output
18800    // stays a proper weighted average.
18801    if let Some(tau) = m.route_tau {
18802        let total: f32 = idx.iter().map(|&e| p[e]).sum();
18803        if total > 0.0 {
18804            let mut acc = 0.0f32;
18805            let mut keep = idx.len();
18806            for (i, &e) in idx.iter().enumerate() {
18807                acc += p[e];
18808                if acc >= tau * total {
18809                    keep = i + 1;
18810                    break;
18811                }
18812            }
18813            idx.truncate(keep);
18814        }
18815    }
18816    let wsum: f32 = if m.norm_topk_prob {
18817        let s: f32 = idx.iter().map(|&e| p[e]).sum();
18818        // Sigmoid routers use their architecture's reference floor; the
18819        // softmax path's probs already sum near 1, so it stays exactly as
18820        // before.
18821        (if m.router_sigmoid {
18822            s + sigmoid_denom_eps
18823        } else {
18824            s
18825        }) / m.routed_scaling
18826    } else {
18827        1.0 / m.routed_scaling
18828    };
18829    (idx, p, wsum)
18830}
18831
18832/// See the call site: one `layer:e1,e2,…` line per routed token.
18833fn moe_trace(idx: &[usize]) {
18834    moe_trace_at(crate::gpu::cur_layer() as i32, idx)
18835}
18836
18837/// The same, for callers that know their layer (DSV4 owns its layers and
18838/// never sets the pipeline's current-layer marker).
18839pub(crate) fn moe_trace_at(li: i32, idx: &[usize]) {
18840    use std::io::Write;
18841    static F: std::sync::OnceLock<Option<std::sync::Mutex<std::fs::File>>> =
18842        std::sync::OnceLock::new();
18843    let Some(f) = F.get_or_init(|| {
18844        let p = std::env::var("CMF_MOE_TRACE").ok()?;
18845        Some(std::sync::Mutex::new(
18846            std::fs::OpenOptions::new()
18847                .create(true)
18848                .append(true)
18849                .open(p)
18850                .ok()?,
18851        ))
18852    }) else {
18853        return;
18854    };
18855    let ids: Vec<String> = idx.iter().map(|e| e.to_string()).collect();
18856    let _ = writeln!(f.lock().unwrap(), "{li}:{}", ids.join(","));
18857}
18858
18859/// MoE FFN: router → top-k experts (see `moe_route`). Only selected
18860/// experts' pages are touched in mmap.
18861pub(crate) fn moe_ffn(
18862    m: &MoeFfn,
18863    x: &[f32],
18864    pool: Option<&Pool>,
18865    allowed: Option<&[bool]>,
18866) -> Vec<f32> {
18867    let r = moe_ffn_route(m, x, pool, allowed);
18868    moe_ffn_experts(m, x, &r, pool)
18869}
18870
18871/// One token's host route through a MoE layer: the chosen experts in
18872/// selection order, the per-expert scores and the normalizer (see
18873/// `moe_route`), plus the raw router logits.
18874pub(crate) struct MoeRoute {
18875    pub idx: Vec<usize>,
18876    pub p: Vec<f32>,
18877    pub wsum: f32,
18878    pub logits: Vec<f32>,
18879}
18880
18881/// The routing half of `moe_ffn`, shared by every executor of the chosen
18882/// experts (the host/per-op path below and the MiMo dynamic device cache,
18883/// `crate::mimo_moe`): activation accounting, router logits, `moe_route`,
18884/// the selection statistics and the `CMF_MOE_TRACE` line — so switching
18885/// executors can never change which experts a token gets.
18886pub(crate) fn moe_ffn_route(
18887    m: &MoeFfn,
18888    x: &[f32],
18889    pool: Option<&Pool>,
18890    allowed: Option<&[bool]>,
18891) -> MoeRoute {
18892    accumulate_act(m, x, 1);
18893    let ne = m.experts.len();
18894    let mut logits = vec![0.0f32; ne];
18895    match &m.resonance {
18896        Some(r) => r.scores(x, &mut logits),
18897        None => m.router.matvec(x, &mut logits, pool),
18898    }
18899    let (idx, p, wsum) = moe_route(&logits, m, allowed);
18900    {
18901        let mut st = m.stats.borrow_mut();
18902        if st.len() < ne {
18903            st.resize(ne, 0);
18904        }
18905        for &e in &idx {
18906            st[e] += 1;
18907        }
18908    }
18909    // `CMF_MOE_TRACE=<file>`: append one line per (layer, token) with the
18910    // selected expert ids. The cumulative `stats` above answer "which
18911    // experts are popular"; a residency design needs the question they
18912    // cannot answer — whether CONSECUTIVE tokens reuse experts (the
18913    // temporal locality an LRU cache lives on, FreeToken §4).
18914    moe_trace(&idx);
18915    MoeRoute {
18916        idx,
18917        p,
18918        wsum,
18919        logits,
18920    }
18921}
18922
18923/// The expert half of `moe_ffn`: run a route's experts on the per-op GPU
18924/// block or the host.
18925pub(crate) fn moe_ffn_experts(
18926    m: &MoeFfn,
18927    x: &[f32],
18928    r: &MoeRoute,
18929    pool: Option<&Pool>,
18930) -> Vec<f32> {
18931    let (idx, p, wsum) = (&r.idx, &r.p, r.wsum);
18932    // D5: the whole layer MoE block in one GPU command buffer (experts — the
18933    // same mmap via a no-copy buffer; intermediate activations on the GPU).
18934    // Same Ffn probe class as the dense chain: one submit per layer
18935    // either wins on this driver stack or it doesn't.
18936    if crate::gpu::enabled_here() {
18937        match crate::gpu::probe_arm(crate::gpu::OpClass::Ffn) {
18938            crate::gpu::ProbeArm::Gpu => {
18939                let t0 = std::time::Instant::now();
18940                if let Some(out) = moe_ffn_gpu(m, x, &idx, &p, wsum, pool) {
18941                    crate::gpu::probe_record(crate::gpu::OpClass::Ffn, true, t0.elapsed());
18942                    return out;
18943                }
18944            }
18945            crate::gpu::ProbeArm::CpuTimed => {
18946                let t0 = std::time::Instant::now();
18947                let out = crate::gpu::cpu_scope(|| moe_ffn_cpu(m, x, &idx, &p, wsum, pool));
18948                crate::gpu::probe_record(crate::gpu::OpClass::Ffn, false, t0.elapsed());
18949                return out;
18950            }
18951            crate::gpu::ProbeArm::Cpu => {
18952                return crate::gpu::cpu_scope(|| moe_ffn_cpu(m, x, &idx, &p, wsum, pool));
18953            }
18954        }
18955    }
18956    moe_ffn_cpu(m, x, &idx, &p, wsum, pool)
18957}
18958
18959/// One MoE token through the MiMo expert bank (`crate::mimo_moe`), or —
18960/// when the bank does not serve it — through the host path with the SAME
18961/// route, so the routing statistics and `CMF_MOE_TRACE` see it once.
18962fn moe_ffn_banked(
18963    slot: &mut crate::mimo_moe::Slot,
18964    li: usize,
18965    m: &MoeFfn,
18966    x: &[f32],
18967    pool: Option<&Pool>,
18968) -> Vec<f32> {
18969    let t0 = std::time::Instant::now();
18970    let r = moe_ffn_route(m, x, pool, None);
18971    slot.note_route(t0.elapsed().as_nanos() as u64);
18972    match slot.forward(li, m, x, &r, pool) {
18973        Some(out) => out,
18974        None => crate::qtensor::float_activations_scope(|| {
18975            crate::gpu::cpu_scope(|| moe_ffn_experts(m, x, &r, pool))
18976        }),
18977    }
18978}
18979
18980/// Verify rows share a bank frame; routing and fallback are decode's.
18981fn moe_ffn_banked_rows(
18982    slot: &mut crate::mimo_moe::Slot,
18983    li: usize,
18984    m: &MoeFfn,
18985    xs: &[f32],
18986    b: usize,
18987    hidden: usize,
18988    pool: Option<&Pool>,
18989) -> Vec<f32> {
18990    let t0 = std::time::Instant::now();
18991    let routes: Vec<_> = xs
18992        .chunks_exact(hidden)
18993        .map(|x| moe_ffn_route(m, x, pool, None))
18994        .collect();
18995    slot.note_route(t0.elapsed().as_nanos() as u64);
18996    if let Some(out) = slot.forward_rows(li, m, xs, &routes, pool) {
18997        return out;
18998    }
18999    let mut out = Vec::with_capacity(b * hidden);
19000    for (x, r) in xs.chunks_exact(hidden).zip(&routes) {
19001        let row = slot.forward(li, m, x, r, pool).unwrap_or_else(|| {
19002            // A failed bank must not stream missing experts into the arena.
19003            crate::qtensor::float_activations_scope(|| {
19004                crate::gpu::cpu_scope(|| moe_ffn_experts(m, x, r, pool))
19005            })
19006        });
19007        out.extend(row);
19008    }
19009    out
19010}
19011
19012/// One-shot report of whether the whole-token wgpu graph actually formed.
19013/// A refusal silently reverts to the per-op path, which is how a model can
19014/// look "GPU-accelerated" while every layer walks the host.  A device prefix
19015/// is tracked separately because it still pays a host boundary for the tail.
19016fn graph_note(built: bool, layers_run: usize, total_layers: usize) {
19017    use std::sync::atomic::{AtomicBool, Ordering};
19018    if built {
19019        GRAPH_TOK_OK.fetch_add(1, Ordering::Relaxed);
19020        if total_layers > 0 && layers_run < total_layers {
19021            GRAPH_TOK_PREFIX.fetch_add(1, Ordering::Relaxed);
19022        } else {
19023            GRAPH_TOK_FULL.fetch_add(1, Ordering::Relaxed);
19024        }
19025    } else {
19026        GRAPH_TOK_MISS.fetch_add(1, Ordering::Relaxed);
19027    }
19028    static SAID: AtomicBool = AtomicBool::new(false);
19029    if !SAID.swap(true, Ordering::Relaxed) {
19030        if built {
19031            tracing::info!("wgpu whole-token graph: ACTIVE");
19032        } else {
19033            tracing::warn!("wgpu whole-token graph refused — per-op path");
19034        }
19035    }
19036}
19037
19038/// Whole-token graph outcomes, process-wide: a benchmark that claims a
19039/// GPU number while MISS climbs is measuring the CPU — the honest-bench
19040/// contract makes that an error, not a footnote.
19041pub static GRAPH_TOK_OK: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
19042pub static GRAPH_TOK_MISS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
19043/// Graph calls that returned a hidden after running only a leading device
19044/// prefix.  These are valid hybrid executions but must not be reported as a
19045/// full GPU graph in benchmark evidence.
19046pub static GRAPH_TOK_PREFIX: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
19047/// Graph calls that covered the complete requested layer span.
19048pub static GRAPH_TOK_FULL: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
19049
19050/// Native Metal TokenGraph completion counters. These are incremented only
19051/// after checked command-buffer completion and successful readback, so a
19052/// fused-head NLL report can prove the route rather than infer it from env.
19053pub static METAL_GRAPH_TOK_OK: std::sync::atomic::AtomicU64 =
19054    std::sync::atomic::AtomicU64::new(0);
19055pub static METAL_GRAPH_HEAD_OK: std::sync::atomic::AtomicU64 =
19056    std::sync::atomic::AtomicU64::new(0);
19057pub static METAL_GRAPH_HEAD_MISS: std::sync::atomic::AtomicU64 =
19058    std::sync::atomic::AtomicU64::new(0);
19059pub static METAL_GRAPH_LAYERS: std::sync::atomic::AtomicU64 =
19060    std::sync::atomic::AtomicU64::new(0);
19061pub static METAL_GRAPH_ERRORS: std::sync::atomic::AtomicU64 =
19062    std::sync::atomic::AtomicU64::new(0);
19063/// Ordinary native-Metal rows-prefill admissions and completed rows.  These
19064/// counters are separate from TokenGraph token/head counts so a batch NLL
19065/// receipt cannot accidentally claim serial execution as batched.
19066pub static METAL_PREFILL_CHUNKS: std::sync::atomic::AtomicU64 =
19067    std::sync::atomic::AtomicU64::new(0);
19068pub static METAL_PREFILL_ROWS: std::sync::atomic::AtomicU64 =
19069    std::sync::atomic::AtomicU64::new(0);
19070pub static METAL_PREFILL_HEAD_ROWS: std::sync::atomic::AtomicU64 =
19071    std::sync::atomic::AtomicU64::new(0);
19072pub static METAL_PREFILL_ERRORS: std::sync::atomic::AtomicU64 =
19073    std::sync::atomic::AtomicU64::new(0);
19074
19075/// `CMF_MOE_BATCH=0` restores the per-expert serial loop — the A/B lever
19076/// for the batched kernel, and how its bit-identity is checked.
19077fn moe_batch_enabled() -> bool {
19078    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
19079    *ON.get_or_init(|| std::env::var("CMF_MOE_BATCH").as_deref() != Ok("0"))
19080}
19081
19082/// Two-dispatch CPU MoE: every routed expert (and the shared one) fused
19083/// into one gate/up/SiLU dispatch and one down dispatch, instead of two
19084/// pool barriers per expert. Bit-identical to the serial loop below —
19085/// see `moe_gate_up_many` / `moe_down_many`. `None` = the batched kernel
19086/// does not cover this layer, walk the serial path.
19087fn moe_ffn_cpu_batched(
19088    m: &MoeFfn,
19089    x: &[f32],
19090    idx: &[usize],
19091    p: &[f32],
19092    wsum: f32,
19093    pool: Option<&Pool>,
19094) -> Option<Vec<f32>> {
19095    if idx.is_empty() || !moe_batch_enabled() {
19096        return None;
19097    }
19098    // The bake probe reads per-neuron activation mass out of the
19099    // single-expert path; batching would skip it. Rare and offline —
19100    // hand those runs to the serial loop.
19101    if FFN_PROBE.with(|pr| pr.borrow().is_some()) {
19102        return None;
19103    }
19104    let n = idx.len() + usize::from(m.shared.is_some());
19105    let mut pairs = Vec::with_capacity(n);
19106    let mut downs = Vec::with_capacity(n);
19107    let mut ws = Vec::with_capacity(n);
19108    for &e in idx {
19109        let d = &m.experts[e];
19110        if d.act != Act::Silu {
19111            return None;
19112        }
19113        pairs.push((&d.gate_proj, &d.up_proj));
19114        downs.push(&d.down_proj);
19115        ws.push(p[e] / wsum * m.per_expert_scale.as_ref().map_or(1.0, |v| v[e]));
19116    }
19117    // The shared expert goes last, matching the serial loop's order —
19118    // the f32 accumulation order is part of the bit-identity claim.
19119    if let Some((se, gate)) = &m.shared {
19120        if se.act != Act::Silu {
19121            return None;
19122        }
19123        let g = gate.as_ref().map_or(1.0, |gate| {
19124            let mut gl = [0.0f32; 1];
19125            gate.matvec(x, &mut gl, pool);
19126            1.0 / (1.0 + (-gl[0]).exp())
19127        });
19128        pairs.push((&se.gate_proj, &se.up_proj));
19129        downs.push(&se.down_proj);
19130        ws.push(g);
19131    }
19132    let inter = pairs[0].0.rows();
19133    let mut gs: Vec<Vec<f32>> = (0..pairs.len()).map(|_| vec![0f32; inter]).collect();
19134    if !QTensor::moe_gate_up_many(&pairs, x, &mut gs, pool) {
19135        return None;
19136    }
19137    let mut out = attention::take_buf(x.len());
19138    if !QTensor::moe_down_many(&downs, &gs, &ws, &mut out, pool) {
19139        attention::recycle_buf(&mut out);
19140        return None;
19141    }
19142    Some(out)
19143}
19144
19145/// Exact CPU completion for the routed experts a dynamic device cache did
19146/// not contain. The weights are already the router's final normalized mix.
19147/// Keeping this independent of `MoeFfn` makes the job `Sync`: its routing
19148/// statistics live in a `RefCell`, while the immutable expert tensors can be
19149/// evaluated safely in parallel with the GPU's resident subset.
19150pub(crate) fn moe_cold_experts_cpu(
19151    experts: &[(&DenseFfn, f32)],
19152    x: &[f32],
19153    pool: Option<&Pool>,
19154) -> Vec<f32> {
19155    let mut out = attention::take_buf(x.len());
19156    if experts.is_empty() {
19157        return out;
19158    }
19159    let pairs: Vec<_> = experts
19160        .iter()
19161        .map(|(e, _)| (&e.gate_proj, &e.up_proj))
19162        .collect();
19163    let downs: Vec<_> = experts.iter().map(|(e, _)| &e.down_proj).collect();
19164    let weights: Vec<_> = experts.iter().map(|(_, w)| *w).collect();
19165    let inter = experts[0].0.gate_proj.rows();
19166    let mut activations: Vec<Vec<f32>> = (0..experts.len()).map(|_| vec![0.0; inter]).collect();
19167    if QTensor::moe_gate_up_many(&pairs, x, &mut activations, pool)
19168        && QTensor::moe_down_many(&downs, &activations, &weights, &mut out, pool)
19169    {
19170        return out;
19171    }
19172    out.fill(0.0);
19173    for &(expert, weight) in experts {
19174        let mut one = dense_ffn(expert, x, pool);
19175        for (o, v) in out.iter_mut().zip(&one) {
19176            *o += weight * v;
19177        }
19178        attention::recycle_buf(&mut one);
19179    }
19180    out
19181}
19182
19183/// Cold part of a short bank batch. Share each expert's weight stream
19184/// across its tokens, but reduce contributions in each token's route order.
19185/// On an unsupported CPU/layout, retain the single-token cold kernels.
19186pub(crate) fn moe_cold_experts_rows_cpu(
19187    jobs: &[Vec<(&DenseFfn, f32)>],
19188    xs: &[f32],
19189    hidden: usize,
19190    pool: Option<&Pool>,
19191) -> Vec<f32> {
19192    let mut out = vec![0.0; xs.len()];
19193    let mut experts: Vec<&DenseFfn> = Vec::new();
19194    let mut groups: Vec<Vec<usize>> = Vec::new();
19195    let mut terms = vec![Vec::new(); jobs.len()];
19196    for (r, row) in jobs.iter().enumerate() {
19197        for &(e, w) in row {
19198            let g = match experts.iter().position(|&d| std::ptr::eq(d, e)) {
19199                Some(g) => g,
19200                None => {
19201                    experts.push(e);
19202                    groups.push(Vec::new());
19203                    groups.len() - 1
19204                }
19205            };
19206            terms[r].push((g, groups[g].len(), w));
19207            groups[g].push(r);
19208        }
19209    }
19210    if experts.is_empty() {
19211        return out;
19212    }
19213    let pairs: Vec<_> = experts.iter().map(|e| (&e.gate_proj, &e.up_proj)).collect();
19214    let downs: Vec<_> = experts.iter().map(|e| &e.down_proj).collect();
19215    let lens: Vec<_> = groups.iter().map(Vec::len).collect();
19216    let count: usize = lens.iter().sum();
19217    let mut acts = vec![vec![0.0; experts[0].gate_proj.rows()]; count];
19218    let mut ds = vec![vec![0.0; hidden]; count];
19219    if QTensor::moe_gate_up_rows(&pairs, &groups, xs, &mut acts, pool)
19220        && QTensor::moe_down_rows(&downs, &lens, &acts, &mut ds, pool)
19221    {
19222        let mut offset = 0;
19223        let offsets: Vec<_> = lens
19224            .iter()
19225            .map(|&n| {
19226                let start = offset;
19227                offset += n;
19228                start
19229            })
19230            .collect();
19231        for (r, terms) in terms.iter().enumerate() {
19232            for &(g, slot, w) in terms {
19233                for (o, &v) in out[r * hidden..(r + 1) * hidden]
19234                    .iter_mut()
19235                    .zip(&ds[offsets[g] + slot])
19236                {
19237                    *o += w * v;
19238                }
19239            }
19240        }
19241    } else {
19242        for (r, jobs) in jobs.iter().enumerate() {
19243            if !jobs.is_empty() {
19244                let mut row = moe_cold_experts_cpu(jobs, &xs[r * hidden..(r + 1) * hidden], pool);
19245                out[r * hidden..(r + 1) * hidden].copy_from_slice(&row);
19246                attention::recycle_buf(&mut row);
19247            }
19248        }
19249    }
19250    out
19251}
19252
19253/// The pure-CPU MoE expert loop (also the fallback of every GPU refusal).
19254fn moe_ffn_cpu(
19255    m: &MoeFfn,
19256    x: &[f32],
19257    idx: &[usize],
19258    p: &[f32],
19259    wsum: f32,
19260    pool: Option<&Pool>,
19261) -> Vec<f32> {
19262    if let Some(out) = moe_ffn_cpu_batched(m, x, idx, p, wsum, pool) {
19263        return out;
19264    }
19265    let mut out = attention::take_buf(x.len());
19266    for &e in idx {
19267        let mut eo = dense_ffn(&m.experts[e], x, pool);
19268        let w = p[e] / wsum * m.per_expert_scale.as_ref().map_or(1.0, |v| v[e]);
19269        for i in 0..out.len() {
19270            out[i] += w * eo[i];
19271        }
19272        attention::recycle_buf(&mut eo);
19273    }
19274    if let Some((se, gate)) = &m.shared {
19275        let mut so = dense_ffn(se, x, pool);
19276        let g = gate.as_ref().map_or(1.0, |gate| {
19277            let mut gl = [0.0f32; 1];
19278            gate.matvec(x, &mut gl, pool);
19279            1.0 / (1.0 + (-gl[0]).exp())
19280        });
19281        for i in 0..out.len() {
19282            out[i] += g * so[i];
19283        }
19284        attention::recycle_buf(&mut so);
19285    }
19286    out
19287}
19288
19289/// DeepSeek-V2 MLA forward, expand-to-MHA form (see `AttnKind::Mla`):
19290/// per token the latent expands to every head's K/V and the ordinary
19291/// cache + grouped attend do the rest. K head layout is [rope | nope]
19292/// (rotary_dim = qk_rope rotates the shared rope key and each q head's
19293/// prefix); V rows are zero-padded to the K head_dim inside the cache
19294/// and the pad is sliced off before O. Attention importance is not
19295/// accumulated for MLA yet (no eviction interplay).
19296#[allow(clippy::too_many_arguments)]
19297pub(crate) fn mla_attention(
19298    w: &MlaWeights,
19299    normed: &[f32],
19300    cache: &mut crate::kv_cache::LayerKvCache,
19301    position: usize,
19302    inv_freq: &[f32],
19303    rope_scale: f32,
19304    eps: f64,
19305    pool: Option<&Pool>,
19306) -> Vec<f32> {
19307    let (nh, dr, dn, dv, lora) = (w.nh, w.qk_rope, w.qk_nope, w.v_dim, w.lora);
19308    let hd = dr + dn;
19309    let mut q = vec![0.0f32; nh * hd];
19310    match (&w.q_a, &w.q_a_norm) {
19311        (Some(qa), Some(qn)) => {
19312            let mut t = vec![0.0f32; qa.rows()];
19313            qa.matvec(normed, &mut t, pool);
19314            let tn = inference::rms_norm(&t, qn, eps, NormStyle::Qwen);
19315            w.q_proj.matvec(&tn, &mut q, pool);
19316        }
19317        _ => w.q_proj.matvec(normed, &mut q, pool),
19318    }
19319    let mut ca = vec![0.0f32; lora + dr];
19320    w.kv_a.matvec(normed, &mut ca, pool);
19321    let (c_lat, k_rope) = ca.split_at_mut(lora);
19322    let latn = inference::rms_norm(c_lat, &w.kv_a_norm, eps, NormStyle::Qwen);
19323    let mut kvb = vec![0.0f32; nh * (dn + dv)];
19324    w.kv_b.matvec(&latn, &mut kvb, pool);
19325    if !w.nope {
19326        attention::rope_rotate_scaled(k_rope, position, inv_freq, rope_scale);
19327    }
19328    for h in 0..nh {
19329        if !w.nope {
19330            attention::rope_rotate_scaled(
19331                &mut q[h * hd..h * hd + dr],
19332                position,
19333                inv_freq,
19334                rope_scale,
19335            );
19336        }
19337    }
19338    let mut k = vec![0.0f32; nh * hd];
19339    let mut v = vec![0.0f32; nh * hd];
19340    for h in 0..nh {
19341        k[h * hd..h * hd + dr].copy_from_slice(k_rope);
19342        k[h * hd + dr..(h + 1) * hd].copy_from_slice(&kvb[h * (dn + dv)..h * (dn + dv) + dn]);
19343        v[h * hd..h * hd + dv].copy_from_slice(&kvb[h * (dn + dv) + dn..(h + 1) * (dn + dv)]);
19344    }
19345    cache.append(&k, &v, &vec![true; nh]);
19346    let (ao, mut imp) = attention::attend_all_heads(&q, cache, nh, 1, hd, w.scale, None, 0.0);
19347    attention::recycle_buf(&mut imp);
19348    let mut ov = vec![0.0f32; nh * dv];
19349    for h in 0..nh {
19350        ov[h * dv..(h + 1) * dv].copy_from_slice(&ao[h * hd..h * hd + dv]);
19351    }
19352    let mut out = vec![0.0f32; w.o_proj.rows()];
19353    w.o_proj.matvec(&ov, &mut out, pool);
19354    out
19355}
19356
19357/// Gemma-4 dual-branch FFN (spec: see `FfnKind::DenseMoe`). The dense
19358/// branch reads the pre-FFN-normed activation; the router and the
19359/// expert branch read the RAW residual — the router through a
19360/// scale-less rms norm (its constant gain is folded into the weights),
19361/// the experts through `pre_norm_2`. CPU path; GPU graphs refuse the
19362/// layer kind honestly.
19363fn dense_moe_ffn(
19364    dm: &DenseMoeFfn,
19365    x_normed: &[f32],
19366    h_raw: &[f32],
19367    eps: f64,
19368    norm_style: NormStyle,
19369    pool: Option<&Pool>,
19370) -> Vec<f32> {
19371    let mut d = dense_ffn(&dm.dense, x_normed, pool);
19372    d = inference::rms_norm(&d, &dm.post_norm_1, eps, norm_style);
19373    let m = &dm.moe;
19374    let ne = m.experts.len();
19375    let mut logits = vec![0.0f32; ne];
19376    if m.router_input_norm {
19377        let ss: f32 = h_raw.iter().map(|v| v * v).sum::<f32>() / h_raw.len() as f32;
19378        let inv = 1.0 / (ss + eps as f32).sqrt();
19379        let xr: Vec<f32> = h_raw.iter().map(|v| v * inv).collect();
19380        m.router.matvec(&xr, &mut logits, pool);
19381    } else {
19382        m.router.matvec(h_raw, &mut logits, pool);
19383    }
19384    let (idx, p, wsum) = moe_route(&logits, m, None);
19385    {
19386        let mut st = m.stats.borrow_mut();
19387        if st.len() < ne {
19388            st.resize(ne, 0);
19389        }
19390        for &e in &idx {
19391            st[e] += 1;
19392        }
19393    }
19394    let x2 = inference::rms_norm(h_raw, &dm.pre_norm_2, eps, norm_style);
19395    let mo = moe_ffn_cpu(m, &x2, &idx, &p, wsum, pool);
19396    let mo = inference::rms_norm(&mo, &dm.post_norm_2, eps, norm_style);
19397    for (di, mi) in d.iter_mut().zip(&mo) {
19398        *di += mi;
19399    }
19400    d
19401}
19402
19403/// Building the MoE-layer GPU jobs: all selected experts (+shared) must
19404/// be q8_2f-Mapped from the primary mapping; otherwise None → CPU path.
19405/// One-shot report of why the MoE GPU block refused. A silent `?` here
19406/// sends every expert to the CPU with nothing in the logs to say so —
19407/// which is exactly how a q4tp MoE model looked "GPU-accelerated" while
19408/// running entirely on the host.
19409fn moe_gpu_refused(why: &'static str) {
19410    use std::sync::atomic::{AtomicBool, Ordering};
19411    static SAID: AtomicBool = AtomicBool::new(false);
19412    if !SAID.swap(true, Ordering::Relaxed) {
19413        tracing::warn!("MoE GPU block refused ({why}) — experts run on the CPU");
19414    }
19415}
19416
19417fn moe_ffn_gpu(
19418    m: &MoeFfn,
19419    x: &[f32],
19420    idx: &[usize],
19421    p: &[f32],
19422    wsum: f32,
19423    pool: Option<&Pool>,
19424) -> Option<Vec<f32>> {
19425    use crate::gpu::MoeJob;
19426
19427    let mut jobs: Vec<MoeJob> = Vec::with_capacity(idx.len() + 1);
19428    let mut model_ref = None;
19429    for &e in idx {
19430        if moe_push_job(&m.experts[e], x, p[e] / wsum, &mut jobs, &mut model_ref).is_none() {
19431            moe_gpu_refused("push_job(expert)");
19432            return None;
19433        }
19434    }
19435    if let Some((se, gate)) = &m.shared {
19436        let g = gate.as_ref().map_or(1.0, |gate| {
19437            let mut gl = [0.0f32; 1];
19438            gate.matvec(x, &mut gl, pool);
19439            1.0 / (1.0 + (-gl[0]).exp())
19440        });
19441        if moe_push_job(se, x, g, &mut jobs, &mut model_ref).is_none() {
19442            moe_gpu_refused("push_job(shared)");
19443            return None;
19444        }
19445    }
19446    let Some(model) = model_ref else {
19447        moe_gpu_refused("no model_ref");
19448        return None;
19449    };
19450    let hidden = jobs[0].down.1;
19451    let mut out = vec![0.0f32; hidden];
19452    if crate::gpu::moe_block(&model, &jobs, &mut out) {
19453        Some(out)
19454    } else {
19455        moe_gpu_refused("gpu::moe_block");
19456        None
19457    }
19458}
19459
19460/// Single-position FFN dispatch.
19461fn ffn_forward(
19462    ffn: &FfnKind,
19463    x: &[f32],
19464    pool: Option<&Pool>,
19465    experts_allowed: Option<&[bool]>,
19466) -> Vec<f32> {
19467    match ffn {
19468        FfnKind::Dense(d) if !d.segs.is_empty() => tube_ffn(d, x, 1, pool, None),
19469        FfnKind::Dense(d) => dense_ffn(d, x, pool),
19470        FfnKind::Moe(m) => moe_ffn(m, x, pool, experts_allowed),
19471        // Dual-branch layers need the raw residual — their callers
19472        // dispatch dense_moe_ffn directly; the auxiliary paths that land
19473        // here (MTP draft, o1 replay) do not co-occur with gemma-4 MoE.
19474        FfnKind::DenseMoe(_) => unreachable!("DenseMoe dispatches via dense_moe_ffn"),
19475    }
19476}
19477
19478/// Fused two-position FFN: gate/up/down streamed once (dense). MoE
19479/// falls back to two singles — expert sets differ per position, there
19480/// is nothing to fuse.
19481fn ffn_forward_pair(
19482    ffn: &FfnKind,
19483    x1: &[f32],
19484    x2: &[f32],
19485    pool: Option<&Pool>,
19486    experts_allowed: Option<&[bool]>,
19487) -> (Vec<f32>, Vec<f32>) {
19488    let d = match ffn {
19489        // A tube layer has nothing to fuse across the pair — the tubes
19490        // are separate matrices; two singles are the honest path.
19491        FfnKind::Dense(d) if !d.segs.is_empty() => {
19492            return (
19493                tube_ffn(d, x1, 1, pool, None),
19494                tube_ffn(d, x2, 1, pool, None),
19495            );
19496        }
19497        FfnKind::Dense(d) => d,
19498        FfnKind::Moe(m) => {
19499            return (
19500                moe_ffn(m, x1, pool, experts_allowed),
19501                moe_ffn(m, x2, pool, experts_allowed),
19502            );
19503        }
19504        FfnKind::DenseMoe(_) => unreachable!("DenseMoe dispatches via dense_moe_ffn"),
19505    };
19506    let inter = d.gate_proj.rows();
19507    FFN_SCRATCH.with(|s| {
19508        let mut s = s.borrow_mut();
19509        let [g1, g2, u1, u2] = &mut *s;
19510        g1.resize(inter, 0.0);
19511        g2.resize(inter, 0.0);
19512        u1.resize(inter, 0.0);
19513        u2.resize(inter, 0.0);
19514        // Multi-matrix pair job: gate+up under one pool dispatch
19515        // (o1s = lane-1 outputs across tensors, o2s = lane-2).
19516        QTensor::matvec2_many(
19517            [&d.gate_proj, &d.up_proj],
19518            x1,
19519            x2,
19520            [g1.as_mut_slice(), u1.as_mut_slice()],
19521            [g2.as_mut_slice(), u2.as_mut_slice()],
19522            pool,
19523        );
19524        for i in 0..inter {
19525            g1[i] = d.act.combine(g1[i], u1[i]);
19526            g2[i] = d.act.combine(g2[i], u2[i]);
19527        }
19528        let mut o1 = attention::take_buf(d.down_proj.rows());
19529        let mut o2 = attention::take_buf(d.down_proj.rows());
19530        d.down_proj.matvec2(g1, g2, &mut o1, &mut o2, pool);
19531        (o1, o2)
19532    })
19533}
19534
19535#[cfg(test)]
19536mod tests {
19537
19538    /// The 0.7.6 prefill-chunk rule: a plain dense stack wholly on a
19539    /// discrete card reads the prompt in wide chunks on x86; every other
19540    /// case keeps the width it had (the GDN-hybrid, MoE and DeepSeek paths
19541    /// were tuned on hardware not measured for this change).
19542    #[test]
19543    fn prefill_chunk_rule_widens_only_dense_on_discrete() {
19544        use super::{
19545            prefill_chunk_rule, ChunkHost, ChunkStackFacts, DISCRETE_DENSE_PREFILL_CHUNK,
19546        };
19547        let dense_card = ChunkStackFacts {
19548            plain_dense: true,
19549            discrete: true,
19550            gpu_on: true,
19551            ..Default::default()
19552        };
19553        assert!(dense_card.dense_on_discrete());
19554        // The bug: a dense Llama on a Vulkan RTX 3090 got 48.
19555        assert_eq!(
19556            prefill_chunk_rule(None, ChunkHost::Other, dense_card.dense_on_discrete()),
19557            DISCRETE_DENSE_PREFILL_CHUNK
19558        );
19559        assert!(DISCRETE_DENSE_PREFILL_CHUNK > 48);
19560        for (label, facts) in [
19561            ("GDN hybrid / MoE / DeepSeek stack", ChunkStackFacts { plain_dense: false, ..dense_card }),
19562            ("integrated GPU", ChunkStackFacts { discrete: false, ..dense_card }),
19563            ("CPU only", ChunkStackFacts { gpu_on: false, discrete: false, ..dense_card }),
19564            ("capacity split", ChunkStackFacts { capacity_split: true, ..dense_card }),
19565            ("multi-GPU plan", ChunkStackFacts { multi_gpu: true, ..dense_card }),
19566            ("O(1) layers", ChunkStackFacts { o1: true, ..dense_card }),
19567        ] {
19568            assert!(!facts.dense_on_discrete(), "{label}");
19569            assert_eq!(
19570                prefill_chunk_rule(None, ChunkHost::Other, facts.dense_on_discrete()),
19571                48,
19572                "{label} keeps the historical x86 chunk"
19573            );
19574        }
19575        // Other hosts are untouched whatever the model.
19576        for dense in [false, true] {
19577            assert_eq!(prefill_chunk_rule(None, ChunkHost::Macos, dense), 512);
19578            assert_eq!(prefill_chunk_rule(None, ChunkHost::Aarch64, dense), 256);
19579        }
19580        // CMF_PREFILL_CHUNK still wins everywhere (and is clamped to ≥ 1).
19581        for host in [ChunkHost::Macos, ChunkHost::Aarch64, ChunkHost::Other] {
19582            for dense in [false, true] {
19583                assert_eq!(prefill_chunk_rule(Some(48), host, dense), 48);
19584                assert_eq!(prefill_chunk_rule(Some(0), host, dense), 1);
19585            }
19586        }
19587    }
19588
19589    #[test]
19590    fn kv_reuse_plan_pulls_rows_decode_wrote_only_on_the_device() {
19591        use super::{ReuseLayer, ReusePlan, kv_reuse_plan};
19592        let full = |host_rows, device_rows| ReuseLayer {
19593            full: true,
19594            host_rows,
19595            device_rows,
19596            device_state: false,
19597        };
19598        // Turn 1: 300-token prompt prefilled on the host, 40 tokens decoded
19599        // by the wgpu graph into the device mirror only. Turn 2 reuses 339.
19600        assert_eq!(
19601            kv_reuse_plan(339, &[full(300, Some(339)), full(300, Some(339))]),
19602            ReusePlan::Pull(vec![(0, 300, 339), (1, 300, 339)])
19603        );
19604        // CPU / Metal: the host owner already holds every forwarded row.
19605        assert_eq!(kv_reuse_plan(339, &[full(339, None)]), ReusePlan::Ready);
19606        // A mirror past the prefix is fine for the host (it gets rewound).
19607        assert_eq!(kv_reuse_plan(339, &[full(339, Some(345))]), ReusePlan::Ready);
19608        // GPU prefix / CPU tail: only the device layers lag.
19609        assert_eq!(
19610            kv_reuse_plan(339, &[full(300, Some(339)), full(339, None)]),
19611            ReusePlan::Pull(vec![(0, 300, 339)])
19612        );
19613        // The device cannot supply the missing rows: never continue.
19614        assert_eq!(kv_reuse_plan(339, &[full(300, Some(320))]), ReusePlan::Fresh);
19615        assert_eq!(kv_reuse_plan(339, &[full(300, None)]), ReusePlan::Fresh);
19616        assert_eq!(kv_reuse_plan(339, &[full(350, None)]), ReusePlan::Fresh);
19617        // A recurrent state advanced on the device cannot be handed to a
19618        // host prefill (it is not rewindable and the host copy is stale).
19619        let conv = |device_state| ReuseLayer {
19620            full: false,
19621            host_rows: 0,
19622            device_rows: None,
19623            device_state,
19624        };
19625        assert_eq!(
19626            kv_reuse_plan(339, &[conv(true), full(300, Some(339))]),
19627            ReusePlan::Fresh
19628        );
19629        assert_eq!(kv_reuse_plan(339, &[conv(false), full(339, None)]), ReusePlan::Ready);
19630    }
19631
19632    #[test]
19633    fn nll_graph_policy_scopes_only_the_fused_head() {
19634        for (label, unmasked, prefer_graph, native_metal, want_graph, want_head) in [
19635            // A Vulkan/Wgpu hidden-only graph remains the quality route.
19636            ("vulkan graph", true, true, false, true, false),
19637            // Native Metal adds the strict fused graph-head contract.
19638            ("native Metal graph", true, true, true, true, true),
19639            // Masked NLL and the explicit non-graph fallback remain unchanged.
19640            ("masked", false, true, false, false, false),
19641            ("graph disabled", true, false, true, false, false),
19642        ] {
19643            let (graph_quality, graph_head_required) =
19644                super::nll_graph_policy(unmasked, prefer_graph, native_metal);
19645            assert_eq!(graph_quality, want_graph, "{label}: graph quality");
19646            assert_eq!(graph_head_required, want_head, "{label}: fused head");
19647        }
19648    }
19649
19650    #[test]
19651    fn mtp_prefill_pair_boundaries_skip_only_final_prompt_row() {
19652        assert_eq!(mtp_prefill_pair_count(0, 128, 256), 128);
19653        assert_eq!(mtp_prefill_pair_count(128, 256, 256), 127);
19654        assert_eq!(mtp_prefill_pair_count(0, 256, 256), 255);
19655        assert_eq!(mtp_prefill_pair_count(256, 256, 256), 0);
19656        assert_eq!(mtp_prefill_pair_count(300, 320, 256), 0);
19657    }
19658
19659    #[test]
19660    fn cancel_flag_stops_generation() {
19661        let mut p = create_test_pipeline(16, 32, 2, 2, 8, 2, 32);
19662        // Set before the call: the prefill loops honour it, the run
19663        // returns immediately with the cancelled reason and no tokens.
19664        p.cancel.store(true, std::sync::atomic::Ordering::Relaxed);
19665        let r = p.generate_from_ids(&[1, 2, 3], 8, None, None).unwrap();
19666        assert_eq!(r.finish_reason, "cancelled");
19667        assert!(
19668            r.token_ids.is_empty(),
19669            "no tokens after cancel: {:?}",
19670            r.token_ids
19671        );
19672        assert_eq!(p.kv_cache.seq_len(), 0);
19673        assert!(p.kv_history.is_empty());
19674        assert!(!p.graph_want_logits);
19675        assert!(p.graph_logits.is_none());
19676        // Flag auto-cleared: the next call generates normally.
19677        let r2 = p.generate_from_ids(&[1, 2, 3], 4, None, None).unwrap();
19678        assert_ne!(r2.finish_reason, "cancelled");
19679    }
19680    use super::*;
19681
19682    #[test]
19683    fn explicit_sliding_schedule_selects_local_rope_profile() {
19684        // Mellum alternates three local 1,024-token layers with one global
19685        // YaRN layer.  An explicit schedule deliberately overrides the
19686        // cadence sentinel in `swa`, so test both RoPE table and scale
19687        // dispatch rather than merely checking that the metadata loaded.
19688        let mut p = create_test_pipeline(32, 64, 4, 1, 8, 4, 32);
19689        p.swa = Some((1024, usize::MAX));
19690        p.sliding_layers = Some(vec![true, true, true, false]);
19691        p.inv_freq = std::sync::Arc::new(vec![0.125; 4]);
19692        p.inv_freq_local = Some(std::sync::Arc::new(vec![0.25; 4]));
19693        p.rope_scale = 1.277_258_9;
19694        p.rope_scale_local = 1.0;
19695
19696        for li in 0..3 {
19697            assert_eq!(p.layer_window(li), Some(1024), "local layer {li}");
19698            assert_eq!(p.layer_inv_freq(li).as_slice(), &[0.25; 4]);
19699            assert_eq!(p.layer_rope_scale(li), 1.0, "local layer {li}");
19700        }
19701        assert_eq!(p.layer_window(3), None);
19702        assert_eq!(p.layer_inv_freq(3).as_slice(), &[0.125; 4]);
19703        assert_eq!(p.layer_rope_scale(3), 1.277_258_9);
19704    }
19705
19706    /// sparse_ffn_quant must equal a dense FFN where inactive neurons are
19707    /// zeroed (mask × mmap correctness). On F32 tensors this is EXACT —
19708    /// it validates the row_dot / add_col_scaled / scatter indexing, the
19709    /// bug-prone part. The q8 branches reuse the golden-tested linear
19710    /// The per-token sparse path reads a transposed `down`; it must
19711    /// agree with the arm that computes everything and zeroes the
19712    /// losers, or the speed measurement is measuring a different model.
19713    #[test]
19714    fn dynamic_ffn_equals_the_zeroing_arm() {
19715        let (hidden, inter) = (8usize, 32usize);
19716        let synth = |n: usize, salt: usize| -> Vec<f32> {
19717            (0..n)
19718                .map(|i| (((i * 29 + salt * 13 + 7) % 89) as f32 / 89.0 - 0.5) * 0.6)
19719                .collect()
19720        };
19721        let down = synth(hidden * inter, 3);
19722        let mut down_t = vec![0.0f32; inter * hidden];
19723        for r in 0..hidden {
19724            for c in 0..inter {
19725                down_t[c * hidden + r] = down[r * inter + c];
19726            }
19727        }
19728        let d = DenseFfn {
19729            gate_proj: QTensor::from_f32(synth(inter * hidden, 1), inter, hidden),
19730            up_proj: QTensor::from_f32(synth(inter * hidden, 2), inter, hidden),
19731            down_proj: QTensor::from_f32(down.clone(), hidden, inter),
19732            act: Act::Silu,
19733            down_t: Some(QTensor::from_f32(down_t, inter, hidden)),
19734            segs: Vec::new(),
19735        };
19736        let x = synth(hidden, 11);
19737        let k = 12usize;
19738        let got = dense_ffn_dynamic(&d, &x, None, k).expect("down_t present");
19739        // Reference: full compute, keep the k loudest |silu(gate)|.
19740        let mut g = vec![0.0f32; inter];
19741        d.gate_proj.matvec(&x, &mut g, None);
19742        let mut u = vec![0.0f32; inter];
19743        d.up_proj.matvec(&x, &mut u, None);
19744        for v in g.iter_mut() {
19745            *v = inference::silu(*v);
19746        }
19747        keep_top_k(&mut g, k);
19748        for i in 0..inter {
19749            g[i] *= u[i];
19750        }
19751        let mut want = vec![0.0f32; hidden];
19752        d.down_proj.matvec(&g, &mut want, None);
19753        for (a, b) in want.iter().zip(&got) {
19754            assert!((a - b).abs() < 1e-5, "dynamic {b} vs reference {a}");
19755        }
19756    }
19757
19758    /// A tube layer is the same layer, re-cut. With every tube open the
19759    /// answer must equal the dense FFN over the concatenated neurons
19760    /// (the permutation is an identity on the layer's function); with a
19761    /// tube closed it must equal the dense FFN with those neurons
19762    /// zeroed — the mask semantics, now paid for in bytes not read.
19763    #[test]
19764    fn tube_ffn_open_equals_dense_and_closed_equals_masked() {
19765        let (hidden, core, tube) = (8usize, 12usize, 8usize);
19766        let inter = core + tube;
19767        let synth = |n: usize, salt: usize| -> Vec<f32> {
19768            (0..n)
19769                .map(|i| (((i * 41 + salt * 17 + 5) % 97) as f32 / 97.0 - 0.5) * 0.5)
19770                .collect()
19771        };
19772        let (g_all, u_all) = (synth(inter * hidden, 1), synth(inter * hidden, 2));
19773        let d_all = synth(hidden * inter, 3);
19774        // The dense layer, and the same weights cut into core + tube.
19775        let dense = DenseFfn {
19776            gate_proj: QTensor::from_f32(g_all.clone(), inter, hidden),
19777            up_proj: QTensor::from_f32(u_all.clone(), inter, hidden),
19778            down_proj: QTensor::from_f32(d_all.clone(), hidden, inter),
19779            act: Act::Silu,
19780            down_t: None,
19781            segs: Vec::new(),
19782        };
19783        let rows =
19784            |v: &[f32], a: usize, b: usize| -> Vec<f32> { v[a * hidden..b * hidden].to_vec() };
19785        let cols = |v: &[f32], a: usize, b: usize| -> Vec<f32> {
19786            let mut o = Vec::with_capacity(hidden * (b - a));
19787            for r in 0..hidden {
19788                o.extend_from_slice(&v[r * inter + a..r * inter + b]);
19789            }
19790            o
19791        };
19792        let tubed = DenseFfn {
19793            down_t: None,
19794            gate_proj: QTensor::from_f32(rows(&g_all, 0, core), core, hidden),
19795            up_proj: QTensor::from_f32(rows(&u_all, 0, core), core, hidden),
19796            down_proj: QTensor::from_f32(cols(&d_all, 0, core), hidden, core),
19797            act: Act::Silu,
19798            segs: vec![FfnSeg {
19799                gate: QTensor::from_f32(rows(&g_all, core, inter), tube, hidden),
19800                up: QTensor::from_f32(rows(&u_all, core, inter), tube, hidden),
19801                down: QTensor::from_f32(cols(&d_all, core, inter), hidden, tube),
19802                start: core,
19803                width: tube,
19804            }],
19805        };
19806        let x = synth(hidden, 7);
19807        let want = dense_ffn(&dense, &x, None);
19808        let got = tube_ffn(&tubed, &x, 1, None, None);
19809        for (a, b) in want.iter().zip(&got) {
19810            assert!((a - b).abs() < 1e-5, "open tube: {a} vs {b}");
19811        }
19812        // Closed tube: bits on for the core, off for the tube.
19813        let mut bits = vec![0u8; inter.div_ceil(8)];
19814        for n in 0..core {
19815            bits[n / 8] |= 1 << (n % 8);
19816        }
19817        let closed = tube_ffn(&tubed, &x, 1, None, Some(&bits));
19818        let masked = dense_ffn_masked(&dense, &x, None, &bits);
19819        for (a, b) in masked.iter().zip(&closed) {
19820            assert!((a - b).abs() < 1e-5, "closed tube: {a} vs {b}");
19821        }
19822        // The batched arm must agree with the single-position one.
19823        let batch = tube_ffn(&tubed, &x, 1, None, Some(&bits));
19824        for (a, b) in closed.iter().zip(&batch) {
19825            assert_eq!(a, b, "batch arm disagrees with decode arm");
19826        }
19827    }
19828
19829    /// scale, structurally identical to the matvec kernels.
19830    #[test]
19831    fn sparse_ffn_quant_equals_dense_with_inactive_zeroed() {
19832        let (hidden, inter) = (16usize, 40usize);
19833        let synth = |n: usize, salt: usize| -> Vec<f32> {
19834            (0..n)
19835                .map(|i| (((i * 37 + salt * 11 + 3) % 101) as f32 / 101.0 - 0.5) * 0.4)
19836                .collect()
19837        };
19838        let d = DenseFfn {
19839            gate_proj: QTensor::from_f32(synth(inter * hidden, 1), inter, hidden),
19840            up_proj: QTensor::from_f32(synth(inter * hidden, 2), inter, hidden),
19841            down_proj: QTensor::from_f32(synth(hidden * inter, 3), hidden, inter),
19842            act: Act::Silu,
19843            down_t: None,
19844            segs: Vec::new(),
19845        };
19846        let x = synth(hidden, 9);
19847        // Active = every 3rd neuron.
19848        let active: Vec<u16> = (0..inter as u16).filter(|i| i % 3 == 0).collect();
19849
19850        let sparse = sparse_ffn_quant(&d, &x, &active, hidden, None);
19851
19852        // Reference: full dense FFN but g[i]=0 for inactive neurons.
19853        let mut g = vec![0.0f32; inter];
19854        d.gate_proj.matvec(&x, &mut g, None);
19855        let mut u = vec![0.0f32; inter];
19856        d.up_proj.matvec(&x, &mut u, None);
19857        let act_set: std::collections::HashSet<u16> = active.iter().copied().collect();
19858        for i in 0..inter {
19859            g[i] = if act_set.contains(&(i as u16)) {
19860                inference::silu(g[i]) * u[i]
19861            } else {
19862                0.0
19863            };
19864        }
19865        let mut reference = vec![0.0f32; hidden];
19866        d.down_proj.matvec(&g, &mut reference, None);
19867
19868        let max_d = sparse
19869            .iter()
19870            .zip(&reference)
19871            .map(|(a, b)| (a - b).abs())
19872            .fold(0.0f32, f32::max);
19873        assert!(max_d < 1e-5, "sparse != dense-zeroed: max|Δ| = {max_d}");
19874    }
19875
19876    /// Attach a synthetic MTP head (same structure as a main layer).
19877    fn attach_test_mtp(p: &mut Pipeline) {
19878        let (h, inter, heads, kv, hd) = (
19879            p.hidden_size,
19880            p.intermediate_size,
19881            p.num_heads,
19882            p.num_kv_heads,
19883            p.head_dim,
19884        );
19885        let synth = |n: usize, salt: usize| -> Vec<f32> {
19886            (0..n)
19887                .map(|i| (((i * 29 + salt * 23 + 5) % 101) as f32 / 101.0 - 0.5) * 0.2)
19888                .collect()
19889        };
19890        let qt = |rows: usize, cols: usize, salt: usize| -> QTensor {
19891            QTensor::from_f32(synth(rows * cols, salt), rows, cols)
19892        };
19893        p.mtp = Some(MtpModule {
19894            enorm: vec![1.0; h],
19895            hnorm: vec![1.0; h],
19896            eh_proj: qt(h, 2 * h, 301),
19897            layer: LayerWeights {
19898                input_norm: vec![1.0; h],
19899                post_norm: vec![1.0; h],
19900                attn_out_norm: None,
19901                ffn_out_norm: None,
19902                layer_scale: None,
19903                ffn: FfnKind::Dense(DenseFfn {
19904                    gate_proj: qt(inter, h, 315),
19905                    up_proj: qt(inter, h, 316),
19906                    down_proj: qt(h, inter, 317),
19907                    act: Act::Silu,
19908                    down_t: None,
19909                    segs: Vec::new(),
19910                }),
19911                attn: AttnKind::Full {
19912                    bias: None,
19913                    wq: qt(heads * hd, h, 311),
19914                    wk: qt(kv * hd, h, 312),
19915                    wv: qt(kv * hd, h, 313),
19916                    wo: qt(h, heads * hd, 314),
19917                    q_norm: None,
19918                    k_norm: None,
19919                    output_gate: false,
19920                    softplus_gate: None,
19921                },
19922            },
19923            final_norm: vec![1.0; h],
19924            kv: crate::kv_cache::LayerKvCache::new(kv, hd),
19925        });
19926    }
19927
19928    #[test]
19929    fn speculative_equals_vanilla_greedy() {
19930        // Speculative decode and the wgpu token graph are mutually
19931        // exclusive; a leaked CMF_GPU=wgpu from a parallel gpu test
19932        // would silently disable drafting. Pin the graph off.
19933        unsafe { std::env::set_var("CMF_GPU_WGPU_GRAPH", "0") };
19934        let run = |spec: bool| {
19935            let mut p = create_test_pipeline(8, 16, 2, 1, 4, 2, 260);
19936            p.sampler_config.temperature = 0.0;
19937            attach_test_mtp(&mut p);
19938            p.speculative = spec;
19939            let r = p.generate("abcdef", 12, None, None).unwrap();
19940            (r.token_ids, r.mtp_drafted, r.mtp_accepted)
19941        };
19942        let (vanilla, d0, _) = run(false);
19943        let (spec, d1, a1) = run(true);
19944        assert_eq!(d0, 0, "vanilla path must not draft");
19945        assert!(d1 > 0, "speculative path must draft");
19946        assert_eq!(
19947            vanilla, spec,
19948            "speculative must reproduce the exact greedy sequence (accepted {a1}/{d1})"
19949        );
19950    }
19951
19952    #[test]
19953    fn speculative_accepts_constant_oracle() {
19954        // See speculative_equals_vanilla_greedy: pin the wgpu graph off.
19955        unsafe { std::env::set_var("CMF_GPU_WGPU_GRAPH", "0") };
19956        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
19957        p.sampler_config.temperature = 0.0;
19958        p.sampler_config.repetition_penalty = 1.0;
19959        // Constant lm_head → every logit equal → both the main model and
19960        // the draft head argmax to token 0: acceptance must be 100%.
19961        p.weights.lm_head = QTensor::from_f32(vec![0.01; 64 * 8], 64, 8);
19962        attach_test_mtp(&mut p);
19963        p.speculative = true;
19964        let r = p.generate("abcd", 10, None, None).unwrap();
19965        assert!(r.mtp_drafted > 0);
19966        assert_eq!(
19967            r.mtp_accepted, r.mtp_drafted,
19968            "constant logits → every draft accepted"
19969        );
19970        // Ties resolve to the same token in both the main and draft
19971        // heads — the sequence is one repeated token.
19972        assert!(r.token_ids.windows(2).all(|w| w[0] == w[1]));
19973    }
19974
19975    #[test]
19976    fn empty_prompt_is_an_error_not_a_panic() {
19977        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 260);
19978        let r = p.generate("", 4, None, None);
19979        assert!(r.is_err(), "empty prompt must be a clean error");
19980    }
19981
19982    #[test]
19983    fn every_token_enters_kv_exactly_once() {
19984        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 2, 260);
19985        // Greedy so no RNG variance; byte tokenizer → 3 prompt tokens.
19986        p.sampler_config.temperature = 0.0;
19987        let r = p.generate("abc", 2, None, None).unwrap();
19988        assert_eq!(r.prompt_tokens, 3);
19989        // prompt(3) + first sampled token forwarded before second logits:
19990        // step0 samples from prefill hidden (no extra forward), then
19991        // forwards t1 → cache 4; step1 samples, loop ends (max_tokens).
19992        assert_eq!(
19993            p.kv_cache.seq_len(),
19994            3 + r.tokens_generated - 1,
19995            "each token must be cached exactly once (v1 cached the last prompt token twice)"
19996        );
19997    }
19998
19999    #[test]
20000    fn generation_is_reproducible_with_seed() {
20001        let run = || {
20002            let mut p = create_test_pipeline(8, 16, 2, 1, 4, 2, 260);
20003            p.generate("hello", 8, None, None).unwrap().token_ids
20004        };
20005        assert_eq!(run(), run());
20006    }
20007
20008    #[test]
20009    fn resetting_sampler_restarts_the_seeded_stream() {
20010        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 2, 260);
20011        let config = SamplerConfig {
20012            seed: Some(1234),
20013            ..SamplerConfig::default()
20014        };
20015        p.set_sampler_config(config.clone());
20016        let first = p.generate("hello", 8, None, None).unwrap().token_ids;
20017        p.set_sampler_config(config);
20018        let second = p.generate("hello", 8, None, None).unwrap().token_ids;
20019        assert_eq!(first, second);
20020    }
20021
20022    #[test]
20023    fn eviction_bounds_the_cache() {
20024        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 260);
20025        p.kv_cache.max_seq_len = 6;
20026        p.sampler_config.temperature = 0.0;
20027        let _ = p.generate("abcd", 12, None, None).unwrap();
20028        assert!(
20029            p.kv_cache.seq_len() <= 6 + 1,
20030            "cache must stay bounded by max_seq_len (got {})",
20031            p.kv_cache.seq_len()
20032        );
20033    }
20034
20035    #[test]
20036    fn confidence_matches_tokens_and_is_a_probability() {
20037        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
20038        p.sampler_config.temperature = 0.0;
20039        p.sampler_config.repetition_penalty = 1.0;
20040        let r = p.generate("abcd", 10, None, None).unwrap();
20041        assert_eq!(
20042            r.token_confidence.len(),
20043            r.token_ids.len(),
20044            "one confidence per emitted token"
20045        );
20046        for &c in &r.token_confidence {
20047            assert!((0.0..=1.0).contains(&c), "confidence out of [0,1]: {c}");
20048        }
20049        // top1_prob is a valid softmax probability.
20050        let logits = [1.0f32, 3.0, 0.5, 3.0];
20051        let p0 = top1_prob_t(&logits, 1, 1.0);
20052        let p1 = top1_prob_t(&logits, 3, 1.0);
20053        assert!((p0 - p1).abs() < 1e-6, "equal logits → equal prob");
20054        assert!(p0 > 0.0 && p0 < 1.0);
20055        // Calibration temperature > 1 softens an over-confident peak.
20056        let sharp = top1_prob_t(&logits, 1, 1.0);
20057        let soft = top1_prob_t(&logits, 1, 2.0);
20058        assert!(soft < sharp, "higher temperature lowers peak confidence");
20059    }
20060
20061    #[test]
20062    fn trace_is_opt_in_and_parallels_the_output() {
20063        // Off by default: the runtime is silent unless observation asked.
20064        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
20065        p.sampler_config.temperature = 0.0;
20066        p.sampler_config.repetition_penalty = 1.0;
20067        let r = p.generate("abcd", 10, None, None).unwrap();
20068        assert!(r.traces.is_empty(), "trace must be empty unless enabled");
20069
20070        // On: exactly one row per emitted token, aligned with the output.
20071        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
20072        p.sampler_config.temperature = 0.0;
20073        p.sampler_config.repetition_penalty = 1.0;
20074        p.set_trace(true);
20075        let r = p.generate("abcd", 10, None, None).unwrap();
20076        assert_eq!(r.traces.len(), r.token_ids.len(), "one trace row per token");
20077        for (i, tr) in r.traces.iter().enumerate() {
20078            assert_eq!(tr.t, i, "trace index is sequential");
20079            assert_eq!(tr.token_id, r.token_ids[i], "trace token_id matches output");
20080            assert_eq!(
20081                tr.confidence, r.token_confidence[i],
20082                "trace confidence matches the confidence channel"
20083            );
20084            // No dynamic router in this pipeline → no skill, no coherence.
20085            assert!(tr.active_skill.is_none() && tr.recon.is_none() && !tr.switched);
20086        }
20087    }
20088
20089    #[test]
20090    fn explain_prefill_logits_match_greedy_first_token() {
20091        // `cortiq explain` shows the next-token distribution from
20092        // prefill_next_logits; its argmax must equal what greedy generate
20093        // actually emits first — otherwise explain would lie.
20094        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
20095        p.sampler_config.temperature = 0.0;
20096        p.sampler_config.repetition_penalty = 1.0;
20097        let ids = p.tokenizer.encode("abcd");
20098        let logits = p.prefill_next_logits(&ids, None);
20099        let argmax = logits
20100            .iter()
20101            .enumerate()
20102            .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
20103            .unwrap()
20104            .0 as u32;
20105        let r = p.generate("abcd", 1, None, None).unwrap();
20106        assert_eq!(
20107            argmax, r.token_ids[0],
20108            "explain preview must match greedy emit"
20109        );
20110    }
20111
20112    #[test]
20113    fn laguna_shared_expert_is_unconditionally_added() {
20114        let matrix = |values: Vec<f32>| QTensor::from_f32(values, 2, 2);
20115        let identity = || matrix(vec![1.0, 0.0, 0.0, 1.0]);
20116        let zero_dense = || DenseFfn {
20117            gate_proj: matrix(vec![0.0; 4]),
20118            up_proj: matrix(vec![0.0; 4]),
20119            down_proj: matrix(vec![0.0; 4]),
20120            act: Act::Silu,
20121            down_t: None,
20122            segs: Vec::new(),
20123        };
20124        let shared = DenseFfn {
20125            gate_proj: identity(),
20126            up_proj: identity(),
20127            down_proj: identity(),
20128            act: Act::Silu,
20129            down_t: None,
20130            segs: Vec::new(),
20131        };
20132        let x = [1.0, 2.0];
20133        let expected = dense_ffn(&shared, &x, None);
20134        let moe = MoeFfn {
20135            router: QTensor::from_f32(vec![0.0, 0.0], 1, 2),
20136            experts: vec![zero_dense()],
20137            top_k: 1,
20138            norm_topk_prob: true,
20139            router_sigmoid: true,
20140            expert_bias: None,
20141            routed_scaling: 1.0,
20142            route_tau: None,
20143            shared: Some((shared, None)),
20144            stats: std::cell::RefCell::new(Vec::new()),
20145            act_sq: std::cell::RefCell::new(Vec::new()),
20146            act_rows: std::cell::RefCell::new(Vec::new()),
20147            mask: None,
20148            per_expert_scale: None,
20149            router_input_norm: false,
20150            resonance: None,
20151            grown: Vec::new(),
20152        };
20153        let actual = moe_ffn_cpu(&moe, &x, &[0], &[0.0], 1.0, None);
20154        for (actual, expected) in actual.iter().zip(expected) {
20155            assert!((actual - expected).abs() < 1e-6);
20156        }
20157    }
20158
20159    /// A tiny MiMo-V2-shaped stack (the M3 fixture): layers [full, sliding,
20160    /// sliding, full]; 4 Q heads over 1 (full) / 2 (sliding) KV heads;
20161    /// head_dim 8 with 4-wide V heads; partial rotary 4 at θ 1e7 (full) /
20162    /// 1e4 (sliding); window 3; learned sinks on the sliding layers; layer
20163    /// 0 a dense FFN, layers 1..3 sigmoid-routed MoE with a selection bias
20164    /// (4 experts, top-2, renormalized, no shared expert). Geometry and
20165    /// sinks go through the same `set_attn_geometry` / `set_layer_sinks`
20166    /// the loader calls.
20167    fn mimo_test_pipeline() -> Pipeline {
20168        let (hs, inter, nh, hd, vd, vocab) = (16usize, 24usize, 4usize, 8usize, 4usize, 64usize);
20169        let kvh = [1usize, 2, 2, 1];
20170        let synth = |n: usize, salt: usize| -> Vec<f32> {
20171            (0..n)
20172                .map(|i| (((i * 31 + salt * 17 + 7) % 97) as f32 / 97.0 - 0.5) * 0.4)
20173                .collect()
20174        };
20175        let qt = |rows: usize, cols: usize, salt: usize| {
20176            QTensor::from_f32(synth(rows * cols, salt), rows, cols)
20177        };
20178        let dense = |inter: usize, salt: usize| DenseFfn {
20179            gate_proj: qt(inter, hs, salt),
20180            up_proj: qt(inter, hs, salt + 1),
20181            down_proj: qt(hs, inter, salt + 2),
20182            act: Act::Silu,
20183            down_t: None,
20184            segs: Vec::new(),
20185        };
20186        let layers: Vec<LayerWeights> = (0..4)
20187            .map(|li| LayerWeights {
20188                input_norm: vec![1.0; hs],
20189                post_norm: vec![1.0; hs],
20190                attn_out_norm: None,
20191                ffn_out_norm: None,
20192                layer_scale: None,
20193                ffn: if li == 0 {
20194                    FfnKind::Dense(dense(inter, 50))
20195                } else {
20196                    FfnKind::Moe(MoeFfn {
20197                        router: qt(4, hs, 60 + li),
20198                        experts: (0..4).map(|e| dense(8, 70 + li * 10 + e * 3)).collect(),
20199                        top_k: 2,
20200                        norm_topk_prob: true,
20201                        router_sigmoid: true,
20202                        expert_bias: Some(vec![0.02, -0.03, 0.01, 0.0]),
20203                        routed_scaling: 1.0,
20204                        route_tau: None,
20205                        shared: None,
20206                        stats: std::cell::RefCell::new(Vec::new()),
20207                        act_sq: std::cell::RefCell::new(Vec::new()),
20208                        act_rows: std::cell::RefCell::new(Vec::new()),
20209                        mask: None,
20210                        per_expert_scale: None,
20211                        router_input_norm: false,
20212                        resonance: None,
20213                        grown: Vec::new(),
20214                    })
20215                },
20216                attn: AttnKind::Full {
20217                    wq: qt(nh * hd, hs, li * 10 + 1),
20218                    wk: qt(kvh[li] * hd, hs, li * 10 + 2),
20219                    wv: qt(kvh[li] * vd, hs, li * 10 + 3),
20220                    wo: qt(hs, nh * vd, li * 10 + 4),
20221                    q_norm: None,
20222                    k_norm: None,
20223                    output_gate: false,
20224                    softplus_gate: None,
20225                    bias: None,
20226                },
20227            })
20228            .collect();
20229        let mut p = Pipeline::new(
20230            Tokenizer::byte_level(),
20231            PipelineWeights {
20232                embed_tokens: qt(vocab, hs, 100),
20233                layers,
20234                lm_head: qt(vocab, hs, 200),
20235                final_norm: vec![1.0; hs],
20236            },
20237            hs,
20238            inter,
20239            nh,
20240            1, // header num_kv_heads (the full layers')
20241            hd,
20242            4,
20243            4,
20244            false,
20245            vocab,
20246            1e-6,
20247            1e7,
20248            NormStyle::Qwen,
20249            4096,
20250            SamplerConfig {
20251                seed: Some(7),
20252                ..Default::default()
20253            },
20254        );
20255        // Diagnostics stay off whatever the test environment exports.
20256        p.layer_dump = None;
20257        p.set_rotary(4, 1e7);
20258        p.sliding_layers = Some(vec![false, true, true, false]);
20259        p.swa = Some((3, usize::MAX));
20260        p.rotary_dim_local = Some(4);
20261        p.inv_freq_local = Some(std::sync::Arc::new(attention::rope_inv_freq(4, 1e4)));
20262        p.set_attn_geometry(Some(kvh.to_vec()), Some(vd)).unwrap();
20263        p.set_layer_sinks(1, vec![0.5, -1.0, 1.5, 0.0]).unwrap();
20264        p.set_layer_sinks(2, vec![-0.25, 2.0, 0.75, -1.5]).unwrap();
20265        p
20266    }
20267
20268    #[test]
20269    fn mimo_embedded_prompt_uses_rows_and_never_reuses_token_only_kv() {
20270        let mut p = mimo_test_pipeline();
20271        p.speculative = false;
20272        p.ignore_eos = true;
20273        p.sampler_config.temperature = 0.0;
20274        p.sampler_config.repetition_penalty = 1.0;
20275        let a = vec![3, 5, 7, 9, 11, 13];
20276        let b = vec![4, 8, 12, 16, 20, 24];
20277        let rows: Vec<_> = b.iter().flat_map(|&id| p.embed_id(id)).collect();
20278        let expected = p.generate_from_ids(&b, 8, None, None).unwrap().token_ids;
20279        // Same placeholder IDs as an earlier request are not a cache key
20280        // for different media. The actual rows, not a re-embedding of a,
20281        // must determine the continuation.
20282        let actual = p.generate_from_embeds(&a, &rows, 8, None, None).unwrap().token_ids;
20283        assert_eq!(actual, expected);
20284        assert!(p.kv_history.is_empty());
20285        let mut extended = a.clone();
20286        extended.push(17);
20287        let after_media = p.generate_from_ids(&extended, 8, None, None).unwrap().token_ids;
20288        p.reset_session();
20289        let fresh = p.generate_from_ids(&extended, 8, None, None).unwrap().token_ids;
20290        assert_eq!(after_media, fresh);
20291        assert!(p.generate_from_embeds(&a, &rows[..rows.len()-1], 1, None, None).is_err());
20292        // Force a real token-prefix reuse opportunity into the media call.
20293        // Those labels are unchanged, but their embeddings now describe a
20294        // different source sequence and every KV row must be rebuilt.
20295        p.reset_session();
20296        p.generate_from_ids(&a, 1, None, None).unwrap();
20297        let mut media_ids = p.kv_history.clone();
20298        assert!(!media_ids.is_empty());
20299        media_ids.extend_from_slice(&[19, 21, 23]);
20300        let source_ids: Vec<_> = (0..media_ids.len()).map(|i| b[i % b.len()]).collect();
20301        let source_rows: Vec<_> = source_ids.iter().flat_map(|&id| p.embed_id(id)).collect();
20302        let mut oracle = mimo_test_pipeline();
20303        oracle.speculative = false;
20304        oracle.ignore_eos = true;
20305        oracle.sampler_config.temperature = 0.0;
20306        oracle.sampler_config.repetition_penalty = 1.0;
20307        let expected = oracle.generate_from_ids(&source_ids, 8, None, None).unwrap().token_ids;
20308        assert_eq!(p.generate_from_embeds(&media_ids, &source_rows, 8, None, None).unwrap().token_ids, expected);
20309        assert!(p.kv_history.is_empty());
20310        let mut bad = rows;
20311        bad[0] = f32::NAN;
20312        assert!(p.generate_from_embeds(&a, &bad, 1, None, None).is_err());
20313    }
20314
20315    fn f32_bits(v: &[f32]) -> Vec<u32> {
20316        v.iter().map(|x| x.to_bits()).collect()
20317    }
20318
20319    /// M3 acceptance: on the MiMo-shaped stack the decode walk (one
20320    /// position at a time through `forward_layers`) and the batched
20321    /// prefill (`prefill_batch_span`, whole prompt and split in two
20322    /// chunks) give bit-identical logits at all 12 positions — per-layer
20323    /// KV heads, narrow V, sinks, the window and the biased sigmoid MoE all
20324    /// agree across the two walks.
20325    #[test]
20326    fn mimo_shaped_decode_matches_prefill_batch_bitwise() {
20327        let mut p = mimo_test_pipeline();
20328        let kv: Vec<usize> = p.kv_cache.layers.iter().map(|l| l.num_kv_heads).collect();
20329        assert_eq!(kv, vec![1, 2, 2, 1]);
20330        assert!(p.kv_cache.layers[0].sinks.is_none() && p.kv_cache.layers[3].sinks.is_none());
20331        assert!(p.kv_cache.layers[1].sinks.is_some() && p.kv_cache.layers[2].sinks.is_some());
20332        let ids: Vec<u32> = (0..12u32).map(|i| (i * 7 + 3) % 64).collect();
20333        let hs = p.hidden_size;
20334        let mut decode = Vec::new();
20335        for (pos, &id) in ids.iter().enumerate() {
20336            let e = p.embed_single(id);
20337            let h = p.forward_layers(&e, pos, None);
20338            decode.push(p.logits_from_hidden(&h));
20339        }
20340        for l in &p.kv_cache.layers {
20341            assert_eq!(l.seq_len, 12);
20342            // V rows are padded to head_dim inside the cache.
20343            assert_eq!(l.head_values(0).len(), 12 * 8);
20344        }
20345        assert!(decode.iter().flatten().all(|v| v.is_finite()));
20346
20347        p.clear_sequence_state();
20348        let hb = p.prefill_batch_span(PrefillIn::Ids(&ids), 0, None, 0, p.num_layers);
20349        for pos in 0..ids.len() {
20350            let lg = p.logits_from_hidden(&hb[pos * hs..(pos + 1) * hs]);
20351            assert_eq!(
20352                f32_bits(&decode[pos]),
20353                f32_bits(&lg),
20354                "whole prompt, pos {pos}"
20355            );
20356        }
20357
20358        p.clear_sequence_state();
20359        let a = p.prefill_batch_span(PrefillIn::Ids(&ids[..5]), 0, None, 0, p.num_layers);
20360        let b = p.prefill_batch_span(PrefillIn::Ids(&ids[5..]), 5, None, 0, p.num_layers);
20361        for pos in 0..ids.len() {
20362            let row = if pos < 5 {
20363                &a[pos * hs..(pos + 1) * hs]
20364            } else {
20365                &b[(pos - 5) * hs..(pos - 4) * hs]
20366            };
20367            let lg = p.logits_from_hidden(row);
20368            assert_eq!(
20369                f32_bits(&decode[pos]),
20370                f32_bits(&lg),
20371                "two chunks, pos {pos}"
20372            );
20373        }
20374
20375        // The fixture is not degenerate: the sinks and the window each
20376        // change the answer.
20377        let last = |p: &mut Pipeline| {
20378            p.clear_sequence_state();
20379            let hb = p.prefill_batch_span(PrefillIn::Ids(&ids), 0, None, 0, p.num_layers);
20380            p.logits_from_hidden(&hb[11 * hs..12 * hs])
20381        };
20382        let base = last(&mut p);
20383        let mut no_sinks = mimo_test_pipeline();
20384        for l in &mut no_sinks.kv_cache.layers {
20385            l.sinks = None;
20386        }
20387        assert_ne!(
20388            f32_bits(&last(&mut no_sinks)),
20389            f32_bits(&base),
20390            "sinks are live"
20391        );
20392        let mut wide = mimo_test_pipeline();
20393        wide.swa = Some((64, usize::MAX));
20394        assert_ne!(
20395            f32_bits(&last(&mut wide)),
20396            f32_bits(&base),
20397            "window is live"
20398        );
20399
20400        // Generation runs end to end on the same stack.
20401        p.clear_sequence_state();
20402        p.ignore_eos = true;
20403        let r = p.generate_from_ids(&ids, 4, None, None).unwrap();
20404        assert_eq!(r.token_ids.len(), 4);
20405    }
20406
20407    /// A Spark-X2.5-shaped stack: layers 0..3 slide (window 6), layer 3
20408    /// is global, every attention carries the head-wise sigmoid g_proj gate.
20409    fn spark_test_pipeline(trim: Option<(usize, usize)>) -> Pipeline {
20410        let (hs, nh) = (16usize, 4usize);
20411        let mut p = create_test_pipeline(hs, 24, nh, 2, 8, 4, 64);
20412        p.layer_dump = None;
20413        p.swa = Some((6, 4));
20414        p.proj_gate_sigmoid = true;
20415        for (li, lw) in p.weights.layers.iter_mut().enumerate() {
20416            if let AttnKind::Full { softplus_gate, .. } = &mut lw.attn {
20417                let g: Vec<f32> = (0..nh * hs)
20418                    .map(|i| (((i * 29 + li * 7) % 83) as f32 / 83.0 - 0.5) * 0.8)
20419                    .collect();
20420                *softplus_gate = Some((QTensor::from_f32(g, nh, hs), true));
20421            }
20422        }
20423        p.swa_trim = trim;
20424        p
20425    }
20426
20427    /// Trimming the sliding tails changes nothing but memory: the same
20428    /// prompt in uneven chunks, a decode walk, and fused pairs with a
20429    /// 2-row rollback give bit-identical hiddens with and without it,
20430    /// while the trimmed sliding layers stay bounded and the global layer
20431    /// keeps every row.
20432    #[test]
20433    fn swa_trim_pipeline_matches_untrimmed_bitwise() {
20434        let mut a = spark_test_pipeline(None);
20435        let mut b = spark_test_pipeline(Some((2, 4)));
20436        assert_eq!(
20437            (0..4).map(|li| b.layer_window(li)).collect::<Vec<_>>(),
20438            vec![Some(6), Some(6), Some(6), None]
20439        );
20440        let ids: Vec<u32> = (0..41u32).map(|i| (i * 11 + 5) % 64).collect();
20441        let mut pos = 0usize;
20442        for &n in [5usize, 13, 7, 16].iter().cycle() {
20443            if pos >= ids.len() {
20444                break;
20445            }
20446            let end = (pos + n).min(ids.len());
20447            let ha = a.prefill_batch_span(PrefillIn::Ids(&ids[pos..end]), pos, None, 0, 4);
20448            let hb = b.prefill_batch_span(PrefillIn::Ids(&ids[pos..end]), pos, None, 0, 4);
20449            assert_eq!(f32_bits(&ha), f32_bits(&hb), "prefill chunk at {pos}");
20450            pos = end;
20451        }
20452        for _ in 0..23 {
20453            let e = a.embed_single(((pos * 7) % 64) as u32);
20454            let ha = a.forward_layers(&e, pos, None);
20455            let hb = b.forward_layers(&e, pos, None);
20456            assert_eq!(f32_bits(&ha), f32_bits(&hb), "decode at {pos}");
20457            for li in 0..3 {
20458                assert!(b.kv_cache.layers[li].seq_len <= 12, "layer {li} bounded");
20459            }
20460            pos += 1;
20461        }
20462        for round in 0..9 {
20463            let (e1, e2) = (a.embed_single(round * 3 + 1), a.embed_single(round * 5 + 2));
20464            let (a1, a2) = a.forward_pair(&e1, &e2, pos);
20465            let (b1, b2) = b.forward_pair(&e1, &e2, pos);
20466            assert_eq!(f32_bits(&a1), f32_bits(&b1), "pair lane 1 round {round}");
20467            assert_eq!(f32_bits(&a2), f32_bits(&b2), "pair lane 2 round {round}");
20468            if round % 2 == 1 {
20469                // A rejected draft: both lanes roll back.
20470                for p in [&mut a, &mut b] {
20471                    for l in &mut p.kv_cache.layers {
20472                        l.truncate_last(2);
20473                    }
20474                }
20475            } else {
20476                pos += 2;
20477            }
20478        }
20479        let e = a.embed_single(9);
20480        let (ha, hb) = (
20481            a.forward_layers(&e, pos, None),
20482            b.forward_layers(&e, pos, None),
20483        );
20484        assert_eq!(f32_bits(&ha), f32_bits(&hb), "after the pairs");
20485        for li in 0..3 {
20486            let (la, lb) = (&a.kv_cache.layers[li], &b.kv_cache.layers[li]);
20487            assert!(lb.base() > 0, "layer {li} trimmed");
20488            assert_eq!(la.base(), 0);
20489            assert_eq!(lb.pos_len(), la.seq_len);
20490            assert_eq!(lb.head_keys(0), &la.head_keys(0)[lb.base() * 8..]);
20491        }
20492        let (ga, gb) = (&a.kv_cache.layers[3], &b.kv_cache.layers[3]);
20493        assert_eq!(
20494            (gb.base(), gb.seq_len),
20495            (0, ga.seq_len),
20496            "the global layer keeps all"
20497        );
20498        assert_eq!(b.kv_cache.seq_len(), a.kv_cache.seq_len());
20499        assert!(b.kv_cache.total_memory_bytes() < a.kv_cache.total_memory_bytes());
20500    }
20501
20502    /// The network split's KvFetch hand-off on a trimmed stack: every
20503    /// layer shipped over the wire mid-sequence (the sliding ones as
20504    /// `FullTail`) into a fresh pipeline that never ran a position, which
20505    /// then decodes on — bit for bit what the untrimmed pipeline decodes.
20506    #[test]
20507    fn swa_trim_wire_handoff_continues_bitwise() {
20508        let mut a = spark_test_pipeline(None);
20509        let mut b = spark_test_pipeline(Some((2, 4)));
20510        let ids: Vec<u32> = (0..29u32).map(|i| (i * 13 + 7) % 64).collect();
20511        let ha = a.prefill_batch_span(PrefillIn::Ids(&ids), 0, None, 0, 4);
20512        let hb = b.prefill_batch_span(PrefillIn::Ids(&ids), 0, None, 0, 4);
20513        assert_eq!(f32_bits(&ha), f32_bits(&hb));
20514        let mut pos = ids.len();
20515        for _ in 0..5 {
20516            let e = a.embed_single(((pos * 5) % 64) as u32);
20517            assert_eq!(
20518                f32_bits(&a.forward_layers(&e, pos, None)),
20519                f32_bits(&b.forward_layers(&e, pos, None))
20520            );
20521            pos += 1;
20522        }
20523        let mut c = spark_test_pipeline(Some((2, 4)));
20524        for li in 0..4 {
20525            let bytes = b.kv_cache.layers[li].export_wire(false).unwrap();
20526            c.kv_cache.layers[li].import_wire(&bytes).unwrap();
20527        }
20528        assert!(c.kv_cache.layers[0].base() > 0, "a tail travelled");
20529        assert_eq!(c.kv_cache.seq_len(), pos);
20530        for step in 0..17 {
20531            let e = a.embed_single(((pos * 3 + 1) % 64) as u32);
20532            let ha = a.forward_layers(&e, pos, None);
20533            let hc = c.forward_layers(&e, pos, None);
20534            assert_eq!(
20535                f32_bits(&ha),
20536                f32_bits(&hc),
20537                "after the hand-off, step {step}"
20538            );
20539            pos += 1;
20540        }
20541        assert!(c.kv_cache.layers[0].seq_len <= 12, "the receiver trims on");
20542    }
20543
20544    /// A synthetic MiMo draft stack of `n` layers for `mimo_test_pipeline`
20545    /// (the SWA geometry of its sliding layers: 2 KV heads, head 8 / V 4).
20546    fn mimo_test_mtp(n: usize, gain: f32) -> mimo_mtp::MimoMtp {
20547        let (hs, inter, nh, hd, vd, nkv) = (16usize, 24usize, 4usize, 8usize, 4usize, 2usize);
20548        let synth = |len: usize, salt: usize| -> Vec<f32> {
20549            (0..len)
20550                .map(|i| (((i * 37 + salt * 13 + 3) % 89) as f32 / 89.0 - 0.5) * 0.6 * gain)
20551                .collect()
20552        };
20553        let qt = |rows: usize, cols: usize, salt: usize| {
20554            QTensor::from_f32(synth(rows * cols, salt), rows, cols)
20555        };
20556        let layers = (0..n)
20557            .map(|k| {
20558                let s = 500 + k * 40;
20559                let mut kv = crate::kv_cache::LayerKvCache::new(nkv, hd);
20560                kv.sinks = Some(vec![0.3, -0.7, 1.1, 0.0]);
20561                MtpModule {
20562                    enorm: vec![1.0; hs],
20563                    hnorm: vec![1.0; hs],
20564                    eh_proj: qt(hs, 2 * hs, s),
20565                    layer: LayerWeights {
20566                        input_norm: vec![1.0; hs],
20567                        post_norm: vec![1.0; hs],
20568                        attn_out_norm: None,
20569                        ffn_out_norm: None,
20570                        layer_scale: None,
20571                        attn: AttnKind::Full {
20572                            wq: qt(nh * hd, hs, s + 1),
20573                            wk: qt(nkv * hd, hs, s + 2),
20574                            wv: qt(nkv * vd, hs, s + 3),
20575                            wo: qt(hs, nh * vd, s + 4),
20576                            q_norm: None,
20577                            k_norm: None,
20578                            output_gate: false,
20579                            softplus_gate: None,
20580                            bias: None,
20581                        },
20582                        ffn: FfnKind::Dense(DenseFfn {
20583                            gate_proj: qt(inter, hs, s + 5),
20584                            up_proj: qt(inter, hs, s + 6),
20585                            down_proj: qt(hs, inter, s + 7),
20586                            act: Act::Silu,
20587                            down_t: None,
20588                            segs: Vec::new(),
20589                        }),
20590                    },
20591                    final_norm: vec![1.0; hs],
20592                    kv,
20593                }
20594            })
20595            .collect();
20596        mimo_mtp::MimoMtp::from_layers(layers)
20597    }
20598
20599    fn mimo_greedy(p: &mut Pipeline, ids: &[u32], n: usize, spec: bool) -> GenerateResult {
20600        p.clear_sequence_state();
20601        p.speculative = spec;
20602        p.ignore_eos = true;
20603        p.sampler_config.temperature = 0.0;
20604        p.generate_from_ids(ids, n, None, None).unwrap()
20605    }
20606
20607    /// The draft stack's incremental rounds (a few rows per layer, last
20608    /// round's provisional rows dropped) give exactly the teacher-forced
20609    /// table of one causal pass per layer over the whole sequence — the
20610    /// table `tools/mimo_ref.py mtp` computes for variant A: layer k, row
20611    /// j reads (x[j+k+1], norm(h_j)) at RoPE position j.
20612    #[test]
20613    fn mimo_mtp_incremental_rounds_equal_the_teacher_forced_table() {
20614        // Both readings of the backbone hidden: pre-final-norm (default)
20615        // and post-final-norm (`CMF_MIMO_MTP_HIDDEN=post`).
20616        for post in [false, true] {
20617            let mut p = mimo_test_pipeline();
20618            // A non-trivial final norm, so the two readings differ.
20619            p.weights.final_norm = (0..p.hidden_size).map(|i| 0.5 + 0.1 * i as f32).collect();
20620            let mut st0 = mimo_test_mtp(3, 1.0);
20621            st0.post_norm_hidden = post;
20622            p.mimo_mtp = Some(st0);
20623            let ids: Vec<u32> = (0..14u32).map(|i| (i * 11 + 5) % 64).collect();
20624            let hs = p.hidden_size;
20625            let hb = p.prefill_batch_span(PrefillIn::Ids(&ids), 0, None, 0, p.num_layers);
20626            p.mimo_note_rows(&hb, 0);
20627            let mut st = p.mimo_mtp.take().unwrap();
20628            // Incremental: one round per t through the decode path (later
20629            // tokens from `ids`, the probe's teacher forcing).
20630            let k = 3;
20631            let mut inc = Vec::new();
20632            for t in 0..ids.len() - k - 1 {
20633                inc.push(p.mimo_mtp_draft(&mut st, t, &ids, k));
20634            }
20635            // Reference: per layer, ONE batched causal pass over all rows
20636            // with fresh caches.
20637            let s = ids.len();
20638            let mut reference = vec![vec![0u32; k]; s - k - 1];
20639            let mut fresh = mimo_test_mtp(3, 1.0);
20640            for (layer, m) in fresh.layers.iter_mut().enumerate() {
20641                let n = s - layer - 1;
20642                let mut cats = vec![0.0f32; n * 2 * hs];
20643                for j in 0..n {
20644                    let e = p.embed_single(ids[j + layer + 1]);
20645                    let raw = &hb[j * hs..(j + 1) * hs];
20646                    let g = if post {
20647                        inference::rms_norm(raw, &p.weights.final_norm, p.rms_eps, p.norm_style)
20648                    } else {
20649                        raw.to_vec()
20650                    };
20651                    let (ce, ch) = cats[j * 2 * hs..(j + 1) * 2 * hs].split_at_mut(hs);
20652                    inference::rms_norm_into(&e, &m.enorm, p.rms_eps, p.norm_style, ce);
20653                    inference::rms_norm_into(&g, &m.hnorm, p.rms_eps, p.norm_style, ch);
20654                }
20655                let mut x = vec![0.0f32; n * hs];
20656                m.eh_proj.matmat(&cats, n, &mut x, None);
20657                p.mimo_mtp_block(m, &mut x, n, 0);
20658                for (t, row) in reference.iter_mut().enumerate() {
20659                    let y = inference::rms_norm(
20660                        &x[t * hs..(t + 1) * hs],
20661                        &m.final_norm,
20662                        p.rms_eps,
20663                        p.norm_style,
20664                    );
20665                    row[layer] = sampler::argmax(&p.lm_head_forward(&y));
20666                }
20667            }
20668            assert_eq!(inc, reference, "post_norm_hidden = {post}");
20669            // Not a degenerate table: the drafts vary.
20670            let distinct: std::collections::HashSet<u32> =
20671                inc.iter().flatten().copied().collect();
20672            assert!(distinct.len() > 3, "{inc:?}");
20673            // Each layer's cache ends holding rows up to the last round start.
20674            let last_t = ids.len() - k - 2;
20675            for m in &st.layers {
20676                assert_eq!(m.kv.seq_len, last_t + 1);
20677            }
20678        }
20679    }
20680
20681    /// Greedy with the MiMo draft stack is the plain greedy stream, token
20682    /// for token — with the real draft layers (low acceptance) and with a
20683    /// drafter that is right most of the time (exercises accepted prefixes
20684    /// of every length, the KV truncation of the rejected rows and the
20685    /// logits hand-off to the loop top), under the default repetition
20686    /// penalty.
20687    #[test]
20688    fn mimo_speculative_greedy_equals_plain_greedy() {
20689        unsafe { std::env::set_var("CMF_GPU_WGPU_GRAPH", "0") };
20690        let ids: Vec<u32> = (0..9u32).map(|i| (i * 7 + 3) % 64).collect();
20691        let n = 24;
20692        let mut p = mimo_test_pipeline();
20693        let plain = mimo_greedy(&mut p, &ids, n, false);
20694        assert_eq!(plain.mtp_drafted, 0);
20695        assert_eq!(plain.token_ids.len(), n);
20696        let plain_kv = p.kv_cache.layers[0].seq_len;
20697
20698        // Real draft layers.
20699        p.mimo_mtp = Some(mimo_test_mtp(3, 1.0));
20700        let spec = mimo_greedy(&mut p, &ids, n, true);
20701        assert!(spec.mtp_drafted > 0, "the round must draft");
20702        assert_eq!(spec.token_ids, plain.token_ids);
20703        assert_eq!(p.kv_cache.layers[0].seq_len, plain_kv);
20704
20705        // A drafter reading the true continuation with every fifth token
20706        // wrong: accepted prefixes of 0..=3 all occur.
20707        let mut truth: Vec<u32> = ids.clone();
20708        truth.extend(&plain.token_ids);
20709        let mut noisy = truth.clone();
20710        for (i, t) in noisy.iter_mut().enumerate() {
20711            if i % 5 == 0 {
20712                *t = (*t + 1) % 64;
20713            }
20714        }
20715        let mut st = mimo_test_mtp(3, 1.0);
20716        st.draft_override = Some(noisy);
20717        p.mimo_mtp = Some(st);
20718        let spec = mimo_greedy(&mut p, &ids, n, true);
20719        assert_eq!(spec.token_ids, plain.token_ids);
20720        assert_eq!(p.kv_cache.layers[0].seq_len, plain_kv);
20721        let stats = p.mimo_mtp.as_ref().unwrap().stats.clone();
20722        assert_eq!(stats.accepted as usize, spec.mtp_accepted);
20723        assert!(spec.mtp_accepted > 0 && spec.mtp_accepted < spec.mtp_drafted);
20724        assert!(
20725            stats.accept_hist.iter().filter(|&&c| c > 0).count() >= 3,
20726            "{:?}",
20727            stats.accept_hist
20728        );
20729        assert!(stats.tokens_per_round() > 1.5, "{}", stats.line());
20730
20731        // A perfect drafter: every draft accepted, rounds of K+1 tokens,
20732        // and the budget is never overrun.
20733        let mut st = mimo_test_mtp(3, 1.0);
20734        st.draft_override = Some(truth);
20735        p.mimo_mtp = Some(st);
20736        let spec = mimo_greedy(&mut p, &ids, n, true);
20737        assert_eq!(spec.token_ids, plain.token_ids);
20738        assert_eq!(spec.mtp_accepted, spec.mtp_drafted);
20739        assert_eq!(p.kv_cache.layers[0].seq_len, plain_kv);
20740
20741        // CMF_MTP=0 path: the stack is attached but idle.
20742        let off = mimo_greedy(&mut p, &ids, n, false);
20743        assert_eq!(off.token_ids, plain.token_ids);
20744        assert_eq!(off.mtp_drafted, 0);
20745    }
20746
20747    /// The wgpu graphs carry MiMo-V2's attention per layer (KV heads,
20748    /// narrow V, sinks, windows, two RoPE tables): no attention-level
20749    /// decline for it any more, and the geometry each layer hands the
20750    /// graph is exactly what the CPU attention reads for that layer. The
20751    /// descriptive reasons stay (the Metal graphs and the q1 dropin still
20752    /// decline on them), and what the per-layer geometry cannot express
20753    /// keeps a named wgpu decline.
20754
20755    #[test]
20756    fn mimo_shaped_model_rides_the_wgpu_graph_geometry() {
20757        let p = mimo_test_pipeline();
20758        assert_eq!(
20759            p.graph_attn_decline_reason(),
20760            Some("per-layer KV head counts")
20761        );
20762        assert_eq!(p.wgpu_graph_attn_decline(), None);
20763        let g0 = p.graph_attn_geom(0).expect("full layer geometry");
20764        assert_eq!(
20765            (g0.nkv, g0.dv, g0.rd, g0.window, g0.sink.is_some()),
20766            (1, 4, 4, None, false)
20767        );
20768        assert_eq!(g0.invf, p.inv_freq.as_slice());
20769        let g1 = p.graph_attn_geom(1).expect("sliding layer geometry");
20770        assert_eq!((g1.nkv, g1.dv, g1.rd, g1.window), (2, 4, 4, Some(3)));
20771        assert_eq!(g1.sink, Some(&[0.5f32, -1.0, 1.5, 0.0][..]));
20772        assert_eq!(g1.invf, p.inv_freq_local.as_ref().unwrap().as_slice());
20773        assert_ne!(g0.invf, g1.invf, "two RoPE tables");
20774        let g3 = p.graph_attn_geom(3).expect("full layer geometry");
20775        assert_eq!((g3.nkv, g3.window, g3.sink.is_some()), (1, None, false));
20776
20777        // No wgpu device in this process: the builders run and decline on
20778        // the (f32, unmapped) experts — never with an attention line.
20779        let emb = p.embed_single(3);
20780        let mut lg = Vec::new();
20781        assert!(
20782            p.try_token_graph_wgpu_steps(&emb, 0, &mut lg, 1, None, None, 0, p.num_layers)
20783                .is_none()
20784        );
20785        let mut hid = emb.clone();
20786        assert_eq!(
20787            p.try_batch_graph_wgpu(&mut hid, &[0], 1, None),
20788            crate::gpu::BatchGraphOutcome::Declined
20789        );
20790        assert_eq!(hid, emb, "a declined batch graph leaves the rows untouched");
20791        assert!(p.try_multi_burst(3, 0, 4).is_none());
20792        assert!(
20793            p.graph_declines().is_empty(),
20794            "no attention decline logged: {:?}",
20795            p.graph_declines()
20796        );
20797        // (No assertion on graph_prefill_preferred: with no attention
20798        // decline it follows the device — a test process that brought a
20799        // wgpu adapter up routes this resident MoE through the graph.)
20800
20801        let plain = || create_test_pipeline(8, 16, 2, 1, 4, 2, 32);
20802        assert_eq!(plain().graph_attn_decline_reason(), None);
20803        assert_eq!(plain().wgpu_graph_attn_decline(), None);
20804        assert!(
20805            plain().graph_attn_geom(0).is_none(),
20806            "uniform models keep the historical arms"
20807        );
20808        let mut q = plain();
20809        q.set_layer_sinks(1, vec![0.25, -0.25]).unwrap();
20810        assert_eq!(
20811            q.graph_attn_decline_reason(),
20812            Some("learned attention sinks")
20813        );
20814        assert_eq!(
20815            q.graph_attn_geom(1).unwrap().sink,
20816            Some(&[0.25f32, -0.25][..])
20817        );
20818        let mut q = plain();
20819        q.set_attn_geometry(None, Some(2)).unwrap();
20820        assert_eq!(
20821            q.graph_attn_decline_reason(),
20822            Some("V heads narrower than Q/K heads")
20823        );
20824        assert_eq!(q.graph_attn_geom(0).unwrap().dv, 2);
20825        let mut q = plain();
20826        q.sliding_layers = Some(vec![true, false]);
20827        q.swa = Some((4, usize::MAX));
20828        assert_eq!(q.graph_attn_decline_reason(), Some("sliding-window layers"));
20829        assert_eq!(q.graph_attn_geom(0).unwrap().window, Some(4));
20830        assert_eq!(q.graph_attn_geom(1).unwrap().window, None);
20831
20832        // YaRN's post-rotation amplitude factor rides the per-layer graph
20833        // geometry. It is intentionally not an angular scale folded into
20834        // `invf`: the WGPU RoPE kernel receives it separately.
20835        let mut q = mimo_test_pipeline();
20836        q.rope_scale = 2.0;
20837        q.rope_scale_local = 1.25;
20838        assert_eq!(q.wgpu_graph_attn_decline(), None);
20839        assert_eq!(q.graph_attn_geom(0).unwrap().rope_scale, 2.0);
20840        assert_eq!(q.graph_attn_geom(1).unwrap().rope_scale, 1.25);
20841
20842        // A uniform YaRN model cannot yet pass the factor through the fused
20843        // RKV arm; decline safely instead of producing scale-1 output.
20844        let mut q = plain();
20845        q.rope_scale = 2.0;
20846        assert_eq!(
20847            q.wgpu_graph_attn_decline(),
20848            Some("scaled RoPE without per-layer geometry")
20849        );
20850    }
20851
20852    #[test]
20853    fn mimo_verify_rewind_preserves_lagging_host_caches() {
20854        let mut p = mimo_test_pipeline();
20855        for (li, layer) in p.kv_cache.layers.iter_mut().enumerate() {
20856            let row = vec![0.0; layer.num_kv_heads * layer.head_dim];
20857            for _ in 0..if li == 0 { 2 } else { 12 } {
20858                layer.append(&row, &row, &[]);
20859            }
20860        }
20861        p.mimo_verify_rewind(9).unwrap();
20862        assert_eq!(p.kv_cache.layers[0].seq_len, 2);
20863        for layer in &p.kv_cache.layers[1..] {
20864            assert_eq!(layer.seq_len, 9);
20865        }
20866    }
20867
20868    /// CMF_LAYER_DUMP: the decode walk and the batched prefill both write
20869    /// every (position, layer) hidden, the two sets agree byte for byte,
20870    /// and the last layer's file is the stack output.
20871    #[test]
20872    fn layer_dump_covers_every_position_and_layer_on_both_walks() {
20873        let dir = std::env::temp_dir().join(format!("cmf-layer-dump-{}", std::process::id()));
20874        let _ = std::fs::remove_dir_all(&dir);
20875        let mut p = mimo_test_pipeline();
20876        let hs = p.hidden_size;
20877        let ids = [5u32, 9, 11, 2, 40];
20878        p.layer_dump = Some(dir.join("decode"));
20879        for (pos, &id) in ids.iter().enumerate() {
20880            let e = p.embed_single(id);
20881            let _ = p.forward_layers(&e, pos, None);
20882        }
20883        p.clear_sequence_state();
20884        p.layer_dump = Some(dir.join("prefill"));
20885        let hb = p.prefill_batch_span(PrefillIn::Ids(&ids), 0, None, 0, p.num_layers);
20886        for pos in 0..ids.len() {
20887            for li in 0..p.num_layers {
20888                let name = format!("p{pos:06}_l{li:02}.f32");
20889                let a = std::fs::read(dir.join("decode").join(&name)).unwrap();
20890                let b = std::fs::read(dir.join("prefill").join(&name)).unwrap();
20891                assert_eq!(a.len(), hs * 4, "{name}");
20892                assert_eq!(a, b, "{name}");
20893            }
20894        }
20895        let last = std::fs::read(dir.join("prefill").join("p000004_l03.f32")).unwrap();
20896        let vals: Vec<f32> = last
20897            .chunks(4)
20898            .map(|c| f32::from_le_bytes(c.try_into().unwrap()))
20899            .collect();
20900        assert_eq!(f32_bits(&vals), f32_bits(&hb[4 * hs..5 * hs]));
20901        let _ = std::fs::remove_dir_all(&dir);
20902    }
20903
20904    #[test]
20905    fn attn_geometry_and_sinks_are_validated() {
20906        let mut p = create_test_pipeline(8, 16, 4, 2, 4, 2, 32);
20907        assert!(
20908            p.set_attn_geometry(Some(vec![2]), None).is_err(),
20909            "one entry per layer"
20910        );
20911        assert!(
20912            p.set_attn_geometry(Some(vec![2, 3]), None).is_err(),
20913            "3 does not divide 4"
20914        );
20915        assert!(p.set_attn_geometry(Some(vec![2, 0]), None).is_err());
20916        assert!(p.set_attn_geometry(None, Some(0)).is_err());
20917        assert!(
20918            p.set_attn_geometry(None, Some(5)).is_err(),
20919            "V wider than the head"
20920        );
20921        p.set_attn_geometry(None, Some(4)).unwrap();
20922        assert_eq!(
20923            p.v_head_dim, None,
20924            "v_head_dim == head_dim is the uniform case"
20925        );
20926        p.set_layer_sinks(1, vec![0.1; 4]).unwrap();
20927        p.set_attn_geometry(Some(vec![1, 4]), None).unwrap();
20928        assert_eq!(p.kv_cache.layers[0].num_kv_heads, 1);
20929        assert_eq!(p.kv_cache.layers[1].num_kv_heads, 4);
20930        assert!(
20931            p.kv_cache.layers[1].sinks.is_some(),
20932            "a reshape keeps the layer's sinks"
20933        );
20934        assert_eq!(p.layer_geom(1).0, 4);
20935        assert!(
20936            p.set_layer_sinks(0, vec![0.0; 3]).is_err(),
20937            "one sink per Q head"
20938        );
20939        assert!(p.set_layer_sinks(7, vec![0.0; 4]).is_err());
20940        assert!(p.set_layer_sinks(0, vec![f32::NAN, 0.0, 0.0, 0.0]).is_err());
20941    }
20942
20943    /// The O(1) Nyström state replaces a plain full-context softmax; it
20944    /// must never be armed on a sliding, sink or narrow-V layer.
20945    #[test]
20946    fn o1_is_never_armed_on_sink_window_or_narrow_v_layers() {
20947        let cfg = || {
20948            Some(crate::nystrom::O1Cfg {
20949                layers: crate::nystrom::O1Layers::All,
20950                m: 4,
20951                w: 8,
20952                sink: 2,
20953                rect: crate::nystrom::O1Rect::Aggregate,
20954            })
20955        };
20956        let mut p = mimo_test_pipeline();
20957        p.set_o1(cfg());
20958        assert!(!p.o1_active(), "every MiMo-shaped layer is ineligible");
20959        let mut q = create_test_pipeline(8, 16, 2, 1, 4, 3, 64);
20960        q.set_layer_sinks(1, vec![0.0, 0.0]).unwrap();
20961        q.sliding_layers = Some(vec![false, false, true]);
20962        q.swa = Some((4, usize::MAX));
20963        q.set_o1(cfg());
20964        assert_eq!(q.o1_flags, vec![true, false, false]);
20965    }
20966
20967    #[test]
20968    fn o1_batch_transition_publishes_one_epoch_before_serial_handoff() {
20969        const B: usize = 19;
20970        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 2, 260);
20971        p.set_o1(Some(crate::nystrom::O1Cfg {
20972            layers: crate::nystrom::O1Layers::All,
20973            m: 4,
20974            w: 8,
20975            sink: 2,
20976            rect: crate::nystrom::O1Rect::Aggregate,
20977        }));
20978        p.o1_begin_with_prefix(Some(B));
20979        let ids: Vec<u32> = (0..B as u32).collect();
20980        let _ = p.prefill_batch_span(PrefillIn::Ids(&ids), 0, None, 0, p.num_layers);
20981
20982        assert_eq!(p.o1_epoch, 1, "all layers publish one completed transition");
20983        assert!(p.kv_cache.layers.iter().all(|l| l.o1_sealed()));
20984        let next = p.embed_single(B as u32);
20985        let _ = p.forward_layers(&next, B, None);
20986        assert_eq!(p.o1_epoch, 1, "sealed handoff must not republish the epoch");
20987    }
20988
20989    #[test]
20990    fn o1_pair_transition_commits_scratch_before_epoch_publication() {
20991        const B: usize = 19;
20992        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 2, 260);
20993        // Keep a real recurrent layer ahead of the Full O(1) layer so the
20994        // pair test observes the GDN lane-2 scratch swap at the same
20995        // boundary, rather than only exercising an artificial scratch vec.
20996        let gdn_cfg = crate::linear_core::GdnCfg {
20997            num_v_heads: 2,
20998            num_k_heads: 1,
20999            key_head_dim: 2,
21000            value_head_dim: 4,
21001            conv_kernel: 3,
21002            hidden_size: 8,
21003            rms_eps: 1e-6,
21004            output_gate_sigmoid: false,
21005        };
21006        let synth = |n: usize, salt: usize| -> Vec<f32> {
21007            (0..n)
21008                .map(|i| (((i * 13 + salt * 7) % 97) as f32 / 97.0 - 0.5) * 0.4)
21009                .collect()
21010        };
21011        let qt = |rows: usize, cols: usize, salt: usize| {
21012            crate::qtensor::QTensor::from_f32(synth(rows * cols, salt), rows, cols)
21013        };
21014        let c_dim = gdn_cfg.conv_dim();
21015        let vd = gdn_cfg.num_v_heads * gdn_cfg.value_head_dim;
21016        p.weights.layers[0].attn = AttnKind::LinearGdn(crate::linear_core::GdnWeights {
21017            in_proj_qkv: qt(c_dim, 8, 1),
21018            in_proj_z: qt(vd, 8, 2),
21019            in_proj_a: qt(gdn_cfg.num_v_heads, 8, 3),
21020            in_proj_b: qt(gdn_cfg.num_v_heads, 8, 4),
21021            conv1d: synth(c_dim * gdn_cfg.conv_kernel, 5),
21022            a_log: vec![0.2, 0.5],
21023            dt_bias: synth(gdn_cfg.num_v_heads, 6),
21024            norm: vec![1.0; gdn_cfg.value_head_dim],
21025            out_proj: qt(8, vd, 7),
21026        });
21027        p.gdn_cfg = Some(gdn_cfg);
21028        p.set_o1(Some(crate::nystrom::O1Cfg {
21029            layers: crate::nystrom::O1Layers::All,
21030            m: 4,
21031            w: 8,
21032            sink: 2,
21033            rect: crate::nystrom::O1Rect::Aggregate,
21034        }));
21035        p.o1_begin_with_prefix(Some(B));
21036        for pos in 0..B - 2 {
21037            let emb = p.embed_single(pos as u32);
21038            let _ = p.forward_layers(&emb, pos, None);
21039        }
21040        let lane1_state = p.kv_cache.layers[0].linear_state.clone();
21041
21042        let e1 = p.embed_single((B - 2) as u32);
21043        let e2 = p.embed_single((B - 1) as u32);
21044        let _ = p.forward_pair(&e1, &e2, B - 2);
21045
21046        assert_eq!(p.o1_epoch, 1, "pair crossing B publishes one epoch");
21047        assert!(
21048            p.kv_cache
21049                .layers
21050                .iter()
21051                .enumerate()
21052                .all(|(li, l)| !p.o1_flags[li] || l.o1_sealed())
21053        );
21054        assert!(!p.kv_cache.layers[0].linear_state.is_empty());
21055        assert_ne!(
21056            p.kv_cache.layers[0].linear_state, lane1_state,
21057            "real pair must commit GDN lane 2 before returning"
21058        );
21059        assert!(p.kv_cache.layers[0].linear_scratch.is_empty());
21060        let next = p.embed_single(B as u32);
21061        let _ = p.forward_layers(&next, B, None);
21062        assert_eq!(p.o1_epoch, 1, "serial continuation must reuse the epoch");
21063    }
21064
21065    #[test]
21066    fn o1_error_observation_stays_terminal_until_reset() {
21067        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
21068        p.set_o1(Some(crate::nystrom::O1Cfg {
21069            layers: crate::nystrom::O1Layers::All,
21070            m: 4,
21071            w: 8,
21072            sink: 2,
21073            rect: crate::nystrom::O1Rect::Aggregate,
21074        }));
21075        p.o1_begin();
21076        p.kv_cache.layers[0].o1_abort("synthetic transition failure".into());
21077
21078        assert!(p.o1_seal_checked().is_err());
21079        assert!(
21080            p.o1_seal_checked().is_err(),
21081            "retry must see the sticky error"
21082        );
21083        let k = vec![0.2f32; 4];
21084        let v = vec![0.3f32; 4];
21085        p.kv_cache.layers[0].append(&k, &v, &[]);
21086        assert_eq!(p.kv_cache.layers[0].seq_len, 0);
21087
21088        p.reset_session();
21089        p.o1_begin();
21090        p.kv_cache.layers[0].append(&k, &v, &[]);
21091        assert_eq!(p.kv_cache.layers[0].seq_len, 1);
21092    }
21093
21094    #[test]
21095    fn nll_graph_failure_is_terminal_and_request_is_reusable() {
21096        let ids = vec![1u32, 2, 3, 4, 5, 6];
21097        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
21098        p.graph_logits = Some(vec![123.0]);
21099        p.graph_want_logits = true;
21100        p.graph_failed
21101            .store(true, std::sync::atomic::Ordering::Relaxed);
21102        p.cancel.store(true, std::sync::atomic::Ordering::Relaxed);
21103        let err = p.nll_ids_from(&ids, 0).expect_err("prior graph failure");
21104        assert!(err.contains("before NLL"));
21105        assert!(p.graph_logits.is_none());
21106        assert!(!p.graph_want_logits);
21107        assert!(!p.graph_failed.load(std::sync::atomic::Ordering::Relaxed));
21108        assert!(!p.cancel.load(std::sync::atomic::Ordering::Relaxed));
21109
21110        let mut fresh = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
21111        let expected = fresh.nll_ids_from(&ids, 0).expect("fresh NLL");
21112        let actual = p.nll_ids_from(&ids, 0).expect("reused NLL");
21113        assert_eq!(actual.1, expected.1);
21114        assert!((actual.0 - expected.0).abs() < 1e-9);
21115    }
21116
21117    #[test]
21118    fn nll_forward_failure_discards_partial_score_and_clears_sidechannels() {
21119        let ids = vec![1u32, 2, 3, 4, 5, 6];
21120        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
21121        p.nll_test_fail_at = Some(1);
21122        let err = p
21123            .nll_ids_from(&ids, 0)
21124            .expect_err("one-shot forward failure");
21125        assert!(err.contains("forward") || err.contains("score row"));
21126        assert!(!p.graph_failed.load(std::sync::atomic::Ordering::Relaxed));
21127        assert!(!p.graph_want_logits);
21128        assert!(p.graph_logits.is_none());
21129        assert!(p.kv_history.is_empty());
21130
21131        let mut fresh = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
21132        let expected = fresh.nll_ids_from(&ids, 0).expect("fresh NLL");
21133        let actual = p.nll_ids_from(&ids, 0).expect("reused NLL");
21134        assert_eq!(actual.1, expected.1);
21135        assert!((actual.0 - expected.0).abs() < 1e-9);
21136    }
21137
21138    #[test]
21139    fn nll_serial_failure_before_first_row_is_reported() {
21140        let ids = vec![1u32, 2, 3, 4];
21141        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
21142        p.nll_test_force_serial = true;
21143        p.nll_test_fail_at = Some(0);
21144        let err = p.nll_ids_from(&ids, 0).expect_err("serial forward failure");
21145        assert!(err.contains("serial forward"));
21146        assert!(p.kv_history.is_empty());
21147        assert!(!p.graph_failed.load(std::sync::atomic::Ordering::Relaxed));
21148        assert!(!p.cancel.load(std::sync::atomic::Ordering::Relaxed));
21149    }
21150
21151    #[test]
21152    fn ffn_probe_failure_discards_recorder_and_state() {
21153        let ids = vec![1u32, 2, 3, 4];
21154        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
21155        p.nll_test_fail_at = Some(0);
21156        let err = p
21157            .probe_ffn_mass_batch(&ids)
21158            .expect_err("probe forward failure");
21159        assert!(err.contains("NLL"));
21160        assert!(FFN_PROBE.with(|probe| probe.borrow().is_none()));
21161        assert!(p.kv_history.is_empty());
21162        assert!(!p.graph_failed.load(std::sync::atomic::Ordering::Relaxed));
21163    }
21164
21165    #[test]
21166    fn nll_test_controls_are_pipeline_scoped() {
21167        let ids = vec![1u32, 2, 3, 4];
21168        let mut failing = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
21169        let mut unaffected = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
21170        failing.nll_test_force_serial = true;
21171        failing.nll_test_fail_at = Some(0);
21172
21173        assert!(!failing.can_prefill_batched());
21174        assert!(unaffected.can_prefill_batched());
21175        let expected = unaffected
21176            .nll_ids_from(&ids, 0)
21177            .expect("unaffected pipeline remains usable");
21178        let err = failing
21179            .nll_ids_from(&ids, 0)
21180            .expect_err("failure injection belongs to failing pipeline");
21181        assert!(err.contains("serial forward"));
21182        assert!(failing.nll_test_fail_at.is_none());
21183        assert!(unaffected.can_prefill_batched());
21184        let actual = unaffected
21185            .nll_ids_from(&ids, 0)
21186            .expect("unaffected pipeline remains reusable");
21187        assert_eq!(actual.1, expected.1);
21188        assert!((actual.0 - expected.0).abs() < 1e-9);
21189    }
21190
21191    #[test]
21192    fn forward_ids_failure_channel_is_terminal_and_reusable() {
21193        let ids = vec![1u32, 2, 3, 4, 5, 6];
21194        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
21195        p.graph_logits = Some(vec![123.0]);
21196        p.graph_want_logits = true;
21197        p.graph_failed
21198            .store(true, std::sync::atomic::Ordering::Relaxed);
21199        p.cancel.store(true, std::sync::atomic::Ordering::Relaxed);
21200
21201        let err = p
21202            .forward_ids(&ids, None)
21203            .expect_err("a failed forward must not become a valid head result");
21204        assert!(err.contains("forward_ids setup"));
21205        assert!(p.graph_logits.is_none());
21206        assert!(!p.graph_want_logits);
21207        assert!(!p.graph_failed.load(std::sync::atomic::Ordering::Relaxed));
21208        assert!(!p.cancel.load(std::sync::atomic::Ordering::Relaxed));
21209        assert_eq!(p.kv_cache.seq_len(), 0);
21210
21211        let expected = create_test_pipeline(8, 16, 2, 1, 4, 1, 64)
21212            .forward_ids(&ids, None)
21213            .expect("fresh forward_ids");
21214        let actual = p
21215            .forward_ids(&ids, None)
21216            .expect("pipeline remains reusable after a failed forward");
21217        assert_eq!(actual.len(), expected.len());
21218        assert!(
21219            actual
21220                .iter()
21221                .zip(expected)
21222                .all(|(a, b)| (a - b).abs() < 1e-9)
21223        );
21224        assert_eq!(p.kv_cache.seq_len(), ids.len());
21225    }
21226
21227    #[test]
21228    fn sigmoid_router_floor_is_explicit_per_architecture() {
21229        // GLM-5's noaux_tc reference uses +1e-20 while the generic
21230        // LFM2-compatible path uses +1e-6.  At low (but representable)
21231        // sigmoid scores, silently sharing the latter changes expert weights
21232        // by orders of magnitude and can make a routed layer look coherent
21233        // while discarding its expert contribution.
21234        let zero = || DenseFfn {
21235            gate_proj: QTensor::from_f32(vec![0.0; 4], 2, 2),
21236            up_proj: QTensor::from_f32(vec![0.0; 4], 2, 2),
21237            down_proj: QTensor::from_f32(vec![0.0; 4], 2, 2),
21238            act: Act::Silu,
21239            down_t: None,
21240            segs: Vec::new(),
21241        };
21242        let m = MoeFfn {
21243            router: QTensor::from_f32(vec![0.0; 4], 2, 2),
21244            experts: vec![zero(), zero()],
21245            top_k: 1,
21246            norm_topk_prob: true,
21247            router_sigmoid: true,
21248            expert_bias: None,
21249            routed_scaling: 2.5,
21250            route_tau: None,
21251            shared: None,
21252            stats: std::cell::RefCell::new(Vec::new()),
21253            act_sq: std::cell::RefCell::new(Vec::new()),
21254            act_rows: std::cell::RefCell::new(Vec::new()),
21255            mask: None,
21256            per_expert_scale: None,
21257            router_input_norm: false,
21258            resonance: None,
21259            grown: Vec::new(),
21260        };
21261        let logits = [-20.0f32, -20.0];
21262        let (_, p, glm_wsum) = moe_route_with_eps(&logits, &m, None, 1e-20);
21263        let (_, _, generic_wsum) = moe_route(&logits, &m, None);
21264        let expected = (p[0] + 1e-20) / m.routed_scaling;
21265        assert!((glm_wsum - expected).abs() < 1e-15);
21266        assert!(generic_wsum > glm_wsum * 100.0);
21267    }
21268
21269    #[test]
21270    fn resonance_scores_match_formula_and_stable_tie() {
21271        let r = Resonance {
21272            // Three descriptors, hidden=2, one projection row each.
21273            mu: vec![0.0, 0.0, 1.0, 0.0, 0.0, 1.0],
21274            u: vec![1.0, 0.0, 0.0, 1.0, 1.0, 1.0],
21275            k: 1,
21276            bias: vec![1.5, 0.5, 0.0],
21277            shell: Vec::new(),
21278        };
21279        let x = [1.0f32, 1.0];
21280        let mut got = vec![0.0; 3];
21281        r.scores(&x, &mut got);
21282        // Expert 0 and 1 are an exact score tie; the CPU top-1 contract uses
21283        // the lower index.  The values also check d² - (U·d)², not just tie
21284        // ordering.
21285        assert!((got[0] - 0.5).abs() < 1e-6);
21286        assert!((got[1] - 0.5).abs() < 1e-6);
21287        assert!(got[2].abs() < 1e-6);
21288        let best = got
21289            .iter()
21290            .enumerate()
21291            .max_by(|(ia, a), (ib, b)| a.partial_cmp(b).unwrap().then(ib.cmp(ia)))
21292            .map(|(i, _)| i);
21293        assert_eq!(best, Some(0));
21294        assert!(got.iter().all(|v| v.is_finite()));
21295    }
21296
21297    /// The growth shell (spec §2): a grown expert whose reconstruction
21298    /// error lies outside its shell scores −∞, one inside keeps the exact
21299    /// resonance score, trunk rows (`+inf` shell) are bit-identical to the
21300    /// shell-less computation; the process-wide switch disables it.
21301    #[test]
21302    fn resonance_shell_masks_outside_keeps_inside_and_trunk_bits() {
21303        // hidden = 2, rank 1. Experts 0/1 = trunk (shell +inf); 2 and 3 =
21304        // grown, the same descriptor (μ = (0, 1), u = (1, 1)) with shells
21305        // 6.0 and 0.25. At x' = (3, 0): d = (3, −1), d² = 10, proj =
21306        // (3 − 1)² = 4, err = 6 exactly — on the boundary of expert 2's
21307        // shell (kept: the rule is strict `>`), outside expert 3's.
21308        let plain = Resonance {
21309            mu: vec![0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0],
21310            u: vec![1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0],
21311            k: 1,
21312            bias: vec![1.5, 0.5, 0.0, 0.0],
21313            shell: Vec::new(),
21314        };
21315        let shelled = Resonance {
21316            mu: plain.mu.clone(),
21317            u: plain.u.clone(),
21318            k: 1,
21319            bias: plain.bias.clone(),
21320            shell: vec![f32::INFINITY, f32::INFINITY, 6.0, 0.25],
21321        };
21322        assert!(!plain.has_shell());
21323        assert!(shelled.has_shell());
21324        let bits = |v: &[f32]| v.iter().map(|x| x.to_bits()).collect::<Vec<_>>();
21325        let (mut a, mut b) = (vec![0.0; 4], vec![0.0; 4]);
21326        set_growth_shell(Some(true));
21327        assert!(growth_shell_enabled());
21328        // x = (1, 1): the grown experts reconstruct it exactly (err 0):
21329        // inside both shells, every row the shell-less bits.
21330        let x = [1.0f32, 1.0];
21331        plain.scores(&x, &mut a);
21332        shelled.scores(&x, &mut b);
21333        assert_eq!(bits(&a), bits(&b), "inside every shell: unchanged");
21334        assert!(a[2] == 0.0 && a[3] == 0.0);
21335        // x' = (3, 0): expert 3 → −∞, expert 2 (err == shell) and the
21336        // trunk rows keep their exact bits.
21337        let xo = [3.0f32, 0.0];
21338        plain.scores(&xo, &mut a);
21339        shelled.scores(&xo, &mut b);
21340        assert_eq!(a[2], -6.0);
21341        assert_eq!(a[3], -6.0);
21342        assert_eq!(bits(&a[..3]), bits(&b[..3]), "trunk rows + the boundary row");
21343        assert_eq!(b[3], f32::NEG_INFINITY, "outside the shell: −∞");
21344        assert_eq!(shelled.effective_shell(4), shelled.shell);
21345        // The switch (`CMF_GROWTH_SHELL=off` / `growth-eval --shell off`):
21346        // all +inf, the shell-less bits everywhere.
21347        set_growth_shell(Some(false));
21348        assert!(!growth_shell_enabled());
21349        assert_eq!(shelled.effective_shell(4), vec![f32::INFINITY; 4]);
21350        shelled.scores(&xo, &mut b);
21351        assert_eq!(bits(&a), bits(&b));
21352        set_growth_shell(None);
21353        // A shell vector shorter than the expert count masks nothing
21354        // beyond it (a legacy layer whose tail has no shell).
21355        let short = Resonance {
21356            shell: vec![f32::INFINITY, f32::INFINITY],
21357            ..shelled
21358        };
21359        set_growth_shell(Some(true));
21360        short.scores(&xo, &mut b);
21361        assert_eq!(bits(&a), bits(&b));
21362        set_growth_shell(None);
21363    }
21364
21365    /// `moe_route` with −∞ logits (a grown expert outside its shell):
21366    /// top-1 is the best finite expert with weight exactly 1.0 on both
21367    /// the softmax and the sigmoid path; all −∞ degrades to uniform.
21368    #[test]
21369    fn moe_route_neg_inf_logits_pick_the_best_finite_expert_with_weight_one() {
21370        let zero = || DenseFfn {
21371            gate_proj: QTensor::from_f32(vec![0.0; 4], 2, 2),
21372            up_proj: QTensor::from_f32(vec![0.0; 4], 2, 2),
21373            down_proj: QTensor::from_f32(vec![0.0; 4], 2, 2),
21374            act: Act::Silu,
21375            down_t: None,
21376            segs: Vec::new(),
21377        };
21378        let moe = |sigmoid: bool| MoeFfn {
21379            router: QTensor::from_f32(vec![0.0; 8], 4, 2),
21380            experts: vec![zero(), zero(), zero(), zero()],
21381            top_k: 1,
21382            norm_topk_prob: true,
21383            router_sigmoid: sigmoid,
21384            expert_bias: None,
21385            routed_scaling: 1.0,
21386            route_tau: None,
21387            shared: None,
21388            stats: std::cell::RefCell::new(Vec::new()),
21389            act_sq: std::cell::RefCell::new(Vec::new()),
21390            act_rows: std::cell::RefCell::new(Vec::new()),
21391            mask: None,
21392            per_expert_scale: None,
21393            router_input_norm: false,
21394            resonance: None,
21395            grown: Vec::new(),
21396        };
21397        let logits = [-1.0f32, f32::NEG_INFINITY, -0.5, f32::NEG_INFINITY];
21398        for sigmoid in [false, true] {
21399            let m = moe(sigmoid);
21400            let (idx, p, wsum) = moe_route(&logits, &m, None);
21401            assert_eq!(idx, vec![2], "sigmoid {sigmoid}");
21402            assert_eq!(p[1], 0.0);
21403            assert_eq!(p[3], 0.0);
21404            assert!(p[2] > p[0] && p[0] > 0.0);
21405            assert!(p.iter().all(|v| v.is_finite()));
21406            let w = p[2] / wsum;
21407            if sigmoid {
21408                // The sigmoid renorm keeps its reference floor (+1e-6).
21409                assert!((w - 1.0).abs() < 1e-5, "sigmoid: weight {w}");
21410            } else {
21411                assert_eq!(w, 1.0, "softmax: the renormalized top-1 weight is exactly 1");
21412            }
21413            // Masked experts stay masked even when they are the only ones
21414            // "admitted" by an allow-list that covers everything.
21415            let (idx, _, _) = moe_route(&logits, &m, Some(&[true, true, true, true]));
21416            assert_eq!(idx, vec![2]);
21417        }
21418        // A finite expert always beats −∞ whatever the bias / order.
21419        let m = moe(false);
21420        let (idx, _, _) = moe_route(&[f32::NEG_INFINITY, f32::NEG_INFINITY, f32::NEG_INFINITY, -9.0], &m, None);
21421        assert_eq!(idx, vec![3]);
21422        // Every expert at −∞ (cannot happen on a grown file — trunk rows
21423        // have no shell): uniform, finite, lowest index.
21424        let (idx, p, wsum) = moe_route(&[f32::NEG_INFINITY; 4], &m, None);
21425        assert_eq!(idx, vec![0]);
21426        assert!(p.iter().all(|&v| v == 0.25));
21427        assert!(wsum.is_finite() && wsum > 0.0);
21428    }
21429
21430    /// The resonance router (top-1) selects by the raw score as the
21431    /// trainer and the graph do — not by softmax probabilities, where two
21432    /// scores closer than 2^-25 collapse to the same `exp(l − max) = 1.0`
21433    /// and the LOWER index wins a token whose score is strictly smaller.
21434    #[test]
21435    fn resonance_route_picks_the_raw_argmax_not_the_softmax_tie() {
21436        let zero = || DenseFfn {
21437            gate_proj: QTensor::from_f32(vec![0.0; 4], 2, 2),
21438            up_proj: QTensor::from_f32(vec![0.0; 4], 2, 2),
21439            down_proj: QTensor::from_f32(vec![0.0; 4], 2, 2),
21440            act: Act::Silu,
21441            down_t: None,
21442            segs: Vec::new(),
21443        };
21444        let moe = |resonant: bool, norm_topk: bool| MoeFfn {
21445            router: QTensor::from_f32(vec![0.0; 6], 3, 2),
21446            experts: vec![zero(), zero(), zero()],
21447            top_k: 1,
21448            norm_topk_prob: norm_topk,
21449            router_sigmoid: false,
21450            expert_bias: None,
21451            routed_scaling: 1.0,
21452            route_tau: None,
21453            shared: None,
21454            stats: std::cell::RefCell::new(Vec::new()),
21455            act_sq: std::cell::RefCell::new(Vec::new()),
21456            act_rows: std::cell::RefCell::new(Vec::new()),
21457            mask: None,
21458            per_expert_scale: None,
21459            router_input_norm: false,
21460            resonance: resonant.then(|| Resonance {
21461                mu: vec![0.0; 6],
21462                u: Vec::new(),
21463                k: 0,
21464                bias: vec![0.0; 3],
21465                shell: Vec::new(),
21466            }),
21467            grown: Vec::new(),
21468        };
21469        // lo = −0.1, hi = the next f32 towards zero: hi − lo = 2^-27 <
21470        // 2^-25, so exp(lo − hi) rounds to exactly 1.0 — a softmax tie.
21471        let lo = -0.1f32;
21472        let hi = f32::from_bits(lo.to_bits() - 1);
21473        assert!(hi > lo && hi - lo < 2f32.powi(-25));
21474        assert_eq!((lo - hi).exp(), 1.0, "the tie this test is about");
21475        // The gated MoE (softmax) path: the tie hands the token to index 0.
21476        let (idx, _, _) = moe_route(&[lo, hi], &moe(false, true), None);
21477        assert_eq!(idx, vec![0], "softmax tie → lower index (the gated contract)");
21478        // The resonance path: the strictly larger raw score wins, weight
21479        // exactly 1.0 with and without norm_topk.
21480        for norm in [true, false] {
21481            let m = moe(true, norm);
21482            let (idx, p, wsum) = moe_route(&[lo, hi, f32::NEG_INFINITY], &m, None);
21483            assert_eq!(idx, vec![1], "norm_topk {norm}");
21484            assert_eq!(p, vec![0.0, 1.0, 0.0]);
21485            assert_eq!(p[1] / wsum, 1.0);
21486            // An exact tie: the first maximum (as `resonance_winner` and
21487            // `embryo_core_route_pick`).
21488            let (idx, _, _) = moe_route(&[hi, hi, lo], &m, None);
21489            assert_eq!(idx, vec![0]);
21490            // `−∞` never wins; the admitted set is honoured.
21491            let (idx, _, _) = moe_route(&[f32::NEG_INFINITY, lo, hi], &m, None);
21492            assert_eq!(idx, vec![2]);
21493            let (idx, p, wsum) = moe_route(&[lo, hi, hi], &m, Some(&[true, false, false]));
21494            assert_eq!(idx, vec![0]);
21495            assert_eq!(p[0] / wsum, 1.0);
21496            // Every admitted expert at −∞: the generic path's uniform
21497            // fallback (lowest index, finite weights).
21498            let (idx, p, wsum) = moe_route(&[f32::NEG_INFINITY; 3], &m, None);
21499            assert_eq!(idx, vec![0]);
21500            assert!(p.iter().all(|v| v.is_finite()) && wsum.is_finite() && wsum > 0.0);
21501        }
21502    }
21503}