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;
11use crate::kv_cache::KvCache;
12use crate::linear_core::{
13    GdnCfg, GdnWeights, ShortConvCfg, ShortConvWeights, VmfPhaseCfg, VmfPhaseWeights, gdn_forward,
14    gdn_pair, short_conv_forward, short_conv_forward_batch, short_conv_pair, vmf_phase_forward,
15    vmf_phase_pair,
16};
17use crate::pool::Pool;
18use crate::qtensor::QTensor;
19use crate::sampler::{self, SamplerConfig, SamplerScratch, SplitMix64};
20use crate::tokenizer::Tokenizer;
21use cortiq_core::mask::TaskMask;
22use cortiq_core::types::NormStyle;
23
24pub static GLOBAL_USE_GPU: std::sync::atomic::AtomicBool =
25    std::sync::atomic::AtomicBool::new(false);
26
27/// Reusable per-pipeline forward scratch: the four norm outputs the
28/// decode paths recompute every layer (single: n1/p1; pair: all four).
29/// Plain buffers, resized once — steady-state decode reuses them.
30struct ForwardScratch {
31    n1: Vec<f32>,
32    n2: Vec<f32>,
33    p1: Vec<f32>,
34    p2: Vec<f32>,
35}
36
37impl ForwardScratch {
38    fn new(hidden: usize) -> Self {
39        Self {
40            n1: vec![0.0; hidden],
41            n2: vec![0.0; hidden],
42            p1: vec![0.0; hidden],
43            p2: vec![0.0; hidden],
44        }
45    }
46}
47
48/// Complete inference pipeline state.
49pub struct Pipeline {
50    /// In-process layer split across local GPUs: (device, first layer,
51    /// last layer) per segment, in execution order. `None` = one device.
52    /// Arc so cloning the plan out of `&mut self` does not fight the
53    /// borrow checker on the hot path.
54    gpu_plan: Option<std::sync::Arc<Vec<(usize, usize, usize)>>>,
55    /// Arc: the server shares one tokenizer handle across request
56    /// handlers without borrowing a pipeline slot.
57    pub tokenizer: std::sync::Arc<Tokenizer>,
58    pub kv_cache: KvCache,
59    pub sampler_config: SamplerConfig,
60    pub weights: PipelineWeights,
61    pub hidden_size: usize,
62    pub intermediate_size: usize,
63    pub num_heads: usize,
64    pub num_kv_heads: usize,
65    pub head_dim: usize,
66    /// Total virtual layers (num_layers × num_loops for looped models).
67    pub num_layers: usize,
68    /// Physical layers in weights.layers (≤ num_layers for looped models).
69    pub physical_layers: usize,
70    /// Looped Transformer: apply final norm after each loop iteration.
71    pub loop_final_norm: bool,
72    pub vocab_size: usize,
73    pub rms_eps: f64,
74    pub rope_base: f32,
75    pub norm_style: NormStyle,
76    /// RoPE dims actually rotated (≤ head_dim; Qwen3.5 uses head_dim/4).
77    pub rotary_dim: usize,
78    /// Optional Q-head count override for each attention layer (Laguna).
79    pub attention_heads_per_layer: Option<Vec<usize>>,
80    /// Linear-core geometry (present when the model has linear layers).
81    pub vmf_cfg: Option<VmfPhaseCfg>,
82    /// GatedDeltaNet geometry (faithful vendor operator).
83    pub gdn_cfg: Option<GdnCfg>,
84    /// MiniCPM-class logit scale (tied lm_head → cannot fold into weights).
85    pub logit_multiplier: Option<f32>,
86    /// Cooperative cancel: set from any thread (FFI `cortiq_cancel`,
87    /// a dropped server connection); the generate loop checks it at
88    /// every prefill chunk and decode step and finishes with
89    /// `finish_reason: "cancelled"`. Auto-cleared when honoured.
90    pub cancel: std::sync::Arc<std::sync::atomic::AtomicBool>,
91    /// Token ids currently materialized in the KV cache (the forwarded
92    /// prompt + all generated tokens except the last, which is sampled
93    /// but not yet forwarded). Lets the next generate call prefill only
94    /// the suffix when a chat app resends the whole history.
95    pub kv_history: Vec<u32>,
96    /// KDA geometry (Kimi Linear / Kimi-K3) — shared by every Kda layer.
97    pub kda_cfg: Option<crate::linear_core::KdaCfg>,
98    /// Gemma-3n stack (AltUp/LAuReL/PLE/KV-sharing): its own forward —
99    /// weights.layers stays empty, the KV caches are the shared ones.
100    pub g3n: Option<Box<(crate::g3n::G3nGlobals, Vec<crate::g3n::G3nLayer>)>>,
101    /// DeepSeek-V4 runs its own stack too: its hidden state is `hc_mult`
102    /// copies of a vector, so no loop written for a single residual
103    /// stream can carry it.
104    pub dsv4: Option<
105        Box<(
106            crate::dsv4::Dsv4Globals,
107            Vec<crate::dsv4::Dsv4Layer>,
108            crate::dsv4::Dsv4Cfg,
109            crate::dsv4::Dsv4State,
110        )>,
111    >,
112    /// Qwen3.8-Flash-Next owns four residual streams plus QSA/PLE state;
113    /// the generic single-residual layer loop cannot represent it.
114    pub qwen4_exp: Option<
115        Box<(
116            crate::qwen4_exp::Globals,
117            Vec<crate::qwen4_exp::Layer>,
118            crate::qwen4_exp::Cfg,
119            crate::qwen4_exp::State,
120        )>,
121    >,
122    /// DeepSeek-V4's own speculation stack: three draft modules, each a full
123    /// layer, plus a confidence head on the last. Empty when the file has
124    /// none, which is the only signal the decode path needs.
125    pub dsv4_mtp: Vec<crate::dsv4::Dsv4Mtp>,
126    /// The draft's per-sequence state (KV rings, captured trunk hidden).
127    pub dspark: Option<crate::dsv4::DsparkState>,
128    /// Drafts awaiting their verdict: (position, proposals, still matching,
129    /// accepted so far).
130    pub dspark_pending: Vec<(usize, Vec<u32>, bool, usize)>,
131    /// Accepted prefix length of every graded draft.
132    pub dspark_hist: Vec<usize>,
133    /// The real tokens the drafts were graded against — a degenerate,
134    /// repeating output would make any acceptance number meaningless, and
135    /// the cheapest guard against believing one is to count them.
136    pub dspark_real: Vec<u32>,
137    /// The trunk's expert picks for the last few tokens, per layer. The
138    /// union over a window of them is what a batched verify would have to
139    /// read, and the ratio to the pick count is all it could save.
140    pub dspark_trunk_picks: Vec<Vec<(usize, Vec<usize>)>>,
141    /// (unique, total) expert picks per draft, trunk side and draft side.
142    pub dspark_exp: Vec<(usize, usize, usize, usize)>,
143    /// Wall time spent in the deliberately out-of-core draft. Kept separate
144    /// from trunk decode so block batching can be judged without conflating
145    /// it with GPU chain variance.
146    pub dspark_draft_ns: u128,
147    /// LFM2 short-convolution geometry (present when the model has
148    /// `ShortConv` mixer layers).
149    pub short_conv_cfg: Option<ShortConvCfg>,
150    /// Multi-token-prediction head (None = absent).
151    pub mtp: Option<MtpModule>,
152    /// Speculative decode via MTP (greedy only; `CMF_MTP=0` disables).
153    pub speculative: bool,
154    rng: SplitMix64,
155    sampler_scratch: SamplerScratch,
156    /// Speculative SAMPLING state (graph_spec_step, temperature > 0): the
157    /// correction token a rejected draft produced — committed by the loop
158    /// top in place of a fresh draw — and the per-round draft
159    /// distributions / target scratch, reused so a round allocates
160    /// nothing at the vocab size.
161    spec_forced: Option<u32>,
162    spec_q: Vec<Vec<f32>>,
163    spec_p: Vec<f32>,
164    spec_res: Vec<f32>,
165    /// The same three for the sparse chain (top-k configs).
166    spec_qs: Vec<sampler::Sparse>,
167    spec_ps: sampler::Sparse,
168    spec_ress: sampler::Sparse,
169    /// Which arm the MTP draft block runs on this generation: Some(true)
170    /// = the whole-token graph (device attention, one submit a step),
171    /// Some(false) = the per-op path; None = not decided yet. Decided
172    /// on the first draft and held, because the two arms keep the MTP
173    /// KV in different places (device mirror vs the CPU cache) and a
174    /// mid-run switch would read the wrong one.
175    mtp_graph_mode: Option<bool>,
176    /// The Metal verify graph of the round in flight, between its sync
177    /// (logits read) and the commit that replays the accepted prefix.
178    #[cfg(target_os = "macos")]
179    metal_verify: Option<MetalVerifyPending>,
180    /// Precomputed RoPE inverse frequencies [head_dim/2]. Arc: the
181    /// forward path clones a handle to escape the &mut self borrow —
182    /// cloning the table itself was a per-forward allocation.
183    pub(crate) inv_freq: std::sync::Arc<Vec<f32>>,
184    /// Reusable norm buffers for the decode hot path (roadmap §3 P0:
185    /// steady-state forward should not heap-allocate). Disjoint field
186    /// from `weights`/`kv_cache`, so split borrows keep working.
187    ws: ForwardScratch,
188    /// Persistent worker pool (None = serial; see CMF_THREADS).
189    pool: Option<std::sync::Arc<Pool>>,
190    // ── Dynamic per-token skill routing (spec §9, claim 14/16) ──
191    /// Source model, retained so a skill switch can re-resolve the
192    /// touched layers' FFN tensors (Mapped = mmap pointers, cheap).
193    pub(crate) model: Option<std::sync::Arc<cortiq_core::CmfModel>>,
194    /// Masks present → weights are dequantized f32 (rebuild path).
195    pub(crate) dyn_force_f32: bool,
196    /// Per-skill FFN layers actually replaced (derived from tensors, not
197    /// the meta `layers` field — ru2 replaces down_proj in 0..23 while
198    /// its meta says [20..23]). None = skill touches non-FFN tensors →
199    /// ineligible for cheap dynamic switching (honest refusal).
200    pub(crate) dyn_skill_layers: Vec<Option<Vec<usize>>>,
201    /// Currently overlaid skill (index into model.header.skills); None =
202    /// backbone. Set at load time to the statically-overlaid skill so
203    /// `set_active_skill(None)` correctly reverts it (else a static
204    /// skill would silently persist — the union-diff assumes dyn_active
205    /// always mirrors the live overlay). Switched by `set_active_skill`.
206    pub(crate) dyn_active: Option<usize>,
207    /// Pipeline was loaded with a soft blend (materialized working
208    /// tensors, not a single skill index) → dynamic routing refuses:
209    /// there is no single index to revert the blend from.
210    pub(crate) dyn_blend_loaded: bool,
211    /// Layer whose post-residual hidden feeds the router φ (shared by
212    /// swarm skills). None = φ capture off.
213    pub(crate) dyn_phi_layer: Option<usize>,
214    /// EMA of φ at `dyn_phi_layer` over the decode window (on-policy).
215    dyn_phi_ema: Vec<f32>,
216    dyn_phi_seen: usize,
217    /// Hysteresis router driving per-token skill switches during decode
218    /// (None = static/no dynamic routing). Taken out during generation.
219    pub dyn_router: Option<crate::swarm::DynRouter>,
220    /// O(1) Nyström attention setting (CLI/env/header-hint resolved by
221    /// the caller; None = plain cache attention everywhere).
222    o1_cfg: Option<crate::nystrom::O1Cfg>,
223    /// Bumped at every o1 seal — the GPU state mirror re-uploads when it
224    /// sees a new epoch (each generate seals fresh CPU state).
225    o1_epoch: u64,
226    /// Per-layer o1 flags derived from `o1_cfg` (Full layers only).
227    o1_flags: Vec<bool>,
228    /// Emit a structured per-token trace (B4 telemetry channel). Off by
229    /// default — the runtime is silent unless observation is requested.
230    trace: bool,
231    /// Confidence-calibration temperature (B1): reported Born mass is
232    /// softmax(logits / calib_temp). 1.0 = raw. Set from header.calibration.
233    calib_temp: f32,
234    /// Process-unique id keying this pipeline's device KV mirrors.
235    #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
236    graph_kv_id: u64,
237    /// Decode asks the token graph to also run final-norm + lm_head on
238    /// the device (drops the separate per-op lm_head round trip).
239    #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
240    graph_want_logits: bool,
241    /// Logits the graph produced for the token just forwarded (taken by
242    /// the decode loop; None = compute on the CPU path).
243    graph_logits: Option<Vec<f32>>,
244    /// Token embeddings are multiplied by this at input (Gemma: √hidden).
245    pub embed_multiplier: f32,
246    /// Attention score scale (1/√head_dim unless the arch overrides —
247    /// Gemma's query_pre_attn_scalar).
248    pub attn_scale: f32,
249    /// Sliding-window attention: (window, every-Nth-layer-is-global
250    /// pattern) — Gemma-3.
251    pub swa: Option<(usize, usize)>,
252    /// Explicit local/global schedule for architectures that cannot be
253    /// represented by Gemma's every-Nth-global convention.
254    pub sliding_layers: Option<Vec<bool>>,
255    /// RoPE table of the sliding (local) layers, when they use their
256    /// own base frequency (Gemma-3: 10k local vs 1M global).
257    pub inv_freq_local: Option<std::sync::Arc<Vec<f32>>>,
258    pub rotary_dim_local: Option<usize>,
259    pub rope_scale: f32,
260    pub rope_scale_local: f32,
261    /// Gemma-4: global layers run their own geometry — (head_dim,
262    /// num_kv_heads); sliding layers keep the base fields.
263    pub global_attn: Option<(usize, usize)>,
264    /// Gemma-4: the global layers' proportional RoPE table (len
265    /// global_head_dim/2, zero-padded tail = identity rotation).
266    pub inv_freq_global: Option<std::sync::Arc<Vec<f32>>>,
267    /// Scale-less RMS normalization of V heads before caching (Gemma-4).
268    pub attn_v_norm: bool,
269    /// Final-logit soft-capping C: logits = C·tanh(logits/C) (Gemma-4).
270    pub final_softcap: Option<f32>,
271    /// Cortiq Embryo hierarchical head: cluster matrix [C, hidden]. The
272    /// flat logits h·Eᵀ are turned into the two-level log-probabilities
273    /// log softmax_c(h·Cᵀ)[c(v)] + log softmax_{s∈c(v)}(h·E_c(v)ᵀ)[v].
274    pub head_clusters: Option<std::sync::Arc<Vec<f32>>>,
275    /// Gemma-2 attention-logit soft-capping (0.0 = off).
276    pub attn_softcap: f32,
277    /// Compute per-token Born confidence (a full-vocab softmax each
278    /// token). On by default; `bench --core` turns it off to match
279    /// llama-bench's core timing.
280    confidence_on: bool,
281}
282
283#[cfg(target_os = "macos")]
284impl Drop for Pipeline {
285    fn drop(&mut self) {
286        crate::gpu::kv_mirror_drop(self.graph_kv_id);
287    }
288}
289
290/// Model weights. Matrices are `QTensor` (owned f32 for small models
291/// and tests — bit-identical to the historical paths — or quantized
292/// bytes zero-copy from the CMF mmap for big models). 1-D norms are
293/// always small and stay f32.
294pub struct PipelineWeights {
295    /// Embedding table: [vocab_size, hidden_size]
296    pub embed_tokens: QTensor,
297    /// Per-layer weights
298    pub layers: Vec<LayerWeights>,
299    /// LM head: [vocab_size, hidden_size]
300    pub lm_head: QTensor,
301    /// Final norm: [hidden_size]
302    pub final_norm: Vec<f32>,
303}
304
305/// One transformer layer: shared norms + MLP, attention by kind.
306pub struct LayerWeights {
307    pub input_norm: Vec<f32>,
308    /// The pre-FFN norm (`post_attention_layernorm` classically;
309    /// `pre_feedforward_layernorm` on Gemma-2/3 sandwich layers).
310    pub post_norm: Vec<f32>,
311    /// Gemma-2/3 sandwich: norm applied to the ATTENTION OUTPUT before
312    /// its residual add (`post_attention_layernorm` there).
313    pub attn_out_norm: Option<Vec<f32>>,
314    /// Gemma-4: the whole layer output is multiplied by this scalar.
315    pub layer_scale: Option<f32>,
316    /// Gemma-2/3 sandwich: norm applied to the FFN OUTPUT before its
317    /// residual add (`post_feedforward_layernorm`).
318    pub ffn_out_norm: Option<Vec<f32>>,
319    pub ffn: FfnKind,
320    pub attn: AttnKind,
321}
322
323/// FFN gate activation: SiLU (SwiGLU family) or tanh-GELU (Gemma's
324/// GeGLU). A property of the model, carried on every FFN triple.
325#[derive(Clone, Copy, PartialEq, Debug, Default)]
326pub enum Act {
327    #[default]
328    Silu,
329    GeluTanh,
330    /// Kimi-K3 SituAndMul: BOTH halves transform —
331    /// a = β·tanh(g/β)·σ(g), up' = linβ·tanh(u/linβ) (linβ>0), out = a·up'.
332    Situ {
333        beta: f32,
334        linear_beta: f32,
335    },
336}
337
338impl Act {
339    pub fn from_arch(name: &str) -> Self {
340        if name == "gelu_tanh" {
341            Self::GeluTanh
342        } else {
343            Self::Silu
344        }
345    }
346
347    /// Arch-driven constructor (activation name + situ betas).
348    pub fn from_arch_full(arch: &cortiq_core::ModelArch) -> Self {
349        match arch.hidden_act.as_str() {
350            "situ" => Self::Situ {
351                beta: arch.activation_situ_beta.unwrap_or(1.0) as f32,
352                linear_beta: arch.activation_situ_linear_beta.unwrap_or(0.0) as f32,
353            },
354            other => Self::from_arch(other),
355        }
356    }
357
358    #[inline]
359    pub fn apply(self, x: f32) -> f32 {
360        match self {
361            Self::Silu => inference::silu(x),
362            Self::GeluTanh => inference::gelu_tanh(x),
363            Self::Situ { beta, .. } => beta * (x / beta).tanh() * (1.0 / (1.0 + (-x).exp())),
364        }
365    }
366
367    /// Gated combine — the FFN contract. Situ transforms the UP half
368    /// too, so callers must use this instead of apply(g)·u.
369    #[inline]
370    pub fn combine(self, g: f32, u: f32) -> f32 {
371        match self {
372            Self::Situ { linear_beta, .. } if linear_beta > 0.0 => {
373                self.apply(g) * (linear_beta * (u / linear_beta).tanh())
374            }
375            _ => self.apply(g) * u,
376        }
377    }
378}
379
380/// Dense gated triple — the FFN of a dense layer or of one expert.
381pub struct DenseFfn {
382    pub gate_proj: QTensor,
383    pub up_proj: QTensor,
384    pub down_proj: QTensor,
385    /// Gate activation (SiLU default; Gemma: tanh-GELU).
386    pub act: Act,
387    /// `down_proj` stored transposed (`[inter, hidden]`), when the file
388    /// carries it. Only the per-token sparse path reads it: a neuron's
389    /// down weights are a contiguous ROW there, so the token's chosen
390    /// neurons are the only bytes touched. `None` = the ordinary layout,
391    /// and the sparse path stays off.
392    pub down_t: Option<QTensor>,
393    /// Task tubes (spec: defragged task-conditional width). The three
394    /// matrices above are the CORE — the neurons every task computes;
395    /// each tube is an independently quantized slice of the SAME layer
396    /// holding the neurons only some tasks need. A tube is a normal
397    /// tensor triple, so every kernel runs it unchanged, and the bytes
398    /// of an inactive tube are never read. Empty = ordinary dense FFN.
399    pub segs: Vec<FfnSeg>,
400}
401
402/// One task tube: a contiguous slice of a layer's FFN neurons, stored
403/// as its own `[w, hidden]` / `[hidden, w]` triple. `start` is the
404/// neuron's index in the layer's FULL space (core first, then tubes in
405/// order) — the bit a task mask sets to switch this tube on.
406pub struct FfnSeg {
407    pub gate: QTensor,
408    pub up: QTensor,
409    pub down: QTensor,
410    pub start: usize,
411    pub width: usize,
412}
413
414/// FFN operator of a layer, decided by tensor presence at load time
415/// (router `mlp.gate.weight` in the directory = MoE layer).
416pub enum FfnKind {
417    Dense(DenseFfn),
418    /// Mixture-of-Experts (Qwen2-MoE / Qwen3-MoE): softmax over ALL
419    /// expert logits → top-k, optional renorm; experts stay quantized
420    /// in mmap — only the selected ones are touched per token.
421    Moe(MoeFfn),
422    /// Gemma-4 MoE: a dense MLP branch AND a routed-expert branch in
423    /// the SAME layer, each with its own norm sandwich. The dense
424    /// branch reads the pre-FFN-normed input; the expert branch (and
425    /// the router) read the RAW residual through `pre_norm_2`:
426    ///   d = post_norm_1(dense(x̂));  m = post_norm_2(Σwₑ·FFNₑ(pre_norm_2(h)))
427    ///   ffn_out = d + m   (the caller's ffn_out_norm + residual follow)
428    DenseMoe(Box<DenseMoeFfn>),
429}
430
431/// Gemma-4 dual-branch FFN (see `FfnKind::DenseMoe`).
432pub struct DenseMoeFfn {
433    pub dense: DenseFfn,
434    pub moe: MoeFfn,
435    /// post_feedforward_layernorm_1 — dense-branch output norm.
436    pub post_norm_1: Vec<f32>,
437    /// pre_feedforward_layernorm_2 — expert-branch input norm (applied
438    /// to the RAW residual, not the pre-FFN-normed activation).
439    pub pre_norm_2: Vec<f32>,
440    /// post_feedforward_layernorm_2 — expert-branch output norm.
441    pub post_norm_2: Vec<f32>,
442}
443
444pub struct MoeFfn {
445    /// Router `mlp.gate.weight` [num_experts, hidden].
446    pub router: QTensor,
447    pub experts: Vec<DenseFfn>,
448    pub top_k: usize,
449    pub norm_topk_prob: bool,
450    /// Router scores per-expert with a sigmoid (LFM2-MoE / DeepSeek-V3
451    /// `noaux_tc`) instead of a softmax over all experts (Qwen).
452    pub router_sigmoid: bool,
453    /// Per-expert selection bias `mlp.expert_bias` [num_experts]
454    /// (LFM2-MoE): added to the sigmoid scores for the top-k CHOICE only;
455    /// the gathered weights use the unbiased scores. None = no bias.
456    pub expert_bias: Option<Vec<f32>>,
457    /// Top-k weights are multiplied by this after the optional renorm
458    /// (LFM2-MoE `routed_scaling_factor`; 1.0 = off).
459    pub routed_scaling: f32,
460    /// Adaptive routing (CMF_MOE_TAU, opt-in): keep the smallest
461    /// prefix of the top-k whose renormalized mass reaches τ —
462    /// confident tokens touch 1–2 experts, flat ones keep all k.
463    /// MoE decode is memory-bound, so skipped experts are skipped
464    /// weight traffic. None = classic fixed top-k (bit-identical).
465    pub route_tau: Option<f32>,
466    /// Always-on shared expert. Qwen2-MoE carries an additional sigmoid
467    /// gate; Laguna adds the shared expert unconditionally (`None`).
468    pub shared: Option<(DenseFfn, Option<QTensor>)>,
469    /// Expert-selection counters (truncated Fisher B-field of claim 12:
470    /// routing frequency during calibration). Filled by every forward,
471    /// read by the CLI via CMF_MOE_STATS. RefCell: decode is single-threaded.
472    pub stats: std::cell::RefCell<Vec<u64>>,
473    /// Per-CHANNEL sum of squares of this FFN's input, accumulated over a
474    /// calibration run (`CMF_RMS_TRACE`). These are the RMS activation
475    /// traces AWNP needs: raw weight magnitude says every channel matters
476    /// equally, and the question AWNP asks is whether the ACTIVATIONS
477    /// disagree. Off unless the env var is set — an f64 add per channel
478    /// per token is cheap, but not free.
479    pub act_sq: std::cell::RefCell<Vec<f64>>,
480    /// Raw FFN-input rows captured for the layers named by `CMF_ACT_DUMP`
481    /// (`"9,19"`). AWNP is nullspace PROJECTION: after dropping channels the
482    /// survivors are refitted to absorb what was removed, and how much they
483    /// can absorb depends on the activation COVARIANCE, not on per-channel
484    /// RMS. Per-channel numbers can only bound the cost from above.
485    pub act_rows: std::cell::RefCell<Vec<f32>>,
486    /// Task mask over routed experts (DTG-MA over MoE, claim-12 B-field
487    /// applied): `false` experts are excluded from selection, the
488    /// softmax renormalizes over the allowed set. Built by the loader
489    /// from CMF_MOE_MASK=<stats.json> + CMF_MOE_MASK_COVER. None = all.
490    pub mask: Option<Vec<bool>>,
491    /// Gemma-4: per-expert weight scale applied AFTER the top-k renorm
492    /// (`router.per_expert_scale`). None = 1.0 everywhere.
493    pub per_expert_scale: Option<Vec<f32>>,
494    /// Gemma-4: the router reads a SCALE-LESS rms-norm of its input
495    /// (the constant gain router.scale·√hidden is folded into the
496    /// router weights at convert time).
497    pub router_input_norm: bool,
498    /// Cortiq Embryo: resonance routing (P1) — the "logits" are
499    /// bias_e − ‖(x−μ_e) − U_eᵀU_e(x−μ_e)‖², argmax = the expert whose
500    /// descriptor reconstructs the input best. `router` is a placeholder.
501    pub resonance: Option<Resonance>,
502}
503
504/// Per-expert resonance descriptors of one MoE layer (`mlp.desc.*`).
505pub struct Resonance {
506    /// [E, hidden]
507    pub mu: Vec<f32>,
508    /// [E, k, hidden] orthonormal directions (k may be 0)
509    pub u: Vec<f32>,
510    pub k: usize,
511    /// [E] selection bias (loss-free balancing, trained online)
512    pub bias: Vec<f32>,
513}
514
515impl Resonance {
516    /// Routing scores for one input row (higher = better).
517    pub fn scores(&self, x: &[f32], out: &mut [f32]) {
518        let h = x.len();
519        let ne = out.len();
520        for e in 0..ne {
521            let mu = &self.mu[e * h..(e + 1) * h];
522            let mut d2 = 0.0f32;
523            for j in 0..h {
524                let d = x[j] - mu[j];
525                d2 += d * d;
526            }
527            let mut proj = 0.0f32;
528            for i in 0..self.k {
529                let u = &self.u[(e * self.k + i) * h..(e * self.k + i + 1) * h];
530                let mut p = 0.0f32;
531                for j in 0..h {
532                    p += (x[j] - mu[j]) * u[j];
533                }
534                proj += p * p;
535            }
536            out[e] = self.bias.get(e).copied().unwrap_or(0.0) - (d2 - proj);
537        }
538    }
539}
540
541/// Attention operator of a layer. Extension point: new operators are
542/// new variants here + a forward in their own module.
543pub enum AttnKind {
544    /// GQA softmax attention (+ optional Qwen3.5 qk-norm / output gate).
545    Full {
546        wq: QTensor,
547        wk: QTensor,
548        wv: QTensor,
549        wo: QTensor,
550        q_norm: Option<Vec<f32>>,
551        k_norm: Option<Vec<f32>>,
552        output_gate: bool,
553        /// Laguna: a separate softplus projection applied to the attention
554        /// output before O. The bool means one scalar per head (broadcast
555        /// across head_dim); false means one scalar per element.
556        softplus_gate: Option<(QTensor, bool)>,
557        /// Qwen2-family projection biases (q, k, v).
558        bias: Option<(Vec<f32>, Vec<f32>, Vec<f32>)>,
559    },
560    /// Canonical linear core (VMF phase attention).
561    Linear(VmfPhaseWeights),
562    /// Faithful vendor linear operator (Qwen3.5 GatedDeltaNet).
563    LinearGdn(GdnWeights),
564    /// LFM2 gated short-convolution mixer (no KV cache; conv ring state
565    /// lives in the layer's `linear_state`).
566    ShortConv(ShortConvWeights),
567    /// DeepSeek-V2 Multi-head Latent Attention. v1 executes it as
568    /// expand-to-MHA: the latent is projected per token, K/V expand to
569    /// every head and live in the ordinary cache (K head layout
570    /// [rope | nope] so the standard partial rotary covers the shared
571    /// rope key; V rows are zero-padded to the K head_dim and the pad
572    /// is sliced off before O). Latent-resident cache is a later
573    /// optimization, not a semantic change.
574    Mla(Box<MlaWeights>),
575    /// Kimi Delta Attention (Kimi Linear / Kimi-K3): per-channel decayed
576    /// delta rule, separate q/k/v short convs, sigmoid-gated output norm.
577    /// State lives in the layer's `linear_state` (no KV cache).
578    Kda(Box<crate::linear_core::KdaWeights>),
579}
580
581/// DeepSeek-V2 MLA projections (see `AttnKind::Mla`).
582pub struct MlaWeights {
583    /// `[nh·(rope+nope), hidden]` (or `[…, q_lora]` when compressed) —
584    /// the converter permutes each head rope-first so rotary_dim =
585    /// qk_rope works unchanged.
586    pub q_proj: QTensor,
587    /// Compressed q (K3/V3 class): x → q_a `[q_lora, hidden]` →
588    /// rms(q_a_norm) → q_proj (= q_b). None = direct q (V2-Lite).
589    pub q_a: Option<QTensor>,
590    pub q_a_norm: Option<Vec<f32>>,
591    /// `kv_a_proj_with_mqa` `[lora + rope, hidden]` (latent first).
592    pub kv_a: QTensor,
593    /// RMS-norm weights over the latent (`kv_a_layernorm`, [lora]).
594    pub kv_a_norm: Vec<f32>,
595    /// `[nh·(nope+v), lora]` — per head [k_nope | v].
596    pub kv_b: QTensor,
597    /// `[hidden, nh·v]`.
598    pub o_proj: QTensor,
599    pub nh: usize,
600    pub qk_rope: usize,
601    pub qk_nope: usize,
602    pub v_dim: usize,
603    pub lora: usize,
604    /// Softmax scale (1/√(rope+nope), YaRN-mscale-corrected at load).
605    pub scale: f32,
606    /// Kimi Linear NoPE: skip the rotary entirely (layout unchanged).
607    pub nope: bool,
608}
609
610/// Multi-token-prediction head (DeepSeek/Qwen style, spec §2.1):
611/// `x = eh_proj·[enorm(embed(next)); hnorm(hidden)]` → one transformer
612/// block over its own KV → shared lm_head. Drafts the token after next;
613/// the main model verifies, so output is exact — MTP only buys speed.
614pub struct MtpModule {
615    pub enorm: Vec<f32>,
616    pub hnorm: Vec<f32>,
617    /// [hidden, 2·hidden]
618    pub eh_proj: QTensor,
619    pub layer: LayerWeights,
620    pub final_norm: Vec<f32>,
621    pub kv: crate::kv_cache::LayerKvCache,
622}
623
624/// A Metal verify graph after its sync: what the commit needs — the
625/// graph (per-layer replay scratch), the GDN layers in encode order (their
626/// CPU states receive the replay), and the attention layers with the CPU
627/// row count they were encoded against (the accepted rows are pulled from
628/// the mirror from there).
629/// One item of the Metal rows-graph plan.
630#[cfg(target_os = "macos")]
631enum MetalRowsItem<'a> {
632    Gdn {
633        run: Vec<crate::gpu_metal::GdnGpuLayer<'a>>,
634        first: usize,
635    },
636    Attn {
637        l: crate::gpu_metal::AttnGpuLayer<'a>,
638        li: usize,
639        q_norm: Option<&'a [f32]>,
640        k_norm: Option<&'a [f32]>,
641        output_gate: bool,
642    },
643}
644
645#[cfg(target_os = "macos")]
646struct MetalVerifyPending {
647    graph: crate::gpu_metal::VerifyGraph,
648    gdn_layers: Vec<usize>,
649    attn_layers: Vec<(usize, usize)>,
650}
651
652/// The speculation trial's phases (see the decode loop): four timed
653/// speculative rounds, eight timed plain tokens, then the faster arm
654/// until a re-check.
655#[derive(Clone, Copy)]
656enum SpecTrial {
657    Spec {
658        t0: std::time::Instant,
659        gen0: usize,
660        rounds: usize,
661    },
662    Plain {
663        t0: std::time::Instant,
664        gen0: usize,
665    },
666    Decided {
667        spec: bool,
668        recheck_at: usize,
669    },
670}
671
672/// The speculation monitor: exponential averages of a round's wall time
673/// and of the tokens it produced, and the plain token's wall time — the
674/// three numbers the keep/stop rule needs. A round pays when
675/// `tokens_per_round · plain_ms > round_ms · 1.03`. The one-shot trial
676/// (four rounds against eight tokens) mis-called prose: the first rounds
677/// after a prompt are formulaic and accept well, the body does not (an
678/// essay measured 39 against a plain 44.8 with the trial saying
679/// "speculate"), so the rule now runs on EVERY round and stops after four
680/// consecutive losing rounds; a stopped speculation is retried 128 tokens
681/// later.
682#[derive(Default, Clone, Copy)]
683struct SpecMon {
684    round_ms: f64,
685    tokens: f64,
686    plain_ms: f64,
687    n: u32,
688    fails: u32,
689}
690
691impl SpecMon {
692    fn round(&mut self, dt_ms: f64, produced: usize) {
693        self.n += 1;
694        if self.n == 1 {
695            return; // round 1 pays the batch scratch and the draft mirror
696        }
697        let a = if self.n == 2 { 1.0 } else { 0.3 };
698        self.round_ms += a * (dt_ms - self.round_ms);
699        self.tokens += a * (produced as f64 - self.tokens);
700    }
701    fn pays(&self) -> bool {
702        self.plain_ms > 0.0 && self.tokens * self.plain_ms > self.round_ms * 1.03
703    }
704}
705
706/// Result of a generation call.
707pub struct GenerateResult {
708    pub text: String,
709    pub token_ids: Vec<u32>,
710    pub prompt_tokens: usize,
711    pub tokens_generated: usize,
712    pub finish_reason: String,
713    /// Speculative-decode stats (0/0 when MTP is absent or inactive).
714    pub mtp_drafted: usize,
715    pub mtp_accepted: usize,
716    /// Per-generated-token confidence = softmax probability of the token
717    /// that was actually emitted (Born mass on the chosen state). High =
718    /// the model was sure; low = it was guessing. Same length as the
719    /// generated slice of `token_ids`.
720    pub token_confidence: Vec<f32>,
721    /// Structured per-token telemetry (B4 channel). Empty unless
722    /// `set_trace(true)`; otherwise same length as the generated slice.
723    pub traces: Vec<TokenTrace>,
724}
725
726/// One row of the structured telemetry trace (B4): the model's internal
727/// routing state at the moment a token was emitted. Every field is a
728/// quantity the runtime already computes — nothing is inferred or
729/// estimated (anti-principle: only measured bytes).
730#[derive(Clone, Debug)]
731pub struct TokenTrace {
732    /// 0-based index within the generated slice.
733    pub t: usize,
734    /// The emitted token id.
735    pub token_id: u32,
736    /// Born mass on the emitted token (softmax prob) — how sure the model was.
737    pub confidence: f32,
738    /// Skill in force while this token was generated (None = backbone).
739    pub active_skill: Option<String>,
740    /// Recon error E = ‖r−BBᵀr‖²/‖φ‖² at the last routing eval — coherence
741    /// with the active skill's subspace (low = coherent). None = no router
742    /// or not yet evaluated.
743    pub recon: Option<f32>,
744    /// The router changed the active skill right after this token (a
745    /// domain boundary crossed under the hysteresis barrier).
746    pub switched: bool,
747}
748
749/// Calibrated softmax probability of `id` under `logits` (the Born mass on
750/// the emitted token) — the confidence signal, cheap from logits already
751/// computed for sampling. `temp` is the calibration temperature (B1):
752/// softmax(logits / temp); 1.0 = raw.
753#[cfg_attr(not(test), allow(dead_code))]
754fn top1_prob_t(logits: &[f32], id: u32, temp: f32) -> f32 {
755    let t = if temp > 1e-3 { temp } else { 1.0 };
756    let max = logits.iter().fold(f32::NEG_INFINITY, |m, &v| m.max(v));
757    let sum: f32 = logits.iter().map(|&v| ((v - max) / t).exp()).sum();
758    if sum > 0.0 {
759        (((logits[id as usize] - max) / t).exp()) / sum
760    } else {
761        0.0
762    }
763}
764
765/// prefill-GEMM enabled? (CMF_PREFILL=seq — emergency fallback to the
766/// sequential path.)
767fn prefill_batched() -> bool {
768    std::env::var("CMF_PREFILL")
769        .map(|v| v != "seq")
770        .unwrap_or(true)
771}
772
773/// Input to the layer-major batched span walk: token ids (embeds itself,
774/// full-stack and coordinator prefill) or ready boundary hiddens (the
775/// network worker's side of a split).
776#[derive(Clone, Copy)]
777enum PrefillIn<'a> {
778    Ids(&'a [u32]),
779    Hidden(&'a [f32]),
780}
781
782/// The batched prefill walks `weights.layers`. Architectures that load
783/// their own stack (gemma-3n's AltUp replicas, DeepSeek-V4's hyper-
784/// connections) leave that empty and must go position by position — asking
785/// otherwise indexes an empty vector, which is a panic rather than a
786/// fallback. Every call site goes through here so the next such
787/// architecture is one line, not four.
788impl Pipeline {
789    fn can_prefill_batched(&self) -> bool {
790        prefill_batched() && !self.weights.layers.is_empty()
791    }
792}
793
794/// Prefill chunk (positions per batched pass). On macOS the AMX GEMM
795/// path wants tall panels — M=48 starves the matrix units (ggml uses
796/// ubatch 512); elsewhere the historical 48 stays. CMF_PREFILL_CHUNK
797/// overrides. Pub: the network split MUST chunk identically to the
798/// local path — panel width reorders float accumulation, so a different
799/// chunk is a different (equally valid) generation.
800pub fn prefill_chunk() -> usize {
801    if let Some(n) = std::env::var("CMF_PREFILL_CHUNK")
802        .ok()
803        .and_then(|v| v.parse::<usize>().ok())
804    {
805        return n.max(1);
806    }
807    if cfg!(target_os = "macos") {
808        512
809    } else if cfg!(target_arch = "aarch64") {
810        // Mobile: big enough to feed the batched attend (gate b ≥ 32)
811        // and the blocked SDOT GEMM without the memory of 512.
812        256
813    } else {
814        48
815    }
816}
817
818/// Callback for streaming tokens. Return `false` to cancel.
819pub type TokenCallback = Box<dyn FnMut(&str) -> bool + Send>;
820
821impl Pipeline {
822    /// Map a virtual layer index to its physical weight index.
823    /// Looped Transformer (Nanbeige 4.2): 22 physical layers × 2 loops = 44 virtual;
824    /// virtual layer 23 maps back to physical layer 1 (23 % 22 = 1).
825    #[inline]
826    pub fn phys_layer(&self, virtual_idx: usize) -> usize {
827        virtual_idx % self.physical_layers
828    }
829
830    /// True when `virtual_idx` is the last layer of a loop iteration
831    /// (used for loop_final_norm insertion).
832    #[inline]
833    pub fn is_loop_end(&self, virtual_idx: usize) -> bool {
834        self.loop_final_norm && (virtual_idx + 1) % self.physical_layers == 0
835    }
836
837    /// Build a pipeline from parts (used by the loader and tests).
838    #[allow(clippy::too_many_arguments)]
839
840    /// Whole-block q1 token graph on the GPU (macOS/Metal): the run of
841    /// consecutive q1 layers — GDN *and* full attention — starting at
842    /// `start` executes as few command buffers as the CPU truly needs.
843    /// Hidden stays device-resident across every layer; the only syncs
844    /// are before each CPU attend (it needs q/k/v and owns the KV
845    /// cache) and the final hidden readback. Recurrent states
846    /// round-trip through shared memory (the CPU stays their owner, so
847    /// every other path remains coherent). Returns the first layer
848    /// index NOT covered (== `start` → refused, caller falls through
849    /// to the per-layer CPU path).
850    /// Should prefill run position-by-position through the GPU token
851    /// graph instead of the batched CPU chunk-GEMM? True for q1 GDN
852    /// hybrids on native Metal: their chunk prefill is walled by the
853    /// sequential scalar recurrence, so the graph's decode rate wins.
854    /// NOT for Looped Transformers, despite the per-chunk loop_final_norm
855    /// sync: the chunk-GEMM amortizes each weight over the whole chunk,
856    /// which the per-position graph cannot (Nanbeige 4.2 on M4, 512-token
857    /// prompt: 85 tok/s chunked vs 14 through the graph).
858    #[cfg(target_os = "macos")]
859    fn graph_prefill_preferred(&self) -> bool {
860        if !crate::gpu::enabled_here()
861            || !crate::gpu::q1_force()
862            || std::env::var("CMF_GPU_BLOCK")
863                .map(|v| v == "0")
864                .unwrap_or(false)
865            // CMF_PREFILL_GRAPH=0: the chunked prefill (GEMM projections,
866            // CPU recurrence) instead of the per-position token graph.
867            || std::env::var("CMF_PREFILL_GRAPH").as_deref() == Ok("0")
868        {
869            return false;
870        }
871        self.weights
872            .layers
873            .iter()
874            .any(|lw| matches!(&lw.attn, AttnKind::LinearGdn(w) if w.in_proj_qkv.is_q1()))
875    }
876
877    #[cfg(not(target_os = "macos"))]
878    fn graph_prefill_preferred(&self) -> bool {
879        // Discrete-GPU wgpu whole-token graph: GDN layers carry recurrent state
880        // (conv ring + delta-rule S) resident on the GPU. A batched CPU prefill
881        // builds that state on the CPU only, leaving the GPU buffers zeroed at
882        // decode → garbage. Route GDN-hybrid prefill through the graph one
883        // position at a time so the resident state is seeded exactly as decode
884        // will read it. Pure-attention models keep the batched CPU prefill (its
885        // KV mirror re-syncs from the CPU cache, so no seeding gap).
886        let graph_on = crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Prefill);
887        if !graph_on || !crate::gpu::enabled_here() {
888            return false;
889        }
890        // O(1) needs the CPU prefill: the q-trace that seals the Nyström
891        // skeleton is recorded there and nowhere else. The GDN half of
892        // the hybrid loses nothing — the graph's first decode creates
893        // its (ring, S) entries seeded from `cpu_state`, the same
894        // handoff every graph run relies on when the entry is fresh.
895        // Without this line the two designs collide on hybrids and o1
896        // never becomes graph-portable: prefill through the graph
897        // records no trace, so views stay None forever.
898        if self.o1_active() {
899            return false;
900        }
901        self.weights
902            .layers
903            .iter()
904            .any(|lw| matches!(&lw.attn, AttnKind::LinearGdn(_)))
905    }
906
907    #[cfg(target_os = "macos")]
908    fn q1_graph_gpu(
909        &mut self,
910        start: usize,
911        upto: Option<usize>,
912        position: usize,
913        h: &mut [f32],
914    ) -> usize {
915        let _mt0 = std::time::Instant::now(); // CMF_METAL_HOSTPROF
916        use crate::gpu::{AttnGpuLayer, GdnGpuCfg, GdnGpuLayer, GraphDims, MetalFfn, TokenGraph};
917        if self.attn_softcap > 0.0 // capped scores: no graph kernel — CPU path
918            || !crate::gpu::enabled_here()
919            || !crate::gpu::q1_force()
920            || std::env::var("CMF_GPU_BLOCK")
921                .map(|v| v == "0")
922                .unwrap_or(false)
923        {
924            if std::env::var("CMF_GRAPH_DBG").is_ok() {
925                eprintln!(
926                    "block-graph: front gate (softcap={} enabled_here={} q1_force={})",
927                    self.attn_softcap > 0.0,
928                    crate::gpu::enabled_here(),
929                    crate::gpu::q1_force(),
930                );
931            }
932            return start;
933        }
934        // The graph encodes SiLU FFN, 1/√hd attention scores and
935        // full-context attend with no branch norms — Gemma-style archs
936        // (sliding window, scale override, sandwich norms, GeLU) fall
937        // back to the CPU path.
938        if self.swa.is_some()
939            || self.global_attn.is_some()
940            || self.attention_heads_per_layer.is_some()
941            || self.attn_v_norm
942            || (self.attn_scale - 1.0 / (self.head_dim as f32).sqrt()).abs() > 1e-9
943            || self.weights.layers.iter().any(|lw| {
944                lw.attn_out_norm.is_some()
945                    || lw.ffn_out_norm.is_some()
946                    || lw.layer_scale.is_some()
947                    || matches!(&lw.ffn, FfnKind::Dense(d) if d.act != Act::Silu)
948            })
949        {
950            if std::env::var("CMF_GRAPH_DBG").is_ok() {
951                eprintln!(
952                    "block-graph: arch ineligible (swa={} gattn={} hpl={} vnorm={} scale_delta={:.2e})",
953                    self.swa.is_some(),
954                    self.global_attn.is_some(),
955                    self.attention_heads_per_layer.is_some(),
956                    self.attn_v_norm,
957                    (self.attn_scale - 1.0 / (self.head_dim as f32).sqrt()).abs(),
958                );
959            }
960            return start;
961        }
962        // Looped Transformer: the graph covers ALL loop iterations;
963        // encode_loop_norm is inserted on-device at each boundary.
964        let limit = upto
965            .map(|u| u + 1)
966            .unwrap_or(self.num_layers)
967            .min(self.num_layers);
968
969        enum Item<'a> {
970            Gdn {
971                run: Vec<GdnGpuLayer<'a>>,
972                first: usize,
973            },
974            Attn {
975                l: AttnGpuLayer<'a>,
976                li: usize,
977                q_norm: Option<&'a [f32]>,
978                k_norm: Option<&'a [f32]>,
979                output_gate: bool,
980                bias: Option<(&'a [f32], &'a [f32], &'a [f32])>,
981                /// Attend on the device too (no sync): F32 KV, no
982                /// o1/bias, dims inside the kernels' contract.
983                full_gpu: bool,
984            },
985        }
986
987        // Device-attend KERNEL contract, shared by every Full layer. The
988        // hd>128 default-off POLICY is applied after the scan: it was
989        // measured on dense models, and a MoE plan inverts it — with the
990        // experts on device each CPU-attend sandwich costs a
991        // commit+wait, ~30 submits/token (W2 on M4: 14.7 tok/s
992        // sandwiched vs 27.1 device-attend vs 18.8 pure CPU).
993        let attend_mode = std::env::var("CMF_GPU_ATTEND").unwrap_or_else(|_| "auto".into());
994        let attend_contract = attend_mode != "0"
995            && attend_mode != "off"
996            && self.head_dim % 4 == 0
997            && self.head_dim <= 256
998            && self.rotary_dim >= 2
999            && self.rotary_dim <= self.head_dim
1000            && (self.rotary_dim / 2) % 32 == 0
1001            && self.num_kv_heads > 0
1002            && self.num_heads % self.num_kv_heads == 0;
1003
1004        let mut plan: Vec<Item> = Vec::new();
1005        let mut model_ref: Option<std::sync::Arc<cortiq_core::CmfModel>> = None;
1006        // Break-reason diagnostics ride the same env as the plan summary.
1007        let block_diag = std::env::var("CMF_GRAPH_DBG").is_ok();
1008        let mut scan = start;
1009        while scan < limit {
1010            let lw = &self.weights.layers[self.phys_layer(scan)];
1011            let ffn = match &lw.ffn {
1012                FfnKind::Dense(d) if d.segs.is_empty() => {
1013                    let (Some(g), Some(u), Some(dn)) = (
1014                        d.gate_proj.q1_parts(),
1015                        d.up_proj.q1_parts(),
1016                        d.down_proj.q1_parts(),
1017                    ) else {
1018                        if block_diag {
1019                            eprintln!(
1020                                "block-graph: L{scan} FFN trio not graph-mappable — run ends"
1021                            );
1022                        }
1023                        break;
1024                    };
1025                    MetalFfn::Dense {
1026                        gate: g,
1027                        up: u,
1028                        down: dn,
1029                    }
1030                }
1031                FfnKind::Moe(m) => {
1032                    let Some(moe) = metal_moe_graph_parts(m, self.hidden_size) else {
1033                        if block_diag {
1034                            eprintln!(
1035                                "block-graph: L{scan} MoE outside the graph contract — run ends"
1036                            );
1037                        }
1038                        break;
1039                    };
1040                    if let QTensor::Mapped { model, .. } = &m.experts[0].gate_proj {
1041                        model_ref.get_or_insert_with(|| model.clone());
1042                    }
1043                    MetalFfn::Moe(moe)
1044                }
1045                _ => {
1046                    if block_diag {
1047                        eprintln!("block-graph: L{scan} non-graph FFN — run ends");
1048                    }
1049                    break;
1050                }
1051            };
1052            match &lw.attn {
1053                AttnKind::LinearGdn(w) if self.gdn_cfg.is_some() => {
1054                    let parts = (
1055                        w.in_proj_qkv.q1_parts(),
1056                        w.in_proj_z.q1_parts(),
1057                        w.in_proj_a.f32_parts(),
1058                        w.in_proj_b.f32_parts(),
1059                        w.out_proj.q1_parts(),
1060                    );
1061                    let (Some(qkv), Some(z), Some(a), Some(b), Some(out)) = parts else {
1062                        if block_diag {
1063                            eprintln!(
1064                                "block-graph: L{scan} GDN parts refused (qkv={} z={} a_f32={} b_f32={} out={})",
1065                                w.in_proj_qkv.q1_parts().is_some(),
1066                                w.in_proj_z.q1_parts().is_some(),
1067                                w.in_proj_a.f32_parts().is_some(),
1068                                w.in_proj_b.f32_parts().is_some(),
1069                                w.out_proj.q1_parts().is_some(),
1070                            );
1071                        }
1072                        break;
1073                    };
1074                    if let QTensor::Mapped { model, .. } = &w.in_proj_qkv {
1075                        model_ref.get_or_insert_with(|| model.clone());
1076                    }
1077                    let gl = GdnGpuLayer {
1078                        attn_norm: &lw.input_norm,
1079                        post_norm: &lw.post_norm,
1080                        qkv,
1081                        z,
1082                        a,
1083                        b,
1084                        out,
1085                        ffn,
1086                        conv1d: &w.conv1d,
1087                        a_log: &w.a_log,
1088                        dt_bias: &w.dt_bias,
1089                        gnorm: &w.norm,
1090                    };
1091                    match plan.last_mut() {
1092                        Some(Item::Gdn { run, .. }) => run.push(gl),
1093                        _ => plan.push(Item::Gdn {
1094                            run: vec![gl],
1095                            first: scan,
1096                        }),
1097                    }
1098                }
1099                AttnKind::Full {
1100                    wq,
1101                    wk,
1102                    wv,
1103                    wo,
1104                    q_norm,
1105                    k_norm,
1106                    output_gate,
1107                    softplus_gate: None,
1108                    bias,
1109                } if !self.kv_cache.layers[scan].o1_sealed()
1110                    // Sealed o1 stays plannable when the Metal o1 port
1111                    // is on: full_gpu attends through the device state,
1112                    // and any refusal falls to the sandwich, whose CPU
1113                    // core routes sealed layers through the nystrom step.
1114                    || std::env::var("CMF_O1_METAL").as_deref() == Ok("1") =>
1115                {
1116                    let parts = (wq.q1_parts(), wk.q1_parts(), wv.q1_parts(), wo.q1_parts());
1117                    let (Some(pq), Some(pk), Some(pv), Some(po)) = parts else {
1118                        break;
1119                    };
1120                    if let QTensor::Mapped { model, .. } = wq {
1121                        model_ref.get_or_insert_with(|| model.clone());
1122                    }
1123                    let cache = &self.kv_cache.layers[scan];
1124                    // O(1) layer on Metal: the device attends through the
1125                    // sealed Nystrom state (opt-in while the port proves
1126                    // itself). Unsealed -> sandwich path = the CPU o1 step.
1127                    let o1_metal = cache.o1.is_some()
1128                        && std::env::var("CMF_O1_METAL").as_deref() == Ok("1")
1129                        && cache.o1_views().is_some();
1130                    let full_gpu = attend_contract
1131                        && cache.mode == crate::kv_cache::KvMode::F32
1132                        && (cache.o1.is_none() || o1_metal)
1133                        && bias.is_none()
1134                        && pq.1 == self.num_heads * self.head_dim * (1 + *output_gate as usize)
1135                        && pk.1 == self.num_kv_heads * self.head_dim
1136                        && pv.1 == self.num_kv_heads * self.head_dim
1137                        && po.2 == self.num_heads * self.head_dim;
1138                    plan.push(Item::Attn {
1139                        l: AttnGpuLayer {
1140                            attn_norm: &lw.input_norm,
1141                            post_norm: &lw.post_norm,
1142                            wq: pq,
1143                            wk: pk,
1144                            wv: pv,
1145                            wo: po,
1146                            ffn,
1147                        },
1148                        li: scan,
1149                        q_norm: q_norm.as_deref(),
1150                        k_norm: k_norm.as_deref(),
1151                        output_gate: *output_gate,
1152                        bias: bias
1153                            .as_ref()
1154                            .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
1155                        full_gpu,
1156                    });
1157                }
1158                _ => break,
1159            }
1160            scan += 1;
1161        }
1162        let Some(model) = model_ref else {
1163            if std::env::var("CMF_GRAPH_DBG").is_ok() {
1164                eprintln!("q1-graph: no model ref (start {start}, scanned to {scan})");
1165            }
1166            return start;
1167        };
1168        if plan.is_empty() {
1169            if std::env::var("CMF_GRAPH_DBG").is_ok() {
1170                eprintln!("q1-graph: empty plan at layer {start}");
1171            }
1172            return start;
1173        }
1174        let has_moe = plan.iter().any(|it| match it {
1175            Item::Gdn { run, .. } => run.iter().any(|l| matches!(l.ffn, MetalFfn::Moe(_))),
1176            Item::Attn { l, .. } => matches!(l.ffn, MetalFfn::Moe(_)),
1177        });
1178        let has_gdn = plan.iter().any(|it| matches!(it, Item::Gdn { .. }));
1179        let dev_attend = attend_contract
1180            && (self.head_dim <= 128
1181                || has_moe
1182                // A GDN hybrid attends on a quarter of its layers: the
1183                // hd>128 caution was measured on pure-dense models where
1184                // gqa_attend dominates, and on Qwen3.8-27B (hd 256, 48
1185                // GDN + 16 attn) the sandwich costs 2x the whole decode
1186                // (1.2 vs 2.21 tok/s measured before the arena fix).
1187                || (self.head_dim <= 256 && has_gdn)
1188                || attend_mode == "force"
1189                || attend_mode == "256");
1190        if !dev_attend {
1191            for it in &mut plan {
1192                if let Item::Attn { li, full_gpu, .. } = it {
1193                    // The hd>128 policy is about gqa_attend; an o1 layer
1194                    // attends through its own kernel set.
1195                    let keep_o1 = self.kv_cache.layers[*li].o1.is_some()
1196                        && std::env::var("CMF_O1_METAL").as_deref() == Ok("1");
1197                    if !keep_o1 {
1198                        *full_gpu = false;
1199                    }
1200                }
1201            }
1202        }
1203        if std::env::var("CMF_GRAPH_DBG").is_ok() {
1204            use std::sync::atomic::{AtomicBool, Ordering};
1205            static SAID: AtomicBool = AtomicBool::new(false);
1206            if !SAID.swap(true, Ordering::Relaxed) {
1207                let fg = plan
1208                    .iter()
1209                    .filter(|it| matches!(it, Item::Attn { full_gpu: true, .. }))
1210                    .count();
1211                let att = plan
1212                    .iter()
1213                    .filter(|it| matches!(it, Item::Attn { .. }))
1214                    .count();
1215                eprintln!(
1216                    "q1-graph: plan of {} items from layer {start} to {scan} | dev_attend={dev_attend} full_gpu {fg}/{att} | hd={} rd={} nkv={} nh={}",
1217                    plan.len(),
1218                    self.head_dim,
1219                    self.rotary_dim,
1220                    self.num_kv_heads,
1221                    self.num_heads,
1222                );
1223            }
1224        }
1225        let dims = GraphDims {
1226            hidden: self.hidden_size,
1227            eps: self.rms_eps as f32,
1228            gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
1229        };
1230        let Some(mut graph) = TokenGraph::new(&model, dims, h) else {
1231            return start;
1232        };
1233        let gcfg = self.gdn_cfg.map(|cfg| GdnGpuCfg {
1234            nv: cfg.num_v_heads,
1235            nk: cfg.num_k_heads,
1236            dk: cfg.key_head_dim,
1237            dv: cfg.value_head_dim,
1238            kk: cfg.conv_kernel,
1239            hidden: self.hidden_size,
1240            inter: self.intermediate_size,
1241            c_dim: cfg.conv_dim(),
1242            eps: cfg.rms_eps as f32,
1243            gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
1244        });
1245        // Validate the whole plan BEFORE encoding anything: after the
1246        // first sync a refused layer would leave the token
1247        // half-executed, so truncate to the provably encodable prefix.
1248        let mut valid = 0usize;
1249        let mut end = start;
1250        crate::gpu::stageprof(1, _mt0.elapsed()); // конец планирования
1251        if std::env::var("CMF_PLAN_DUMP").is_ok() {
1252            static ONCE: std::sync::Once = std::sync::Once::new();
1253            ONCE.call_once(|| {
1254                for it in &plan {
1255                    match it {
1256                        Item::Gdn { first, run } => {
1257                            eprintln!("plan: Gdn first={first} len={}", run.len())
1258                        }
1259                        Item::Attn { li, full_gpu, .. } => {
1260                            eprintln!("plan: Attn li={li} full_gpu={full_gpu}")
1261                        }
1262                    }
1263                }
1264            });
1265        }
1266        for item in &plan {
1267            let ok = match item {
1268                Item::Gdn { run, .. } => gcfg
1269                    .as_ref()
1270                    .map(|gc| run.iter().all(|l| graph.gdn_ok(l, gc)))
1271                    .unwrap_or(false),
1272                Item::Attn { l, .. } => graph.attn_ok(l),
1273            };
1274            if !ok {
1275                if block_diag {
1276                    eprintln!(
1277                        "block-graph: plan item {} ({}) failed graph preflight",
1278                        valid,
1279                        match item {
1280                            Item::Gdn { run, first } => format!("GDN run L{first}+{}", run.len()),
1281                            Item::Attn { li, .. } => format!("Attn L{li}"),
1282                        }
1283                    );
1284                }
1285                break;
1286            }
1287            valid += 1;
1288            end += match item {
1289                Item::Gdn { run, .. } => run.len(),
1290                Item::Attn { .. } => 1,
1291            };
1292        }
1293        plan.truncate(valid);
1294        if plan.is_empty() {
1295            return start;
1296        }
1297
1298        let inv_freq = self.inv_freq.clone();
1299        let pool = self.pool.clone();
1300        let (nh, nkv, hd, hs, rd, eps) = (
1301            self.num_heads,
1302            self.num_kv_heads,
1303            self.head_dim,
1304            self.hidden_size,
1305            self.rotary_dim,
1306            self.rms_eps,
1307        );
1308        let norm_style = self.norm_style;
1309        let gemma = norm_style == cortiq_core::NormStyle::Gemma;
1310        let want = self.gdn_cfg.map(|c| c.state_len()).unwrap_or(0);
1311        let kv_id = self.graph_kv_id;
1312        // GDN runs whose states await readback after the next sync
1313        // (device-attended layers add no sync, so several may stack).
1314        let mut pending: Vec<(usize, usize)> = Vec::new();
1315        // Device-attended layers: their K/V/imp are pulled from the
1316        // mirror after the final sync.
1317        let mut dev_attn: Vec<usize> = Vec::new();
1318        for item in &plan {
1319            let _xt0 = std::time::Instant::now();
1320            let _xkind: u32 = match item {
1321                Item::Gdn { .. } => 2,
1322                Item::Attn { .. } => 3,
1323            };
1324            // Looped Transformer: insert on-device norm at loop boundaries.
1325            if self.loop_final_norm {
1326                let item_start = match item {
1327                    Item::Gdn { first, .. } => *first,
1328                    Item::Attn { li, .. } => *li,
1329                };
1330                if item_start > start && self.is_loop_end(item_start - 1) {
1331                    graph.encode_loop_norm(&self.weights.final_norm);
1332                }
1333            }
1334            match item {
1335                Item::Gdn { run, first } => {
1336                    for l in &mut self.kv_cache.layers[*first..*first + run.len()] {
1337                        if l.linear_state.len() != want {
1338                            l.linear_state = vec![0f32; want];
1339                        }
1340                    }
1341                    let ro: Vec<&[f32]> = self.kv_cache.layers[*first..*first + run.len()]
1342                        .iter()
1343                        .map(|l| l.linear_state.as_slice())
1344                        .collect();
1345                    let _ig = std::time::Instant::now();
1346                    if !graph.encode_gdn_run(run, &ro, gcfg.as_ref().unwrap()) {
1347                        // Unreachable: the plan was validated above.
1348                        tracing::error!("q1 graph: GDN run refused after validation");
1349                        return start;
1350                    }
1351                    // Early commit: the GPU starts the run while the
1352                    // CPU encodes the next layer (nothing to wait on).
1353                    graph.commit_kind = 2;
1354                    graph.commit();
1355                    crate::gpu::stageprof(0, _ig.elapsed());
1356                    pending.push((*first, run.len()));
1357                }
1358                Item::Attn {
1359                    l,
1360                    li,
1361                    q_norm,
1362                    k_norm,
1363                    output_gate,
1364                    bias,
1365                    full_gpu,
1366                } => {
1367                    let _ia = std::time::Instant::now();
1368                    // ── Fully device-resident attention: no sync at all.
1369                    if *full_gpu {
1370                        let cache = &self.kv_cache.layers[*li];
1371                        let o1p = if cache.o1.is_some() {
1372                            match cache.o1_views() {
1373                                Some(views) => Some(crate::gpu::O1AttnParams {
1374                                    views,
1375                                    epoch: self.o1_epoch,
1376                                }),
1377                                // Sealed state gone mid-run: sandwich.
1378                                None => None,
1379                            }
1380                        } else {
1381                            None
1382                        };
1383                        let o1_layer = cache.o1.is_some();
1384                        if o1_layer && o1p.is_none() {
1385                            // fall to the sandwich (CPU o1 step)
1386                        }
1387                        let cpu_k: Vec<&[f32]> = (0..nkv).map(|g| cache.head_keys(g)).collect();
1388                        let cpu_v: Vec<&[f32]> = (0..nkv).map(|g| cache.head_values(g)).collect();
1389                        let cpu_stored = if o1_layer { 0 } else { cpu_k[0].len() / hd };
1390                        let p = crate::gpu::AttnDeviceParams {
1391                            kv_id,
1392                            layer: *li,
1393                            nh,
1394                            nkv,
1395                            hd,
1396                            rd,
1397                            position,
1398                            eps: eps as f32,
1399                            gemma,
1400                            output_gate: *output_gate,
1401                            q_norm: *q_norm,
1402                            k_norm: *k_norm,
1403                            inv_freq: &inv_freq,
1404                            cpu_k,
1405                            cpu_v,
1406                            cpu_stored,
1407                            o1: o1p,
1408                        };
1409                        let o1_bad = o1_layer && p.o1.is_none();
1410                        if !o1_bad && graph.attn_device_ok(l, &p) && graph.encode_attn_device(l, &p)
1411                        {
1412                            // o1 layers leave no mirror row to pull.
1413                            if p.o1.is_none() {
1414                                dev_attn.push(*li);
1415                            }
1416                            graph.commit_kind = 3;
1417                            graph.commit();
1418                            // The footer below is skipped by `continue`:
1419                            // account the device-attn item here or its
1420                            // cost hides from the stage profile entirely.
1421                            crate::gpu::stageprof(_xkind, _xt0.elapsed());
1422                            continue;
1423                        }
1424                        // Mirror refused (nothing encoded) → sandwich.
1425                    }
1426                    graph.encode_attn_prefix(l);
1427                    graph.sync();
1428                    if !pending.is_empty() {
1429                        let idxs: Vec<usize> =
1430                            pending.drain(..).flat_map(|(f, n)| f..f + n).collect();
1431                        let mut outs: Vec<&mut [f32]> = self
1432                            .kv_cache
1433                            .layers
1434                            .iter_mut()
1435                            .enumerate()
1436                            .filter(|(i, _)| idxs.binary_search(i).is_ok())
1437                            .map(|(_, s)| s.linear_state.as_mut_slice())
1438                            .collect();
1439                        graph.read_states(&mut outs);
1440                    }
1441                    let mut q_raw = attention::take_buf(l.wq.1);
1442                    let mut k = attention::take_buf(l.wk.1);
1443                    let mut v = attention::take_buf(l.wv.1);
1444                    graph.read_qkv(&mut q_raw, &mut k, &mut v);
1445                    let cfg = QwenAttnCfg {
1446                        num_heads: nh,
1447                        num_kv_heads: nkv,
1448                        head_dim: hd,
1449                        hidden_size: hs,
1450                        position,
1451                        inv_freq: &inv_freq,
1452                        rotary_dim: rd,
1453                        scale: self.attn_scale,
1454                        softcap: self.attn_softcap,
1455                        window: None,
1456                        v_norm: false,
1457                        q_norm: *q_norm,
1458                        k_norm: *k_norm,
1459                        output_gate: *output_gate,
1460                        softplus_gate: None,
1461                        rope_scale: 1.0,
1462                        bias: *bias,
1463                        rms_eps: eps,
1464                        norm_style,
1465                        pool: pool.as_deref(),
1466                    };
1467                    // CMF_ATTN_ORACLE=1: diff the device attend against
1468                    // this CPU attend on identical inputs (bring-up).
1469                    let oracle = std::env::var("CMF_ATTN_ORACLE").as_deref() == Ok("1")
1470                        || std::env::var("CMF_ATTN_DUMP").is_ok();
1471                    let _ = full_gpu;
1472                    let oracle_in = oracle.then(|| (q_raw.clone(), k.clone(), v.clone()));
1473                    let mut ao = attention::qwen_attention_core(
1474                        q_raw,
1475                        k,
1476                        v,
1477                        &mut self.kv_cache.layers[*li],
1478                        &cfg,
1479                    );
1480                    // CMF_ATTN_DUMP=<dir>: this token's rope'd Q and the layer's whole
1481                    // K/V cache as raw f32 (offline attention-statistics probes:
1482                    // block bounds, mass concentration). Needs CMF_GPU_ATTEND=0.
1483                    if let Ok(dir) = std::env::var("CMF_ATTN_DUMP") {
1484                        if let Some((qr0, k0, v0)) = oracle_in.clone() {
1485                            let (cq, _cg, _ck, _cv) =
1486                                attention::finish_projection_debug(qr0, k0, v0, &cfg, position);
1487                            let cache = &self.kv_cache.layers[*li];
1488                            let n = cache.head_keys(0).len() / hd;
1489                            let mut bytes: Vec<u8> = Vec::new();
1490                            for v in [nh as u32, nkv as u32, hd as u32, n as u32, position as u32] {
1491                                bytes.extend_from_slice(&v.to_le_bytes());
1492                            }
1493                            for v in &cq {
1494                                bytes.extend_from_slice(&v.to_le_bytes());
1495                            }
1496                            for g in 0..nkv {
1497                                for v in cache.head_keys(g) {
1498                                    bytes.extend_from_slice(&v.to_le_bytes());
1499                                }
1500                            }
1501                            for g in 0..nkv {
1502                                for v in cache.head_values(g) {
1503                                    bytes.extend_from_slice(&v.to_le_bytes());
1504                                }
1505                            }
1506                            let _ =
1507                                std::fs::write(format!("{dir}/L{li}_pos{position}.bin"), &bytes);
1508                        }
1509                    }
1510                    if let Some((qr0, k0, v0)) =
1511                        oracle_in.filter(|_| std::env::var("CMF_ATTN_ORACLE").as_deref() == Ok("1"))
1512                    {
1513                        let (cq, _cg, ck, cv) =
1514                            attention::finish_projection_debug(qr0, k0, v0, &cfg, position);
1515                        let mut h_now = vec![0f32; hs];
1516                        graph.read_h(&mut h_now);
1517                        let cache = &self.kv_cache.layers[*li];
1518                        let n_after = cache.head_keys(0).len() / hd;
1519                        let cpu_k: Vec<&[f32]> = (0..nkv)
1520                            .map(|g| &cache.head_keys(g)[..(n_after - 1) * hd])
1521                            .collect();
1522                        let cpu_v: Vec<&[f32]> = (0..nkv)
1523                            .map(|g| &cache.head_values(g)[..(n_after - 1) * hd])
1524                            .collect();
1525                        let p = crate::gpu::AttnDeviceParams {
1526                            kv_id,
1527                            layer: *li,
1528                            nh,
1529                            nkv,
1530                            hd,
1531                            rd,
1532                            position,
1533                            eps: eps as f32,
1534                            gemma,
1535                            output_gate: *output_gate,
1536                            q_norm: *q_norm,
1537                            k_norm: *k_norm,
1538                            inv_freq: &inv_freq,
1539                            cpu_k,
1540                            cpu_v,
1541                            cpu_stored: n_after - 1,
1542                            o1: None,
1543                        };
1544                        if let Some((dq, dk, dv, dao)) = graph.debug_attn_device(l, &p, &h_now) {
1545                            let md = |a: &[f32], b: &[f32]| {
1546                                a.iter().zip(b).fold(0f32, |m, (x, y)| m.max((x - y).abs()))
1547                            };
1548                            let nn = |a: &[f32]| a.iter().map(|x| x * x).sum::<f32>().sqrt();
1549                            eprintln!(
1550                                "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}",
1551                                nn(&cq),
1552                                md(&cq, &dq),
1553                                nn(&ck),
1554                                md(&ck, &dk),
1555                                nn(&cv),
1556                                md(&cv, &dv),
1557                                nn(&ao),
1558                                md(&ao, &dao)
1559                            );
1560                        } else {
1561                            eprintln!("attn-oracle L{li}: device probe declined");
1562                        }
1563                    }
1564                    graph.encode_attn_suffix(l, &ao);
1565                    // Early commit: the GPU starts O+FFN while the CPU
1566                    // encodes the following GDN run / attention prefix.
1567                    graph.commit();
1568                    attention::recycle_buf(&mut ao);
1569                }
1570            }
1571
1572            crate::gpu::stageprof(_xkind, _xt0.elapsed());
1573        }
1574        // Ride the final norm + lm_head in the same command buffer when
1575        // this run reaches the model's end and the caller wants logits:
1576        // the separate per-op lm_head submit (a full round trip) folds
1577        // into the sync that already happens here.
1578        let mut lm_rows = None;
1579        if self.graph_want_logits
1580            && upto.is_none()
1581            && end == self.num_layers
1582            && std::env::var("CMF_GPU_LMHEAD")
1583                .map(|v| v != "0")
1584                .unwrap_or(true)
1585        {
1586            if let Some(lm) = self.weights.lm_head.q1_parts() {
1587                if graph.lm_head_ok(lm) {
1588                    graph.encode_lm_head(&self.weights.final_norm, lm);
1589                    lm_rows = Some(lm.1);
1590                }
1591            }
1592        }
1593        let _sy0 = std::time::Instant::now();
1594        graph.sync();
1595        let _rs0 = std::time::Instant::now();
1596        if !pending.is_empty() {
1597            let idxs: Vec<usize> = pending.drain(..).flat_map(|(f, n)| f..f + n).collect();
1598            let mut outs: Vec<&mut [f32]> = self
1599                .kv_cache
1600                .layers
1601                .iter_mut()
1602                .enumerate()
1603                .filter(|(i, _)| idxs.binary_search(i).is_ok())
1604                .map(|(_, s)| s.linear_state.as_mut_slice())
1605                .collect();
1606            graph.read_states(&mut outs);
1607        }
1608        if std::env::var("CMF_GRAPH_HOSTPROF").as_deref() == Ok("1") {
1609            use std::sync::atomic::{AtomicU64, Ordering};
1610            static SY: AtomicU64 = AtomicU64::new(0);
1611            static RS: AtomicU64 = AtomicU64::new(0);
1612            static N: AtomicU64 = AtomicU64::new(0);
1613            SY.fetch_add((_rs0 - _sy0).as_nanos() as u64, Ordering::Relaxed);
1614            RS.fetch_add(_rs0.elapsed().as_nanos() as u64, Ordering::Relaxed);
1615            let n = N.fetch_add(1, Ordering::Relaxed) + 1;
1616            if n % 100 == 0 {
1617                eprintln!(
1618                    "postprof: sync-wait {:.1} ms/ток | read_states {:.1} ms/ток ({n})",
1619                    SY.load(Ordering::Relaxed) as f64 / n as f64 / 1e6,
1620                    RS.load(Ordering::Relaxed) as f64 / n as f64 / 1e6
1621                );
1622            }
1623        }
1624        if let Some(rows) = lm_rows {
1625            crate::gpu::hostprof_encode_done(_mt0);
1626            let mut lg = attention::take_buf(rows.min(self.vocab_size));
1627            graph.read_logits(&mut lg);
1628            crate::gpu::hostprof_total(_mt0);
1629            lg.resize(self.vocab_size, 0.0);
1630            if let Some(c) = self.final_softcap {
1631                for l in lg.iter_mut() {
1632                    *l = c * (*l / c).tanh();
1633                }
1634            }
1635            self.graph_logits = Some(lg);
1636        }
1637        graph.finish(h);
1638        // Device-attended layers: replay the CPU bookkeeping — append
1639        // the mirror's new K/V row (rope'd on the GPU) into the owner
1640        // cache, then bank this token's Born-importance mass.
1641        for li in dev_attn {
1642            let mut krow = attention::take_buf(nkv * hd);
1643            let mut vrow = attention::take_buf(nkv * hd);
1644            if crate::gpu::kv_mirror_read_last(kv_id, li, nkv, hd, &mut krow, &mut vrow) {
1645                let cache = &mut self.kv_cache.layers[li];
1646                cache.append(&krow, &vrow, &[]);
1647                let n = cache.seq_len;
1648                let mut imp = attention::take_buf(n);
1649                crate::gpu::kv_mirror_take_imp(kv_id, li, &mut imp);
1650                cache.accumulate_imp(&imp);
1651                attention::recycle_buf(&mut imp);
1652            }
1653            attention::recycle_buf(&mut krow);
1654            attention::recycle_buf(&mut vrow);
1655        }
1656        end
1657    }
1658
1659    pub fn new(
1660        tokenizer: Tokenizer,
1661        weights: PipelineWeights,
1662        hidden_size: usize,
1663        intermediate_size: usize,
1664        num_heads: usize,
1665        num_kv_heads: usize,
1666        head_dim: usize,
1667        num_layers: usize,
1668        physical_layers: usize,
1669        loop_final_norm: bool,
1670        vocab_size: usize,
1671        rms_eps: f64,
1672        rope_base: f32,
1673        norm_style: NormStyle,
1674        max_seq_len: usize,
1675        sampler_config: SamplerConfig,
1676    ) -> Self {
1677        let rng = match sampler_config.seed {
1678            Some(s) => SplitMix64::new(s),
1679            None => SplitMix64::from_entropy(),
1680        };
1681        let inv_freq = std::sync::Arc::new(attention::rope_inv_freq(head_dim, rope_base));
1682        let pool = Pool::from_env();
1683        if let Some(p) = &pool {
1684            tracing::info!("worker pool: {} threads", p.n_workers());
1685        }
1686        Self {
1687            gpu_plan: None,
1688            tokenizer: std::sync::Arc::new(tokenizer),
1689            kv_cache: KvCache::new(num_layers, num_kv_heads, head_dim, max_seq_len),
1690            sampler_config,
1691            weights,
1692            hidden_size,
1693            intermediate_size,
1694            num_heads,
1695            num_kv_heads,
1696            head_dim,
1697            num_layers,
1698            physical_layers,
1699            loop_final_norm,
1700            vocab_size,
1701            rms_eps,
1702            rope_base,
1703            norm_style,
1704            rotary_dim: head_dim,
1705            attention_heads_per_layer: None,
1706            vmf_cfg: None,
1707            gdn_cfg: None,
1708            kda_cfg: None,
1709            g3n: None,
1710            dsv4: None,
1711            qwen4_exp: None,
1712            dsv4_mtp: Vec::new(),
1713            dspark: None,
1714            dspark_pending: Vec::new(),
1715            dspark_hist: Vec::new(),
1716            dspark_real: Vec::new(),
1717            dspark_trunk_picks: Vec::new(),
1718            dspark_exp: Vec::new(),
1719            dspark_draft_ns: 0,
1720            logit_multiplier: None,
1721            cancel: std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)),
1722            kv_history: Vec::new(),
1723            short_conv_cfg: None,
1724            mtp: None,
1725            speculative: std::env::var("CMF_MTP").map(|v| v != "0").unwrap_or(true),
1726            rng,
1727            sampler_scratch: SamplerScratch::default(),
1728            spec_forced: None,
1729            spec_q: Vec::new(),
1730            spec_p: Vec::new(),
1731            spec_res: Vec::new(),
1732            spec_qs: Vec::new(),
1733            spec_ps: Vec::new(),
1734            spec_ress: Vec::new(),
1735            mtp_graph_mode: None,
1736            #[cfg(target_os = "macos")]
1737            metal_verify: None,
1738            inv_freq,
1739            ws: ForwardScratch::new(hidden_size),
1740            pool,
1741            model: None,
1742            dyn_force_f32: false,
1743            dyn_skill_layers: Vec::new(),
1744            dyn_active: None,
1745            dyn_blend_loaded: false,
1746            dyn_phi_layer: None,
1747            dyn_phi_ema: Vec::new(),
1748            dyn_phi_seen: 0,
1749            dyn_router: None,
1750            o1_cfg: None,
1751            o1_epoch: 0,
1752            o1_flags: Vec::new(),
1753            trace: false,
1754            calib_temp: 1.0,
1755            confidence_on: true,
1756            embed_multiplier: 1.0,
1757            attn_scale: 1.0 / (head_dim as f32).sqrt(),
1758            swa: None,
1759            sliding_layers: None,
1760            inv_freq_local: None,
1761            rotary_dim_local: None,
1762            rope_scale: 1.0,
1763            rope_scale_local: 1.0,
1764            global_attn: None,
1765            inv_freq_global: None,
1766            attn_v_norm: false,
1767            final_softcap: None,
1768            head_clusters: None,
1769            attn_softcap: 0.0,
1770            graph_want_logits: false,
1771            graph_logits: None,
1772            graph_kv_id: {
1773                static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
1774                NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
1775            },
1776        }
1777    }
1778
1779    /// Enable/disable per-layer O(1) Nyström attention. Only Full
1780    /// layers are eligible (a linear layer keeps its own operator).
1781    /// Applies to generation (`generate*`/`forward_ids`): the prompt
1782    /// pass stays exact, the seal happens once after prefill, decode
1783    /// runs on the O(1) state. Teacher-forced scoring (`ppl_ids`)
1784    /// intentionally stays exact.
1785    pub fn set_o1(&mut self, cfg: Option<crate::nystrom::O1Cfg>) {
1786        self.o1_flags = match &cfg {
1787            Some(c) => {
1788                let mut flags = c.layer_flags(self.num_layers);
1789                for (li, f) in flags.iter_mut().enumerate() {
1790                    if *f
1791                        && !matches!(
1792                            self.weights.layers[self.phys_layer(li)].attn,
1793                            AttnKind::Full { .. }
1794                        )
1795                    {
1796                        *f = false;
1797                    }
1798                }
1799                flags
1800            }
1801            None => Vec::new(),
1802        };
1803        if let Some(c) = &cfg {
1804            let n = self.o1_flags.iter().filter(|&&f| f).count();
1805            tracing::info!(
1806                "o1 nystrom attention: {n}/{} layer(s), m={} w={} sink={} rect={:?}",
1807                self.num_layers,
1808                c.m,
1809                c.w,
1810                c.sink,
1811                c.rect
1812            );
1813        }
1814        self.o1_cfg = cfg;
1815    }
1816
1817    /// True when at least one layer runs the O(1) kernel.
1818    pub fn o1_active(&self) -> bool {
1819        self.o1_cfg.is_some() && self.o1_flags.iter().any(|&f| f)
1820    }
1821
1822    /// Arm query collection on the o1 layers (fresh prompt pass).
1823    /// Reset the o1 layers to Collecting for a fresh sequence. Pub for the
1824    /// network split: each side runs the o1 lifecycle over ITS OWN layers
1825    /// (begin before prefill, seal at the prefill barrier).
1826    pub fn o1_begin(&mut self) {
1827        if let Some(c) = &self.o1_cfg {
1828            let (m, w, sink, rect) = (c.m, c.w, c.sink, c.rect);
1829            for (li, &f) in self.o1_flags.iter().enumerate() {
1830                if f {
1831                    self.kv_cache.layers[li].o1_begin(m, w, sink, rect);
1832                }
1833            }
1834        }
1835    }
1836
1837    /// Freeze landmarks + skeleton state after the prompt pass and drop
1838    /// the o1 layers' full KV; decode then runs `step()` per token.
1839    /// Pub for the network split (see `o1_begin`).
1840    pub fn o1_seal(&mut self) {
1841        self.o1_epoch = self.o1_epoch.wrapping_add(1);
1842        if self.o1_cfg.is_none() {
1843            return;
1844        }
1845        for li in 0..self.num_layers {
1846            if self.o1_flags.get(li).copied().unwrap_or(false) {
1847                self.kv_cache.layers[li].o1_seal(self.num_heads);
1848            }
1849        }
1850    }
1851
1852    /// Enable/disable the structured per-token telemetry trace (B4).
1853    pub fn set_trace(&mut self, on: bool) {
1854        self.trace = on;
1855    }
1856
1857    /// Replace all request-scoped sampler options and reset the random stream.
1858    /// This is required for deterministic `seed` semantics in pooled servers.
1859    pub fn set_sampler_config(&mut self, config: SamplerConfig) {
1860        self.rng = match config.seed {
1861            Some(seed) => SplitMix64::new(seed),
1862            None => SplitMix64::from_entropy(),
1863        };
1864        self.sampler_config = config;
1865    }
1866
1867    /// Toggle the per-token Born-confidence reduction (a full-vocab
1868    /// softmax each token). `bench --core` turns it off so the timed
1869    /// loop matches llama-bench's core contract; the result's
1870    /// `confidence` vec is empty while off.
1871    pub fn set_confidence(&mut self, on: bool) {
1872        self.confidence_on = on;
1873    }
1874
1875    /// Set the confidence-calibration temperature (B1). Values ≤0 are
1876    /// clamped to raw (1.0).
1877    pub fn set_calib_temp(&mut self, t: f32) {
1878        self.calib_temp = if t > 1e-3 { t } else { 1.0 };
1879    }
1880
1881    /// The active calibration temperature (1.0 = raw Born mass).
1882    pub fn calib_temp(&self) -> f32 {
1883        self.calib_temp
1884    }
1885
1886    /// Partial rotary (Qwen3.5): rotate only the first `rotary_dim` dims;
1887    /// the frequency table is rebuilt over the rotary dims.
1888    pub fn set_rotary(&mut self, rotary_dim: usize, base: f32) {
1889        self.rotary_dim = rotary_dim.min(self.head_dim);
1890        self.inv_freq = std::sync::Arc::new(attention::rope_inv_freq(self.rotary_dim, base));
1891    }
1892
1893    fn attn_cfg(&self, position: usize) -> QwenAttnCfg<'_> {
1894        QwenAttnCfg {
1895            num_heads: self.num_heads,
1896            num_kv_heads: self.num_kv_heads,
1897            head_dim: self.head_dim,
1898            hidden_size: self.hidden_size,
1899            position,
1900            inv_freq: &self.inv_freq,
1901            rotary_dim: self.rotary_dim,
1902            scale: self.attn_scale,
1903            softcap: self.attn_softcap,
1904            window: None,
1905            v_norm: false,
1906            q_norm: None,
1907            k_norm: None,
1908            output_gate: false,
1909            softplus_gate: None,
1910            rope_scale: self.rope_scale,
1911            bias: None,
1912            rms_eps: self.rms_eps,
1913            norm_style: self.norm_style,
1914            pool: self.pool.as_deref(),
1915        }
1916    }
1917
1918    /// Generate text from a plain-text prompt. Streams tokens via `on_token`.
1919    pub fn generate(
1920        &mut self,
1921        prompt: &str,
1922        max_tokens: usize,
1923        task_mask: Option<&TaskMask>,
1924        on_token: Option<TokenCallback>,
1925    ) -> Result<GenerateResult, String> {
1926        let input_ids = self.tokenizer.with_bos(self.tokenizer.encode(prompt));
1927        self.generate_from_ids(&input_ids, max_tokens, task_mask, on_token)
1928    }
1929
1930    /// `None` when the mask forbids nothing (see `TaskMask::fully_open`).
1931    fn drop_open_mask<'m>(&self, m: Option<&'m TaskMask>) -> Option<&'m TaskMask> {
1932        m.filter(|m| !m.fully_open(self.intermediate_size, self.num_heads))
1933    }
1934
1935    /// Generate from prepared token ids (e.g. a chat template).
1936    ///
1937    /// With an MTP head, greedy generation without a task mask takes the
1938    /// speculative path: the MTP module drafts the token after next and
1939    /// the main model verifies both in one fused two-position forward
1940    /// (weights streamed once). The output is EXACTLY the vanilla greedy
1941    /// sequence — a rejected draft is rolled back — MTP only buys speed.
1942    pub fn generate_from_ids(
1943        &mut self,
1944        input_ids: &[u32],
1945        max_tokens: usize,
1946        task_mask: Option<&TaskMask>,
1947        mut on_token: Option<TokenCallback>,
1948    ) -> Result<GenerateResult, String> {
1949        if std::env::var("CMF_TRACE_H").is_ok() {
1950            eprintln!("input_ids: {input_ids:?}");
1951        }
1952        if input_ids.is_empty() {
1953            return Err("empty prompt: nothing to generate from".to_string());
1954        }
1955        // A mask that forbids nothing still costs every fused path and
1956        // whole-token graph, all of which are gated on `is_none()`. A
1957        // narrowed file whose one segment is always on carries exactly
1958        // such a mask — drop it here rather than pay 5x for a no-op.
1959        let task_mask = self.drop_open_mask(task_mask);
1960
1961        // Cross-turn KV reuse: a chat app resends the whole history
1962        // every turn; when the new ids strictly EXTEND what the cache
1963        // already holds, prefill only the tail — turn latency stays
1964        // proportional to the new text instead of the whole session.
1965        // Extension-only (no rollback), so it is exact for every layer
1966        // kind including recurrent state; MTP/o1/task-mask runs keep
1967        // the fresh-sequence path. CMF_KV_REUSE=0 disables.
1968        let reuse_from = {
1969            let on = !std::env::var("CMF_KV_REUSE").is_ok_and(|v| v == "0");
1970            let h = &self.kv_history;
1971            if on
1972                && task_mask.is_none()
1973                && self.mtp.is_none()
1974                && self.o1_cfg.is_none()
1975                && !h.is_empty()
1976                && h.len() < input_ids.len()
1977                && input_ids[..h.len()] == h[..]
1978            {
1979                h.len()
1980            } else {
1981                0
1982            }
1983        };
1984        if reuse_from == 0 {
1985            // Fresh sequence — the cache holds absolute positions.
1986            self.kv_cache.clear();
1987            self.kv_history.clear();
1988            crate::gpu::graph_kv_reset(self.graph_kv_id);
1989        } else if std::env::var("CMF_PREFILL_PROF").is_ok() {
1990            eprintln!(
1991                "kv-reuse: {} of {} prompt positions already cached",
1992                reuse_from,
1993                input_ids.len()
1994            );
1995        }
1996        crate::gpu::graph_race_begin_generation();
1997        self.o1_begin();
1998
1999        // Speculative decode is off under o1: a rejected draft can't be
2000        // rolled back out of the far accumulators / ring window (the
2001        // Nyström insertion is irreversible by design).
2002        // The wgpu token graph owns a device K/V mirror that speculative
2003        // rollback would desync — the two are mutually exclusive.
2004        let graph_on = crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Decode);
2005        // Graph speculative decode (`CMF_GRAPH_SPEC=1`): the MTP head
2006        // drafts, ONE batched graph submit verifies the whole chain.
2007        //
2008        // It now PAYS on Qwen3.6-27B / RTX 5090 — 51.1 tok/s against a
2009        // plain 49.4 at k=3, medians of three, 89% of drafts accepted,
2010        // and the greedy continuation is byte-identical to the plain
2011        // path. That took the batch matvec sharing its nibble unpack
2012        // across the batch (`CMF_MV_BK=2`); before it, the same round
2013        // measured 43.6, an 11% LOSS, which is what the earlier note
2014        // here described.
2015        //
2016        // Still opt-in. One model's win is not a default: the verify
2017        // rides `gdn_spec_restore` and a batched frame whose numerics
2018        // are the batch kernels', and that has to be shown on more than
2019        // one architecture before every greedy decode takes it.
2020        // Greedy (with or without penalties) verifies by argmax equality.
2021        // Sampling (temperature > 0) can go through speculative SAMPLING —
2022        // draft from the MTP head's own post-chain distribution, accept
2023        // with min(1, p/q), correct from max(0, p − q); the emitted stream
2024        // is distributed exactly as the plain sampler's — but it is
2025        // OPT-IN (`CMF_GRAPH_SPEC_SAMPLE=1`): measured on Qwen3.8-27B /
2026        // RTX 5090 at the instruct row (0.7 / 0.80 / 20 / presence 1.5)
2027        // it decoded 19-22 tok/s against a plain 40 — nine post-chain
2028        // distributions a round plus a lower acceptance than greedy's,
2029        // against a verify that costs 2.7 single tokens. The greedy arms
2030        // pay +10%; the sampling arm needs a cheaper verify first.
2031        let spec_sampling_ok = self.sampler_config.temperature < 1e-6
2032            || std::env::var("CMF_GRAPH_SPEC_SAMPLE").as_deref() == Ok("1");
2033        // ON by default for greedy on the wgpu graph: with the draft on
2034        // the graph and the verify bit-exact, it measured 58.7 tok/s
2035        // against a plain 48.1 on Qwen3.8-27B q4tp / RTX 5090 (k=4) and
2036        // 51.1 against 49.4 on Qwen3.6-27B, and a round that stops
2037        // paying turns itself off below (acceptance watchdog).
2038        // `CMF_GRAPH_SPEC=0` disables; `=1` was the old opt-in spelling.
2039        // …but only where the batched verify has its register-blocked
2040        // kernel: q4tp dense FFNs (graph kind 6). q4t and q8_2f verify
2041        // through tile GEMMs today and measured a LOSS (q8_2f 22 against
2042        // 29 tok/s), the 2-bit plane the same; those stay opt-in
2043        // (`CMF_GRAPH_SPEC=1`).
2044        // …at least in nine dense FFNs of ten: a healed file carries its
2045        // last two layers at q8_2f, and two tile-GEMM verifies among 64 do
2046        // not change the arithmetic (measured: the healed q4tp file
2047        // decodes at the plain file's rate and would otherwise sit out).
2048        let (mut dense_n, mut dense_q4tp) = (0usize, 0usize);
2049        for lw in &self.weights.layers {
2050            if let FfnKind::Dense(d) = &lw.ffn {
2051                dense_n += 1;
2052                if matches!(d.gate_proj.graph_weight(), Some((_, _, 6, _)))
2053                    && matches!(d.up_proj.graph_weight(), Some((_, _, 6, _)))
2054                    && matches!(d.down_proj.graph_weight(), Some((_, _, 6, _)))
2055                {
2056                    dense_q4tp += 1;
2057                }
2058            }
2059        }
2060        let spec_default_ok = dense_n == 0 || dense_q4tp * 10 >= dense_n * 9;
2061        // Penalties break the draft head's agreement with the trunk (a
2062        // 1.1 repetition penalty measured 2 of 16 accepted): not by
2063        // default there either.
2064        let penalized = self.sampler_config.repetition_penalty != 1.0
2065            || self.sampler_config.presence_penalty != 0.0
2066            || !self.sampler_config.suppress_tokens.is_empty();
2067        // …and not on wgpu-over-Metal: the batched verify graph there
2068        // returned 0 accepted drafts and garbage text on a GDN hybrid
2069        // (16.08, Qwen3.5-0.8B) while Vulkan is bit-exact; the Mac's
2070        // default backend is native Metal without a batch graph anyway.
2071        #[cfg(feature = "gpu")]
2072        let metal_wgpu = graph_on && crate::gpu_wgpu::wgpu_backend_is_metal();
2073        #[cfg(not(feature = "gpu"))]
2074        let metal_wgpu = false;
2075        let spec_env = std::env::var("CMF_GRAPH_SPEC").ok();
2076        let spec_wanted = match spec_env.as_deref() {
2077            Some("0") => false,
2078            Some(_) => {
2079                if metal_wgpu {
2080                    tracing::warn!(
2081                        "CMF_GRAPH_SPEC forced on wgpu/Metal: the batched verify graph is not \
2082                         verified on this backend (garbage measured on Qwen3.5-0.8B)"
2083                    );
2084                }
2085                true
2086            }
2087            None => spec_default_ok && !penalized && !metal_wgpu,
2088        };
2089        // Native Metal: the b-row verify graph (`try_batch_graph_metal`)
2090        // stands where the wgpu batch graph stands on discrete cards.
2091        #[cfg(target_os = "macos")]
2092        let metal_graph = crate::gpu::q1_force()
2093            && crate::gpu::enabled_here()
2094            && std::env::var("CMF_GPU_BLOCK")
2095                .map(|v| v != "0")
2096                .unwrap_or(true);
2097        #[cfg(not(target_os = "macos"))]
2098        let metal_graph = false;
2099        let graph_spec = self.speculative
2100            && (graph_on || metal_graph)
2101            && self.mtp.is_some()
2102            && task_mask.is_none()
2103            && !self.o1_active()
2104            && spec_sampling_ok
2105            && spec_wanted;
2106        // GDN hybrids sit the fused-pair speculation out by default: the
2107        // recurrence is sequential, so the pair lane cannot parallelize
2108        // (the bench's own Pair line reads fused 1.28x TWO singles on the
2109        // 35B) and the draft's full-vocab head rides on top — measured 2x
2110        // SLOWER end to end (16.1 vs 32.4 tok/s on the 48-core stand).
2111        // CMF_MTP=1 forces it back for study.
2112        let pair_pays = self.gdn_cfg.is_none() || std::env::var("CMF_MTP").as_deref() == Ok("1");
2113        let spec_active = self.speculative
2114            && self.mtp.is_some()
2115            && task_mask.is_none()
2116            && !self.o1_active()
2117            && ((!graph_on && pair_pays && self.sampler_config.temperature < 1e-6) || graph_spec);
2118        // The MTP module is detached during generation so its mutable
2119        // state does not fight the borrow on `self`.
2120        let mut mtp = if spec_active { self.mtp.take() } else { None };
2121        if std::env::var("CMF_MTP_CHAIN_PROBE").is_ok() {
2122            eprintln!(
2123                "mtp-probe gate: spec_active={spec_active} mtp={} speculative={} graph_on={graph_on} temp_ok={}",
2124                mtp.is_some(),
2125                self.speculative,
2126                self.sampler_config.temperature < 1e-6,
2127            );
2128        }
2129        if let Some(m) = &mut mtp {
2130            m.kv.clear();
2131            // The MTP block's own device mirror starts over with its cache.
2132            crate::gpu::graph_kv_reset(self.mtp_kv_id());
2133            self.mtp_graph_mode = None;
2134        }
2135        // Dynamic router detached during decode (same borrow trick as MTP).
2136        // Speculative decode and dynamic routing are mutually exclusive
2137        // for now — the fused-pair path doesn't carry per-token φ.
2138        let mut router = if mtp.is_none() {
2139            self.dyn_router.take()
2140        } else {
2141            None
2142        };
2143        if let Some(r) = &mut router {
2144            r.reset(); // active=backbone, matching a fresh overlay
2145            self.dyn_phi_seen = 0; // fresh φ EMA per generation
2146            let _ = self.set_active_skill(None);
2147        }
2148
2149        let mut all_ids = input_ids.to_vec();
2150        let mut generated = 0usize;
2151        let mut finish_reason = "max_tokens".to_string();
2152        let mut drafted = 0usize;
2153        let mut accepted = 0usize;
2154        // DeepSeek-V4's draft quality is strongly content-dependent.  Two
2155        // consecutive paid rounds with no extra token put it on a bounded
2156        // cooldown; predictable text keeps batching, ordinary prose falls
2157        // back to the exact walk instead of paying a slow draft forever.
2158        // Local to one generation so one difficult request cannot poison the
2159        // next one, and deliberately automatic — this is not a user knob.
2160        let mut dsv4_spec_bad = 0usize;
2161        let mut dsv4_spec_retry_at = 0usize;
2162        let mut confidence: Vec<f32> = Vec::new();
2163        let trace_on = self.trace;
2164        let calib_temp = self.calib_temp;
2165        let mut traces: Vec<TokenTrace> = Vec::new();
2166
2167        // ── Prefill: forward each prompt token once, KEEP the last hidden.
2168        //    Dense prefill runs in fused pairs (weights streamed once per
2169        //    two positions — bit-identical to sequential, proven by the
2170        //    pair tests). With MTP: warm the draft head on
2171        //    (hidden_p, token_{p+1}) pairs.
2172        let mut hidden = vec![0.0f32; self.hidden_size];
2173        let mut pos = reuse_from;
2174        // lm_head-in-graph is only sound when the very next logits
2175        // consumer is this loop's own (MTP and skill routing interleave
2176        // other forwards / can swap lm_head between forward and sample).
2177        // CMF_GPU_LMHEAD=0 keeps lm_head off the graph: the token reads back
2178        // the 8 KB hidden instead of ~1 MB of logits, and the head runs on
2179        // the host. A probe for how much of the graph's fixed per-token cost
2180        // is the logits readback (the layer sweep puts that fixed part at
2181        // 3.88 ms of an 18.5 ms frame).
2182        let fuse_lm = mtp.is_none()
2183            && router.is_none()
2184            && std::env::var("CMF_GPU_LMHEAD").as_deref() != Ok("0");
2185        self.graph_logits = None;
2186        self.graph_want_logits = false;
2187        let _tpf = std::time::Instant::now();
2188        let batch_k = std::env::var("CMF_BATCH_K")
2189            .ok()
2190            .and_then(|v| v.parse::<usize>().ok())
2191            .unwrap_or(0);
2192        // DeepSeek-V4 owns a separate hyper-connection stack. Route it
2193        // before the generic prefill choices: those correctly reject an
2194        // empty `weights.layers`, but their final per-position fallback used
2195        // to consume the whole prompt before `dsv4::forward_chunk` could see
2196        // it. The batch implementation therefore existed without a live
2197        // production entry point.
2198        //
2199        // Bounded chunks preserve cancellation responsiveness. Only the
2200        // prompt's final chunk asks for logits; every earlier head projection
2201        // would produce 129 280 values that no caller reads.
2202        while self.qwen4_exp.is_some()
2203            && mtp.is_none()
2204            && pos < input_ids.len()
2205            && !self.cancel.load(std::sync::atomic::Ordering::Relaxed)
2206        {
2207            let token_id = input_ids[pos];
2208            let want_logits = pos + 1 == input_ids.len();
2209            let mut lg = Vec::new();
2210            if let Some(b) = &mut self.qwen4_exp {
2211                crate::qwen4_exp::forward_token(
2212                    &b.0,
2213                    &b.1,
2214                    &b.2,
2215                    &mut b.3,
2216                    token_id,
2217                    pos,
2218                    &self.inv_freq,
2219                    self.pool.as_deref(),
2220                    &mut lg,
2221                    want_logits,
2222                );
2223            }
2224            if want_logits {
2225                self.graph_logits = Some(lg);
2226            }
2227            pos += 1;
2228            hidden.fill(0.0);
2229        }
2230        while self.dsv4.is_some()
2231            && mtp.is_none()
2232            && pos < input_ids.len()
2233            && !self.cancel.load(std::sync::atomic::Ordering::Relaxed)
2234        {
2235            let end = (pos + prefill_chunk()).min(input_ids.len());
2236            let ids: Vec<u32> = input_ids[pos..end].to_vec();
2237            let mut lg = Vec::new();
2238            if let Some(b) = &mut self.dsv4 {
2239                let (g, layers, cfg, st) = (&b.0, &b.1, b.2, &mut b.3);
2240                crate::dsv4::forward_chunk(
2241                    g,
2242                    layers,
2243                    &cfg,
2244                    st,
2245                    &ids,
2246                    pos,
2247                    &self.inv_freq,
2248                    self.pool.as_deref(),
2249                    &mut lg,
2250                    end == input_ids.len(),
2251                );
2252            }
2253            if end == input_ids.len() {
2254                self.graph_logits = Some(lg);
2255            }
2256            pos = end;
2257            hidden = vec![0.0; self.hidden_size];
2258        }
2259        // With dynamic routing, prefill sequentially so the φ hook fires
2260        // over the PROMPT — the router enters decode with a warm φ (the
2261        // fused-pair path skips the per-layer φ capture). o1 layers
2262        // collect their query trace in both the single and pair paths.
2263        let dyn_prefill = router.is_some();
2264        // q1 hybrids on Metal: the per-position GPU token graph beats
2265        // the CPU chunk-GEMM (whose wall is the sequential scalar GDN
2266        // recurrence), so prefill goes position-by-position through the
2267        // same graph as decode. Pure-attention models keep the batched
2268        // path — there the chunk-GEMM amortization wins.
2269        let graph_prefill = self.graph_prefill_preferred();
2270        // Native Metal, q4tp GDN hybrids: the prompt through the b-row
2271        // rows graph — projections as GEMMs over up to 512 positions, the
2272        // GDN recurrence in registers on the device, K/V rows appended by
2273        // the chunk — instead of one token-graph submit per position (the
2274        // 27B: 8 tok/s → GEMM-bound). The MTP warm-up rows come out of one
2275        // batched run of the block per chunk. Any refusal leaves the rest
2276        // of the prompt to the sequential paths below.
2277        #[cfg(target_os = "macos")]
2278        if task_mask.is_none()
2279            && !dyn_prefill
2280            && crate::gpu::q1_force()
2281            && crate::gpu::enabled_here()
2282            && self.gdn_cfg.is_some()
2283            && self.g3n.is_none()
2284            && input_ids.len() > 8
2285            && std::env::var("CMF_MTP_CHAIN_PROBE").is_err()
2286            && std::env::var("CMF_METAL_PREFILL").as_deref() != Ok("0")
2287        {
2288            let chunk: usize = std::env::var("CMF_METAL_PREFILL_CHUNK")
2289                .ok()
2290                .and_then(|v| v.parse().ok())
2291                .filter(|&v| (16..=512).contains(&v))
2292                .unwrap_or(256);
2293            let hs = self.hidden_size;
2294            let _tp = std::time::Instant::now();
2295            while pos < input_ids.len() && !self.cancel.load(std::sync::atomic::Ordering::Relaxed) {
2296                let end = (pos + chunk).min(input_ids.len());
2297                let Some(hb) = self.prefill_batch_metal(&input_ids[pos..end], pos) else {
2298                    break;
2299                };
2300                if let Some(m) = &mut mtp {
2301                    let n_pairs = if end < input_ids.len() {
2302                        end - pos
2303                    } else {
2304                        end - pos - 1
2305                    };
2306                    if n_pairs > 0 {
2307                        let pairs: Vec<(&[f32], u32)> = (0..n_pairs)
2308                            .map(|j| (&hb[j * hs..(j + 1) * hs], input_ids[pos + j + 1]))
2309                            .collect();
2310                        if !self.mtp_warm_batch_metal(m, &pairs, pos) {
2311                            for (j, (h, t)) in pairs.iter().enumerate() {
2312                                let h = h.to_vec();
2313                                let _ = self.mtp_step(m, &h, *t, pos + j);
2314                            }
2315                        }
2316                    }
2317                }
2318                hidden.copy_from_slice(&hb[(end - pos - 1) * hs..]);
2319                pos = end;
2320            }
2321            if std::env::var("CMF_PREFILL_PROF").is_ok() {
2322                eprintln!(
2323                    "metal-prefill: {} of {} tokens in {:.1} ms",
2324                    pos,
2325                    input_ids.len(),
2326                    _tp.elapsed().as_secs_f64() * 1e3
2327                );
2328            }
2329        }
2330        if task_mask.is_none()
2331            && !dyn_prefill
2332            && !graph_prefill
2333            && self.can_prefill_batched()
2334            && self.g3n.is_none()
2335            && input_ids.len() > 2
2336        {
2337            // Production prefill = the same chunked prefill-GEMM that
2338            // bench/PPL measure (roadmap §3 P0: generation used to warm
2339            // the prompt with the slower pair path — the published
2340            // prefill number didn't match real TTFT). MTP warm-up reads
2341            // each position's hidden straight from the chunk result.
2342            let chunk = prefill_chunk();
2343            let hs = self.hidden_size;
2344            while pos < input_ids.len() && !self.cancel.load(std::sync::atomic::Ordering::Relaxed) {
2345                let end = (pos + chunk).min(input_ids.len());
2346                let hb = self.prefill_batch(&input_ids[pos..end], pos);
2347                if let Some(m) = &mut mtp {
2348                    let probe: usize = std::env::var("CMF_MTP_CHAIN_PROBE")
2349                        .ok()
2350                        .and_then(|v| v.parse().ok())
2351                        .unwrap_or(0);
2352                    for p in pos..end {
2353                        if p + 1 < input_ids.len() {
2354                            if probe >= 1 && p + 2 < input_ids.len() {
2355                                // Teacher-forced chain acceptance (see the
2356                                // tail loop's twin): the warm-up row stays,
2357                                // the chain's rows roll back.
2358                                let (d1, mut hx) = self.mtp_step_h(
2359                                    m,
2360                                    &hb[(p - pos) * hs..(p - pos + 1) * hs],
2361                                    input_ids[p + 1],
2362                                    p,
2363                                );
2364                                let mut ok = d1 == input_ids[p + 2];
2365                                Self::chain_probe_note(0, ok);
2366                                let mut d_prev = d1;
2367                                let mut extra = 0usize;
2368                                for j in 1..probe {
2369                                    if p + 2 + j >= input_ids.len() {
2370                                        break;
2371                                    }
2372                                    let (dj, hj) = self.mtp_step_h(m, &hx, d_prev, p + 1 + j);
2373                                    extra += 1;
2374                                    ok = ok && dj == input_ids[p + 2 + j];
2375                                    Self::chain_probe_note(j, ok);
2376                                    d_prev = dj;
2377                                    hx = hj;
2378                                }
2379                                m.kv.truncate_last(extra);
2380                            } else {
2381                                let _ = self.mtp_step(
2382                                    m,
2383                                    &hb[(p - pos) * hs..(p - pos + 1) * hs],
2384                                    input_ids[p + 1],
2385                                    p,
2386                                );
2387                            }
2388                        }
2389                    }
2390                }
2391                hidden.copy_from_slice(&hb[(end - pos - 1) * hs..]);
2392                pos = end;
2393            }
2394        }
2395        let pair_off = std::env::var("CMF_PAIR").is_ok_and(|v| v == "0");
2396        if task_mask.is_none()
2397            && !dyn_prefill
2398            && !graph_prefill
2399            && !pair_off
2400            && self.pair_supported()
2401        {
2402            while pos + 1 < input_ids.len()
2403                && !self.cancel.load(std::sync::atomic::Ordering::Relaxed)
2404            {
2405                let e1 = self.embed_single(input_ids[pos]);
2406                let e2 = self.embed_single(input_ids[pos + 1]);
2407                let (h1, h2) = self.forward_pair(&e1, &e2, pos);
2408                // Both prefill tokens are real → commit lane-2 states.
2409                self.commit_linear_scratch();
2410                if let Some(m) = &mut mtp {
2411                    let _ = self.mtp_step(m, &h1, input_ids[pos + 1], pos);
2412                    if pos + 2 < input_ids.len() {
2413                        let probe: usize = std::env::var("CMF_MTP_CHAIN_PROBE")
2414                            .ok()
2415                            .and_then(|v| v.parse().ok())
2416                            .unwrap_or(0);
2417                        if probe >= 1 && pos + 3 < input_ids.len() {
2418                            // Same teacher-forced chain table as the tail
2419                            // loop below, fed from the pair path that owns
2420                            // most prefill positions.
2421                            let (d1, mut hx) = self.mtp_step_h(m, &h2, input_ids[pos + 2], pos + 1);
2422                            let mut ok = d1 == input_ids[pos + 3];
2423                            Self::chain_probe_note(0, ok);
2424                            let mut d_prev = d1;
2425                            let mut extra = 0usize;
2426                            for j in 1..probe {
2427                                if pos + 3 + j >= input_ids.len() {
2428                                    break;
2429                                }
2430                                let (dj, hj) = self.mtp_step_h(m, &hx, d_prev, pos + 2 + j);
2431                                extra += 1;
2432                                ok = ok && dj == input_ids[pos + 3 + j];
2433                                Self::chain_probe_note(j, ok);
2434                                d_prev = dj;
2435                                hx = hj;
2436                            }
2437                            m.kv.truncate_last(extra);
2438                        } else {
2439                            let _ = self.mtp_step(m, &h2, input_ids[pos + 2], pos + 1);
2440                        }
2441                    }
2442                }
2443                hidden = h2;
2444                pos += 2;
2445            }
2446        }
2447        // Batched GPU prefill for the wgpu decode graph (GDN hybrids): K prompt
2448        // positions per submit — projections/FFN as GEMMs (weight once per K),
2449        // attention/GDN looped inside — instead of one whole-graph submit per
2450        // position. Falls through to the per-position graph on any refusal.
2451        // Batched prefill is opt-in (CMF_BATCH_K>0). Default 0 = per-position
2452        // graph prefill. (Steady-state decode is provably identical either way —
2453        // token-graph submit and lm_head both unchanged — so this only trades
2454        // prefill wall.)
2455        if batch_k > 0
2456            && graph_prefill
2457            && task_mask.is_none()
2458            && !self.o1_active()
2459            && mtp.is_none()
2460            && !dyn_prefill
2461            && pos + 1 < input_ids.len()
2462        {
2463            let hs = self.hidden_size;
2464            let chunk = batch_k;
2465            while pos < input_ids.len() {
2466                let end = (pos + chunk).min(input_ids.len());
2467                let bk = end - pos;
2468                let mut hiddens = vec![0f32; bk * hs];
2469                for (j, &id) in input_ids[pos..end].iter().enumerate() {
2470                    hiddens[j * hs..(j + 1) * hs].copy_from_slice(&self.embed_single(id));
2471                }
2472                let positions: Vec<usize> = (pos..end).collect();
2473                let t_chunk = std::time::Instant::now();
2474                let ok_b = self.try_batch_graph_wgpu(&mut hiddens, &positions, bk, None);
2475                if std::env::var("CMF_GRAPH_PROF").is_ok() {
2476                    let ms = t_chunk.elapsed().as_secs_f64() * 1000.0;
2477                    eprintln!(
2478                        "batch-chunk: k={bk} ok={ok_b} {ms:.1} ms ({:.1} tok/s)",
2479                        bk as f64 / (ms / 1000.0)
2480                    );
2481                }
2482                {
2483                    use std::sync::atomic::{AtomicBool, Ordering};
2484                    static SAID: AtomicBool = AtomicBool::new(false);
2485                    if !SAID.swap(true, Ordering::Relaxed) {
2486                        if ok_b {
2487                            tracing::info!("batched prefill: ACTIVE (k={bk})");
2488                        } else {
2489                            tracing::warn!("batched prefill declined — per-position graph");
2490                        }
2491                    }
2492                }
2493                if ok_b {
2494                    hidden.copy_from_slice(&hiddens[(bk - 1) * hs..]);
2495                    pos = end;
2496                } else {
2497                    break; // unsupported → per-position graph handles the rest
2498                }
2499            }
2500        }
2501        while pos < input_ids.len() && !self.cancel.load(std::sync::atomic::Ordering::Relaxed) {
2502            self.graph_want_logits = fuse_lm && pos + 1 == input_ids.len();
2503            hidden = self.forward_layers(&self.embed_single(input_ids[pos]), pos, task_mask);
2504            if let Some(m) = &mut mtp {
2505                if pos + 1 < input_ids.len() {
2506                    // `CMF_MTP_CHAIN_PROBE=k`: teacher-forced acceptance of a
2507                    // CHAINED draft — iterate the head on its own hidden k
2508                    // deep and score every depth against the prompt's real
2509                    // continuation. The economics of a k-token speculative
2510                    // round stand or fall on this table.
2511                    let probe: usize = std::env::var("CMF_MTP_CHAIN_PROBE")
2512                        .ok()
2513                        .and_then(|v| v.parse().ok())
2514                        .unwrap_or(0);
2515                    if probe >= 1 && pos + 2 < input_ids.len() {
2516                        let (d1, mut hx) = self.mtp_step_h(m, &hidden, input_ids[pos + 1], pos);
2517                        let mut ok = d1 == input_ids[pos + 2];
2518                        Self::chain_probe_note(0, ok);
2519                        let mut d_prev = d1;
2520                        let mut extra = 0usize;
2521                        for j in 1..probe {
2522                            if pos + 2 + j >= input_ids.len() {
2523                                break;
2524                            }
2525                            let (dj, hj) = self.mtp_step_h(m, &hx, d_prev, pos + 1 + j);
2526                            extra += 1;
2527                            ok = ok && dj == input_ids[pos + 2 + j];
2528                            Self::chain_probe_note(j, ok);
2529                            d_prev = dj;
2530                            hx = hj;
2531                        }
2532                        // The chain's rows are speculation, not the prompt —
2533                        // keep only the warmup row the plain path would add.
2534                        m.kv.truncate_last(extra);
2535                    } else {
2536                        let _ = self.mtp_step(m, &hidden, input_ids[pos + 1], pos);
2537                    }
2538                }
2539            }
2540            pos += 1;
2541        }
2542        if std::env::var("CMF_PREFILL_PROF").is_ok() {
2543            eprintln!(
2544                "prefill: {} tokens in {:.1} ms (batch_k={batch_k})",
2545                input_ids.len(),
2546                _tpf.elapsed().as_secs_f64() * 1000.0
2547            );
2548        }
2549        // Cancelled mid-prefill: the cache holds a partial prompt —
2550        // drop the reuse history and return an empty generation.
2551        if self
2552            .cancel
2553            .swap(false, std::sync::atomic::Ordering::Relaxed)
2554        {
2555            self.kv_history.clear();
2556            if let Some(m) = mtp {
2557                self.mtp = Some(m);
2558            }
2559            return Ok(GenerateResult {
2560                text: String::new(),
2561                token_ids: Vec::new(),
2562                prompt_tokens: input_ids.len(),
2563                tokens_generated: 0,
2564                finish_reason: "cancelled".to_string(),
2565                mtp_drafted: 0,
2566                mtp_accepted: 0,
2567                token_confidence: Vec::new(),
2568                traces: Vec::new(),
2569            });
2570        }
2571
2572        // Prompt absorbed → freeze the o1 layers' skeletons; from here
2573        // every decode step on those layers is O(W + m·dv + m²).
2574        self.o1_seal();
2575
2576        // Commit one token: push, check EOS, stream. Returns false = stop.
2577        macro_rules! commit {
2578            ($id:expr) => {{
2579                all_ids.push($id);
2580                generated += 1;
2581                if self.tokenizer.is_eos($id) {
2582                    finish_reason = "stop".to_string();
2583                    false
2584                } else {
2585                    let token_text = self.tokenizer.decode_token($id);
2586                    let mut go = true;
2587                    if let Some(ref mut cb) = on_token {
2588                        if !cb(&token_text) {
2589                            finish_reason = "cancelled".to_string();
2590                            go = false;
2591                        }
2592                    }
2593                    go
2594                }
2595            }};
2596        }
2597
2598        // Speculation is decided by MEASUREMENT, not by an acceptance
2599        // model. A k=4 round costs ~3.8 plain tokens on the 5090 (draft
2600        // 6.6 + verify 66.6 + commit 4.8 ms against a 20.6 ms token), so it
2601        // pays only when the head lands ~2.8 of 4 — predictable text (code,
2602        // structured output) does, free prose often does not, and the
2603        // ratio at which the two cross depends on the card and the context
2604        // depth. So: four speculative rounds timed, then eight plain
2605        // tokens timed, and the faster arm runs until a re-check 256
2606        // tokens later (context growth moves the balance). The trial
2607        // costs at most a few tokens of the slower arm per 256.
2608        let mut spec_trial = SpecTrial::Spec {
2609            t0: std::time::Instant::now(),
2610            gen0: generated,
2611            rounds: 0,
2612        };
2613        let mut spec_mon = SpecMon::default();
2614        let mut spec_watchdog_off = false;
2615        // ── Decode ──
2616        let mut next_pos = input_ids.len();
2617        'decode: while generated < max_tokens {
2618            if self
2619                .cancel
2620                .swap(false, std::sync::atomic::Ordering::Relaxed)
2621            {
2622                finish_reason = "cancelled".to_string();
2623                break 'decode;
2624            }
2625            // A rejected speculative draft already drew this position's
2626            // token from the residual distribution (graph_spec_step); it
2627            // is committed as-is — sampling again from the row's logits
2628            // would bias the stream toward the target's mode.
2629            let forced = self.spec_forced.take();
2630            let mut logits = match (forced, self.graph_logits.take()) {
2631                (Some(_), _) => Vec::new(),
2632                (None, Some(lg)) => lg,
2633                (None, None) => {
2634                    inference::rms_norm_into(
2635                        &hidden,
2636                        &self.weights.final_norm,
2637                        self.rms_eps,
2638                        self.norm_style,
2639                        &mut self.ws.n1,
2640                    );
2641                    self.lm_head_forward(&self.ws.n1)
2642                }
2643            };
2644            // CMF_LOGIT_DUMP=<path>: the first decode step's hidden + logits
2645            // as raw f32 (hidden first) — cross-backend numerics diffing.
2646            if generated
2647                == std::env::var("CMF_LOGIT_DUMP_STEP")
2648                    .ok()
2649                    .and_then(|v| v.parse().ok())
2650                    .unwrap_or(0)
2651            {
2652                if let Ok(path) = std::env::var("CMF_LOGIT_DUMP") {
2653                    let mut bytes: Vec<u8> = Vec::with_capacity((hidden.len() + logits.len()) * 4);
2654                    for v in hidden.iter().chain(logits.iter()) {
2655                        bytes.extend_from_slice(&v.to_le_bytes());
2656                    }
2657                    let _ = std::fs::write(&path, &bytes);
2658                }
2659            }
2660            let t_next = match forced {
2661                Some(c) => c,
2662                None => sampler::sample_with_scratch_pool(
2663                    &logits,
2664                    &self.sampler_config,
2665                    &all_ids,
2666                    &mut self.rng,
2667                    &mut self.sampler_scratch,
2668                    self.pool.as_deref(),
2669                ),
2670            };
2671            if self.confidence_on {
2672                confidence.push(if logits.is_empty() {
2673                    0.0
2674                } else {
2675                    sampler::top1_prob_pool(
2676                        self.pool.as_deref(),
2677                        &mut self.sampler_scratch,
2678                        &logits,
2679                        t_next,
2680                        calib_temp,
2681                    )
2682                });
2683            }
2684            if !logits.is_empty() {
2685                attention::recycle_buf(&mut logits);
2686            }
2687            if trace_on {
2688                // active_skill = the overlay in force while this token was
2689                // generated; recon/switched are filled after the post-emit
2690                // routing eval below (freshest coherence for this token).
2691                let skill = router.as_ref().and_then(|r| r.active_id());
2692                traces.push(TokenTrace {
2693                    t: generated,
2694                    token_id: t_next,
2695                    confidence: confidence.last().copied().unwrap_or(0.0),
2696                    active_skill: skill,
2697                    recon: None,
2698                    switched: false,
2699                });
2700            }
2701            if !commit!(t_next) {
2702                break 'decode;
2703            }
2704            if generated >= max_tokens {
2705                break 'decode;
2706            }
2707
2708            if self.kv_cache.needs_eviction() {
2709                // Say it ONCE, loudly: past this point the model keeps
2710                // talking but has lost half its context, and on a GDN
2711                // hybrid the graph's device state goes stale on top. The
2712                // Qwen3.8 bring-up spent a day reading this cliff as
2713                // three different model bugs.
2714                static SAID: std::sync::Once = std::sync::Once::new();
2715                SAID.call_once(|| {
2716                    tracing::warn!(
2717                        "KV cache full at {} positions — evicting half; quality \
2718                         will degrade. Raise CMF_MAX_SEQ.",
2719                        self.kv_cache.max_seq_len,
2720                    );
2721                });
2722                let keep = (self.kv_cache.max_seq_len / 2).max(1);
2723                self.kv_cache.evict(keep);
2724            }
2725
2726            // Advance the speculation trial: plain-phase accounting and
2727            // the periodic re-check happen here, on every token.
2728            if graph_spec {
2729                match spec_trial {
2730                    SpecTrial::Plain { t0, gen0 } if generated >= gen0 + 8 => {
2731                        spec_mon.plain_ms =
2732                            t0.elapsed().as_secs_f64() * 1e3 / (generated - gen0) as f64;
2733                        let keep = spec_mon.pays();
2734                        tracing::info!(
2735                            "speculation trial: {:.2} tok/round in {:.1} ms vs plain {:.1} ms/tok — {}",
2736                            spec_mon.tokens,
2737                            spec_mon.round_ms,
2738                            spec_mon.plain_ms,
2739                            if keep { "speculating" } else { "plain" }
2740                        );
2741                        spec_mon.fails = 0;
2742                        spec_trial = SpecTrial::Decided {
2743                            spec: keep,
2744                            recheck_at: if keep { usize::MAX } else { generated + 128 },
2745                        };
2746                    }
2747                    SpecTrial::Decided { recheck_at, .. } if generated >= recheck_at => {
2748                        spec_mon.n = 0;
2749                        spec_trial = SpecTrial::Spec {
2750                            t0: std::time::Instant::now(),
2751                            gen0: generated,
2752                            rounds: 0,
2753                        };
2754                    }
2755                    _ => {}
2756                }
2757                spec_watchdog_off = matches!(
2758                    spec_trial,
2759                    SpecTrial::Plain { .. } | SpecTrial::Decided { spec: false, .. }
2760                );
2761            }
2762            match &mut mtp {
2763                // ── Graph speculation: chain-draft, batch-verify on device ──
2764                #[cfg(feature = "gpu")]
2765                Some(m)
2766                    if graph_spec
2767                        && !spec_watchdog_off
2768                        && generated + 1 < max_tokens
2769                        && next_pos > 0 =>
2770                {
2771                    let t_round = std::time::Instant::now();
2772                    if let Some((extra, n_pos, new_h)) = self.graph_spec_step(
2773                        m,
2774                        &hidden,
2775                        t_next,
2776                        next_pos,
2777                        &mut drafted,
2778                        &mut accepted,
2779                        &mut all_ids,
2780                    ) {
2781                        next_pos = n_pos;
2782                        hidden = new_h;
2783                        if std::env::var("CMF_GRAPH_SPEC_TIME").is_ok() {
2784                            eprintln!(
2785                                "spec-round wall {:.1} ms → {} tokens",
2786                                t_round.elapsed().as_secs_f64() * 1e3,
2787                                extra.len() + 1
2788                            );
2789                        }
2790                        // One speculative round done: the monitor counts it
2791                        // (round 1 untimed — it pays the batch scratch and
2792                        // the draft mirror), and the trial advances.
2793                        spec_mon.round(t_round.elapsed().as_secs_f64() * 1e3, extra.len() + 1);
2794                        // the round's tokens land in `generated` below; the
2795                        // plain phase must start counting AFTER them
2796                        spec_trial = Self::spec_trial_round(
2797                            spec_trial,
2798                            &mut spec_mon,
2799                            generated + extra.len() + 1,
2800                        );
2801                        let mut stopped = false;
2802                        for &id in &extra {
2803                            if self.confidence_on {
2804                                confidence.push(0.0);
2805                            }
2806                            if !commit!(id) {
2807                                stopped = true;
2808                                break;
2809                            }
2810                        }
2811                        if stopped {
2812                            break 'decode;
2813                        }
2814                        continue 'decode;
2815                    }
2816                    // Declined (batch graph refused): plain forward below —
2817                    // and a round that produced one token for the trial's
2818                    // ledger, so a graph that keeps refusing is measured out
2819                    // like a head that keeps missing (it was spinning
2820                    // forever on a file whose batch graph declines).
2821                    // A declined round is not a cheap one-token round — it
2822                    // is a verify that does not exist for this file (a
2823                    // healed q8_2f tail measured 760 drafts, 0 accepted, 33
2824                    // against 48.8 tok/s while the monitor called the draft
2825                    // alone "paying"). Count it as the losing streak in one.
2826                    spec_mon.round(t_round.elapsed().as_secs_f64() * 1e3, 1);
2827                    spec_mon.tokens = 0.0;
2828                    spec_mon.fails = 3;
2829                    spec_trial = Self::spec_trial_round(spec_trial, &mut spec_mon, generated + 1);
2830                    hidden = self.forward_layers(&self.embed_single(t_next), next_pos, task_mask);
2831                    next_pos += 1;
2832                    continue 'decode;
2833                }
2834                // ── Speculative: draft t+2, verify in a fused pair ──
2835                Some(m) if !graph_spec && generated + 1 < max_tokens => {
2836                    let draft = self.mtp_step(m, &hidden, t_next, next_pos - 1);
2837                    drafted += 1;
2838                    let emb1 = self.embed_single(t_next);
2839                    let emb2 = self.embed_single(draft);
2840                    let (h1, h2) = self.forward_pair(&emb1, &emb2, next_pos);
2841
2842                    inference::rms_norm_into(
2843                        &h1,
2844                        &self.weights.final_norm,
2845                        self.rms_eps,
2846                        self.norm_style,
2847                        &mut self.ws.n1,
2848                    );
2849                    let mut logits1 = self.lm_head_forward(&self.ws.n1);
2850                    let t_after = sampler::sample_with_scratch_pool(
2851                        &logits1,
2852                        &self.sampler_config,
2853                        &all_ids,
2854                        &mut self.rng,
2855                        &mut self.sampler_scratch,
2856                        self.pool.as_deref(),
2857                    );
2858                    if self.confidence_on {
2859                        confidence.push(sampler::top1_prob_pool(
2860                            self.pool.as_deref(),
2861                            &mut self.sampler_scratch,
2862                            &logits1,
2863                            t_after,
2864                            calib_temp,
2865                        ));
2866                    }
2867                    attention::recycle_buf(&mut logits1);
2868                    if trace_on {
2869                        // Speculative decode is mutually exclusive with
2870                        // dynamic routing (router is None here) — no skill.
2871                        traces.push(TokenTrace {
2872                            t: generated,
2873                            token_id: t_after,
2874                            confidence: confidence.last().copied().unwrap_or(0.0),
2875                            active_skill: None,
2876                            recon: None,
2877                            switched: false,
2878                        });
2879                    }
2880                    let stop = !commit!(t_after);
2881
2882                    if t_after == draft {
2883                        accepted += 1;
2884                        self.commit_linear_scratch();
2885                        let _ = self.mtp_step(m, &h1, t_after, next_pos);
2886                        hidden = h2;
2887                        next_pos += 2;
2888                    } else {
2889                        // The draft lane is wrong: roll its KV entry back.
2890                        for layer in &mut self.kv_cache.layers {
2891                            layer.truncate_last(1);
2892                        }
2893                        if !stop {
2894                            let _ = self.mtp_step(m, &h1, t_after, next_pos);
2895                            hidden = self.forward_layers(
2896                                &self.embed_single(t_after),
2897                                next_pos + 1,
2898                                None,
2899                            );
2900                        }
2901                        next_pos += 2;
2902                    }
2903                    if stop {
2904                        break 'decode;
2905                    }
2906                }
2907                // ── Vanilla: forward the sampled token ──
2908                _ => {
2909                    // ── DeepSeek-V4 speculative decode (CMF_DSV4_SPEC=1):
2910                    // draft five on the card, verify batched, commit the
2911                    // accepted prefix. Greedy only; a rejected token's state
2912                    // is restored and replayed, so output equals the walk. ──
2913                    #[cfg(feature = "gpu")]
2914                    if Self::dsv4_spec_on() && self.dsv4.is_some() {
2915                        static SAID: std::sync::Once = std::sync::Once::new();
2916                        SAID.call_once(|| {
2917                            eprintln!(
2918                                "dsv4-spec гейт: mtp={} mask={} router={} trace={} temp={} rep={} ",
2919                                !self.dsv4_mtp.is_empty(),
2920                                task_mask.is_none(),
2921                                router.is_none(),
2922                                !trace_on,
2923                                self.sampler_config.temperature < 1e-6,
2924                                self.sampler_config.repetition_penalty == 1.0,
2925                            );
2926                        });
2927                    }
2928                    #[cfg(feature = "gpu")]
2929                    if Self::dsv4_spec_on()
2930                        && self.dsv4.is_some()
2931                        && !self.dsv4_mtp.is_empty()
2932                        && task_mask.is_none()
2933                        && router.is_none()
2934                        && !trace_on
2935                        && self.sampler_config.temperature < 1e-6
2936                        && self.sampler_config.repetition_penalty == 1.0
2937                        && generated + 1 < max_tokens
2938                        && all_ids.len() >= 2
2939                        && generated >= dsv4_spec_retry_at
2940                    {
2941                        let tip_token = all_ids[all_ids.len() - 2];
2942                        let drafted0 = drafted;
2943                        let round = self.dsv4_spec_step(
2944                            tip_token,
2945                            t_next,
2946                            next_pos,
2947                            max_tokens.saturating_sub(generated),
2948                            &mut drafted,
2949                            &mut accepted,
2950                        );
2951                        if drafted > drafted0 {
2952                            let useful = round.as_ref().is_some_and(|(extra, _)| !extra.is_empty());
2953                            if useful {
2954                                dsv4_spec_bad = 0;
2955                            } else {
2956                                dsv4_spec_bad += 1;
2957                                if dsv4_spec_bad >= 2 {
2958                                    dsv4_spec_bad = 0;
2959                                    dsv4_spec_retry_at = generated.saturating_add(32);
2960                                    tracing::info!(
2961                                        "dsv4: draft не окупился дважды — точный walk на 32 токена"
2962                                    );
2963                                }
2964                            }
2965                        }
2966                        if let Some((extra, n_pos)) = round {
2967                            next_pos = n_pos;
2968                            let mut stopped = false;
2969                            for &id in &extra {
2970                                if self.confidence_on {
2971                                    confidence.push(0.0);
2972                                }
2973                                if !commit!(id) {
2974                                    stopped = true;
2975                                    break;
2976                                }
2977                            }
2978                            if stopped {
2979                                break 'decode;
2980                            }
2981                            continue 'decode;
2982                        }
2983                    }
2984                    self.graph_want_logits = fuse_lm;
2985                    // Greedy burst (CMF_MULTISTEP, default 8, 1 = off): while
2986                    // nothing observes per-token state — pure argmax sampling,
2987                    // no router/trace/confidence/mask — decode k tokens per
2988                    // submit and commit them wholesale. The trailing normal
2989                    // forward leaves logits for the loop top, as always.
2990                    let mut t_fwd = t_next;
2991                    let pure_greedy = self.sampler_config.temperature < 1e-6
2992                        && self.sampler_config.repetition_penalty == 1.0
2993                        && self.sampler_config.suppress_tokens.is_empty();
2994                    // Off by default: at every k the burst measured at or
2995                    // below the plain path on this graph shape (k=1 loses
2996                    // the argmax dispatches vs a 1 MB readback, k>=8 loses
2997                    // inter-step drains vs the saved sync). Experimental.
2998                    let burst_k = std::env::var("CMF_MULTISTEP")
2999                        .ok()
3000                        .and_then(|v| v.parse::<usize>().ok())
3001                        .unwrap_or(0);
3002                    if pure_greedy
3003                        && burst_k >= 1
3004                        && fuse_lm
3005                        && task_mask.is_none()
3006                        && router.is_none()
3007                        && !trace_on
3008                        && !self.confidence_on
3009                    {
3010                        let mut stopped = false;
3011                        loop {
3012                            let room = max_tokens.saturating_sub(generated);
3013                            if room <= 2 {
3014                                break;
3015                            }
3016                            let k = burst_k.min(room - 1);
3017                            if k < 1 {
3018                                break;
3019                            }
3020                            let Some(ids) = self.try_multi_burst(t_fwd, next_pos, k) else {
3021                                break;
3022                            };
3023                            next_pos += k;
3024                            for &id in &ids {
3025                                if !commit!(id) {
3026                                    stopped = true;
3027                                    break;
3028                                }
3029                            }
3030                            if stopped {
3031                                break;
3032                            }
3033                            t_fwd = *ids.last().unwrap();
3034                        }
3035                        if stopped {
3036                            break 'decode;
3037                        }
3038                    }
3039                    hidden = self.forward_layers(&self.embed_single(t_fwd), next_pos, task_mask);
3040                    next_pos += 1;
3041                    // Dynamic routing: the forward updated φ; ask the
3042                    // router whether to switch skills before the next token.
3043                    if let Some(r) = &mut router {
3044                        let phi = self.dyn_phi_ema.clone();
3045                        let decision = r.step(&phi, generated);
3046                        if let Some(new_active) = decision {
3047                            let _ = self.set_active_skill(new_active);
3048                        }
3049                        // Backfill this token's coherence + switch flag from
3050                        // the just-run eval (freshest measured values).
3051                        if trace_on {
3052                            if let Some(last) = traces.last_mut() {
3053                                let e = r.last_best_e();
3054                                last.recon = e.is_finite().then_some(e);
3055                                last.switched = decision.is_some();
3056                            }
3057                        }
3058                    }
3059                }
3060            }
3061        }
3062
3063        self.graph_want_logits = false;
3064        self.graph_logits = None;
3065        // Restore backbone overlay and re-attach the router for reuse.
3066        if router.is_some() {
3067            let _ = self.set_active_skill(None);
3068        }
3069        self.dyn_router = router.or(self.dyn_router.take());
3070        self.mtp = mtp.or(self.mtp.take());
3071
3072        let output_ids = &all_ids[input_ids.len()..];
3073        // Forwarded = prompt + all generated but the LAST sampled token
3074        // (emitted without being fed back). Exact only without MTP —
3075        // reuse is gated off when MTP is active.
3076        let forwarded = input_ids.len() + output_ids.len().saturating_sub(1);
3077        self.kv_history = all_ids[..forwarded.min(all_ids.len())].to_vec();
3078        confidence.truncate(output_ids.len()); // guard against any overshoot
3079        traces.truncate(output_ids.len());
3080        Ok(GenerateResult {
3081            text: self.tokenizer.decode(output_ids),
3082            token_ids: output_ids.to_vec(),
3083            prompt_tokens: input_ids.len(),
3084            tokens_generated: generated,
3085            finish_reason,
3086            mtp_drafted: drafted,
3087            mtp_accepted: accepted,
3088            token_confidence: confidence,
3089            traces,
3090        })
3091    }
3092
3093    /// One MTP step: feed `(hidden_p, token_{p+1})` into the draft head,
3094    /// advance its KV cache at position `p`, return the drafted token
3095    /// for position `p+2`.
3096    fn mtp_step(
3097        &mut self,
3098        m: &mut MtpModule,
3099        hidden: &[f32],
3100        next_token: u32,
3101        position: usize,
3102    ) -> u32 {
3103        self.mtp_step_h(m, hidden, next_token, position).0
3104    }
3105
3106    /// Tally for `CMF_MTP_CHAIN_PROBE`: per depth, how often the CHAIN is
3107    /// still an exact prefix of the real continuation. Printed every 128
3108    /// depth-0 samples so a killed run still shows its table.
3109    fn chain_probe_note(depth: usize, prefix_ok: bool) {
3110        use std::sync::Mutex;
3111        static T: Mutex<Vec<(u64, u64)>> = Mutex::new(Vec::new());
3112        let mut t = T.lock().unwrap();
3113        if t.len() <= depth {
3114            t.resize(depth + 1, (0, 0));
3115        }
3116        t[depth].0 += 1;
3117        t[depth].1 += prefix_ok as u64;
3118        if depth == 0 && t[0].0 % 128 == 0 {
3119            let line: Vec<String> = t
3120                .iter()
3121                .enumerate()
3122                .map(|(d, (n, k))| {
3123                    format!(
3124                        "d{}={:.0}%({n})",
3125                        d + 1,
3126                        100.0 * *k as f64 / (*n).max(1) as f64
3127                    )
3128                })
3129                .collect();
3130            eprintln!("mtp-chain: {}", line.join(" "));
3131        }
3132    }
3133
3134    /// `mtp_step` that also hands back the block's own output hidden — the
3135    /// state a CHAINED draft feeds the next step, the way a multi-token
3136    /// speculative round iterates the head on itself.
3137    /// One MTP block step from (trunk hidden, token): the head's LOGITS
3138    /// and the block's own hidden for chaining. The draft is argmax of the
3139    /// logits on the greedy path and a draw from their post-chain
3140    /// distribution on the sampling path.
3141    fn mtp_step_hl(
3142        &mut self,
3143        m: &mut MtpModule,
3144        hidden: &[f32],
3145        next_token: u32,
3146        position: usize,
3147    ) -> (Vec<f32>, Vec<f32>) {
3148        // The graph arm: the MTP block as a one-layer token graph with the
3149        // head fused — device attention over the block's own KV mirror,
3150        // one submit for block + head, hidden and logits back together.
3151        // Decided once per generation (see `mtp_graph_mode`).
3152        #[cfg(target_os = "macos")]
3153        if self.mtp_graph_mode != Some(false) && crate::gpu::q1_force() {
3154            if let Some(r) = self.mtp_step_metal(m, hidden, next_token, position, true) {
3155                self.mtp_graph_mode = Some(true);
3156                return r;
3157            }
3158            self.mtp_graph_mode = Some(false);
3159        }
3160        #[cfg(feature = "gpu")]
3161        if self.mtp_graph_mode != Some(false) {
3162            if let Some(r) = self.mtp_step_graph(m, hidden, next_token, position) {
3163                self.mtp_graph_mode = Some(true);
3164                return r;
3165            }
3166            if self.mtp_graph_mode == Some(true) {
3167                // The graph carried this generation's MTP KV and just
3168                // declined — the CPU cache is not current. A draft from
3169                // stale attention is still only a draft (verify decides),
3170                // but say so once.
3171                tracing::warn!("mtp graph declined mid-run — draft falls to the per-op path");
3172            }
3173            self.mtp_graph_mode = Some(false);
3174        }
3175        // fc concat order is [enorm(embed); hnorm(hidden)] — EMBEDDING
3176        // FIRST. Verified by the oracle (converter/mtp_oracle.py):
3177        // [emb;hid] → 45.8% acceptance, [hid;emb] → 0.00%.
3178        let e = self.embed_single(next_token);
3179        let mut cat = vec![0.0f32; 2 * self.hidden_size];
3180        let (cat_e, cat_h) = cat.split_at_mut(self.hidden_size);
3181        inference::rms_norm_into(&e, &m.enorm, self.rms_eps, self.norm_style, cat_e);
3182        inference::rms_norm_into(hidden, &m.hnorm, self.rms_eps, self.norm_style, cat_h);
3183        let mut x = vec![0.0f32; self.hidden_size];
3184        m.eh_proj.matvec(&cat, &mut x, self.pool.as_deref());
3185
3186        // One standard transformer block over the MTP's own cache.
3187        let lw = &m.layer;
3188        inference::rms_norm_into(
3189            &x,
3190            &lw.input_norm,
3191            self.rms_eps,
3192            self.norm_style,
3193            &mut self.ws.n1,
3194        );
3195        let attn = match &lw.attn {
3196            // MLA models carry no MTP head; this path cannot see them.
3197            AttnKind::Mla(_) => unreachable!("MLA has no MTP/pair path"),
3198            AttnKind::Kda(_) => unreachable!("KDA has no MTP/pair path"),
3199            AttnKind::Full {
3200                wq,
3201                wk,
3202                wv,
3203                wo,
3204                q_norm,
3205                k_norm,
3206                output_gate,
3207                softplus_gate,
3208                bias,
3209            } => {
3210                let mut cfg = self.attn_cfg(position);
3211                cfg.q_norm = q_norm.as_deref();
3212                cfg.k_norm = k_norm.as_deref();
3213                cfg.output_gate = *output_gate;
3214                cfg.softplus_gate = softplus_gate
3215                    .as_ref()
3216                    .map(|(gate, per_head)| (gate, *per_head));
3217                cfg.bias = bias
3218                    .as_ref()
3219                    .map(|(q, k, v)| (q.as_slice(), k.as_slice(), v.as_slice()));
3220                attention::qwen_attention(&self.ws.n1, wq, wk, wv, wo, &mut m.kv, &cfg)
3221            }
3222            AttnKind::Linear(_) | AttnKind::LinearGdn(_) | AttnKind::ShortConv(_) => {
3223                unreachable!("MTP block is full attention")
3224            }
3225        };
3226        for (i, &a) in attn.iter().enumerate() {
3227            x[i] += a;
3228        }
3229        inference::rms_norm_into(
3230            &x,
3231            &lw.post_norm,
3232            self.rms_eps,
3233            self.norm_style,
3234            &mut self.ws.p1,
3235        );
3236        let ffn = ffn_forward(&lw.ffn, &self.ws.p1, self.pool.as_deref(), None);
3237        for (i, &f) in ffn.iter().enumerate() {
3238            x[i] += f;
3239        }
3240
3241        inference::rms_norm_into(
3242            &x,
3243            &m.final_norm,
3244            self.rms_eps,
3245            self.norm_style,
3246            &mut self.ws.n1,
3247        );
3248        let lg = self.lm_head_forward(&self.ws.n1);
3249        (lg, x)
3250    }
3251
3252    /// `mtp_step_hl` reduced to the greedy draft: argmax of the head.
3253    fn mtp_step_h(
3254        &mut self,
3255        m: &mut MtpModule,
3256        hidden: &[f32],
3257        next_token: u32,
3258        position: usize,
3259    ) -> (u32, Vec<f32>) {
3260        let (mut lg, x) = self.mtp_step_hl(m, hidden, next_token, position);
3261        let draft = sampler::argmax(&lg);
3262        attention::recycle_buf(&mut lg);
3263        (draft, x)
3264    }
3265
3266    /// One speculative round for the trial: rounds 1..5 of a `Spec` phase
3267    /// advance it (the monitor already averaged this round); after five,
3268    /// the plain phase runs (once — a known plain rate decides at once);
3269    /// a decided speculation keeps re-checking the rule every round and
3270    /// stops after four losing rounds in a row.
3271    fn spec_trial_round(trial: SpecTrial, mon: &mut SpecMon, generated: usize) -> SpecTrial {
3272        match trial {
3273            SpecTrial::Spec { t0, gen0, rounds } => {
3274                let rounds = rounds + 1;
3275                if rounds >= 5 {
3276                    if mon.plain_ms > 0.0 {
3277                        let keep = mon.pays();
3278                        mon.fails = 0;
3279                        tracing::info!(
3280                            "speculation re-check: {:.2} tok/round in {:.1} ms vs plain {:.1} ms/tok — {}",
3281                            mon.tokens,
3282                            mon.round_ms,
3283                            mon.plain_ms,
3284                            if keep { "speculating" } else { "plain" }
3285                        );
3286                        SpecTrial::Decided {
3287                            spec: keep,
3288                            recheck_at: if keep { usize::MAX } else { generated + 128 },
3289                        }
3290                    } else {
3291                        SpecTrial::Plain {
3292                            t0: std::time::Instant::now(),
3293                            gen0: generated,
3294                        }
3295                    }
3296                } else {
3297                    SpecTrial::Spec { t0, gen0, rounds }
3298                }
3299            }
3300            SpecTrial::Decided { spec: true, .. } => {
3301                if mon.pays() {
3302                    mon.fails = 0;
3303                    trial
3304                } else {
3305                    mon.fails += 1;
3306                    if mon.fails >= 4 {
3307                        tracing::info!(
3308                            "speculation stopped: {:.2} tok/round in {:.1} ms vs plain {:.1} ms/tok",
3309                            mon.tokens,
3310                            mon.round_ms,
3311                            mon.plain_ms
3312                        );
3313                        SpecTrial::Decided {
3314                            spec: false,
3315                            recheck_at: generated + 128,
3316                        }
3317                    } else {
3318                        trial
3319                    }
3320                }
3321            }
3322            other => other,
3323        }
3324    }
3325
3326    /// The MTP block's device-mirror id: the trunk's id with a high bit,
3327    /// so the (kv_id, layer) mirror keys never collide.
3328    fn mtp_kv_id(&self) -> u64 {
3329        self.graph_kv_id | (1u64 << 40)
3330    }
3331
3332    /// The MTP block's mirror layer index: 0 — its own kv_id keeps it
3333    /// apart from the trunk, and the BATCH graph (the warm-up path) keys
3334    /// its mirrors at layer 0 with no base of its own, so the draft's
3335    /// token graph must key the same slot.
3336    const MTP_LAYER_BASE: usize = 0;
3337
3338    /// The block's input from (trunk hidden, token): eh_proj · [enorm(e);
3339    /// hnorm(h)] — the same arithmetic the per-op path starts with.
3340    fn mtp_block_input(&mut self, m: &MtpModule, hidden: &[f32], next_token: u32) -> Vec<f32> {
3341        let e = self.embed_single(next_token);
3342        let mut cat = vec![0.0f32; 2 * self.hidden_size];
3343        let (cat_e, cat_h) = cat.split_at_mut(self.hidden_size);
3344        inference::rms_norm_into(&e, &m.enorm, self.rms_eps, self.norm_style, cat_e);
3345        inference::rms_norm_into(hidden, &m.hnorm, self.rms_eps, self.norm_style, cat_h);
3346        let mut x = vec![0.0f32; self.hidden_size];
3347        m.eh_proj.matvec(&cat, &mut x, self.pool.as_deref());
3348        x
3349    }
3350
3351    /// Is the MTP block graphable at all (device up, full attention
3352    /// without softplus, dense FFN)? The plan itself is built per call.
3353    #[cfg(feature = "gpu")]
3354    fn mtp_graph_ok(&self, m: &MtpModule) -> bool {
3355        if std::env::var("CMF_MTP_GRAPH").as_deref() == Ok("0") {
3356            return false;
3357        }
3358        if !crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Decode)
3359            || !crate::gpu::enabled_here()
3360            || self.attn_softcap > 0.0
3361            || self.attention_heads_per_layer.is_some()
3362        {
3363            return false;
3364        }
3365        matches!(
3366            &m.layer.attn,
3367            AttnKind::Full {
3368                softplus_gate: None,
3369                ..
3370            }
3371        ) && matches!(&m.layer.ffn, FfnKind::Dense(_))
3372    }
3373
3374    /// One MTP block step on the wgpu token graph: block + fused head in
3375    /// one submit, the block hidden and the logits read back together.
3376    /// None = the graph cannot take this block (softplus gate, non-dense
3377    /// FFN, unquantized head, no device) — the caller keeps the per-op
3378    /// path for the whole generation.
3379    #[cfg(feature = "gpu")]
3380    fn mtp_step_graph(
3381        &mut self,
3382        m: &mut MtpModule,
3383        hidden: &[f32],
3384        next_token: u32,
3385        position: usize,
3386    ) -> Option<(Vec<f32>, Vec<f32>)> {
3387        if !self.mtp_graph_ok(m) {
3388            return None;
3389        }
3390        let lw = &m.layer;
3391        let AttnKind::Full {
3392            wq,
3393            wk,
3394            wv,
3395            wo,
3396            q_norm,
3397            k_norm,
3398            output_gate,
3399            softplus_gate,
3400            bias,
3401        } = &lw.attn
3402        else {
3403            return None;
3404        };
3405        if softplus_gate.is_some() {
3406            return None;
3407        }
3408        let FfnKind::Dense(d) = &lw.ffn else {
3409            return None;
3410        };
3411        if !d.segs.is_empty() {
3412            return None; // tube layers run on the segmented path
3413        }
3414        // The block's input first: it borrows `self` mutably (embed scratch,
3415        // pool), the plan below borrows the weights immutably.
3416        let mut x = self.mtp_block_input(m, hidden, next_token);
3417        fn gw(t: &QTensor) -> Option<crate::gpu::GraphW<'_>> {
3418            let (_, i, kind, rs) = t.graph_weight()?;
3419            Some(crate::gpu::GraphW {
3420                idx: i,
3421                kind,
3422                row_scale: rs,
3423                data: &[],
3424            })
3425        }
3426        let (model, _, _, _) = wq.graph_weight()?;
3427        let model = model.clone();
3428        let (lm_gw, lm_rows) = {
3429            let (_, i, kind, rs) = self.weights.lm_head.graph_weight()?;
3430            (
3431                crate::gpu::GraphW {
3432                    idx: i,
3433                    kind,
3434                    row_scale: rs,
3435                    data: &[],
3436                },
3437                self.weights.lm_head.rows(),
3438            )
3439        };
3440        let layer = crate::gpu::GraphLayer {
3441            input_norm: &lw.input_norm,
3442            attn: crate::gpu::GraphAttn::Full {
3443                wq: gw(wq)?,
3444                wk: gw(wk)?,
3445                wv: gw(wv)?,
3446                wo: gw(wo)?,
3447                q_norm: q_norm.as_deref(),
3448                k_norm: k_norm.as_deref(),
3449                bias: bias
3450                    .as_ref()
3451                    .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
3452                output_gate: *output_gate,
3453                cpu_k: m.kv.k_heads(),
3454                cpu_v: m.kv.v_heads(),
3455            },
3456            post_norm: &lw.post_norm,
3457            ffn: crate::gpu::GraphFfn::Dense {
3458                gate: gw(&d.gate_proj)?,
3459                up: gw(&d.up_proj)?,
3460                down: gw(&d.down_proj)?,
3461            },
3462        };
3463        let nh = self.num_heads;
3464        let (nkv, hd, rd) = self.layer_geom(0);
3465        let gemma = self.norm_style == cortiq_core::NormStyle::Gemma;
3466        let mut logits = Vec::new();
3467        let ok = crate::gpu::forward_token_graph(
3468            &model,
3469            self.mtp_kv_id(),
3470            std::slice::from_ref(&layer),
3471            &[None],
3472            self.o1_epoch,
3473            &self.inv_freq,
3474            &mut x,
3475            nh,
3476            nkv,
3477            hd,
3478            rd,
3479            self.hidden_size,
3480            self.intermediate_size,
3481            position,
3482            self.kv_cache.max_seq_len,
3483            gemma,
3484            self.rms_eps as f32,
3485            Some((&lm_gw, lm_rows)),
3486            &m.final_norm,
3487            &mut logits,
3488            &[],
3489            1,
3490            None,
3491            None,
3492            None,
3493            Self::MTP_LAYER_BASE,
3494            true,
3495        );
3496        if !ok {
3497            return None;
3498        }
3499        logits.resize(self.vocab_size, 0.0);
3500        Some((logits, x))
3501    }
3502
3503    /// The warm-ups of one speculative round on the device: every accepted
3504    /// (hidden, token) pair as ONE batched graph run over the MTP block
3505    /// (no head) — its kv_append lands the pairs in the block's mirror.
3506    /// `pairs` are consecutive positions from `first_pos`. False = the
3507    /// batch graph declined; the caller warms one by one on the token
3508    /// graph (prefix mode) instead.
3509    #[cfg(feature = "gpu")]
3510    fn mtp_warm_graph(
3511        &mut self,
3512        m: &mut MtpModule,
3513        pairs: &[(&[f32], u32)],
3514        first_pos: usize,
3515    ) -> bool {
3516        if pairs.is_empty() || !self.mtp_graph_ok(m) {
3517            return pairs.is_empty();
3518        }
3519        let hs = self.hidden_size;
3520        // Block inputs for every pair (eh_proj on the per-op path, one
3521        // matvec each — the plan's own prologue).
3522        let mut hiddens = Vec::with_capacity(pairs.len() * hs);
3523        for (h, t) in pairs {
3524            hiddens.extend_from_slice(&self.mtp_block_input(m, h, *t));
3525        }
3526        let lw = &m.layer;
3527        let AttnKind::Full {
3528            wq,
3529            wk,
3530            wv,
3531            wo,
3532            q_norm,
3533            k_norm,
3534            output_gate,
3535            bias,
3536            ..
3537        } = &lw.attn
3538        else {
3539            return false;
3540        };
3541        let FfnKind::Dense(d) = &lw.ffn else {
3542            return false;
3543        };
3544        if !d.segs.is_empty() {
3545            return false; // tube layers run on the segmented path
3546        }
3547        fn gw(t: &QTensor) -> Option<crate::gpu::GraphW<'_>> {
3548            let (_, i, kind, rs) = t.graph_weight()?;
3549            Some(crate::gpu::GraphW {
3550                idx: i,
3551                kind,
3552                row_scale: rs,
3553                data: &[],
3554            })
3555        }
3556        let Some((model, _, _, _)) = wq.graph_weight() else {
3557            return false;
3558        };
3559        let model = model.clone();
3560        let (Some(gwq), Some(gwk), Some(gwv), Some(gwo), Some(gg), Some(gu), Some(gd)) = (
3561            gw(wq),
3562            gw(wk),
3563            gw(wv),
3564            gw(wo),
3565            gw(&d.gate_proj),
3566            gw(&d.up_proj),
3567            gw(&d.down_proj),
3568        ) else {
3569            return false;
3570        };
3571        let layer = crate::gpu::GraphLayer {
3572            input_norm: &lw.input_norm,
3573            attn: crate::gpu::GraphAttn::Full {
3574                wq: gwq,
3575                wk: gwk,
3576                wv: gwv,
3577                wo: gwo,
3578                q_norm: q_norm.as_deref(),
3579                k_norm: k_norm.as_deref(),
3580                bias: bias
3581                    .as_ref()
3582                    .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
3583                output_gate: *output_gate,
3584                cpu_k: m.kv.k_heads(),
3585                cpu_v: m.kv.v_heads(),
3586            },
3587            post_norm: &lw.post_norm,
3588            ffn: crate::gpu::GraphFfn::Dense {
3589                gate: gg,
3590                up: gu,
3591                down: gd,
3592            },
3593        };
3594        let positions: Vec<usize> = (first_pos..first_pos + pairs.len()).collect();
3595        let nh = self.num_heads;
3596        let (nkv, hd, rd) = self.layer_geom(0);
3597        let gemma = self.norm_style == cortiq_core::NormStyle::Gemma;
3598        crate::gpu::forward_batch_graph(
3599            &model,
3600            self.mtp_kv_id(),
3601            std::slice::from_ref(&layer),
3602            &self.inv_freq,
3603            &mut hiddens,
3604            nh,
3605            nkv,
3606            hd,
3607            rd,
3608            hs,
3609            self.intermediate_size,
3610            &positions,
3611            self.kv_cache.max_seq_len,
3612            gemma,
3613            self.rms_eps as f32,
3614            pairs.len(),
3615            None,
3616        )
3617    }
3618
3619    /// The MTP block alone — advance its KV with a (hidden, token) pair the
3620    /// verify just proved, without paying the head. What keeps the draft's
3621    /// attention context warm between speculative rounds.
3622    fn mtp_warm(&mut self, m: &mut MtpModule, hidden: &[f32], next_token: u32, position: usize) {
3623        let e = self.embed_single(next_token);
3624        let mut cat = vec![0.0f32; 2 * self.hidden_size];
3625        let (cat_e, cat_h) = cat.split_at_mut(self.hidden_size);
3626        inference::rms_norm_into(&e, &m.enorm, self.rms_eps, self.norm_style, cat_e);
3627        inference::rms_norm_into(hidden, &m.hnorm, self.rms_eps, self.norm_style, cat_h);
3628        let mut x = vec![0.0f32; self.hidden_size];
3629        m.eh_proj.matvec(&cat, &mut x, self.pool.as_deref());
3630        inference::rms_norm_into(
3631            &x,
3632            &m.layer.input_norm,
3633            self.rms_eps,
3634            self.norm_style,
3635            &mut self.ws.n1,
3636        );
3637        let attn = match &m.layer.attn {
3638            AttnKind::Full {
3639                wq,
3640                wk,
3641                wv,
3642                wo,
3643                q_norm,
3644                k_norm,
3645                output_gate,
3646                softplus_gate,
3647                bias,
3648            } => {
3649                let mut cfg = self.attn_cfg(position);
3650                cfg.q_norm = q_norm.as_deref();
3651                cfg.k_norm = k_norm.as_deref();
3652                cfg.output_gate = *output_gate;
3653                cfg.softplus_gate = softplus_gate.as_ref().map(|(g, p)| (g, *p));
3654                cfg.bias = bias
3655                    .as_ref()
3656                    .map(|(q, k, v)| (q.as_slice(), k.as_slice(), v.as_slice()));
3657                attention::qwen_attention(&self.ws.n1, wq, wk, wv, wo, &mut m.kv, &cfg)
3658            }
3659            _ => return,
3660        };
3661        let _ = attn;
3662    }
3663
3664    /// Speculative decode ON the wgpu whole-token graph: draft k with the
3665    /// MTP head, verify all of them plus the tip in ONE batched graph
3666    /// submit whose tail folds the head, commit the accepted prefix and
3667    /// roll the GDN state back to the last real position. Greedy only —
3668    /// output equals the plain graph's token for token, the way the DSV4
3669    /// verify equals the walk.
3670    #[cfg(feature = "gpu")]
3671    #[allow(clippy::too_many_arguments)]
3672    fn graph_spec_step(
3673        &mut self,
3674        m: &mut MtpModule,
3675        hidden: &[f32],
3676        t_next: u32,
3677        next_pos: usize,
3678        drafted: &mut usize,
3679        accepted: &mut usize,
3680        // The committed stream (prompt + generated so far, `t_next`
3681        // included): the sampler chain's penalties read it, and the
3682        // sampling arm extends it with the drafts position by position.
3683        all_ids: &mut Vec<u32>,
3684    ) -> Option<(Vec<u32>, usize, Vec<f32>)> {
3685        // 3 is the measured optimum on Qwen3.6-27B / RTX 5090 (medians
3686        // of three, greedy): 51.1 tok/s against a plain 49.4, where k=2
3687        // gives 46.1, k=4 50.0, k=5 47.4, k=6 45.2. Acceptance is 89-91%
3688        // throughout — what turns the curve over is the verify, which
3689        // costs ~7.4 ms per extra position, and the draft ~3 ms a step.
3690        // 4 since the draft moved onto the graph (Qwen3.8-27B / 5090:
3691        // k=3 51.2, k=4 51.8 with the per-op draft; the graph draft
3692        // halves the draft cost, so the extra draft is cheaper still).
3693        // 5 with the int8 verify (the default: measured 76.5 against
3694        // k=4's 72-74 and k=6's 74 on the 5090), 4 with the f32 one.
3695        #[cfg(target_os = "macos")]
3696        let metal_native = crate::gpu::q1_force();
3697        #[cfg(not(target_os = "macos"))]
3698        let metal_native = false;
3699        #[cfg(feature = "gpu")]
3700        let k_default = if metal_native {
3701            // the Metal verify's GEMM tile is 8 rows wide and flat in b:
3702            // seven drafts + the tip fill it for free
3703            7
3704        } else if crate::gpu_wgpu::verify_i8_on() {
3705            5
3706        } else {
3707            4
3708        };
3709        #[cfg(not(feature = "gpu"))]
3710        let k_default = 4;
3711        let k_spec: usize = std::env::var("CMF_GRAPH_SPEC_K")
3712            .ok()
3713            .and_then(|v| v.parse().ok())
3714            .filter(|&v| (1..=8).contains(&v))
3715            .unwrap_or(k_default);
3716        if next_pos == 0 {
3717            return None;
3718        }
3719        let t_round = std::time::Instant::now();
3720        // Submissions per phase — and they say where the round's money is.
3721        // Qwen3.6-27B on an RTX 5090, k=3:
3722        //
3723        //   draft   9.3 ms / 12 submissions   (four per MTP step)
3724        //   verify 52.8 ms /  1               (the batched graph)
3725        //   commit  5.4 ms /  6               (two per warm)
3726        //
3727        // The verify is already one submit. The draft's own work is 834 MB
3728        // a step — 0.8 ms at this card's measured 1056 GB/s — against 3.1
3729        // ms measured, so ~0.58 ms of every step is round trip, not
3730        // arithmetic, and the same holds for the warms. Eighteen round
3731        // trips a round at roughly half a millisecond each is ~11 ms of a
3732        // 68 ms round: fusing the MTP block into ONE submit the way the
3733        // trunk already is projects to ~64 tok/s against today's 50.9.
3734        // That is the largest measured item left on this path.
3735        let subs = || crate::gpu_wgpu::SUBMITS.load(std::sync::atomic::Ordering::Relaxed);
3736        let sub0 = subs();
3737        // Greedy without penalties verifies by argmax equality (bit-exact
3738        // against the plain path). Anything else is speculative SAMPLING:
3739        // each draft is a DRAW from the MTP head's post-chain distribution
3740        // q_j, kept for the accept test; the verify's rows give p_j.
3741        let cfg = self.sampler_config.clone();
3742        let penalized = !(cfg.repetition_penalty == 1.0
3743            && cfg.presence_penalty == 0.0
3744            && cfg.suppress_tokens.is_empty());
3745        // Three verify regimes: plain greedy (argmax of the raw rows),
3746        // greedy WITH penalties (argmax of the penalized rows — a single
3747        // pass each, no distributions), and sampling (draw / accept /
3748        // correct on post-chain distributions).
3749        let greedy_pen = cfg.temperature < 1e-6 && penalized;
3750        let sampling = cfg.temperature >= 1e-6;
3751        // Sampling with a top-k goes through the SPARSE chain: the dense
3752        // one builds nine 248k-float distributions a round (four drafts,
3753        // five verify rows) and measured 19-22 tok/s against a plain 40 —
3754        // the host, not the card. Sparse, the same nine cost tens of
3755        // microseconds each.
3756        let sparse = sampling && sampler::sparse_ok(&cfg);
3757        let base_len = all_ids.len();
3758        if sampling && !sparse && self.spec_q.len() < k_spec {
3759            self.spec_q.resize_with(k_spec, Vec::new);
3760        }
3761        if sparse && self.spec_qs.len() < k_spec {
3762            self.spec_qs.resize_with(k_spec, Vec::new);
3763        }
3764        // Draft the chain: first from the trunk's tip hidden, then the head
3765        // iterating on itself. Rows land in the MTP KV; the chain rows past
3766        // the first are speculation over speculative state and roll back
3767        // below, replaced by verified pairs.
3768        let mut drafts = Vec::with_capacity(k_spec);
3769        let mut hx = hidden.to_vec();
3770        // CMF_SPEC_DBG=1: draft 0 through BOTH MTP arms (graph and per-op)
3771        // from the same inputs — are the arms the difference, or the inputs?
3772        let spec_dbg = std::env::var("CMF_SPEC_DBG").is_ok();
3773        for j in 0..k_spec {
3774            let tok_in = if j == 0 { t_next } else { drafts[j - 1] };
3775            let mut dbg_ref: Option<(Vec<f32>, Vec<f32>)> = None;
3776            if spec_dbg {
3777                let saved = self.mtp_graph_mode;
3778                self.mtp_graph_mode = Some(false);
3779                let r = self.mtp_step_hl(m, &hx, tok_in, next_pos - 1 + j);
3780                self.mtp_graph_mode = saved;
3781                m.kv.truncate_last(1);
3782                dbg_ref = Some(r);
3783            }
3784            let (mut lg, hj) = self.mtp_step_hl(m, &hx, tok_in, next_pos - 1 + j);
3785            if let Some((lg_cpu, h_cpu)) = dbg_ref {
3786                let n = |v: &[f32]| v.iter().map(|x| x * x).sum::<f32>().sqrt();
3787                let dl = lg
3788                    .iter()
3789                    .zip(&lg_cpu)
3790                    .fold(0f32, |m, (a, b)| m.max((a - b).abs()));
3791                let dh = hj
3792                    .iter()
3793                    .zip(&h_cpu)
3794                    .fold(0f32, |m, (a, b)| m.max((a - b).abs()));
3795                eprintln!(
3796                    "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 {}",
3797                    next_pos - 1 + j,
3798                    sampler::argmax(&lg_cpu),
3799                    sampler::argmax(&lg),
3800                    n(&h_cpu),
3801                    n(&hj),
3802                    m.kv.seq_len
3803                );
3804            }
3805            let dj = if sparse {
3806                let mut q = std::mem::take(&mut self.spec_qs[j]);
3807                let ok = sampler::sparse_distribution_into(
3808                    &lg,
3809                    &cfg,
3810                    all_ids,
3811                    &mut self.sampler_scratch,
3812                    self.pool.as_deref(),
3813                    &mut q,
3814                );
3815                let d = if ok {
3816                    sampler::draw_sparse(&q, &mut self.rng)
3817                } else {
3818                    // everything filtered: the dense chain's greedy fallback
3819                    let t = sampler::argmax(&lg);
3820                    q.clear();
3821                    q.push((t, 1.0));
3822                    t
3823                };
3824                self.spec_qs[j] = q;
3825                all_ids.push(d);
3826                d
3827            } else if sampling {
3828                let mut q = std::mem::take(&mut self.spec_q[j]);
3829                sampler::distribution_into(
3830                    &lg,
3831                    &cfg,
3832                    all_ids,
3833                    &mut self.sampler_scratch,
3834                    self.pool.as_deref(),
3835                    &mut q,
3836                );
3837                let d = sampler::draw(&q, &mut self.rng);
3838                self.spec_q[j] = q;
3839                all_ids.push(d); // the next draft's penalties see this one
3840                d
3841            } else if greedy_pen {
3842                let d = sampler::argmax_penalized(
3843                    &lg,
3844                    &cfg,
3845                    all_ids,
3846                    &mut self.sampler_scratch,
3847                    self.pool.as_deref(),
3848                );
3849                all_ids.push(d);
3850                d
3851            } else {
3852                sampler::argmax(&lg)
3853            };
3854            attention::recycle_buf(&mut lg);
3855            drafts.push(dj);
3856            hx = hj;
3857        }
3858        all_ids.truncate(base_len);
3859        *drafted += k_spec;
3860        let t_draft = t_round.elapsed();
3861        let sub_draft = subs();
3862        // Verify batch: [t_next, d1 .. d_{k-1}] at next_pos.. — every row's
3863        // logits come back from the graph's own head.
3864        let b = k_spec + 1;
3865        let mut hiddens = vec![0.0f32; b * self.hidden_size];
3866        for (i, &t) in std::iter::once(&t_next).chain(drafts.iter()).enumerate() {
3867            let e = self.embed_single(t);
3868            hiddens[i * self.hidden_size..(i + 1) * self.hidden_size].copy_from_slice(&e);
3869        }
3870        let positions: Vec<usize> = (next_pos..next_pos + b).collect();
3871        let (lm_gw, lm_rows) = {
3872            let (_, i, kind, rs) = self.weights.lm_head.graph_weight()?;
3873            (
3874                crate::gpu::GraphW {
3875                    idx: i,
3876                    kind,
3877                    row_scale: rs,
3878                    data: &[],
3879                },
3880                self.weights.lm_head.rows(),
3881            )
3882        };
3883        let mut logits = Vec::new();
3884        let final_norm = self.weights.final_norm.clone();
3885        #[cfg(target_os = "macos")]
3886        let ok = if metal_native {
3887            let lm = self.weights.lm_head.q1_parts()?;
3888            self.try_batch_graph_metal(
3889                &mut hiddens,
3890                &positions,
3891                b,
3892                Some((lm, &final_norm, &mut logits)),
3893            )
3894        } else {
3895            self.try_batch_graph_wgpu(
3896                &mut hiddens,
3897                &positions,
3898                b,
3899                Some(crate::gpu::SpecTail {
3900                    lm: lm_gw,
3901                    lm_rows,
3902                    final_norm: &final_norm,
3903                    logits_out: &mut logits,
3904                }),
3905            )
3906        };
3907        #[cfg(not(target_os = "macos"))]
3908        let ok = self.try_batch_graph_wgpu(
3909            &mut hiddens,
3910            &positions,
3911            b,
3912            Some(crate::gpu::SpecTail {
3913                lm: lm_gw,
3914                lm_rows,
3915                final_norm: &final_norm,
3916                logits_out: &mut logits,
3917            }),
3918        );
3919        if !ok {
3920            // Roll the draft rows back out of the MTP cache and decline —
3921            // the caller runs the plain path, nothing has changed.
3922            m.kv.truncate_last(k_spec);
3923            return None;
3924        }
3925        // `CMF_METAL_VERIFY_CHECK=1`: run the same b tokens through the
3926        // plain per-token path and compare each row's argmax + logits with
3927        // the verify's — the bring-up oracle for the batched graph. The
3928        // plain forwards mutate the CPU state; it is snapshotted and put
3929        // back, and the K/V mirrors re-pointed, before the round goes on.
3930        #[cfg(target_os = "macos")]
3931        if metal_native && std::env::var("CMF_METAL_VERIFY_CHECK").as_deref() == Ok("1") {
3932            let snap: Vec<Vec<f32>> = self
3933                .kv_cache
3934                .layers
3935                .iter()
3936                .map(|l| l.linear_state.clone())
3937                .collect();
3938            let attn_lens: Vec<usize> = self.kv_cache.layers.iter().map(|l| l.seq_len).collect();
3939            let toks: Vec<u32> = std::iter::once(t_next)
3940                .chain(drafts.iter().copied())
3941                .collect();
3942            let want_save = self.graph_want_logits;
3943            self.graph_want_logits = false;
3944            for (i, &t) in toks.iter().enumerate() {
3945                let hi = self.forward_layers(&self.embed_single(t), next_pos + i, None);
3946                let _ = self.graph_logits.take();
3947                // CMF_SPEC_PLAIN_HIDDEN=1: the next round drafts from the
3948                // plain path's hidden instead of the verify's (an experiment
3949                // on the chain's sensitivity to the half-GEMM noise)
3950                if std::env::var("CMF_SPEC_PLAIN_HIDDEN").as_deref() == Ok("1") {
3951                    hiddens[i * self.hidden_size..(i + 1) * self.hidden_size].copy_from_slice(&hi);
3952                }
3953                let ref_lg = self.logits_from_hidden(&hi);
3954                let row = &logits[i * lm_rows..(i + 1) * lm_rows];
3955                let ra = sampler::argmax(&ref_lg);
3956                let va = sampler::argmax(row);
3957                let mut md = 0f32;
3958                let mut rms = 0f64;
3959                for j in 0..lm_rows.min(ref_lg.len()) {
3960                    let d = (ref_lg[j] - row[j]).abs();
3961                    md = md.max(d);
3962                    rms += (d as f64) * (d as f64);
3963                }
3964                let mut hd = 0f32;
3965                for j in 0..self.hidden_size {
3966                    hd = hd.max((hi[j] - hiddens[i * self.hidden_size + j]).abs());
3967                }
3968                eprintln!(
3969                    "verify-check row {i} tok {t} pos {}: ref argmax {ra} verify argmax {va} {} | max|dlogit| {md:.3} rms {:.4} | max|dhidden| {hd:.4}",
3970                    next_pos + i,
3971                    if ra == va { "OK" } else { "MISMATCH" },
3972                    (rms / lm_rows as f64).sqrt()
3973                );
3974            }
3975            self.graph_want_logits = want_save;
3976            // restore IN PLACE: the pending verify graph wraps these very
3977            // allocations (zero-copy) — replacing the Vec would strand it
3978            for (l, st) in self.kv_cache.layers.iter_mut().zip(snap) {
3979                if l.linear_state.len() == st.len() {
3980                    l.linear_state.copy_from_slice(&st);
3981                } else {
3982                    l.linear_state = st;
3983                }
3984            }
3985            for (li, (l, n0)) in self.kv_cache.layers.iter_mut().zip(attn_lens).enumerate() {
3986                let extra = l.seq_len.saturating_sub(n0);
3987                if extra > 0 {
3988                    l.truncate_last(extra);
3989                    crate::gpu_metal::kv_mirror_set_stored(self.graph_kv_id, li, n0);
3990                }
3991            }
3992        }
3993        let t_verify = t_round.elapsed();
3994        let sub_verify = subs();
3995        // Acceptance. Greedy: row i's argmax is the trunk's token after
3996        // input i. Sampling: accept draft i with min(1, p_i/q_i), and on
3997        // the first rejection draw the correction from max(0, p_i − q_i)
3998        // — that token is committed by the loop top as-is (spec_forced).
3999        let mut a = 0usize;
4000        let mut forced: Option<u32> = None;
4001        let ids: Vec<u32> = if sparse {
4002            let mut p = std::mem::take(&mut self.spec_ps);
4003            let mut res = std::mem::take(&mut self.spec_ress);
4004            while a < k_spec {
4005                let ok = sampler::sparse_distribution_into(
4006                    &logits[a * lm_rows..(a + 1) * lm_rows],
4007                    &cfg,
4008                    all_ids,
4009                    &mut self.sampler_scratch,
4010                    self.pool.as_deref(),
4011                    &mut p,
4012                );
4013                if !ok {
4014                    let t = sampler::argmax(&logits[a * lm_rows..(a + 1) * lm_rows]);
4015                    p.clear();
4016                    p.push((t, 1.0));
4017                }
4018                match sampler::spec_accept_or_correct_sparse(
4019                    &p,
4020                    &self.spec_qs[a],
4021                    drafts[a],
4022                    &mut self.rng,
4023                    &mut res,
4024                ) {
4025                    None => {
4026                        all_ids.push(drafts[a]);
4027                        a += 1;
4028                    }
4029                    Some(c) => {
4030                        forced = Some(c);
4031                        break;
4032                    }
4033                }
4034            }
4035            all_ids.truncate(base_len);
4036            self.spec_ps = p;
4037            self.spec_ress = res;
4038            drafts.clone()
4039        } else if sampling {
4040            let mut p = std::mem::take(&mut self.spec_p);
4041            let mut res = std::mem::take(&mut self.spec_res);
4042            while a < k_spec {
4043                sampler::distribution_into(
4044                    &logits[a * lm_rows..(a + 1) * lm_rows],
4045                    &cfg,
4046                    all_ids,
4047                    &mut self.sampler_scratch,
4048                    self.pool.as_deref(),
4049                    &mut p,
4050                );
4051                match sampler::spec_accept_or_correct(
4052                    &p,
4053                    &self.spec_q[a],
4054                    drafts[a],
4055                    &mut self.rng,
4056                    &mut res,
4057                    self.pool.as_deref(),
4058                ) {
4059                    None => {
4060                        all_ids.push(drafts[a]);
4061                        a += 1;
4062                    }
4063                    Some(c) => {
4064                        forced = Some(c);
4065                        break;
4066                    }
4067                }
4068            }
4069            all_ids.truncate(base_len);
4070            self.spec_p = p;
4071            self.spec_res = res;
4072            // the accepted drafts ARE the verified tokens after inputs 0..a
4073            drafts.clone()
4074        } else if greedy_pen {
4075            // Row i's penalized argmax, penalties over the stream that
4076            // includes the accepted drafts before it — the plain loop's
4077            // exact arithmetic, one pass per row, no working copy.
4078            let mut ids: Vec<u32> = Vec::with_capacity(b);
4079            for i in 0..b {
4080                let t = sampler::argmax_penalized(
4081                    &logits[i * lm_rows..(i + 1) * lm_rows],
4082                    &cfg,
4083                    all_ids,
4084                    &mut self.sampler_scratch,
4085                    self.pool.as_deref(),
4086                );
4087                ids.push(t);
4088                if i < k_spec && t == drafts[i] {
4089                    all_ids.push(t);
4090                } else {
4091                    break;
4092                }
4093            }
4094            all_ids.truncate(base_len);
4095            while a < k_spec && a < ids.len() && ids[a] == drafts[a] {
4096                a += 1;
4097            }
4098            // rows past the first mismatch were never scored; the loop
4099            // top re-samples the last verified row itself.
4100            ids
4101        } else {
4102            let ids: Vec<u32> = (0..b)
4103                .map(|i| sampler::argmax(&logits[i * lm_rows..(i + 1) * lm_rows]))
4104                .collect();
4105            while a < k_spec && ids[a] == drafts[a] {
4106                a += 1;
4107            }
4108            ids
4109        };
4110        if spec_dbg {
4111            eprintln!(
4112                "spec-dbg round: t_next {t_next} drafts {:?} verified {:?} accepted {a}",
4113                drafts, ids
4114            );
4115        }
4116        // CMF_METAL_VERIFY_CHECK=2: the commit oracle — plain-forward the
4117        // a+1 accepted tokens from a snapshot, then diff the replayed GDN
4118        // states and the appended K/V rows against that.
4119        #[cfg(target_os = "macos")]
4120        let commit_ref: Option<(Vec<Vec<f32>>, Vec<(usize, Vec<f32>, Vec<f32>)>)> = if metal_native
4121            && std::env::var("CMF_METAL_VERIFY_CHECK").as_deref() == Ok("2")
4122        {
4123            let snap: Vec<Vec<f32>> = self
4124                .kv_cache
4125                .layers
4126                .iter()
4127                .map(|l| l.linear_state.clone())
4128                .collect();
4129            let attn_lens: Vec<usize> = self.kv_cache.layers.iter().map(|l| l.seq_len).collect();
4130            let toks: Vec<u32> = std::iter::once(t_next)
4131                .chain(drafts.iter().copied())
4132                .collect();
4133            let want_save = self.graph_want_logits;
4134            self.graph_want_logits = false;
4135            for (i, &t) in toks.iter().take(a + 1).enumerate() {
4136                let _ = self.forward_layers(&self.embed_single(t), next_pos + i, None);
4137                let _ = self.graph_logits.take();
4138            }
4139            self.graph_want_logits = want_save;
4140            let plain_states: Vec<Vec<f32>> = self
4141                .kv_cache
4142                .layers
4143                .iter()
4144                .map(|l| l.linear_state.clone())
4145                .collect();
4146            let (nkv, hd) = (self.num_kv_heads, self.head_dim);
4147            let mut rows = Vec::new();
4148            for (li, (l, n0)) in self
4149                .kv_cache
4150                .layers
4151                .iter_mut()
4152                .zip(attn_lens.iter())
4153                .enumerate()
4154            {
4155                let extra = l.seq_len.saturating_sub(*n0);
4156                if extra > 0 {
4157                    let mut kk = Vec::new();
4158                    let mut vv = Vec::new();
4159                    for g in 0..nkv {
4160                        kk.extend_from_slice(&l.head_keys(g)[n0 * hd..]);
4161                        vv.extend_from_slice(&l.head_values(g)[n0 * hd..]);
4162                    }
4163                    rows.push((li, kk, vv));
4164                    l.truncate_last(extra);
4165                    crate::gpu_metal::kv_mirror_set_stored(self.graph_kv_id, li, *n0);
4166                }
4167            }
4168            for (l, st) in self.kv_cache.layers.iter_mut().zip(snap) {
4169                if l.linear_state.len() == st.len() {
4170                    l.linear_state.copy_from_slice(&st);
4171                } else {
4172                    l.linear_state = st;
4173                }
4174            }
4175            Some((plain_states, rows))
4176        } else {
4177            None
4178        };
4179        // a fully-accepted round needs no restore: every input was real.
4180        #[cfg(target_os = "macos")]
4181        if metal_native {
4182            // the Metal verify never wrote its states: the commit replays the
4183            // accepted prefix into the CPU owners and appends the K/V rows
4184            self.metal_verify_commit(a);
4185            if let Some((plain_states, rows)) = commit_ref {
4186                crate::gpu_metal::queue_fence();
4187                let (nkv, hd) = (self.num_kv_heads, self.head_dim);
4188                let mut worst_s = 0f32;
4189                let mut worst_li = 0usize;
4190                for (li, (l, ps)) in self.kv_cache.layers.iter().zip(&plain_states).enumerate() {
4191                    if l.linear_state.len() != ps.len() || ps.is_empty() {
4192                        continue;
4193                    }
4194                    let d = l
4195                        .linear_state
4196                        .iter()
4197                        .zip(ps)
4198                        .fold(0f32, |m, (x, y)| m.max((x - y).abs()));
4199                    let n = ps.iter().fold(0f32, |m, y| m.max(y.abs()));
4200                    let rel = d / n.max(1e-6);
4201                    if rel > worst_s {
4202                        worst_s = rel;
4203                        worst_li = li;
4204                    }
4205                }
4206                let mut worst_k = 0f32;
4207                for (li, kk, vv) in &rows {
4208                    let l = &self.kv_cache.layers[*li];
4209                    let n0 = l.seq_len - (kk.len() / (nkv * hd));
4210                    let mut ck = Vec::new();
4211                    let mut cv = Vec::new();
4212                    for g in 0..nkv {
4213                        ck.extend_from_slice(&l.head_keys(g)[n0 * hd..]);
4214                        cv.extend_from_slice(&l.head_values(g)[n0 * hd..]);
4215                    }
4216                    if ck.len() == kk.len() {
4217                        let dk = ck
4218                            .iter()
4219                            .zip(kk)
4220                            .fold(0f32, |m, (x, y)| m.max((x - y).abs()));
4221                        let dv = cv
4222                            .iter()
4223                            .zip(vv)
4224                            .fold(0f32, |m, (x, y)| m.max((x - y).abs()));
4225                        worst_k = worst_k.max(dk).max(dv);
4226                    } else {
4227                        eprintln!(
4228                            "commit-check L{li}: kv row count mismatch {} vs {}",
4229                            ck.len(),
4230                            kk.len()
4231                        );
4232                    }
4233                }
4234                eprintln!(
4235                    "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}"
4236                );
4237            }
4238        } else if a + 1 < b {
4239            crate::gpu::gdn_spec_restore(self.graph_kv_id, a);
4240        }
4241        #[cfg(not(target_os = "macos"))]
4242        if a + 1 < b {
4243            crate::gpu::gdn_spec_restore(self.graph_kv_id, a);
4244        }
4245        *accepted += a;
4246        // MTP cache: keep the first draft row (its inputs were real), drop
4247        // the chain's, then append the verified pairs the round produced.
4248        // Each of those is a whole MTP block on the per-op path and they
4249        // cost 5.8 ms of a 69 ms round at k=3 — a third of what the
4250        // round's own draft costs. PRICED, and they earn it: skipping
4251        // them (`CMF_SPEC_WARM=0`) drops acceptance from 89% to 81% at
4252        // k=3 and 85% to 74% at k=4, and the tok/s goes nowhere at k=3
4253        // (50.3 against 50.5) and backwards at k=4 (48.1 against 50.1).
4254        // The knob stays so the next person can re-price it after the
4255        // warms are batched instead of assuming either way.
4256        m.kv.truncate_last(k_spec.saturating_sub(1));
4257        #[cfg(target_os = "macos")]
4258        if metal_native && self.mtp_graph_mode == Some(true) {
4259            // the mirror rows below the cut are the CPU rows: re-point,
4260            // no re-upload
4261            crate::gpu_metal::kv_mirror_set_stored(
4262                self.mtp_kv_id(),
4263                Self::MTP_LAYER_BASE,
4264                m.kv.seq_len,
4265            );
4266        }
4267        let warm_off = std::env::var("CMF_SPEC_WARM").is_ok_and(|v| v == "0");
4268        if !warm_off && a > 0 {
4269            // Graph arm: all accepted pairs in ONE batched run over the
4270            // MTP block; the token graph one by one if the batch declines.
4271            let mut warmed = false;
4272            #[cfg(target_os = "macos")]
4273            if metal_native && self.mtp_graph_mode == Some(true) {
4274                // all accepted pairs in ONE b-row graph run over the MTP
4275                // block (its input projection folded in); one by one on
4276                // the token graph if that declines
4277                let pairs: Vec<(&[f32], u32)> = (0..a)
4278                    .map(|j| {
4279                        (
4280                            &hiddens[j * self.hidden_size..(j + 1) * self.hidden_size],
4281                            ids[j],
4282                        )
4283                    })
4284                    .collect();
4285                warmed = self.mtp_warm_batch_metal(m, &pairs, next_pos);
4286                if !warmed {
4287                    warmed = true;
4288                    for j in 0..a {
4289                        let row =
4290                            hiddens[j * self.hidden_size..(j + 1) * self.hidden_size].to_vec();
4291                        if self
4292                            .mtp_step_metal(m, &row, ids[j], next_pos + j, false)
4293                            .is_none()
4294                        {
4295                            warmed = false;
4296                            break;
4297                        }
4298                    }
4299                }
4300            }
4301            if !warmed && self.mtp_graph_mode == Some(true) && !metal_native {
4302                let rows: Vec<Vec<f32>> = (0..a)
4303                    .map(|j| hiddens[j * self.hidden_size..(j + 1) * self.hidden_size].to_vec())
4304                    .collect();
4305                let pairs: Vec<(&[f32], u32)> = rows
4306                    .iter()
4307                    .zip(ids.iter())
4308                    .map(|(r, &t)| (r.as_slice(), t))
4309                    .collect();
4310                warmed = self.mtp_warm_graph(m, &pairs, next_pos);
4311                if !warmed {
4312                    // Prefix-mode token graph per pair (kv_append inside).
4313                    warmed = true;
4314                    for j in 0..a {
4315                        if self
4316                            .mtp_step_graph(m, &rows[j], ids[j], next_pos + j)
4317                            .is_none()
4318                        {
4319                            warmed = false;
4320                            break;
4321                        }
4322                    }
4323                }
4324            }
4325            if !warmed {
4326                for j in 0..a {
4327                    let row = &hiddens[j * self.hidden_size..(j + 1) * self.hidden_size];
4328                    let row = row.to_vec();
4329                    self.mtp_warm(m, &row, ids[j], next_pos + j);
4330                }
4331            }
4332        }
4333        // The sampler's contract: logits of the LAST verified position —
4334        // unless a rejected draft already drew the correction, in which
4335        // case the loop top commits that token and samples nothing.
4336        if let Some(c) = forced {
4337            self.spec_forced = Some(c);
4338            self.graph_logits = None;
4339        } else {
4340            let mut row = logits[a * lm_rows..(a + 1) * lm_rows].to_vec();
4341            row.resize(self.vocab_size, 0.0);
4342            if let Some(c) = self.final_softcap {
4343                for l in row.iter_mut() {
4344                    *l = c * (*l / c).tanh();
4345                }
4346            }
4347            self.graph_logits = Some(row);
4348        }
4349        let new_hidden = hiddens[a * self.hidden_size..(a + 1) * self.hidden_size].to_vec();
4350        // Three phases, not two. The round's wall clock was 4 ms longer
4351        // than draft+verify and the difference had nowhere to be seen:
4352        // the accepted prefix re-runs the MTP block once per token to
4353        // keep the draft head's attention cache warm, and the GDN state
4354        // rolls back on any rejection. Both live here, after the verify.
4355        if std::env::var("CMF_GRAPH_SPEC_TIME").is_ok() {
4356            let end = subs();
4357            eprintln!(
4358                "spec-round: draft {:.1} ms/{} sub | verify {:.1} ms/{} sub | \
4359                 commit {:.1} ms/{} sub (accepted {a} of {k_spec})",
4360                t_draft.as_secs_f64() * 1e3,
4361                sub_draft - sub0,
4362                (t_verify - t_draft).as_secs_f64() * 1e3,
4363                sub_verify - sub_draft,
4364                (t_round.elapsed() - t_verify).as_secs_f64() * 1e3,
4365                end - sub_verify,
4366            );
4367        }
4368        Some((drafts[..a].to_vec(), next_pos + a + 1, new_hidden))
4369    }
4370
4371    /// Micro-benchmark: two single-position forwards vs one fused pair
4372    /// from the current cache state (KV rewound after each probe).
4373    /// Returns (two_singles_ms, fused_pair_ms) per probe, or the (0, 0)
4374    /// sentinel when this model has no pair path to measure — the same
4375    /// answer the o1 arm gives, and the bench prints it the same way.
4376    /// (An architecture that loads its own layers leaves `weights.layers`
4377    /// empty; walking it here was an index panic, found by `bench` on
4378    /// deepseek_v4.)
4379    pub fn measure_pair_fusion(&mut self, iters: usize) -> (f64, f64) {
4380        if !self.pair_supported() {
4381            return (0.0, 0.0);
4382        }
4383        let emb1 = self.embed_single(1);
4384        let emb2 = self.embed_single(2);
4385        let pos = self.kv_cache.seq_len();
4386
4387        let t0 = std::time::Instant::now();
4388        for _ in 0..iters {
4389            let _ = self.forward_layers(&emb1, pos, None);
4390            let _ = self.forward_layers(&emb2, pos + 1, None);
4391            for l in &mut self.kv_cache.layers {
4392                l.truncate_last(2);
4393            }
4394        }
4395        let singles_ms = t0.elapsed().as_secs_f64() * 1000.0 / iters as f64;
4396
4397        let t1 = std::time::Instant::now();
4398        for _ in 0..iters {
4399            let _ = self.forward_pair(&emb1, &emb2, pos);
4400            for l in &mut self.kv_cache.layers {
4401                l.truncate_last(2);
4402            }
4403        }
4404        let pair_ms = t1.elapsed().as_secs_f64() * 1000.0 / iters as f64;
4405        (singles_ms, pair_ms)
4406    }
4407
4408    /// Fused two-position forward: weight rows are streamed from memory
4409    /// once per layer for both positions. Full layers → fused GQA pair;
4410    /// linear layers → vmf_phase pair (lane 2 state is tentative in the
4411    /// per-layer scratch until the draft is accepted).
4412    /// Whether the fused two-position path covers every layer kind in
4413    /// this model. MLA and KDA run per position (their pair arms are
4414    /// unreachable); the seq prefill falls back to singles for them.
4415    fn pair_supported(&self) -> bool {
4416        // An EMPTY layer stack means the architecture loaded its own and
4417        // this path has nothing to walk. Checking that directly, rather
4418        // than naming each such architecture, is what makes the guard hold
4419        // for the next one: `any()` over no layers is false, so a
4420        // feature-by-feature test says "supported" for a model that has no
4421        // layers here at all.
4422        !self.weights.layers.is_empty()
4423            && self.g3n.is_none()
4424            && !self
4425                .weights
4426                .layers
4427                .iter()
4428                .any(|lw| matches!(&lw.attn, AttnKind::Mla(_) | AttnKind::Kda(_)))
4429    }
4430
4431    fn forward_pair(
4432        &mut self,
4433        emb1: &[f32],
4434        emb2: &[f32],
4435        position: usize,
4436    ) -> (Vec<f32>, Vec<f32>) {
4437        let mut h1 = emb1.to_vec();
4438        let mut h2 = emb2.to_vec();
4439        let (_nkv, _hd, hs, _rd, eps) = (
4440            self.num_kv_heads,
4441            self.head_dim,
4442            self.hidden_size,
4443            self.rotary_dim,
4444            self.rms_eps,
4445        );
4446        let pool = self.pool.clone();
4447
4448        for li in 0..self.num_layers {
4449            let lw = &self.weights.layers[self.phys_layer(li)];
4450            // Norms into pipeline scratch (4 allocs/layer on the MTP
4451            // decode hot path before this).
4452            inference::rms_norm_into(
4453                &h1,
4454                &lw.input_norm,
4455                self.rms_eps,
4456                self.norm_style,
4457                &mut self.ws.n1,
4458            );
4459            inference::rms_norm_into(
4460                &h2,
4461                &lw.input_norm,
4462                self.rms_eps,
4463                self.norm_style,
4464                &mut self.ws.n2,
4465            );
4466
4467            let (a1, a2) = match &lw.attn {
4468                AttnKind::Mla(_) => unreachable!("MLA has no MTP/pair path"),
4469                AttnKind::Kda(_) => unreachable!("KDA has no MTP/pair path"),
4470                AttnKind::Linear(w) => {
4471                    let cfg = self.vmf_cfg.expect("linear layer without vmf_cfg");
4472                    let layer = &mut self.kv_cache.layers[li];
4473                    let (state, scratch) = (&mut layer.linear_state, &mut layer.linear_scratch);
4474                    vmf_phase_pair(
4475                        &self.ws.n1,
4476                        &self.ws.n2,
4477                        w,
4478                        &cfg,
4479                        state,
4480                        scratch,
4481                        self.pool.as_deref(),
4482                    )
4483                }
4484                AttnKind::LinearGdn(w) => {
4485                    let cfg = self.gdn_cfg.expect("gdn layer without gdn_cfg");
4486                    let layer = &mut self.kv_cache.layers[li];
4487                    let (state, scratch) = (&mut layer.linear_state, &mut layer.linear_scratch);
4488                    gdn_pair(
4489                        &self.ws.n1,
4490                        &self.ws.n2,
4491                        w,
4492                        &cfg,
4493                        state,
4494                        scratch,
4495                        self.pool.as_deref(),
4496                    )
4497                }
4498                AttnKind::ShortConv(w) => {
4499                    let cfg = self
4500                        .short_conv_cfg
4501                        .expect("short-conv layer without short_conv_cfg");
4502                    let layer = &mut self.kv_cache.layers[li];
4503                    let (state, scratch) = (&mut layer.linear_state, &mut layer.linear_scratch);
4504                    short_conv_pair(
4505                        &self.ws.n1,
4506                        &self.ws.n2,
4507                        w,
4508                        &cfg,
4509                        state,
4510                        scratch,
4511                        self.pool.as_deref(),
4512                    )
4513                }
4514                AttnKind::Full {
4515                    wq,
4516                    wk,
4517                    wv,
4518                    wo,
4519                    q_norm,
4520                    k_norm,
4521                    output_gate,
4522                    softplus_gate,
4523                    bias,
4524                } => {
4525                    let inv_freq_l = self.layer_inv_freq(li);
4526                    let (nkv_l, hd_l, rd_l) = self.layer_geom(li);
4527                    let cfg = QwenAttnCfg {
4528                        num_heads: self.layer_num_heads(li),
4529                        num_kv_heads: nkv_l,
4530                        head_dim: hd_l,
4531                        hidden_size: hs,
4532                        position,
4533                        inv_freq: &inv_freq_l,
4534                        rotary_dim: rd_l,
4535                        scale: self.attn_scale,
4536                        softcap: self.attn_softcap,
4537                        window: self.layer_window(li),
4538                        v_norm: self.attn_v_norm,
4539                        q_norm: q_norm.as_deref(),
4540                        k_norm: k_norm.as_deref(),
4541                        output_gate: *output_gate,
4542                        softplus_gate: softplus_gate
4543                            .as_ref()
4544                            .map(|(gate, per_head)| (gate, *per_head)),
4545                        rope_scale: self.layer_rope_scale(li),
4546                        bias: bias
4547                            .as_ref()
4548                            .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
4549                        rms_eps: eps,
4550                        norm_style: self.norm_style,
4551                        pool: pool.as_deref(),
4552                    };
4553                    attention::qwen_attention_pair(
4554                        &self.ws.n1,
4555                        &self.ws.n2,
4556                        wq,
4557                        wk,
4558                        wv,
4559                        wo,
4560                        &mut self.kv_cache.layers[li],
4561                        &cfg,
4562                    )
4563                }
4564            };
4565            let (a1, a2) = match &self.weights.layers[self.phys_layer(li)].attn_out_norm {
4566                Some(w) => (
4567                    inference::rms_norm(&a1, w, self.rms_eps, self.norm_style),
4568                    inference::rms_norm(&a2, w, self.rms_eps, self.norm_style),
4569                ),
4570                None => (a1, a2),
4571            };
4572            for i in 0..self.hidden_size {
4573                h1[i] += a1[i];
4574                h2[i] += a2[i];
4575            }
4576            let (mut a1, mut a2) = (a1, a2);
4577            attention::recycle_buf(&mut a1);
4578            attention::recycle_buf(&mut a2);
4579
4580            let lw = &self.weights.layers[self.phys_layer(li)];
4581            inference::rms_norm_into(
4582                &h1,
4583                &lw.post_norm,
4584                self.rms_eps,
4585                self.norm_style,
4586                &mut self.ws.p1,
4587            );
4588            inference::rms_norm_into(
4589                &h2,
4590                &lw.post_norm,
4591                self.rms_eps,
4592                self.norm_style,
4593                &mut self.ws.p2,
4594            );
4595            let (f1, f2) = match &lw.ffn {
4596                // Dual-branch layers need the raw residuals — run the
4597                // two positions through the same fn decode uses.
4598                FfnKind::DenseMoe(dm) => (
4599                    dense_moe_ffn(
4600                        dm,
4601                        &self.ws.p1,
4602                        &h1,
4603                        self.rms_eps,
4604                        self.norm_style,
4605                        self.pool.as_deref(),
4606                    ),
4607                    dense_moe_ffn(
4608                        dm,
4609                        &self.ws.p2,
4610                        &h2,
4611                        self.rms_eps,
4612                        self.norm_style,
4613                        self.pool.as_deref(),
4614                    ),
4615                ),
4616                _ => ffn_forward_pair(
4617                    &lw.ffn,
4618                    &self.ws.p1,
4619                    &self.ws.p2,
4620                    self.pool.as_deref(),
4621                    None,
4622                ),
4623            };
4624            let (f1, f2) = match &self.weights.layers[self.phys_layer(li)].ffn_out_norm {
4625                Some(w) => (
4626                    inference::rms_norm(&f1, w, self.rms_eps, self.norm_style),
4627                    inference::rms_norm(&f2, w, self.rms_eps, self.norm_style),
4628                ),
4629                None => (f1, f2),
4630            };
4631            for i in 0..self.hidden_size {
4632                h1[i] += f1[i];
4633                h2[i] += f2[i];
4634            }
4635            let (mut f1, mut f2) = (f1, f2);
4636            attention::recycle_buf(&mut f1);
4637            attention::recycle_buf(&mut f2);
4638            if let Some(sc) = self.weights.layers[self.phys_layer(li)].layer_scale {
4639                for i in 0..self.hidden_size {
4640                    h1[i] *= sc;
4641                    h2[i] *= sc;
4642                }
4643            }
4644            // Looped Transformer: apply final norm at the end of each loop iteration.
4645            if self.is_loop_end(li) && li + 1 < self.num_layers {
4646                h1 = inference::rms_norm(
4647                    &h1,
4648                    &self.weights.final_norm,
4649                    self.rms_eps,
4650                    self.norm_style,
4651                );
4652                h2 = inference::rms_norm(
4653                    &h2,
4654                    &self.weights.final_norm,
4655                    self.rms_eps,
4656                    self.norm_style,
4657                );
4658            }
4659        }
4660        (h1, h2)
4661    }
4662
4663    /// Commit lane-2 linear states after an accepted draft.
4664    fn commit_linear_scratch(&mut self) {
4665        for layer in &mut self.kv_cache.layers {
4666            if !layer.linear_scratch.is_empty() {
4667                std::mem::swap(&mut layer.linear_state, &mut layer.linear_scratch);
4668                layer.linear_scratch.clear();
4669            }
4670        }
4671    }
4672
4673    /// Forward a full id sequence from a fresh cache and return the
4674    /// logits after the last position (golden-parity harness, bench).
4675    pub fn forward_ids(
4676        &mut self,
4677        ids: &[u32],
4678        task_mask: Option<&TaskMask>,
4679    ) -> Result<Vec<f32>, String> {
4680        if ids.is_empty() {
4681            return Err("empty id sequence".to_string());
4682        }
4683        self.kv_cache.clear();
4684        self.kv_history.clear();
4685        self.o1_begin();
4686        let mut hidden = vec![0.0f32; self.hidden_size];
4687        let mut pos = 0usize;
4688        // Same routing predicate generation uses. Two reasons it must be
4689        // the same one: (1) a GDN hybrid's recurrent state is GPU-
4690        // resident, and a batched CPU prefill would build it on the host
4691        // only — decode then reads buffers the prefill never wrote;
4692        // (2) bench times THIS function and calls the result "prefill",
4693        // so a different path here reports a number production never
4694        // sees (W2 on 2×5090: 8.7 tok/s reported against 125 real).
4695        if self.can_prefill_batched() && !self.graph_prefill_preferred() && ids.len() > 2 {
4696            // prefill-GEMM in chunks; only the last position's hidden is
4697            // needed. (o1-compatible: the batch path attends per position
4698            // through qwen_attention, which carries the collection hook.)
4699            let chunk = prefill_chunk();
4700            let hs = self.hidden_size;
4701            while pos < ids.len() {
4702                let end = (pos + chunk).min(ids.len());
4703                let hb = self.prefill_batch_masked(&ids[pos..end], pos, task_mask);
4704                hidden.copy_from_slice(&hb[(end - pos - 1) * hs..]);
4705                pos = end;
4706            }
4707        }
4708        // Same guards as generation's prefill — INCLUDING the graph one.
4709        // The CPU pair walk was intercepting positions that the resident
4710        // token graph would have run itself: on a GDN hybrid over wgpu
4711        // that is 89 ms of host forward against 7 ms of device submit,
4712        // and it made prefill look 12× slower than it is (W2 on an RTX
4713        // 5090, ctx 512: 11.2 tok/s with the walk, 136.6 without).
4714        // CMF_PAIR=0 opts out; a model whose layers live outside
4715        // `weights.layers` has no pair walk to take.
4716        if task_mask.is_none()
4717            && !self.graph_prefill_preferred()
4718            && !std::env::var("CMF_PAIR").is_ok_and(|v| v == "0")
4719            && self.pair_supported()
4720        {
4721            while pos + 1 < ids.len() {
4722                let e1 = self.embed_single(ids[pos]);
4723                let e2 = self.embed_single(ids[pos + 1]);
4724                let (_, h2) = self.forward_pair(&e1, &e2, pos);
4725                self.commit_linear_scratch();
4726                hidden = h2;
4727                pos += 2;
4728            }
4729        }
4730        while pos < ids.len() {
4731            hidden = self.forward_layers(&self.embed_single(ids[pos]), pos, task_mask);
4732            pos += 1;
4733        }
4734        // Harness contract: after forward_ids the cache is decode-ready —
4735        // under o1 that means sealed (bench measures the seal as part of
4736        // prefill, honestly).
4737        self.o1_seal();
4738        let normed = inference::rms_norm(
4739            &hidden,
4740            &self.weights.final_norm,
4741            self.rms_eps,
4742            self.norm_style,
4743        );
4744        Ok(self.lm_head_forward(&normed))
4745    }
4746
4747    /// Teacher-forced perplexity over a token sequence (phase-C gate:
4748    /// honest quant comparisons instead of prompt vibes).
4749    ///
4750    /// Attention is EXACT even on a model whose layers are flagged for
4751    /// the O(1) kernel — scoring the backbone is the default on purpose
4752    /// (it is the yardstick). `nll_ids_o1` scores the CONVERTED model.
4753    pub fn ppl_ids(&mut self, ids: &[u32]) -> f64 {
4754        let (nll, cnt) = self.nll_ids_from(ids, 0);
4755        (nll / cnt.max(1) as f64).exp()
4756    }
4757
4758    /// DTG-MA calibration pass (Patent 2): run `ids` through the model
4759    /// (CPU path, per position) and return each layer's per-neuron
4760    /// activation mass Σ|silu(gate)·up| — the statistic the task-guided
4761    /// FFN mask is derived from.
4762    pub fn probe_ffn_mass(&mut self, ids: &[u32]) -> Vec<Vec<f64>> {
4763        self.kv_cache.clear();
4764        self.kv_history.clear();
4765        FFN_PROBE.with(|p| {
4766            *p.borrow_mut() = Some(vec![vec![0f64; self.intermediate_size]; self.num_layers]);
4767        });
4768        crate::gpu::cpu_scope(|| {
4769            for (pos, &id) in ids.iter().enumerate() {
4770                let emb = self.embed_single(id);
4771                let _ = self.forward_layers(&emb, pos, None);
4772            }
4773        });
4774        self.kv_cache.clear();
4775        self.kv_history.clear();
4776        FFN_PROBE
4777            .with(|p| p.borrow_mut().take())
4778            .unwrap_or_default()
4779    }
4780
4781    /// `probe_ffn_mass` over the BATCHED prefill: same accumulator, one
4782    /// sweep instead of one forward per token. What makes the statistic
4783    /// affordable on a 27B.
4784    pub fn probe_ffn_mass_batch(&mut self, ids: &[u32]) -> Vec<Vec<f64>> {
4785        self.kv_cache.clear();
4786        self.kv_history.clear();
4787        FFN_PROBE.with(|p| {
4788            *p.borrow_mut() = Some(vec![vec![0f64; self.intermediate_size]; self.num_layers]);
4789        });
4790        for chunk in ids.chunks(256) {
4791            if chunk.len() < 2 {
4792                continue;
4793            }
4794            let _ = self.nll_ids_masked(chunk, 0, None);
4795        }
4796        self.kv_cache.clear();
4797        self.kv_history.clear();
4798        FFN_PROBE
4799            .with(|p| p.borrow_mut().take())
4800            .unwrap_or_default()
4801    }
4802
4803    /// Teacher-forced PPL with a task mask active (sparse execution) —
4804    /// the quality gate for a DTG-MA-masked skill. Sequential per
4805    /// position: the batched prefill path is dense-only.
4806    pub fn ppl_ids_masked(&mut self, ids: &[u32], mask: &TaskMask) -> f64 {
4807        self.kv_cache.clear();
4808        self.kv_history.clear();
4809        let mut nll = 0f64;
4810        let mut cnt = 0usize;
4811        let mut hidden = vec![0f32; self.hidden_size];
4812        for (pos, &id) in ids.iter().enumerate() {
4813            if pos > 0 {
4814                inference::rms_norm_into(
4815                    &hidden,
4816                    &self.weights.final_norm,
4817                    self.rms_eps,
4818                    self.norm_style,
4819                    &mut self.ws.n1,
4820                );
4821                let mut logits = self.lm_head_forward(&self.ws.n1);
4822                let max = logits.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
4823                let sum: f64 = logits.iter().map(|&v| ((v - max) as f64).exp()).sum();
4824                let p = ((logits[id as usize] - max) as f64).exp() / sum.max(1e-300);
4825                nll -= p.max(1e-300).ln();
4826                cnt += 1;
4827                attention::recycle_buf(&mut logits);
4828            }
4829            let emb = self.embed_single(id);
4830            hidden = self.forward_layers(&emb, pos, Some(mask));
4831        }
4832        self.kv_cache.clear();
4833        self.kv_history.clear();
4834        (nll / cnt.max(1) as f64).exp()
4835    }
4836
4837    /// Teacher-forced NLL sum + scored-token count over positions
4838    /// `start..len-1`, attention EXACT. Positions below `start` still
4839    /// run — they are the context — they are just not scored, so this
4840    /// pairs with `nll_ids_o1(ids, start)` over the very same tokens.
4841    ///
4842    /// Returning (nll, cnt) rather than a ppl is what lets a windowed
4843    /// caller combine windows before the exp, so every scored token
4844    /// weighs the same regardless of how the windows are cut.
4845    /// `nll_ids_from` with a task mask held active at every position.
4846    ///
4847    /// The batched prefill path does not thread masks, so this walks the
4848    /// per-position forward — slower, but it scores the file exactly the
4849    /// way `run --task` will serve it, which is the point of the gate
4850    /// that calls it. With `None` it defers to the fast path.
4851    /// Masked scoring rides the SAME batched sweep as unmasked scoring —
4852    /// the masked-inference fast path: `prefill_batch_masked` lands the
4853    /// per-visit FFN rows on the activations inside the fused arms. The
4854    /// per-position loop below remains only as the no-batch fallback.
4855    pub fn nll_ids_masked(
4856        &mut self,
4857        ids: &[u32],
4858        start: usize,
4859        task_mask: Option<&TaskMask>,
4860    ) -> (f64, usize) {
4861        let task_mask = self.drop_open_mask(task_mask);
4862        self.nll_ids_inner(ids, start, task_mask)
4863    }
4864
4865    pub fn nll_ids_from(&mut self, ids: &[u32], start: usize) -> (f64, usize) {
4866        self.nll_ids_inner(ids, start, None)
4867    }
4868
4869    fn nll_ids_inner(
4870        &mut self,
4871        ids: &[u32],
4872        start: usize,
4873        task_mask: Option<&TaskMask>,
4874    ) -> (f64, usize) {
4875        self.kv_cache.clear();
4876        self.kv_history.clear();
4877        let mut nll = 0f64;
4878        let mut cnt = 0usize;
4879        if self.can_prefill_batched() {
4880            // prefill-GEMM: layer-major position chunks, lm_head batched
4881            // (254MB lm_head read once per chunk, not per position).
4882            // The layer chunk is large (grouping positions by MoE experts
4883            // wins with size), lm_head in sub-blocks (logit buffer
4884            // 32×vocab ≈ 32MB instead of 128×).
4885            const CHUNK: usize = 128;
4886            const LM_SUB: usize = 32;
4887            let n = ids.len().saturating_sub(1);
4888            let hs = self.hidden_size;
4889            let rows = self.weights.lm_head.rows();
4890            let mut pos = 0usize;
4891            while pos < n {
4892                let end = (pos + CHUNK).min(n);
4893                let bsz = end - pos;
4894                let hb = self.prefill_batch_masked(&ids[pos..end], pos, task_mask);
4895                let mut k0 = 0usize;
4896                while k0 < bsz {
4897                    let k1 = (k0 + LM_SUB).min(bsz);
4898                    let sb = k1 - k0;
4899                    // Sub-block entirely below the scored range: the KV
4900                    // it just built is all this pass needed from it.
4901                    if pos + k1 <= start {
4902                        k0 = k1;
4903                        continue;
4904                    }
4905                    let mut normed = vec![0.0f32; sb * hs];
4906                    for k in 0..sb {
4907                        let r = inference::rms_norm(
4908                            &hb[(k0 + k) * hs..(k0 + k + 1) * hs],
4909                            &self.weights.final_norm,
4910                            self.rms_eps,
4911                            self.norm_style,
4912                        );
4913                        normed[k * hs..(k + 1) * hs].copy_from_slice(&r);
4914                    }
4915                    let mut logits = vec![0.0f32; sb * rows];
4916                    self.weights
4917                        .lm_head
4918                        .matmat(&normed, sb, &mut logits, self.pool.as_deref());
4919                    for k in 0..sb {
4920                        if pos + k0 + k < start {
4921                            continue;
4922                        }
4923                        let lg = &mut logits[k * rows..k * rows + self.vocab_size.min(rows)];
4924                        if let Some(mu) = self.logit_multiplier {
4925                            for v in lg.iter_mut() {
4926                                *v *= mu;
4927                            }
4928                        }
4929                        // Gemma-class final-logit soft-capping: the
4930                        // decode paths apply it; scoring must too, or
4931                        // the uncapped softmax misprices every token.
4932                        if let Some(c) = self.final_softcap {
4933                            for v in lg.iter_mut() {
4934                                *v = c * (*v / c).tanh();
4935                            }
4936                        }
4937                        // Cortiq Embryo hierarchical head: same correction
4938                        // the decode path applies (lm_head_forward).
4939                        if let Some(cm) = self.head_clusters.clone() {
4940                            self.hierarchical_head_logprobs(&normed[k * hs..(k + 1) * hs], &cm, lg);
4941                        }
4942                        let lg = &logits[k * rows..k * rows + self.vocab_size.min(rows)];
4943                        let target = ids[pos + k0 + k + 1] as usize;
4944                        let max = lg.iter().fold(f32::NEG_INFINITY, |m, &v| m.max(v));
4945                        let lse: f64 = lg
4946                            .iter()
4947                            .map(|&v| ((v - max) as f64).exp())
4948                            .sum::<f64>()
4949                            .ln()
4950                            + max as f64;
4951                        nll += lse - lg[target] as f64;
4952                        cnt += 1;
4953                        if std::env::var("CMF_PPL_TRACE").is_ok() {
4954                            let top = lg
4955                                .iter()
4956                                .enumerate()
4957                                .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
4958                                .map(|(i, _)| i)
4959                                .unwrap_or(0);
4960                            eprintln!(
4961                                "BTRACE pos {} target {} nll {:.4} top {} lg_t {:.3} lg_top {:.3}",
4962                                pos + k0 + k,
4963                                target,
4964                                lse - lg[target] as f64,
4965                                top,
4966                                lg[target],
4967                                lg[top]
4968                            );
4969                        }
4970                    }
4971                    k0 = k1;
4972                }
4973                pos = end;
4974            }
4975            self.kv_cache.clear();
4976            self.kv_history.clear();
4977            return (nll, cnt);
4978        }
4979        for pos in 0..ids.len().saturating_sub(1) {
4980            let hidden = self.forward_layers(&self.embed_single(ids[pos]), pos, task_mask);
4981            // Architectures whose head lives inside their own stack return
4982            // the logits out of band and a zero hidden — DeepSeek-V4 folds
4983            // its hyper-connection copies between the last layer and the
4984            // norm, so it cannot hand back a vector this loop could use.
4985            // Scoring the zeros gave a perplexity of exactly the vocabulary
4986            // size, which is a uniform distribution reported as a
4987            // measurement. `generate` already reads this channel.
4988            let out_of_band = self.graph_logits.take();
4989            if pos < start {
4990                continue;
4991            }
4992            let logits = match out_of_band {
4993                Some(lg) => lg,
4994                None => {
4995                    let normed = inference::rms_norm(
4996                        &hidden,
4997                        &self.weights.final_norm,
4998                        self.rms_eps,
4999                        self.norm_style,
5000                    );
5001                    // lm_head_forward applies the final-logit softcap itself
5002                    // — capping again here double-squashed gemma-class
5003                    // logits (tanh∘tanh) and reported a flattered ppl.
5004                    self.lm_head_forward(&normed)
5005                }
5006            };
5007            let target = ids[pos + 1] as usize;
5008            let max = logits.iter().fold(f32::NEG_INFINITY, |m, &v| m.max(v));
5009            let lse: f64 = logits
5010                .iter()
5011                .map(|&v| ((v - max) as f64).exp())
5012                .sum::<f64>()
5013                .ln()
5014                + max as f64;
5015            let tok_nll = lse - logits[target] as f64;
5016            if std::env::var("CMF_PPL_TRACE").is_ok() && pos < 48 {
5017                let top = logits
5018                    .iter()
5019                    .enumerate()
5020                    .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
5021                    .map(|(i, _)| i)
5022                    .unwrap_or(0);
5023                eprintln!(
5024                    "pos {pos:3} tgt {target:6} nll {tok_nll:7.3} | top1 {top:6} lg[t]={:.2} lg[top]={:.2}",
5025                    logits[target], logits[top]
5026                );
5027            }
5028            nll += tok_nll;
5029            cnt += 1;
5030        }
5031        self.kv_cache.clear();
5032        self.kv_history.clear();
5033        (nll, cnt)
5034    }
5035
5036    /// Teacher-forced NLL of the CONVERTED model: the O(1) Nyström path
5037    /// is ACTIVE over the scored positions. Returns (nll sum, scored
5038    /// count) over `prefill..len-1`.
5039    ///
5040    /// Runtime discipline, deliberately NOT the matrix probe's: the
5041    /// first `prefill` tokens run the exact prompt pass — that pass is
5042    /// what freezes the landmarks and M — and every scored position then
5043    /// goes through `NystromState::step()`, the same code decode runs.
5044    /// So the landmarks are PREFILL-frozen (what ships), not
5045    /// full-sequence oracles (what the published probe measured), and
5046    /// every scored row carries a real far field rather than sitting
5047    /// inside the exact window.
5048    ///
5049    /// Pair with `nll_ids_from(ids, prefill)` for the exact baseline
5050    /// over the identical token set — that ratio is the honest one.
5051    pub fn nll_ids_o1(&mut self, ids: &[u32], prefill: usize) -> (f64, usize) {
5052        self.kv_cache.clear();
5053        self.kv_history.clear();
5054        self.o1_begin();
5055        let n = ids.len().saturating_sub(1);
5056        let p = prefill.min(n);
5057        // Exact prompt pass over ids[..p]: the seal consumes its q/k/v.
5058        let mut pos = 0usize;
5059        if self.can_prefill_batched() {
5060            const CHUNK: usize = 128;
5061            while pos < p {
5062                let end = (pos + CHUNK).min(p);
5063                let _ = self.prefill_batch(&ids[pos..end], pos);
5064                pos = end;
5065            }
5066        } else {
5067            while pos < p {
5068                let _ = self.forward_layers(&self.embed_single(ids[pos]), pos, None);
5069                pos += 1;
5070            }
5071        }
5072        self.o1_seal();
5073
5074        let mut nll = 0f64;
5075        let mut cnt = 0usize;
5076        for pos in p..n {
5077            let hidden = self.forward_layers(&self.embed_single(ids[pos]), pos, None);
5078            let normed = inference::rms_norm(
5079                &hidden,
5080                &self.weights.final_norm,
5081                self.rms_eps,
5082                self.norm_style,
5083            );
5084            // lm_head_forward applies the final-logit softcap itself —
5085            // capping again here double-squashed gemma-class logits
5086            // (tanh∘tanh) and reported a flattered ppl.
5087            let logits = self.lm_head_forward(&normed);
5088            let target = ids[pos + 1] as usize;
5089            let max = logits.iter().fold(f32::NEG_INFINITY, |m, &v| m.max(v));
5090            let lse: f64 = logits
5091                .iter()
5092                .map(|&v| ((v - max) as f64).exp())
5093                .sum::<f64>()
5094                .ln()
5095                + max as f64;
5096            let tok_nll = lse - logits[target] as f64;
5097            if std::env::var("CMF_PPL_TRACE").is_ok() && pos < 48 {
5098                let top = logits
5099                    .iter()
5100                    .enumerate()
5101                    .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
5102                    .map(|(i, _)| i)
5103                    .unwrap_or(0);
5104                eprintln!(
5105                    "pos {pos:3} tgt {target:6} nll {tok_nll:7.3} | top1 {top:6} lg[t]={:.2} lg[top]={:.2}",
5106                    logits[target], logits[top]
5107                );
5108            }
5109            nll += tok_nll;
5110            cnt += 1;
5111        }
5112        self.kv_cache.clear();
5113        self.kv_history.clear();
5114        (nll, cnt)
5115    }
5116
5117    /// Teacher-forced calibration data (B1): for each position, whether the
5118    /// argmax equals the actual next token, and the top-1 softmax prob
5119    /// (Born mass) under EACH temperature in `temps` — all from ONE forward
5120    /// pass (argmax/correctness are temperature-invariant; only p_max
5121    /// reshapes). Feeds `cortiq calibrate` (reliability/ECE + temperature
5122    /// fit): is the model's confidence a true property, or does it need a
5123    /// measured scaling?
5124    pub fn calib_ids(&mut self, ids: &[u32], temps: &[f32]) -> (Vec<bool>, Vec<Vec<f32>>) {
5125        self.kv_cache.clear();
5126        self.kv_history.clear();
5127        let n = ids.len().saturating_sub(1);
5128        let mut correct = Vec::with_capacity(n);
5129        let mut pmax = Vec::with_capacity(n);
5130        for pos in 0..n {
5131            let emb = self.embed_single(ids[pos]);
5132            let hidden = self.forward_layers(&emb, pos, None);
5133            let normed = inference::rms_norm(
5134                &hidden,
5135                &self.weights.final_norm,
5136                self.rms_eps,
5137                self.norm_style,
5138            );
5139            // lm_head_forward applies the final-logit softcap itself —
5140            // capping again here double-squashed gemma-class logits
5141            // (tanh∘tanh) and reported a flattered ppl.
5142            let logits = self.lm_head_forward(&normed);
5143            let target = ids[pos + 1] as usize;
5144            let (mut amax, mut mval) = (0usize, f32::NEG_INFINITY);
5145            for (i, &v) in logits.iter().enumerate() {
5146                if v > mval {
5147                    mval = v;
5148                    amax = i;
5149                }
5150            }
5151            correct.push(amax == target);
5152            let row: Vec<f32> = temps
5153                .iter()
5154                .map(|&t| {
5155                    let tt = t.max(1e-3);
5156                    let s: f32 = logits.iter().map(|&v| ((v - mval) / tt).exp()).sum();
5157                    1.0 / s.max(1e-12) // numerator at the max is exp(0)=1
5158                })
5159                .collect();
5160            pmax.push(row);
5161        }
5162        self.kv_cache.clear();
5163        self.kv_history.clear();
5164        (correct, pmax)
5165    }
5166
5167    /// Teacher-forced PPL with the dynamic router driving per-window
5168    /// skill switches (VMF experiment №2 measurement). Sequential (φ
5169    /// must update per token), returns (ppl, switch_count). The router
5170    /// must be enabled (`enable_dynamic_routing`); else this equals
5171    /// plain `ppl_ids`. The active skill when scoring token t shapes the
5172    /// logits for t+1 — on-policy over the held-out text itself.
5173    pub fn ppl_ids_dynamic(&mut self, ids: &[u32]) -> (f64, usize) {
5174        let mut router = match self.dyn_router.take() {
5175            Some(r) => r,
5176            None => return (self.ppl_ids(ids), 0),
5177        };
5178        router.reset();
5179        self.dyn_phi_seen = 0;
5180        let _ = self.set_active_skill(None);
5181
5182        self.kv_cache.clear();
5183
5184        self.kv_history.clear();
5185        let mut nll = 0f64;
5186        let mut cnt = 0usize;
5187        for pos in 0..ids.len().saturating_sub(1) {
5188            let hidden = self.forward_layers(&self.embed_single(ids[pos]), pos, None);
5189            let normed = inference::rms_norm(
5190                &hidden,
5191                &self.weights.final_norm,
5192                self.rms_eps,
5193                self.norm_style,
5194            );
5195            // lm_head_forward applies the final-logit softcap itself —
5196            // capping again here double-squashed gemma-class logits
5197            // (tanh∘tanh) and reported a flattered ppl.
5198            let logits = self.lm_head_forward(&normed);
5199            let target = ids[pos + 1] as usize;
5200            let max = logits.iter().fold(f32::NEG_INFINITY, |m, &v| m.max(v));
5201            let lse: f64 = logits
5202                .iter()
5203                .map(|&v| ((v - max) as f64).exp())
5204                .sum::<f64>()
5205                .ln()
5206                + max as f64;
5207            let tok_nll = lse - logits[target] as f64;
5208            if std::env::var("CMF_PPL_TRACE").is_ok() && pos < 48 {
5209                let top = logits
5210                    .iter()
5211                    .enumerate()
5212                    .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
5213                    .map(|(i, _)| i)
5214                    .unwrap_or(0);
5215                eprintln!(
5216                    "pos {pos:3} tgt {target:6} nll {tok_nll:7.3} | top1 {top:6} lg[t]={:.2} lg[top]={:.2}",
5217                    logits[target], logits[top]
5218                );
5219            }
5220            nll += tok_nll;
5221            cnt += 1;
5222            // Route on the evolving φ (drives the NEXT token's skill).
5223            let phi = self.dyn_phi_ema.clone();
5224            if let Some(new_active) = router.step(&phi, pos) {
5225                let _ = self.set_active_skill(new_active);
5226            }
5227        }
5228        let switches = router.switches.len();
5229        let _ = self.set_active_skill(None);
5230        self.dyn_router = Some(router);
5231        self.kv_cache.clear();
5232        self.kv_history.clear();
5233        ((nll / cnt.max(1) as f64).exp(), switches)
5234    }
5235
5236    /// Routing probe φ (spec §9): mean-pooled hidden after `layer`.
5237    pub fn probe_phi(&mut self, ids: &[u32], layer: usize) -> Vec<f32> {
5238        self.kv_cache.clear();
5239        self.kv_history.clear();
5240        let mut acc = vec![0f32; self.hidden_size];
5241        for (pos, &id) in ids.iter().enumerate() {
5242            let h = self.forward_layers_upto(&self.embed_single(id), pos, None, Some(layer));
5243            for (a, v) in acc.iter_mut().zip(&h) {
5244                *a += v;
5245            }
5246        }
5247        let n = ids.len().max(1) as f32;
5248        for a in acc.iter_mut() {
5249            *a /= n;
5250        }
5251        self.kv_cache.clear();
5252        self.kv_history.clear();
5253        acc
5254    }
5255
5256    /// Layer-major batched prefill (prefill-GEMM): full-attention —
5257    /// per-position with the existing operators (KV grows naturally,
5258    /// causality preserved), GDN projections / FFN / MoE — batched
5259    /// (a weight row is read from DRAM once per chunk, not per
5260    /// position). Returns the hidden of all positions [b × hidden].
5261    fn prefill_batch(&mut self, ids: &[u32], start_pos: usize) -> Vec<f32> {
5262        self.prefill_batch_masked(ids, start_pos, None)
5263    }
5264
5265    /// `prefill_batch` with a task mask honored on the dense-FFN panels
5266    /// (the masked-inference fast path: full fused compute, mask lands on
5267    /// the activations). The whole-chunk GPU graph is skipped for masked
5268    /// layers by the callers' arms; the per-GEMM device paths stay in
5269    /// play because the zeroing happens on the host between them.
5270    fn prefill_batch_masked(
5271        &mut self,
5272        ids: &[u32],
5273        start_pos: usize,
5274        task_mask: Option<&TaskMask>,
5275    ) -> Vec<f32> {
5276        self.prefill_batch_span(PrefillIn::Ids(ids), start_pos, task_mask, 0, usize::MAX)
5277    }
5278
5279    /// The layer-major batched walk over a layer span [from..upto_excl):
5280    /// the whole prefill machinery (chunk graph, batched attends, GEMM
5281    /// panels) for a PARTIAL stack — the network split's prefill rides
5282    /// the same canon as the local one. Input is token ids (embeds
5283    /// itself, coordinator side) or ready boundary hiddens (worker side).
5284    fn prefill_batch_span(
5285        &mut self,
5286        input: PrefillIn<'_>,
5287        start_pos: usize,
5288        task_mask: Option<&TaskMask>,
5289        from: usize,
5290        upto_excl: usize,
5291    ) -> Vec<f32> {
5292        let hs = self.hidden_size;
5293        let b = match input {
5294            PrefillIn::Ids(ids) => ids.len(),
5295            PrefillIn::Hidden(hb) => hb.len() / hs,
5296        };
5297        let upto_excl = upto_excl.min(self.num_layers);
5298        // The CPU embed is deferred: when the chunk graph takes the run
5299        // from layer 0 it gathers the embeddings on the device instead.
5300        // A hidden input is ready by definition.
5301        let mut h: Vec<f32>;
5302        let mut h_ready;
5303        match input {
5304            PrefillIn::Ids(_) => {
5305                h = vec![0.0; b * hs];
5306                h_ready = false;
5307            }
5308            PrefillIn::Hidden(hb) => {
5309                h = hb.to_vec();
5310                h_ready = true;
5311            }
5312        }
5313        let fill_h = |h: &mut Vec<f32>, me: &Self| {
5314            if let PrefillIn::Ids(ids) = input {
5315                for (bi, &id) in ids.iter().enumerate() {
5316                    let e = me.embed_single(id);
5317                    h[bi * hs..(bi + 1) * hs].copy_from_slice(&e);
5318                }
5319                if let Ok(tp) = std::env::var("CMF_TRACE_POS") {
5320                    if let Ok(t) = tp.parse::<usize>() {
5321                        if t >= start_pos && t < start_pos + ids.len() {
5322                            let bi = t - start_pos;
5323                            let row = &h[bi * hs..(bi + 1) * hs];
5324                            let n: f32 = row.iter().map(|x| x * x).sum::<f32>().sqrt();
5325                            eprintln!(
5326                                "BATCH pos {t} embed: id {} |h| = {n:.6} h0 {:.6} h1 {:.6} | b={} start={start_pos} ids[..8]={:?}",
5327                                ids[bi],
5328                                row[0],
5329                                row[1],
5330                                ids.len(),
5331                                &ids[..ids.len().min(8)]
5332                            );
5333                        }
5334                    }
5335                }
5336            }
5337        };
5338        let (_nkv, _hd, _rd, eps) = (
5339            self.num_kv_heads,
5340            self.head_dim,
5341            self.rotary_dim,
5342            self.rms_eps,
5343        );
5344        let pool = self.pool.clone();
5345        let norm_style = self.norm_style;
5346
5347        #[cfg(target_os = "macos")]
5348        let mut chunk_skip_until = 0usize;
5349        for li in from..upto_excl {
5350            crate::gpu::set_layer(li as i64); // layer-split GPU/CPU
5351            // GPU chunk graph (default-on under CMF_GPU=1): a run of
5352            // consecutive eligible layers for the whole chunk in ONE
5353            // Metal submission — norm, QKV, RoPE with fused mirror
5354            // append, causal attend, O, FFN, hidden device-resident
5355            // across the run. Any refusal falls through to the CPU path.
5356            #[cfg(target_os = "macos")]
5357            if task_mask.is_none() {
5358                if li < chunk_skip_until {
5359                    continue;
5360                }
5361                // Device-side embedding needs a q8_row embedding matrix;
5362                // with any other layout the CPU fills `h` first and the
5363                // graph starts from a ready hidden (refusing the whole
5364                // run over the embedding alone kept q4t models — the
5365                // whole Nanbeige/Bonsai class — on the CPU prefill).
5366                if !h_ready && li == 0 && self.weights.embed_tokens.q8_row_parts().is_none() {
5367                    fill_h(&mut h, self);
5368                    h_ready = true;
5369                }
5370                let ids_for_embed = match input {
5371                    PrefillIn::Ids(ids) => (!h_ready && li == 0).then_some(ids),
5372                    PrefillIn::Hidden(_) => None,
5373                };
5374                let end = self.chunk_run_gpu(li, &mut h, b, start_pos, ids_for_embed, upto_excl);
5375                if end > li {
5376                    h_ready = true;
5377                    chunk_skip_until = end;
5378                    // Looped Transformer: the graph stopped at a loop
5379                    // boundary — apply final norm before the next iteration.
5380                    if self.is_loop_end(end - 1) && end < self.num_layers {
5381                        for bi in 0..b {
5382                            let normed = inference::rms_norm(
5383                                &h[bi * hs..(bi + 1) * hs],
5384                                &self.weights.final_norm,
5385                                eps,
5386                                norm_style,
5387                            );
5388                            h[bi * hs..(bi + 1) * hs].copy_from_slice(&normed);
5389                        }
5390                    }
5391                    continue;
5392                }
5393            }
5394            if !h_ready {
5395                fill_h(&mut h, self);
5396                h_ready = true;
5397            }
5398            let lw = &self.weights.layers[self.phys_layer(li)];
5399            // ── attention ──
5400            match &lw.attn {
5401                AttnKind::Kda(w) => {
5402                    // Projections batched, recurrence sequential.
5403                    let cfg = self.kda_cfg.expect("kda layer without kda_cfg");
5404                    let mut normed = vec![0.0f32; b * hs];
5405                    for bi in 0..b {
5406                        inference::rms_norm_into(
5407                            &h[bi * hs..(bi + 1) * hs],
5408                            &lw.input_norm,
5409                            eps,
5410                            norm_style,
5411                            &mut normed[bi * hs..(bi + 1) * hs],
5412                        );
5413                    }
5414                    let attn = crate::linear_core::kda_forward_batch(
5415                        &normed,
5416                        b,
5417                        w,
5418                        &cfg,
5419                        &mut self.kv_cache.layers[li].linear_state,
5420                        pool.as_deref(),
5421                    );
5422                    for (dst, &a) in h.iter_mut().zip(&attn) {
5423                        *dst += a;
5424                    }
5425                }
5426                AttnKind::LinearGdn(w) => {
5427                    // Projections batched, recurrence sequential.
5428                    let cfg = self.gdn_cfg.expect("gdn layer without gdn_cfg");
5429                    let mut normed = vec![0.0f32; b * hs];
5430                    for bi in 0..b {
5431                        let r = inference::rms_norm(
5432                            &h[bi * hs..(bi + 1) * hs],
5433                            &lw.input_norm,
5434                            eps,
5435                            norm_style,
5436                        );
5437                        normed[bi * hs..(bi + 1) * hs].copy_from_slice(&r);
5438                    }
5439                    let attn = crate::linear_core::gdn_forward_batch(
5440                        &normed,
5441                        b,
5442                        w,
5443                        &cfg,
5444                        &mut self.kv_cache.layers[li].linear_state,
5445                        pool.as_deref(),
5446                    );
5447                    for (dst, &a) in h.iter_mut().zip(&attn) {
5448                        *dst += a;
5449                    }
5450                }
5451                AttnKind::ShortConv(w) => {
5452                    // Projections batched over the chunk; the conv walks the
5453                    // contiguous positions in order (same ring as decode).
5454                    let cfg = self
5455                        .short_conv_cfg
5456                        .expect("short-conv layer without short_conv_cfg");
5457                    let mut normed = vec![0.0f32; b * hs];
5458                    for bi in 0..b {
5459                        inference::rms_norm_into(
5460                            &h[bi * hs..(bi + 1) * hs],
5461                            &lw.input_norm,
5462                            eps,
5463                            norm_style,
5464                            &mut normed[bi * hs..(bi + 1) * hs],
5465                        );
5466                    }
5467                    let attn = short_conv_forward_batch(
5468                        &normed,
5469                        b,
5470                        w,
5471                        &cfg,
5472                        &mut self.kv_cache.layers[li].linear_state,
5473                        pool.as_deref(),
5474                    );
5475                    for (dst, &a) in h.iter_mut().zip(&attn) {
5476                        *dst += a;
5477                    }
5478                }
5479                AttnKind::Mla(w) => {
5480                    // Per-position prefill (correctness first; latent
5481                    // batching is a later optimization).
5482                    let inv_freq_l = self.layer_inv_freq(li);
5483                    let rs = self.layer_rope_scale(li);
5484                    let mut normed = vec![0.0f32; hs];
5485                    for bi in 0..b {
5486                        inference::rms_norm_into(
5487                            &h[bi * hs..(bi + 1) * hs],
5488                            &lw.input_norm,
5489                            eps,
5490                            norm_style,
5491                            &mut normed,
5492                        );
5493                        let ao = mla_attention(
5494                            w,
5495                            &normed,
5496                            &mut self.kv_cache.layers[li],
5497                            start_pos + bi,
5498                            &inv_freq_l,
5499                            rs,
5500                            eps,
5501                            pool.as_deref(),
5502                        );
5503                        for (dst, &a) in h[bi * hs..(bi + 1) * hs].iter_mut().zip(&ao) {
5504                            *dst += a;
5505                        }
5506                    }
5507                }
5508                AttnKind::Full {
5509                    wq,
5510                    wk,
5511                    wv,
5512                    wo,
5513                    q_norm,
5514                    k_norm,
5515                    output_gate,
5516                    softplus_gate,
5517                    bias,
5518                } => {
5519                    // Chunk-GEMM QKV/O; per-position causal attention
5520                    // inside (roadmap §3 P0 — full-attention prefill no
5521                    // longer re-reads the projection weights b times).
5522                    let mut normed = vec![0.0f32; b * hs];
5523                    for bi in 0..b {
5524                        inference::rms_norm_into(
5525                            &h[bi * hs..(bi + 1) * hs],
5526                            &lw.input_norm,
5527                            eps,
5528                            norm_style,
5529                            &mut normed[bi * hs..(bi + 1) * hs],
5530                        );
5531                    }
5532                    let inv_freq_l = self.layer_inv_freq(li);
5533                    let (nkv_l, hd_l, rd_l) = self.layer_geom(li);
5534                    let cfg = QwenAttnCfg {
5535                        num_heads: self.layer_num_heads(li),
5536                        num_kv_heads: nkv_l,
5537                        head_dim: hd_l,
5538                        hidden_size: hs,
5539                        position: start_pos,
5540                        inv_freq: &inv_freq_l,
5541                        rotary_dim: rd_l,
5542                        scale: self.attn_scale,
5543                        softcap: self.attn_softcap,
5544                        window: self.layer_window(li),
5545                        v_norm: self.attn_v_norm,
5546                        q_norm: q_norm.as_deref(),
5547                        k_norm: k_norm.as_deref(),
5548                        output_gate: *output_gate,
5549                        softplus_gate: softplus_gate
5550                            .as_ref()
5551                            .map(|(gate, per_head)| (gate, *per_head)),
5552                        rope_scale: self.layer_rope_scale(li),
5553                        bias: bias
5554                            .as_ref()
5555                            .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
5556                        rms_eps: eps,
5557                        norm_style,
5558                        pool: pool.as_deref(),
5559                    };
5560                    let mut attn = attention::qwen_attention_batch(
5561                        &normed,
5562                        b,
5563                        wq,
5564                        wk,
5565                        wv,
5566                        wo,
5567                        &mut self.kv_cache.layers[li],
5568                        &cfg,
5569                    );
5570                    if let Some(w) = &lw.attn_out_norm {
5571                        for bi in 0..b {
5572                            inference::rms_norm_into(
5573                                &attn[bi * hs..(bi + 1) * hs],
5574                                w,
5575                                eps,
5576                                norm_style,
5577                                &mut normed[bi * hs..(bi + 1) * hs],
5578                            );
5579                        }
5580                        attn.copy_from_slice(&normed);
5581                    }
5582                    for (dst, &a) in h.iter_mut().zip(&attn) {
5583                        *dst += a;
5584                    }
5585                }
5586                AttnKind::Linear(w) => {
5587                    for bi in 0..b {
5588                        let normed = inference::rms_norm(
5589                            &h[bi * hs..(bi + 1) * hs],
5590                            &lw.input_norm,
5591                            eps,
5592                            norm_style,
5593                        );
5594                        vmf_phase_forward(
5595                            &normed,
5596                            w,
5597                            &self.vmf_cfg.expect("linear layer without vmf_cfg"),
5598                            &mut self.kv_cache.layers[li].linear_state,
5599                            pool.as_deref(),
5600                        )
5601                        .iter()
5602                        .enumerate()
5603                        .for_each(|(i, &a)| h[bi * hs + i] += a);
5604                    }
5605                }
5606            }
5607
5608            // ── FFN batched ──
5609            let lw = &self.weights.layers[self.phys_layer(li)];
5610            let mut post = vec![0.0f32; b * hs];
5611            for bi in 0..b {
5612                let r =
5613                    inference::rms_norm(&h[bi * hs..(bi + 1) * hs], &lw.post_norm, eps, norm_style);
5614                post[bi * hs..(bi + 1) * hs].copy_from_slice(&r);
5615            }
5616            // A restrictive per-visit FFN row lands on the activations
5617            // inside the dense arm; an all-open row costs nothing.
5618            let mask_row = task_mask
5619                .filter(|m| m.ffn_active_count(li) < self.intermediate_size)
5620                .and_then(|m| m.ffn_masks.get(li))
5621                .map(|v| v.as_slice());
5622            let mut ffn = match &lw.ffn {
5623                FfnKind::Dense(d) if !d.segs.is_empty() => {
5624                    tube_ffn(d, &post, b, pool.as_deref(), mask_row)
5625                }
5626                FfnKind::Dense(d) => dense_ffn_batch(d, &post, b, pool.as_deref(), mask_row),
5627                FfnKind::Moe(m) => moe_ffn_batch(m, &post, b, hs, pool.as_deref(), None),
5628                // Dual-branch layers run per position (the expert branch
5629                // reads the raw residual — nothing to batch yet).
5630                FfnKind::DenseMoe(dm) => {
5631                    let mut out = vec![0.0f32; b * hs];
5632                    for bi in 0..b {
5633                        let r = dense_moe_ffn(
5634                            dm,
5635                            &post[bi * hs..(bi + 1) * hs],
5636                            &h[bi * hs..(bi + 1) * hs],
5637                            eps,
5638                            norm_style,
5639                            pool.as_deref(),
5640                        );
5641                        out[bi * hs..(bi + 1) * hs].copy_from_slice(&r);
5642                    }
5643                    out
5644                }
5645            };
5646            if let Some(w) = &lw.ffn_out_norm {
5647                for bi in 0..b {
5648                    inference::rms_norm_into(
5649                        &ffn[bi * hs..(bi + 1) * hs],
5650                        w,
5651                        eps,
5652                        norm_style,
5653                        &mut post[bi * hs..(bi + 1) * hs],
5654                    );
5655                }
5656                ffn.copy_from_slice(&post);
5657            }
5658            for (dst, &f) in h.iter_mut().zip(&ffn) {
5659                *dst += f;
5660            }
5661            if let Some(sc) = lw.layer_scale {
5662                for v in h.iter_mut() {
5663                    *v *= sc;
5664                }
5665            }
5666            if let Ok(tp) = std::env::var("CMF_TRACE_POS") {
5667                if let Ok(t) = tp.parse::<usize>() {
5668                    if t >= start_pos && t < start_pos + b {
5669                        let bi = t - start_pos;
5670                        let row = &h[bi * hs..(bi + 1) * hs];
5671                        let n: f32 = row.iter().map(|x| x * x).sum::<f32>().sqrt();
5672                        eprintln!(
5673                            "BATCH pos {t} after layer {li}: |h| = {n:.6} h0 {:.6} h1 {:.6}",
5674                            row[0], row[1]
5675                        );
5676                    }
5677                }
5678            }
5679            // CMF_DEBUG_LAYERS=1: per-layer hidden-state health of the
5680            // LAST prompt position — the knife for "which layer type
5681            // breaks first" on a new architecture.
5682            if std::env::var("CMF_DEBUG_LAYERS").is_ok() {
5683                let row = &h[(b - 1) * hs..b * hs];
5684                let rms =
5685                    (row.iter().map(|&v| (v as f64) * (v as f64)).sum::<f64>() / hs as f64).sqrt();
5686                let mx = row.iter().fold(0f32, |m, &v| m.max(v.abs()));
5687                eprintln!(
5688                    "layer {li:>3} {:>10} ffn={:<5} rms={rms:>12.4} max={mx:>12.4}",
5689                    match &self.weights.layers[self.phys_layer(li)].attn {
5690                        AttnKind::LinearGdn(_) => "gdn",
5691                        AttnKind::Linear(_) => "vmf",
5692                        AttnKind::ShortConv(_) => "conv",
5693                        _ => "attn",
5694                    },
5695                    match &lw.ffn {
5696                        FfnKind::Moe(_) => "moe",
5697                        FfnKind::Dense(_) => "dense",
5698                        FfnKind::DenseMoe(_) => "dense+moe",
5699                    },
5700                );
5701            }
5702            // Looped Transformer: apply final norm at the end of each loop iteration.
5703            if self.is_loop_end(li) && li + 1 < self.num_layers {
5704                for bi in 0..b {
5705                    let normed = inference::rms_norm(
5706                        &h[bi * hs..(bi + 1) * hs],
5707                        &self.weights.final_norm,
5708                        eps,
5709                        norm_style,
5710                    );
5711                    h[bi * hs..(bi + 1) * hs].copy_from_slice(&normed);
5712                }
5713            }
5714            if std::env::var("CMF_TRACE_H").is_ok() {
5715                let n = h[..hs].iter().map(|v| v.abs()).sum::<f32>() / hs as f32;
5716                let mx = h[..hs].iter().fold(0.0f32, |a, &v| a.max(v.abs()));
5717                eprintln!(
5718                    "layer {li}: mean|h|={n:.4} max|h|={mx:.2} scale={:?}",
5719                    lw.layer_scale
5720                );
5721            }
5722        }
5723        crate::gpu::set_layer(-1); // lm_head/final ops outside layer-split
5724        h
5725    }
5726
5727    /// Embed a single token.
5728    fn embed_single(&self, id: u32) -> Vec<f32> {
5729        let mut out = vec![0.0f32; self.hidden_size];
5730        if (id as usize) < self.weights.embed_tokens.rows() {
5731            self.weights.embed_tokens.row_f32(id as usize, &mut out);
5732        }
5733        if self.embed_multiplier != 1.0 {
5734            for v in out.iter_mut() {
5735                *v *= self.embed_multiplier;
5736            }
5737        }
5738        // DeepSeek-V4's hash layers route by TOKEN ID, so the id has to
5739        // reach the forward. It rides in slot 0 (the forward re-reads the
5740        // real embedding itself from the table).
5741        if self.dsv4.is_some() || self.qwen4_exp.is_some() {
5742            let mut v = vec![0.0f32; self.hidden_size.max(1)];
5743            v[0] = id as f32;
5744            return v;
5745        }
5746        // Gemma-3n: the per-layer-embedding half needs the token ID, so
5747        // it rides appended to the embedding; the g3n forward splits it.
5748        if let Some(b) = &self.g3n {
5749            return b.0.extend_embedding(id, &out, self.pool.as_deref());
5750        }
5751        out
5752    }
5753
5754    /// A run of consecutive prefill layers on the GPU for the whole
5755    /// chunk (default-on under CMF_GPU=1; CMF_GPU_CHUNK=0 disables).
5756    /// Eligibility per layer: q8_row weights, plain full attention
5757    /// (no output gate), F32 KV, no o1/masks/gemma extras. Returns the
5758    /// first layer index NOT processed (== `li0` when the run is empty).
5759    #[cfg(target_os = "macos")]
5760    fn chunk_run_gpu(
5761        &mut self,
5762        li0: usize,
5763        h: &mut [f32],
5764        b: usize,
5765        pos0: usize,
5766        embed_ids: Option<&[u32]>,
5767        cap: usize,
5768    ) -> usize {
5769        // (The old streaming attend needed a depth bound at ~1k; the
5770        // GEMM attention scales like the CPU path and lifted it.)
5771        // CMF_GPU_CHUNK=0 disables the graph.
5772        if !crate::gpu::enabled_here()
5773            || std::env::var("CMF_GPU_CHUNK")
5774                .map(|v| v == "0")
5775                .unwrap_or(false)
5776            || b < 32
5777            || self.swa.is_some()
5778            || self.global_attn.is_some()
5779            || self.attn_v_norm
5780            || (self.attn_scale - 1.0 / (self.head_dim as f32).sqrt()).abs() > 1e-9
5781        {
5782            return li0;
5783        }
5784        let Some(model) = self.model.clone() else {
5785            return li0;
5786        };
5787        let inv_freq = self.inv_freq.clone();
5788        let (nh, nkv, hd, hs) = (
5789            self.num_heads,
5790            self.num_kv_heads,
5791            self.head_dim,
5792            self.hidden_size,
5793        );
5794        // Collect the longest run of consecutive eligible layers.
5795        // Looped Transformer: stop at the loop boundary so the CPU can
5796        // apply loop_final_norm between iterations.
5797        let loop_end = if self.loop_final_norm {
5798            ((li0 / self.physical_layers) + 1) * self.physical_layers
5799        } else {
5800            self.num_layers
5801        };
5802        let mut layers: Vec<crate::gpu_metal::ChunkLayer> = Vec::new();
5803        let mut stored_at: Vec<usize> = Vec::new();
5804        for li in li0..self.num_layers.min(loop_end).min(cap) {
5805            let lw = &self.weights.layers[self.phys_layer(li)];
5806            if lw.attn_out_norm.is_some() || lw.ffn_out_norm.is_some() || lw.layer_scale.is_some() {
5807                break;
5808            }
5809            let AttnKind::Full {
5810                wq,
5811                wk,
5812                wv,
5813                wo,
5814                q_norm,
5815                k_norm,
5816                output_gate: false,
5817                softplus_gate: None,
5818                bias,
5819            } = &lw.attn
5820            else {
5821                break;
5822            };
5823            let FfnKind::Dense(d) = &lw.ffn else { break };
5824            if d.act != Act::Silu || !d.segs.is_empty() {
5825                break;
5826            }
5827            // q8_row (row_scale populated), or q4_tiled / q4tp (row_scale
5828            // empty — their scales are in the payload). Mixing across the
5829            // seven projections of one layer is fine; the encoder branches
5830            // per weight on the tensor's dtype. Anything else refuses.
5831            fn cw(t: &QTensor) -> Option<(usize, usize, usize, &[f32])> {
5832                t.q8_row_parts()
5833                    .or_else(|| t.q4t_parts().map(|(i, r, c)| (i, r, c, &[][..])))
5834                    .or_else(|| t.q4tp_parts().map(|(i, r, c)| (i, r, c, &[][..])))
5835            }
5836            let parts = (
5837                cw(wq),
5838                cw(wk),
5839                cw(wv),
5840                cw(wo),
5841                cw(&d.gate_proj),
5842                cw(&d.up_proj),
5843                cw(&d.down_proj),
5844            );
5845            let (Some(pq), Some(pk), Some(pv), Some(po), Some(pg), Some(pu), Some(pd)) = parts
5846            else {
5847                break;
5848            };
5849            let layer = &self.kv_cache.layers[li];
5850            if layer.mode != crate::kv_cache::KvMode::F32 || layer.o1.is_some() {
5851                break;
5852            }
5853            stored_at.push(layer.head_len(0));
5854            layers.push(crate::gpu_metal::ChunkLayer {
5855                model: &model,
5856                kv_id: self.graph_kv_id,
5857                layer: li,
5858                wq: pq,
5859                wk: pk,
5860                wv: pv,
5861                wo: po,
5862                gate: pg,
5863                up: pu,
5864                down: pd,
5865                input_norm: &lw.input_norm,
5866                post_norm: &lw.post_norm,
5867                bias: bias
5868                    .as_ref()
5869                    .map(|(a, bb, cc)| (a.as_slice(), bb.as_slice(), cc.as_slice())),
5870                q_norm: q_norm.as_deref(),
5871                k_norm: k_norm.as_deref(),
5872                inv_freq: &inv_freq,
5873                rd: self.rotary_dim,
5874                nh,
5875                nkv,
5876                hd,
5877                hs,
5878                inter: d.gate_proj.rows(),
5879                gemma: matches!(self.norm_style, cortiq_core::NormStyle::Gemma),
5880                eps: self.rms_eps as f32,
5881            });
5882        }
5883        if layers.is_empty() {
5884            return li0;
5885        }
5886        let row = nkv * hd;
5887        let mut store: Vec<(Vec<f32>, Vec<f32>, Vec<f32>)> = stored_at
5888            .iter()
5889            .map(|&st| (vec![0f32; b * row], vec![0f32; b * row], vec![0f32; st + b]))
5890            .collect();
5891        let mut io: Vec<crate::gpu_metal::ChunkIo> = Vec::with_capacity(layers.len());
5892        for (i, (ok, ov, oi)) in store.iter_mut().enumerate() {
5893            let li = layers[i].layer;
5894            let layer = &self.kv_cache.layers[li];
5895            io.push(crate::gpu_metal::ChunkIo {
5896                cpu_stored: stored_at[i],
5897                cpu_k: (0..nkv).map(|g| layer.head_keys(g)).collect(),
5898                cpu_v: (0..nkv).map(|g| layer.head_values(g)).collect(),
5899                out_k: ok,
5900                out_v: ov,
5901                imp: oi,
5902            });
5903        }
5904        let n_run = layers.len();
5905        let last = layers.last().map(|l| l.layer + 1).unwrap_or(li0);
5906        // Device-side embedding when the run starts the model and the
5907        // embedding matrix is q8_row-mapped.
5908        let ep = embed_ids.and_then(|ids| {
5909            self.weights
5910                .embed_tokens
5911                .q8_row_parts()
5912                .map(|(idx, rows, _c, rs)| crate::gpu_metal::ChunkEmbed {
5913                    idx,
5914                    rows,
5915                    row_scale: rs,
5916                    ids,
5917                    mult: self.embed_multiplier,
5918                })
5919        });
5920        if embed_ids.is_some() && ep.is_none() {
5921            return li0;
5922        }
5923        if !crate::gpu_metal::chunk_run_gpu(&layers, &mut io, h, b, pos0, ep.as_ref()) {
5924            return li0;
5925        }
5926        drop(io);
5927        drop(layers);
5928        // CPU caches stay the owners of record: append the chunk rows
5929        // and bank the importance masses per layer.
5930        for (i, (ok, ov, oi)) in store.iter().enumerate().take(n_run) {
5931            let li = li0 + i;
5932            let layer = &mut self.kv_cache.layers[li];
5933            for bi in 0..b {
5934                layer.append(
5935                    &ok[bi * row..(bi + 1) * row],
5936                    &ov[bi * row..(bi + 1) * row],
5937                    &[],
5938                );
5939            }
5940            layer.accumulate_imp(oi);
5941        }
5942        last
5943    }
5944
5945    /// Is layer `li` a sliding-window (local-RoPE) layer? Gemma-3:
5946    /// every `pattern`-th layer is global, the rest are local.
5947    fn layer_is_local(&self, li: usize) -> bool {
5948        if let Some(layers) = &self.sliding_layers {
5949            return layers.get(li).copied().unwrap_or(false);
5950        }
5951        match self.swa {
5952            Some((_, pattern)) => (li + 1) % pattern.max(1) != 0,
5953            None => false,
5954        }
5955    }
5956
5957    /// The RoPE table for layer `li` (local layers may have their own;
5958    /// Gemma-4 global layers use the proportional padded table).
5959    fn layer_inv_freq(&self, li: usize) -> std::sync::Arc<Vec<f32>> {
5960        if self.layer_is_local(li) {
5961            if let Some(f) = &self.inv_freq_local {
5962                return f.clone();
5963            }
5964        } else if let Some(f) = &self.inv_freq_global {
5965            return f.clone();
5966        }
5967        self.inv_freq.clone()
5968    }
5969
5970    /// The attend window for layer `li` (None = full context).
5971    fn layer_window(&self, li: usize) -> Option<usize> {
5972        self.swa
5973            .and_then(|(w, _)| self.layer_is_local(li).then_some(w))
5974    }
5975
5976    fn layer_num_heads(&self, li: usize) -> usize {
5977        self.attention_heads_per_layer
5978            .as_ref()
5979            .and_then(|v| v.get(li).copied())
5980            .unwrap_or(self.num_heads)
5981    }
5982
5983    fn layer_rope_scale(&self, li: usize) -> f32 {
5984        if self.layer_is_local(li) {
5985            self.rope_scale_local
5986        } else {
5987            self.rope_scale
5988        }
5989    }
5990
5991    /// Attention geometry of layer `li`: (num_kv_heads, head_dim,
5992    /// rotary_dim). Gemma-4 global layers override all three.
5993    fn layer_geom(&self, li: usize) -> (usize, usize, usize) {
5994        if !self.layer_is_local(li) {
5995            if let Some((ghd, gkv)) = self.global_attn {
5996                return (gkv, ghd, ghd);
5997            }
5998        }
5999        (
6000            self.num_kv_heads,
6001            self.head_dim,
6002            if self.layer_is_local(li) {
6003                self.rotary_dim_local.unwrap_or(self.rotary_dim)
6004            } else {
6005                self.rotary_dim
6006            },
6007        )
6008    }
6009
6010    /// Forward one position through all layers (hybrid dispatch).
6011    fn forward_layers(
6012        &mut self,
6013        hidden: &[f32],
6014        position: usize,
6015        task_mask: Option<&TaskMask>,
6016    ) -> Vec<f32> {
6017        self.forward_layers_upto(hidden, position, task_mask, None)
6018    }
6019
6020    // ── Network pipeline-split building blocks (coordinator/worker) ──
6021    // A remote worker owns layers [from ..= upto] and their KV; the
6022    // coordinator owns the rest plus embed / final norm / head. Attention
6023    // causality is per-layer, so a whole prompt's boundary hiddens ship
6024    // as one batch and decode ships one vector per token.
6025
6026    /// Embed one token id (embed multiplier applied).
6027    pub fn embed_id(&self, id: u32) -> Vec<f32> {
6028        self.embed_single(id)
6029    }
6030
6031    /// Refuse the archs/modes whose forward cannot be cut at a layer
6032    /// boundary. Loud by design: a split that silently changed the math
6033    /// would be a chimera.
6034    pub fn split_supported(&self) -> Result<(), String> {
6035        if self.dsv4.is_some() {
6036            return Err(
6037                "network split: DeepSeek-V4 runs its own fused stack (not splittable yet)".into(),
6038            );
6039        }
6040        if self.qwen4_exp.is_some() {
6041            return Err(
6042                "network split: Qwen3.8-Flash-Next hyper/QSA stack is not splittable yet".into(),
6043            );
6044        }
6045        if self.g3n.is_some() {
6046            return Err(
6047                "network split: Gemma-3n runs its own AltUp stack (not splittable yet)".into(),
6048            );
6049        }
6050        Ok(())
6051    }
6052
6053    /// Forward `hidden` through layers [from ..= upto] at `position`,
6054    /// appending those layers' KV/state. Both split sides call this
6055    /// over their own range; a task mask applies to the span's own
6056    /// layers (each side masks what it runs).
6057    pub fn forward_span(
6058        &mut self,
6059        hidden: &[f32],
6060        position: usize,
6061        from: usize,
6062        upto: usize,
6063        task_mask: Option<&TaskMask>,
6064    ) -> Result<Vec<f32>, String> {
6065        self.split_supported()?;
6066        if from > upto || upto >= self.num_layers {
6067            return Err(format!(
6068                "forward_span: layer range {from}..={upto} outside 0..{}",
6069                self.num_layers
6070            ));
6071        }
6072        if hidden.len() != self.hidden_size {
6073            return Err(format!(
6074                "forward_span: hidden len {} ≠ hidden_size {}",
6075                hidden.len(),
6076                self.hidden_size
6077            ));
6078        }
6079        Ok(self.forward_layers_span(hidden, position, task_mask, from, Some(upto)))
6080    }
6081
6082    /// Final norm + lm_head over a boundary hidden (the final-logit
6083    /// softcap is applied by lm_head_forward itself).
6084    pub fn logits_from_hidden(&mut self, hidden: &[f32]) -> Vec<f32> {
6085        let normed = inference::rms_norm(
6086            hidden,
6087            &self.weights.final_norm,
6088            self.rms_eps,
6089            self.norm_style,
6090        );
6091        self.lm_head_forward(&normed)
6092    }
6093
6094    /// Sample the next token with this pipeline's sampler state.
6095    pub fn sample_next(&mut self, logits: &[f32], past_tokens: &[u32]) -> u32 {
6096        sampler::sample_with_scratch(
6097            logits,
6098            &self.sampler_config,
6099            past_tokens,
6100            &mut self.rng,
6101            &mut self.sampler_scratch,
6102        )
6103    }
6104
6105    /// Fresh sequence: clear KV, reuse history and device mirrors.
6106    pub fn reset_session(&mut self) {
6107        self.kv_cache.clear();
6108        self.kv_history.clear();
6109        crate::gpu::graph_kv_reset(self.graph_kv_id);
6110    }
6111
6112    /// Batched span prefill from token ids (coordinator side): embed +
6113    /// layers [0 ..= upto]; returns the boundary hiddens of ALL positions
6114    /// (ids.len() × hidden). Rides the same layer-major machinery as the
6115    /// local prefill; falls back to the per-position walk under
6116    /// CMF_PREFILL=seq.
6117    pub fn prefill_span_ids(
6118        &mut self,
6119        ids: &[u32],
6120        start_pos: usize,
6121        upto: usize,
6122        task_mask: Option<&TaskMask>,
6123    ) -> Result<Vec<f32>, String> {
6124        self.split_supported()?;
6125        if upto >= self.num_layers {
6126            return Err(format!(
6127                "prefill_span_ids: upto {upto} outside 0..{}",
6128                self.num_layers
6129            ));
6130        }
6131        // Same predicate as the whole-stack prefill: a span whose GDN
6132        // state lives on the device must walk positions through the
6133        // graph, not through the batched CPU span.
6134        if self.can_prefill_batched() && !self.graph_prefill_preferred() {
6135            Ok(self.prefill_batch_span(PrefillIn::Ids(ids), start_pos, task_mask, 0, upto + 1))
6136        } else {
6137            let hs = self.hidden_size;
6138            let mut out = Vec::with_capacity(ids.len() * hs);
6139            for (i, &id) in ids.iter().enumerate() {
6140                let emb = self.embed_id(id);
6141                out.extend_from_slice(&self.forward_span(
6142                    &emb,
6143                    start_pos + i,
6144                    0,
6145                    upto,
6146                    task_mask,
6147                )?);
6148            }
6149            Ok(out)
6150        }
6151    }
6152
6153    /// Batched span prefill from boundary hiddens (worker side): layers
6154    /// [from ..= upto] for every position in the batch; returns the batch.
6155    pub fn prefill_span_hidden(
6156        &mut self,
6157        hidden: &[f32],
6158        start_pos: usize,
6159        from: usize,
6160        upto: usize,
6161        task_mask: Option<&TaskMask>,
6162    ) -> Result<Vec<f32>, String> {
6163        self.split_supported()?;
6164        let hs = self.hidden_size;
6165        if hidden.is_empty() || hidden.len() % hs != 0 {
6166            return Err(format!(
6167                "prefill_span_hidden: {} floats is not a multiple of hidden {hs}",
6168                hidden.len()
6169            ));
6170        }
6171        if from > upto || upto >= self.num_layers {
6172            return Err(format!(
6173                "prefill_span_hidden: layer range {from}..={upto} outside 0..{}",
6174                self.num_layers
6175            ));
6176        }
6177        if self.can_prefill_batched() && !self.graph_prefill_preferred() {
6178            Ok(self.prefill_batch_span(
6179                PrefillIn::Hidden(hidden),
6180                start_pos,
6181                task_mask,
6182                from,
6183                upto + 1,
6184            ))
6185        } else {
6186            let b = hidden.len() / hs;
6187            let mut out = Vec::with_capacity(hidden.len());
6188            for i in 0..b {
6189                let h = self.forward_span(
6190                    &hidden[i * hs..(i + 1) * hs],
6191                    start_pos + i,
6192                    from,
6193                    upto,
6194                    task_mask,
6195                )?;
6196                out.extend_from_slice(&h);
6197            }
6198            Ok(out)
6199        }
6200    }
6201
6202    /// Build the whole-token wgpu graph for a pure-attention q1 model (every
6203    /// layer Full q1 + dense q1 FFN, no gate/bias). Returns the post-stack
6204    /// hidden (caller does final norm + lm_head), or None to fall back.
6205    fn try_token_graph_wgpu(
6206        &self,
6207        hidden: &[f32],
6208        position: usize,
6209        logits_out: &mut Vec<f32>,
6210        layers_run: &mut usize,
6211    ) -> Option<Vec<f32>> {
6212        self.try_token_graph_wgpu_steps(
6213            hidden,
6214            position,
6215            logits_out,
6216            1,
6217            None,
6218            Some(layers_run),
6219            0,
6220            self.num_layers,
6221        )
6222    }
6223
6224    /// The span twin (network split): the graph covers [from..upto_excl)
6225    /// — one submit per SEGMENT per token. lm_head folds in only when
6226    /// the span reaches the last layer.
6227    fn try_token_graph_wgpu_span(
6228        &self,
6229        hidden: &[f32],
6230        position: usize,
6231        logits_out: &mut Vec<f32>,
6232        from: usize,
6233        upto_excl: usize,
6234        layers_run: &mut usize,
6235    ) -> Option<Vec<f32>> {
6236        self.try_token_graph_wgpu_steps(
6237            hidden,
6238            position,
6239            logits_out,
6240            1,
6241            None,
6242            Some(layers_run),
6243            from,
6244            upto_excl,
6245        )
6246    }
6247
6248    /// Greedy burst: forward `t_next` and let the device pick + re-embed
6249    /// the next k−1 tokens — k frames, ONE submit, k ids back. The ZML
6250    /// trade, on wgpu. None ⇒ caller keeps the per-token path.
6251    fn try_multi_burst(&self, t_next: u32, position: usize, k: usize) -> Option<Vec<u32>> {
6252        if self.o1_active() || self.attn_softcap > 0.0 {
6253            return None;
6254        }
6255        let graph_on = crate::gpu::wgpu_graph_on(crate::gpu::GraphPhase::Decode);
6256        if !graph_on || crate::gpu::graph_unsupported() {
6257            // Same memo as the decode site: this path builds the very
6258            // same graph, so a model it cannot build for must not be
6259            // walked again here either. Missing this guard was worth
6260            // 2.5x on an Adreno — 0.361 tok/s against 0.905 — because
6261            // the burst retried per token what decode had already given
6262            // up on.
6263            return None;
6264        }
6265        let emb = self.embed_single(t_next);
6266        let mut lg = Vec::new();
6267        let mut ids = Vec::new();
6268        self.try_token_graph_wgpu_steps(
6269            &emb,
6270            position,
6271            &mut lg,
6272            k,
6273            Some(&mut ids),
6274            None,
6275            0,
6276            self.num_layers,
6277        )?;
6278        (ids.len() == k).then_some(ids)
6279    }
6280
6281    /// Multi-step greedy: k whole frames in ONE submit, argmax and re-embed
6282    /// on the device. `ids_out` receives the k winner ids; the hidden/logits
6283    /// outputs are NOT produced in that mode.
6284    fn try_token_graph_wgpu_steps(
6285        &self,
6286        hidden: &[f32],
6287        position: usize,
6288        logits_out: &mut Vec<f32>,
6289        steps: usize,
6290        ids_out: Option<&mut Vec<u32>>,
6291        layers_run: Option<&mut usize>,
6292        from: usize,
6293        upto_excl: usize,
6294    ) -> Option<Vec<f32>> {
6295        // O(1) Nyström decode runs off the sealed state, not the KV cache the
6296        // graph mirrors — never take the graph while o1 is active.
6297        let o1_gpu = std::env::var("CMF_O1_GPU").as_deref() == Ok("1");
6298        if (self.o1_active() && !o1_gpu) || self.attn_softcap > 0.0 {
6299            // Softcapped scores have no graph kernel yet — CPU owns them.
6300            // o1 rides the graph only behind CMF_O1_GPU=1 while the port
6301            // proves itself; without it the CPU path owns o1 as before.
6302            return None;
6303        }
6304        // Per-layer sealed o1 state for the graph. During prefill the
6305        // state is still Collecting -> views are None -> the graph
6306        // refuses below and the CPU prefill records the q trace and
6307        // seals, exactly as the o1 design requires.
6308        let o1_views: Vec<Option<Vec<crate::nystrom::O1DeviceView<'_>>>> = (from..upto_excl)
6309            .map(|li| {
6310                if !o1_gpu {
6311                    return None;
6312                }
6313                self.kv_cache.layers[self.phys_layer(li)].o1_views()
6314            })
6315            .collect();
6316        if self.o1_active() && o1_gpu {
6317            // Any o1 layer not sealed (or degenerate exact-only) keeps the
6318            // whole token on the CPU: half-graph forwards would desync.
6319            let want: usize = (from..upto_excl)
6320                .filter(|li| self.kv_cache.layers[self.phys_layer(*li)].o1.is_some())
6321                .count();
6322            let have = o1_views.iter().filter(|v| v.is_some()).count();
6323            if want == 0 || have != want {
6324                // The silent twin of the gpu-side o1 gates, found the
6325                // same way: a 15x decode drop with an empty log. Views
6326                // stay None until the layer's state SEALS, so `have`
6327                // lagging `want` early in a run is the o1 design working
6328                // — but it must say so, or the next reader spends a
6329                // night proving the kernels innocent.
6330                // On CHANGE, not once: the first decline is the legal
6331                // unsealed prefill, and a once-print buries the state
6332                // that matters — what the count reads AFTER the seal.
6333                use std::sync::atomic::{AtomicUsize, Ordering};
6334                static LAST: AtomicUsize = AtomicUsize::new(usize::MAX);
6335                let code = have * 1000 + want;
6336                if LAST.swap(code, Ordering::Relaxed) != code {
6337                    tracing::warn!(
6338                        "o1 graph: {have} of {want} layers sealed — per-op until all seal"
6339                    );
6340                }
6341                return None;
6342            }
6343        }
6344        let nh = self.num_heads;
6345        let (nkv, hd, rd) = self.layer_geom(0);
6346        let gemma = self.norm_style == cortiq_core::NormStyle::Gemma;
6347        let mut layers = Vec::with_capacity(upto_excl - from);
6348        let mut model = None;
6349        let dbg = std::env::var("CMF_GRAPH_DEBUG").is_ok();
6350        fn gw(t: &QTensor) -> Option<crate::gpu::GraphW<'_>> {
6351            if let Some((_, i, kind, rs)) = t.graph_weight() {
6352                return Some(crate::gpu::GraphW {
6353                    idx: i,
6354                    kind,
6355                    row_scale: rs,
6356                    data: &[],
6357                });
6358            }
6359            // Small unquantized projections (GDN in_proj_a/b) stay f32.
6360            t.as_f32().map(|d| crate::gpu::GraphW {
6361                idx: 0,
6362                kind: 4,
6363                row_scale: &[],
6364                data: d,
6365            })
6366        }
6367        for li in from..upto_excl {
6368            let lw = &self.weights.layers[self.phys_layer(li)];
6369            if dbg {
6370                let ak = match &lw.attn {
6371                    AttnKind::Mla(_) => "Mla".into(),
6372                    AttnKind::Full {
6373                        output_gate, bias, ..
6374                    } => format!("Full gate={output_gate} bias={}", bias.is_some()),
6375                    AttnKind::LinearGdn(_) => "LinearGdn".into(),
6376                    AttnKind::Kda(_) => "Kda".into(),
6377                    AttnKind::Linear(_) => "Linear".into(),
6378                    AttnKind::ShortConv(_) => "ShortConv".into(),
6379                };
6380                let fk = match &lw.ffn {
6381                    FfnKind::Dense(_) => "Dense",
6382                    FfnKind::Moe(_) => "Moe",
6383                    FfnKind::DenseMoe(_) => "DenseMoe",
6384                };
6385                eprintln!("graph L{li}: attn={ak} ffn={fk}");
6386            }
6387            let gffn = match &lw.ffn {
6388                FfnKind::DenseMoe(_) => return None, // dual branch: CPU path
6389                // A tube layer is several matrices, not one — the
6390                // whole-layer graph has no shape for it yet.
6391                FfnKind::Dense(d) if !d.segs.is_empty() => return None,
6392                FfnKind::Dense(d) => crate::gpu::GraphFfn::Dense {
6393                    gate: gw(&d.gate_proj)?,
6394                    up: gw(&d.up_proj)?,
6395                    down: gw(&d.down_proj)?,
6396                },
6397                FfnKind::Moe(m) => {
6398                    // Adaptive τ and expert masks keep the CPU path, where
6399                    // they are implemented; so does a routed scale ≠ 1 (rare,
6400                    // and folding it into the select kernel is not written).
6401                    // Sigmoid routing with a selection bias (LFM2-MoE /
6402                    // DeepSeek noaux_tc) IS graphed — before it was, every
6403                    // LFM2-MoE token fell to the per-op path whole.
6404                    if m.route_tau.is_some()
6405                        || m.mask.is_some()
6406                        || (m.routed_scaling - 1.0).abs() > 1e-9
6407                    {
6408                        return None;
6409                    }
6410                    let shared = m.shared.as_ref();
6411                    let has_shared = shared.is_some();
6412                    let sgate = match shared {
6413                        Some((_, sg)) => gw(sg.as_ref()?)?,
6414                        // Unused by the kernel when has_shared is false; the
6415                        // router weight stands in so the plumbing stays total.
6416                        None => gw(&m.router)?,
6417                    };
6418                    let router = gw(&m.router)?;
6419                    let inter = m.experts.first()?.gate_proj.rows();
6420                    let mut experts = Vec::with_capacity(m.experts.len() + 1);
6421                    // q4t or q4tp, but not both in one layer — the kernels
6422                    // are picked per layer, not per expert.
6423                    let mut q4tp: Option<bool> = None;
6424                    // The mixed 2-bit profile: q2tp gate/up over a q4tp
6425                    // down. Uniform across the layer, like `q4tp` itself.
6426                    let mut gu_q2: Option<bool> = None;
6427                    for e in m.experts.iter().chain(shared.map(|(se, _)| se)) {
6428                        if !matches!(e.act, Act::Silu)
6429                            || e.gate_proj.rows() != inter
6430                            || e.up_proj.rows() != inter
6431                        {
6432                            return None;
6433                        }
6434                        let (mm, gi, ui, di, is_p, is_q2) = match e.gate_proj.mapped_q4t() {
6435                            Some((mm, gi)) => (
6436                                mm,
6437                                gi,
6438                                e.up_proj.mapped_q4t()?.1,
6439                                e.down_proj.mapped_q4t()?.1,
6440                                false,
6441                                false,
6442                            ),
6443                            None => match e.gate_proj.mapped_q2tp() {
6444                                Some((mm, gi)) => (
6445                                    mm,
6446                                    gi,
6447                                    e.up_proj.mapped_q2tp()?.1,
6448                                    e.down_proj.mapped_q4tp()?.1,
6449                                    true,
6450                                    true,
6451                                ),
6452                                None => {
6453                                    let (mm, gi) = e.gate_proj.mapped_q4tp()?;
6454                                    (
6455                                        mm,
6456                                        gi,
6457                                        e.up_proj.mapped_q4tp()?.1,
6458                                        e.down_proj.mapped_q4tp()?.1,
6459                                        true,
6460                                        false,
6461                                    )
6462                                }
6463                            },
6464                        };
6465                        if *q4tp.get_or_insert(is_p) != is_p || *gu_q2.get_or_insert(is_q2) != is_q2
6466                        {
6467                            // The shared expert rides in the same packed
6468                            // buffer as the routed ones, so a layer that
6469                            // mixes layouts cannot be indexed by one stride.
6470                            // Say so: the symptom is a whole model quietly
6471                            // running its MoE on the CPU.
6472                            tracing::warn!(
6473                                "MoE layer mixes expert layouts (q4tp={is_p}, q2tp gate/up={is_q2})                                  — every expert of a layer, INCLUDING the shared one, must share                                  a layout. The whole-token graph declines this layer."
6474                            );
6475                            return None;
6476                        }
6477                        model.get_or_insert_with(|| mm.clone());
6478                        experts.push((gi, ui, di));
6479                    }
6480                    crate::gpu::GraphFfn::Moe {
6481                        router,
6482                        shared_gate: sgate,
6483                        experts,
6484                        n_exp: m.experts.len(),
6485                        // CMF_TOPK_PROBE: timing probe only — output is WRONG.
6486                        // Fewer experts shrink the MoE arithmetic while the
6487                        // dispatch count stays identical, which is the only
6488                        // clean way to tell a launch-bound decode from a
6489                        // compute-bound one.
6490                        top_k: std::env::var("CMF_TOPK_PROBE")
6491                            .ok()
6492                            .and_then(|v| v.parse::<usize>().ok())
6493                            .filter(|k| *k > 0 && *k <= m.top_k)
6494                            .unwrap_or(m.top_k),
6495                        inter,
6496                        norm_topk: m.norm_topk_prob,
6497                        q4tp: q4tp?,
6498                        gu_q2: gu_q2.unwrap_or(false),
6499                        sigmoid: m.router_sigmoid,
6500                        bias: m.expert_bias.as_deref(),
6501                        has_shared,
6502                    }
6503                }
6504            };
6505            let attn = match &lw.attn {
6506                AttnKind::Full {
6507                    wq,
6508                    wk,
6509                    wv,
6510                    wo,
6511                    q_norm,
6512                    k_norm,
6513                    output_gate,
6514                    softplus_gate,
6515                    bias,
6516                } => {
6517                    if softplus_gate.is_some() || self.attention_heads_per_layer.is_some() {
6518                        return None;
6519                    }
6520                    let (m, _, _, _) = wq.graph_weight()?;
6521                    model = Some(m.clone());
6522                    crate::gpu::GraphAttn::Full {
6523                        wq: gw(wq)?,
6524                        wk: gw(wk)?,
6525                        wv: gw(wv)?,
6526                        wo: gw(wo)?,
6527                        q_norm: q_norm.as_deref(),
6528                        k_norm: k_norm.as_deref(),
6529                        bias: bias
6530                            .as_ref()
6531                            .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
6532                        output_gate: *output_gate,
6533                        cpu_k: self.kv_cache.layers[li].k_heads(),
6534                        cpu_v: self.kv_cache.layers[li].v_heads(),
6535                    }
6536                }
6537                AttnKind::LinearGdn(w) => {
6538                    let cfg = self.gdn_cfg?;
6539                    let (m, _, _, _) = w.in_proj_qkv.graph_weight()?;
6540                    model = Some(m.clone());
6541                    crate::gpu::GraphAttn::Gdn {
6542                        qkv: gw(&w.in_proj_qkv)?,
6543                        z: gw(&w.in_proj_z)?,
6544                        a: gw(&w.in_proj_a)?,
6545                        b: gw(&w.in_proj_b)?,
6546                        out: gw(&w.out_proj)?,
6547                        conv1d: &w.conv1d,
6548                        a_log: &w.a_log,
6549                        dt_bias: &w.dt_bias,
6550                        norm: &w.norm,
6551                        nv: cfg.num_v_heads,
6552                        nk: cfg.num_k_heads,
6553                        dk: cfg.key_head_dim,
6554                        dv: cfg.value_head_dim,
6555                        kk: cfg.conv_kernel,
6556                        cpu_state: &self.kv_cache.layers[self.phys_layer(li)].linear_state,
6557                    }
6558                }
6559                AttnKind::ShortConv(w) => {
6560                    let cfg = self.short_conv_cfg?;
6561                    let (m, _, _, _) = w.in_proj.graph_weight()?;
6562                    model = Some(m.clone());
6563                    crate::gpu::GraphAttn::ShortConv {
6564                        inp: gw(&w.in_proj)?,
6565                        out: gw(&w.out_proj)?,
6566                        taps: &w.conv,
6567                        kernel: cfg.kernel,
6568                        cpu_state: &self.kv_cache.layers[self.phys_layer(li)].linear_state,
6569                    }
6570                }
6571                _ => return None,
6572            };
6573            layers.push(crate::gpu::GraphLayer {
6574                input_norm: &lw.input_norm,
6575                attn,
6576                post_norm: &lw.post_norm,
6577                ffn: gffn,
6578            });
6579        }
6580        let model = model?;
6581        // Fold final-norm + lm_head into the graph when this call wants logits
6582        // and the lm_head is a graphable (quantized) weight — the graph then
6583        // reads back logits (into logits_out) instead of the hidden, dropping
6584        // the separate CPU/GPU lm_head op + its sync. Never the f32 fallback:
6585        // an unquantized lm_head is vocab·hidden and must not be uploaded.
6586        let lm_gw = if upto_excl == self.num_layers
6587            && self.graph_want_logits
6588            && std::env::var("CMF_GPU_LMHEAD")
6589                .map(|v| v != "0")
6590                .unwrap_or(true)
6591        {
6592            self.weights.lm_head.graph_weight().map(|(_, i, kind, rs)| {
6593                (
6594                    crate::gpu::GraphW {
6595                        idx: i,
6596                        kind,
6597                        row_scale: rs,
6598                        data: &[],
6599                    },
6600                    self.weights.lm_head.rows(),
6601                )
6602            })
6603        } else {
6604            None
6605        };
6606        let lm = lm_gw.as_ref().map(|(gw, rows)| (gw, *rows));
6607        // Multi-step re-embeds the winner on the device.
6608        let emb_gw = if steps > 1 {
6609            self.weights
6610                .embed_tokens
6611                .graph_weight()
6612                .map(|(_, i, kind, rs)| {
6613                    (
6614                        crate::gpu::GraphW {
6615                            idx: i,
6616                            kind,
6617                            row_scale: rs,
6618                            data: &[],
6619                        },
6620                        self.weights.embed_tokens.rows(),
6621                        self.embed_multiplier,
6622                    )
6623                })
6624        } else {
6625            None
6626        };
6627
6628        // Loop boundaries: virtual layer indices after which final_norm is
6629        // applied (mid-stack only; the GLOBAL last layer's norm folds into
6630        // lm_head). Span-relative — the executor compares its enumerate
6631        // index. A span ending mid-stack keeps its boundary norm even when
6632        // it is the span's own last layer.
6633        let loop_norm_at: Vec<usize> = if self.loop_final_norm {
6634            (from..upto_excl.min(self.num_layers - 1))
6635                .filter(|&li| (li + 1) % self.physical_layers == 0)
6636                .map(|li| li - from)
6637                .collect()
6638        } else {
6639            Vec::new()
6640        };
6641        let mut h = hidden.to_vec();
6642        crate::gpu::forward_token_graph(
6643            &model,
6644            self.graph_kv_id,
6645            &layers,
6646            &o1_views,
6647            self.o1_epoch,
6648            &self.inv_freq,
6649            &mut h,
6650            nh,
6651            nkv,
6652            hd,
6653            rd,
6654            self.hidden_size,
6655            self.intermediate_size,
6656            position,
6657            self.kv_cache.max_seq_len,
6658            gemma,
6659            self.rms_eps as f32,
6660            lm,
6661            &self.weights.final_norm,
6662            logits_out,
6663            &loop_norm_at,
6664            steps,
6665            emb_gw.as_ref().map(|(gw, rows, m)| (gw, *rows, *m)),
6666            ids_out,
6667            layers_run,
6668            from,
6669            false,
6670        )
6671        .then_some(h)
6672    }
6673
6674    /// Batched prefill: k contiguous prompt positions through the whole wgpu
6675    /// graph in ONE submit (projections/FFN as GEMMs). `hiddens` is [k·hidden]
6676    /// in/out (embeddings in, layer output out); KV mirror / GDN state advance.
6677    /// false ⇒ unsupported → caller keeps the per-position graph.
6678    /// The b-row Metal graph plan for the whole model: every layer as a
6679    /// GDN run or a full-attention item, all-or-nothing (a layer outside the
6680    /// graph's contract → None, the caller runs plain). Shared by the
6681    /// speculative verify and the batched prefill.
6682    #[cfg(target_os = "macos")]
6683    #[allow(clippy::type_complexity)]
6684    fn metal_rows_plan(
6685        &self,
6686    ) -> Option<(
6687        Vec<MetalRowsItem<'_>>,
6688        std::sync::Arc<cortiq_core::CmfModel>,
6689        Option<crate::gpu_metal::GdnGpuCfg>,
6690    )> {
6691        use crate::gpu_metal::{AttnGpuLayer, GdnGpuCfg, GdnGpuLayer, MetalFfn};
6692        if !crate::gpu::q1_force()
6693            || !crate::gpu::enabled_here()
6694            || std::env::var("CMF_GPU_BLOCK")
6695                .map(|v| v == "0")
6696                .unwrap_or(false)
6697            || self.attn_softcap > 0.0
6698            || self.o1_active()
6699            || self.swa.is_some()
6700            || self.global_attn.is_some()
6701            || self.attention_heads_per_layer.is_some()
6702            || self.attn_v_norm
6703            || self.loop_final_norm
6704            || (self.attn_scale - 1.0 / (self.head_dim as f32).sqrt()).abs() > 1e-9
6705        {
6706            return None;
6707        }
6708        let attend_contract = self.head_dim % 4 == 0
6709            && self.head_dim <= 256
6710            && self.rotary_dim >= 2
6711            && self.rotary_dim <= self.head_dim
6712            && (self.rotary_dim / 2) % 32 == 0
6713            && self.num_kv_heads > 0
6714            && self.num_heads % self.num_kv_heads == 0;
6715        if !attend_contract {
6716            return None;
6717        }
6718        let mut plan: Vec<MetalRowsItem> = Vec::new();
6719        let mut model_ref: Option<std::sync::Arc<cortiq_core::CmfModel>> = None;
6720        for li in 0..self.num_layers {
6721            let lw = &self.weights.layers[self.phys_layer(li)];
6722            if lw.attn_out_norm.is_some() || lw.ffn_out_norm.is_some() || lw.layer_scale.is_some() {
6723                return None;
6724            }
6725            let ffn = match &lw.ffn {
6726                FfnKind::Dense(d) if d.act == Act::Silu && d.segs.is_empty() => {
6727                    let (Some(g), Some(u), Some(dn)) = (
6728                        d.gate_proj.q1_parts(),
6729                        d.up_proj.q1_parts(),
6730                        d.down_proj.q1_parts(),
6731                    ) else {
6732                        return None;
6733                    };
6734                    MetalFfn::Dense {
6735                        gate: g,
6736                        up: u,
6737                        down: dn,
6738                    }
6739                }
6740                _ => return None,
6741            };
6742            match &lw.attn {
6743                AttnKind::LinearGdn(w) if self.gdn_cfg.is_some() => {
6744                    let (Some(qkv), Some(z), Some(a), Some(bb), Some(out)) = (
6745                        w.in_proj_qkv.q1_parts(),
6746                        w.in_proj_z.q1_parts(),
6747                        w.in_proj_a.f32_parts(),
6748                        w.in_proj_b.f32_parts(),
6749                        w.out_proj.q1_parts(),
6750                    ) else {
6751                        return None;
6752                    };
6753                    if let QTensor::Mapped { model, .. } = &w.in_proj_qkv {
6754                        model_ref.get_or_insert_with(|| model.clone());
6755                    }
6756                    let gl = GdnGpuLayer {
6757                        attn_norm: &lw.input_norm,
6758                        post_norm: &lw.post_norm,
6759                        qkv,
6760                        z,
6761                        a,
6762                        b: bb,
6763                        out,
6764                        ffn,
6765                        conv1d: &w.conv1d,
6766                        a_log: &w.a_log,
6767                        dt_bias: &w.dt_bias,
6768                        gnorm: &w.norm,
6769                    };
6770                    match plan.last_mut() {
6771                        Some(MetalRowsItem::Gdn { run, .. }) => run.push(gl),
6772                        _ => plan.push(MetalRowsItem::Gdn {
6773                            run: vec![gl],
6774                            first: li,
6775                        }),
6776                    }
6777                }
6778                AttnKind::Full {
6779                    wq,
6780                    wk,
6781                    wv,
6782                    wo,
6783                    q_norm,
6784                    k_norm,
6785                    output_gate,
6786                    softplus_gate: None,
6787                    bias: None,
6788                } => {
6789                    let (Some(pq), Some(pk), Some(pv), Some(po)) =
6790                        (wq.q1_parts(), wk.q1_parts(), wv.q1_parts(), wo.q1_parts())
6791                    else {
6792                        return None;
6793                    };
6794                    if let QTensor::Mapped { model, .. } = wq {
6795                        model_ref.get_or_insert_with(|| model.clone());
6796                    }
6797                    let cache = &self.kv_cache.layers[li];
6798                    if cache.mode != crate::kv_cache::KvMode::F32 || cache.o1.is_some() {
6799                        return None;
6800                    }
6801                    plan.push(MetalRowsItem::Attn {
6802                        l: AttnGpuLayer {
6803                            attn_norm: &lw.input_norm,
6804                            post_norm: &lw.post_norm,
6805                            wq: pq,
6806                            wk: pk,
6807                            wv: pv,
6808                            wo: po,
6809                            ffn,
6810                        },
6811                        li,
6812                        q_norm: q_norm.as_deref(),
6813                        k_norm: k_norm.as_deref(),
6814                        output_gate: *output_gate,
6815                    });
6816                }
6817                _ => return None,
6818            }
6819        }
6820        let model = model_ref?;
6821        let gcfg = self.gdn_cfg.map(|cfg| GdnGpuCfg {
6822            nv: cfg.num_v_heads,
6823            nk: cfg.num_k_heads,
6824            dk: cfg.key_head_dim,
6825            dv: cfg.value_head_dim,
6826            kk: cfg.conv_kernel,
6827            hidden: self.hidden_size,
6828            inter: self.intermediate_size,
6829            c_dim: cfg.conv_dim(),
6830            eps: cfg.rms_eps as f32,
6831            gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
6832        });
6833        Some((plan, model, gcfg))
6834    }
6835
6836    /// `AttnDeviceParams` for a plan item over the CPU cache as it stands.
6837    #[cfg(target_os = "macos")]
6838    #[allow(clippy::too_many_arguments)]
6839    fn metal_attn_params<'a>(
6840        li: usize,
6841        cache: &'a crate::kv_cache::LayerKvCache,
6842        q_norm: Option<&'a [f32]>,
6843        k_norm: Option<&'a [f32]>,
6844        output_gate: bool,
6845        inv_freq: &'a [f32],
6846        geom: (usize, usize, usize, usize),
6847        pos0: usize,
6848        kv_id: u64,
6849        eps: f32,
6850        gemma: bool,
6851    ) -> (crate::gpu_metal::AttnDeviceParams<'a>, usize) {
6852        let (nh, nkv, hd, rd) = geom;
6853        let cpu_k: Vec<&[f32]> = (0..nkv).map(|g| cache.head_keys(g)).collect();
6854        let cpu_v: Vec<&[f32]> = (0..nkv).map(|g| cache.head_values(g)).collect();
6855        let cpu_stored = cpu_k[0].len() / hd;
6856        (
6857            crate::gpu_metal::AttnDeviceParams {
6858                kv_id,
6859                layer: li,
6860                nh,
6861                nkv,
6862                hd,
6863                rd,
6864                position: pos0,
6865                eps,
6866                gemma,
6867                output_gate,
6868                q_norm,
6869                k_norm,
6870                inv_freq,
6871                cpu_k,
6872                cpu_v,
6873                cpu_stored,
6874                o1: None,
6875            },
6876            cpu_stored,
6877        )
6878    }
6879
6880    /// Run the rows plan over `hiddens` (b rows at `pos0..`): validate,
6881    /// encode every item, optionally the head, sync. Returns the graph
6882    /// (for the commit / state finish) plus the GDN layer indices and the
6883    /// attention layers with the row count they were encoded against.
6884    #[cfg(target_os = "macos")]
6885    #[allow(clippy::type_complexity)]
6886    fn metal_rows_run(
6887        &mut self,
6888        hiddens: &mut [f32],
6889        pos0: usize,
6890        b: usize,
6891        prefill: bool,
6892        spec: Option<((usize, usize, usize), &[f32], &mut Vec<f32>)>,
6893    ) -> Option<MetalVerifyPending> {
6894        use crate::gpu_metal::{GraphDims, VerifyGraph};
6895        let want = self.gdn_cfg.map(|c| c.state_len()).unwrap_or(0);
6896        for l in &mut self.kv_cache.layers {
6897            if l.linear_state.len() != want && want > 0 {
6898                l.linear_state = vec![0f32; want];
6899            }
6900        }
6901        let (plan, model, gcfg) = self.metal_rows_plan()?;
6902        let dims = GraphDims {
6903            hidden: self.hidden_size,
6904            eps: self.rms_eps as f32,
6905            gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
6906        };
6907        let mut graph = if prefill {
6908            VerifyGraph::new_prefill(&model, dims, hiddens, b)?
6909        } else {
6910            VerifyGraph::new(&model, dims, hiddens, b)?
6911        };
6912        let geom = (
6913            self.num_heads,
6914            self.num_kv_heads,
6915            self.head_dim,
6916            self.rotary_dim,
6917        );
6918        let gemma = self.norm_style == cortiq_core::NormStyle::Gemma;
6919        let eps = self.rms_eps as f32;
6920        let kv_id = self.graph_kv_id;
6921        let inv_freq = self.inv_freq.clone();
6922        for item in &plan {
6923            let ok = match item {
6924                MetalRowsItem::Gdn { run, .. } => gcfg
6925                    .as_ref()
6926                    .map(|gc| run.iter().all(|l| graph.gdn_ok(l, gc)))
6927                    .unwrap_or(false),
6928                MetalRowsItem::Attn {
6929                    l,
6930                    li,
6931                    q_norm,
6932                    k_norm,
6933                    output_gate,
6934                } => {
6935                    let (p, _) = Self::metal_attn_params(
6936                        *li,
6937                        &self.kv_cache.layers[*li],
6938                        *q_norm,
6939                        *k_norm,
6940                        *output_gate,
6941                        &inv_freq,
6942                        geom,
6943                        pos0,
6944                        kv_id,
6945                        eps,
6946                        gemma,
6947                    );
6948                    graph.attn_ok(l, &p)
6949                }
6950            };
6951            if !ok {
6952                use std::sync::atomic::{AtomicBool, Ordering};
6953                static SAID: AtomicBool = AtomicBool::new(false);
6954                if !SAID.swap(true, Ordering::Relaxed) {
6955                    tracing::warn!("metal rows graph: a layer failed preflight — declining");
6956                }
6957                return None;
6958            }
6959        }
6960        let lm = match &spec {
6961            Some((lm, _, _)) => {
6962                if !graph.lm_head_ok(*lm) {
6963                    return None;
6964                }
6965                Some(*lm)
6966            }
6967            None => None,
6968        };
6969        let mut gdn_layers = Vec::new();
6970        let mut attn_layers = Vec::new();
6971        for item in &plan {
6972            match item {
6973                MetalRowsItem::Gdn { run, first } => {
6974                    let ro: Vec<&[f32]> = self.kv_cache.layers[*first..*first + run.len()]
6975                        .iter()
6976                        .map(|l| l.linear_state.as_slice())
6977                        .collect();
6978                    if !graph.encode_gdn_run_b(run, &ro, gcfg.as_ref().unwrap()) {
6979                        return None;
6980                    }
6981                    gdn_layers.extend(*first..*first + run.len());
6982                }
6983                MetalRowsItem::Attn {
6984                    l,
6985                    li,
6986                    q_norm,
6987                    k_norm,
6988                    output_gate,
6989                } => {
6990                    let (p, cpu_stored) = Self::metal_attn_params(
6991                        *li,
6992                        &self.kv_cache.layers[*li],
6993                        *q_norm,
6994                        *k_norm,
6995                        *output_gate,
6996                        &inv_freq,
6997                        geom,
6998                        pos0,
6999                        kv_id,
7000                        eps,
7001                        gemma,
7002                    );
7003                    if !graph.encode_attn_b(l, &p) {
7004                        return None;
7005                    }
7006                    attn_layers.push((*li, cpu_stored));
7007                }
7008            }
7009        }
7010        if let (Some(lm), Some((_, final_norm, _))) = (lm, spec.as_ref()) {
7011            if !graph.encode_lm_head_b(final_norm, lm) {
7012                return None;
7013            }
7014        }
7015        graph.sync();
7016        if let Some((lm, _, logits)) = spec {
7017            logits.resize(b * lm.1, 0.0);
7018            graph.read_logits(logits);
7019        }
7020        graph.read_hidden(hiddens);
7021        Some(MetalVerifyPending {
7022            graph,
7023            gdn_layers,
7024            attn_layers,
7025        })
7026    }
7027
7028    /// Native-Metal twin of `try_batch_graph_wgpu`: the b rows through the
7029    /// whole model on the `VerifyGraph` (one submit), the head folded in
7030    /// when `spec` asks; `hiddens` come back as the last layer's output
7031    /// rows, `spec.2` as `[b][lm_rows]` logits. The graph is parked in
7032    /// `metal_verify` for `metal_verify_commit`.
7033    #[cfg(target_os = "macos")]
7034    fn try_batch_graph_metal(
7035        &mut self,
7036        hiddens: &mut [f32],
7037        positions: &[usize],
7038        b: usize,
7039        spec: Option<((usize, usize, usize), &[f32], &mut Vec<f32>)>,
7040    ) -> bool {
7041        let _t0 = std::time::Instant::now();
7042        if positions.len() != b
7043            || positions.windows(2).any(|w| w[1] != w[0] + 1)
7044            || hiddens.len() != b * self.hidden_size
7045        {
7046            return false;
7047        }
7048        let Some(pending) = self.metal_rows_run(hiddens, positions[0], b, false, spec) else {
7049            return false;
7050        };
7051        if std::env::var("CMF_GRAPH_SPEC_TIME").is_ok() {
7052            eprintln!(
7053                "metal-verify: {:.1} ms | b={b}",
7054                _t0.elapsed().as_secs_f64() * 1e3
7055            );
7056        }
7057        self.metal_verify = Some(pending);
7058        true
7059    }
7060
7061    /// Batched prefill on the Metal rows graph: `ids` (≤ 512) at
7062    /// `start_pos..`, states written in place, K/V rows appended to the
7063    /// CPU caches; returns every position's output hidden (`[b][hidden]`).
7064    /// None = the graph declined before touching anything.
7065    #[cfg(target_os = "macos")]
7066    fn prefill_batch_metal(&mut self, ids: &[u32], start_pos: usize) -> Option<Vec<f32>> {
7067        let b = ids.len();
7068        if b == 0 || b > 512 {
7069            return None;
7070        }
7071        let hs = self.hidden_size;
7072        let mut hiddens = vec![0f32; b * hs];
7073        for (j, &id) in ids.iter().enumerate() {
7074            let e = self.embed_single(id);
7075            hiddens[j * hs..(j + 1) * hs].copy_from_slice(&e);
7076        }
7077        let mut pending = self.metal_rows_run(&mut hiddens, start_pos, b, true, None)?;
7078        // states are final: copy them to the owners
7079        let idxs = pending.gdn_layers.clone();
7080        let mut outs: Vec<&mut [f32]> = self
7081            .kv_cache
7082            .layers
7083            .iter_mut()
7084            .enumerate()
7085            .filter(|(i, _)| idxs.binary_search(i).is_ok())
7086            .map(|(_, l)| l.linear_state.as_mut_slice())
7087            .collect();
7088        pending.graph.finish_states(&mut outs);
7089        let (nkv, hd) = (self.num_kv_heads, self.head_dim);
7090        let mut kbuf = vec![0f32; b * nkv * hd];
7091        let mut vbuf = vec![0f32; b * nkv * hd];
7092        for (li, cpu_stored) in &pending.attn_layers {
7093            if crate::gpu_metal::kv_mirror_read_rows(
7094                self.graph_kv_id,
7095                *li,
7096                nkv,
7097                hd,
7098                *cpu_stored,
7099                b,
7100                &mut kbuf,
7101                &mut vbuf,
7102            ) {
7103                let cache = &mut self.kv_cache.layers[*li];
7104                for r in 0..b {
7105                    cache.append(
7106                        &kbuf[r * nkv * hd..(r + 1) * nkv * hd],
7107                        &vbuf[r * nkv * hd..(r + 1) * nkv * hd],
7108                        &[],
7109                    );
7110                }
7111                crate::gpu_metal::kv_mirror_set_stored(self.graph_kv_id, *li, cpu_stored + b);
7112            }
7113        }
7114        Some(hiddens)
7115    }
7116
7117    /// Commit a Metal verify round: replay the GDN recurrences over the
7118    /// `a + 1` accepted positions into the CPU states, append the accepted
7119    /// K/V rows from the mirrors to the CPU caches, re-point the mirrors.
7120    #[cfg(target_os = "macos")]
7121    fn metal_verify_commit(&mut self, a: usize) -> bool {
7122        let Some(mut pending) = self.metal_verify.take() else {
7123            return false;
7124        };
7125        let n = a + 1;
7126        // encode order == ascending layer order (the plan walks 0..layers)
7127        let idxs = pending.gdn_layers.clone();
7128        let mut outs: Vec<&mut [f32]> = self
7129            .kv_cache
7130            .layers
7131            .iter_mut()
7132            .enumerate()
7133            .filter(|(i, _)| idxs.binary_search(i).is_ok())
7134            .map(|(_, l)| l.linear_state.as_mut_slice())
7135            .collect();
7136        if !pending.graph.commit(n, &mut outs) {
7137            return false;
7138        }
7139        let (nkv, hd) = (self.num_kv_heads, self.head_dim);
7140        let mut kbuf = vec![0f32; n * nkv * hd];
7141        let mut vbuf = vec![0f32; n * nkv * hd];
7142        for (li, cpu_stored) in &pending.attn_layers {
7143            if crate::gpu_metal::kv_mirror_read_rows(
7144                self.graph_kv_id,
7145                *li,
7146                nkv,
7147                hd,
7148                *cpu_stored,
7149                n,
7150                &mut kbuf,
7151                &mut vbuf,
7152            ) {
7153                let cache = &mut self.kv_cache.layers[*li];
7154                for r in 0..n {
7155                    cache.append(
7156                        &kbuf[r * nkv * hd..(r + 1) * nkv * hd],
7157                        &vbuf[r * nkv * hd..(r + 1) * nkv * hd],
7158                        &[],
7159                    );
7160                }
7161                crate::gpu_metal::kv_mirror_set_stored(self.graph_kv_id, *li, cpu_stored + n);
7162            }
7163        }
7164        true
7165    }
7166
7167    /// The round's warm-ups as ONE b-row graph run over the MTP block on
7168    /// Metal: `pairs` = (trunk hidden, next token) at consecutive positions
7169    /// from `first_pos`; the block's input projection is folded in, the
7170    /// appended K/V rows are pulled into the CPU MTP cache. False = the
7171    /// graph declined (nothing appended).
7172    #[cfg(target_os = "macos")]
7173    fn mtp_warm_batch_metal(
7174        &mut self,
7175        m: &mut MtpModule,
7176        pairs: &[(&[f32], u32)],
7177        first_pos: usize,
7178    ) -> bool {
7179        use crate::gpu_metal::{AttnDeviceParams, AttnGpuLayer, GraphDims, MetalFfn, VerifyGraph};
7180        let b = pairs.len();
7181        if b == 0 || b > 512 || m.kv.mode != crate::kv_cache::KvMode::F32 || m.kv.o1.is_some() {
7182            return false;
7183        }
7184        let AttnKind::Full {
7185            wq,
7186            wk,
7187            wv,
7188            wo,
7189            q_norm,
7190            k_norm,
7191            output_gate,
7192            softplus_gate: None,
7193            bias: None,
7194        } = &m.layer.attn
7195        else {
7196            return false;
7197        };
7198        let FfnKind::Dense(d) = &m.layer.ffn else {
7199            return false;
7200        };
7201        if !d.segs.is_empty() {
7202            return false;
7203        }
7204        let (Some(pq), Some(pk), Some(pv), Some(po)) =
7205            (wq.q1_parts(), wk.q1_parts(), wv.q1_parts(), wo.q1_parts())
7206        else {
7207            return false;
7208        };
7209        let (Some(g), Some(u), Some(dn)) = (
7210            d.gate_proj.q1_parts(),
7211            d.up_proj.q1_parts(),
7212            d.down_proj.q1_parts(),
7213        ) else {
7214            return false;
7215        };
7216        let Some(eh) = m.eh_proj.q1_parts() else {
7217            return false;
7218        };
7219        let QTensor::Mapped { model, .. } = wq else {
7220            return false;
7221        };
7222        let model = model.clone();
7223        let hs = self.hidden_size;
7224        // [enorm(embed(tok)); hnorm(hidden)] rows
7225        let mut cat = vec![0f32; b * 2 * hs];
7226        for (j, (h, tok)) in pairs.iter().enumerate() {
7227            let e = self.embed_single(*tok);
7228            let (ce, ch) = cat[j * 2 * hs..(j + 1) * 2 * hs].split_at_mut(hs);
7229            inference::rms_norm_into(&e, &m.enorm, self.rms_eps, self.norm_style, ce);
7230            inference::rms_norm_into(h, &m.hnorm, self.rms_eps, self.norm_style, ch);
7231        }
7232        let dims = GraphDims {
7233            hidden: hs,
7234            eps: self.rms_eps as f32,
7235            gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
7236        };
7237        let Some(mut graph) = VerifyGraph::new_via_proj(&model, dims, eh, &cat, b) else {
7238            return false;
7239        };
7240        let l = AttnGpuLayer {
7241            attn_norm: &m.layer.input_norm,
7242            post_norm: &m.layer.post_norm,
7243            wq: pq,
7244            wk: pk,
7245            wv: pv,
7246            wo: po,
7247            ffn: MetalFfn::Dense {
7248                gate: g,
7249                up: u,
7250                down: dn,
7251            },
7252        };
7253        let (nh, nkv, hd, rd) = (
7254            self.num_heads,
7255            self.num_kv_heads,
7256            self.head_dim,
7257            self.rotary_dim,
7258        );
7259        let inv_freq = self.inv_freq.clone();
7260        let cpu_stored;
7261        {
7262            let cache = &m.kv;
7263            let cpu_k: Vec<&[f32]> = (0..nkv).map(|g| cache.head_keys(g)).collect();
7264            let cpu_v: Vec<&[f32]> = (0..nkv).map(|g| cache.head_values(g)).collect();
7265            cpu_stored = cpu_k[0].len() / hd;
7266            if cpu_stored != first_pos {
7267                return false;
7268            }
7269            let p = AttnDeviceParams {
7270                kv_id: self.mtp_kv_id(),
7271                layer: Self::MTP_LAYER_BASE,
7272                nh,
7273                nkv,
7274                hd,
7275                rd,
7276                position: first_pos,
7277                eps: self.rms_eps as f32,
7278                gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
7279                output_gate: *output_gate,
7280                q_norm: q_norm.as_deref(),
7281                k_norm: k_norm.as_deref(),
7282                inv_freq: &inv_freq,
7283                cpu_k,
7284                cpu_v,
7285                cpu_stored,
7286                o1: None,
7287            };
7288            if !graph.attn_ok(&l, &p) || !graph.encode_attn_b(&l, &p) {
7289                return false;
7290            }
7291        }
7292        graph.sync();
7293        let mut kbuf = vec![0f32; b * nkv * hd];
7294        let mut vbuf = vec![0f32; b * nkv * hd];
7295        if !crate::gpu_metal::kv_mirror_read_rows(
7296            self.mtp_kv_id(),
7297            Self::MTP_LAYER_BASE,
7298            nkv,
7299            hd,
7300            cpu_stored,
7301            b,
7302            &mut kbuf,
7303            &mut vbuf,
7304        ) {
7305            return false;
7306        }
7307        for r in 0..b {
7308            m.kv.append(
7309                &kbuf[r * nkv * hd..(r + 1) * nkv * hd],
7310                &vbuf[r * nkv * hd..(r + 1) * nkv * hd],
7311                &[],
7312            );
7313        }
7314        crate::gpu_metal::kv_mirror_set_stored(
7315            self.mtp_kv_id(),
7316            Self::MTP_LAYER_BASE,
7317            cpu_stored + b,
7318        );
7319        true
7320    }
7321
7322    /// Draft-head shortlist size: `CMF_DRAFT_VOCAB` rows (default 65536,
7323    /// capped at the head; 0 = full head).
7324    fn draft_vocab_rows(head_rows: usize) -> usize {
7325        static N: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
7326        let n = *N.get_or_init(|| {
7327            std::env::var("CMF_DRAFT_VOCAB")
7328                .ok()
7329                .and_then(|v| v.parse().ok())
7330                .unwrap_or(65536)
7331        });
7332        if n == 0 { head_rows } else { n.min(head_rows) }
7333    }
7334
7335    /// One MTP block step on the native Metal token graph: block input on
7336    /// the host, the attention layer + FFN device-resident over the MTP
7337    /// mirror, the head folded in when `want_logits`. The appended K/V row
7338    /// is pulled into the CPU MTP cache (owner of record) after the sync.
7339    #[cfg(target_os = "macos")]
7340    fn mtp_step_metal(
7341        &mut self,
7342        m: &mut MtpModule,
7343        hidden: &[f32],
7344        next_token: u32,
7345        position: usize,
7346        want_logits: bool,
7347    ) -> Option<(Vec<f32>, Vec<f32>)> {
7348        use crate::gpu_metal::{AttnDeviceParams, AttnGpuLayer, GraphDims, MetalFfn, TokenGraph};
7349        if std::env::var("CMF_MTP_GRAPH").as_deref() == Ok("0")
7350            || !crate::gpu::q1_force()
7351            || !crate::gpu::enabled_here()
7352            || self.attn_softcap > 0.0
7353            || self.attention_heads_per_layer.is_some()
7354            || m.kv.mode != crate::kv_cache::KvMode::F32
7355            || m.kv.o1.is_some()
7356        {
7357            return None;
7358        }
7359        let AttnKind::Full {
7360            wq,
7361            wk,
7362            wv,
7363            wo,
7364            q_norm,
7365            k_norm,
7366            output_gate,
7367            softplus_gate: None,
7368            bias: None,
7369        } = &m.layer.attn
7370        else {
7371            return None;
7372        };
7373        let FfnKind::Dense(d) = &m.layer.ffn else {
7374            return None;
7375        };
7376        if d.act != Act::Silu || !d.segs.is_empty() {
7377            return None;
7378        }
7379        let (pq, pk, pv, po) = (
7380            wq.q1_parts()?,
7381            wk.q1_parts()?,
7382            wv.q1_parts()?,
7383            wo.q1_parts()?,
7384        );
7385        let (g, u, dn) = (
7386            d.gate_proj.q1_parts()?,
7387            d.up_proj.q1_parts()?,
7388            d.down_proj.q1_parts()?,
7389        );
7390        let QTensor::Mapped { model, .. } = wq else {
7391            return None;
7392        };
7393        let model = model.clone();
7394        let lm = if want_logits {
7395            Some(self.weights.lm_head.q1_parts()?)
7396        } else {
7397            None
7398        };
7399        let dims = GraphDims {
7400            hidden: self.hidden_size,
7401            eps: self.rms_eps as f32,
7402            gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
7403        };
7404        // The block input `eh_proj · [enorm(e); hnorm(h)]` rides in the
7405        // graph (one submit a step); the host per-op matvec if it cannot.
7406        let hs = self.hidden_size;
7407        let mut x = vec![0f32; hs];
7408        let mut graph = TokenGraph::new(&model, dims, &x)?;
7409        let mut folded = false;
7410        if let Some(eh) = m.eh_proj.q1_parts() {
7411            let e = self.embed_single(next_token);
7412            let mut cat = vec![0.0f32; 2 * hs];
7413            let (cat_e, cat_h) = cat.split_at_mut(hs);
7414            inference::rms_norm_into(&e, &m.enorm, self.rms_eps, self.norm_style, cat_e);
7415            inference::rms_norm_into(hidden, &m.hnorm, self.rms_eps, self.norm_style, cat_h);
7416            folded = graph.encode_input_proj(eh, &cat);
7417        }
7418        if !folded {
7419            x = self.mtp_block_input(m, hidden, next_token);
7420            graph = TokenGraph::new(&model, dims, &x)?;
7421        }
7422        let l = AttnGpuLayer {
7423            attn_norm: &m.layer.input_norm,
7424            post_norm: &m.layer.post_norm,
7425            wq: pq,
7426            wk: pk,
7427            wv: pv,
7428            wo: po,
7429            ffn: MetalFfn::Dense {
7430                gate: g,
7431                up: u,
7432                down: dn,
7433            },
7434        };
7435        let (nh, nkv, hd, rd) = (
7436            self.num_heads,
7437            self.num_kv_heads,
7438            self.head_dim,
7439            self.rotary_dim,
7440        );
7441        let inv_freq = self.inv_freq.clone();
7442        {
7443            let cache = &m.kv;
7444            let cpu_k: Vec<&[f32]> = (0..nkv).map(|g| cache.head_keys(g)).collect();
7445            let cpu_v: Vec<&[f32]> = (0..nkv).map(|g| cache.head_values(g)).collect();
7446            let cpu_stored = cpu_k[0].len() / hd;
7447            let p = AttnDeviceParams {
7448                kv_id: self.mtp_kv_id(),
7449                layer: Self::MTP_LAYER_BASE,
7450                nh,
7451                nkv,
7452                hd,
7453                rd,
7454                position,
7455                eps: self.rms_eps as f32,
7456                gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
7457                output_gate: *output_gate,
7458                q_norm: q_norm.as_deref(),
7459                k_norm: k_norm.as_deref(),
7460                inv_freq: &inv_freq,
7461                cpu_k,
7462                cpu_v,
7463                cpu_stored,
7464                o1: None,
7465            };
7466            if !graph.attn_device_ok(&l, &p) || !graph.encode_attn_device(&l, &p) {
7467                return None;
7468            }
7469        }
7470        // The draft's head over a vocabulary SHORTLIST (the first
7471        // CMF_DRAFT_VOCAB rows — BPE ids run roughly by merge rank, so the
7472        // low ids carry the mass): the verify keeps the full head, so a true
7473        // token past the cut is only a rejected draft, never a wrong token.
7474        // 662 MB a step on Qwen3.8 becomes 170 MB at 65536.
7475        let draft_rows = if let Some(lm) = lm {
7476            Self::draft_vocab_rows(lm.1)
7477        } else {
7478            0
7479        };
7480        if let Some(lm) = lm {
7481            if !graph.lm_head_ok(lm) {
7482                return None;
7483            }
7484            if draft_rows < lm.1 {
7485                if !graph.encode_lm_head_part(&m.final_norm, lm, draft_rows) {
7486                    return None;
7487                }
7488            } else {
7489                graph.encode_lm_head(&m.final_norm, lm);
7490            }
7491        }
7492        graph.sync();
7493        let mut logits = Vec::new();
7494        if let Some(lm) = lm {
7495            let n_read = draft_rows.min(lm.1).min(self.vocab_size);
7496            logits = attention::take_buf(n_read);
7497            graph.read_logits(&mut logits);
7498            // ids past the shortlist: never drafted (−∞ in every chain)
7499            logits.resize(self.vocab_size, f32::NEG_INFINITY);
7500        }
7501        graph.finish(&mut x);
7502        let mut krow = attention::take_buf(nkv * hd);
7503        let mut vrow = attention::take_buf(nkv * hd);
7504        if crate::gpu_metal::kv_mirror_read_last(
7505            self.mtp_kv_id(),
7506            Self::MTP_LAYER_BASE,
7507            nkv,
7508            hd,
7509            &mut krow,
7510            &mut vrow,
7511        ) {
7512            m.kv.append(&krow, &vrow, &[]);
7513        }
7514        attention::recycle_buf(&mut krow);
7515        attention::recycle_buf(&mut vrow);
7516        Some((logits, x))
7517    }
7518
7519    fn try_batch_graph_wgpu(
7520        &self,
7521        hiddens: &mut [f32],
7522        positions: &[usize],
7523        k: usize,
7524        spec: Option<crate::gpu::SpecTail<'_>>,
7525    ) -> bool {
7526        let _tb = std::time::Instant::now();
7527        if self.attn_softcap > 0.0 {
7528            return false; // capped scores: no graph kernel — CPU path
7529        }
7530        if self.o1_active() {
7531            return false;
7532        }
7533        let nh = self.num_heads;
7534        let (nkv, hd, rd) = self.layer_geom(0);
7535        let gemma = self.norm_style == cortiq_core::NormStyle::Gemma;
7536        fn gw(t: &QTensor) -> Option<crate::gpu::GraphW<'_>> {
7537            if let Some((_, i, kind, rs)) = t.graph_weight() {
7538                return Some(crate::gpu::GraphW {
7539                    idx: i,
7540                    kind,
7541                    row_scale: rs,
7542                    data: &[],
7543                });
7544            }
7545            t.as_f32().map(|d| crate::gpu::GraphW {
7546                idx: 0,
7547                kind: 4,
7548                row_scale: &[],
7549                data: d,
7550            })
7551        }
7552        let built: Option<(
7553            Vec<crate::gpu::GraphLayer<'_>>,
7554            std::sync::Arc<cortiq_core::CmfModel>,
7555        )> = (|| {
7556            let mut layers = Vec::with_capacity(self.num_layers);
7557            let mut model = None;
7558            for li in 0..self.num_layers {
7559                let lw = &self.weights.layers[self.phys_layer(li)];
7560                // MoE routes per token, so its experts are encoded token by
7561                // token inside the batched submit while attention and the
7562                // projections stay GEMMs. Refusing MoE here is what left
7563                // prefill running one position at a time: 33 tok/s against
7564                // 54 on decode, i.e. reading the prompt was slower than
7565                // writing the answer.
7566                let gffn = match &lw.ffn {
7567                    FfnKind::Dense(d) if !d.segs.is_empty() => return None,
7568                    FfnKind::Dense(d) => crate::gpu::GraphFfn::Dense {
7569                        gate: gw(&d.gate_proj)?,
7570                        up: gw(&d.up_proj)?,
7571                        down: gw(&d.down_proj)?,
7572                    },
7573                    FfnKind::Moe(m) => {
7574                        if m.router_sigmoid
7575                            || m.expert_bias.is_some()
7576                            || m.route_tau.is_some()
7577                            || m.mask.is_some()
7578                        {
7579                            return None;
7580                        }
7581                        let (se, sg) = m.shared.as_ref()?;
7582                        let sgate = gw(sg.as_ref()?)?;
7583                        let router = gw(&m.router)?;
7584                        let inter = m.experts.first()?.gate_proj.rows();
7585                        let mut experts = Vec::with_capacity(m.experts.len() + 1);
7586                        let mut q4tp: Option<bool> = None;
7587                        let mut gu_q2: Option<bool> = None;
7588                        for e in m.experts.iter().chain(std::iter::once(se)) {
7589                            if !matches!(e.act, Act::Silu)
7590                                || e.gate_proj.rows() != inter
7591                                || e.up_proj.rows() != inter
7592                            {
7593                                return None;
7594                            }
7595                            // Same ladder as the token graph: q4t → q2tp
7596                            // (mixed profile: 2-bit gate/up over a q4tp
7597                            // down) → q4tp. Uniform across the layer.
7598                            let (mm, gi, ui, di, is_p, is_q2) = match e.gate_proj.mapped_q4t() {
7599                                Some((mm, gi)) => (
7600                                    mm,
7601                                    gi,
7602                                    e.up_proj.mapped_q4t()?.1,
7603                                    e.down_proj.mapped_q4t()?.1,
7604                                    false,
7605                                    false,
7606                                ),
7607                                None => match e.gate_proj.mapped_q2tp() {
7608                                    Some((mm, gi)) => (
7609                                        mm,
7610                                        gi,
7611                                        e.up_proj.mapped_q2tp()?.1,
7612                                        e.down_proj.mapped_q4tp()?.1,
7613                                        true,
7614                                        true,
7615                                    ),
7616                                    None => {
7617                                        let (mm, gi) = e.gate_proj.mapped_q4tp()?;
7618                                        (
7619                                            mm,
7620                                            gi,
7621                                            e.up_proj.mapped_q4tp()?.1,
7622                                            e.down_proj.mapped_q4tp()?.1,
7623                                            true,
7624                                            false,
7625                                        )
7626                                    }
7627                                },
7628                            };
7629                            if *q4tp.get_or_insert(is_p) != is_p
7630                                || *gu_q2.get_or_insert(is_q2) != is_q2
7631                            {
7632                                return None;
7633                            }
7634                            model.get_or_insert_with(|| mm.clone());
7635                            experts.push((gi, ui, di));
7636                        }
7637                        crate::gpu::GraphFfn::Moe {
7638                            router,
7639                            shared_gate: sgate,
7640                            experts,
7641                            n_exp: m.experts.len(),
7642                            top_k: m.top_k,
7643                            inter,
7644                            norm_topk: m.norm_topk_prob,
7645                            q4tp: q4tp?,
7646                            gu_q2: gu_q2.unwrap_or(false),
7647                            sigmoid: false,
7648                            bias: None,
7649                            has_shared: true,
7650                        }
7651                    }
7652                    _ => return None,
7653                };
7654                let attn = match &lw.attn {
7655                    AttnKind::Full {
7656                        wq,
7657                        wk,
7658                        wv,
7659                        wo,
7660                        q_norm,
7661                        k_norm,
7662                        output_gate,
7663                        softplus_gate,
7664                        bias,
7665                    } => {
7666                        if softplus_gate.is_some() || self.attention_heads_per_layer.is_some() {
7667                            return None;
7668                        }
7669                        let (m, _, _, _) = wq.graph_weight()?;
7670                        model = Some(m.clone());
7671                        crate::gpu::GraphAttn::Full {
7672                            wq: gw(wq)?,
7673                            wk: gw(wk)?,
7674                            wv: gw(wv)?,
7675                            wo: gw(wo)?,
7676                            q_norm: q_norm.as_deref(),
7677                            k_norm: k_norm.as_deref(),
7678                            bias: bias
7679                                .as_ref()
7680                                .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
7681                            output_gate: *output_gate,
7682                            cpu_k: self.kv_cache.layers[li].k_heads(),
7683                            cpu_v: self.kv_cache.layers[li].v_heads(),
7684                        }
7685                    }
7686                    AttnKind::LinearGdn(w) => {
7687                        let cfg = self.gdn_cfg?;
7688                        let (m, _, _, _) = w.in_proj_qkv.graph_weight()?;
7689                        model = Some(m.clone());
7690                        crate::gpu::GraphAttn::Gdn {
7691                            qkv: gw(&w.in_proj_qkv)?,
7692                            z: gw(&w.in_proj_z)?,
7693                            a: gw(&w.in_proj_a)?,
7694                            b: gw(&w.in_proj_b)?,
7695                            out: gw(&w.out_proj)?,
7696                            conv1d: &w.conv1d,
7697                            a_log: &w.a_log,
7698                            dt_bias: &w.dt_bias,
7699                            norm: &w.norm,
7700                            nv: cfg.num_v_heads,
7701                            nk: cfg.num_k_heads,
7702                            dk: cfg.key_head_dim,
7703                            dv: cfg.value_head_dim,
7704                            kk: cfg.conv_kernel,
7705                            cpu_state: &self.kv_cache.layers[self.phys_layer(li)].linear_state,
7706                        }
7707                    }
7708                    _ => return None,
7709                };
7710                layers.push(crate::gpu::GraphLayer {
7711                    input_norm: &lw.input_norm,
7712                    attn,
7713                    post_norm: &lw.post_norm,
7714                    ffn: gffn,
7715                });
7716            }
7717            Some((layers, model?))
7718        })();
7719        let Some((layers, model)) = built else {
7720            {
7721                use std::sync::atomic::{AtomicBool, Ordering};
7722                static SAID: AtomicBool = AtomicBool::new(false);
7723                if !SAID.swap(true, Ordering::Relaxed) {
7724                    tracing::warn!("batch graph: BUILDER refused (layer weights/kinds)");
7725                }
7726            }
7727            return false;
7728        };
7729        if std::env::var("CMF_GRAPH_SPEC_TIME").is_ok() {
7730            eprintln!("batch-build: {:.1} ms", _tb.elapsed().as_secs_f64() * 1e3);
7731        }
7732        crate::gpu::forward_batch_graph(
7733            &model,
7734            self.graph_kv_id,
7735            &layers,
7736            &self.inv_freq,
7737            hiddens,
7738            nh,
7739            nkv,
7740            hd,
7741            rd,
7742            self.hidden_size,
7743            self.intermediate_size,
7744            positions,
7745            self.kv_cache.max_seq_len,
7746            gemma,
7747            self.rms_eps as f32,
7748            k,
7749            spec,
7750        )
7751    }
7752
7753    /// Same, stopping after layer `upto` inclusive (routing probe φ).
7754    /// `CMF_DSV4_DRAFT_PROBE=1` — grade the draft against what the trunk goes on
7755    /// to produce. Off by default; it runs a whole draft per decoded token.
7756    fn draft_probe() -> bool {
7757        static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
7758        *ON.get_or_init(|| std::env::var("CMF_DSV4_DRAFT_PROBE").is_ok_and(|v| v != "0"))
7759    }
7760
7761    /// `CMF_DSV4_DRAFT_PROBE=1`: measure how much of the draft the trunk
7762    /// would have agreed with, WITHOUT verifying or rolling anything back.
7763    ///
7764    /// The number this produces decides the whole speculation design — at
7765    /// acceptance a, a block of B positions yields 1 + a + a² + ... tokens
7766    /// per trunk pass — so it is worth measuring before any of the machinery
7767    /// that would exploit it exists. Each draft is parked with the position
7768    /// it was made at, and graded as the real tokens arrive.
7769    /// `CMF_DSV4_SPEC=1` — the DeepSeek-V4 speculative decode: draft five
7770    /// on the card, verify them in one batched trunk pass, commit the
7771    /// accepted prefix, roll the rest back.
7772    #[cfg(feature = "gpu")]
7773    fn dsv4_spec_on() -> bool {
7774        static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
7775        *ON.get_or_init(|| {
7776            // Test-only runtime gate: model loading still performs the same
7777            // reservation and trunk packing, which gives rollback parity a
7778            // topology-identical non-speculative control arm.
7779            if let Ok(v) = std::env::var("CMF_DSV4_SPEC_RUN") {
7780                return v != "0";
7781            }
7782            // An explicit value is a diagnostic force/escape hatch.  With no
7783            // knob, speculation is eligible only when model loading reserved
7784            // its bounded pack.  On small q4tp cards the geometric reserve
7785            // gate deliberately leaves this at zero: trying to build DSpark
7786            // after the exact trunk filled VRAM is both slower and a device
7787            // OOM (measured on A40).
7788            std::env::var("CMF_DSV4_SPEC")
7789                .map(|v| v != "0")
7790                .unwrap_or_else(|_| {
7791                    crate::gpu_wgpu::DRAFT_RESERVE.load(std::sync::atomic::Ordering::Relaxed) > 0
7792                })
7793        })
7794    }
7795
7796    /// One speculative round at the decode tip. `t_next` is the token the
7797    /// sampler just committed for `next_pos`. Returns the EXTRA accepted
7798    /// tokens (possibly none) and the new position, with `graph_logits`
7799    /// left holding the last accepted position's logits — exactly what the
7800    /// loop top expects. `None` means "speculate not this round": nothing
7801    /// was committed, the caller forwards normally.
7802    #[cfg(feature = "gpu")]
7803    fn dsv4_spec_step(
7804        &mut self,
7805        tip_token: u32,
7806        t_next: u32,
7807        next_pos: usize,
7808        max_extra: usize,
7809        drafted: &mut usize,
7810        accepted_ctr: &mut usize,
7811    ) -> Option<(Vec<u32>, usize)> {
7812        let t_all = std::time::Instant::now();
7813        if std::env::var("CMF_DSV4_SPEC_TIME").is_ok() {
7814            thread_local! {
7815                static LAST: std::cell::Cell<Option<std::time::Instant>> =
7816                    const { std::cell::Cell::new(None) };
7817            }
7818            LAST.with(|l| {
7819                if let Some(prev) = l.get() {
7820                    eprintln!(
7821                        "между раундами {:.1} мс",
7822                        prev.elapsed().as_secs_f64() * 1e3
7823                    );
7824                }
7825                l.set(Some(std::time::Instant::now()));
7826            });
7827        }
7828        if std::env::var("CMF_DSV4_SPEC_DEBUG").is_ok() {
7829            eprintln!("spec_step: вход pos={next_pos}");
7830        }
7831        let n_layers = self.dsv4.as_ref().map(|b| b.1.len())?;
7832        let cfg = self.dsv4.as_ref().map(|b| b.2)?;
7833        // The draft state and its capture, armed exactly as the probe does.
7834        if self.dspark.is_none() {
7835            let t = crate::dsv4::dspark_targets(&self.dsv4_mtp, &cfg, n_layers);
7836            if t.is_empty() {
7837                return None;
7838            }
7839            crate::dsv4::dspark_arm(&t, cfg.dim);
7840            self.dspark = Some(crate::dsv4::DsparkState::new(
7841                self.dsv4_mtp.len(),
7842                &cfg,
7843                t.len(),
7844            ));
7845        }
7846        let targets = crate::dsv4::dspark_targets(&self.dsv4_mtp, &cfg, n_layers);
7847        let pack = crate::dsv4::dspark_pack_get(&self.dsv4_mtp, &cfg);
7848        if pack.is_none() && std::env::var("CMF_DSV4_SPEC_DEBUG").is_ok() {
7849            eprintln!("spec_step: пак не построился (targets {targets:?})");
7850        }
7851        let pack = pack?;
7852        let block = crate::dsv4::dspark_block();
7853        let b_box = self.dsv4.as_mut()?;
7854        let (g, layers, st) = (&b_box.0, &b_box.1, &mut b_box.3);
7855        let ds = self.dspark.as_mut()?;
7856        // The tip's captures: either this token ran on a normal path that
7857        // filled the thread-local, or the previous spec round left them.
7858        let dbg = std::env::var("CMF_DSV4_SPEC_DEBUG").is_ok();
7859        if !crate::dsv4::dspark_take(&mut ds.main_hidden) && !ds.have_hidden {
7860            if dbg {
7861                eprintln!("spec_step: нет захвата");
7862            }
7863            return None;
7864        }
7865        ds.have_hidden = true;
7866        let tip_pos = next_pos.checked_sub(1)?;
7867        let draft_started = std::time::Instant::now();
7868        let mut conf = Vec::new();
7869        let props = crate::dsv4::dspark_draft_gpu(
7870            g,
7871            &self.dsv4_mtp,
7872            &cfg,
7873            ds,
7874            pack,
7875            st.kv_id,
7876            tip_token,
7877            tip_pos,
7878            self.pool.as_deref(),
7879            &mut conf,
7880        );
7881        self.dspark_draft_ns += draft_started.elapsed().as_nanos();
7882        *drafted += block;
7883        if props.is_empty() || props[0] != t_next {
7884            if dbg {
7885                eprintln!(
7886                    "spec_step: черновик {} (props0={:?} t_next={t_next})",
7887                    if props.is_empty() {
7888                        "пуст"
7889                    } else {
7890                        "мимо"
7891                    },
7892                    props.first()
7893                );
7894            }
7895            return None;
7896        }
7897        // `fed[0]` is `t_next`, which the outer loop has already committed;
7898        // only `fed[1..]` become additional output tokens. Cap the verify
7899        // transaction itself to the caller's remaining output budget instead
7900        // of merely truncating the returned vector: otherwise the KV/state
7901        // would advance past `max_tokens` and a 64-token request could return
7902        // 66 tokens (and poison a reused session with two invisible steps).
7903        let mut k_verify = crate::dsv4::dspark_verify_k()
7904            .min(props.len())
7905            .min(max_extra.saturating_add(1));
7906        // Adaptive depth: positions the draft itself doubts are paid for on
7907        // every verify and delivered almost never (natural-text survival
7908        // [.67 .50 .29 .08 .04]). `CMF_DSPARK_CONF_MIN=p` trims the fed
7909        // prefix at the first proposal whose confidence drops below p; on
7910        // predictable text the confidences stay high and nothing changes.
7911        let conf_min = {
7912            static M: std::sync::OnceLock<f32> = std::sync::OnceLock::new();
7913            *M.get_or_init(|| {
7914                std::env::var("CMF_DSPARK_CONF_MIN")
7915                    .ok()
7916                    .and_then(|v| v.parse().ok())
7917                    .unwrap_or(0.0)
7918            })
7919        };
7920        if conf_min > 0.0 && conf.len() >= props.len() {
7921            let mut keep = 1usize;
7922            while keep < k_verify && conf.get(keep).copied().unwrap_or(0.0) >= conf_min {
7923                keep += 1;
7924            }
7925            k_verify = k_verify.min(keep.max(2));
7926        }
7927        if k_verify < 2 {
7928            return None;
7929        }
7930        let mut fed = Vec::with_capacity(k_verify);
7931        fed.push(t_next);
7932        fed.extend_from_slice(&props[1..k_verify]);
7933        let mut argmax = Vec::new();
7934        let mut logits_all = Vec::new();
7935        let mut walked = Vec::new();
7936        let txn = crate::dsv4::dsv4_verify_chunk(
7937            g,
7938            layers,
7939            &cfg,
7940            st,
7941            &fed,
7942            next_pos,
7943            &self.inv_freq,
7944            self.pool.as_deref(),
7945            &targets,
7946            &mut argmax,
7947            &mut logits_all,
7948            &mut walked,
7949        );
7950        if txn.is_none() && dbg {
7951            eprintln!("spec_step: verify отказал");
7952        }
7953        let txn = txn?;
7954        let spec_gpu_end = txn.gpu_end;
7955        let b = fed.len();
7956        let mut accepted = 1usize;
7957        while accepted < b && fed[accepted] == argmax[accepted - 1] {
7958            accepted += 1;
7959        }
7960        // `CMF_DSV4_SPEC_FORCE_REJECT=1` — accept nothing beyond the known
7961        // token, every round: the pure rollback exerciser. The output must
7962        // stay byte-identical to the plain walk; anything else is a
7963        // transaction bug, isolated from the acceptance logic.
7964        if std::env::var("CMF_DSV4_SPEC_FORCE_REJECT").is_ok_and(|v| v != "0") {
7965            accepted = 1;
7966        }
7967        if std::env::var("CMF_DSV4_SPEC_TRACE").is_ok() {
7968            eprintln!("spec@{next_pos}: fed={fed:?} argmax={argmax:?} accepted={accepted}");
7969        }
7970        let t_fin = std::time::Instant::now();
7971        if !crate::dsv4::dsv4_spec_finish(
7972            g,
7973            layers,
7974            &cfg,
7975            st,
7976            txn,
7977            accepted,
7978            &fed,
7979            &self.inv_freq,
7980            self.pool.as_deref(),
7981        ) {
7982            tracing::warn!("dsv4: спекулятивный откат не удался — состояние подозрительно");
7983            return None;
7984        }
7985        if std::env::var("CMF_DSV4_SPEC_TIME").is_ok() {
7986            eprintln!(
7987                "finish(k={accepted}): {:.1} мс",
7988                t_fin.elapsed().as_secs_f64() * 1e3
7989            );
7990        }
7991        *accepted_ctr += accepted - 1;
7992        // Captures per accepted token: device targets photographed by the
7993        // batch, host targets from the verify's own walk. The last one
7994        // becomes the new tip's draft input; every one owes the ring an
7995        // entry for its position.
7996        let (hc, dim) = (cfg.hc_mult, cfg.dim);
7997        // Complete-chain layers are photographed by the fused submission;
7998        // partial device layers overwrite that slot after exact host cold-
7999        // expert correction.  Thus every target in the contiguous device
8000        // prefix has a valid per-token capture.
8001        let dev_caps: Vec<usize> = targets
8002            .iter()
8003            .copied()
8004            .filter(|&t| t < spec_gpu_end)
8005            .collect();
8006        let mut caps_all = vec![0.0f32; dev_caps.len() * b * hc * dim];
8007        if !crate::gpu_wgpu::dsv4_spec_cap_read_all(b, dev_caps.len(), hc * dim, &mut caps_all) {
8008            return None;
8009        }
8010        for t in 0..accepted {
8011            let tip = t + 1 == accepted;
8012            for (slot, &tl) in targets.iter().enumerate() {
8013                if let Some(di) = dev_caps.iter().position(|&d| d == tl) {
8014                    let lo = (di * b + t) * hc * dim;
8015                    crate::dsv4::dspark_capture(
8016                        &caps_all[lo..lo + hc * dim],
8017                        &cfg,
8018                        slot,
8019                        &mut ds.main_hidden,
8020                    );
8021                } else if tip
8022                    && crate::dsv4::dspark_peek_slot(slot, dim, {
8023                        let lo = slot * dim;
8024                        &mut ds.main_hidden[lo..lo + dim]
8025                    })
8026                {
8027                    // The tip's host-layer captures are the walk's own
8028                    // per-layer notes — exact. (The walk that ran last ended
8029                    // on exactly this token, on both the accept-all and the
8030                    // rollback path.)
8031                } else {
8032                    // Intermediate tokens: the post-tail state stands in for
8033                    // the per-layer capture on host targets below the last
8034                    // layer. Ring-entry quality only; the tip is exact.
8035                    crate::dsv4::dspark_capture(
8036                        &walked[t * hc * dim..(t + 1) * hc * dim],
8037                        &cfg,
8038                        slot,
8039                        &mut ds.main_hidden,
8040                    );
8041                }
8042            }
8043            crate::dsv4::dspark_ring_append(
8044                g,
8045                &self.dsv4_mtp,
8046                &cfg,
8047                ds,
8048                next_pos + t,
8049                self.pool.as_deref(),
8050            );
8051        }
8052        let row = logits_all[(accepted - 1) * cfg.vocab..accepted * cfg.vocab].to_vec();
8053        self.graph_logits = Some(row);
8054        // The speculative loop never runs the probe, so the trunk tally has
8055        // no other place to cycle. Armed only when someone asked for the
8056        // dump; the host tail is the only tallying path here, which is
8057        // precisely the population a partial pack would serve.
8058        if std::env::var("CMF_DSV4_TRUNK_PICK_DUMP").is_ok() {
8059            crate::dsv4::trunk_freq_note(&crate::dsv4::pick_tally_take());
8060            crate::dsv4::pick_tally_arm();
8061        }
8062        if std::env::var("CMF_DSV4_SPEC_TIME").is_ok() {
8063            eprintln!(
8064                "spec_step total {:.1} мс (k={accepted})",
8065                t_all.elapsed().as_secs_f64() * 1e3
8066            );
8067        }
8068        Some((fed[1..accepted].to_vec(), next_pos + accepted))
8069    }
8070
8071    fn dspark_probe(&mut self, position: usize, token_id: u32) {
8072        if self.dsv4_mtp.is_empty() || !Self::draft_probe() {
8073            return;
8074        }
8075        // What the trunk just routed to, for this token.
8076        let trunk_now = crate::dsv4::pick_tally_take();
8077        crate::dsv4::trunk_freq_note(&trunk_now);
8078        if !trunk_now.is_empty() {
8079            self.dspark_trunk_picks.push(trunk_now);
8080            let keep = crate::dsv4::dspark_block();
8081            if self.dspark_trunk_picks.len() > keep {
8082                self.dspark_trunk_picks.remove(0);
8083            }
8084        }
8085        // Grade whatever is waiting: the token just decoded sits at
8086        // `position`, so it answers the draft made at `position - 1 - i`.
8087        for p in std::mem::take(&mut self.dspark_pending) {
8088            let Some(i) = position.checked_sub(p.0 + 1) else {
8089                continue;
8090            };
8091            let mut p = p;
8092            if i < p.1.len() {
8093                if p.2 && p.1[i] == token_id {
8094                    p.3 = i + 1;
8095                } else {
8096                    p.2 = false;
8097                }
8098                if i + 1 < p.1.len() {
8099                    self.dspark_pending.push(p);
8100                    continue;
8101                }
8102            }
8103            self.dspark_hist.push(p.3);
8104            self.dspark_real.push(token_id);
8105        }
8106        let Some(b) = &mut self.dsv4 else { return };
8107        let (g, layers, cfg) = (&b.0, &b.1, b.2);
8108        let n_layers = layers.len();
8109        if self.dspark.is_none() {
8110            let t = crate::dsv4::dspark_targets(&self.dsv4_mtp, &cfg, n_layers);
8111            if t.is_empty() {
8112                return;
8113            }
8114            eprintln!(
8115                "DSpark: захват со слоёв {t:?}, блок {}",
8116                crate::dsv4::dspark_block()
8117            );
8118            crate::dsv4::dspark_arm(&t, cfg.dim);
8119            self.dspark = Some(crate::dsv4::DsparkState::new(
8120                self.dsv4_mtp.len(),
8121                &cfg,
8122                t.len(),
8123            ));
8124        }
8125        let ds = self.dspark.as_mut().unwrap();
8126        if !crate::dsv4::dspark_take(&mut ds.main_hidden) {
8127            return; // this token ran on a path that captures nothing
8128        }
8129        let mut conf = Vec::new();
8130        crate::dsv4::pick_tally_arm();
8131        // The trunk has already consumed the adaptive VRAM budget. Until the
8132        // draft owns an explicit bounded device pack, its tensors are an
8133        // out-of-core CPU/disk tier by contract: never let per-op probes try
8134        // to squeeze another multi-gigabyte MTP expert cache onto the card.
8135        let draft_started = std::time::Instant::now();
8136        #[cfg(feature = "gpu")]
8137        let gpu_draft = crate::dsv4::dspark_gpu_on();
8138        #[cfg(not(feature = "gpu"))]
8139        let gpu_draft = false;
8140        let props = if gpu_draft {
8141            #[cfg(feature = "gpu")]
8142            {
8143                let kv_id = b.3.kv_id;
8144                match crate::dsv4::dspark_pack_get(&self.dsv4_mtp, &cfg) {
8145                    Some(pk) => crate::dsv4::dspark_draft_gpu(
8146                        g,
8147                        &self.dsv4_mtp,
8148                        &cfg,
8149                        ds,
8150                        pk,
8151                        kv_id,
8152                        token_id,
8153                        position,
8154                        self.pool.as_deref(),
8155                        &mut conf,
8156                    ),
8157                    None => Vec::new(),
8158                }
8159            }
8160            #[cfg(not(feature = "gpu"))]
8161            Vec::new()
8162        } else {
8163            crate::gpu::cpu_scope(|| {
8164                crate::dsv4::dspark_draft(
8165                    g,
8166                    &self.dsv4_mtp,
8167                    &cfg,
8168                    ds,
8169                    token_id,
8170                    position,
8171                    self.pool.as_deref(),
8172                    &mut conf,
8173                )
8174            })
8175        };
8176        self.dspark_draft_ns += draft_started.elapsed().as_nanos();
8177        let draft_picks = crate::dsv4::pick_tally_take();
8178        crate::dsv4::dspark_freq_note(&draft_picks);
8179        // Re-arm for the NEXT trunk token; the probe runs after the forward,
8180        // so this is the only place that can.
8181        crate::dsv4::pick_tally_arm();
8182        if !props.is_empty() {
8183            // Two ratios, side by side: what a batched verify over the trunk
8184            // would read against what it asks for, and the same for the
8185            // draft's three stages. Near 1.0 means a batch amortises nothing.
8186            let (tu, tt) = {
8187                let flat: Vec<(usize, Vec<usize>)> = self
8188                    .dspark_trunk_picks
8189                    .iter()
8190                    .flat_map(|v| v.iter().cloned())
8191                    .collect();
8192                // Per layer, across the window of tokens.
8193                let mut per: std::collections::HashMap<usize, Vec<usize>> =
8194                    std::collections::HashMap::new();
8195                for (li, picks) in flat {
8196                    per.entry(li).or_default().extend(picks);
8197                }
8198                let n = per.len().max(1);
8199                let mut u = 0usize;
8200                let mut t = 0usize;
8201                for (_, v) in per {
8202                    t += v.len();
8203                    u += v.iter().collect::<std::collections::HashSet<_>>().len();
8204                }
8205                (u / n, t / n)
8206            };
8207            let (du, dt) = crate::dsv4::tally_unique(&draft_picks);
8208            self.dspark_exp.push((tu, tt, du, dt));
8209            self.dspark_pending.push((position, props, true, 0));
8210        }
8211        if self.dspark_hist.len() >= 8 && self.dspark_hist.len() % 8 == 0 {
8212            let n = self.dspark_hist.len() as f32;
8213            let mean: f32 = self.dspark_hist.iter().sum::<usize>() as f32 / n;
8214            let block = crate::dsv4::dspark_block();
8215            let mut at = vec![0usize; block + 1];
8216            for &k in &self.dspark_hist {
8217                at[k] += 1;
8218            }
8219            // Prefix survival: S_i = P(the first i positions all held).
8220            let mut surv = Vec::with_capacity(block);
8221            for i in 1..=block {
8222                let k = at[i..].iter().sum::<usize>() as f32 / n;
8223                surv.push(format!("{k:.2}"));
8224            }
8225            let distinct = self
8226                .dspark_real
8227                .iter()
8228                .collect::<std::collections::HashSet<_>>()
8229                .len();
8230            let (tu, tt, du, dt) = self.dspark_exp.iter().fold((0, 0, 0, 0), |a, b| {
8231                (a.0 + b.0, a.1 + b.1, a.2 + b.2, a.3 + b.3)
8232            });
8233            let m = self.dspark_exp.len().max(1);
8234            eprintln!(
8235                "DSpark: черновиков {}, принято в среднем {mean:.2} из {block} \
8236                 (токенов за проход {:.2}), распределение {at:?}, выживание [{}]",
8237                self.dspark_hist.len(),
8238                mean + 1.0,
8239                surv.join(" ")
8240            );
8241            eprintln!(
8242                "DSpark: разных токенов {distinct} из {} (вырожденность), \
8243                 эксперты ствол {}/{} на слой за {block} токенов, \
8244                 черновик {}/{} за блок, draft {:.2} мс/блок",
8245                self.dspark_real.len(),
8246                tu / m,
8247                tt / m,
8248                du / m,
8249                dt / m,
8250                self.dspark_draft_ns as f64 / self.dspark_exp.len().max(1) as f64 / 1e6
8251            );
8252        }
8253    }
8254
8255    fn forward_layers_upto(
8256        &mut self,
8257        hidden: &[f32],
8258        position: usize,
8259        task_mask: Option<&TaskMask>,
8260        upto: Option<usize>,
8261    ) -> Vec<f32> {
8262        // In-process multi-GPU: each segment runs pinned to its card,
8263        // and the only thing crossing the boundary is one hidden vector
8264        // that never leaves this address space. Same layer split the
8265        // network mode does, minus the second process, the socket, the
8266        // serialization and the dir_hash handshake.
8267        if let Some(plan) = self.gpu_plan.clone() {
8268            if upto.is_none() && plan.len() > 1 {
8269                let mut h = hidden.to_vec();
8270                for &(dev, from, upto_incl) in plan.iter() {
8271                    h = crate::gpu::with_device(dev, || {
8272                        self.forward_layers_span(&h, position, task_mask, from, Some(upto_incl))
8273                    });
8274                }
8275                return h;
8276            }
8277        }
8278        self.forward_layers_span(hidden, position, task_mask, 0, upto)
8279    }
8280
8281    /// Split this pipeline's layer stack across local GPUs: segment i
8282    /// runs on `devices[i]`. Contiguous and even by layer count — the
8283    /// VRAM-weighted planner is the next step, and an uneven card pair
8284    /// is why it will be needed. `None` clears the plan.
8285    pub fn set_gpu_plan(&mut self, devices: Option<&[usize]>) -> Result<(), String> {
8286        self.set_gpu_plan_at(devices, None)
8287    }
8288
8289    /// The same, with an explicit first boundary (`--peer-split`): card
8290    /// 0 takes layers `[0..at)`, the rest split what remains. Uneven
8291    /// cards, or an attention-heavy head, are why this knob exists.
8292    pub fn set_gpu_plan_at(
8293        &mut self,
8294        devices: Option<&[usize]>,
8295        at: Option<usize>,
8296    ) -> Result<(), String> {
8297        let Some(devs) = devices.filter(|d| d.len() > 1) else {
8298            self.gpu_plan = None;
8299            return Ok(());
8300        };
8301        self.split_supported()?;
8302        let n = self.num_layers;
8303        if devs.len() > n {
8304            return Err(format!("{} devices for {n} layers", devs.len()));
8305        }
8306        if let Some(k) = at {
8307            if k == 0 || k >= n {
8308                return Err(format!("split at {k}: the model has {n} layers"));
8309            }
8310            if devs.len() == 2 {
8311                self.gpu_plan = Some(std::sync::Arc::new(vec![
8312                    (devs[0], 0, k - 1),
8313                    (devs[1], k, n - 1),
8314                ]));
8315                return Ok(());
8316            }
8317            return Err(format!(
8318                "an explicit split point takes exactly 2 devices, got {}",
8319                devs.len()
8320            ));
8321        }
8322        let per = n.div_ceil(devs.len());
8323        let mut plan = Vec::with_capacity(devs.len());
8324        let mut from = 0usize;
8325        for &d in devs {
8326            if from >= n {
8327                break;
8328            }
8329            let upto = (from + per - 1).min(n - 1);
8330            plan.push((d, from, upto));
8331            from = upto + 1;
8332        }
8333        self.gpu_plan = Some(std::sync::Arc::new(plan));
8334        Ok(())
8335    }
8336
8337    /// The active in-process split, if any: (device, first layer, last).
8338    pub fn gpu_plan(&self) -> Option<Vec<(usize, usize, usize)>> {
8339        self.gpu_plan.as_ref().map(|p| p.as_ref().clone())
8340    }
8341
8342    /// Layer span [from ..= upto] (upto None = last layer): the building
8343    /// block the network pipeline-split rides on. `from > 0` skips the
8344    /// arch escape hatches (the pub `forward_span` refuses those archs
8345    /// first) and the whole-token graph — the plain per-layer loop is
8346    /// the canonical executor for a partial stack.
8347    fn forward_layers_span(
8348        &mut self,
8349        hidden: &[f32],
8350        position: usize,
8351        task_mask: Option<&TaskMask>,
8352        from: usize,
8353        upto: Option<usize>,
8354    ) -> Vec<f32> {
8355        debug_assert!(
8356            from == 0 || (self.dsv4.is_none() && self.qwen4_exp.is_none() && self.g3n.is_none())
8357        );
8358        if let Some(b) = &mut self.qwen4_exp {
8359            let _ = (task_mask, upto);
8360            let token_id = hidden.first().copied().unwrap_or(0.0) as u32;
8361            let mut logits = Vec::new();
8362            crate::qwen4_exp::forward_token(
8363                &b.0,
8364                &b.1,
8365                &b.2,
8366                &mut b.3,
8367                token_id,
8368                position,
8369                &self.inv_freq,
8370                self.pool.as_deref(),
8371                &mut logits,
8372                true,
8373            );
8374            self.graph_logits = Some(logits);
8375            return vec![0.0; self.hidden_size];
8376        }
8377        // DeepSeek-V4 runs its own stack: the state is hc_mult copies, and
8378        // the forward returns LOGITS, not a hidden — the head is inside it
8379        // (the final fold sits between the last layer and the norm). The
8380        // token id rides in `hidden[0]`, written by embed_single, because
8381        // the hash layers route by id rather than by content.
8382        if let Some(b) = &mut self.dsv4 {
8383            let _ = (task_mask, upto);
8384            let token_id = hidden.first().copied().unwrap_or(0.0) as u32;
8385            let (g, layers, cfg, st) = (&b.0, &b.1, b.2, &mut b.3);
8386            st.pos = position;
8387            let mut logits = Vec::new();
8388            crate::dsv4::forward_token(
8389                g,
8390                layers,
8391                &cfg,
8392                st,
8393                token_id,
8394                &self.inv_freq,
8395                self.pool.as_deref(),
8396                &mut logits,
8397            );
8398            self.graph_logits = Some(logits);
8399            self.dspark_probe(position, token_id);
8400            // The caller expects a hidden; the logits went out of band, as
8401            // with the fused lm_head path.
8402            return vec![0.0; self.hidden_size];
8403        }
8404        // Gemma-3n runs its own stack (4 AltUp replicas don't fit this
8405        // loop); `hidden` is the extended embedding from embed_single.
8406        if let Some(b) = &self.g3n {
8407            let _ = (task_mask, upto);
8408            return crate::g3n::g3n_forward(
8409                &b.0,
8410                &b.1,
8411                hidden,
8412                position,
8413                &mut self.kv_cache.layers,
8414                self.num_heads,
8415                self.num_kv_heads,
8416                self.head_dim,
8417                self.pool.as_deref(),
8418            );
8419        }
8420        let mut h = hidden.to_vec();
8421        // Split borrows: copy scalars / clone handles so the per-layer
8422        // cfg does not hold `&self` while the KV cache is `&mut`.
8423        let (nh, _nkv, _hd, hs, _rd, eps) = (
8424            self.num_heads,
8425            self.num_kv_heads,
8426            self.head_dim,
8427            self.hidden_size,
8428            self.rotary_dim,
8429            self.rms_eps,
8430        );
8431        let pool = self.pool.clone();
8432        // Opt-in wgpu token-graph attention (discrete Vulkan/DX12): the whole
8433        // attention sub-block runs resident in one submit. Off by default.
8434        // Whole-token wgpu graph: eligibility + arbitration.
8435        //  - explicit CMF_GPU_WGPU_GRAPH forces it on/off;
8436        //  - discrete adapters (4090: decode 76 -> 137 tok/s) and GDN
8437        //    hybrids (recurrent state device-resident, no CPU twin to
8438        //    race) TRUST it;
8439        //  - integrated/mobile adapters RACE it against the normal path
8440        //    at generation granularity (gpu::graph_race_*) — tiled
8441        //    mobile GPUs can turn the ~300-dispatch graph into seconds
8442        //    per token, while a fast phone GPU keeps its win.
8443        let graph_env = std::env::var("CMF_GPU_WGPU_GRAPH").ok();
8444        let graph_on = match graph_env.as_deref() {
8445            Some("0") => false,
8446            Some("prefill") => false, // decode keeps the per-op path
8447            Some(_) => true,
8448            // Unset: same discrete-only default as every other graph
8449            // site. "Is the GPU on" used to stand in here — which made
8450            // the 0.2 tok/s whole-token graph race-eligible on mobile
8451            // adapters and cost 12-14× on first tokens (cmfmobile
8452            // TUNING.md); integrated GPUs keep the per-op probe path.
8453            None => crate::gpu::wgpu_graph_default(),
8454        };
8455        let graph_trusted =
8456            graph_env.is_some() || crate::gpu::wgpu_graph_default() || self.gdn_cfg.is_some();
8457        let race_eligible = graph_on
8458            && upto.is_none()
8459            && task_mask.is_none()
8460            && from == 0
8461            && !crate::gpu::graph_unsupported();
8462        let mut tail_start = 0usize;
8463        if race_eligible && crate::gpu::graph_race_use_graph(graph_trusted) {
8464            let t_graph = std::time::Instant::now();
8465            let mut lg = Vec::new();
8466            let mut gl = 0usize;
8467            let built = self.try_token_graph_wgpu(hidden, position, &mut lg, &mut gl);
8468            // Past the transient guards (o1 still collecting, a softcap)
8469            // a refusal is about the weights and will never change —
8470            // remember it instead of walking every layer again next
8471            // token.
8472            if built.is_none() && !self.o1_active() && self.attn_softcap == 0.0 {
8473                crate::gpu::graph_mark_unsupported();
8474            }
8475            graph_note(built.is_some());
8476            if let Some(hh) = built {
8477                let dur = t_graph.elapsed();
8478                if std::env::var("CMF_GRAPH_PROF").is_ok() {
8479                    eprintln!("graph-call: {:.2} ms total", dur.as_secs_f64() * 1000.0);
8480                }
8481                if gl > 0 && gl < self.num_layers {
8482                    // Device prefix: the graph ran layers 0..gl and handed
8483                    // back the boundary hidden — the loop below owns the
8484                    // tail. The prefix layers' KV/state advanced on the
8485                    // device; the tail's advances on the host below. One
8486                    // boundary crossing per token.
8487                    h = hh;
8488                    tail_start = gl;
8489                } else if graph_trusted || !crate::gpu::graph_race_first_token_hopeless(dur) {
8490                    if !graph_trusted {
8491                        crate::gpu::graph_race_record(true, dur);
8492                    }
8493                    if !lg.is_empty() {
8494                        // Graph produced logits (final-norm + lm_head folded in) —
8495                        // pad/cap to vocab and hand them to the sampler directly.
8496                        lg.resize(self.vocab_size, 0.0);
8497                        if let Some(c) = self.final_softcap {
8498                            for l in lg.iter_mut() {
8499                                *l = c * (*l / c).tanh();
8500                            }
8501                        }
8502                        self.graph_logits = Some(lg);
8503                    }
8504                    return hh;
8505                }
8506                // Hopeless first graph token: discard it and fall through
8507                // to the normal path. Safe exactly here — the prompt KV is
8508                // still CPU-owned (chunked prefill), so recomputing this
8509                // position is exact; the mirror's extra row is never read
8510                // (the race just settled on the normal path).
8511            }
8512        }
8513        // KIMI-LINEAR HAS NO SPLIT BUG. The 2.6× reported from the
8514        // model rotation (12.2 tok/s on one card against 4.6 on two)
8515        // was a single measurement of a model whose arm arbitration is
8516        // borderline, and it did not survive repetition. Three runs an
8517        // arm, same binary, back to back:
8518        //   probe on : 1 GPU 9.5 / 5.7 / 5.9   2 GPU 7.8 / 13.0 / 13.3
8519        //   pinned   : 1 GPU 5.6 / 5.3 / 5.2   2 GPU 3.5 / 4.2 / 3.4
8520        // With the arms pinned the split costs about 1.45×, which is
8521        // what a layer split costs. With the probe free, TWO CARDS RUN
8522        // FASTER — because for this model the CPU arm wins some op
8523        // classes and the probe finds that.
8524        //
8525        // Two things do stand, and both are measured. The token graph
8526        // builds NOTHING here (`covered 0 of 14 layers [0..14)`), so
8527        // every layer walks per-op on either arm — that is where the
8528        // headroom is, not in the split. And this model's benchmark is
8529        // unusable without `CMF_GPU_PROBE=0`: the arbitration alone
8530        // moves it by more than 2×.
8531        //
8532        // Span runs (network split): the graph covers exactly [from..=upto]
8533        // — one submit per SEGMENT per token. No race: its state is global
8534        // and calibrated on full stacks, so spans take the graph only where
8535        // it is trusted by default (discrete adapters / CMF_GPU_WGPU_GRAPH).
8536        let span = from > 0 || upto.is_some();
8537        if span && graph_on && task_mask.is_none() && graph_trusted {
8538            let upto_excl = upto.map_or(self.num_layers, |u| u + 1);
8539            let mut lg = Vec::new();
8540            let mut gl = 0usize;
8541            let span_res =
8542                self.try_token_graph_wgpu_span(hidden, position, &mut lg, from, upto_excl, &mut gl);
8543            graph_note(span_res.is_some() && gl == upto_excl - from);
8544            if std::env::var("CMF_GPU_DEBUG").is_ok() {
8545                // How much of the span the graph actually covered. A
8546                // prefix of nothing means every layer walks per-op and
8547                // the split's extra cost is elsewhere.
8548                static SEEN: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
8549                if SEEN.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 4 {
8550                    eprintln!(
8551                        "span graph: covered {gl} of {} layers [{from}..{upto_excl}) res={}",
8552                        upto_excl - from,
8553                        span_res.is_some()
8554                    );
8555                }
8556            }
8557            if let Some(hh) = span_res {
8558                if gl == upto_excl - from {
8559                    if !lg.is_empty() {
8560                        lg.resize(self.vocab_size, 0.0);
8561                        if let Some(c) = self.final_softcap {
8562                            for l in lg.iter_mut() {
8563                                *l = c * (*l / c).tanh();
8564                            }
8565                        }
8566                        self.graph_logits = Some(lg);
8567                    }
8568                    crate::gpu::set_layer(-1);
8569                    return hh;
8570                }
8571                // Partial device prefix of the span: CPU owns the tail.
8572                h = hh;
8573                tail_start = from + gl;
8574            }
8575        }
8576        let t_race_cpu = (race_eligible && !graph_trusted).then(std::time::Instant::now);
8577
8578        #[cfg(target_os = "macos")]
8579        let mut gpu_skip_until = 0usize;
8580        for li in tail_start.max(from)..self.num_layers {
8581            crate::gpu::set_layer(li as i64); // layer-split GPU/CPU (CMF_GPU_LAYERS)
8582            if let Some(u) = upto {
8583                if li > u {
8584                    break;
8585                }
8586            }
8587            if let Some(mask) = task_mask {
8588                if !mask.layer_alive(li) {
8589                    continue; // dead layer: residual pass-through
8590                }
8591            }
8592            // Whole-block q1 token graph: a run of consecutive q1
8593            // layers — GDN and full attention — executes with one sync
8594            // per CPU attend instead of per op (macOS/Metal).
8595            #[cfg(target_os = "macos")]
8596            {
8597                if li < gpu_skip_until {
8598                    continue;
8599                }
8600                if task_mask.is_none() {
8601                    let end = self.q1_graph_gpu(li, upto, position, &mut h);
8602                    if end > li {
8603                        gpu_skip_until = end;
8604                        // Looped Transformer: the graph stopped at a loop
8605                        // boundary — apply final norm before the next iteration.
8606                        if self.is_loop_end(end - 1) && end < self.num_layers {
8607                            h = inference::rms_norm(
8608                                &h,
8609                                &self.weights.final_norm,
8610                                self.rms_eps,
8611                                self.norm_style,
8612                            );
8613                        }
8614                        continue;
8615                    }
8616                }
8617            }
8618
8619            let lw = &self.weights.layers[self.phys_layer(li)];
8620            if let Ok(tp) = std::env::var("CMF_TRACE_POS") {
8621                if tp.parse::<usize>().ok() == Some(position) {
8622                    let n: f32 = h.iter().map(|x| x * x).sum::<f32>().sqrt();
8623                    eprintln!(
8624                        "TRACE pos {position} layer {li}: |h| = {n:.6} h0 {:.6} h1 {:.6}",
8625                        h[0], h[1]
8626                    );
8627                }
8628            }
8629            // Norm into the pipeline scratch — the returning rms_norm
8630            // allocated twice per layer per token (roadmap §3 P0).
8631            inference::rms_norm_into(
8632                &h,
8633                &lw.input_norm,
8634                self.rms_eps,
8635                self.norm_style,
8636                &mut self.ws.n1,
8637            );
8638
8639            let attn_out = match &lw.attn {
8640                AttnKind::Mla(w) => {
8641                    let inv_freq_l = self.layer_inv_freq(li);
8642                    let rs = self.layer_rope_scale(li);
8643                    let eps = self.rms_eps;
8644                    let pool = self.pool.clone();
8645                    mla_attention(
8646                        w,
8647                        &self.ws.n1,
8648                        &mut self.kv_cache.layers[li],
8649                        position,
8650                        &inv_freq_l,
8651                        rs,
8652                        eps,
8653                        pool.as_deref(),
8654                    )
8655                }
8656                AttnKind::Linear(w) => {
8657                    let cfg = self.vmf_cfg.expect("linear layer without vmf_cfg");
8658                    vmf_phase_forward(
8659                        &self.ws.n1,
8660                        w,
8661                        &cfg,
8662                        &mut self.kv_cache.layers[li].linear_state,
8663                        self.pool.as_deref(),
8664                    )
8665                }
8666                AttnKind::Kda(w) => {
8667                    let cfg = self.kda_cfg.expect("kda layer without kda_cfg");
8668                    crate::linear_core::kda_forward(
8669                        &self.ws.n1,
8670                        w,
8671                        &cfg,
8672                        &mut self.kv_cache.layers[li].linear_state,
8673                        self.pool.as_deref(),
8674                    )
8675                }
8676                AttnKind::LinearGdn(w) => {
8677                    let cfg = self.gdn_cfg.expect("gdn layer without gdn_cfg");
8678                    gdn_forward(
8679                        &self.ws.n1,
8680                        w,
8681                        &cfg,
8682                        &mut self.kv_cache.layers[li].linear_state,
8683                        self.pool.as_deref(),
8684                    )
8685                }
8686                AttnKind::ShortConv(w) => {
8687                    let cfg = self
8688                        .short_conv_cfg
8689                        .expect("short-conv layer without short_conv_cfg");
8690                    short_conv_forward(
8691                        &self.ws.n1,
8692                        w,
8693                        &cfg,
8694                        &mut self.kv_cache.layers[li].linear_state,
8695                        self.pool.as_deref(),
8696                    )
8697                }
8698                AttnKind::Full {
8699                    wq,
8700                    wk,
8701                    wv,
8702                    wo,
8703                    q_norm,
8704                    k_norm,
8705                    output_gate,
8706                    softplus_gate,
8707                    bias,
8708                } if self.kv_cache.layers[li].o1_sealed() => {
8709                    // O(1) override: decode on the sealed Nyström state
8710                    // instead of the growing KV cache.
8711                    let inv_freq_l = self.layer_inv_freq(li);
8712                    let (nkv_l, hd_l, rd_l) = self.layer_geom(li);
8713                    let cfg = QwenAttnCfg {
8714                        num_heads: self.layer_num_heads(li),
8715                        num_kv_heads: nkv_l,
8716                        head_dim: hd_l,
8717                        hidden_size: hs,
8718                        position,
8719                        inv_freq: &inv_freq_l,
8720                        rotary_dim: rd_l,
8721                        scale: self.attn_scale,
8722                        softcap: self.attn_softcap,
8723                        window: None,
8724                        v_norm: self.attn_v_norm,
8725                        q_norm: q_norm.as_deref(),
8726                        k_norm: k_norm.as_deref(),
8727                        output_gate: *output_gate,
8728                        softplus_gate: softplus_gate
8729                            .as_ref()
8730                            .map(|(gate, per_head)| (gate, *per_head)),
8731                        rope_scale: self.layer_rope_scale(li),
8732                        bias: bias
8733                            .as_ref()
8734                            .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
8735                        rms_eps: eps,
8736                        norm_style: self.norm_style,
8737                        pool: pool.as_deref(),
8738                    };
8739                    attention::qwen_attention_nystrom(
8740                        &self.ws.n1,
8741                        wq,
8742                        wk,
8743                        wv,
8744                        wo,
8745                        &mut self.kv_cache.layers[li],
8746                        &cfg,
8747                    )
8748                }
8749                AttnKind::Full {
8750                    wq,
8751                    wk,
8752                    wv,
8753                    wo,
8754                    q_norm,
8755                    k_norm,
8756                    output_gate,
8757                    softplus_gate,
8758                    bias,
8759                } => 'attn: {
8760                    // wgpu token-graph attention (opt-in): whole sub-block in
8761                    // one submit, device K/V mirror. q1 only, no gate/bias/mask.
8762                    if graph_on
8763                        && !*output_gate
8764                        && softplus_gate.is_none()
8765                        && self.attention_heads_per_layer.is_none()
8766                        && bias.is_none()
8767                        && task_mask.is_none()
8768                    {
8769                        let inv_freq_l = self.layer_inv_freq(li);
8770                        let (nkv_l, hd_l, rd_l) = self.layer_geom(li);
8771                        let gemma = self.norm_style == cortiq_core::NormStyle::Gemma;
8772                        if let (Some((gm, qi)), Some((_, ki)), Some((_, vi)), Some((_, oi))) = (
8773                            wq.mapped_q1(),
8774                            wk.mapped_q1(),
8775                            wv.mapped_q1(),
8776                            wo.mapped_q1(),
8777                        ) {
8778                            let gm = gm.clone();
8779                            let mut out = vec![0f32; hs];
8780                            let cache = &self.kv_cache.layers[li];
8781                            if crate::gpu::attn_dropin(
8782                                &gm,
8783                                self.graph_kv_id,
8784                                li,
8785                                &self.ws.n1,
8786                                qi,
8787                                ki,
8788                                vi,
8789                                oi,
8790                                q_norm.as_deref(),
8791                                k_norm.as_deref(),
8792                                &inv_freq_l,
8793                                nh,
8794                                nkv_l,
8795                                hd_l,
8796                                rd_l,
8797                                hs,
8798                                position,
8799                                self.kv_cache.max_seq_len,
8800                                gemma,
8801                                eps as f32,
8802                                cache.k_heads(),
8803                                cache.v_heads(),
8804                                &mut out,
8805                            ) {
8806                                break 'attn out;
8807                            }
8808                        }
8809                    }
8810                    let masked = task_mask
8811                        .map(|m| m.head_flags(li, self.num_heads).iter().any(|&a| !a))
8812                        .unwrap_or(false);
8813                    let f32_view = (wq.as_f32(), wk.as_f32(), wv.as_f32(), wo.as_f32());
8814                    match (masked, f32_view) {
8815                        // Historical masked path (f32 slices; the loader
8816                        // keeps masked models in f32).
8817                        (true, (Some(q), Some(k), Some(v), Some(o))) => {
8818                            let active_heads = task_mask.unwrap().head_flags(li, self.num_heads);
8819                            attention::multi_head_attention(
8820                                &self.ws.n1,
8821                                q,
8822                                k,
8823                                v,
8824                                o,
8825                                &mut self.kv_cache.layers[li],
8826                                self.num_heads,
8827                                self.num_kv_heads,
8828                                self.head_dim,
8829                                self.hidden_size,
8830                                position,
8831                                &active_heads,
8832                                &self.inv_freq,
8833                            )
8834                        }
8835                        (masked, _) => {
8836                            if masked {
8837                                tracing::warn!(
8838                                    "layer {li}: head mask on quantized weights not \
8839                                     supported yet — executing dense"
8840                                );
8841                            }
8842                            let inv_freq_l = self.layer_inv_freq(li);
8843                            let (nkv_l, hd_l, rd_l) = self.layer_geom(li);
8844                            let cfg = QwenAttnCfg {
8845                                num_heads: self.layer_num_heads(li),
8846                                num_kv_heads: nkv_l,
8847                                head_dim: hd_l,
8848                                hidden_size: hs,
8849                                position,
8850                                inv_freq: &inv_freq_l,
8851                                rotary_dim: rd_l,
8852                                scale: self.attn_scale,
8853                                softcap: self.attn_softcap,
8854                                window: self.layer_window(li),
8855                                v_norm: self.attn_v_norm,
8856                                q_norm: q_norm.as_deref(),
8857                                k_norm: k_norm.as_deref(),
8858                                output_gate: *output_gate,
8859                                softplus_gate: softplus_gate
8860                                    .as_ref()
8861                                    .map(|(gate, per_head)| (gate, *per_head)),
8862                                rope_scale: self.layer_rope_scale(li),
8863                                bias: bias
8864                                    .as_ref()
8865                                    .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
8866                                rms_eps: eps,
8867                                norm_style: self.norm_style,
8868                                pool: pool.as_deref(),
8869                            };
8870                            attention::qwen_attention(
8871                                &self.ws.n1,
8872                                wq,
8873                                wk,
8874                                wv,
8875                                wo,
8876                                &mut self.kv_cache.layers[li],
8877                                &cfg,
8878                            )
8879                        }
8880                    }
8881                }
8882            };
8883            // Gemma sandwich norm: normalize the attention branch before
8884            // it joins the residual stream.
8885            let attn_out = match &self.weights.layers[self.phys_layer(li)].attn_out_norm {
8886                Some(w) => inference::rms_norm(&attn_out, w, self.rms_eps, self.norm_style),
8887                None => attn_out,
8888            };
8889            let lw = &self.weights.layers[self.phys_layer(li)];
8890            inference::add_rmsnorm_fused_into(
8891                &mut h,
8892                &attn_out,
8893                &lw.post_norm,
8894                self.rms_eps,
8895                self.norm_style,
8896                &mut self.ws.p1,
8897            );
8898            let mut attn_out = attn_out;
8899            attention::recycle_buf(&mut attn_out);
8900            let post_normed = &self.ws.p1;
8901
8902            let ffn_masked = task_mask
8903                .map(|m| m.ffn_active_count(li) < self.intermediate_size)
8904                .unwrap_or(false);
8905            // One masked dense CONTRACT, dispatched by cost. The
8906            // activation-zeroing arm (the batched sweep's, validated
8907            // against the replica to 0.8%) computes the FULL fused FFN
8908            // and zeroes the dead — right whenever most neurons live.
8909            // The sparse arm reads ONLY active rows and down columns —
8910            // per-row dots are slower per element than the fused kernel,
8911            // so it pays only once the mask is deep enough. The 0.5
8912            // crossover is first-principles (fused kernels run ~2x the
8913            // per-row dot throughput); a shallow specialist (95% alive)
8914            // stays fused, a --target-sparsity bake flips arms on its
8915            // own weight.
8916            let ffn_out = match (ffn_masked, &lw.ffn) {
8917                // A defragged tube layer answers its own mask: the core
8918                // always runs, each tube runs when its bit is on, and
8919                // the tubes that are off are never read from the mmap.
8920                (_, FfnKind::Dense(d)) if !d.segs.is_empty() => {
8921                    let row = task_mask
8922                        .and_then(|tm| tm.ffn_masks.get(li))
8923                        .map(|v| v.as_slice());
8924                    tube_ffn(d, post_normed, 1, self.pool.as_deref(), row)
8925                }
8926                (true, FfnKind::Dense(d)) => {
8927                    let tm = task_mask.unwrap();
8928                    let alive = tm.ffn_active_count(li);
8929                    let deep = alive * 2 <= self.intermediate_size;
8930                    if deep && d.down_proj.sparse_col_ok() {
8931                        let active = tm.ffn_active_indices(li);
8932                        sparse_ffn_quant(
8933                            d,
8934                            post_normed,
8935                            &active,
8936                            self.hidden_size,
8937                            self.pool.as_deref(),
8938                        )
8939                    } else if deep
8940                        && let (Some(g), Some(u), Some(dn)) = (
8941                            d.gate_proj.as_f32(),
8942                            d.up_proj.as_f32(),
8943                            d.down_proj.as_f32(),
8944                        )
8945                    {
8946                        let active = tm.ffn_active_indices(li);
8947                        inference::sparse_ffn_forward(
8948                            post_normed,
8949                            g,
8950                            u,
8951                            dn,
8952                            self.hidden_size,
8953                            self.intermediate_size,
8954                            &active,
8955                            self.pool.as_deref(),
8956                        )
8957                    } else {
8958                        let row = tm.ffn_masks.get(li).map(|v| v.as_slice());
8959                        dense_ffn_batch(d, post_normed, 1, self.pool.as_deref(), row)
8960                    }
8961                }
8962                (true, FfnKind::Moe(m)) => {
8963                    // MoE is sparse by expert selection; a task mask
8964                    // narrows the ROUTABLE set via its expert fields
8965                    // (spec §5) when it carries them.
8966                    let allowed = task_mask.and_then(|tm| tm.expert_flags(li, m.experts.len()));
8967                    ffn_forward(
8968                        &lw.ffn,
8969                        post_normed,
8970                        self.pool.as_deref(),
8971                        allowed.as_deref(),
8972                    )
8973                }
8974                (true, FfnKind::DenseMoe(dm)) => dense_moe_ffn(
8975                    dm,
8976                    post_normed,
8977                    &h,
8978                    self.rms_eps,
8979                    self.norm_style,
8980                    self.pool.as_deref(),
8981                ),
8982                (false, _) => match &lw.ffn {
8983                    FfnKind::DenseMoe(dm) => dense_moe_ffn(
8984                        dm,
8985                        post_normed,
8986                        &h,
8987                        self.rms_eps,
8988                        self.norm_style,
8989                        self.pool.as_deref(),
8990                    ),
8991                    _ => {
8992                        let allowed = match (&lw.ffn, task_mask) {
8993                            (FfnKind::Moe(m), Some(tm)) => tm.expert_flags(li, m.experts.len()),
8994                            _ => None,
8995                        };
8996                        ffn_forward(
8997                            &lw.ffn,
8998                            post_normed,
8999                            self.pool.as_deref(),
9000                            allowed.as_deref(),
9001                        )
9002                    }
9003                },
9004            };
9005            let ffn_out = match &self.weights.layers[self.phys_layer(li)].ffn_out_norm {
9006                Some(w) => inference::rms_norm(&ffn_out, w, self.rms_eps, self.norm_style),
9007                None => ffn_out,
9008            };
9009            for (i, &f) in ffn_out.iter().enumerate() {
9010                h[i] += f;
9011            }
9012            let mut ffn_out = ffn_out;
9013            attention::recycle_buf(&mut ffn_out);
9014
9015            // Gemma-4: the layer output is scaled by a learned scalar.
9016            if let Some(sc) = self.weights.layers[self.phys_layer(li)].layer_scale {
9017                for v in h.iter_mut() {
9018                    *v *= sc;
9019                }
9020            }
9021
9022            // Looped Transformer: apply final norm at the end of each loop iteration.
9023            // Nanbeige 4.2: after layer 21 (virtual), apply norm before looping back to layer 0.
9024            if self.is_loop_end(li) && li + 1 < self.num_layers {
9025                h = inference::rms_norm(
9026                    &h,
9027                    &self.weights.final_norm,
9028                    self.rms_eps,
9029                    self.norm_style,
9030                );
9031            }
9032
9033            // Dynamic routing φ capture (on-policy, fireball-style): the
9034            // EMA of the post-residual hidden at the router's phi_layer,
9035            // updated as the context evolves during decode.
9036            if self.dyn_phi_layer == Some(li) {
9037                self.update_dyn_phi(&h);
9038            }
9039        }
9040        crate::gpu::set_layer(-1); // layers done — lm_head outside layer-split
9041        if let Some(t) = t_race_cpu {
9042            crate::gpu::graph_race_record(false, t.elapsed());
9043        }
9044
9045        h
9046    }
9047
9048    /// EMA of φ at the router layer (rolling, weight 0.2 = ~5-token
9049    /// horizon). First observation seeds it exactly.
9050    fn update_dyn_phi(&mut self, h: &[f32]) {
9051        const A: f32 = 0.2;
9052        if self.dyn_phi_ema.len() != h.len() {
9053            self.dyn_phi_ema = vec![0.0; h.len()];
9054            self.dyn_phi_seen = 0;
9055        }
9056        if self.dyn_phi_seen == 0 {
9057            self.dyn_phi_ema.copy_from_slice(h);
9058        } else {
9059            for (e, &v) in self.dyn_phi_ema.iter_mut().zip(h) {
9060                *e = (1.0 - A) * *e + A * v;
9061            }
9062        }
9063        self.dyn_phi_seen += 1;
9064    }
9065
9066    /// Current router φ (EMA at phi_layer); empty until first capture.
9067    pub fn dyn_phi(&self) -> &[f32] {
9068        &self.dyn_phi_ema
9069    }
9070
9071    /// Enable/disable φ capture at the router layer, reset the EMA.
9072    pub fn set_dyn_phi_layer(&mut self, layer: Option<usize>) {
9073        self.dyn_phi_layer = layer;
9074        self.dyn_phi_ema.clear();
9075        self.dyn_phi_seen = 0;
9076    }
9077
9078    /// Skills eligible for dynamic switching: (index, id, phi_layer).
9079    pub fn dynamic_skills(&self) -> Vec<(usize, String, usize)> {
9080        let Some(model) = &self.model else {
9081            return Vec::new();
9082        };
9083        model
9084            .header
9085            .skills
9086            .iter()
9087            .enumerate()
9088            .filter_map(|(i, sk)| {
9089                let ok = matches!(self.dyn_skill_layers.get(i), Some(Some(_)));
9090                let sel = sk.selection.as_ref()?;
9091                (ok).then(|| (i, sk.id.clone(), sel.phi_layer))
9092            })
9093            .collect()
9094    }
9095
9096    /// Index of the currently overlaid skill (None = backbone).
9097    pub fn active_skill(&self) -> Option<usize> {
9098        self.dyn_active
9099    }
9100
9101    /// Enable dynamic per-token skill routing: build the hysteresis
9102    /// router from the container's routable skills, start φ capture at
9103    /// their (shared) phi_layer. Returns the number of routable skills
9104    /// (0 = nothing to route; router stays off). Idempotent.
9105    pub fn enable_dynamic_routing(&mut self) -> usize {
9106        use crate::swarm::{DynRouter, RoutableSkill};
9107        let Some(model) = self.model.clone() else {
9108            return 0;
9109        };
9110        // A blend materialized f32 working tensors into the layers; there
9111        // is no single skill index to revert from → refuse (honest).
9112        if self.dyn_blend_loaded {
9113            tracing::warn!("dynamic routing unavailable on a blend-loaded pipeline");
9114            return 0;
9115        }
9116        // A statically-overlaid skill that is NOT FFN-eligible can't be
9117        // cheaply reverted at generation start → refuse rather than
9118        // silently keep it overlaid.
9119        if let Some(a) = self.dyn_active {
9120            if !matches!(self.dyn_skill_layers.get(a), Some(Some(_))) {
9121                tracing::warn!("loaded skill is not FFN-eligible — dynamic routing unavailable");
9122                return 0;
9123            }
9124        }
9125        let hidden = self.hidden_size;
9126        let mut skills = Vec::new();
9127        for (idx, id, _phi) in self.dynamic_skills() {
9128            if let Some(sel) = model.header.skills[idx].selection.as_ref() {
9129                if let Some(rs) = RoutableSkill::from_descriptor(idx, id, sel, hidden) {
9130                    skills.push(rs);
9131                }
9132            }
9133        }
9134        if skills.is_empty() {
9135            return 0;
9136        }
9137        // Skills should share a phi_layer; warn (not fail) if they don't.
9138        let phi = skills[0].phi_layer;
9139        if skills.iter().any(|s| s.phi_layer != phi) {
9140            tracing::warn!("routable skills disagree on phi_layer; using {phi}");
9141        }
9142        let n = skills.len();
9143        self.set_dyn_phi_layer(Some(phi));
9144        self.dyn_router = Some(DynRouter::new(skills));
9145        n
9146    }
9147
9148    /// Human-readable switch log from the last dynamic-routed generation.
9149    pub fn route_switches(&self) -> Vec<(usize, Option<String>, Option<String>)> {
9150        self.dyn_router
9151            .as_ref()
9152            .map(|r| r.switches.clone())
9153            .unwrap_or_default()
9154    }
9155
9156    /// LM head: hidden → logits [vocab_size]. The dominant matvec of
9157    /// every decode step — row-parallel on the worker pool.
9158    fn lm_head_forward(&self, hidden: &[f32]) -> Vec<f32> {
9159        let rows = self.weights.lm_head.rows();
9160        let mut logits = attention::take_buf(rows.min(self.vocab_size));
9161        self.weights
9162            .lm_head
9163            .matvec(hidden, &mut logits, self.pool.as_deref());
9164        logits.resize(self.vocab_size, 0.0);
9165        if let Some(m) = self.logit_multiplier {
9166            for l in logits.iter_mut() {
9167                *l *= m;
9168            }
9169        }
9170        if let Some(c) = self.final_softcap {
9171            for l in logits.iter_mut() {
9172                *l = c * (*l / c).tanh();
9173            }
9174        }
9175        if let Some(cm) = self.head_clusters.as_ref() {
9176            self.hierarchical_head_logprobs(hidden, cm, &mut logits);
9177        }
9178        logits
9179    }
9180
9181    /// Two-level head (Cortiq Embryo): in place, logits[v] ← log p(v) =
9182    /// (lc[c] − lse(lc)) + (logit[v] − lse over v's cluster block), c = v / S.
9183    fn hierarchical_head_logprobs(&self, hidden: &[f32], cm: &[f32], logits: &mut [f32]) {
9184        let h = hidden.len();
9185        let ncl = cm.len() / h.max(1);
9186        if ncl == 0 || logits.len() % ncl != 0 {
9187            return;
9188        }
9189        let cs = logits.len() / ncl;
9190        // cluster logits + log-softmax
9191        let mut lc = vec![0.0f32; ncl];
9192        for c in 0..ncl {
9193            let row = &cm[c * h..(c + 1) * h];
9194            let mut s = 0.0f32;
9195            for j in 0..h {
9196                s += row[j] * hidden[j];
9197            }
9198            lc[c] = s;
9199        }
9200        let mx = lc.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
9201        let lse: f32 = mx + lc.iter().map(|v| (v - mx).exp()).sum::<f32>().ln();
9202        for c in 0..ncl {
9203            let blk = &mut logits[c * cs..(c + 1) * cs];
9204            let bm = blk.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
9205            let bl: f32 = bm + blk.iter().map(|v| (v - bm).exp()).sum::<f32>().ln();
9206            let add = lc[c] - lse - bl;
9207            for v in blk.iter_mut() {
9208                *v += add;
9209            }
9210        }
9211    }
9212
9213    /// Prefill `ids` and return the next-token logits — what the model
9214    /// would predict next, WITHOUT committing to generation (introspection
9215    /// for `cortiq explain`). Clears and repopulates the KV cache; leaves
9216    /// the active overlay untouched.
9217    pub fn prefill_next_logits(&mut self, ids: &[u32], task_mask: Option<&TaskMask>) -> Vec<f32> {
9218        self.kv_cache.clear();
9219        self.kv_history.clear();
9220        let mut hidden = vec![0.0f32; self.hidden_size];
9221        for (pos, &id) in ids.iter().enumerate() {
9222            let emb = self.embed_single(id);
9223            hidden = self.forward_layers(&emb, pos, task_mask);
9224        }
9225        inference::rms_norm_into(
9226            &hidden,
9227            &self.weights.final_norm,
9228            self.rms_eps,
9229            self.norm_style,
9230            &mut self.ws.n1,
9231        );
9232        self.lm_head_forward(&self.ws.n1)
9233    }
9234}
9235
9236/// Convenience: deterministic tiny pipeline for tests.
9237pub fn create_test_pipeline(
9238    hidden_size: usize,
9239    intermediate_size: usize,
9240    num_heads: usize,
9241    num_kv_heads: usize,
9242    head_dim: usize,
9243    num_layers: usize,
9244    vocab_size: usize,
9245) -> Pipeline {
9246    // Small pseudo-random weights: constant weights make attention
9247    // degenerate and hide indexing bugs.
9248    let synth = |n: usize, salt: usize| -> Vec<f32> {
9249        (0..n)
9250            .map(|i| (((i * 31 + salt * 17 + 7) % 97) as f32 / 97.0 - 0.5) * 0.2)
9251            .collect()
9252    };
9253    let qt = |rows: usize, cols: usize, salt: usize| -> QTensor {
9254        QTensor::from_f32(synth(rows * cols, salt), rows, cols)
9255    };
9256    let layer_weights: Vec<LayerWeights> = (0..num_layers)
9257        .map(|li| LayerWeights {
9258            input_norm: vec![1.0; hidden_size],
9259            post_norm: vec![1.0; hidden_size],
9260            attn_out_norm: None,
9261            ffn_out_norm: None,
9262            layer_scale: None,
9263            ffn: FfnKind::Dense(DenseFfn {
9264                gate_proj: qt(intermediate_size, hidden_size, li * 10 + 5),
9265                up_proj: qt(intermediate_size, hidden_size, li * 10 + 6),
9266                down_proj: qt(hidden_size, intermediate_size, li * 10 + 7),
9267                act: Act::Silu,
9268                down_t: None,
9269                segs: Vec::new(),
9270            }),
9271            attn: AttnKind::Full {
9272                bias: None,
9273                wq: qt(num_heads * head_dim, hidden_size, li * 10 + 1),
9274                wk: qt(num_kv_heads * head_dim, hidden_size, li * 10 + 2),
9275                wv: qt(num_kv_heads * head_dim, hidden_size, li * 10 + 3),
9276                wo: qt(hidden_size, num_heads * head_dim, li * 10 + 4),
9277                q_norm: None,
9278                k_norm: None,
9279                output_gate: false,
9280                softplus_gate: None,
9281            },
9282        })
9283        .collect();
9284
9285    Pipeline::new(
9286        Tokenizer::byte_level(),
9287        PipelineWeights {
9288            embed_tokens: qt(vocab_size, hidden_size, 100),
9289            layers: layer_weights,
9290            lm_head: qt(vocab_size, hidden_size, 200),
9291            final_norm: vec![1.0; hidden_size],
9292        },
9293        hidden_size,
9294        intermediate_size,
9295        num_heads,
9296        num_kv_heads,
9297        head_dim,
9298        num_layers,
9299        num_layers, // physical_layers = num_layers (non-looped)
9300        false,      // loop_final_norm
9301        vocab_size,
9302        1e-6,
9303        10_000.0,
9304        NormStyle::Qwen,
9305        4096,
9306        SamplerConfig {
9307            seed: Some(42),
9308            ..Default::default()
9309        },
9310    )
9311}
9312
9313/// Batched dense-FFN: gate/up/down via matmat (element-wise the same
9314/// math as b × dense_ffn — the same dot kernels).
9315/// One mask bit, LSB-first per byte — `TaskMask::ffn_active_indices`'s
9316/// convention.
9317#[inline]
9318fn mask_bit(row: &[u8], j: usize) -> bool {
9319    (row.get(j >> 3).copied().unwrap_or(0) >> (j & 7)) & 1 != 0
9320}
9321
9322/// Zero the CLOSED neurons' activations in a [rows × inter] panel — the
9323/// masked-inference fast path's whole trick: full fused quant compute,
9324/// then the mask lands on the ACTIVATIONS, which is arithmetically the
9325/// pruned network without touching a quantized weight byte. Whole open
9326/// bytes (0xFF = 8 open neurons) skip in one test.
9327/// `CMF_FFN_MASK_GAIN` — Patent 12 FIG. 4, variance-preserving
9328/// rescaling: truncation removes a share of the layer's output energy,
9329/// so the survivors are scaled up to put the variance back where the
9330/// downstream norm expects it. A scalar here; per layer it is
9331/// `sqrt(total energy / kept energy)`.
9332fn mask_gain() -> f32 {
9333    static G: std::sync::OnceLock<f32> = std::sync::OnceLock::new();
9334    *G.get_or_init(|| {
9335        std::env::var("CMF_FFN_MASK_GAIN")
9336            .ok()
9337            .and_then(|v| v.parse().ok())
9338            .unwrap_or(1.0)
9339    })
9340}
9341
9342fn zero_masked_cols(g: &mut [f32], rows: usize, inter: usize, row: &[u8]) {
9343    // With CMF_FFN_MEANFILL a closed neuron contributes its average
9344    // instead of nothing — same bytes read, one constant restored.
9345    let fill = meanfill().and_then(|(i, v)| {
9346        let li = crate::gpu::cur_layer();
9347        (*i == inter && li >= 0).then(|| &v[li as usize * inter..(li as usize + 1) * inter])
9348    });
9349    for r in 0..rows {
9350        let base = r * inter;
9351        for (bi, &byte) in row.iter().enumerate() {
9352            if byte == 0xFF {
9353                continue;
9354            }
9355            let j0 = bi * 8;
9356            for bit in 0..8 {
9357                let j = j0 + bit;
9358                if j < inter && byte & (1 << bit) == 0 {
9359                    g[base + j] = fill.map_or(0.0, |f| f[j]);
9360                }
9361            }
9362        }
9363    }
9364    let gain = mask_gain();
9365    if gain != 1.0 {
9366        for v in g[..rows * inter].iter_mut() {
9367            *v *= gain;
9368        }
9369    }
9370}
9371
9372/// True when neuron `i`'s bit is set (no mask = everything runs).
9373#[inline]
9374fn tube_bit(row: Option<&[u8]>, i: usize) -> bool {
9375    row.is_none_or(|r| mask_bit(r, i))
9376}
9377
9378/// Every bit below `n` set — the common case for a tube file's CORE,
9379/// where only the tube bits vary per task.
9380fn all_bits_on(row: &[u8], n: usize) -> bool {
9381    (0..n).all(|i| mask_bit(row, i))
9382}
9383
9384/// `CMF_TUBE_TOPK` — how many tubes a TOKEN may open (0 = the task mask
9385/// decides alone). This is the dense FFN read as a mixture: the tubes
9386/// are the experts a k-means over `gate_proj` rows found, and the token
9387/// picks among them. `CMF_TUBE_SCORE=gate` scores a tube by its own
9388/// gate (realizable: only `up`/`down` of the losers go unread),
9389/// `=oracle` scores by the true `silu(gate)·up` mass (the ceiling —
9390/// only `down` is saved, and the selection has read what it predicts).
9391fn tube_topk() -> usize {
9392    static K: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
9393    *K.get_or_init(|| {
9394        std::env::var("CMF_TUBE_TOPK")
9395            .ok()
9396            .and_then(|v| v.parse().ok())
9397            .unwrap_or(0)
9398    })
9399}
9400
9401fn tube_score_oracle() -> bool {
9402    static O: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
9403    *O.get_or_init(|| std::env::var("CMF_TUBE_SCORE").is_ok_and(|v| v == "oracle"))
9404}
9405
9406/// The routed arm of `tube_ffn`: a token opens only its best `k` tubes.
9407/// At `b == 1` (decode) the losers are genuinely never read — that is
9408/// the speed. At `b > 1` (the scoring sweep) every tube is computed and
9409/// the losers' activations are zeroed instead: same arithmetic, so the
9410/// perplexity is the routed model's, measured without a per-token
9411/// gather in the middle of a GEMM.
9412fn tube_ffn_routed(
9413    d: &DenseFfn,
9414    xs: &[f32],
9415    b: usize,
9416    pool: Option<&Pool>,
9417    mask_row: Option<&[u8]>,
9418    k: usize,
9419) -> Vec<f32> {
9420    let hidden = d.down_proj.rows();
9421    let core = d.gate_proj.rows();
9422    let core_full = mask_row.is_none_or(|r| all_bits_on(r, core));
9423    let mut out = match (b, core_full, mask_row) {
9424        (1, true, _) => dense_ffn(d, xs, pool),
9425        (1, false, Some(row)) => dense_ffn_masked(d, xs, pool, row),
9426        (_, true, _) => dense_ffn_batch(d, xs, b, pool, None),
9427        (_, false, row) => dense_ffn_batch(d, xs, b, pool, row),
9428    };
9429    let cand: Vec<usize> = (0..d.segs.len())
9430        .filter(|&i| tube_bit(mask_row, d.segs[i].start))
9431        .collect();
9432    if cand.is_empty() {
9433        return out;
9434    }
9435    // gate (and, where the score or the batch needs it, up) per tube.
9436    // The SCORE is taken at the point the serving path could take it:
9437    // off the gate alone, or off the finished activation for the oracle.
9438    let oracle = tube_score_oracle();
9439    let mut acts: Vec<Vec<f32>> = Vec::with_capacity(cand.len());
9440    let mut scores = vec![0f32; b * cand.len()];
9441    for (ci, &i) in cand.iter().enumerate() {
9442        let seg = &d.segs[i];
9443        let w = seg.width;
9444        let mut g = vec![0.0f32; b * w];
9445        if b == 1 {
9446            seg.gate.matvec(xs, &mut g, pool);
9447        } else {
9448            seg.gate.matmat(xs, b, &mut g, pool);
9449        }
9450        for v in g.iter_mut() {
9451            *v = Act::Silu.combine(*v, 1.0);
9452        }
9453        if !oracle {
9454            for t in 0..b {
9455                scores[t * cand.len() + ci] =
9456                    g[t * w..(t + 1) * w].iter().map(|v| v * v).sum::<f32>();
9457            }
9458        }
9459        if oracle || b > 1 {
9460            let mut u = vec![0.0f32; b * w];
9461            if b == 1 {
9462                seg.up.matvec(xs, &mut u, pool);
9463            } else {
9464                seg.up.matmat(xs, b, &mut u, pool);
9465            }
9466            for (a, &v) in g.iter_mut().zip(u.iter()) {
9467                *a *= v;
9468            }
9469            if oracle {
9470                for t in 0..b {
9471                    scores[t * cand.len() + ci] =
9472                        g[t * w..(t + 1) * w].iter().map(|v| v * v).sum::<f32>();
9473                }
9474            }
9475        }
9476        acts.push(g);
9477    }
9478    // per-token scores and the winners
9479    let keep = k.min(cand.len());
9480    let mut scratch: Vec<f32> = Vec::new();
9481    for t in 0..b {
9482        let mut sc: Vec<(f32, usize)> = (0..cand.len())
9483            .map(|ci| (scores[t * cand.len() + ci], ci))
9484            .collect();
9485        sc.sort_unstable_by(|x, y| y.0.total_cmp(&x.0));
9486        let mut alive = vec![false; cand.len()];
9487        for &(_, ci) in sc.iter().take(keep) {
9488            alive[ci] = true;
9489        }
9490        if b > 1 {
9491            for (ci, a) in acts.iter_mut().enumerate() {
9492                if !alive[ci] {
9493                    let w = d.segs[cand[ci]].width;
9494                    a[t * w..(t + 1) * w].fill(0.0);
9495                }
9496            }
9497        } else {
9498            // decode: finish only the winners — the losers' up/down
9499            // (and, with the gate score, everything but their gate)
9500            // are never touched.
9501            for (ci, &i) in cand.iter().enumerate() {
9502                if !alive[ci] {
9503                    continue;
9504                }
9505                let seg = &d.segs[i];
9506                let w = seg.width;
9507                let g = &mut acts[ci];
9508                if !tube_score_oracle() {
9509                    scratch.clear();
9510                    scratch.resize(w, 0.0);
9511                    seg.up.matvec(xs, &mut scratch, pool);
9512                    for (a, &v) in g.iter_mut().zip(scratch.iter()) {
9513                        *a *= v;
9514                    }
9515                }
9516                let mut acc = vec![0.0f32; hidden];
9517                seg.down.matvec(g, &mut acc, pool);
9518                for (o, a) in out.iter_mut().zip(&acc) {
9519                    *o += *a;
9520                }
9521            }
9522        }
9523    }
9524    if b > 1 {
9525        for (ci, &i) in cand.iter().enumerate() {
9526            let seg = &d.segs[i];
9527            let mut acc = vec![0.0f32; b * hidden];
9528            seg.down.matmat(&acts[ci], b, &mut acc, pool);
9529            for (o, a) in out.iter_mut().zip(&acc) {
9530                *o += *a;
9531            }
9532        }
9533    }
9534    out
9535}
9536
9537/// FFN of a defragged tube layer: the always-on core plus the tubes the
9538/// task mask switches on. Each tube is a normal tensor triple, so the
9539/// same kernels run it and an inactive tube's bytes are never read —
9540/// that is the whole point of the defrag (a scattered mask cannot skip
9541/// bytes; a contiguous one is just a smaller matrix).
9542fn tube_ffn(
9543    d: &DenseFfn,
9544    xs: &[f32],
9545    b: usize,
9546    pool: Option<&Pool>,
9547    mask_row: Option<&[u8]>,
9548) -> Vec<f32> {
9549    if tube_topk() > 0 {
9550        return tube_ffn_routed(d, xs, b, pool, mask_row, tube_topk());
9551    }
9552    let hidden = d.down_proj.rows();
9553    let core = d.gate_proj.rows();
9554    let core_full = mask_row.is_none_or(|r| all_bits_on(r, core));
9555    let mut out = match (b, core_full, mask_row) {
9556        (1, true, _) => dense_ffn(d, xs, pool),
9557        (1, false, Some(row)) => dense_ffn_masked(d, xs, pool, row),
9558        (_, true, _) => dense_ffn_batch(d, xs, b, pool, None),
9559        (_, false, row) => dense_ffn_batch(d, xs, b, pool, row),
9560    };
9561    TUBE_SCRATCH.with(|sc| {
9562        let mut sc = sc.borrow_mut();
9563        let [g, u, acc] = &mut *sc;
9564        for seg in &d.segs {
9565            if !tube_bit(mask_row, seg.start) {
9566                continue;
9567            }
9568            let w = seg.width;
9569            g.resize(b * w, 0.0);
9570            if b == 1
9571                && d.act == Act::Silu
9572                && QTensor::matvec_silu_mul(&seg.gate, &seg.up, xs, g, pool)
9573            {
9574                // g holds silu(gate)·up.
9575            } else {
9576                u.resize(b * w, 0.0);
9577                if b == 1 {
9578                    QTensor::matvec_many([&seg.gate, &seg.up], xs, [g, u], pool);
9579                } else {
9580                    seg.gate.matmat(xs, b, g, pool);
9581                    seg.up.matmat(xs, b, u, pool);
9582                }
9583                for i in 0..b * w {
9584                    g[i] = d.act.combine(g[i], u[i]);
9585                }
9586            }
9587            acc.resize(b * hidden, 0.0);
9588            acc.fill(0.0);
9589            if b == 1 {
9590                seg.down.matvec(g, acc, pool);
9591            } else {
9592                seg.down.matmat(g, b, acc, pool);
9593            }
9594            for (o, a) in out.iter_mut().zip(acc.iter()) {
9595                *o += *a;
9596            }
9597        }
9598        out
9599    })
9600}
9601
9602thread_local! {
9603    /// gate / up / down-accumulator scratch for the tube loop — a tube
9604    /// runs once per layer per token, and a fresh Vec each time is a
9605    /// malloc per tube per layer per token.
9606    static TUBE_SCRATCH: std::cell::RefCell<[Vec<f32>; 3]> =
9607        const { std::cell::RefCell::new([Vec::new(), Vec::new(), Vec::new()]) };
9608}
9609
9610fn dense_ffn_batch(
9611    d: &DenseFfn,
9612    xs: &[f32],
9613    b: usize,
9614    pool: Option<&Pool>,
9615    mask_row: Option<&[u8]>,
9616) -> Vec<f32> {
9617    let inter = d.gate_proj.rows();
9618    let hidden = d.down_proj.rows();
9619    // Fused on-device SwiGLU when the device is in play: three separate
9620    // `matmat` calls are three round trips per layer, and the gate/up
9621    // panels (b × inter — 22 MB each at a 512-token chunk) cross the bus
9622    // twice for nothing. The kernel already existed for the image DiT;
9623    // the LLM prefill was simply never wired to it. A task mask needs the
9624    // activations on the host between the halves, so it keeps the CPU
9625    // arm below.
9626    if mask_row.is_none()
9627        && d.act == Act::Silu
9628        && b >= 32
9629        && crate::gpu::enabled_here()
9630        && !crate::gpu::mm_killed()
9631        // The refit pass needs this layer's activations on the host; the
9632        // fused chain keeps them on the device. Refusing it here costs
9633        // one round trip and keeps every GEMM on the card — the
9634        // alternative was running the whole calibration on the CPU.
9635        && refit_dir().is_none()
9636        // Same for the mass/hit probes. The accumulator at the bottom of
9637        // this function only sees `g` when `g` came back to the host, so
9638        // a fused batch would leave it summing nothing — a probe that
9639        // reports zeros rather than failing, which is worse.
9640        && !ffn_probe_active()
9641    {
9642        if let (Some((model, w1)), Some((_, w3)), Some((_, w2))) = (
9643            d.gate_proj.mapped_q4t(),
9644            d.up_proj.mapped_q4t(),
9645            d.down_proj.mapped_q4t(),
9646        ) {
9647            let mut out = vec![0.0f32; b * hidden];
9648            if crate::gpu::q4t_ffn(model, w1, w3, w2, xs, b, hidden, inter, &mut out) {
9649                return out;
9650            }
9651        }
9652        // The q4tp twin (same kernel family, scale from the row ladder) —
9653        // the DiT has run it in production since the pipeline containers;
9654        // the LLM prefill was simply never wired to it, so a q4tp model's
9655        // prefill panels stayed on the CPU.
9656        if let (Some((model, w1)), Some((_, w3)), Some((_, w2))) = (
9657            d.gate_proj.mapped_q4tp(),
9658            d.up_proj.mapped_q4tp(),
9659            d.down_proj.mapped_q4tp(),
9660        ) {
9661            let mut out = vec![0.0f32; b * hidden];
9662            if crate::gpu::q4tp_ffn(model, w1, w3, w2, xs, b, hidden, inter, &mut out) {
9663                return out;
9664            }
9665        }
9666    }
9667    let mut g = vec![0.0f32; b * inter];
9668    d.gate_proj.matmat(xs, b, &mut g, pool);
9669    let mut u = vec![0.0f32; b * inter];
9670    d.up_proj.matmat(xs, b, &mut u, pool);
9671    if gate_topk() > 0 && d.act == Act::Silu {
9672        for t in 0..b {
9673            let row = &mut g[t * inter..(t + 1) * inter];
9674            for v in row.iter_mut() {
9675                *v = Act::Silu.combine(*v, 1.0);
9676            }
9677            keep_top_k(row, gate_topk());
9678        }
9679        for i in 0..b * inter {
9680            g[i] *= u[i];
9681        }
9682    } else {
9683        for i in 0..b * inter {
9684            g[i] = d.act.combine(g[i], u[i]);
9685        }
9686    }
9687    if let Some(row) = mask_row {
9688        zero_masked_cols(&mut g, b, inter, row);
9689    }
9690    if oracle_topk() > 0 {
9691        for t in 0..b {
9692            keep_top_k(&mut g[t * inter..(t + 1) * inter], oracle_topk());
9693        }
9694    }
9695    let mut out = vec![0.0f32; b * hidden];
9696    d.down_proj.matmat(&g, b, &mut out, pool);
9697    if refit_dir().is_some() {
9698        let li = crate::gpu::cur_layer();
9699        if li >= 0 {
9700            refit_accumulate(li as usize, &g, b, inter, &out, hidden, pool);
9701        }
9702    }
9703    // The DTG-MA probe, on the batched path: one prefill sweep gives the
9704    // same per-neuron statistic the per-position probe does, and on a 27B
9705    // that is minutes instead of hours.
9706    FFN_PROBE.with(|pr| {
9707        if let Some(acc) = pr.borrow_mut().as_mut() {
9708            let li = crate::gpu::cur_layer();
9709            if li < 0 {
9710                return;
9711            }
9712            let Some(row) = acc.get_mut(li as usize) else {
9713                return;
9714            };
9715            let sq = probe_sq();
9716            for t in 0..b {
9717                for (a, &v) in row.iter_mut().zip(&g[t * inter..(t + 1) * inter]) {
9718                    *a += if sq {
9719                        (v as f64) * (v as f64)
9720                    } else {
9721                        (v as f64).abs()
9722                    };
9723                }
9724            }
9725        }
9726    });
9727    out
9728}
9729
9730/// Batched MoE-FFN: router batched, positions are GROUPED by expert —
9731/// an expert's weights are read once for all its positions in the chunk
9732/// (the main prefill-GEMM win on MoE: 960MB/token of 35B experts).
9733/// Accumulate per-channel activation energy for `CMF_RMS_TRACE`.
9734fn accumulate_act(m: &MoeFfn, xs: &[f32], b: usize) {
9735    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
9736    static DUMP: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
9737    let on = *ON.get_or_init(|| std::env::var("CMF_RMS_TRACE").is_ok());
9738    let dump = *DUMP.get_or_init(|| std::env::var("CMF_ACT_DUMP").is_ok());
9739    if (!on && !dump) || b == 0 {
9740        return;
9741    }
9742    let hidden = xs.len() / b;
9743    if on {
9744        let mut acc = m.act_sq.borrow_mut();
9745        if acc.len() < hidden {
9746            acc.resize(hidden, 0.0);
9747        }
9748        for t in 0..b {
9749            let row = &xs[t * hidden..(t + 1) * hidden];
9750            for (a, &v) in acc.iter_mut().zip(row) {
9751                *a += (v as f64) * (v as f64);
9752            }
9753        }
9754    }
9755    if dump {
9756        // Cap the capture: the covariance needs a few thousand rows, and a
9757        // whole prefill of every layer would be gigabytes for no extra rank.
9758        let cap: usize = std::env::var("CMF_ACT_DUMP_ROWS")
9759            .ok()
9760            .and_then(|v| v.parse().ok())
9761            .unwrap_or(4096);
9762        let mut rows = m.act_rows.borrow_mut();
9763        if rows.len() < cap * hidden {
9764            let take = b.min((cap * hidden - rows.len()) / hidden.max(1));
9765            rows.extend_from_slice(&xs[..take * hidden]);
9766        }
9767    }
9768}
9769
9770/// Send-able cursor over a Vec-of-Vecs: each pool worker writes only its
9771/// own slots (disjoint by construction in the caller).
9772#[derive(Clone, Copy)]
9773struct SendVecs(*mut Vec<f32>);
9774unsafe impl Send for SendVecs {}
9775unsafe impl Sync for SendVecs {}
9776impl SendVecs {
9777    #[inline]
9778    fn at(self, i: usize) -> *mut Vec<f32> {
9779        unsafe { self.0.add(i) }
9780    }
9781}
9782
9783fn moe_ffn_batch(
9784    m: &MoeFfn,
9785    xs: &[f32],
9786    b: usize,
9787    hidden: usize,
9788    pool: Option<&Pool>,
9789    allowed: Option<&[bool]>,
9790) -> Vec<f32> {
9791    accumulate_act(m, xs, b);
9792    let ne = m.experts.len();
9793    let mut logits = vec![0.0f32; b * ne];
9794    match &m.resonance {
9795        Some(r) => {
9796            let hdim = xs.len() / b.max(1);
9797            for bi in 0..b {
9798                r.scores(
9799                    &xs[bi * hdim..(bi + 1) * hdim],
9800                    &mut logits[bi * ne..(bi + 1) * ne],
9801                );
9802            }
9803        }
9804        None => m.router.matmat(xs, b, &mut logits, pool),
9805    }
9806
9807    // Assignments: expert → [(position, weight)] — same routing as
9808    // moe_ffn, per position (see `moe_route`).
9809    let mut assign: Vec<Vec<(usize, f32)>> = vec![Vec::new(); ne];
9810    {
9811        let mut st = m.stats.borrow_mut();
9812        if st.len() < ne {
9813            st.resize(ne, 0);
9814        }
9815        for bi in 0..b {
9816            let (idx, p, wsum) = moe_route(&logits[bi * ne..(bi + 1) * ne], m, allowed);
9817            for &e in &idx {
9818                st[e] += 1;
9819                assign[e].push((bi, p[e] / wsum));
9820            }
9821        }
9822    }
9823
9824    let mut out = vec![0.0f32; b * hidden];
9825    let cols = m.experts[0].gate_proj.cols();
9826    let run_expert = |d: &DenseFfn, list: &[(usize, f32)], out: &mut [f32]| {
9827        let sb = list.len();
9828        let mut sub = vec![0.0f32; sb * cols];
9829        for (k, &(bi, _)) in list.iter().enumerate() {
9830            sub[k * cols..(k + 1) * cols].copy_from_slice(&xs[bi * cols..(bi + 1) * cols]);
9831        }
9832        let eo = dense_ffn_batch(d, &sub, sb, pool, None);
9833        for (k, &(bi, w)) in list.iter().enumerate() {
9834            for i in 0..hidden {
9835                out[bi * hidden + i] += w * eo[k * hidden + i];
9836            }
9837        }
9838    };
9839    // Routed experts: the panels are TINY (b·top_k spread over every
9840    // expert — a few positions each), so a pool dispatch per expert is
9841    // pure barrier cost. Invert the parallelism: workers take WHOLE
9842    // experts (serial math inside), then one deterministic scatter in
9843    // expert order — the exact accumulation order the serial loop had.
9844    let active: Vec<usize> = (0..ne).filter(|&e| !assign[e].is_empty()).collect();
9845    if pool.is_some() && active.len() >= 8 {
9846        let mut panels: Vec<Vec<f32>> = vec![Vec::new(); active.len()];
9847        {
9848            let panel_ptr = SendVecs(panels.as_mut_ptr());
9849            // Capture only the expert table: `m` itself carries RefCell
9850            // stats and must not cross the pool boundary.
9851            let experts = &m.experts;
9852            let (active_r, assign_r) = (&active, &assign);
9853            let run = |start: usize, end: usize| {
9854                for ai in start..end {
9855                    let e = active_r[ai];
9856                    let list = &assign_r[e];
9857                    let sb = list.len();
9858                    let mut sub = vec![0.0f32; sb * cols];
9859                    for (k, &(bi, _)) in list.iter().enumerate() {
9860                        sub[k * cols..(k + 1) * cols]
9861                            .copy_from_slice(&xs[bi * cols..(bi + 1) * cols]);
9862                    }
9863                    // SAFETY: each worker owns a disjoint panels[ai].
9864                    unsafe {
9865                        *panel_ptr.at(ai) = dense_ffn_batch(&experts[e], &sub, sb, None, None);
9866                    }
9867                }
9868            };
9869            match pool {
9870                Some(p) => p.run_rows(active.len(), &run),
9871                None => run(0, active.len()),
9872            }
9873        }
9874        for (ai, &e) in active.iter().enumerate() {
9875            for (k, &(bi, w)) in assign[e].iter().enumerate() {
9876                let eo = &panels[ai][k * hidden..(k + 1) * hidden];
9877                for i in 0..hidden {
9878                    out[bi * hidden + i] += w * eo[i];
9879                }
9880            }
9881        }
9882    } else {
9883        for &e in &active {
9884            run_expert(&m.experts[e], &assign[e], &mut out);
9885        }
9886    }
9887    if let Some((se, gate)) = &m.shared {
9888        let all: Vec<(usize, f32)> = if let Some(gate) = gate {
9889            let mut gl = vec![0.0f32; b];
9890            gate.matmat(xs, b, &mut gl, pool);
9891            (0..b)
9892                .map(|bi| (bi, 1.0 / (1.0 + (-gl[bi]).exp())))
9893                .collect()
9894        } else {
9895            (0..b).map(|bi| (bi, 1.0)).collect()
9896        };
9897        run_expert(se, &all, &mut out);
9898    }
9899    out
9900}
9901
9902thread_local! {
9903    /// gate/up activation scratch for the dense FFN paths (single uses
9904    /// two slots, the fused pair all four) — these were fresh
9905    /// intermediate-size Vecs on every layer of every token.
9906    static FFN_SCRATCH: std::cell::RefCell<[Vec<f32>; 4]> =
9907        const { std::cell::RefCell::new([Vec::new(), Vec::new(), Vec::new(), Vec::new()]) };
9908}
9909
9910/// Dense SwiGLU FFN through QTensor matvecs (any storage).
9911fn dense_ffn(d: &DenseFfn, x: &[f32], pool: Option<&Pool>) -> Vec<f32> {
9912    // Per-token sparsity, when the file was built for it: gate first,
9913    // then only the chosen neurons' up/down rows leave the mmap.
9914    if gate_topk() > 0
9915        && let Some(out) = dense_ffn_dynamic(d, x, pool, gate_topk())
9916    {
9917        return out;
9918    }
9919    // Whole-FFN GPU submit (этап 4.2 increment): gate → silu·up → down
9920    // chained in ONE command buffer with the intermediate activations
9921    // resident on the device — 3 per-op polls become 1 per layer. The
9922    // moe_block backend already implements exactly this chain; a dense
9923    // FFN is one expert with weight 1. Runtime probe: the chain still
9924    // pays one submit+poll per layer — alternate it against the pure-CPU
9925    // FFN and keep whichever is faster on this machine.
9926    // q1 FFNs offload at any practical size: the q1 CPU kernel is
9927    // compute-bound, so the UMA threshold logic does not apply — the
9928    // probe measures and decides either way.
9929    if crate::gpu::enabled_here()
9930        && (d.gate_proj.rows() >= crate::gpu::min_rows() || d.gate_proj.is_q1())
9931    {
9932        let arm = if d.gate_proj.is_q1() && crate::gpu::q1_force() {
9933            crate::gpu::ProbeArm::Gpu
9934        } else {
9935            crate::gpu::probe_arm(crate::gpu::OpClass::Ffn)
9936        };
9937        match arm {
9938            crate::gpu::ProbeArm::Gpu => {
9939                let t0 = std::time::Instant::now();
9940                if let Some(out) = dense_ffn_gpu(d, x, pool) {
9941                    crate::gpu::probe_record(crate::gpu::OpClass::Ffn, true, t0.elapsed());
9942                    return out;
9943                }
9944                // Declined: no timing exists, so say so. Silence here is
9945                // what left `ffn` undecided for 9000 calls and cost a
9946                // failed device attempt on half of them.
9947                crate::gpu::probe_note_decline(crate::gpu::OpClass::Ffn);
9948            }
9949            crate::gpu::ProbeArm::CpuTimed => {
9950                let t0 = std::time::Instant::now();
9951                let out = crate::gpu::cpu_scope(|| dense_ffn_cpu(d, x, pool));
9952                crate::gpu::probe_record(crate::gpu::OpClass::Ffn, false, t0.elapsed());
9953                return out;
9954            }
9955            crate::gpu::ProbeArm::Cpu => {
9956                return crate::gpu::cpu_scope(|| dense_ffn_cpu(d, x, pool));
9957            }
9958        }
9959    }
9960    dense_ffn_cpu(d, x, pool)
9961}
9962
9963/// The pure-CPU dense-FFN body (also the fallback of every GPU refusal).
9964fn dense_ffn_cpu(d: &DenseFfn, x: &[f32], pool: Option<&Pool>) -> Vec<f32> {
9965    let inter = d.gate_proj.rows();
9966    FFN_SCRATCH.with(|s| {
9967        let mut s = s.borrow_mut();
9968        let [g, u, ..] = &mut *s;
9969        g.resize(inter, 0.0);
9970        // Fused gate+up+silu: one dispatch, no separate silu pass.
9971        // Falls back to matvec_many + silu loop for unsupported dtypes.
9972        if gate_topk() > 0 {
9973            // Gate first, select, and only then pay for `up`: the
9974            // measurement arm computes both and zeroes the losers, which
9975            // is the same arithmetic.
9976            u.resize(inter, 0.0);
9977            QTensor::matvec_many([&d.gate_proj, &d.up_proj], x, [g, u], pool);
9978            for i in 0..inter {
9979                g[i] = Act::Silu.combine(g[i], 1.0);
9980            }
9981            keep_top_k(g, gate_topk());
9982            for i in 0..inter {
9983                g[i] *= u[i];
9984            }
9985        } else if d.act == Act::Silu
9986            && QTensor::matvec_silu_mul(&d.gate_proj, &d.up_proj, x, g, pool)
9987        {
9988            // g now holds silu(gate)·up directly.
9989        } else {
9990            u.resize(inter, 0.0);
9991            // Multi-matrix job: gate+up under one pool dispatch.
9992            QTensor::matvec_many([&d.gate_proj, &d.up_proj], x, [g, u], pool);
9993            for i in 0..inter {
9994                g[i] = d.act.combine(g[i], u[i]);
9995            }
9996        }
9997        // DTG-MA bake probe (Patent 2): accumulate this layer's
9998        // per-neuron activation mass while a probe pass is active.
9999        // `CMF_FFN_PROBE_TOPK=k` switches the statistic from mass to a
10000        // HIT COUNT — how many tokens rank the neuron in their own top
10001        // k. Mass asks "how loud is this neuron overall", the count
10002        // asks "how often does this task actually need it", and the two
10003        // rank neurons differently whenever a few tokens are loud.
10004        FFN_PROBE.with(|pr| {
10005            if let Some(acc) = pr.borrow_mut().as_mut() {
10006                let li = crate::gpu::cur_layer();
10007                if li >= 0 {
10008                    if let Some(row) = acc.get_mut(li as usize) {
10009                        match probe_topk() {
10010                            0 if probe_sq() => {
10011                                for (a, &v) in row.iter_mut().zip(g.iter()) {
10012                                    *a += (v as f64) * (v as f64);
10013                                }
10014                            }
10015                            0 if probe_signed() => {
10016                                for (a, &v) in row.iter_mut().zip(g.iter()) {
10017                                    *a += v as f64;
10018                                }
10019                            }
10020                            0 => {
10021                                for (a, &v) in row.iter_mut().zip(g.iter()) {
10022                                    *a += (v as f64).abs();
10023                                }
10024                            }
10025                            k => {
10026                                let n = g.len();
10027                                let k = k.min(n);
10028                                let mut mag: Vec<f32> = g.iter().map(|v| v.abs()).collect();
10029                                let (_, kth, _) = mag.select_nth_unstable_by(k - 1, |a, b| {
10030                                    b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal)
10031                                });
10032                                let thr = *kth;
10033                                for (a, &v) in row.iter_mut().zip(g.iter()) {
10034                                    if v.abs() >= thr {
10035                                        *a += 1.0;
10036                                    }
10037                                }
10038                            }
10039                        }
10040                    }
10041                }
10042            }
10043        });
10044        if oracle_topk() > 0 {
10045            keep_top_k(g, oracle_topk());
10046        }
10047        {
10048            let li = crate::gpu::cur_layer();
10049            if li >= 0 {
10050                adump_row(li as usize, g);
10051            }
10052        }
10053        let mut out = attention::take_buf(d.down_proj.rows());
10054        d.down_proj.matvec(g, &mut out, pool);
10055        out
10056    })
10057}
10058
10059/// Online accumulators for the AWNP refit of a narrowed FFN.
10060///
10061/// The refit needs `Gss = A_SᵀA_S` and `YA = YᵀA_S` per layer, where `A_S`
10062/// are the calibration activations of the KEPT neurons and `Y` the full
10063/// FFN output. Both are small enough to hold; the thing that is not is
10064/// the activations they are built from — a 27B layer would dump a
10065/// gigabyte per thousand tokens. So they are accumulated as the
10066/// calibration runs and written once at the end.
10067///
10068/// `CMF_FFN_REFIT=<dir>` holds `support.<L>.u32` (a u32 count then the
10069/// kept indices) for every layer to accumulate; `CMF_FFN_REFIT_FROM/TO`
10070/// bound the layer span so the accumulators fit in RAM.
10071pub struct RefitAcc {
10072    pub support: Vec<u32>,
10073    pub gss: Vec<f32>,
10074    pub ya: Vec<f32>,
10075    pub hidden: usize,
10076    pub tokens: u64,
10077    /// Activations staged transposed ([ns, t] and [hidden, t]) until the
10078    /// batch is worth a GEMM. The product costs `ns²` to move and add
10079    /// REGARDLESS of how many tokens went into it, so folding 16 chunks
10080    /// into one call cuts that cost 16× — it was 15 TB of traffic per
10081    /// calibration pass at one call per 256 tokens.
10082    pub buf_g: Vec<f32>,
10083    pub buf_o: Vec<f32>,
10084    pub buf_t: usize,
10085}
10086
10087/// The product buffer is SHARED across layers — one 473 MB allocation,
10088/// not one per layer (that was 30 GB of nothing on a 64-layer model).
10089/// It lives under the same lock as the accumulators.
10090type RefitState = (std::collections::HashMap<usize, RefitAcc>, Vec<f32>);
10091
10092static REFIT: std::sync::OnceLock<Option<(String, std::sync::Mutex<RefitState>)>> =
10093    std::sync::OnceLock::new();
10094
10095/// Is an FFN probe accumulator installed on this thread? The fused GPU
10096/// FFN must decline while one is, or the probe silently measures zero.
10097fn ffn_probe_active() -> bool {
10098    FFN_PROBE.with(|p| p.borrow().is_some())
10099}
10100
10101fn refit_dir() -> Option<&'static (String, std::sync::Mutex<RefitState>)> {
10102    REFIT
10103        .get_or_init(|| {
10104            std::env::var("CMF_FFN_REFIT").ok().map(|d| {
10105                (
10106                    d,
10107                    std::sync::Mutex::new((std::collections::HashMap::new(), Vec::new())),
10108                )
10109            })
10110        })
10111        .as_ref()
10112}
10113
10114/// Accumulate one prefill panel into the layer's refit statistics.
10115fn refit_accumulate(
10116    li: usize,
10117    g: &[f32],
10118    b: usize,
10119    inter: usize,
10120    out: &[f32],
10121    hidden: usize,
10122    pool: Option<&Pool>,
10123) {
10124    let Some((dir, map)) = refit_dir() else {
10125        return;
10126    };
10127    static SPAN: std::sync::OnceLock<(usize, usize)> = std::sync::OnceLock::new();
10128    let (from, to) = *SPAN.get_or_init(|| {
10129        let g = |k: &str, d: usize| {
10130            std::env::var(k)
10131                .ok()
10132                .and_then(|v| v.parse().ok())
10133                .unwrap_or(d)
10134        };
10135        (
10136            g("CMF_FFN_REFIT_FROM", 0),
10137            g("CMF_FFN_REFIT_TO", usize::MAX),
10138        )
10139    });
10140    if li < from || li > to {
10141        return;
10142    }
10143    let mut guard = map.lock().unwrap();
10144    let (map, shared) = &mut *guard;
10145    let acc = match map.entry(li) {
10146        std::collections::hash_map::Entry::Occupied(e) => e.into_mut(),
10147        std::collections::hash_map::Entry::Vacant(e) => {
10148            let path = format!("{dir}/support.{li}.u32");
10149            let Ok(bytes) = std::fs::read(&path) else {
10150                eprintln!("refit: no {path} — layer {li} skipped");
10151                return;
10152            };
10153            let n = u32::from_le_bytes(bytes[0..4].try_into().unwrap()) as usize;
10154            let support: Vec<u32> = bytes[4..4 + n * 4]
10155                .chunks_exact(4)
10156                .map(|c| u32::from_le_bytes([c[0], c[1], c[2], c[3]]))
10157                .collect();
10158            eprintln!(
10159                "refit: layer {li} support {n} ({:.0} MB of accumulator)",
10160                (n * n + hidden * n) as f64 * 4.0 / 1e6
10161            );
10162            e.insert(RefitAcc {
10163                gss: vec![0.0; n * n],
10164                ya: vec![0.0; hidden * n],
10165                buf_g: Vec::new(),
10166                buf_o: Vec::new(),
10167                buf_t: 0,
10168                support,
10169                hidden,
10170                tokens: 0,
10171            })
10172        }
10173    };
10174    let ns = acc.support.len();
10175    // Stage this chunk transposed; the GEMM fires once the batch is full.
10176    let cap = refit_batch();
10177    if acc.buf_g.is_empty() {
10178        acc.buf_g = vec![0.0; ns * cap];
10179        acc.buf_o = vec![0.0; hidden * cap];
10180    }
10181    let take = b.min(cap - acc.buf_t);
10182    for t in 0..take {
10183        let col = acc.buf_t + t;
10184        for (j, &n) in acc.support.iter().enumerate() {
10185            acc.buf_g[j * cap + col] = g[t * inter + n as usize];
10186        }
10187        for h in 0..hidden {
10188            acc.buf_o[h * cap + col] = out[t * hidden + h];
10189        }
10190    }
10191    acc.buf_t += take;
10192    acc.tokens += take as u64;
10193    if acc.buf_t < cap {
10194        return;
10195    }
10196    let bt = acc.buf_t;
10197    acc.buf_t = 0;
10198    // The GEMM WRITES its C (it zeroes the accumulators it uses), so the
10199    // chunk product lands in scratch and is added on — the one thing that
10200    // silently turns a Gram over 13 000 tokens into a Gram over 256.
10201    // Both products are `C[n, m] += X[n, b] · Yᵀ[b, m]` with X and Y
10202    // stored row-major [·, b] — exactly `gemm_nt_f32`'s shape, so the
10203    // card does them when it is up (this is the whole calibration's
10204    // cost: O(|S|²) per token, 2.9 PFLOP for a 27B pass). The tiled CPU
10205    // loop stays as the fallback. Neither accumulates, so the product
10206    // lands in scratch and is added on.
10207    let RefitAcc {
10208        gss,
10209        ya,
10210        buf_g,
10211        buf_o,
10212        ..
10213    } = acc;
10214    let need = (ns * ns).max(hidden * ns);
10215    if shared.len() < need {
10216        shared.resize(need, 0.0);
10217    }
10218    let scratch = &mut shared[..];
10219    let _ = bt;
10220    if crate::gpu::gemm_nt_f32_transient(buf_g, buf_g, &mut scratch[..ns * ns], ns, cap, ns) {
10221        add_into(gss, &scratch[..ns * ns], pool);
10222        if crate::gpu::gemm_nt_f32_transient(
10223            buf_o,
10224            buf_g,
10225            &mut scratch[..hidden * ns],
10226            hidden,
10227            cap,
10228            ns,
10229        ) {
10230            add_into(ya, &scratch[..hidden * ns], pool);
10231        } else {
10232            accum_outer_t(ya, hidden, ns, cap, buf_o, buf_g, pool);
10233        }
10234    } else {
10235        accum_outer_t(gss, ns, ns, cap, buf_g, buf_g, pool);
10236        accum_outer_t(ya, hidden, ns, cap, buf_o, buf_g, pool);
10237    }
10238    // No zeroing: the batch is always filled exactly (cap is a multiple
10239    // of the prefill chunk), and a memset of 178 MB a layer would cost
10240    // more than the GEMM.
10241}
10242
10243/// `CMF_FFN_REFIT_BATCH` — tokens staged before each GEMM (default 4096).
10244fn refit_batch() -> usize {
10245    static B: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
10246    *B.get_or_init(|| {
10247        std::env::var("CMF_FFN_REFIT_BATCH")
10248            .ok()
10249            .and_then(|v| v.parse().ok())
10250            .unwrap_or(4096)
10251    })
10252}
10253
10254/// `c[m, n] += Σ_t left[m, t]·right[n, t]` — both operands transposed,
10255/// the CPU fallback for the staged batch.
10256fn accum_outer_t(
10257    c: &mut [f32],
10258    m: usize,
10259    n: usize,
10260    b: usize,
10261    left: &[f32],
10262    right: &[f32],
10263    pool: Option<&Pool>,
10264) {
10265    let ptr = SendMut(c.as_mut_ptr());
10266    let body = |i: usize| {
10267        let ptr = &ptr;
10268        let row = unsafe { std::slice::from_raw_parts_mut(ptr.0.add(i * n), n) };
10269        for t in 0..b {
10270            let a = left[i * b + t];
10271            if a == 0.0 {
10272                continue;
10273            }
10274            for (j, o) in row.iter_mut().enumerate() {
10275                *o += a * right[j * b + t];
10276            }
10277        }
10278    };
10279    match pool {
10280        Some(p) if m > 1 => p.run_rows(m, &|s, e| {
10281            for i in s..e {
10282                body(i);
10283            }
10284        }),
10285        _ => {
10286            for i in 0..m {
10287                body(i);
10288            }
10289        }
10290    }
10291}
10292
10293/// `dst += src`, spread over the pool — at 118 M floats a layer this is
10294/// not a loop to leave on one core.
10295fn add_into(dst: &mut [f32], src: &[f32], pool: Option<&Pool>) {
10296    let n = dst.len().min(src.len());
10297    match pool {
10298        Some(p) if n >= 1 << 16 => {
10299            let ptr = SendMut(dst.as_mut_ptr());
10300            let f = |s: usize, e: usize| {
10301                let ptr = &ptr;
10302                for blk in s..e {
10303                    let (a, b) = (blk * 4096, ((blk + 1) * 4096).min(n));
10304                    for i in a..b {
10305                        unsafe { *ptr.0.add(i) += src[i] };
10306                    }
10307                }
10308            };
10309            p.run_rows(n.div_ceil(4096), &f);
10310        }
10311        _ => {
10312            for (d, v) in dst.iter_mut().zip(&src[..n]) {
10313                *d += *v;
10314            }
10315        }
10316    }
10317}
10318
10319/// `c[m, n] += Σ_t left[t, m]·right[t, n]`, with `left` stored [m, t] and
10320/// `right` [t, n]. Tiled over the rows of `c` so a tile stays in cache
10321/// while each token's `right` row streams past it once, and parallel
10322/// over tiles.
10323fn accum_outer(
10324    c: &mut [f32],
10325    m: usize,
10326    n: usize,
10327    b: usize,
10328    left: &[f32],
10329    right: &[f32],
10330    pool: Option<&Pool>,
10331) {
10332    const TILE: usize = 32;
10333    let tiles = m.div_ceil(TILE);
10334    let cp = SendMut(c.as_mut_ptr());
10335    let body = |ti: usize| {
10336        let cp = &cp;
10337        let i0 = ti * TILE;
10338        let i1 = (i0 + TILE).min(m);
10339        for t in 0..b {
10340            let r = &right[t * n..t * n + n];
10341            for i in i0..i1 {
10342                let a = left[i * b + t];
10343                if a == 0.0 {
10344                    continue;
10345                }
10346                // SAFETY: tiles partition c's rows; workers never overlap.
10347                let row = unsafe { std::slice::from_raw_parts_mut(cp.0.add(i * n), n) };
10348                for (o, v) in row.iter_mut().zip(r) {
10349                    *o += a * *v;
10350                }
10351            }
10352        }
10353    };
10354    match pool {
10355        Some(p) if tiles > 1 => p.run_rows(tiles, &|s, e| {
10356            for ti in s..e {
10357                body(ti);
10358            }
10359        }),
10360        _ => {
10361            for ti in 0..tiles {
10362                body(ti);
10363            }
10364        }
10365    }
10366}
10367
10368/// Write what the calibration accumulated: `gss.<L>.f32` and `ya.<L>.f32`.
10369pub fn refit_flush() -> usize {
10370    let Some((dir, map)) = refit_dir() else {
10371        return 0;
10372    };
10373    let guard = map.lock().unwrap();
10374    let mut n = 0;
10375    for (li, acc) in guard.0.iter() {
10376        // A silently truncated write here is a Gram that reshapes to
10377        // nothing an hour later — say it out loud instead.
10378        let w = |name: &str, v: &[f32]| {
10379            let path = format!("{dir}/{name}.{li}.f32");
10380            let bytes: Vec<u8> = v.iter().flat_map(|x| x.to_le_bytes()).collect();
10381            match std::fs::write(&path, &bytes) {
10382                Ok(()) => {}
10383                Err(e) => eprintln!(
10384                    "refit: FAILED to write {path} ({} MB): {e}",
10385                    bytes.len() / 1_000_000
10386                ),
10387            }
10388        };
10389        w("gss", &acc.gss);
10390        w("ya", &acc.ya);
10391        println!(
10392            "refit L{li}: {} support, {} tokens, hidden {}",
10393            acc.support.len(),
10394            acc.tokens,
10395            acc.hidden
10396        );
10397        n += 1;
10398    }
10399    n
10400}
10401
10402/// `CMF_FFN_ADUMP=<prefix>` — append every probed token's FFN activation
10403/// row to `<prefix>.<layer>.f16`. The co-activation record: which
10404/// neurons fire together, which is what a tube has to group if a token
10405/// is ever going to open one tube instead of sixteen.
10406fn adump_row(li: usize, g: &[f32]) {
10407    use std::io::Write as _;
10408    static FILES: std::sync::OnceLock<
10409        Option<(
10410            String,
10411            std::sync::Mutex<std::collections::HashMap<usize, std::fs::File>>,
10412        )>,
10413    > = std::sync::OnceLock::new();
10414    let Some((prefix, map)) = FILES
10415        .get_or_init(|| {
10416            std::env::var("CMF_FFN_ADUMP")
10417                .ok()
10418                .map(|p| (p, std::sync::Mutex::new(std::collections::HashMap::new())))
10419        })
10420        .as_ref()
10421    else {
10422        return;
10423    };
10424    // `CMF_FFN_ADUMP_FROM/_TO` narrow the dump to a layer span, so a big
10425    // calibration run fits on disk in a few passes instead of one.
10426    static SPAN: std::sync::OnceLock<(usize, usize)> = std::sync::OnceLock::new();
10427    let (from, to) = *SPAN.get_or_init(|| {
10428        let g = |k: &str, d: usize| {
10429            std::env::var(k)
10430                .ok()
10431                .and_then(|v| v.parse().ok())
10432                .unwrap_or(d)
10433        };
10434        (
10435            g("CMF_FFN_ADUMP_FROM", 0),
10436            g("CMF_FFN_ADUMP_TO", usize::MAX),
10437        )
10438    });
10439    if li < from || li > to {
10440        return;
10441    }
10442    let mut map = map.lock().unwrap();
10443    let f = map.entry(li).or_insert_with(|| {
10444        std::fs::File::create(format!("{prefix}.{li}.f16")).expect("adump file")
10445    });
10446    let mut bytes = Vec::with_capacity(g.len() * 2);
10447    for v in g {
10448        bytes.extend_from_slice(&cortiq_core::quant::f32_to_f16(*v).to_le_bytes());
10449    }
10450    let _ = f.write_all(&bytes);
10451}
10452
10453/// `CMF_FFN_ORACLE_TOPK` — keep only the k largest |silu(g)·u| of each
10454/// token and zero the rest. Not a serving mode: it is the CEILING of
10455/// contextual sparsity — what a per-token router would be chasing —
10456/// measured by cheating, since the selection reads the very activations
10457/// it would have to predict.
10458fn oracle_topk() -> usize {
10459    static K: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
10460    *K.get_or_init(|| {
10461        std::env::var("CMF_FFN_ORACLE_TOPK")
10462            .ok()
10463            .and_then(|v| v.parse().ok())
10464            .unwrap_or(0)
10465    })
10466}
10467
10468/// `CMF_FFN_GATE_TOPK` — the REALIZABLE cousin of the oracle: rank the
10469/// neurons by their gate alone (which the kernel has computed anyway
10470/// before it reads `up`), keep the k best, and drop the rest. Every
10471/// dropped neuron's `up` row and `down` column stay unread, so this is
10472/// the sparsity a serving path can actually take without a router.
10473fn gate_topk() -> usize {
10474    static K: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
10475    *K.get_or_init(|| {
10476        std::env::var("CMF_FFN_GATE_TOPK")
10477            .ok()
10478            .and_then(|v| v.parse().ok())
10479            .unwrap_or(0)
10480    })
10481}
10482
10483/// `CMF_FFN_GATE_BLOCK` — select in blocks of B neurons instead of one
10484/// by one. A scattered per-neuron choice cannot be read efficiently (a
10485/// row at a time, no prefetch runway); a block of 32 is a contiguous
10486/// 32-row slab of `up` and of the transposed `down`, which the ordinary
10487/// kernels stream. The question the measurement answers is what the
10488/// block costs in quality.
10489fn gate_block() -> usize {
10490    static B: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
10491    *B.get_or_init(|| {
10492        std::env::var("CMF_FFN_GATE_BLOCK")
10493            .ok()
10494            .and_then(|v| v.parse().ok())
10495            .unwrap_or(1)
10496    })
10497}
10498
10499/// Zero all but the `k` largest BLOCKS (by summed square) of a row.
10500fn keep_top_blocks(g: &mut [f32], keep_n: usize, block: usize) {
10501    let n = g.len();
10502    let nb = n.div_ceil(block);
10503    let kb = (keep_n.div_ceil(block)).clamp(1, nb);
10504    if kb >= nb {
10505        return;
10506    }
10507    let mut score: Vec<f32> = (0..nb)
10508        .map(|b| {
10509            g[b * block..((b + 1) * block).min(n)]
10510                .iter()
10511                .map(|v| v * v)
10512                .sum::<f32>()
10513        })
10514        .collect();
10515    let mut ord = score.clone();
10516    let (_, kth, _) = ord.select_nth_unstable_by(kb - 1, |a, b| {
10517        b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal)
10518    });
10519    let thr = *kth;
10520    for b in 0..nb {
10521        if score[b] < thr {
10522            g[b * block..((b + 1) * block).min(n)].fill(0.0);
10523        }
10524    }
10525    score.clear();
10526}
10527
10528/// Zero all but the `k` largest magnitudes of one token's activation row.
10529fn keep_top_k(g: &mut [f32], k: usize) {
10530    if gate_block() > 1 {
10531        return keep_top_blocks(g, k, gate_block());
10532    }
10533    let n = g.len();
10534    if k == 0 || k >= n {
10535        return;
10536    }
10537    let mut mag: Vec<f32> = g.iter().map(|v| v.abs()).collect();
10538    let (_, kth, _) = mag.select_nth_unstable_by(k - 1, |a, b| {
10539        b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal)
10540    });
10541    let thr = *kth;
10542    for v in g.iter_mut() {
10543        if v.abs() < thr {
10544            *v = 0.0;
10545        }
10546    }
10547}
10548
10549/// `CMF_FFN_PROBE_SQ` — accumulate Σa², so the dump divided by the token
10550/// count and square-rooted is the RMS activation trace Patent 12 weights
10551/// its matrices by.
10552fn probe_sq() -> bool {
10553    static S: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
10554    *S.get_or_init(|| std::env::var("CMF_FFN_PROBE_SQ").is_ok())
10555}
10556
10557/// `CMF_FFN_PROBE_SIGNED` — accumulate the SIGNED activation sum
10558/// instead of its magnitude: what a dropped neuron contributes ON
10559/// AVERAGE, which is the bias a narrowed FFN can add back for free.
10560fn probe_signed() -> bool {
10561    static S: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
10562    *S.get_or_init(|| std::env::var("CMF_FFN_PROBE_SIGNED").is_ok())
10563}
10564
10565/// `CMF_FFN_MEANFILL=<file>` — a masked-out neuron contributes its MEAN
10566/// activation instead of zero (`u32 layers, u32 inter, f32[…]`, the mass
10567/// dump layout, holding per-neuron means). Dropping a neuron outright
10568/// also drops its average contribution, which shifts the layer output by
10569/// a constant; filling the mean back is one add per layer and costs no
10570/// bytes off the bus. This is the measurement arm — in a tube file the
10571/// same correction ships as a per-task bias vector.
10572fn meanfill() -> Option<&'static (usize, Vec<f32>)> {
10573    static M: std::sync::OnceLock<Option<(usize, Vec<f32>)>> = std::sync::OnceLock::new();
10574    M.get_or_init(|| {
10575        let p = std::env::var("CMF_FFN_MEANFILL").ok()?;
10576        let b = std::fs::read(&p).ok()?;
10577        let inter = u32::from_le_bytes(b[4..8].try_into().ok()?) as usize;
10578        let vals: Vec<f32> = b[8..]
10579            .chunks_exact(4)
10580            .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]]))
10581            .collect();
10582        eprintln!("meanfill: {} value(s), inter {inter}", vals.len());
10583        Some((inter, vals))
10584    })
10585    .as_ref()
10586}
10587
10588/// `CMF_FFN_PROBE_TOPK` — 0 (default) = accumulate mass, k>0 = count
10589/// how often a neuron lands in a token's top k.
10590fn probe_topk() -> usize {
10591    static K: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
10592    *K.get_or_init(|| {
10593        std::env::var("CMF_FFN_PROBE_TOPK")
10594            .ok()
10595            .and_then(|v| v.parse().ok())
10596            .unwrap_or(0)
10597    })
10598}
10599
10600thread_local! {
10601    /// DTG-MA activation probe: per-layer per-neuron Σ|silu(g)·u|
10602    /// accumulator, alive only during `Pipeline::probe_ffn_mass`.
10603    static FFN_PROBE: std::cell::RefCell<Option<Vec<Vec<f64>>>> =
10604        const { std::cell::RefCell::new(None) };
10605}
10606
10607/// Per-token structured sparsity, paid for in bytes.
10608///
10609/// The gate is the cheapest third of an FFN and it already says which
10610/// neurons matter: `silu(gate)` near zero means the neuron contributes
10611/// nothing whatever `up` says. So compute every gate, keep the `k`
10612/// loudest, and read ONLY those neurons' `up` rows and `down` rows —
10613/// the latter needs `down_proj` stored transposed, otherwise a neuron's
10614/// down weights are a strided column and "reading only those" costs a
10615/// full cache line each.
10616///
10617/// Returns `None` when the file has no transposed `down` (the caller
10618/// then runs the ordinary dense path).
10619fn dense_ffn_dynamic(d: &DenseFfn, x: &[f32], pool: Option<&Pool>, k: usize) -> Option<Vec<f32>> {
10620    let dt = d.down_t.as_ref()?;
10621    let inter = d.gate_proj.rows();
10622    let hidden = dt.cols();
10623    if k == 0 || k >= inter || d.act != Act::Silu {
10624        return None;
10625    }
10626    DYN_SCRATCH.with(|sc| {
10627        let mut sc = sc.borrow_mut();
10628        let DynScratch {
10629            g,
10630            mag,
10631            live,
10632            parts,
10633        } = &mut *sc;
10634        g.resize(inter, 0.0);
10635        d.gate_proj.matvec(x, g, pool);
10636        for v in g.iter_mut() {
10637            *v = inference::silu(*v);
10638        }
10639        // The k-th largest |silu(gate)| is the threshold; ties keep more,
10640        // which is the safe side.
10641        mag.clear();
10642        mag.extend(g.iter().map(|v| v.abs()));
10643        let (_, kth, _) = mag.select_nth_unstable_by(k - 1, |a, b| {
10644            b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal)
10645        });
10646        let thr = *kth;
10647        live.clear();
10648        live.extend((0..inter as u32).filter(|&n| g[n as usize].abs() >= thr));
10649        let mut out = vec![0.0f32; hidden];
10650        match pool {
10651            Some(p) if live.len() >= 64 => {
10652                let nw = p.n_workers() + 1;
10653                parts.clear();
10654                parts.resize(nw * hidden, 0.0);
10655                let ptr = SendMut(parts.as_mut_ptr());
10656                let n = live.len();
10657                let live_ref: &[u32] = live;
10658                let g_ref: &[f32] = g;
10659                p.run(&|w, workers| {
10660                    let chunk = n.div_ceil(workers);
10661                    let (s, e) = (w * chunk, ((w + 1) * chunk).min(n));
10662                    if s >= e {
10663                        return;
10664                    }
10665                    WORKER_SCRATCH.with(|ws| {
10666                        let mut ws = ws.borrow_mut();
10667                        let [scratch, acc] = &mut *ws;
10668                        scratch.resize(hidden.max(x.len()), 0.0);
10669                        acc.clear();
10670                        acc.resize(hidden, 0.0);
10671                        for (o, &nrm) in live_ref[s..e].iter().enumerate() {
10672                            // One neuron of runway: the next row's lines
10673                            // start moving while this one is multiplied.
10674                            if let Some(&nx) = live_ref[s..e].get(o + 1) {
10675                                d.up_proj.prefetch_row(nx as usize);
10676                                dt.prefetch_row(nx as usize);
10677                            }
10678                            let idx = nrm as usize;
10679                            let up = d.up_proj.row_dot(idx, x, scratch);
10680                            let a = g_ref[idx] * up;
10681                            if a != 0.0 {
10682                                dt.add_row_scaled(idx, a, acc, scratch);
10683                            }
10684                        }
10685                        for (j, v) in acc.iter().enumerate() {
10686                            unsafe { *ptr.at(w * hidden + j) = *v };
10687                        }
10688                    });
10689                });
10690                for w in 0..nw {
10691                    for (j, o) in out.iter_mut().enumerate() {
10692                        *o += parts[w * hidden + j];
10693                    }
10694                }
10695            }
10696            _ => {
10697                WORKER_SCRATCH.with(|ws| {
10698                    let mut ws = ws.borrow_mut();
10699                    let [scratch, _acc] = &mut *ws;
10700                    scratch.resize(hidden.max(x.len()), 0.0);
10701                    for &nrm in live.iter() {
10702                        let idx = nrm as usize;
10703                        let up = d.up_proj.row_dot(idx, x, scratch);
10704                        let a = g[idx] * up;
10705                        if a != 0.0 {
10706                            dt.add_row_scaled(idx, a, &mut out, scratch);
10707                        }
10708                    }
10709                });
10710            }
10711        }
10712        Some(out)
10713    })
10714}
10715
10716/// Caller-side scratch of the dynamic path — one allocation per thread,
10717/// not one per layer per token (that alone cost a third of the decode).
10718struct DynScratch {
10719    g: Vec<f32>,
10720    mag: Vec<f32>,
10721    live: Vec<u32>,
10722    parts: Vec<f32>,
10723}
10724
10725thread_local! {
10726    static DYN_SCRATCH: std::cell::RefCell<DynScratch> = const {
10727        std::cell::RefCell::new(DynScratch {
10728            g: Vec::new(),
10729            mag: Vec::new(),
10730            live: Vec::new(),
10731            parts: Vec::new(),
10732        })
10733    };
10734    /// Pool-worker scratch: the row buffer and this worker's partial sum.
10735    static WORKER_SCRATCH: std::cell::RefCell<[Vec<f32>; 2]> =
10736        const { std::cell::RefCell::new([Vec::new(), Vec::new()]) };
10737}
10738
10739/// `dense_ffn_cpu` with a per-visit mask landing on the activations —
10740/// the masked-inference fast path's decode arm. Full fused quant
10741/// compute, closed neurons zeroed before down: arithmetically the
10742/// pruned network, no dequant, no weight bytes touched.
10743fn dense_ffn_masked(d: &DenseFfn, x: &[f32], pool: Option<&Pool>, mask_row: &[u8]) -> Vec<f32> {
10744    let inter = d.gate_proj.rows();
10745    FFN_SCRATCH.with(|s| {
10746        let mut s = s.borrow_mut();
10747        let [g, u, ..] = &mut *s;
10748        g.resize(inter, 0.0);
10749        if d.act == Act::Silu && QTensor::matvec_silu_mul(&d.gate_proj, &d.up_proj, x, g, pool) {
10750            // g holds silu(gate)·up.
10751        } else {
10752            u.resize(inter, 0.0);
10753            QTensor::matvec_many([&d.gate_proj, &d.up_proj], x, [g, u], pool);
10754            for i in 0..inter {
10755                g[i] = d.act.combine(g[i], u[i]);
10756            }
10757        }
10758        zero_masked_cols(g, 1, inter, mask_row);
10759        let mut out = attention::take_buf(d.down_proj.rows());
10760        d.down_proj.matvec(g, &mut out, pool);
10761        out
10762    })
10763}
10764
10765/// Dense FFN as one GPU submission via the MoE block path (single
10766/// expert, weight 1.0): gate → silu·up → down chained in one command
10767/// buffer, intermediate activations device-resident. None → weights
10768/// not q8-mapped in the primary shard / over the VRAM budget / backend
10769/// refusal → honest CPU path.
10770fn dense_ffn_gpu(d: &DenseFfn, x: &[f32], _pool: Option<&Pool>) -> Option<Vec<f32>> {
10771    // The GPU block hardcodes SiLU; GeLU FFNs (Gemma) stay on CPU.
10772    if d.act != Act::Silu {
10773        return None;
10774    }
10775    // Threshold: tiny FFNs are not worth a submission (q1 excepted —
10776    // see the caller's gate).
10777    if d.gate_proj.rows() < crate::gpu::min_rows() && !d.gate_proj.is_q1() {
10778        return None;
10779    }
10780    let mut jobs: Vec<crate::gpu::MoeJob> = Vec::with_capacity(1);
10781    let mut model_ref = None;
10782    moe_push_job(d, x, 1.0, &mut jobs, &mut model_ref)?;
10783    let model = model_ref?;
10784    let hidden = jobs[0].down.1;
10785    let mut out = attention::take_buf(hidden);
10786    if crate::gpu::moe_block(&model, &jobs, &mut out) {
10787        Some(out)
10788    } else {
10789        let mut out = out;
10790        attention::recycle_buf(&mut out);
10791        None
10792    }
10793}
10794
10795/// q8-mapped primary-shard tensor parts for a GPU job: q8_2f carries
10796/// its column field, q8_row runs with empty col slices (the backend
10797/// skips the multiply). Shared by the MoE block and the dense-FFN
10798/// single-job path.
10799#[allow(clippy::type_complexity)]
10800#[allow(clippy::type_complexity)]
10801pub(crate) fn moe_parts(
10802    t: &QTensor,
10803) -> Option<(
10804    &std::sync::Arc<cortiq_core::CmfModel>,
10805    usize,
10806    usize,
10807    usize,
10808    &[f32],
10809    &[f32],
10810    bool,
10811    bool,
10812    bool,
10813)> {
10814    match t {
10815        QTensor::Mapped {
10816            model,
10817            idx,
10818            dtype: dt @ (cortiq_core::TensorDtype::Q8_2f | cortiq_core::TensorDtype::Q8Row),
10819            rows,
10820            cols,
10821            row_scale,
10822            col_field,
10823            ..
10824        } if (*dt == cortiq_core::TensorDtype::Q8Row) || !col_field.is_empty() => Some((
10825            model, *idx, *rows, *cols, row_scale, col_field, false, false, false,
10826        )),
10827        // q1: tile-embedded scales — empty rs/col slices, raw xs.
10828        QTensor::Mapped {
10829            model,
10830            idx,
10831            dtype: cortiq_core::TensorDtype::Q1,
10832            rows,
10833            cols,
10834            ..
10835        } => Some((
10836            model,
10837            *idx,
10838            *rows,
10839            *cols,
10840            &[][..],
10841            &[][..],
10842            true,
10843            false,
10844            false,
10845        )),
10846        // q4_tiled: 18-byte tiles with embedded f16 scales — raw xs.
10847        QTensor::Mapped {
10848            model,
10849            idx,
10850            dtype: cortiq_core::TensorDtype::Q4Tiled,
10851            rows,
10852            cols,
10853            ..
10854        } => Some((
10855            model,
10856            *idx,
10857            *rows,
10858            *cols,
10859            &[][..],
10860            &[][..],
10861            false,
10862            true,
10863            false,
10864        )),
10865        // q4tp: same raw-xs contract, different stride and scale plane.
10866        QTensor::Mapped {
10867            model,
10868            idx,
10869            dtype: cortiq_core::TensorDtype::Q4TiledP,
10870            rows,
10871            cols,
10872            ..
10873        } => Some((
10874            model,
10875            *idx,
10876            *rows,
10877            *cols,
10878            &[][..],
10879            &[][..],
10880            false,
10881            true,
10882            false,
10883        )),
10884        // q2tp: the 2-bit expert plane of the mixed profile — q4 family
10885        // for stride bookkeeping, flagged q2 so the trio validation can
10886        // demand a q4tp down.
10887        QTensor::Mapped {
10888            model,
10889            idx,
10890            dtype: cortiq_core::TensorDtype::Q2TiledP,
10891            rows,
10892            cols,
10893            ..
10894        } => Some((
10895            model,
10896            *idx,
10897            *rows,
10898            *cols,
10899            &[][..],
10900            &[][..],
10901            false,
10902            true,
10903            true,
10904        )),
10905        _ => None,
10906    }
10907}
10908
10909/// Map a softmax-router MoE onto the Metal token graph's contract:
10910/// f32 router, gated shared expert, experts uniformly q4tp (or the
10911/// mixed profile: q2tp gate/up over a q4tp down). Sigmoid/bias/τ
10912/// routers, masks, per-expert scales and Gemma's router-input norm
10913/// refuse here — those semantics stay on the CPU path.
10914#[cfg(target_os = "macos")]
10915fn metal_moe_graph_parts(m: &MoeFfn, hidden: usize) -> Option<crate::gpu::GpuMoe<'_>> {
10916    if m.router_sigmoid
10917        || m.router_input_norm
10918        || m.expert_bias.is_some()
10919        || m.route_tau.is_some()
10920        || m.mask.is_some()
10921        || m.per_expert_scale.is_some()
10922        || m.experts.is_empty()
10923        || m.top_k == 0
10924        || m.resonance.is_some()
10925    {
10926        return None;
10927    }
10928    // The select kernel hard-codes the gated shared expert; an
10929    // ungated one would need its own weight-1 slot.
10930    let (sh, sg) = match &m.shared {
10931        Some((sh, Some(sg))) => (sh, sg),
10932        _ => return None,
10933    };
10934    let (rf, rr, rc) = m.router.f32_parts()?;
10935    if rr != m.experts.len() || rc != hidden {
10936        return None;
10937    }
10938    let (sf, sr, sc) = sg.f32_parts()?;
10939    if sr * sc != hidden {
10940        return None;
10941    }
10942    let inter = m.experts[0].gate_proj.rows();
10943    // The first expert's gate decides the profile; every trio (shared
10944    // included) must agree — the jobs ladder flips ONE kernel for all.
10945    let gu_q2 = m.experts[0].gate_proj.mapped_q2tp().is_some();
10946    let trio = |e: &DenseFfn| -> Option<(usize, usize, usize)> {
10947        if e.act != Act::Silu
10948            || e.gate_proj.rows() != inter
10949            || e.gate_proj.cols() != hidden
10950            || e.up_proj.rows() != inter
10951            || e.up_proj.cols() != hidden
10952            || e.down_proj.rows() != hidden
10953            || e.down_proj.cols() != inter
10954        {
10955            return None;
10956        }
10957        let pick = |t: &QTensor| -> Option<usize> {
10958            if gu_q2 {
10959                t.mapped_q2tp().map(|(_, i)| i)
10960            } else {
10961                t.mapped_q4tp().map(|(_, i)| i)
10962            }
10963        };
10964        Some((
10965            pick(&e.gate_proj)?,
10966            pick(&e.up_proj)?,
10967            e.down_proj.mapped_q4tp().map(|(_, i)| i)?,
10968        ))
10969    };
10970    let experts = m.experts.iter().map(trio).collect::<Option<Vec<_>>>()?;
10971    let shared = trio(sh)?;
10972    Some(crate::gpu::GpuMoe {
10973        router: rf,
10974        sgate: sf,
10975        experts,
10976        shared,
10977        n_exp: m.experts.len(),
10978        top_k: m.top_k,
10979        inter,
10980        norm_topk: m.norm_topk_prob,
10981        route_scale: m.routed_scaling,
10982        gu_q2,
10983    })
10984}
10985
10986/// Build one gate/up/down GPU job from three tensors. `moe_push_job` is the
10987/// DenseFfn-shaped caller; architectures that keep their experts in their own
10988/// structs (DeepSeek-V4) come here directly.
10989pub(crate) fn moe_push_job_parts<'a>(
10990    gate: &'a QTensor,
10991    up: &'a QTensor,
10992    down: &'a QTensor,
10993    x: &[f32],
10994    w: f32,
10995    swiglu_limit: f32,
10996    jobs: &mut Vec<crate::gpu::MoeJob<'a>>,
10997    model_ref: &mut Option<std::sync::Arc<cortiq_core::CmfModel>>,
10998) -> Option<()> {
10999    use crate::qtensor::prescale;
11000    let (gm, gi, gr, gc, grs, gcf, gq1, gq4, gq2) = moe_parts(gate)?;
11001    let (_, ui, ur, uc, urs, ucf, uq1, uq4, uq2) = moe_parts(up)?;
11002    let (_, di, dr, dc, drs, dcf, dq1, dq4, dq2) = moe_parts(down)?;
11003    if gq1 != uq1 || uq1 != dq1 || gq4 != uq4 || uq4 != dq4 || gq2 != uq2 {
11004        return None; // mixed-dtype trio — honest CPU path
11005    }
11006    // The 2-bit profile is gate/up q2tp over a PLAIN q4tp down; any other
11007    // 2-bit arrangement stays on the CPU.
11008    if gq2 && (dq2 || !dq4 || down.mapped_q4tp().is_none()) {
11009        return None;
11010    }
11011    if !gq2 && dq2 {
11012        return None;
11013    }
11014    model_ref.get_or_insert_with(|| gm.clone());
11015    let dt = |cf: &[f32]| {
11016        if cf.is_empty() {
11017            cortiq_core::TensorDtype::Q8Row
11018        } else {
11019            cortiq_core::TensorDtype::Q8_2f
11020        }
11021    };
11022    jobs.push(crate::gpu::MoeJob {
11023        gate: (gi, gr, gc, grs),
11024        up: (ui, ur, uc, urs),
11025        down: (di, dr, dc, drs),
11026        xs_gate: prescale(x, gcf, dt(gcf)).into_owned(),
11027        xs_up: prescale(x, ucf, dt(ucf)).into_owned(),
11028        down_col: dcf,
11029        w,
11030        q1: gq1,
11031        q4t: gq4 && !gq2 && gate.mapped_q4tp().is_none(),
11032        q4tp: gq4 && (gq2 || gate.mapped_q4tp().is_some()),
11033        gu_q2: gq2,
11034        swiglu_limit,
11035    });
11036    Some(())
11037}
11038
11039/// Build one gate/up/down GPU job (see `moe_parts`).
11040fn moe_push_job<'a>(
11041    d: &'a DenseFfn,
11042    x: &[f32],
11043    w: f32,
11044    jobs: &mut Vec<crate::gpu::MoeJob<'a>>,
11045    model_ref: &mut Option<std::sync::Arc<cortiq_core::CmfModel>>,
11046) -> Option<()> {
11047    use crate::qtensor::prescale;
11048    if d.act != Act::Silu {
11049        return None; // GPU block hardcodes SiLU
11050    }
11051    let (gm, gi, gr, gc, grs, gcf, gq1, gq4, gq2) = moe_parts(&d.gate_proj)?;
11052    let (_, ui, ur, uc, urs, ucf, uq1, uq4, uq2) = moe_parts(&d.up_proj)?;
11053    let (_, di, dr, dc, drs, dcf, dq1, dq4, dq2) = moe_parts(&d.down_proj)?;
11054    if gq1 != uq1 || uq1 != dq1 || gq4 != uq4 || uq4 != dq4 || gq2 != uq2 {
11055        return None; // mixed-dtype trio — honest CPU path
11056    }
11057    if gq2 && (dq2 || !dq4 || d.down_proj.mapped_q4tp().is_none()) {
11058        return None;
11059    }
11060    if !gq2 && dq2 {
11061        return None;
11062    }
11063    model_ref.get_or_insert_with(|| gm.clone());
11064    let gdt = if gcf.is_empty() {
11065        cortiq_core::TensorDtype::Q8Row
11066    } else {
11067        cortiq_core::TensorDtype::Q8_2f
11068    };
11069    let udt = if ucf.is_empty() {
11070        cortiq_core::TensorDtype::Q8Row
11071    } else {
11072        cortiq_core::TensorDtype::Q8_2f
11073    };
11074    jobs.push(crate::gpu::MoeJob {
11075        gate: (gi, gr, gc, grs),
11076        up: (ui, ur, uc, urs),
11077        down: (di, dr, dc, drs),
11078        xs_gate: prescale(x, gcf, gdt).into_owned(),
11079        xs_up: prescale(x, ucf, udt).into_owned(),
11080        down_col: dcf,
11081        w,
11082        q1: gq1,
11083        q4t: gq4 && !gq2 && d.gate_proj.mapped_q4tp().is_none(),
11084        q4tp: gq4 && (gq2 || d.gate_proj.mapped_q4tp().is_some()),
11085        gu_q2: gq2,
11086        swiglu_limit: 0.0,
11087    });
11088    Some(())
11089}
11090
11091/// Sparse dense-FFN directly on QUANTIZED weights (mask × mmap): reads
11092/// ONLY the active neurons' gate/up rows and down columns from the mmap
11093/// — no full-matrix dequant, no f32 model copy. This is what lets a
11094/// masked big model run at quantized RSS (the historical mask path
11095/// forced the whole model to f32). Semantics identical to the f32
11096/// sparse path within quant tolerance.
11097fn sparse_ffn_quant(
11098    d: &DenseFfn,
11099    x: &[f32],
11100    active: &[u16],
11101    hidden: usize,
11102    pool: Option<&Pool>,
11103) -> Vec<f32> {
11104    let n = active.len();
11105    let inter = d.gate_proj.rows();
11106    let mut act = vec![0.0f32; n];
11107    // Scratch is needed if EITHER projection is group-packed (q4/vbit);
11108    // gate/up normally share a dtype but sizing on both is robust.
11109    let need_scratch = !(d.gate_proj.sparse_col_ok() && d.up_proj.sparse_col_ok());
11110    let compute = |ai: usize| -> f32 {
11111        let idx = active[ai] as usize;
11112        if idx >= inter {
11113            return 0.0; // defensive parity with the f32 sparse path
11114        }
11115        let mut s = if need_scratch {
11116            vec![0.0f32; hidden]
11117        } else {
11118            Vec::new()
11119        };
11120        let gate = d.gate_proj.row_dot(idx, x, &mut s);
11121        let up = d.up_proj.row_dot(idx, x, &mut s);
11122        d.act.combine(gate, up)
11123    };
11124    match pool {
11125        Some(p) if n >= 256 => {
11126            let ptr = SendMut(act.as_mut_ptr());
11127            p.run(&|widx, nw| {
11128                let chunk = n.div_ceil(nw);
11129                let (s, e) = (widx * chunk, ((widx + 1) * chunk).min(n));
11130                for ai in s..e {
11131                    unsafe { *ptr.at(ai) = compute(ai) };
11132                }
11133            });
11134        }
11135        _ => {
11136            for (ai, a) in act.iter_mut().enumerate() {
11137                *a = compute(ai);
11138            }
11139        }
11140    }
11141    // Scatter through active down columns (reads only those columns).
11142    let mut out = vec![0.0f32; hidden];
11143    for (ai, &idx) in active.iter().enumerate() {
11144        let w = act[ai];
11145        if w.abs() >= 1e-12 && (idx as usize) < inter {
11146            d.down_proj.add_col_scaled(idx as usize, w, &mut out);
11147        }
11148    }
11149    out
11150}
11151
11152/// Test-only re-export of the private sparse-quant FFN (mask × mmap gate).
11153#[doc(hidden)]
11154pub fn sparse_ffn_quant_for_test(
11155    d: &DenseFfn,
11156    x: &[f32],
11157    active: &[u16],
11158    hidden: usize,
11159) -> Vec<f32> {
11160    sparse_ffn_quant(d, x, active, hidden, None)
11161}
11162
11163/// Dequantize a DenseFfn's three matrices to f32 (transient; only the
11164/// q4/vbit-masked fallback uses it — the memory-lean path is
11165/// sparse_ffn_quant). Reuses row_f32 row-by-row.
11166fn dequant_dense_f32(d: &DenseFfn) -> (Vec<f32>, Vec<f32>, Vec<f32>) {
11167    let deq = |t: &QTensor| -> Vec<f32> {
11168        let (rows, cols) = (t.rows(), t.cols());
11169        let mut out = vec![0.0f32; rows * cols];
11170        for r in 0..rows {
11171            t.row_f32(r, &mut out[r * cols..(r + 1) * cols]);
11172        }
11173        out
11174    };
11175    (deq(&d.gate_proj), deq(&d.up_proj), deq(&d.down_proj))
11176}
11177
11178/// Pointer wrapper for the worker-pool scatter (same pattern as qtensor).
11179struct SendMut(*mut f32);
11180unsafe impl Send for SendMut {}
11181unsafe impl Sync for SendMut {}
11182impl SendMut {
11183    #[inline]
11184    // Deliberate unsynchronized scatter: pool workers write disjoint indices
11185    // in parallel, so returning `&mut` from `&self` is intentional here.
11186    #[allow(clippy::mut_from_ref)]
11187    unsafe fn at(&self, i: usize) -> &mut f32 {
11188        unsafe { &mut *self.0.add(i) }
11189    }
11190}
11191
11192/// Router → (selected experts in torch.topk order, per-expert score
11193/// vector, normalizer). The final weight of expert `e` is `p[e] / wsum`.
11194///
11195/// Two regimes share this. Qwen: softmax over ALL experts, top-k of the
11196/// probabilities, optional renorm — `router_sigmoid=false`, no bias,
11197/// scale 1 → bit-identical to the historical path. LFM2-MoE /
11198/// DeepSeek-V3 `noaux_tc`: per-expert sigmoid scores, an optional
11199/// selection bias (top-k CHOICE only; weights stay unbiased), a 1e-6 renorm
11200/// floor and a routed scale.
11201pub(crate) fn moe_route(
11202    logits: &[f32],
11203    m: &MoeFfn,
11204    allowed: Option<&[bool]>,
11205) -> (Vec<usize>, Vec<f32>, f32) {
11206    let ne = logits.len();
11207    let p: Vec<f32> = if m.router_sigmoid {
11208        logits.iter().map(|&l| 1.0 / (1.0 + (-l).exp())).collect()
11209    } else {
11210        let mx = logits.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
11211        let mut e: Vec<f32> = logits.iter().map(|&l| (l - mx).exp()).collect();
11212        let s: f32 = e.iter().sum();
11213        for v in &mut e {
11214            *v /= s;
11215        }
11216        e
11217    };
11218    // Expert restriction: the static env mask (CMF_MOE_MASK) AND the
11219    // active task mask's expert fields (spec §5) both narrow the
11220    // candidate set; selection happens over the admitted experts only.
11221    // With norm_topk the kept weights renormalize below; without it
11222    // the excluded mass is honestly dropped.
11223    let admit = |e: usize| {
11224        m.mask.as_ref().is_none_or(|mk| mk[e])
11225            && allowed.is_none_or(|a| a.get(e).copied().unwrap_or(false))
11226    };
11227    let mut idx: Vec<usize> = (0..ne).filter(|&e| admit(e)).collect();
11228    // Descending by selection score, lower index wins ties (torch.topk).
11229    match &m.expert_bias {
11230        Some(b) => idx.sort_unstable_by(|&x, &y| {
11231            (p[y] + b[y])
11232                .partial_cmp(&(p[x] + b[x]))
11233                .unwrap()
11234                .then(x.cmp(&y))
11235        }),
11236        None => idx.sort_unstable_by(|&x, &y| p[y].partial_cmp(&p[x]).unwrap().then(x.cmp(&y))),
11237    }
11238    idx.truncate(m.top_k);
11239    // Adaptive τ-routing: trim the tail experts once the kept mass is
11240    // enough. wsum below renormalizes over the KEPT set, so the output
11241    // stays a proper weighted average.
11242    if let Some(tau) = m.route_tau {
11243        let total: f32 = idx.iter().map(|&e| p[e]).sum();
11244        if total > 0.0 {
11245            let mut acc = 0.0f32;
11246            let mut keep = idx.len();
11247            for (i, &e) in idx.iter().enumerate() {
11248                acc += p[e];
11249                if acc >= tau * total {
11250                    keep = i + 1;
11251                    break;
11252                }
11253            }
11254            idx.truncate(keep);
11255        }
11256    }
11257    let wsum: f32 = if m.norm_topk_prob {
11258        let s: f32 = idx.iter().map(|&e| p[e]).sum();
11259        // LFM2 floors the denom (matches HF `+ 1e-6`); the softmax path's
11260        // probs already sum near 1, so it stays exactly as before.
11261        (if m.router_sigmoid { s + 1e-6 } else { s }) / m.routed_scaling
11262    } else {
11263        1.0 / m.routed_scaling
11264    };
11265    (idx, p, wsum)
11266}
11267
11268/// See the call site: one `layer:e1,e2,…` line per routed token.
11269fn moe_trace(idx: &[usize]) {
11270    moe_trace_at(crate::gpu::cur_layer() as i32, idx)
11271}
11272
11273/// The same, for callers that know their layer (DSV4 owns its layers and
11274/// never sets the pipeline's current-layer marker).
11275pub(crate) fn moe_trace_at(li: i32, idx: &[usize]) {
11276    use std::io::Write;
11277    static F: std::sync::OnceLock<Option<std::sync::Mutex<std::fs::File>>> =
11278        std::sync::OnceLock::new();
11279    let Some(f) = F.get_or_init(|| {
11280        let p = std::env::var("CMF_MOE_TRACE").ok()?;
11281        Some(std::sync::Mutex::new(
11282            std::fs::OpenOptions::new()
11283                .create(true)
11284                .append(true)
11285                .open(p)
11286                .ok()?,
11287        ))
11288    }) else {
11289        return;
11290    };
11291    let ids: Vec<String> = idx.iter().map(|e| e.to_string()).collect();
11292    let _ = writeln!(f.lock().unwrap(), "{li}:{}", ids.join(","));
11293}
11294
11295/// MoE FFN: router → top-k experts (see `moe_route`). Only selected
11296/// experts' pages are touched in mmap.
11297pub(crate) fn moe_ffn(
11298    m: &MoeFfn,
11299    x: &[f32],
11300    pool: Option<&Pool>,
11301    allowed: Option<&[bool]>,
11302) -> Vec<f32> {
11303    accumulate_act(m, x, 1);
11304    let ne = m.experts.len();
11305    let mut logits = vec![0.0f32; ne];
11306    match &m.resonance {
11307        Some(r) => r.scores(x, &mut logits),
11308        None => m.router.matvec(x, &mut logits, pool),
11309    }
11310    let (idx, p, wsum) = moe_route(&logits, m, allowed);
11311    {
11312        let mut st = m.stats.borrow_mut();
11313        if st.len() < ne {
11314            st.resize(ne, 0);
11315        }
11316        for &e in &idx {
11317            st[e] += 1;
11318        }
11319    }
11320    // `CMF_MOE_TRACE=<file>`: append one line per (layer, token) with the
11321    // selected expert ids. The cumulative `stats` above answer "which
11322    // experts are popular"; a residency design needs the question they
11323    // cannot answer — whether CONSECUTIVE tokens reuse experts (the
11324    // temporal locality an LRU cache lives on, FreeToken §4).
11325    moe_trace(&idx);
11326    // D5: the whole layer MoE block in one GPU command buffer (experts — the
11327    // same mmap via a no-copy buffer; intermediate activations on the GPU).
11328    // Same Ffn probe class as the dense chain: one submit per layer
11329    // either wins on this driver stack or it doesn't.
11330    if crate::gpu::enabled_here() {
11331        match crate::gpu::probe_arm(crate::gpu::OpClass::Ffn) {
11332            crate::gpu::ProbeArm::Gpu => {
11333                let t0 = std::time::Instant::now();
11334                if let Some(out) = moe_ffn_gpu(m, x, &idx, &p, wsum, pool) {
11335                    crate::gpu::probe_record(crate::gpu::OpClass::Ffn, true, t0.elapsed());
11336                    return out;
11337                }
11338            }
11339            crate::gpu::ProbeArm::CpuTimed => {
11340                let t0 = std::time::Instant::now();
11341                let out = crate::gpu::cpu_scope(|| moe_ffn_cpu(m, x, &idx, &p, wsum, pool));
11342                crate::gpu::probe_record(crate::gpu::OpClass::Ffn, false, t0.elapsed());
11343                return out;
11344            }
11345            crate::gpu::ProbeArm::Cpu => {
11346                return crate::gpu::cpu_scope(|| moe_ffn_cpu(m, x, &idx, &p, wsum, pool));
11347            }
11348        }
11349    }
11350    moe_ffn_cpu(m, x, &idx, &p, wsum, pool)
11351}
11352
11353/// One-shot report of whether the whole-token wgpu graph actually formed.
11354/// A refusal silently reverts to the per-op path, which is how a model can
11355/// look "GPU-accelerated" while every layer walks the host.
11356fn graph_note(built: bool) {
11357    use std::sync::atomic::{AtomicBool, Ordering};
11358    if built {
11359        GRAPH_TOK_OK.fetch_add(1, Ordering::Relaxed);
11360    } else {
11361        GRAPH_TOK_MISS.fetch_add(1, Ordering::Relaxed);
11362    }
11363    static SAID: AtomicBool = AtomicBool::new(false);
11364    if !SAID.swap(true, Ordering::Relaxed) {
11365        if built {
11366            tracing::info!("wgpu whole-token graph: ACTIVE");
11367        } else {
11368            tracing::warn!("wgpu whole-token graph refused — per-op path");
11369        }
11370    }
11371}
11372
11373/// Whole-token graph outcomes, process-wide: a benchmark that claims a
11374/// GPU number while MISS climbs is measuring the CPU — the honest-bench
11375/// contract makes that an error, not a footnote.
11376pub static GRAPH_TOK_OK: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
11377pub static GRAPH_TOK_MISS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
11378
11379/// `CMF_MOE_BATCH=0` restores the per-expert serial loop — the A/B lever
11380/// for the batched kernel, and how its bit-identity is checked.
11381fn moe_batch_enabled() -> bool {
11382    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
11383    *ON.get_or_init(|| std::env::var("CMF_MOE_BATCH").as_deref() != Ok("0"))
11384}
11385
11386/// Two-dispatch CPU MoE: every routed expert (and the shared one) fused
11387/// into one gate/up/SiLU dispatch and one down dispatch, instead of two
11388/// pool barriers per expert. Bit-identical to the serial loop below —
11389/// see `moe_gate_up_many` / `moe_down_many`. `None` = the batched kernel
11390/// does not cover this layer, walk the serial path.
11391fn moe_ffn_cpu_batched(
11392    m: &MoeFfn,
11393    x: &[f32],
11394    idx: &[usize],
11395    p: &[f32],
11396    wsum: f32,
11397    pool: Option<&Pool>,
11398) -> Option<Vec<f32>> {
11399    if idx.is_empty() || !moe_batch_enabled() {
11400        return None;
11401    }
11402    // The bake probe reads per-neuron activation mass out of the
11403    // single-expert path; batching would skip it. Rare and offline —
11404    // hand those runs to the serial loop.
11405    if FFN_PROBE.with(|pr| pr.borrow().is_some()) {
11406        return None;
11407    }
11408    let n = idx.len() + usize::from(m.shared.is_some());
11409    let mut pairs = Vec::with_capacity(n);
11410    let mut downs = Vec::with_capacity(n);
11411    let mut ws = Vec::with_capacity(n);
11412    for &e in idx {
11413        let d = &m.experts[e];
11414        if d.act != Act::Silu {
11415            return None;
11416        }
11417        pairs.push((&d.gate_proj, &d.up_proj));
11418        downs.push(&d.down_proj);
11419        ws.push(p[e] / wsum * m.per_expert_scale.as_ref().map_or(1.0, |v| v[e]));
11420    }
11421    // The shared expert goes last, matching the serial loop's order —
11422    // the f32 accumulation order is part of the bit-identity claim.
11423    if let Some((se, gate)) = &m.shared {
11424        if se.act != Act::Silu {
11425            return None;
11426        }
11427        let g = gate.as_ref().map_or(1.0, |gate| {
11428            let mut gl = [0.0f32; 1];
11429            gate.matvec(x, &mut gl, pool);
11430            1.0 / (1.0 + (-gl[0]).exp())
11431        });
11432        pairs.push((&se.gate_proj, &se.up_proj));
11433        downs.push(&se.down_proj);
11434        ws.push(g);
11435    }
11436    let inter = pairs[0].0.rows();
11437    let mut gs: Vec<Vec<f32>> = (0..pairs.len()).map(|_| vec![0f32; inter]).collect();
11438    if !QTensor::moe_gate_up_many(&pairs, x, &mut gs, pool) {
11439        return None;
11440    }
11441    let mut out = attention::take_buf(x.len());
11442    if !QTensor::moe_down_many(&downs, &gs, &ws, &mut out, pool) {
11443        attention::recycle_buf(&mut out);
11444        return None;
11445    }
11446    Some(out)
11447}
11448
11449/// Exact CPU completion for the routed experts a dynamic device cache did
11450/// not contain. The weights are already the router's final normalized mix.
11451/// Keeping this independent of `MoeFfn` makes the job `Sync`: its routing
11452/// statistics live in a `RefCell`, while the immutable expert tensors can be
11453/// evaluated safely in parallel with the GPU's resident subset.
11454pub(crate) fn moe_cold_experts_cpu(
11455    experts: &[(&DenseFfn, f32)],
11456    x: &[f32],
11457    pool: Option<&Pool>,
11458) -> Vec<f32> {
11459    let mut out = attention::take_buf(x.len());
11460    if experts.is_empty() {
11461        return out;
11462    }
11463    let pairs: Vec<_> = experts
11464        .iter()
11465        .map(|(e, _)| (&e.gate_proj, &e.up_proj))
11466        .collect();
11467    let downs: Vec<_> = experts.iter().map(|(e, _)| &e.down_proj).collect();
11468    let weights: Vec<_> = experts.iter().map(|(_, w)| *w).collect();
11469    let inter = experts[0].0.gate_proj.rows();
11470    let mut activations: Vec<Vec<f32>> = (0..experts.len()).map(|_| vec![0.0; inter]).collect();
11471    if QTensor::moe_gate_up_many(&pairs, x, &mut activations, pool)
11472        && QTensor::moe_down_many(&downs, &activations, &weights, &mut out, pool)
11473    {
11474        return out;
11475    }
11476    out.fill(0.0);
11477    for &(expert, weight) in experts {
11478        let mut one = dense_ffn(expert, x, pool);
11479        for (o, v) in out.iter_mut().zip(&one) {
11480            *o += weight * v;
11481        }
11482        attention::recycle_buf(&mut one);
11483    }
11484    out
11485}
11486
11487/// The pure-CPU MoE expert loop (also the fallback of every GPU refusal).
11488fn moe_ffn_cpu(
11489    m: &MoeFfn,
11490    x: &[f32],
11491    idx: &[usize],
11492    p: &[f32],
11493    wsum: f32,
11494    pool: Option<&Pool>,
11495) -> Vec<f32> {
11496    if let Some(out) = moe_ffn_cpu_batched(m, x, idx, p, wsum, pool) {
11497        return out;
11498    }
11499    let mut out = attention::take_buf(x.len());
11500    for &e in idx {
11501        let mut eo = dense_ffn(&m.experts[e], x, pool);
11502        let w = p[e] / wsum * m.per_expert_scale.as_ref().map_or(1.0, |v| v[e]);
11503        for i in 0..out.len() {
11504            out[i] += w * eo[i];
11505        }
11506        attention::recycle_buf(&mut eo);
11507    }
11508    if let Some((se, gate)) = &m.shared {
11509        let mut so = dense_ffn(se, x, pool);
11510        let g = gate.as_ref().map_or(1.0, |gate| {
11511            let mut gl = [0.0f32; 1];
11512            gate.matvec(x, &mut gl, pool);
11513            1.0 / (1.0 + (-gl[0]).exp())
11514        });
11515        for i in 0..out.len() {
11516            out[i] += g * so[i];
11517        }
11518        attention::recycle_buf(&mut so);
11519    }
11520    out
11521}
11522
11523/// DeepSeek-V2 MLA forward, expand-to-MHA form (see `AttnKind::Mla`):
11524/// per token the latent expands to every head's K/V and the ordinary
11525/// cache + grouped attend do the rest. K head layout is [rope | nope]
11526/// (rotary_dim = qk_rope rotates the shared rope key and each q head's
11527/// prefix); V rows are zero-padded to the K head_dim inside the cache
11528/// and the pad is sliced off before O. Born importance is not
11529/// accumulated for MLA yet (no eviction interplay).
11530#[allow(clippy::too_many_arguments)]
11531fn mla_attention(
11532    w: &MlaWeights,
11533    normed: &[f32],
11534    cache: &mut crate::kv_cache::LayerKvCache,
11535    position: usize,
11536    inv_freq: &[f32],
11537    rope_scale: f32,
11538    eps: f64,
11539    pool: Option<&Pool>,
11540) -> Vec<f32> {
11541    let (nh, dr, dn, dv, lora) = (w.nh, w.qk_rope, w.qk_nope, w.v_dim, w.lora);
11542    let hd = dr + dn;
11543    let mut q = vec![0.0f32; nh * hd];
11544    match (&w.q_a, &w.q_a_norm) {
11545        (Some(qa), Some(qn)) => {
11546            let mut t = vec![0.0f32; qa.rows()];
11547            qa.matvec(normed, &mut t, pool);
11548            let tn = inference::rms_norm(&t, qn, eps, NormStyle::Qwen);
11549            w.q_proj.matvec(&tn, &mut q, pool);
11550        }
11551        _ => w.q_proj.matvec(normed, &mut q, pool),
11552    }
11553    let mut ca = vec![0.0f32; lora + dr];
11554    w.kv_a.matvec(normed, &mut ca, pool);
11555    let (c_lat, k_rope) = ca.split_at_mut(lora);
11556    let latn = inference::rms_norm(c_lat, &w.kv_a_norm, eps, NormStyle::Qwen);
11557    let mut kvb = vec![0.0f32; nh * (dn + dv)];
11558    w.kv_b.matvec(&latn, &mut kvb, pool);
11559    if !w.nope {
11560        attention::rope_rotate_scaled(k_rope, position, inv_freq, rope_scale);
11561    }
11562    for h in 0..nh {
11563        if !w.nope {
11564            attention::rope_rotate_scaled(
11565                &mut q[h * hd..h * hd + dr],
11566                position,
11567                inv_freq,
11568                rope_scale,
11569            );
11570        }
11571    }
11572    let mut k = vec![0.0f32; nh * hd];
11573    let mut v = vec![0.0f32; nh * hd];
11574    for h in 0..nh {
11575        k[h * hd..h * hd + dr].copy_from_slice(k_rope);
11576        k[h * hd + dr..(h + 1) * hd].copy_from_slice(&kvb[h * (dn + dv)..h * (dn + dv) + dn]);
11577        v[h * hd..h * hd + dv].copy_from_slice(&kvb[h * (dn + dv) + dn..(h + 1) * (dn + dv)]);
11578    }
11579    cache.append(&k, &v, &vec![true; nh]);
11580    let (ao, mut imp) = attention::attend_all_heads(&q, cache, nh, 1, hd, w.scale, None, 0.0);
11581    attention::recycle_buf(&mut imp);
11582    let mut ov = vec![0.0f32; nh * dv];
11583    for h in 0..nh {
11584        ov[h * dv..(h + 1) * dv].copy_from_slice(&ao[h * hd..h * hd + dv]);
11585    }
11586    let mut out = vec![0.0f32; w.o_proj.rows()];
11587    w.o_proj.matvec(&ov, &mut out, pool);
11588    out
11589}
11590
11591/// Gemma-4 dual-branch FFN (spec: see `FfnKind::DenseMoe`). The dense
11592/// branch reads the pre-FFN-normed activation; the router and the
11593/// expert branch read the RAW residual — the router through a
11594/// scale-less rms norm (its constant gain is folded into the weights),
11595/// the experts through `pre_norm_2`. CPU path; GPU graphs refuse the
11596/// layer kind honestly.
11597fn dense_moe_ffn(
11598    dm: &DenseMoeFfn,
11599    x_normed: &[f32],
11600    h_raw: &[f32],
11601    eps: f64,
11602    norm_style: NormStyle,
11603    pool: Option<&Pool>,
11604) -> Vec<f32> {
11605    let mut d = dense_ffn(&dm.dense, x_normed, pool);
11606    d = inference::rms_norm(&d, &dm.post_norm_1, eps, norm_style);
11607    let m = &dm.moe;
11608    let ne = m.experts.len();
11609    let mut logits = vec![0.0f32; ne];
11610    if m.router_input_norm {
11611        let ss: f32 = h_raw.iter().map(|v| v * v).sum::<f32>() / h_raw.len() as f32;
11612        let inv = 1.0 / (ss + eps as f32).sqrt();
11613        let xr: Vec<f32> = h_raw.iter().map(|v| v * inv).collect();
11614        m.router.matvec(&xr, &mut logits, pool);
11615    } else {
11616        m.router.matvec(h_raw, &mut logits, pool);
11617    }
11618    let (idx, p, wsum) = moe_route(&logits, m, None);
11619    {
11620        let mut st = m.stats.borrow_mut();
11621        if st.len() < ne {
11622            st.resize(ne, 0);
11623        }
11624        for &e in &idx {
11625            st[e] += 1;
11626        }
11627    }
11628    let x2 = inference::rms_norm(h_raw, &dm.pre_norm_2, eps, norm_style);
11629    let mo = moe_ffn_cpu(m, &x2, &idx, &p, wsum, pool);
11630    let mo = inference::rms_norm(&mo, &dm.post_norm_2, eps, norm_style);
11631    for (di, mi) in d.iter_mut().zip(&mo) {
11632        *di += mi;
11633    }
11634    d
11635}
11636
11637/// Building the MoE-layer GPU jobs: all selected experts (+shared) must
11638/// be q8_2f-Mapped from the primary mapping; otherwise None → CPU path.
11639/// One-shot report of why the MoE GPU block refused. A silent `?` here
11640/// sends every expert to the CPU with nothing in the logs to say so —
11641/// which is exactly how a q4tp MoE model looked "GPU-accelerated" while
11642/// running entirely on the host.
11643fn moe_gpu_refused(why: &'static str) {
11644    use std::sync::atomic::{AtomicBool, Ordering};
11645    static SAID: AtomicBool = AtomicBool::new(false);
11646    if !SAID.swap(true, Ordering::Relaxed) {
11647        tracing::warn!("MoE GPU block refused ({why}) — experts run on the CPU");
11648    }
11649}
11650
11651fn moe_ffn_gpu(
11652    m: &MoeFfn,
11653    x: &[f32],
11654    idx: &[usize],
11655    p: &[f32],
11656    wsum: f32,
11657    pool: Option<&Pool>,
11658) -> Option<Vec<f32>> {
11659    use crate::gpu::MoeJob;
11660
11661    let mut jobs: Vec<MoeJob> = Vec::with_capacity(idx.len() + 1);
11662    let mut model_ref = None;
11663    for &e in idx {
11664        if moe_push_job(&m.experts[e], x, p[e] / wsum, &mut jobs, &mut model_ref).is_none() {
11665            moe_gpu_refused("push_job(expert)");
11666            return None;
11667        }
11668    }
11669    if let Some((se, gate)) = &m.shared {
11670        let g = gate.as_ref().map_or(1.0, |gate| {
11671            let mut gl = [0.0f32; 1];
11672            gate.matvec(x, &mut gl, pool);
11673            1.0 / (1.0 + (-gl[0]).exp())
11674        });
11675        if moe_push_job(se, x, g, &mut jobs, &mut model_ref).is_none() {
11676            moe_gpu_refused("push_job(shared)");
11677            return None;
11678        }
11679    }
11680    let Some(model) = model_ref else {
11681        moe_gpu_refused("no model_ref");
11682        return None;
11683    };
11684    let hidden = jobs[0].down.1;
11685    let mut out = vec![0.0f32; hidden];
11686    if crate::gpu::moe_block(&model, &jobs, &mut out) {
11687        Some(out)
11688    } else {
11689        moe_gpu_refused("gpu::moe_block");
11690        None
11691    }
11692}
11693
11694/// Single-position FFN dispatch.
11695fn ffn_forward(
11696    ffn: &FfnKind,
11697    x: &[f32],
11698    pool: Option<&Pool>,
11699    experts_allowed: Option<&[bool]>,
11700) -> Vec<f32> {
11701    match ffn {
11702        FfnKind::Dense(d) if !d.segs.is_empty() => tube_ffn(d, x, 1, pool, None),
11703        FfnKind::Dense(d) => dense_ffn(d, x, pool),
11704        FfnKind::Moe(m) => moe_ffn(m, x, pool, experts_allowed),
11705        // Dual-branch layers need the raw residual — their callers
11706        // dispatch dense_moe_ffn directly; the auxiliary paths that land
11707        // here (MTP draft, o1 replay) do not co-occur with gemma-4 MoE.
11708        FfnKind::DenseMoe(_) => unreachable!("DenseMoe dispatches via dense_moe_ffn"),
11709    }
11710}
11711
11712/// Fused two-position FFN: gate/up/down streamed once (dense). MoE
11713/// falls back to two singles — expert sets differ per position, there
11714/// is nothing to fuse.
11715fn ffn_forward_pair(
11716    ffn: &FfnKind,
11717    x1: &[f32],
11718    x2: &[f32],
11719    pool: Option<&Pool>,
11720    experts_allowed: Option<&[bool]>,
11721) -> (Vec<f32>, Vec<f32>) {
11722    let d = match ffn {
11723        // A tube layer has nothing to fuse across the pair — the tubes
11724        // are separate matrices; two singles are the honest path.
11725        FfnKind::Dense(d) if !d.segs.is_empty() => {
11726            return (
11727                tube_ffn(d, x1, 1, pool, None),
11728                tube_ffn(d, x2, 1, pool, None),
11729            );
11730        }
11731        FfnKind::Dense(d) => d,
11732        FfnKind::Moe(m) => {
11733            return (
11734                moe_ffn(m, x1, pool, experts_allowed),
11735                moe_ffn(m, x2, pool, experts_allowed),
11736            );
11737        }
11738        FfnKind::DenseMoe(_) => unreachable!("DenseMoe dispatches via dense_moe_ffn"),
11739    };
11740    let inter = d.gate_proj.rows();
11741    FFN_SCRATCH.with(|s| {
11742        let mut s = s.borrow_mut();
11743        let [g1, g2, u1, u2] = &mut *s;
11744        g1.resize(inter, 0.0);
11745        g2.resize(inter, 0.0);
11746        u1.resize(inter, 0.0);
11747        u2.resize(inter, 0.0);
11748        // Multi-matrix pair job: gate+up under one pool dispatch
11749        // (o1s = lane-1 outputs across tensors, o2s = lane-2).
11750        QTensor::matvec2_many(
11751            [&d.gate_proj, &d.up_proj],
11752            x1,
11753            x2,
11754            [g1.as_mut_slice(), u1.as_mut_slice()],
11755            [g2.as_mut_slice(), u2.as_mut_slice()],
11756            pool,
11757        );
11758        for i in 0..inter {
11759            g1[i] = d.act.combine(g1[i], u1[i]);
11760            g2[i] = d.act.combine(g2[i], u2[i]);
11761        }
11762        let mut o1 = attention::take_buf(d.down_proj.rows());
11763        let mut o2 = attention::take_buf(d.down_proj.rows());
11764        d.down_proj.matvec2(g1, g2, &mut o1, &mut o2, pool);
11765        (o1, o2)
11766    })
11767}
11768
11769#[cfg(test)]
11770mod tests {
11771
11772    #[test]
11773    fn cancel_flag_stops_generation() {
11774        let mut p = create_test_pipeline(16, 32, 2, 2, 8, 2, 32);
11775        // Set before the call: the prefill loops honour it, the run
11776        // returns immediately with the cancelled reason and no tokens.
11777        p.cancel.store(true, std::sync::atomic::Ordering::Relaxed);
11778        let r = p.generate_from_ids(&[1, 2, 3], 8, None, None).unwrap();
11779        assert_eq!(r.finish_reason, "cancelled");
11780        assert!(
11781            r.token_ids.is_empty(),
11782            "no tokens after cancel: {:?}",
11783            r.token_ids
11784        );
11785        // Flag auto-cleared: the next call generates normally.
11786        let r2 = p.generate_from_ids(&[1, 2, 3], 4, None, None).unwrap();
11787        assert_ne!(r2.finish_reason, "cancelled");
11788    }
11789    use super::*;
11790
11791    /// sparse_ffn_quant must equal a dense FFN where inactive neurons are
11792    /// zeroed (mask × mmap correctness). On F32 tensors this is EXACT —
11793    /// it validates the row_dot / add_col_scaled / scatter indexing, the
11794    /// bug-prone part. The q8 branches reuse the golden-tested linear
11795    /// The per-token sparse path reads a transposed `down`; it must
11796    /// agree with the arm that computes everything and zeroes the
11797    /// losers, or the speed measurement is measuring a different model.
11798    #[test]
11799    fn dynamic_ffn_equals_the_zeroing_arm() {
11800        let (hidden, inter) = (8usize, 32usize);
11801        let synth = |n: usize, salt: usize| -> Vec<f32> {
11802            (0..n)
11803                .map(|i| (((i * 29 + salt * 13 + 7) % 89) as f32 / 89.0 - 0.5) * 0.6)
11804                .collect()
11805        };
11806        let down = synth(hidden * inter, 3);
11807        let mut down_t = vec![0.0f32; inter * hidden];
11808        for r in 0..hidden {
11809            for c in 0..inter {
11810                down_t[c * hidden + r] = down[r * inter + c];
11811            }
11812        }
11813        let d = DenseFfn {
11814            gate_proj: QTensor::from_f32(synth(inter * hidden, 1), inter, hidden),
11815            up_proj: QTensor::from_f32(synth(inter * hidden, 2), inter, hidden),
11816            down_proj: QTensor::from_f32(down.clone(), hidden, inter),
11817            act: Act::Silu,
11818            down_t: Some(QTensor::from_f32(down_t, inter, hidden)),
11819            segs: Vec::new(),
11820        };
11821        let x = synth(hidden, 11);
11822        let k = 12usize;
11823        let got = dense_ffn_dynamic(&d, &x, None, k).expect("down_t present");
11824        // Reference: full compute, keep the k loudest |silu(gate)|.
11825        let mut g = vec![0.0f32; inter];
11826        d.gate_proj.matvec(&x, &mut g, None);
11827        let mut u = vec![0.0f32; inter];
11828        d.up_proj.matvec(&x, &mut u, None);
11829        for v in g.iter_mut() {
11830            *v = inference::silu(*v);
11831        }
11832        keep_top_k(&mut g, k);
11833        for i in 0..inter {
11834            g[i] *= u[i];
11835        }
11836        let mut want = vec![0.0f32; hidden];
11837        d.down_proj.matvec(&g, &mut want, None);
11838        for (a, b) in want.iter().zip(&got) {
11839            assert!((a - b).abs() < 1e-5, "dynamic {b} vs reference {a}");
11840        }
11841    }
11842
11843    /// A tube layer is the same layer, re-cut. With every tube open the
11844    /// answer must equal the dense FFN over the concatenated neurons
11845    /// (the permutation is an identity on the layer's function); with a
11846    /// tube closed it must equal the dense FFN with those neurons
11847    /// zeroed — the mask semantics, now paid for in bytes not read.
11848    #[test]
11849    fn tube_ffn_open_equals_dense_and_closed_equals_masked() {
11850        let (hidden, core, tube) = (8usize, 12usize, 8usize);
11851        let inter = core + tube;
11852        let synth = |n: usize, salt: usize| -> Vec<f32> {
11853            (0..n)
11854                .map(|i| (((i * 41 + salt * 17 + 5) % 97) as f32 / 97.0 - 0.5) * 0.5)
11855                .collect()
11856        };
11857        let (g_all, u_all) = (synth(inter * hidden, 1), synth(inter * hidden, 2));
11858        let d_all = synth(hidden * inter, 3);
11859        // The dense layer, and the same weights cut into core + tube.
11860        let dense = DenseFfn {
11861            gate_proj: QTensor::from_f32(g_all.clone(), inter, hidden),
11862            up_proj: QTensor::from_f32(u_all.clone(), inter, hidden),
11863            down_proj: QTensor::from_f32(d_all.clone(), hidden, inter),
11864            act: Act::Silu,
11865            down_t: None,
11866            segs: Vec::new(),
11867        };
11868        let rows =
11869            |v: &[f32], a: usize, b: usize| -> Vec<f32> { v[a * hidden..b * hidden].to_vec() };
11870        let cols = |v: &[f32], a: usize, b: usize| -> Vec<f32> {
11871            let mut o = Vec::with_capacity(hidden * (b - a));
11872            for r in 0..hidden {
11873                o.extend_from_slice(&v[r * inter + a..r * inter + b]);
11874            }
11875            o
11876        };
11877        let tubed = DenseFfn {
11878            down_t: None,
11879            gate_proj: QTensor::from_f32(rows(&g_all, 0, core), core, hidden),
11880            up_proj: QTensor::from_f32(rows(&u_all, 0, core), core, hidden),
11881            down_proj: QTensor::from_f32(cols(&d_all, 0, core), hidden, core),
11882            act: Act::Silu,
11883            segs: vec![FfnSeg {
11884                gate: QTensor::from_f32(rows(&g_all, core, inter), tube, hidden),
11885                up: QTensor::from_f32(rows(&u_all, core, inter), tube, hidden),
11886                down: QTensor::from_f32(cols(&d_all, core, inter), hidden, tube),
11887                start: core,
11888                width: tube,
11889            }],
11890        };
11891        let x = synth(hidden, 7);
11892        let want = dense_ffn(&dense, &x, None);
11893        let got = tube_ffn(&tubed, &x, 1, None, None);
11894        for (a, b) in want.iter().zip(&got) {
11895            assert!((a - b).abs() < 1e-5, "open tube: {a} vs {b}");
11896        }
11897        // Closed tube: bits on for the core, off for the tube.
11898        let mut bits = vec![0u8; inter.div_ceil(8)];
11899        for n in 0..core {
11900            bits[n / 8] |= 1 << (n % 8);
11901        }
11902        let closed = tube_ffn(&tubed, &x, 1, None, Some(&bits));
11903        let masked = dense_ffn_masked(&dense, &x, None, &bits);
11904        for (a, b) in masked.iter().zip(&closed) {
11905            assert!((a - b).abs() < 1e-5, "closed tube: {a} vs {b}");
11906        }
11907        // The batched arm must agree with the single-position one.
11908        let batch = tube_ffn(&tubed, &x, 1, None, Some(&bits));
11909        for (a, b) in closed.iter().zip(&batch) {
11910            assert_eq!(a, b, "batch arm disagrees with decode arm");
11911        }
11912    }
11913
11914    /// scale, structurally identical to the matvec kernels.
11915    #[test]
11916    fn sparse_ffn_quant_equals_dense_with_inactive_zeroed() {
11917        let (hidden, inter) = (16usize, 40usize);
11918        let synth = |n: usize, salt: usize| -> Vec<f32> {
11919            (0..n)
11920                .map(|i| (((i * 37 + salt * 11 + 3) % 101) as f32 / 101.0 - 0.5) * 0.4)
11921                .collect()
11922        };
11923        let d = DenseFfn {
11924            gate_proj: QTensor::from_f32(synth(inter * hidden, 1), inter, hidden),
11925            up_proj: QTensor::from_f32(synth(inter * hidden, 2), inter, hidden),
11926            down_proj: QTensor::from_f32(synth(hidden * inter, 3), hidden, inter),
11927            act: Act::Silu,
11928            down_t: None,
11929            segs: Vec::new(),
11930        };
11931        let x = synth(hidden, 9);
11932        // Active = every 3rd neuron.
11933        let active: Vec<u16> = (0..inter as u16).filter(|i| i % 3 == 0).collect();
11934
11935        let sparse = sparse_ffn_quant(&d, &x, &active, hidden, None);
11936
11937        // Reference: full dense FFN but g[i]=0 for inactive neurons.
11938        let mut g = vec![0.0f32; inter];
11939        d.gate_proj.matvec(&x, &mut g, None);
11940        let mut u = vec![0.0f32; inter];
11941        d.up_proj.matvec(&x, &mut u, None);
11942        let act_set: std::collections::HashSet<u16> = active.iter().copied().collect();
11943        for i in 0..inter {
11944            g[i] = if act_set.contains(&(i as u16)) {
11945                inference::silu(g[i]) * u[i]
11946            } else {
11947                0.0
11948            };
11949        }
11950        let mut reference = vec![0.0f32; hidden];
11951        d.down_proj.matvec(&g, &mut reference, None);
11952
11953        let max_d = sparse
11954            .iter()
11955            .zip(&reference)
11956            .map(|(a, b)| (a - b).abs())
11957            .fold(0.0f32, f32::max);
11958        assert!(max_d < 1e-5, "sparse != dense-zeroed: max|Δ| = {max_d}");
11959    }
11960
11961    /// Attach a synthetic MTP head (same structure as a main layer).
11962    fn attach_test_mtp(p: &mut Pipeline) {
11963        let (h, inter, heads, kv, hd) = (
11964            p.hidden_size,
11965            p.intermediate_size,
11966            p.num_heads,
11967            p.num_kv_heads,
11968            p.head_dim,
11969        );
11970        let synth = |n: usize, salt: usize| -> Vec<f32> {
11971            (0..n)
11972                .map(|i| (((i * 29 + salt * 23 + 5) % 101) as f32 / 101.0 - 0.5) * 0.2)
11973                .collect()
11974        };
11975        let qt = |rows: usize, cols: usize, salt: usize| -> QTensor {
11976            QTensor::from_f32(synth(rows * cols, salt), rows, cols)
11977        };
11978        p.mtp = Some(MtpModule {
11979            enorm: vec![1.0; h],
11980            hnorm: vec![1.0; h],
11981            eh_proj: qt(h, 2 * h, 301),
11982            layer: LayerWeights {
11983                input_norm: vec![1.0; h],
11984                post_norm: vec![1.0; h],
11985                attn_out_norm: None,
11986                ffn_out_norm: None,
11987                layer_scale: None,
11988                ffn: FfnKind::Dense(DenseFfn {
11989                    gate_proj: qt(inter, h, 315),
11990                    up_proj: qt(inter, h, 316),
11991                    down_proj: qt(h, inter, 317),
11992                    act: Act::Silu,
11993                    down_t: None,
11994                    segs: Vec::new(),
11995                }),
11996                attn: AttnKind::Full {
11997                    bias: None,
11998                    wq: qt(heads * hd, h, 311),
11999                    wk: qt(kv * hd, h, 312),
12000                    wv: qt(kv * hd, h, 313),
12001                    wo: qt(h, heads * hd, 314),
12002                    q_norm: None,
12003                    k_norm: None,
12004                    output_gate: false,
12005                    softplus_gate: None,
12006                },
12007            },
12008            final_norm: vec![1.0; h],
12009            kv: crate::kv_cache::LayerKvCache::new(kv, hd),
12010        });
12011    }
12012
12013    #[test]
12014    fn speculative_equals_vanilla_greedy() {
12015        // Speculative decode and the wgpu token graph are mutually
12016        // exclusive; a leaked CMF_GPU=wgpu from a parallel gpu test
12017        // would silently disable drafting. Pin the graph off.
12018        unsafe { std::env::set_var("CMF_GPU_WGPU_GRAPH", "0") };
12019        let run = |spec: bool| {
12020            let mut p = create_test_pipeline(8, 16, 2, 1, 4, 2, 260);
12021            p.sampler_config.temperature = 0.0;
12022            attach_test_mtp(&mut p);
12023            p.speculative = spec;
12024            let r = p.generate("abcdef", 12, None, None).unwrap();
12025            (r.token_ids, r.mtp_drafted, r.mtp_accepted)
12026        };
12027        let (vanilla, d0, _) = run(false);
12028        let (spec, d1, a1) = run(true);
12029        assert_eq!(d0, 0, "vanilla path must not draft");
12030        assert!(d1 > 0, "speculative path must draft");
12031        assert_eq!(
12032            vanilla, spec,
12033            "speculative must reproduce the exact greedy sequence (accepted {a1}/{d1})"
12034        );
12035    }
12036
12037    #[test]
12038    fn speculative_accepts_constant_oracle() {
12039        // See speculative_equals_vanilla_greedy: pin the wgpu graph off.
12040        unsafe { std::env::set_var("CMF_GPU_WGPU_GRAPH", "0") };
12041        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
12042        p.sampler_config.temperature = 0.0;
12043        p.sampler_config.repetition_penalty = 1.0;
12044        // Constant lm_head → every logit equal → both the main model and
12045        // the draft head argmax to token 0: acceptance must be 100%.
12046        p.weights.lm_head = QTensor::from_f32(vec![0.01; 64 * 8], 64, 8);
12047        attach_test_mtp(&mut p);
12048        p.speculative = true;
12049        let r = p.generate("abcd", 10, None, None).unwrap();
12050        assert!(r.mtp_drafted > 0);
12051        assert_eq!(
12052            r.mtp_accepted, r.mtp_drafted,
12053            "constant logits → every draft accepted"
12054        );
12055        // Ties resolve to the same token in both the main and draft
12056        // heads — the sequence is one repeated token.
12057        assert!(r.token_ids.windows(2).all(|w| w[0] == w[1]));
12058    }
12059
12060    #[test]
12061    fn empty_prompt_is_an_error_not_a_panic() {
12062        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 260);
12063        let r = p.generate("", 4, None, None);
12064        assert!(r.is_err(), "empty prompt must be a clean error");
12065    }
12066
12067    #[test]
12068    fn every_token_enters_kv_exactly_once() {
12069        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 2, 260);
12070        // Greedy so no RNG variance; byte tokenizer → 3 prompt tokens.
12071        p.sampler_config.temperature = 0.0;
12072        let r = p.generate("abc", 2, None, None).unwrap();
12073        assert_eq!(r.prompt_tokens, 3);
12074        // prompt(3) + first sampled token forwarded before second logits:
12075        // step0 samples from prefill hidden (no extra forward), then
12076        // forwards t1 → cache 4; step1 samples, loop ends (max_tokens).
12077        assert_eq!(
12078            p.kv_cache.seq_len(),
12079            3 + r.tokens_generated - 1,
12080            "each token must be cached exactly once (v1 cached the last prompt token twice)"
12081        );
12082    }
12083
12084    #[test]
12085    fn generation_is_reproducible_with_seed() {
12086        let run = || {
12087            let mut p = create_test_pipeline(8, 16, 2, 1, 4, 2, 260);
12088            p.generate("hello", 8, None, None).unwrap().token_ids
12089        };
12090        assert_eq!(run(), run());
12091    }
12092
12093    #[test]
12094    fn resetting_sampler_restarts_the_seeded_stream() {
12095        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 2, 260);
12096        let config = SamplerConfig {
12097            seed: Some(1234),
12098            ..SamplerConfig::default()
12099        };
12100        p.set_sampler_config(config.clone());
12101        let first = p.generate("hello", 8, None, None).unwrap().token_ids;
12102        p.set_sampler_config(config);
12103        let second = p.generate("hello", 8, None, None).unwrap().token_ids;
12104        assert_eq!(first, second);
12105    }
12106
12107    #[test]
12108    fn eviction_bounds_the_cache() {
12109        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 260);
12110        p.kv_cache.max_seq_len = 6;
12111        p.sampler_config.temperature = 0.0;
12112        let _ = p.generate("abcd", 12, None, None).unwrap();
12113        assert!(
12114            p.kv_cache.seq_len() <= 6 + 1,
12115            "cache must stay bounded by max_seq_len (got {})",
12116            p.kv_cache.seq_len()
12117        );
12118    }
12119
12120    #[test]
12121    fn confidence_matches_tokens_and_is_a_probability() {
12122        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
12123        p.sampler_config.temperature = 0.0;
12124        p.sampler_config.repetition_penalty = 1.0;
12125        let r = p.generate("abcd", 10, None, None).unwrap();
12126        assert_eq!(
12127            r.token_confidence.len(),
12128            r.token_ids.len(),
12129            "one confidence per emitted token"
12130        );
12131        for &c in &r.token_confidence {
12132            assert!((0.0..=1.0).contains(&c), "confidence out of [0,1]: {c}");
12133        }
12134        // top1_prob is a valid softmax probability.
12135        let logits = [1.0f32, 3.0, 0.5, 3.0];
12136        let p0 = top1_prob_t(&logits, 1, 1.0);
12137        let p1 = top1_prob_t(&logits, 3, 1.0);
12138        assert!((p0 - p1).abs() < 1e-6, "equal logits → equal prob");
12139        assert!(p0 > 0.0 && p0 < 1.0);
12140        // Calibration temperature > 1 softens an over-confident peak.
12141        let sharp = top1_prob_t(&logits, 1, 1.0);
12142        let soft = top1_prob_t(&logits, 1, 2.0);
12143        assert!(soft < sharp, "higher temperature lowers peak confidence");
12144    }
12145
12146    #[test]
12147    fn trace_is_opt_in_and_parallels_the_output() {
12148        // Off by default: the runtime is silent unless observation asked.
12149        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
12150        p.sampler_config.temperature = 0.0;
12151        p.sampler_config.repetition_penalty = 1.0;
12152        let r = p.generate("abcd", 10, None, None).unwrap();
12153        assert!(r.traces.is_empty(), "trace must be empty unless enabled");
12154
12155        // On: exactly one row per emitted token, aligned with the output.
12156        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
12157        p.sampler_config.temperature = 0.0;
12158        p.sampler_config.repetition_penalty = 1.0;
12159        p.set_trace(true);
12160        let r = p.generate("abcd", 10, None, None).unwrap();
12161        assert_eq!(r.traces.len(), r.token_ids.len(), "one trace row per token");
12162        for (i, tr) in r.traces.iter().enumerate() {
12163            assert_eq!(tr.t, i, "trace index is sequential");
12164            assert_eq!(tr.token_id, r.token_ids[i], "trace token_id matches output");
12165            assert_eq!(
12166                tr.confidence, r.token_confidence[i],
12167                "trace confidence matches the confidence channel"
12168            );
12169            // No dynamic router in this pipeline → no skill, no coherence.
12170            assert!(tr.active_skill.is_none() && tr.recon.is_none() && !tr.switched);
12171        }
12172    }
12173
12174    #[test]
12175    fn explain_prefill_logits_match_greedy_first_token() {
12176        // `cortiq explain` shows the next-token distribution from
12177        // prefill_next_logits; its argmax must equal what greedy generate
12178        // actually emits first — otherwise explain would lie.
12179        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
12180        p.sampler_config.temperature = 0.0;
12181        p.sampler_config.repetition_penalty = 1.0;
12182        let ids = p.tokenizer.encode("abcd");
12183        let logits = p.prefill_next_logits(&ids, None);
12184        let argmax = logits
12185            .iter()
12186            .enumerate()
12187            .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
12188            .unwrap()
12189            .0 as u32;
12190        let r = p.generate("abcd", 1, None, None).unwrap();
12191        assert_eq!(
12192            argmax, r.token_ids[0],
12193            "explain preview must match greedy emit"
12194        );
12195    }
12196
12197    #[test]
12198    fn laguna_shared_expert_is_unconditionally_added() {
12199        let matrix = |values: Vec<f32>| QTensor::from_f32(values, 2, 2);
12200        let identity = || matrix(vec![1.0, 0.0, 0.0, 1.0]);
12201        let zero_dense = || DenseFfn {
12202            gate_proj: matrix(vec![0.0; 4]),
12203            up_proj: matrix(vec![0.0; 4]),
12204            down_proj: matrix(vec![0.0; 4]),
12205            act: Act::Silu,
12206            down_t: None,
12207            segs: Vec::new(),
12208        };
12209        let shared = DenseFfn {
12210            gate_proj: identity(),
12211            up_proj: identity(),
12212            down_proj: identity(),
12213            act: Act::Silu,
12214            down_t: None,
12215            segs: Vec::new(),
12216        };
12217        let x = [1.0, 2.0];
12218        let expected = dense_ffn(&shared, &x, None);
12219        let moe = MoeFfn {
12220            router: QTensor::from_f32(vec![0.0, 0.0], 1, 2),
12221            experts: vec![zero_dense()],
12222            top_k: 1,
12223            norm_topk_prob: true,
12224            router_sigmoid: true,
12225            expert_bias: None,
12226            routed_scaling: 1.0,
12227            route_tau: None,
12228            shared: Some((shared, None)),
12229            stats: std::cell::RefCell::new(Vec::new()),
12230            act_sq: std::cell::RefCell::new(Vec::new()),
12231            act_rows: std::cell::RefCell::new(Vec::new()),
12232            mask: None,
12233            per_expert_scale: None,
12234            router_input_norm: false,
12235            resonance: None,
12236        };
12237        let actual = moe_ffn_cpu(&moe, &x, &[0], &[0.0], 1.0, None);
12238        for (actual, expected) in actual.iter().zip(expected) {
12239            assert!((actual - expected).abs() < 1e-6);
12240        }
12241    }
12242}