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    /// DeepSeek-V4's own speculation stack: three draft modules, each a full
113    /// layer, plus a confidence head on the last. Empty when the file has
114    /// none, which is the only signal the decode path needs.
115    pub dsv4_mtp: Vec<crate::dsv4::Dsv4Mtp>,
116    /// The draft's per-sequence state (KV rings, captured trunk hidden).
117    pub dspark: Option<crate::dsv4::DsparkState>,
118    /// Drafts awaiting their verdict: (position, proposals, still matching,
119    /// accepted so far).
120    pub dspark_pending: Vec<(usize, Vec<u32>, bool, usize)>,
121    /// Accepted prefix length of every graded draft.
122    pub dspark_hist: Vec<usize>,
123    /// The real tokens the drafts were graded against — a degenerate,
124    /// repeating output would make any acceptance number meaningless, and
125    /// the cheapest guard against believing one is to count them.
126    pub dspark_real: Vec<u32>,
127    /// The trunk's expert picks for the last few tokens, per layer. The
128    /// union over a window of them is what a batched verify would have to
129    /// read, and the ratio to the pick count is all it could save.
130    pub dspark_trunk_picks: Vec<Vec<(usize, Vec<usize>)>>,
131    /// (unique, total) expert picks per draft, trunk side and draft side.
132    pub dspark_exp: Vec<(usize, usize, usize, usize)>,
133    /// Wall time spent in the deliberately out-of-core draft. Kept separate
134    /// from trunk decode so block batching can be judged without conflating
135    /// it with GPU chain variance.
136    pub dspark_draft_ns: u128,
137    /// LFM2 short-convolution geometry (present when the model has
138    /// `ShortConv` mixer layers).
139    pub short_conv_cfg: Option<ShortConvCfg>,
140    /// Multi-token-prediction head (None = absent).
141    pub mtp: Option<MtpModule>,
142    /// Speculative decode via MTP (greedy only; `CMF_MTP=0` disables).
143    pub speculative: bool,
144    rng: SplitMix64,
145    sampler_scratch: SamplerScratch,
146    /// Precomputed RoPE inverse frequencies [head_dim/2]. Arc: the
147    /// forward path clones a handle to escape the &mut self borrow —
148    /// cloning the table itself was a per-forward allocation.
149    pub(crate) inv_freq: std::sync::Arc<Vec<f32>>,
150    /// Reusable norm buffers for the decode hot path (roadmap §3 P0:
151    /// steady-state forward should not heap-allocate). Disjoint field
152    /// from `weights`/`kv_cache`, so split borrows keep working.
153    ws: ForwardScratch,
154    /// Persistent worker pool (None = serial; see CMF_THREADS).
155    pool: Option<std::sync::Arc<Pool>>,
156    // ── Dynamic per-token skill routing (spec §9, claim 14/16) ──
157    /// Source model, retained so a skill switch can re-resolve the
158    /// touched layers' FFN tensors (Mapped = mmap pointers, cheap).
159    pub(crate) model: Option<std::sync::Arc<cortiq_core::CmfModel>>,
160    /// Masks present → weights are dequantized f32 (rebuild path).
161    pub(crate) dyn_force_f32: bool,
162    /// Per-skill FFN layers actually replaced (derived from tensors, not
163    /// the meta `layers` field — ru2 replaces down_proj in 0..23 while
164    /// its meta says [20..23]). None = skill touches non-FFN tensors →
165    /// ineligible for cheap dynamic switching (honest refusal).
166    pub(crate) dyn_skill_layers: Vec<Option<Vec<usize>>>,
167    /// Currently overlaid skill (index into model.header.skills); None =
168    /// backbone. Set at load time to the statically-overlaid skill so
169    /// `set_active_skill(None)` correctly reverts it (else a static
170    /// skill would silently persist — the union-diff assumes dyn_active
171    /// always mirrors the live overlay). Switched by `set_active_skill`.
172    pub(crate) dyn_active: Option<usize>,
173    /// Pipeline was loaded with a soft blend (materialized working
174    /// tensors, not a single skill index) → dynamic routing refuses:
175    /// there is no single index to revert the blend from.
176    pub(crate) dyn_blend_loaded: bool,
177    /// Layer whose post-residual hidden feeds the router φ (shared by
178    /// swarm skills). None = φ capture off.
179    pub(crate) dyn_phi_layer: Option<usize>,
180    /// EMA of φ at `dyn_phi_layer` over the decode window (on-policy).
181    dyn_phi_ema: Vec<f32>,
182    dyn_phi_seen: usize,
183    /// Hysteresis router driving per-token skill switches during decode
184    /// (None = static/no dynamic routing). Taken out during generation.
185    pub dyn_router: Option<crate::swarm::DynRouter>,
186    /// O(1) Nyström attention setting (CLI/env/header-hint resolved by
187    /// the caller; None = plain cache attention everywhere).
188    o1_cfg: Option<crate::nystrom::O1Cfg>,
189    /// Bumped at every o1 seal — the GPU state mirror re-uploads when it
190    /// sees a new epoch (each generate seals fresh CPU state).
191    o1_epoch: u64,
192    /// Per-layer o1 flags derived from `o1_cfg` (Full layers only).
193    o1_flags: Vec<bool>,
194    /// Emit a structured per-token trace (B4 telemetry channel). Off by
195    /// default — the runtime is silent unless observation is requested.
196    trace: bool,
197    /// Confidence-calibration temperature (B1): reported Born mass is
198    /// softmax(logits / calib_temp). 1.0 = raw. Set from header.calibration.
199    calib_temp: f32,
200    /// Process-unique id keying this pipeline's device KV mirrors.
201    #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
202    graph_kv_id: u64,
203    /// Decode asks the token graph to also run final-norm + lm_head on
204    /// the device (drops the separate per-op lm_head round trip).
205    #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
206    graph_want_logits: bool,
207    /// Logits the graph produced for the token just forwarded (taken by
208    /// the decode loop; None = compute on the CPU path).
209    graph_logits: Option<Vec<f32>>,
210    /// Token embeddings are multiplied by this at input (Gemma: √hidden).
211    pub embed_multiplier: f32,
212    /// Attention score scale (1/√head_dim unless the arch overrides —
213    /// Gemma's query_pre_attn_scalar).
214    pub attn_scale: f32,
215    /// Sliding-window attention: (window, every-Nth-layer-is-global
216    /// pattern) — Gemma-3.
217    pub swa: Option<(usize, usize)>,
218    /// Explicit local/global schedule for architectures that cannot be
219    /// represented by Gemma's every-Nth-global convention.
220    pub sliding_layers: Option<Vec<bool>>,
221    /// RoPE table of the sliding (local) layers, when they use their
222    /// own base frequency (Gemma-3: 10k local vs 1M global).
223    pub inv_freq_local: Option<std::sync::Arc<Vec<f32>>>,
224    pub rotary_dim_local: Option<usize>,
225    pub rope_scale: f32,
226    pub rope_scale_local: f32,
227    /// Gemma-4: global layers run their own geometry — (head_dim,
228    /// num_kv_heads); sliding layers keep the base fields.
229    pub global_attn: Option<(usize, usize)>,
230    /// Gemma-4: the global layers' proportional RoPE table (len
231    /// global_head_dim/2, zero-padded tail = identity rotation).
232    pub inv_freq_global: Option<std::sync::Arc<Vec<f32>>>,
233    /// Scale-less RMS normalization of V heads before caching (Gemma-4).
234    pub attn_v_norm: bool,
235    /// Final-logit soft-capping C: logits = C·tanh(logits/C) (Gemma-4).
236    pub final_softcap: Option<f32>,
237    /// Gemma-2 attention-logit soft-capping (0.0 = off).
238    pub attn_softcap: f32,
239    /// Compute per-token Born confidence (a full-vocab softmax each
240    /// token). On by default; `bench --core` turns it off to match
241    /// llama-bench's core timing.
242    confidence_on: bool,
243}
244
245#[cfg(target_os = "macos")]
246impl Drop for Pipeline {
247    fn drop(&mut self) {
248        crate::gpu::kv_mirror_drop(self.graph_kv_id);
249    }
250}
251
252/// Model weights. Matrices are `QTensor` (owned f32 for small models
253/// and tests — bit-identical to the historical paths — or quantized
254/// bytes zero-copy from the CMF mmap for big models). 1-D norms are
255/// always small and stay f32.
256pub struct PipelineWeights {
257    /// Embedding table: [vocab_size, hidden_size]
258    pub embed_tokens: QTensor,
259    /// Per-layer weights
260    pub layers: Vec<LayerWeights>,
261    /// LM head: [vocab_size, hidden_size]
262    pub lm_head: QTensor,
263    /// Final norm: [hidden_size]
264    pub final_norm: Vec<f32>,
265}
266
267/// One transformer layer: shared norms + MLP, attention by kind.
268pub struct LayerWeights {
269    pub input_norm: Vec<f32>,
270    /// The pre-FFN norm (`post_attention_layernorm` classically;
271    /// `pre_feedforward_layernorm` on Gemma-2/3 sandwich layers).
272    pub post_norm: Vec<f32>,
273    /// Gemma-2/3 sandwich: norm applied to the ATTENTION OUTPUT before
274    /// its residual add (`post_attention_layernorm` there).
275    pub attn_out_norm: Option<Vec<f32>>,
276    /// Gemma-4: the whole layer output is multiplied by this scalar.
277    pub layer_scale: Option<f32>,
278    /// Gemma-2/3 sandwich: norm applied to the FFN OUTPUT before its
279    /// residual add (`post_feedforward_layernorm`).
280    pub ffn_out_norm: Option<Vec<f32>>,
281    pub ffn: FfnKind,
282    pub attn: AttnKind,
283}
284
285/// FFN gate activation: SiLU (SwiGLU family) or tanh-GELU (Gemma's
286/// GeGLU). A property of the model, carried on every FFN triple.
287#[derive(Clone, Copy, PartialEq, Debug, Default)]
288pub enum Act {
289    #[default]
290    Silu,
291    GeluTanh,
292    /// Kimi-K3 SituAndMul: BOTH halves transform —
293    /// a = β·tanh(g/β)·σ(g), up' = linβ·tanh(u/linβ) (linβ>0), out = a·up'.
294    Situ {
295        beta: f32,
296        linear_beta: f32,
297    },
298}
299
300impl Act {
301    pub fn from_arch(name: &str) -> Self {
302        if name == "gelu_tanh" {
303            Self::GeluTanh
304        } else {
305            Self::Silu
306        }
307    }
308
309    /// Arch-driven constructor (activation name + situ betas).
310    pub fn from_arch_full(arch: &cortiq_core::ModelArch) -> Self {
311        match arch.hidden_act.as_str() {
312            "situ" => Self::Situ {
313                beta: arch.activation_situ_beta.unwrap_or(1.0) as f32,
314                linear_beta: arch.activation_situ_linear_beta.unwrap_or(0.0) as f32,
315            },
316            other => Self::from_arch(other),
317        }
318    }
319
320    #[inline]
321    pub fn apply(self, x: f32) -> f32 {
322        match self {
323            Self::Silu => inference::silu(x),
324            Self::GeluTanh => inference::gelu_tanh(x),
325            Self::Situ { beta, .. } => beta * (x / beta).tanh() * (1.0 / (1.0 + (-x).exp())),
326        }
327    }
328
329    /// Gated combine — the FFN contract. Situ transforms the UP half
330    /// too, so callers must use this instead of apply(g)·u.
331    #[inline]
332    pub fn combine(self, g: f32, u: f32) -> f32 {
333        match self {
334            Self::Situ { linear_beta, .. } if linear_beta > 0.0 => {
335                self.apply(g) * (linear_beta * (u / linear_beta).tanh())
336            }
337            _ => self.apply(g) * u,
338        }
339    }
340}
341
342/// Dense gated triple — the FFN of a dense layer or of one expert.
343pub struct DenseFfn {
344    pub gate_proj: QTensor,
345    pub up_proj: QTensor,
346    pub down_proj: QTensor,
347    /// Gate activation (SiLU default; Gemma: tanh-GELU).
348    pub act: Act,
349}
350
351/// FFN operator of a layer, decided by tensor presence at load time
352/// (router `mlp.gate.weight` in the directory = MoE layer).
353pub enum FfnKind {
354    Dense(DenseFfn),
355    /// Mixture-of-Experts (Qwen2-MoE / Qwen3-MoE): softmax over ALL
356    /// expert logits → top-k, optional renorm; experts stay quantized
357    /// in mmap — only the selected ones are touched per token.
358    Moe(MoeFfn),
359    /// Gemma-4 MoE: a dense MLP branch AND a routed-expert branch in
360    /// the SAME layer, each with its own norm sandwich. The dense
361    /// branch reads the pre-FFN-normed input; the expert branch (and
362    /// the router) read the RAW residual through `pre_norm_2`:
363    ///   d = post_norm_1(dense(x̂));  m = post_norm_2(Σwₑ·FFNₑ(pre_norm_2(h)))
364    ///   ffn_out = d + m   (the caller's ffn_out_norm + residual follow)
365    DenseMoe(Box<DenseMoeFfn>),
366}
367
368/// Gemma-4 dual-branch FFN (see `FfnKind::DenseMoe`).
369pub struct DenseMoeFfn {
370    pub dense: DenseFfn,
371    pub moe: MoeFfn,
372    /// post_feedforward_layernorm_1 — dense-branch output norm.
373    pub post_norm_1: Vec<f32>,
374    /// pre_feedforward_layernorm_2 — expert-branch input norm (applied
375    /// to the RAW residual, not the pre-FFN-normed activation).
376    pub pre_norm_2: Vec<f32>,
377    /// post_feedforward_layernorm_2 — expert-branch output norm.
378    pub post_norm_2: Vec<f32>,
379}
380
381pub struct MoeFfn {
382    /// Router `mlp.gate.weight` [num_experts, hidden].
383    pub router: QTensor,
384    pub experts: Vec<DenseFfn>,
385    pub top_k: usize,
386    pub norm_topk_prob: bool,
387    /// Router scores per-expert with a sigmoid (LFM2-MoE / DeepSeek-V3
388    /// `noaux_tc`) instead of a softmax over all experts (Qwen).
389    pub router_sigmoid: bool,
390    /// Per-expert selection bias `mlp.expert_bias` [num_experts]
391    /// (LFM2-MoE): added to the sigmoid scores for the top-k CHOICE only;
392    /// the gathered weights use the unbiased scores. None = no bias.
393    pub expert_bias: Option<Vec<f32>>,
394    /// Top-k weights are multiplied by this after the optional renorm
395    /// (LFM2-MoE `routed_scaling_factor`; 1.0 = off).
396    pub routed_scaling: f32,
397    /// Adaptive routing (CMF_MOE_TAU, opt-in): keep the smallest
398    /// prefix of the top-k whose renormalized mass reaches τ —
399    /// confident tokens touch 1–2 experts, flat ones keep all k.
400    /// MoE decode is memory-bound, so skipped experts are skipped
401    /// weight traffic. None = classic fixed top-k (bit-identical).
402    pub route_tau: Option<f32>,
403    /// Always-on shared expert. Qwen2-MoE carries an additional sigmoid
404    /// gate; Laguna adds the shared expert unconditionally (`None`).
405    pub shared: Option<(DenseFfn, Option<QTensor>)>,
406    /// Expert-selection counters (truncated Fisher B-field of claim 12:
407    /// routing frequency during calibration). Filled by every forward,
408    /// read by the CLI via CMF_MOE_STATS. RefCell: decode is single-threaded.
409    pub stats: std::cell::RefCell<Vec<u64>>,
410    /// Per-CHANNEL sum of squares of this FFN's input, accumulated over a
411    /// calibration run (`CMF_RMS_TRACE`). These are the RMS activation
412    /// traces AWNP needs: raw weight magnitude says every channel matters
413    /// equally, and the question AWNP asks is whether the ACTIVATIONS
414    /// disagree. Off unless the env var is set — an f64 add per channel
415    /// per token is cheap, but not free.
416    pub act_sq: std::cell::RefCell<Vec<f64>>,
417    /// Raw FFN-input rows captured for the layers named by `CMF_ACT_DUMP`
418    /// (`"9,19"`). AWNP is nullspace PROJECTION: after dropping channels the
419    /// survivors are refitted to absorb what was removed, and how much they
420    /// can absorb depends on the activation COVARIANCE, not on per-channel
421    /// RMS. Per-channel numbers can only bound the cost from above.
422    pub act_rows: std::cell::RefCell<Vec<f32>>,
423    /// Task mask over routed experts (DTG-MA over MoE, claim-12 B-field
424    /// applied): `false` experts are excluded from selection, the
425    /// softmax renormalizes over the allowed set. Built by the loader
426    /// from CMF_MOE_MASK=<stats.json> + CMF_MOE_MASK_COVER. None = all.
427    pub mask: Option<Vec<bool>>,
428    /// Gemma-4: per-expert weight scale applied AFTER the top-k renorm
429    /// (`router.per_expert_scale`). None = 1.0 everywhere.
430    pub per_expert_scale: Option<Vec<f32>>,
431    /// Gemma-4: the router reads a SCALE-LESS rms-norm of its input
432    /// (the constant gain router.scale·√hidden is folded into the
433    /// router weights at convert time).
434    pub router_input_norm: bool,
435}
436
437/// Attention operator of a layer. Extension point: new operators are
438/// new variants here + a forward in their own module.
439pub enum AttnKind {
440    /// GQA softmax attention (+ optional Qwen3.5 qk-norm / output gate).
441    Full {
442        wq: QTensor,
443        wk: QTensor,
444        wv: QTensor,
445        wo: QTensor,
446        q_norm: Option<Vec<f32>>,
447        k_norm: Option<Vec<f32>>,
448        output_gate: bool,
449        /// Laguna: a separate softplus projection applied to the attention
450        /// output before O. The bool means one scalar per head (broadcast
451        /// across head_dim); false means one scalar per element.
452        softplus_gate: Option<(QTensor, bool)>,
453        /// Qwen2-family projection biases (q, k, v).
454        bias: Option<(Vec<f32>, Vec<f32>, Vec<f32>)>,
455    },
456    /// Canonical linear core (VMF phase attention).
457    Linear(VmfPhaseWeights),
458    /// Faithful vendor linear operator (Qwen3.5 GatedDeltaNet).
459    LinearGdn(GdnWeights),
460    /// LFM2 gated short-convolution mixer (no KV cache; conv ring state
461    /// lives in the layer's `linear_state`).
462    ShortConv(ShortConvWeights),
463    /// DeepSeek-V2 Multi-head Latent Attention. v1 executes it as
464    /// expand-to-MHA: the latent is projected per token, K/V expand to
465    /// every head and live in the ordinary cache (K head layout
466    /// [rope | nope] so the standard partial rotary covers the shared
467    /// rope key; V rows are zero-padded to the K head_dim and the pad
468    /// is sliced off before O). Latent-resident cache is a later
469    /// optimization, not a semantic change.
470    Mla(Box<MlaWeights>),
471    /// Kimi Delta Attention (Kimi Linear / Kimi-K3): per-channel decayed
472    /// delta rule, separate q/k/v short convs, sigmoid-gated output norm.
473    /// State lives in the layer's `linear_state` (no KV cache).
474    Kda(Box<crate::linear_core::KdaWeights>),
475}
476
477/// DeepSeek-V2 MLA projections (see `AttnKind::Mla`).
478pub struct MlaWeights {
479    /// `[nh·(rope+nope), hidden]` (or `[…, q_lora]` when compressed) —
480    /// the converter permutes each head rope-first so rotary_dim =
481    /// qk_rope works unchanged.
482    pub q_proj: QTensor,
483    /// Compressed q (K3/V3 class): x → q_a `[q_lora, hidden]` →
484    /// rms(q_a_norm) → q_proj (= q_b). None = direct q (V2-Lite).
485    pub q_a: Option<QTensor>,
486    pub q_a_norm: Option<Vec<f32>>,
487    /// `kv_a_proj_with_mqa` `[lora + rope, hidden]` (latent first).
488    pub kv_a: QTensor,
489    /// RMS-norm weights over the latent (`kv_a_layernorm`, [lora]).
490    pub kv_a_norm: Vec<f32>,
491    /// `[nh·(nope+v), lora]` — per head [k_nope | v].
492    pub kv_b: QTensor,
493    /// `[hidden, nh·v]`.
494    pub o_proj: QTensor,
495    pub nh: usize,
496    pub qk_rope: usize,
497    pub qk_nope: usize,
498    pub v_dim: usize,
499    pub lora: usize,
500    /// Softmax scale (1/√(rope+nope), YaRN-mscale-corrected at load).
501    pub scale: f32,
502    /// Kimi Linear NoPE: skip the rotary entirely (layout unchanged).
503    pub nope: bool,
504}
505
506/// Multi-token-prediction head (DeepSeek/Qwen style, spec §2.1):
507/// `x = eh_proj·[enorm(embed(next)); hnorm(hidden)]` → one transformer
508/// block over its own KV → shared lm_head. Drafts the token after next;
509/// the main model verifies, so output is exact — MTP only buys speed.
510pub struct MtpModule {
511    pub enorm: Vec<f32>,
512    pub hnorm: Vec<f32>,
513    /// [hidden, 2·hidden]
514    pub eh_proj: QTensor,
515    pub layer: LayerWeights,
516    pub final_norm: Vec<f32>,
517    pub kv: crate::kv_cache::LayerKvCache,
518}
519
520/// Result of a generation call.
521pub struct GenerateResult {
522    pub text: String,
523    pub token_ids: Vec<u32>,
524    pub prompt_tokens: usize,
525    pub tokens_generated: usize,
526    pub finish_reason: String,
527    /// Speculative-decode stats (0/0 when MTP is absent or inactive).
528    pub mtp_drafted: usize,
529    pub mtp_accepted: usize,
530    /// Per-generated-token confidence = softmax probability of the token
531    /// that was actually emitted (Born mass on the chosen state). High =
532    /// the model was sure; low = it was guessing. Same length as the
533    /// generated slice of `token_ids`.
534    pub token_confidence: Vec<f32>,
535    /// Structured per-token telemetry (B4 channel). Empty unless
536    /// `set_trace(true)`; otherwise same length as the generated slice.
537    pub traces: Vec<TokenTrace>,
538}
539
540/// One row of the structured telemetry trace (B4): the model's internal
541/// routing state at the moment a token was emitted. Every field is a
542/// quantity the runtime already computes — nothing is inferred or
543/// estimated (anti-principle: only measured bytes).
544#[derive(Clone, Debug)]
545pub struct TokenTrace {
546    /// 0-based index within the generated slice.
547    pub t: usize,
548    /// The emitted token id.
549    pub token_id: u32,
550    /// Born mass on the emitted token (softmax prob) — how sure the model was.
551    pub confidence: f32,
552    /// Skill in force while this token was generated (None = backbone).
553    pub active_skill: Option<String>,
554    /// Recon error E = ‖r−BBᵀr‖²/‖φ‖² at the last routing eval — coherence
555    /// with the active skill's subspace (low = coherent). None = no router
556    /// or not yet evaluated.
557    pub recon: Option<f32>,
558    /// The router changed the active skill right after this token (a
559    /// domain boundary crossed under the hysteresis barrier).
560    pub switched: bool,
561}
562
563/// Calibrated softmax probability of `id` under `logits` (the Born mass on
564/// the emitted token) — the confidence signal, cheap from logits already
565/// computed for sampling. `temp` is the calibration temperature (B1):
566/// softmax(logits / temp); 1.0 = raw.
567fn top1_prob_t(logits: &[f32], id: u32, temp: f32) -> f32 {
568    let t = if temp > 1e-3 { temp } else { 1.0 };
569    let max = logits.iter().fold(f32::NEG_INFINITY, |m, &v| m.max(v));
570    let sum: f32 = logits.iter().map(|&v| ((v - max) / t).exp()).sum();
571    if sum > 0.0 {
572        (((logits[id as usize] - max) / t).exp()) / sum
573    } else {
574        0.0
575    }
576}
577
578/// prefill-GEMM enabled? (CMF_PREFILL=seq — emergency fallback to the
579/// sequential path.)
580fn prefill_batched() -> bool {
581    std::env::var("CMF_PREFILL")
582        .map(|v| v != "seq")
583        .unwrap_or(true)
584}
585
586/// Input to the layer-major batched span walk: token ids (embeds itself,
587/// full-stack and coordinator prefill) or ready boundary hiddens (the
588/// network worker's side of a split).
589#[derive(Clone, Copy)]
590enum PrefillIn<'a> {
591    Ids(&'a [u32]),
592    Hidden(&'a [f32]),
593}
594
595/// The batched prefill walks `weights.layers`. Architectures that load
596/// their own stack (gemma-3n's AltUp replicas, DeepSeek-V4's hyper-
597/// connections) leave that empty and must go position by position — asking
598/// otherwise indexes an empty vector, which is a panic rather than a
599/// fallback. Every call site goes through here so the next such
600/// architecture is one line, not four.
601impl Pipeline {
602    fn can_prefill_batched(&self) -> bool {
603        prefill_batched() && !self.weights.layers.is_empty()
604    }
605}
606
607/// Prefill chunk (positions per batched pass). On macOS the AMX GEMM
608/// path wants tall panels — M=48 starves the matrix units (ggml uses
609/// ubatch 512); elsewhere the historical 48 stays. CMF_PREFILL_CHUNK
610/// overrides. Pub: the network split MUST chunk identically to the
611/// local path — panel width reorders float accumulation, so a different
612/// chunk is a different (equally valid) generation.
613pub fn prefill_chunk() -> usize {
614    if let Some(n) = std::env::var("CMF_PREFILL_CHUNK")
615        .ok()
616        .and_then(|v| v.parse::<usize>().ok())
617    {
618        return n.max(1);
619    }
620    if cfg!(target_os = "macos") {
621        512
622    } else if cfg!(target_arch = "aarch64") {
623        // Mobile: big enough to feed the batched attend (gate b ≥ 32)
624        // and the blocked SDOT GEMM without the memory of 512.
625        256
626    } else {
627        48
628    }
629}
630
631/// Callback for streaming tokens. Return `false` to cancel.
632pub type TokenCallback = Box<dyn FnMut(&str) -> bool + Send>;
633
634impl Pipeline {
635    /// Map a virtual layer index to its physical weight index.
636    /// Looped Transformer (Nanbeige 4.2): 22 physical layers × 2 loops = 44 virtual;
637    /// virtual layer 23 maps back to physical layer 1 (23 % 22 = 1).
638    #[inline]
639    pub fn phys_layer(&self, virtual_idx: usize) -> usize {
640        virtual_idx % self.physical_layers
641    }
642
643    /// True when `virtual_idx` is the last layer of a loop iteration
644    /// (used for loop_final_norm insertion).
645    #[inline]
646    pub fn is_loop_end(&self, virtual_idx: usize) -> bool {
647        self.loop_final_norm && (virtual_idx + 1) % self.physical_layers == 0
648    }
649
650    /// Build a pipeline from parts (used by the loader and tests).
651    #[allow(clippy::too_many_arguments)]
652
653    /// Whole-block q1 token graph on the GPU (macOS/Metal): the run of
654    /// consecutive q1 layers — GDN *and* full attention — starting at
655    /// `start` executes as few command buffers as the CPU truly needs.
656    /// Hidden stays device-resident across every layer; the only syncs
657    /// are before each CPU attend (it needs q/k/v and owns the KV
658    /// cache) and the final hidden readback. Recurrent states
659    /// round-trip through shared memory (the CPU stays their owner, so
660    /// every other path remains coherent). Returns the first layer
661    /// index NOT covered (== `start` → refused, caller falls through
662    /// to the per-layer CPU path).
663    /// Should prefill run position-by-position through the GPU token
664    /// graph instead of the batched CPU chunk-GEMM? True for q1 GDN
665    /// hybrids on native Metal: their chunk prefill is walled by the
666    /// sequential scalar recurrence, so the graph's decode rate wins.
667    /// NOT for Looped Transformers, despite the per-chunk loop_final_norm
668    /// sync: the chunk-GEMM amortizes each weight over the whole chunk,
669    /// which the per-position graph cannot (Nanbeige 4.2 on M4, 512-token
670    /// prompt: 85 tok/s chunked vs 14 through the graph).
671    #[cfg(target_os = "macos")]
672    fn graph_prefill_preferred(&self) -> bool {
673        if !crate::gpu::enabled_here()
674            || !crate::gpu::q1_force()
675            || std::env::var("CMF_GPU_BLOCK")
676                .map(|v| v == "0")
677                .unwrap_or(false)
678        {
679            return false;
680        }
681        self.weights
682            .layers
683            .iter()
684            .any(|lw| matches!(&lw.attn, AttnKind::LinearGdn(w) if w.in_proj_qkv.is_q1()))
685    }
686
687    #[cfg(not(target_os = "macos"))]
688    fn graph_prefill_preferred(&self) -> bool {
689        // Discrete-GPU wgpu whole-token graph: GDN layers carry recurrent state
690        // (conv ring + delta-rule S) resident on the GPU. A batched CPU prefill
691        // builds that state on the CPU only, leaving the GPU buffers zeroed at
692        // decode → garbage. Route GDN-hybrid prefill through the graph one
693        // position at a time so the resident state is seeded exactly as decode
694        // will read it. Pure-attention models keep the batched CPU prefill (its
695        // KV mirror re-syncs from the CPU cache, so no seeding gap).
696        let graph_on = std::env::var("CMF_GPU_WGPU_GRAPH")
697            .map(|v| v != "0")
698            .unwrap_or_else(|_| {
699                // Default ON for wgpu on DISCRETE adapters (4090:
700                // decode 76 -> 137 tok/s); integrated/mobile GPUs keep
701                // the per-op probe path — see gpu::wgpu_graph_default.
702                crate::gpu::wgpu_graph_default()
703            });
704        if !graph_on || !crate::gpu::enabled_here() {
705            return false;
706        }
707        self.weights
708            .layers
709            .iter()
710            .any(|lw| matches!(&lw.attn, AttnKind::LinearGdn(_)))
711    }
712
713    #[cfg(target_os = "macos")]
714    fn q1_graph_gpu(
715        &mut self,
716        start: usize,
717        upto: Option<usize>,
718        position: usize,
719        h: &mut [f32],
720    ) -> usize {
721        use crate::gpu::{AttnGpuLayer, GdnGpuCfg, GdnGpuLayer, GraphDims, MetalFfn, TokenGraph};
722        if self.attn_softcap > 0.0 // capped scores: no graph kernel — CPU path
723            || !crate::gpu::enabled_here()
724            || !crate::gpu::q1_force()
725            || std::env::var("CMF_GPU_BLOCK")
726                .map(|v| v == "0")
727                .unwrap_or(false)
728        {
729            if std::env::var("CMF_GRAPH_DBG").is_ok() {
730                eprintln!(
731                    "block-graph: front gate (softcap={} enabled_here={} q1_force={})",
732                    self.attn_softcap > 0.0,
733                    crate::gpu::enabled_here(),
734                    crate::gpu::q1_force(),
735                );
736            }
737            return start;
738        }
739        // The graph encodes SiLU FFN, 1/√hd attention scores and
740        // full-context attend with no branch norms — Gemma-style archs
741        // (sliding window, scale override, sandwich norms, GeLU) fall
742        // back to the CPU path.
743        if self.swa.is_some()
744            || self.global_attn.is_some()
745            || self.attention_heads_per_layer.is_some()
746            || self.attn_v_norm
747            || (self.attn_scale - 1.0 / (self.head_dim as f32).sqrt()).abs() > 1e-9
748            || self.weights.layers.iter().any(|lw| {
749                lw.attn_out_norm.is_some()
750                    || lw.ffn_out_norm.is_some()
751                    || lw.layer_scale.is_some()
752                    || matches!(&lw.ffn, FfnKind::Dense(d) if d.act != Act::Silu)
753            })
754        {
755            if std::env::var("CMF_GRAPH_DBG").is_ok() {
756                eprintln!(
757                    "block-graph: arch ineligible (swa={} gattn={} hpl={} vnorm={} scale_delta={:.2e})",
758                    self.swa.is_some(),
759                    self.global_attn.is_some(),
760                    self.attention_heads_per_layer.is_some(),
761                    self.attn_v_norm,
762                    (self.attn_scale - 1.0 / (self.head_dim as f32).sqrt()).abs(),
763                );
764            }
765            return start;
766        }
767        // Looped Transformer: the graph covers ALL loop iterations;
768        // encode_loop_norm is inserted on-device at each boundary.
769        let limit = upto
770            .map(|u| u + 1)
771            .unwrap_or(self.num_layers)
772            .min(self.num_layers);
773
774        enum Item<'a> {
775            Gdn {
776                run: Vec<GdnGpuLayer<'a>>,
777                first: usize,
778            },
779            Attn {
780                l: AttnGpuLayer<'a>,
781                li: usize,
782                q_norm: Option<&'a [f32]>,
783                k_norm: Option<&'a [f32]>,
784                output_gate: bool,
785                bias: Option<(&'a [f32], &'a [f32], &'a [f32])>,
786                /// Attend on the device too (no sync): F32 KV, no
787                /// o1/bias, dims inside the kernels' contract.
788                full_gpu: bool,
789            },
790        }
791
792        // Device-attend KERNEL contract, shared by every Full layer. The
793        // hd>128 default-off POLICY is applied after the scan: it was
794        // measured on dense models, and a MoE plan inverts it — with the
795        // experts on device each CPU-attend sandwich costs a
796        // commit+wait, ~30 submits/token (W2 on M4: 14.7 tok/s
797        // sandwiched vs 27.1 device-attend vs 18.8 pure CPU).
798        let attend_mode = std::env::var("CMF_GPU_ATTEND").unwrap_or_else(|_| "auto".into());
799        let attend_contract = attend_mode != "0"
800            && attend_mode != "off"
801            && self.head_dim % 4 == 0
802            && self.head_dim <= 256
803            && self.rotary_dim >= 2
804            && self.rotary_dim <= self.head_dim
805            && (self.rotary_dim / 2) % 32 == 0
806            && self.num_kv_heads > 0
807            && self.num_heads % self.num_kv_heads == 0;
808
809        let mut plan: Vec<Item> = Vec::new();
810        let mut model_ref: Option<std::sync::Arc<cortiq_core::CmfModel>> = None;
811        // Break-reason diagnostics ride the same env as the plan summary.
812        let block_diag = std::env::var("CMF_GRAPH_DBG").is_ok();
813        let mut scan = start;
814        while scan < limit {
815            let lw = &self.weights.layers[self.phys_layer(scan)];
816            let ffn = match &lw.ffn {
817                FfnKind::Dense(d) => {
818                    let (Some(g), Some(u), Some(dn)) = (
819                        d.gate_proj.q1_parts(),
820                        d.up_proj.q1_parts(),
821                        d.down_proj.q1_parts(),
822                    ) else {
823                        if block_diag {
824                            eprintln!(
825                                "block-graph: L{scan} FFN trio not graph-mappable — run ends"
826                            );
827                        }
828                        break;
829                    };
830                    MetalFfn::Dense {
831                        gate: g,
832                        up: u,
833                        down: dn,
834                    }
835                }
836                FfnKind::Moe(m) => {
837                    let Some(moe) = metal_moe_graph_parts(m, self.hidden_size) else {
838                        if block_diag {
839                            eprintln!(
840                                "block-graph: L{scan} MoE outside the graph contract — run ends"
841                            );
842                        }
843                        break;
844                    };
845                    if let QTensor::Mapped { model, .. } = &m.experts[0].gate_proj {
846                        model_ref.get_or_insert_with(|| model.clone());
847                    }
848                    MetalFfn::Moe(moe)
849                }
850                _ => {
851                    if block_diag {
852                        eprintln!("block-graph: L{scan} non-graph FFN — run ends");
853                    }
854                    break;
855                }
856            };
857            match &lw.attn {
858                AttnKind::LinearGdn(w) if self.gdn_cfg.is_some() => {
859                    let parts = (
860                        w.in_proj_qkv.q1_parts(),
861                        w.in_proj_z.q1_parts(),
862                        w.in_proj_a.f32_parts(),
863                        w.in_proj_b.f32_parts(),
864                        w.out_proj.q1_parts(),
865                    );
866                    let (Some(qkv), Some(z), Some(a), Some(b), Some(out)) = parts else {
867                        if block_diag {
868                            eprintln!(
869                                "block-graph: L{scan} GDN parts refused (qkv={} z={} a_f32={} b_f32={} out={})",
870                                w.in_proj_qkv.q1_parts().is_some(),
871                                w.in_proj_z.q1_parts().is_some(),
872                                w.in_proj_a.f32_parts().is_some(),
873                                w.in_proj_b.f32_parts().is_some(),
874                                w.out_proj.q1_parts().is_some(),
875                            );
876                        }
877                        break;
878                    };
879                    if let QTensor::Mapped { model, .. } = &w.in_proj_qkv {
880                        model_ref.get_or_insert_with(|| model.clone());
881                    }
882                    let gl = GdnGpuLayer {
883                        attn_norm: &lw.input_norm,
884                        post_norm: &lw.post_norm,
885                        qkv,
886                        z,
887                        a,
888                        b,
889                        out,
890                        ffn,
891                        conv1d: &w.conv1d,
892                        a_log: &w.a_log,
893                        dt_bias: &w.dt_bias,
894                        gnorm: &w.norm,
895                    };
896                    match plan.last_mut() {
897                        Some(Item::Gdn { run, .. }) => run.push(gl),
898                        _ => plan.push(Item::Gdn {
899                            run: vec![gl],
900                            first: scan,
901                        }),
902                    }
903                }
904                AttnKind::Full {
905                    wq,
906                    wk,
907                    wv,
908                    wo,
909                    q_norm,
910                    k_norm,
911                    output_gate,
912                    softplus_gate: None,
913                    bias,
914                } if !self.kv_cache.layers[scan].o1_sealed() => {
915                    let parts = (wq.q1_parts(), wk.q1_parts(), wv.q1_parts(), wo.q1_parts());
916                    let (Some(pq), Some(pk), Some(pv), Some(po)) = parts else {
917                        break;
918                    };
919                    if let QTensor::Mapped { model, .. } = wq {
920                        model_ref.get_or_insert_with(|| model.clone());
921                    }
922                    let cache = &self.kv_cache.layers[scan];
923                    let full_gpu = attend_contract
924                        && cache.mode == crate::kv_cache::KvMode::F32
925                        && cache.o1.is_none()
926                        && bias.is_none()
927                        && pq.1 == self.num_heads * self.head_dim * (1 + *output_gate as usize)
928                        && pk.1 == self.num_kv_heads * self.head_dim
929                        && pv.1 == self.num_kv_heads * self.head_dim
930                        && po.2 == self.num_heads * self.head_dim;
931                    plan.push(Item::Attn {
932                        l: AttnGpuLayer {
933                            attn_norm: &lw.input_norm,
934                            post_norm: &lw.post_norm,
935                            wq: pq,
936                            wk: pk,
937                            wv: pv,
938                            wo: po,
939                            ffn,
940                        },
941                        li: scan,
942                        q_norm: q_norm.as_deref(),
943                        k_norm: k_norm.as_deref(),
944                        output_gate: *output_gate,
945                        bias: bias
946                            .as_ref()
947                            .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
948                        full_gpu,
949                    });
950                }
951                _ => break,
952            }
953            scan += 1;
954        }
955        let Some(model) = model_ref else {
956            if std::env::var("CMF_GRAPH_DBG").is_ok() {
957                eprintln!("q1-graph: no model ref (start {start}, scanned to {scan})");
958            }
959            return start;
960        };
961        if plan.is_empty() {
962            if std::env::var("CMF_GRAPH_DBG").is_ok() {
963                eprintln!("q1-graph: empty plan at layer {start}");
964            }
965            return start;
966        }
967        let has_moe = plan.iter().any(|it| match it {
968            Item::Gdn { run, .. } => run.iter().any(|l| matches!(l.ffn, MetalFfn::Moe(_))),
969            Item::Attn { l, .. } => matches!(l.ffn, MetalFfn::Moe(_)),
970        });
971        let dev_attend = attend_contract
972            && (self.head_dim <= 128
973                || has_moe
974                || attend_mode == "force"
975                || attend_mode == "256");
976        if !dev_attend {
977            for it in &mut plan {
978                if let Item::Attn { full_gpu, .. } = it {
979                    *full_gpu = false;
980                }
981            }
982        }
983        if std::env::var("CMF_GRAPH_DBG").is_ok() {
984            use std::sync::atomic::{AtomicBool, Ordering};
985            static SAID: AtomicBool = AtomicBool::new(false);
986            if !SAID.swap(true, Ordering::Relaxed) {
987                let fg = plan
988                    .iter()
989                    .filter(|it| matches!(it, Item::Attn { full_gpu: true, .. }))
990                    .count();
991                let att = plan
992                    .iter()
993                    .filter(|it| matches!(it, Item::Attn { .. }))
994                    .count();
995                eprintln!(
996                    "q1-graph: plan of {} items from layer {start} to {scan} | dev_attend={dev_attend} full_gpu {fg}/{att} | hd={} rd={} nkv={} nh={}",
997                    plan.len(),
998                    self.head_dim,
999                    self.rotary_dim,
1000                    self.num_kv_heads,
1001                    self.num_heads,
1002                );
1003            }
1004        }
1005        let dims = GraphDims {
1006            hidden: self.hidden_size,
1007            eps: self.rms_eps as f32,
1008            gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
1009        };
1010        let Some(mut graph) = TokenGraph::new(&model, dims, h) else {
1011            return start;
1012        };
1013        let gcfg = self.gdn_cfg.map(|cfg| GdnGpuCfg {
1014            nv: cfg.num_v_heads,
1015            nk: cfg.num_k_heads,
1016            dk: cfg.key_head_dim,
1017            dv: cfg.value_head_dim,
1018            kk: cfg.conv_kernel,
1019            hidden: self.hidden_size,
1020            inter: self.intermediate_size,
1021            c_dim: cfg.conv_dim(),
1022            eps: cfg.rms_eps as f32,
1023            gemma: self.norm_style == cortiq_core::NormStyle::Gemma,
1024        });
1025        // Validate the whole plan BEFORE encoding anything: after the
1026        // first sync a refused layer would leave the token
1027        // half-executed, so truncate to the provably encodable prefix.
1028        let mut valid = 0usize;
1029        let mut end = start;
1030        for item in &plan {
1031            let ok = match item {
1032                Item::Gdn { run, .. } => gcfg
1033                    .as_ref()
1034                    .map(|gc| run.iter().all(|l| graph.gdn_ok(l, gc)))
1035                    .unwrap_or(false),
1036                Item::Attn { l, .. } => graph.attn_ok(l),
1037            };
1038            if !ok {
1039                if block_diag {
1040                    eprintln!(
1041                        "block-graph: plan item {} ({}) failed graph preflight",
1042                        valid,
1043                        match item {
1044                            Item::Gdn { run, first } =>
1045                                format!("GDN run L{first}+{}", run.len()),
1046                            Item::Attn { li, .. } => format!("Attn L{li}"),
1047                        }
1048                    );
1049                }
1050                break;
1051            }
1052            valid += 1;
1053            end += match item {
1054                Item::Gdn { run, .. } => run.len(),
1055                Item::Attn { .. } => 1,
1056            };
1057        }
1058        plan.truncate(valid);
1059        if plan.is_empty() {
1060            return start;
1061        }
1062
1063        let inv_freq = self.inv_freq.clone();
1064        let pool = self.pool.clone();
1065        let (nh, nkv, hd, hs, rd, eps) = (
1066            self.num_heads,
1067            self.num_kv_heads,
1068            self.head_dim,
1069            self.hidden_size,
1070            self.rotary_dim,
1071            self.rms_eps,
1072        );
1073        let norm_style = self.norm_style;
1074        let gemma = norm_style == cortiq_core::NormStyle::Gemma;
1075        let want = self.gdn_cfg.map(|c| c.state_len()).unwrap_or(0);
1076        let kv_id = self.graph_kv_id;
1077        // GDN runs whose states await readback after the next sync
1078        // (device-attended layers add no sync, so several may stack).
1079        let mut pending: Vec<(usize, usize)> = Vec::new();
1080        // Device-attended layers: their K/V/imp are pulled from the
1081        // mirror after the final sync.
1082        let mut dev_attn: Vec<usize> = Vec::new();
1083        for item in &plan {
1084            // Looped Transformer: insert on-device norm at loop boundaries.
1085            if self.loop_final_norm {
1086                let item_start = match item {
1087                    Item::Gdn { first, .. } => *first,
1088                    Item::Attn { li, .. } => *li,
1089                };
1090                if item_start > start && self.is_loop_end(item_start - 1) {
1091                    graph.encode_loop_norm(&self.weights.final_norm);
1092                }
1093            }
1094            match item {
1095                Item::Gdn { run, first } => {
1096                    for l in &mut self.kv_cache.layers[*first..*first + run.len()] {
1097                        if l.linear_state.len() != want {
1098                            l.linear_state = vec![0f32; want];
1099                        }
1100                    }
1101                    let ro: Vec<&[f32]> = self.kv_cache.layers[*first..*first + run.len()]
1102                        .iter()
1103                        .map(|l| l.linear_state.as_slice())
1104                        .collect();
1105                    if !graph.encode_gdn_run(run, &ro, gcfg.as_ref().unwrap()) {
1106                        // Unreachable: the plan was validated above.
1107                        tracing::error!("q1 graph: GDN run refused after validation");
1108                        return start;
1109                    }
1110                    // Early commit: the GPU starts the run while the
1111                    // CPU encodes the next layer (nothing to wait on).
1112                    graph.commit();
1113                    pending.push((*first, run.len()));
1114                }
1115                Item::Attn {
1116                    l,
1117                    li,
1118                    q_norm,
1119                    k_norm,
1120                    output_gate,
1121                    bias,
1122                    full_gpu,
1123                } => {
1124                    // ── Fully device-resident attention: no sync at all.
1125                    if *full_gpu {
1126                        let cache = &self.kv_cache.layers[*li];
1127                        let cpu_k: Vec<&[f32]> = (0..nkv).map(|g| cache.head_keys(g)).collect();
1128                        let cpu_v: Vec<&[f32]> = (0..nkv).map(|g| cache.head_values(g)).collect();
1129                        let cpu_stored = cpu_k[0].len() / hd;
1130                        let p = crate::gpu::AttnDeviceParams {
1131                            kv_id,
1132                            layer: *li,
1133                            nh,
1134                            nkv,
1135                            hd,
1136                            rd,
1137                            position,
1138                            eps: eps as f32,
1139                            gemma,
1140                            output_gate: *output_gate,
1141                            q_norm: *q_norm,
1142                            k_norm: *k_norm,
1143                            inv_freq: &inv_freq,
1144                            cpu_k,
1145                            cpu_v,
1146                            cpu_stored,
1147                        };
1148                        if graph.attn_device_ok(l, &p) && graph.encode_attn_device(l, &p) {
1149                            graph.commit();
1150                            dev_attn.push(*li);
1151                            continue;
1152                        }
1153                        // Mirror refused (nothing encoded) → sandwich.
1154                    }
1155                    graph.encode_attn_prefix(l);
1156                    graph.sync();
1157                    if !pending.is_empty() {
1158                        let idxs: Vec<usize> =
1159                            pending.drain(..).flat_map(|(f, n)| f..f + n).collect();
1160                        let mut outs: Vec<&mut [f32]> = self
1161                            .kv_cache
1162                            .layers
1163                            .iter_mut()
1164                            .enumerate()
1165                            .filter(|(i, _)| idxs.binary_search(i).is_ok())
1166                            .map(|(_, s)| s.linear_state.as_mut_slice())
1167                            .collect();
1168                        graph.read_states(&mut outs);
1169                    }
1170                    let mut q_raw = attention::take_buf(l.wq.1);
1171                    let mut k = attention::take_buf(l.wk.1);
1172                    let mut v = attention::take_buf(l.wv.1);
1173                    graph.read_qkv(&mut q_raw, &mut k, &mut v);
1174                    let cfg = QwenAttnCfg {
1175                        num_heads: nh,
1176                        num_kv_heads: nkv,
1177                        head_dim: hd,
1178                        hidden_size: hs,
1179                        position,
1180                        inv_freq: &inv_freq,
1181                        rotary_dim: rd,
1182                        scale: self.attn_scale,
1183                        softcap: self.attn_softcap,
1184                        window: None,
1185                        v_norm: false,
1186                        q_norm: *q_norm,
1187                        k_norm: *k_norm,
1188                        output_gate: *output_gate,
1189                        softplus_gate: None,
1190                        rope_scale: 1.0,
1191                        bias: *bias,
1192                        rms_eps: eps,
1193                        norm_style,
1194                        pool: pool.as_deref(),
1195                    };
1196                    let mut ao = attention::qwen_attention_core(
1197                        q_raw,
1198                        k,
1199                        v,
1200                        &mut self.kv_cache.layers[*li],
1201                        &cfg,
1202                    );
1203                    graph.encode_attn_suffix(l, &ao);
1204                    // Early commit: the GPU starts O+FFN while the CPU
1205                    // encodes the following GDN run / attention prefix.
1206                    graph.commit();
1207                    attention::recycle_buf(&mut ao);
1208                }
1209            }
1210        }
1211        // Ride the final norm + lm_head in the same command buffer when
1212        // this run reaches the model's end and the caller wants logits:
1213        // the separate per-op lm_head submit (a full round trip) folds
1214        // into the sync that already happens here.
1215        let mut lm_rows = None;
1216        if self.graph_want_logits
1217            && upto.is_none()
1218            && end == self.num_layers
1219            && std::env::var("CMF_GPU_LMHEAD")
1220                .map(|v| v != "0")
1221                .unwrap_or(true)
1222        {
1223            if let Some(lm) = self.weights.lm_head.q1_parts() {
1224                if graph.lm_head_ok(lm) {
1225                    graph.encode_lm_head(&self.weights.final_norm, lm);
1226                    lm_rows = Some(lm.1);
1227                }
1228            }
1229        }
1230        graph.sync();
1231        if !pending.is_empty() {
1232            let idxs: Vec<usize> = pending.drain(..).flat_map(|(f, n)| f..f + n).collect();
1233            let mut outs: Vec<&mut [f32]> = self
1234                .kv_cache
1235                .layers
1236                .iter_mut()
1237                .enumerate()
1238                .filter(|(i, _)| idxs.binary_search(i).is_ok())
1239                .map(|(_, s)| s.linear_state.as_mut_slice())
1240                .collect();
1241            graph.read_states(&mut outs);
1242        }
1243        if let Some(rows) = lm_rows {
1244            let mut lg = attention::take_buf(rows.min(self.vocab_size));
1245            graph.read_logits(&mut lg);
1246            lg.resize(self.vocab_size, 0.0);
1247            if let Some(c) = self.final_softcap {
1248                for l in lg.iter_mut() {
1249                    *l = c * (*l / c).tanh();
1250                }
1251            }
1252            self.graph_logits = Some(lg);
1253        }
1254        graph.finish(h);
1255        // Device-attended layers: replay the CPU bookkeeping — append
1256        // the mirror's new K/V row (rope'd on the GPU) into the owner
1257        // cache, then bank this token's Born-importance mass.
1258        for li in dev_attn {
1259            let mut krow = attention::take_buf(nkv * hd);
1260            let mut vrow = attention::take_buf(nkv * hd);
1261            if crate::gpu::kv_mirror_read_last(kv_id, li, nkv, hd, &mut krow, &mut vrow) {
1262                let cache = &mut self.kv_cache.layers[li];
1263                cache.append(&krow, &vrow, &[]);
1264                let n = cache.seq_len;
1265                let mut imp = attention::take_buf(n);
1266                crate::gpu::kv_mirror_take_imp(kv_id, li, &mut imp);
1267                cache.accumulate_imp(&imp);
1268                attention::recycle_buf(&mut imp);
1269            }
1270            attention::recycle_buf(&mut krow);
1271            attention::recycle_buf(&mut vrow);
1272        }
1273        end
1274    }
1275
1276    pub fn new(
1277        tokenizer: Tokenizer,
1278        weights: PipelineWeights,
1279        hidden_size: usize,
1280        intermediate_size: usize,
1281        num_heads: usize,
1282        num_kv_heads: usize,
1283        head_dim: usize,
1284        num_layers: usize,
1285        physical_layers: usize,
1286        loop_final_norm: bool,
1287        vocab_size: usize,
1288        rms_eps: f64,
1289        rope_base: f32,
1290        norm_style: NormStyle,
1291        max_seq_len: usize,
1292        sampler_config: SamplerConfig,
1293    ) -> Self {
1294        let rng = match sampler_config.seed {
1295            Some(s) => SplitMix64::new(s),
1296            None => SplitMix64::from_entropy(),
1297        };
1298        let inv_freq = std::sync::Arc::new(attention::rope_inv_freq(head_dim, rope_base));
1299        let pool = Pool::from_env();
1300        if let Some(p) = &pool {
1301            tracing::info!("worker pool: {} threads", p.n_workers());
1302        }
1303        Self {
1304            gpu_plan: None,
1305            tokenizer: std::sync::Arc::new(tokenizer),
1306            kv_cache: KvCache::new(num_layers, num_kv_heads, head_dim, max_seq_len),
1307            sampler_config,
1308            weights,
1309            hidden_size,
1310            intermediate_size,
1311            num_heads,
1312            num_kv_heads,
1313            head_dim,
1314            num_layers,
1315            physical_layers,
1316            loop_final_norm,
1317            vocab_size,
1318            rms_eps,
1319            rope_base,
1320            norm_style,
1321            rotary_dim: head_dim,
1322            attention_heads_per_layer: None,
1323            vmf_cfg: None,
1324            gdn_cfg: None,
1325            kda_cfg: None,
1326            g3n: None,
1327            dsv4: None,
1328            dsv4_mtp: Vec::new(),
1329            dspark: None,
1330            dspark_pending: Vec::new(),
1331            dspark_hist: Vec::new(),
1332            dspark_real: Vec::new(),
1333            dspark_trunk_picks: Vec::new(),
1334            dspark_exp: Vec::new(),
1335            dspark_draft_ns: 0,
1336            logit_multiplier: None,
1337            cancel: std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)),
1338            kv_history: Vec::new(),
1339            short_conv_cfg: None,
1340            mtp: None,
1341            speculative: std::env::var("CMF_MTP").map(|v| v != "0").unwrap_or(true),
1342            rng,
1343            sampler_scratch: SamplerScratch::default(),
1344            inv_freq,
1345            ws: ForwardScratch::new(hidden_size),
1346            pool,
1347            model: None,
1348            dyn_force_f32: false,
1349            dyn_skill_layers: Vec::new(),
1350            dyn_active: None,
1351            dyn_blend_loaded: false,
1352            dyn_phi_layer: None,
1353            dyn_phi_ema: Vec::new(),
1354            dyn_phi_seen: 0,
1355            dyn_router: None,
1356            o1_cfg: None,
1357            o1_epoch: 0,
1358            o1_flags: Vec::new(),
1359            trace: false,
1360            calib_temp: 1.0,
1361            confidence_on: true,
1362            embed_multiplier: 1.0,
1363            attn_scale: 1.0 / (head_dim as f32).sqrt(),
1364            swa: None,
1365            sliding_layers: None,
1366            inv_freq_local: None,
1367            rotary_dim_local: None,
1368            rope_scale: 1.0,
1369            rope_scale_local: 1.0,
1370            global_attn: None,
1371            inv_freq_global: None,
1372            attn_v_norm: false,
1373            final_softcap: None,
1374            attn_softcap: 0.0,
1375            graph_want_logits: false,
1376            graph_logits: None,
1377            graph_kv_id: {
1378                static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
1379                NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
1380            },
1381        }
1382    }
1383
1384    /// Enable/disable per-layer O(1) Nyström attention. Only Full
1385    /// layers are eligible (a linear layer keeps its own operator).
1386    /// Applies to generation (`generate*`/`forward_ids`): the prompt
1387    /// pass stays exact, the seal happens once after prefill, decode
1388    /// runs on the O(1) state. Teacher-forced scoring (`ppl_ids`)
1389    /// intentionally stays exact.
1390    pub fn set_o1(&mut self, cfg: Option<crate::nystrom::O1Cfg>) {
1391        self.o1_flags = match &cfg {
1392            Some(c) => {
1393                let mut flags = c.layer_flags(self.num_layers);
1394                for (li, f) in flags.iter_mut().enumerate() {
1395                    if *f
1396                        && !matches!(
1397                            self.weights.layers[self.phys_layer(li)].attn,
1398                            AttnKind::Full { .. }
1399                        )
1400                    {
1401                        *f = false;
1402                    }
1403                }
1404                flags
1405            }
1406            None => Vec::new(),
1407        };
1408        if let Some(c) = &cfg {
1409            let n = self.o1_flags.iter().filter(|&&f| f).count();
1410            tracing::info!(
1411                "o1 nystrom attention: {n}/{} layer(s), m={} w={} sink={} rect={:?}",
1412                self.num_layers,
1413                c.m,
1414                c.w,
1415                c.sink,
1416                c.rect
1417            );
1418        }
1419        self.o1_cfg = cfg;
1420    }
1421
1422    /// True when at least one layer runs the O(1) kernel.
1423    pub fn o1_active(&self) -> bool {
1424        self.o1_cfg.is_some() && self.o1_flags.iter().any(|&f| f)
1425    }
1426
1427    /// Arm query collection on the o1 layers (fresh prompt pass).
1428    /// Reset the o1 layers to Collecting for a fresh sequence. Pub for the
1429    /// network split: each side runs the o1 lifecycle over ITS OWN layers
1430    /// (begin before prefill, seal at the prefill barrier).
1431    pub fn o1_begin(&mut self) {
1432        if let Some(c) = &self.o1_cfg {
1433            let (m, w, sink, rect) = (c.m, c.w, c.sink, c.rect);
1434            for (li, &f) in self.o1_flags.iter().enumerate() {
1435                if f {
1436                    self.kv_cache.layers[li].o1_begin(m, w, sink, rect);
1437                }
1438            }
1439        }
1440    }
1441
1442    /// Freeze landmarks + skeleton state after the prompt pass and drop
1443    /// the o1 layers' full KV; decode then runs `step()` per token.
1444    /// Pub for the network split (see `o1_begin`).
1445    pub fn o1_seal(&mut self) {
1446        self.o1_epoch = self.o1_epoch.wrapping_add(1);
1447        if self.o1_cfg.is_none() {
1448            return;
1449        }
1450        for li in 0..self.num_layers {
1451            if self.o1_flags.get(li).copied().unwrap_or(false) {
1452                self.kv_cache.layers[li].o1_seal(self.num_heads);
1453            }
1454        }
1455    }
1456
1457    /// Enable/disable the structured per-token telemetry trace (B4).
1458    pub fn set_trace(&mut self, on: bool) {
1459        self.trace = on;
1460    }
1461
1462    /// Replace all request-scoped sampler options and reset the random stream.
1463    /// This is required for deterministic `seed` semantics in pooled servers.
1464    pub fn set_sampler_config(&mut self, config: SamplerConfig) {
1465        self.rng = match config.seed {
1466            Some(seed) => SplitMix64::new(seed),
1467            None => SplitMix64::from_entropy(),
1468        };
1469        self.sampler_config = config;
1470    }
1471
1472    /// Toggle the per-token Born-confidence reduction (a full-vocab
1473    /// softmax each token). `bench --core` turns it off so the timed
1474    /// loop matches llama-bench's core contract; the result's
1475    /// `confidence` vec is empty while off.
1476    pub fn set_confidence(&mut self, on: bool) {
1477        self.confidence_on = on;
1478    }
1479
1480    /// Set the confidence-calibration temperature (B1). Values ≤0 are
1481    /// clamped to raw (1.0).
1482    pub fn set_calib_temp(&mut self, t: f32) {
1483        self.calib_temp = if t > 1e-3 { t } else { 1.0 };
1484    }
1485
1486    /// The active calibration temperature (1.0 = raw Born mass).
1487    pub fn calib_temp(&self) -> f32 {
1488        self.calib_temp
1489    }
1490
1491    /// Partial rotary (Qwen3.5): rotate only the first `rotary_dim` dims;
1492    /// the frequency table is rebuilt over the rotary dims.
1493    pub fn set_rotary(&mut self, rotary_dim: usize, base: f32) {
1494        self.rotary_dim = rotary_dim.min(self.head_dim);
1495        self.inv_freq = std::sync::Arc::new(attention::rope_inv_freq(self.rotary_dim, base));
1496    }
1497
1498    fn attn_cfg(&self, position: usize) -> QwenAttnCfg<'_> {
1499        QwenAttnCfg {
1500            num_heads: self.num_heads,
1501            num_kv_heads: self.num_kv_heads,
1502            head_dim: self.head_dim,
1503            hidden_size: self.hidden_size,
1504            position,
1505            inv_freq: &self.inv_freq,
1506            rotary_dim: self.rotary_dim,
1507            scale: self.attn_scale,
1508            softcap: self.attn_softcap,
1509            window: None,
1510            v_norm: false,
1511            q_norm: None,
1512            k_norm: None,
1513            output_gate: false,
1514            softplus_gate: None,
1515            rope_scale: self.rope_scale,
1516            bias: None,
1517            rms_eps: self.rms_eps,
1518            norm_style: self.norm_style,
1519            pool: self.pool.as_deref(),
1520        }
1521    }
1522
1523    /// Generate text from a plain-text prompt. Streams tokens via `on_token`.
1524    pub fn generate(
1525        &mut self,
1526        prompt: &str,
1527        max_tokens: usize,
1528        task_mask: Option<&TaskMask>,
1529        on_token: Option<TokenCallback>,
1530    ) -> Result<GenerateResult, String> {
1531        let input_ids = self.tokenizer.with_bos(self.tokenizer.encode(prompt));
1532        self.generate_from_ids(&input_ids, max_tokens, task_mask, on_token)
1533    }
1534
1535    /// Generate from prepared token ids (e.g. a chat template).
1536    ///
1537    /// With an MTP head, greedy generation without a task mask takes the
1538    /// speculative path: the MTP module drafts the token after next and
1539    /// the main model verifies both in one fused two-position forward
1540    /// (weights streamed once). The output is EXACTLY the vanilla greedy
1541    /// sequence — a rejected draft is rolled back — MTP only buys speed.
1542    pub fn generate_from_ids(
1543        &mut self,
1544        input_ids: &[u32],
1545        max_tokens: usize,
1546        task_mask: Option<&TaskMask>,
1547        mut on_token: Option<TokenCallback>,
1548    ) -> Result<GenerateResult, String> {
1549        if std::env::var("CMF_TRACE_H").is_ok() {
1550            eprintln!("input_ids: {input_ids:?}");
1551        }
1552        if input_ids.is_empty() {
1553            return Err("empty prompt: nothing to generate from".to_string());
1554        }
1555
1556        // Cross-turn KV reuse: a chat app resends the whole history
1557        // every turn; when the new ids strictly EXTEND what the cache
1558        // already holds, prefill only the tail — turn latency stays
1559        // proportional to the new text instead of the whole session.
1560        // Extension-only (no rollback), so it is exact for every layer
1561        // kind including recurrent state; MTP/o1/task-mask runs keep
1562        // the fresh-sequence path. CMF_KV_REUSE=0 disables.
1563        let reuse_from = {
1564            let on = !std::env::var("CMF_KV_REUSE").is_ok_and(|v| v == "0");
1565            let h = &self.kv_history;
1566            if on
1567                && task_mask.is_none()
1568                && self.mtp.is_none()
1569                && self.o1_cfg.is_none()
1570                && !h.is_empty()
1571                && h.len() < input_ids.len()
1572                && input_ids[..h.len()] == h[..]
1573            {
1574                h.len()
1575            } else {
1576                0
1577            }
1578        };
1579        if reuse_from == 0 {
1580            // Fresh sequence — the cache holds absolute positions.
1581            self.kv_cache.clear();
1582            self.kv_history.clear();
1583            crate::gpu::graph_kv_reset(self.graph_kv_id);
1584        } else if std::env::var("CMF_PREFILL_PROF").is_ok() {
1585            eprintln!(
1586                "kv-reuse: {} of {} prompt positions already cached",
1587                reuse_from,
1588                input_ids.len()
1589            );
1590        }
1591        crate::gpu::graph_race_begin_generation();
1592        self.o1_begin();
1593
1594        // Speculative decode is off under o1: a rejected draft can't be
1595        // rolled back out of the far accumulators / ring window (the
1596        // Nyström insertion is irreversible by design).
1597        // The wgpu token graph owns a device K/V mirror that speculative
1598        // rollback would desync — the two are mutually exclusive.
1599        let graph_on = std::env::var("CMF_GPU_WGPU_GRAPH")
1600            .map(|v| v != "0")
1601            .unwrap_or_else(|_| {
1602                // Default ON for wgpu on DISCRETE adapters (4090:
1603                // decode 76 -> 137 tok/s); integrated/mobile GPUs keep
1604                // the per-op probe path — see gpu::wgpu_graph_default.
1605                crate::gpu::wgpu_graph_default()
1606            });
1607        // Graph speculative decode (`CMF_GRAPH_SPEC=1`): the MTP head
1608        // drafts, ONE batched graph submit verifies the whole chain.
1609        //
1610        // It now PAYS on Qwen3.6-27B / RTX 5090 — 51.1 tok/s against a
1611        // plain 49.4 at k=3, medians of three, 89% of drafts accepted,
1612        // and the greedy continuation is byte-identical to the plain
1613        // path. That took the batch matvec sharing its nibble unpack
1614        // across the batch (`CMF_MV_BK=2`); before it, the same round
1615        // measured 43.6, an 11% LOSS, which is what the earlier note
1616        // here described.
1617        //
1618        // Still opt-in. One model's win is not a default: the verify
1619        // rides `gdn_spec_restore` and a batched frame whose numerics
1620        // are the batch kernels', and that has to be shown on more than
1621        // one architecture before every greedy decode takes it.
1622        let graph_spec = self.speculative
1623            && graph_on
1624            && self.mtp.is_some()
1625            && task_mask.is_none()
1626            && !self.o1_active()
1627            && self.sampler_config.temperature < 1e-6
1628            && self.sampler_config.repetition_penalty == 1.0
1629            && std::env::var("CMF_GRAPH_SPEC").is_ok_and(|v| v != "0");
1630        // GDN hybrids sit the fused-pair speculation out by default: the
1631        // recurrence is sequential, so the pair lane cannot parallelize
1632        // (the bench's own Pair line reads fused 1.28x TWO singles on the
1633        // 35B) and the draft's full-vocab head rides on top — measured 2x
1634        // SLOWER end to end (16.1 vs 32.4 tok/s on the 48-core stand).
1635        // CMF_MTP=1 forces it back for study.
1636        let pair_pays = self.gdn_cfg.is_none()
1637            || std::env::var("CMF_MTP").as_deref() == Ok("1");
1638        let spec_active = self.speculative
1639            && self.mtp.is_some()
1640            && task_mask.is_none()
1641            && !self.o1_active()
1642            && ((!graph_on && pair_pays) || graph_spec)
1643            && self.sampler_config.temperature < 1e-6;
1644        // The MTP module is detached during generation so its mutable
1645        // state does not fight the borrow on `self`.
1646        let mut mtp = if spec_active { self.mtp.take() } else { None };
1647        if std::env::var("CMF_MTP_CHAIN_PROBE").is_ok() {
1648            eprintln!(
1649                "mtp-probe gate: spec_active={spec_active} mtp={} speculative={} graph_on={graph_on} temp_ok={}",
1650                mtp.is_some(),
1651                self.speculative,
1652                self.sampler_config.temperature < 1e-6,
1653            );
1654        }
1655        if let Some(m) = &mut mtp {
1656            m.kv.clear();
1657        }
1658        // Dynamic router detached during decode (same borrow trick as MTP).
1659        // Speculative decode and dynamic routing are mutually exclusive
1660        // for now — the fused-pair path doesn't carry per-token φ.
1661        let mut router = if mtp.is_none() {
1662            self.dyn_router.take()
1663        } else {
1664            None
1665        };
1666        if let Some(r) = &mut router {
1667            r.reset(); // active=backbone, matching a fresh overlay
1668            self.dyn_phi_seen = 0; // fresh φ EMA per generation
1669            let _ = self.set_active_skill(None);
1670        }
1671
1672        let mut all_ids = input_ids.to_vec();
1673        let mut generated = 0usize;
1674        let mut finish_reason = "max_tokens".to_string();
1675        let mut drafted = 0usize;
1676        let mut accepted = 0usize;
1677        let mut confidence: Vec<f32> = Vec::new();
1678        let trace_on = self.trace;
1679        let calib_temp = self.calib_temp;
1680        let mut traces: Vec<TokenTrace> = Vec::new();
1681
1682        // ── Prefill: forward each prompt token once, KEEP the last hidden.
1683        //    Dense prefill runs in fused pairs (weights streamed once per
1684        //    two positions — bit-identical to sequential, proven by the
1685        //    pair tests). With MTP: warm the draft head on
1686        //    (hidden_p, token_{p+1}) pairs.
1687        let mut hidden = vec![0.0f32; self.hidden_size];
1688        let mut pos = reuse_from;
1689        // lm_head-in-graph is only sound when the very next logits
1690        // consumer is this loop's own (MTP and skill routing interleave
1691        // other forwards / can swap lm_head between forward and sample).
1692        // CMF_GPU_LMHEAD=0 keeps lm_head off the graph: the token reads back
1693        // the 8 KB hidden instead of ~1 MB of logits, and the head runs on
1694        // the host. A probe for how much of the graph's fixed per-token cost
1695        // is the logits readback (the layer sweep puts that fixed part at
1696        // 3.88 ms of an 18.5 ms frame).
1697        let fuse_lm = mtp.is_none()
1698            && router.is_none()
1699            && std::env::var("CMF_GPU_LMHEAD").as_deref() != Ok("0");
1700        self.graph_logits = None;
1701        self.graph_want_logits = false;
1702        let _tpf = std::time::Instant::now();
1703        let batch_k = std::env::var("CMF_BATCH_K")
1704            .ok()
1705            .and_then(|v| v.parse::<usize>().ok())
1706            .unwrap_or(0);
1707        // DeepSeek-V4 owns a separate hyper-connection stack. Route it
1708        // before the generic prefill choices: those correctly reject an
1709        // empty `weights.layers`, but their final per-position fallback used
1710        // to consume the whole prompt before `dsv4::forward_chunk` could see
1711        // it. The batch implementation therefore existed without a live
1712        // production entry point.
1713        //
1714        // Bounded chunks preserve cancellation responsiveness. Only the
1715        // prompt's final chunk asks for logits; every earlier head projection
1716        // would produce 129 280 values that no caller reads.
1717        while self.dsv4.is_some()
1718            && mtp.is_none()
1719            && pos < input_ids.len()
1720            && !self.cancel.load(std::sync::atomic::Ordering::Relaxed)
1721        {
1722            let end = (pos + prefill_chunk()).min(input_ids.len());
1723            let ids: Vec<u32> = input_ids[pos..end].to_vec();
1724            let mut lg = Vec::new();
1725            if let Some(b) = &mut self.dsv4 {
1726                let (g, layers, cfg, st) = (&b.0, &b.1, b.2, &mut b.3);
1727                crate::dsv4::forward_chunk(
1728                    g,
1729                    layers,
1730                    &cfg,
1731                    st,
1732                    &ids,
1733                    pos,
1734                    &self.inv_freq,
1735                    self.pool.as_deref(),
1736                    &mut lg,
1737                    end == input_ids.len(),
1738                );
1739            }
1740            if end == input_ids.len() {
1741                self.graph_logits = Some(lg);
1742            }
1743            pos = end;
1744            hidden = vec![0.0; self.hidden_size];
1745        }
1746        // With dynamic routing, prefill sequentially so the φ hook fires
1747        // over the PROMPT — the router enters decode with a warm φ (the
1748        // fused-pair path skips the per-layer φ capture). o1 layers
1749        // collect their query trace in both the single and pair paths.
1750        let dyn_prefill = router.is_some();
1751        // q1 hybrids on Metal: the per-position GPU token graph beats
1752        // the CPU chunk-GEMM (whose wall is the sequential scalar GDN
1753        // recurrence), so prefill goes position-by-position through the
1754        // same graph as decode. Pure-attention models keep the batched
1755        // path — there the chunk-GEMM amortization wins.
1756        let graph_prefill = self.graph_prefill_preferred();
1757        if task_mask.is_none()
1758            && !dyn_prefill
1759            && !graph_prefill
1760            && self.can_prefill_batched()
1761            && self.g3n.is_none()
1762            && input_ids.len() > 2
1763        {
1764            // Production prefill = the same chunked prefill-GEMM that
1765            // bench/PPL measure (roadmap §3 P0: generation used to warm
1766            // the prompt with the slower pair path — the published
1767            // prefill number didn't match real TTFT). MTP warm-up reads
1768            // each position's hidden straight from the chunk result.
1769            let chunk = prefill_chunk();
1770            let hs = self.hidden_size;
1771            while pos < input_ids.len() && !self.cancel.load(std::sync::atomic::Ordering::Relaxed) {
1772                let end = (pos + chunk).min(input_ids.len());
1773                let hb = self.prefill_batch(&input_ids[pos..end], pos);
1774                if let Some(m) = &mut mtp {
1775                    let probe: usize = std::env::var("CMF_MTP_CHAIN_PROBE")
1776                        .ok()
1777                        .and_then(|v| v.parse().ok())
1778                        .unwrap_or(0);
1779                    for p in pos..end {
1780                        if p + 1 < input_ids.len() {
1781                            if probe >= 1 && p + 2 < input_ids.len() {
1782                                // Teacher-forced chain acceptance (see the
1783                                // tail loop's twin): the warm-up row stays,
1784                                // the chain's rows roll back.
1785                                let (d1, mut hx) = self.mtp_step_h(
1786                                    m,
1787                                    &hb[(p - pos) * hs..(p - pos + 1) * hs],
1788                                    input_ids[p + 1],
1789                                    p,
1790                                );
1791                                let mut ok = d1 == input_ids[p + 2];
1792                                Self::chain_probe_note(0, ok);
1793                                let mut d_prev = d1;
1794                                let mut extra = 0usize;
1795                                for j in 1..probe {
1796                                    if p + 2 + j >= input_ids.len() {
1797                                        break;
1798                                    }
1799                                    let (dj, hj) =
1800                                        self.mtp_step_h(m, &hx, d_prev, p + 1 + j);
1801                                    extra += 1;
1802                                    ok = ok && dj == input_ids[p + 2 + j];
1803                                    Self::chain_probe_note(j, ok);
1804                                    d_prev = dj;
1805                                    hx = hj;
1806                                }
1807                                m.kv.truncate_last(extra);
1808                            } else {
1809                                let _ = self.mtp_step(
1810                                    m,
1811                                    &hb[(p - pos) * hs..(p - pos + 1) * hs],
1812                                    input_ids[p + 1],
1813                                    p,
1814                                );
1815                            }
1816                        }
1817                    }
1818                }
1819                hidden.copy_from_slice(&hb[(end - pos - 1) * hs..]);
1820                pos = end;
1821            }
1822        }
1823        let pair_off = std::env::var("CMF_PAIR").is_ok_and(|v| v == "0");
1824        if task_mask.is_none()
1825            && !dyn_prefill
1826            && !graph_prefill
1827            && !pair_off
1828            && self.pair_supported()
1829        {
1830            while pos + 1 < input_ids.len()
1831                && !self.cancel.load(std::sync::atomic::Ordering::Relaxed)
1832            {
1833                let e1 = self.embed_single(input_ids[pos]);
1834                let e2 = self.embed_single(input_ids[pos + 1]);
1835                let (h1, h2) = self.forward_pair(&e1, &e2, pos);
1836                // Both prefill tokens are real → commit lane-2 states.
1837                self.commit_linear_scratch();
1838                if let Some(m) = &mut mtp {
1839                    let _ = self.mtp_step(m, &h1, input_ids[pos + 1], pos);
1840                    if pos + 2 < input_ids.len() {
1841                        let probe: usize = std::env::var("CMF_MTP_CHAIN_PROBE")
1842                            .ok()
1843                            .and_then(|v| v.parse().ok())
1844                            .unwrap_or(0);
1845                        if probe >= 1 && pos + 3 < input_ids.len() {
1846                            // Same teacher-forced chain table as the tail
1847                            // loop below, fed from the pair path that owns
1848                            // most prefill positions.
1849                            let (d1, mut hx) =
1850                                self.mtp_step_h(m, &h2, input_ids[pos + 2], pos + 1);
1851                            let mut ok = d1 == input_ids[pos + 3];
1852                            Self::chain_probe_note(0, ok);
1853                            let mut d_prev = d1;
1854                            let mut extra = 0usize;
1855                            for j in 1..probe {
1856                                if pos + 3 + j >= input_ids.len() {
1857                                    break;
1858                                }
1859                                let (dj, hj) =
1860                                    self.mtp_step_h(m, &hx, d_prev, pos + 2 + j);
1861                                extra += 1;
1862                                ok = ok && dj == input_ids[pos + 3 + j];
1863                                Self::chain_probe_note(j, ok);
1864                                d_prev = dj;
1865                                hx = hj;
1866                            }
1867                            m.kv.truncate_last(extra);
1868                        } else {
1869                            let _ = self.mtp_step(m, &h2, input_ids[pos + 2], pos + 1);
1870                        }
1871                    }
1872                }
1873                hidden = h2;
1874                pos += 2;
1875            }
1876        }
1877        // Batched GPU prefill for the wgpu decode graph (GDN hybrids): K prompt
1878        // positions per submit — projections/FFN as GEMMs (weight once per K),
1879        // attention/GDN looped inside — instead of one whole-graph submit per
1880        // position. Falls through to the per-position graph on any refusal.
1881        // Batched prefill is opt-in (CMF_BATCH_K>0). Default 0 = per-position
1882        // graph prefill. (Steady-state decode is provably identical either way —
1883        // token-graph submit and lm_head both unchanged — so this only trades
1884        // prefill wall.)
1885        if batch_k > 0
1886            && graph_prefill
1887            && task_mask.is_none()
1888            && !self.o1_active()
1889            && mtp.is_none()
1890            && !dyn_prefill
1891            && pos + 1 < input_ids.len()
1892        {
1893            let hs = self.hidden_size;
1894            let chunk = batch_k;
1895            while pos < input_ids.len() {
1896                let end = (pos + chunk).min(input_ids.len());
1897                let bk = end - pos;
1898                let mut hiddens = vec![0f32; bk * hs];
1899                for (j, &id) in input_ids[pos..end].iter().enumerate() {
1900                    hiddens[j * hs..(j + 1) * hs].copy_from_slice(&self.embed_single(id));
1901                }
1902                let positions: Vec<usize> = (pos..end).collect();
1903                let t_chunk = std::time::Instant::now();
1904                let ok_b = self.try_batch_graph_wgpu(&mut hiddens, &positions, bk, None);
1905                if std::env::var("CMF_GRAPH_PROF").is_ok() {
1906                    let ms = t_chunk.elapsed().as_secs_f64() * 1000.0;
1907                    eprintln!(
1908                        "batch-chunk: k={bk} ok={ok_b} {ms:.1} ms ({:.1} tok/s)",
1909                        bk as f64 / (ms / 1000.0)
1910                    );
1911                }
1912                {
1913                    use std::sync::atomic::{AtomicBool, Ordering};
1914                    static SAID: AtomicBool = AtomicBool::new(false);
1915                    if !SAID.swap(true, Ordering::Relaxed) {
1916                        if ok_b {
1917                            tracing::info!("batched prefill: ACTIVE (k={bk})");
1918                        } else {
1919                            tracing::warn!("batched prefill declined — per-position graph");
1920                        }
1921                    }
1922                }
1923                if ok_b {
1924                    hidden.copy_from_slice(&hiddens[(bk - 1) * hs..]);
1925                    pos = end;
1926                } else {
1927                    break; // unsupported → per-position graph handles the rest
1928                }
1929            }
1930        }
1931        while pos < input_ids.len() && !self.cancel.load(std::sync::atomic::Ordering::Relaxed) {
1932            self.graph_want_logits = fuse_lm && pos + 1 == input_ids.len();
1933            hidden = self.forward_layers(&self.embed_single(input_ids[pos]), pos, task_mask);
1934            if let Some(m) = &mut mtp {
1935                if pos + 1 < input_ids.len() {
1936                    // `CMF_MTP_CHAIN_PROBE=k`: teacher-forced acceptance of a
1937                    // CHAINED draft — iterate the head on its own hidden k
1938                    // deep and score every depth against the prompt's real
1939                    // continuation. The economics of a k-token speculative
1940                    // round stand or fall on this table.
1941                    let probe: usize = std::env::var("CMF_MTP_CHAIN_PROBE")
1942                        .ok()
1943                        .and_then(|v| v.parse().ok())
1944                        .unwrap_or(0);
1945                    if probe >= 1 && pos + 2 < input_ids.len() {
1946                        let (d1, mut hx) = self.mtp_step_h(m, &hidden, input_ids[pos + 1], pos);
1947                        let mut ok = d1 == input_ids[pos + 2];
1948                        Self::chain_probe_note(0, ok);
1949                        let mut d_prev = d1;
1950                        let mut extra = 0usize;
1951                        for j in 1..probe {
1952                            if pos + 2 + j >= input_ids.len() {
1953                                break;
1954                            }
1955                            let (dj, hj) =
1956                                self.mtp_step_h(m, &hx, d_prev, pos + 1 + j);
1957                            extra += 1;
1958                            ok = ok && dj == input_ids[pos + 2 + j];
1959                            Self::chain_probe_note(j, ok);
1960                            d_prev = dj;
1961                            hx = hj;
1962                        }
1963                        // The chain's rows are speculation, not the prompt —
1964                        // keep only the warmup row the plain path would add.
1965                        m.kv.truncate_last(extra);
1966                    } else {
1967                        let _ = self.mtp_step(m, &hidden, input_ids[pos + 1], pos);
1968                    }
1969                }
1970            }
1971            pos += 1;
1972        }
1973        if std::env::var("CMF_PREFILL_PROF").is_ok() {
1974            eprintln!(
1975                "prefill: {} tokens in {:.1} ms (batch_k={batch_k})",
1976                input_ids.len(),
1977                _tpf.elapsed().as_secs_f64() * 1000.0
1978            );
1979        }
1980        // Cancelled mid-prefill: the cache holds a partial prompt —
1981        // drop the reuse history and return an empty generation.
1982        if self
1983            .cancel
1984            .swap(false, std::sync::atomic::Ordering::Relaxed)
1985        {
1986            self.kv_history.clear();
1987            if let Some(m) = mtp {
1988                self.mtp = Some(m);
1989            }
1990            return Ok(GenerateResult {
1991                text: String::new(),
1992                token_ids: Vec::new(),
1993                prompt_tokens: input_ids.len(),
1994                tokens_generated: 0,
1995                finish_reason: "cancelled".to_string(),
1996                mtp_drafted: 0,
1997                mtp_accepted: 0,
1998                token_confidence: Vec::new(),
1999                traces: Vec::new(),
2000            });
2001        }
2002
2003        // Prompt absorbed → freeze the o1 layers' skeletons; from here
2004        // every decode step on those layers is O(W + m·dv + m²).
2005        self.o1_seal();
2006
2007        // Commit one token: push, check EOS, stream. Returns false = stop.
2008        macro_rules! commit {
2009            ($id:expr) => {{
2010                all_ids.push($id);
2011                generated += 1;
2012                if self.tokenizer.is_eos($id) {
2013                    finish_reason = "stop".to_string();
2014                    false
2015                } else {
2016                    let token_text = self.tokenizer.decode_token($id);
2017                    let mut go = true;
2018                    if let Some(ref mut cb) = on_token {
2019                        if !cb(&token_text) {
2020                            finish_reason = "cancelled".to_string();
2021                            go = false;
2022                        }
2023                    }
2024                    go
2025                }
2026            }};
2027        }
2028
2029        // ── Decode ──
2030        let mut next_pos = input_ids.len();
2031        'decode: while generated < max_tokens {
2032            if self
2033                .cancel
2034                .swap(false, std::sync::atomic::Ordering::Relaxed)
2035            {
2036                finish_reason = "cancelled".to_string();
2037                break 'decode;
2038            }
2039            let mut logits = match self.graph_logits.take() {
2040                Some(lg) => lg,
2041                None => {
2042                    inference::rms_norm_into(
2043                        &hidden,
2044                        &self.weights.final_norm,
2045                        self.rms_eps,
2046                        self.norm_style,
2047                        &mut self.ws.n1,
2048                    );
2049                    self.lm_head_forward(&self.ws.n1)
2050                }
2051            };
2052            let t_next = sampler::sample_with_scratch(
2053                &logits,
2054                &self.sampler_config,
2055                &all_ids,
2056                &mut self.rng,
2057                &mut self.sampler_scratch,
2058            );
2059            if self.confidence_on {
2060                confidence.push(top1_prob_t(&logits, t_next, calib_temp));
2061            }
2062            attention::recycle_buf(&mut logits);
2063            if trace_on {
2064                // active_skill = the overlay in force while this token was
2065                // generated; recon/switched are filled after the post-emit
2066                // routing eval below (freshest coherence for this token).
2067                let skill = router.as_ref().and_then(|r| r.active_id());
2068                traces.push(TokenTrace {
2069                    t: generated,
2070                    token_id: t_next,
2071                    confidence: confidence.last().copied().unwrap_or(0.0),
2072                    active_skill: skill,
2073                    recon: None,
2074                    switched: false,
2075                });
2076            }
2077            if !commit!(t_next) {
2078                break 'decode;
2079            }
2080            if generated >= max_tokens {
2081                break 'decode;
2082            }
2083
2084            if self.kv_cache.needs_eviction() {
2085                let keep = (self.kv_cache.max_seq_len / 2).max(1);
2086                self.kv_cache.evict(keep);
2087            }
2088
2089            match &mut mtp {
2090                // ── Graph speculation: chain-draft, batch-verify on device ──
2091                #[cfg(feature = "gpu")]
2092                Some(m) if graph_spec && generated + 1 < max_tokens && next_pos > 0 => {
2093                    if let Some((extra, n_pos, new_h)) = self.graph_spec_step(
2094                        m,
2095                        &hidden,
2096                        t_next,
2097                        next_pos,
2098                        &mut drafted,
2099                        &mut accepted,
2100                    ) {
2101                        next_pos = n_pos;
2102                        hidden = new_h;
2103                        let mut stopped = false;
2104                        for &id in &extra {
2105                            if self.confidence_on {
2106                                confidence.push(0.0);
2107                            }
2108                            if !commit!(id) {
2109                                stopped = true;
2110                                break;
2111                            }
2112                        }
2113                        if stopped {
2114                            break 'decode;
2115                        }
2116                        continue 'decode;
2117                    }
2118                    // Declined (batch graph refused): plain forward below.
2119                    hidden = self.forward_layers(&self.embed_single(t_next), next_pos, task_mask);
2120                    next_pos += 1;
2121                    continue 'decode;
2122                }
2123                // ── Speculative: draft t+2, verify in a fused pair ──
2124                Some(m) if !graph_spec && generated + 1 < max_tokens => {
2125                    let draft = self.mtp_step(m, &hidden, t_next, next_pos - 1);
2126                    drafted += 1;
2127                    let emb1 = self.embed_single(t_next);
2128                    let emb2 = self.embed_single(draft);
2129                    let (h1, h2) = self.forward_pair(&emb1, &emb2, next_pos);
2130
2131                    inference::rms_norm_into(
2132                        &h1,
2133                        &self.weights.final_norm,
2134                        self.rms_eps,
2135                        self.norm_style,
2136                        &mut self.ws.n1,
2137                    );
2138                    let mut logits1 = self.lm_head_forward(&self.ws.n1);
2139                    let t_after = sampler::sample_with_scratch(
2140                        &logits1,
2141                        &self.sampler_config,
2142                        &all_ids,
2143                        &mut self.rng,
2144                        &mut self.sampler_scratch,
2145                    );
2146                    if self.confidence_on {
2147                        confidence.push(top1_prob_t(&logits1, t_after, calib_temp));
2148                    }
2149                    attention::recycle_buf(&mut logits1);
2150                    if trace_on {
2151                        // Speculative decode is mutually exclusive with
2152                        // dynamic routing (router is None here) — no skill.
2153                        traces.push(TokenTrace {
2154                            t: generated,
2155                            token_id: t_after,
2156                            confidence: confidence.last().copied().unwrap_or(0.0),
2157                            active_skill: None,
2158                            recon: None,
2159                            switched: false,
2160                        });
2161                    }
2162                    let stop = !commit!(t_after);
2163
2164                    if t_after == draft {
2165                        accepted += 1;
2166                        self.commit_linear_scratch();
2167                        let _ = self.mtp_step(m, &h1, t_after, next_pos);
2168                        hidden = h2;
2169                        next_pos += 2;
2170                    } else {
2171                        // The draft lane is wrong: roll its KV entry back.
2172                        for layer in &mut self.kv_cache.layers {
2173                            layer.truncate_last(1);
2174                        }
2175                        if !stop {
2176                            let _ = self.mtp_step(m, &h1, t_after, next_pos);
2177                            hidden = self.forward_layers(
2178                                &self.embed_single(t_after),
2179                                next_pos + 1,
2180                                None,
2181                            );
2182                        }
2183                        next_pos += 2;
2184                    }
2185                    if stop {
2186                        break 'decode;
2187                    }
2188                }
2189                // ── Vanilla: forward the sampled token ──
2190                _ => {
2191                    // ── DeepSeek-V4 speculative decode (CMF_DSV4_SPEC=1):
2192                    // draft five on the card, verify batched, commit the
2193                    // accepted prefix. Greedy only; a rejected token's state
2194                    // is restored and replayed, so output equals the walk. ──
2195                    #[cfg(feature = "gpu")]
2196                    if Self::dsv4_spec_on() && self.dsv4.is_some() {
2197                        static SAID: std::sync::Once = std::sync::Once::new();
2198                        SAID.call_once(|| {
2199                            eprintln!(
2200                                "dsv4-spec гейт: mtp={} mask={} router={} trace={} temp={} rep={} ",
2201                                !self.dsv4_mtp.is_empty(),
2202                                task_mask.is_none(),
2203                                router.is_none(),
2204                                !trace_on,
2205                                self.sampler_config.temperature < 1e-6,
2206                                self.sampler_config.repetition_penalty == 1.0,
2207                            );
2208                        });
2209                    }
2210                    #[cfg(feature = "gpu")]
2211                    if Self::dsv4_spec_on()
2212                        && self.dsv4.is_some()
2213                        && !self.dsv4_mtp.is_empty()
2214                        && task_mask.is_none()
2215                        && router.is_none()
2216                        && !trace_on
2217                        && self.sampler_config.temperature < 1e-6
2218                        && self.sampler_config.repetition_penalty == 1.0
2219                        && generated + 1 < max_tokens
2220                        && all_ids.len() >= 2
2221                    {
2222                        let tip_token = all_ids[all_ids.len() - 2];
2223                        if let Some((extra, n_pos)) = self.dsv4_spec_step(
2224                            tip_token,
2225                            t_next,
2226                            next_pos,
2227                            &mut drafted,
2228                            &mut accepted,
2229                        ) {
2230                            next_pos = n_pos;
2231                            let mut stopped = false;
2232                            for &id in &extra {
2233                                if self.confidence_on {
2234                                    confidence.push(0.0);
2235                                }
2236                                if !commit!(id) {
2237                                    stopped = true;
2238                                    break;
2239                                }
2240                            }
2241                            if stopped {
2242                                break 'decode;
2243                            }
2244                            continue 'decode;
2245                        }
2246                    }
2247                    self.graph_want_logits = fuse_lm;
2248                    // Greedy burst (CMF_MULTISTEP, default 8, 1 = off): while
2249                    // nothing observes per-token state — pure argmax sampling,
2250                    // no router/trace/confidence/mask — decode k tokens per
2251                    // submit and commit them wholesale. The trailing normal
2252                    // forward leaves logits for the loop top, as always.
2253                    let mut t_fwd = t_next;
2254                    let pure_greedy = self.sampler_config.temperature < 1e-6
2255                        && self.sampler_config.repetition_penalty == 1.0
2256                        && self.sampler_config.suppress_tokens.is_empty();
2257                    // Off by default: at every k the burst measured at or
2258                    // below the plain path on this graph shape (k=1 loses
2259                    // the argmax dispatches vs a 1 MB readback, k>=8 loses
2260                    // inter-step drains vs the saved sync). Experimental.
2261                    let burst_k = std::env::var("CMF_MULTISTEP")
2262                        .ok()
2263                        .and_then(|v| v.parse::<usize>().ok())
2264                        .unwrap_or(0);
2265                    if pure_greedy
2266                        && burst_k >= 1
2267                        && fuse_lm
2268                        && task_mask.is_none()
2269                        && router.is_none()
2270                        && !trace_on
2271                        && !self.confidence_on
2272                    {
2273                        let mut stopped = false;
2274                        loop {
2275                            let room = max_tokens.saturating_sub(generated);
2276                            if room <= 2 {
2277                                break;
2278                            }
2279                            let k = burst_k.min(room - 1);
2280                            if k < 1 {
2281                                break;
2282                            }
2283                            let Some(ids) = self.try_multi_burst(t_fwd, next_pos, k) else {
2284                                break;
2285                            };
2286                            next_pos += k;
2287                            for &id in &ids {
2288                                if !commit!(id) {
2289                                    stopped = true;
2290                                    break;
2291                                }
2292                            }
2293                            if stopped {
2294                                break;
2295                            }
2296                            t_fwd = *ids.last().unwrap();
2297                        }
2298                        if stopped {
2299                            break 'decode;
2300                        }
2301                    }
2302                    hidden = self.forward_layers(&self.embed_single(t_fwd), next_pos, task_mask);
2303                    next_pos += 1;
2304                    // Dynamic routing: the forward updated φ; ask the
2305                    // router whether to switch skills before the next token.
2306                    if let Some(r) = &mut router {
2307                        let phi = self.dyn_phi_ema.clone();
2308                        let decision = r.step(&phi, generated);
2309                        if let Some(new_active) = decision {
2310                            let _ = self.set_active_skill(new_active);
2311                        }
2312                        // Backfill this token's coherence + switch flag from
2313                        // the just-run eval (freshest measured values).
2314                        if trace_on {
2315                            if let Some(last) = traces.last_mut() {
2316                                let e = r.last_best_e();
2317                                last.recon = e.is_finite().then_some(e);
2318                                last.switched = decision.is_some();
2319                            }
2320                        }
2321                    }
2322                }
2323            }
2324        }
2325
2326        self.graph_want_logits = false;
2327        self.graph_logits = None;
2328        // Restore backbone overlay and re-attach the router for reuse.
2329        if router.is_some() {
2330            let _ = self.set_active_skill(None);
2331        }
2332        self.dyn_router = router.or(self.dyn_router.take());
2333        self.mtp = mtp.or(self.mtp.take());
2334
2335        let output_ids = &all_ids[input_ids.len()..];
2336        // Forwarded = prompt + all generated but the LAST sampled token
2337        // (emitted without being fed back). Exact only without MTP —
2338        // reuse is gated off when MTP is active.
2339        let forwarded = input_ids.len() + output_ids.len().saturating_sub(1);
2340        self.kv_history = all_ids[..forwarded.min(all_ids.len())].to_vec();
2341        confidence.truncate(output_ids.len()); // guard against any overshoot
2342        traces.truncate(output_ids.len());
2343        Ok(GenerateResult {
2344            text: self.tokenizer.decode(output_ids),
2345            token_ids: output_ids.to_vec(),
2346            prompt_tokens: input_ids.len(),
2347            tokens_generated: generated,
2348            finish_reason,
2349            mtp_drafted: drafted,
2350            mtp_accepted: accepted,
2351            token_confidence: confidence,
2352            traces,
2353        })
2354    }
2355
2356    /// One MTP step: feed `(hidden_p, token_{p+1})` into the draft head,
2357    /// advance its KV cache at position `p`, return the drafted token
2358    /// for position `p+2`.
2359    fn mtp_step(
2360        &mut self,
2361        m: &mut MtpModule,
2362        hidden: &[f32],
2363        next_token: u32,
2364        position: usize,
2365    ) -> u32 {
2366        self.mtp_step_h(m, hidden, next_token, position).0
2367    }
2368
2369    /// Tally for `CMF_MTP_CHAIN_PROBE`: per depth, how often the CHAIN is
2370    /// still an exact prefix of the real continuation. Printed every 128
2371    /// depth-0 samples so a killed run still shows its table.
2372    fn chain_probe_note(depth: usize, prefix_ok: bool) {
2373        use std::sync::Mutex;
2374        static T: Mutex<Vec<(u64, u64)>> = Mutex::new(Vec::new());
2375        let mut t = T.lock().unwrap();
2376        if t.len() <= depth {
2377            t.resize(depth + 1, (0, 0));
2378        }
2379        t[depth].0 += 1;
2380        t[depth].1 += prefix_ok as u64;
2381        if depth == 0 && t[0].0 % 128 == 0 {
2382            let line: Vec<String> = t
2383                .iter()
2384                .enumerate()
2385                .map(|(d, (n, k))| format!("d{}={:.0}%({n})", d + 1, 100.0 * *k as f64 / (*n).max(1) as f64))
2386                .collect();
2387            eprintln!("mtp-chain: {}", line.join(" "));
2388        }
2389    }
2390
2391    /// `mtp_step` that also hands back the block's own output hidden — the
2392    /// state a CHAINED draft feeds the next step, the way a multi-token
2393    /// speculative round iterates the head on itself.
2394    fn mtp_step_h(
2395        &mut self,
2396        m: &mut MtpModule,
2397        hidden: &[f32],
2398        next_token: u32,
2399        position: usize,
2400    ) -> (u32, Vec<f32>) {
2401        // fc concat order is [enorm(embed); hnorm(hidden)] — EMBEDDING
2402        // FIRST. Verified by the oracle (converter/mtp_oracle.py):
2403        // [emb;hid] → 45.8% acceptance, [hid;emb] → 0.00%.
2404        let e = self.embed_single(next_token);
2405        let mut cat = vec![0.0f32; 2 * self.hidden_size];
2406        let (cat_e, cat_h) = cat.split_at_mut(self.hidden_size);
2407        inference::rms_norm_into(&e, &m.enorm, self.rms_eps, self.norm_style, cat_e);
2408        inference::rms_norm_into(hidden, &m.hnorm, self.rms_eps, self.norm_style, cat_h);
2409        let mut x = vec![0.0f32; self.hidden_size];
2410        m.eh_proj.matvec(&cat, &mut x, self.pool.as_deref());
2411
2412        // One standard transformer block over the MTP's own cache.
2413        let lw = &m.layer;
2414        inference::rms_norm_into(
2415            &x,
2416            &lw.input_norm,
2417            self.rms_eps,
2418            self.norm_style,
2419            &mut self.ws.n1,
2420        );
2421        let attn = match &lw.attn {
2422            // MLA models carry no MTP head; this path cannot see them.
2423            AttnKind::Mla(_) => unreachable!("MLA has no MTP/pair path"),
2424            AttnKind::Kda(_) => unreachable!("KDA has no MTP/pair path"),
2425            AttnKind::Full {
2426                wq,
2427                wk,
2428                wv,
2429                wo,
2430                q_norm,
2431                k_norm,
2432                output_gate,
2433                softplus_gate,
2434                bias,
2435            } => {
2436                let mut cfg = self.attn_cfg(position);
2437                cfg.q_norm = q_norm.as_deref();
2438                cfg.k_norm = k_norm.as_deref();
2439                cfg.output_gate = *output_gate;
2440                cfg.softplus_gate = softplus_gate
2441                    .as_ref()
2442                    .map(|(gate, per_head)| (gate, *per_head));
2443                cfg.bias = bias
2444                    .as_ref()
2445                    .map(|(q, k, v)| (q.as_slice(), k.as_slice(), v.as_slice()));
2446                attention::qwen_attention(&self.ws.n1, wq, wk, wv, wo, &mut m.kv, &cfg)
2447            }
2448            AttnKind::Linear(_) | AttnKind::LinearGdn(_) | AttnKind::ShortConv(_) => {
2449                unreachable!("MTP block is full attention")
2450            }
2451        };
2452        for (i, &a) in attn.iter().enumerate() {
2453            x[i] += a;
2454        }
2455        inference::rms_norm_into(
2456            &x,
2457            &lw.post_norm,
2458            self.rms_eps,
2459            self.norm_style,
2460            &mut self.ws.p1,
2461        );
2462        let ffn = ffn_forward(&lw.ffn, &self.ws.p1, self.pool.as_deref(), None);
2463        for (i, &f) in ffn.iter().enumerate() {
2464            x[i] += f;
2465        }
2466
2467        inference::rms_norm_into(
2468            &x,
2469            &m.final_norm,
2470            self.rms_eps,
2471            self.norm_style,
2472            &mut self.ws.n1,
2473        );
2474        let mut lg = self.lm_head_forward(&self.ws.n1);
2475        let draft = sampler::argmax(&lg);
2476        attention::recycle_buf(&mut lg);
2477        (draft, x)
2478    }
2479
2480    /// The MTP block alone — advance its KV with a (hidden, token) pair the
2481    /// verify just proved, without paying the head. What keeps the draft's
2482    /// attention context warm between speculative rounds.
2483    fn mtp_warm(&mut self, m: &mut MtpModule, hidden: &[f32], next_token: u32, position: usize) {
2484        let e = self.embed_single(next_token);
2485        let mut cat = vec![0.0f32; 2 * self.hidden_size];
2486        let (cat_e, cat_h) = cat.split_at_mut(self.hidden_size);
2487        inference::rms_norm_into(&e, &m.enorm, self.rms_eps, self.norm_style, cat_e);
2488        inference::rms_norm_into(hidden, &m.hnorm, self.rms_eps, self.norm_style, cat_h);
2489        let mut x = vec![0.0f32; self.hidden_size];
2490        m.eh_proj.matvec(&cat, &mut x, self.pool.as_deref());
2491        inference::rms_norm_into(&x, &m.layer.input_norm, self.rms_eps, self.norm_style, &mut self.ws.n1);
2492        let attn = match &m.layer.attn {
2493            AttnKind::Full { wq, wk, wv, wo, q_norm, k_norm, output_gate, softplus_gate, bias } => {
2494                let mut cfg = self.attn_cfg(position);
2495                cfg.q_norm = q_norm.as_deref();
2496                cfg.k_norm = k_norm.as_deref();
2497                cfg.output_gate = *output_gate;
2498                cfg.softplus_gate = softplus_gate.as_ref().map(|(g, p)| (g, *p));
2499                cfg.bias = bias.as_ref().map(|(q, k, v)| (q.as_slice(), k.as_slice(), v.as_slice()));
2500                attention::qwen_attention(&self.ws.n1, wq, wk, wv, wo, &mut m.kv, &cfg)
2501            }
2502            _ => return,
2503        };
2504        let _ = attn;
2505    }
2506
2507    /// Speculative decode ON the wgpu whole-token graph: draft k with the
2508    /// MTP head, verify all of them plus the tip in ONE batched graph
2509    /// submit whose tail folds the head, commit the accepted prefix and
2510    /// roll the GDN state back to the last real position. Greedy only —
2511    /// output equals the plain graph's token for token, the way the DSV4
2512    /// verify equals the walk.
2513    #[cfg(feature = "gpu")]
2514    #[allow(clippy::too_many_arguments)]
2515    fn graph_spec_step(
2516        &mut self,
2517        m: &mut MtpModule,
2518        hidden: &[f32],
2519        t_next: u32,
2520        next_pos: usize,
2521        drafted: &mut usize,
2522        accepted: &mut usize,
2523    ) -> Option<(Vec<u32>, usize, Vec<f32>)> {
2524        // 3 is the measured optimum on Qwen3.6-27B / RTX 5090 (medians
2525        // of three, greedy): 51.1 tok/s against a plain 49.4, where k=2
2526        // gives 46.1, k=4 50.0, k=5 47.4, k=6 45.2. Acceptance is 89-91%
2527        // throughout — what turns the curve over is the verify, which
2528        // costs ~7.4 ms per extra position, and the draft ~3 ms a step.
2529        let k_spec: usize = std::env::var("CMF_GRAPH_SPEC_K")
2530            .ok()
2531            .and_then(|v| v.parse().ok())
2532            .filter(|&v| (1..=8).contains(&v))
2533            .unwrap_or(3);
2534        if next_pos == 0 {
2535            return None;
2536        }
2537        let t_round = std::time::Instant::now();
2538        // Submissions per phase — and they say where the round's money is.
2539        // Qwen3.6-27B on an RTX 5090, k=3:
2540        //
2541        //   draft   9.3 ms / 12 submissions   (four per MTP step)
2542        //   verify 52.8 ms /  1               (the batched graph)
2543        //   commit  5.4 ms /  6               (two per warm)
2544        //
2545        // The verify is already one submit. The draft's own work is 834 MB
2546        // a step — 0.8 ms at this card's measured 1056 GB/s — against 3.1
2547        // ms measured, so ~0.58 ms of every step is round trip, not
2548        // arithmetic, and the same holds for the warms. Eighteen round
2549        // trips a round at roughly half a millisecond each is ~11 ms of a
2550        // 68 ms round: fusing the MTP block into ONE submit the way the
2551        // trunk already is projects to ~64 tok/s against today's 50.9.
2552        // That is the largest measured item left on this path.
2553        let subs = || crate::gpu_wgpu::SUBMITS.load(std::sync::atomic::Ordering::Relaxed);
2554        let sub0 = subs();
2555        // Draft the chain: first from the trunk's tip hidden, then the head
2556        // iterating on itself. Rows land in the MTP KV; the chain rows past
2557        // the first are speculation over speculative state and roll back
2558        // below, replaced by verified pairs.
2559        let mut drafts = Vec::with_capacity(k_spec);
2560        let (d1, mut hx) = self.mtp_step_h(m, hidden, t_next, next_pos - 1);
2561        drafts.push(d1);
2562        for j in 1..k_spec {
2563            let (dj, hj) = self.mtp_step_h(m, &hx, drafts[j - 1], next_pos - 1 + j);
2564            drafts.push(dj);
2565            hx = hj;
2566        }
2567        *drafted += k_spec;
2568        let t_draft = t_round.elapsed();
2569        let sub_draft = subs();
2570        // Verify batch: [t_next, d1 .. d_{k-1}] at next_pos.. — every row's
2571        // logits come back from the graph's own head.
2572        let b = k_spec + 1;
2573        let mut hiddens = vec![0.0f32; b * self.hidden_size];
2574        for (i, &t) in std::iter::once(&t_next).chain(drafts.iter()).enumerate() {
2575            let e = self.embed_single(t);
2576            hiddens[i * self.hidden_size..(i + 1) * self.hidden_size].copy_from_slice(&e);
2577        }
2578        let positions: Vec<usize> = (next_pos..next_pos + b).collect();
2579        let (lm_gw, lm_rows) = {
2580            let (_, i, kind, rs) = self.weights.lm_head.graph_weight()?;
2581            (
2582                crate::gpu::GraphW { idx: i, kind, row_scale: rs, data: &[] },
2583                self.weights.lm_head.rows(),
2584            )
2585        };
2586        let mut logits = Vec::new();
2587        let final_norm = self.weights.final_norm.clone();
2588        let ok = self.try_batch_graph_wgpu(
2589            &mut hiddens,
2590            &positions,
2591            b,
2592            Some(crate::gpu::SpecTail {
2593                lm: lm_gw,
2594                lm_rows,
2595                final_norm: &final_norm,
2596                logits_out: &mut logits,
2597            }),
2598        );
2599        if !ok {
2600            // Roll the draft rows back out of the MTP cache and decline —
2601            // the caller runs the plain path, nothing has changed.
2602            m.kv.truncate_last(k_spec);
2603            return None;
2604        }
2605        let t_verify = t_round.elapsed();
2606        let sub_verify = subs();
2607        // Acceptance: row i's argmax is the trunk's token after input i.
2608        let ids: Vec<u32> = (0..b)
2609            .map(|i| sampler::argmax(&logits[i * lm_rows..(i + 1) * lm_rows]))
2610            .collect();
2611        let mut a = 0usize;
2612        while a < k_spec && ids[a] == drafts[a] {
2613            a += 1;
2614        }
2615        // a fully-accepted round needs no restore: every input was real.
2616        if a + 1 < b {
2617            crate::gpu::gdn_spec_restore(self.graph_kv_id, a);
2618        }
2619        *accepted += a;
2620        // MTP cache: keep the first draft row (its inputs were real), drop
2621        // the chain's, then append the verified pairs the round produced.
2622        // Each of those is a whole MTP block on the per-op path and they
2623        // cost 5.8 ms of a 69 ms round at k=3 — a third of what the
2624        // round's own draft costs. PRICED, and they earn it: skipping
2625        // them (`CMF_SPEC_WARM=0`) drops acceptance from 89% to 81% at
2626        // k=3 and 85% to 74% at k=4, and the tok/s goes nowhere at k=3
2627        // (50.3 against 50.5) and backwards at k=4 (48.1 against 50.1).
2628        // The knob stays so the next person can re-price it after the
2629        // warms are batched instead of assuming either way.
2630        m.kv.truncate_last(k_spec.saturating_sub(1));
2631        let warm_off = std::env::var("CMF_SPEC_WARM").is_ok_and(|v| v == "0");
2632        if !warm_off {
2633            for j in 0..a {
2634                let row = &hiddens[j * self.hidden_size..(j + 1) * self.hidden_size];
2635                let row = row.to_vec();
2636                self.mtp_warm(m, &row, ids[j], next_pos + j);
2637            }
2638        }
2639        // The sampler's contract: logits of the LAST verified position.
2640        let mut row = logits[a * lm_rows..(a + 1) * lm_rows].to_vec();
2641        row.resize(self.vocab_size, 0.0);
2642        if let Some(c) = self.final_softcap {
2643            for l in row.iter_mut() {
2644                *l = c * (*l / c).tanh();
2645            }
2646        }
2647        self.graph_logits = Some(row);
2648        let new_hidden = hiddens[a * self.hidden_size..(a + 1) * self.hidden_size].to_vec();
2649        // Three phases, not two. The round's wall clock was 4 ms longer
2650        // than draft+verify and the difference had nowhere to be seen:
2651        // the accepted prefix re-runs the MTP block once per token to
2652        // keep the draft head's attention cache warm, and the GDN state
2653        // rolls back on any rejection. Both live here, after the verify.
2654        if std::env::var("CMF_GRAPH_SPEC_TIME").is_ok() {
2655            let end = subs();
2656            eprintln!(
2657                "spec-round: draft {:.1} ms/{} sub | verify {:.1} ms/{} sub | \
2658                 commit {:.1} ms/{} sub (accepted {a} of {k_spec})",
2659                t_draft.as_secs_f64() * 1e3,
2660                sub_draft - sub0,
2661                (t_verify - t_draft).as_secs_f64() * 1e3,
2662                sub_verify - sub_draft,
2663                (t_round.elapsed() - t_verify).as_secs_f64() * 1e3,
2664                end - sub_verify,
2665            );
2666        }
2667        Some((drafts[..a].to_vec(), next_pos + a + 1, new_hidden))
2668    }
2669
2670    /// Micro-benchmark: two single-position forwards vs one fused pair
2671    /// from the current cache state (KV rewound after each probe).
2672    /// Returns (two_singles_ms, fused_pair_ms) per probe, or the (0, 0)
2673    /// sentinel when this model has no pair path to measure — the same
2674    /// answer the o1 arm gives, and the bench prints it the same way.
2675    /// (An architecture that loads its own layers leaves `weights.layers`
2676    /// empty; walking it here was an index panic, found by `bench` on
2677    /// deepseek_v4.)
2678    pub fn measure_pair_fusion(&mut self, iters: usize) -> (f64, f64) {
2679        if !self.pair_supported() {
2680            return (0.0, 0.0);
2681        }
2682        let emb1 = self.embed_single(1);
2683        let emb2 = self.embed_single(2);
2684        let pos = self.kv_cache.seq_len();
2685
2686        let t0 = std::time::Instant::now();
2687        for _ in 0..iters {
2688            let _ = self.forward_layers(&emb1, pos, None);
2689            let _ = self.forward_layers(&emb2, pos + 1, None);
2690            for l in &mut self.kv_cache.layers {
2691                l.truncate_last(2);
2692            }
2693        }
2694        let singles_ms = t0.elapsed().as_secs_f64() * 1000.0 / iters as f64;
2695
2696        let t1 = std::time::Instant::now();
2697        for _ in 0..iters {
2698            let _ = self.forward_pair(&emb1, &emb2, pos);
2699            for l in &mut self.kv_cache.layers {
2700                l.truncate_last(2);
2701            }
2702        }
2703        let pair_ms = t1.elapsed().as_secs_f64() * 1000.0 / iters as f64;
2704        (singles_ms, pair_ms)
2705    }
2706
2707    /// Fused two-position forward: weight rows are streamed from memory
2708    /// once per layer for both positions. Full layers → fused GQA pair;
2709    /// linear layers → vmf_phase pair (lane 2 state is tentative in the
2710    /// per-layer scratch until the draft is accepted).
2711    /// Whether the fused two-position path covers every layer kind in
2712    /// this model. MLA and KDA run per position (their pair arms are
2713    /// unreachable); the seq prefill falls back to singles for them.
2714    fn pair_supported(&self) -> bool {
2715        // An EMPTY layer stack means the architecture loaded its own and
2716        // this path has nothing to walk. Checking that directly, rather
2717        // than naming each such architecture, is what makes the guard hold
2718        // for the next one: `any()` over no layers is false, so a
2719        // feature-by-feature test says "supported" for a model that has no
2720        // layers here at all.
2721        !self.weights.layers.is_empty()
2722            && self.g3n.is_none()
2723            && !self
2724                .weights
2725                .layers
2726                .iter()
2727                .any(|lw| matches!(&lw.attn, AttnKind::Mla(_) | AttnKind::Kda(_)))
2728    }
2729
2730    fn forward_pair(
2731        &mut self,
2732        emb1: &[f32],
2733        emb2: &[f32],
2734        position: usize,
2735    ) -> (Vec<f32>, Vec<f32>) {
2736        let mut h1 = emb1.to_vec();
2737        let mut h2 = emb2.to_vec();
2738        let (_nkv, _hd, hs, _rd, eps) = (
2739            self.num_kv_heads,
2740            self.head_dim,
2741            self.hidden_size,
2742            self.rotary_dim,
2743            self.rms_eps,
2744        );
2745        let pool = self.pool.clone();
2746
2747        for li in 0..self.num_layers {
2748            let lw = &self.weights.layers[self.phys_layer(li)];
2749            // Norms into pipeline scratch (4 allocs/layer on the MTP
2750            // decode hot path before this).
2751            inference::rms_norm_into(
2752                &h1,
2753                &lw.input_norm,
2754                self.rms_eps,
2755                self.norm_style,
2756                &mut self.ws.n1,
2757            );
2758            inference::rms_norm_into(
2759                &h2,
2760                &lw.input_norm,
2761                self.rms_eps,
2762                self.norm_style,
2763                &mut self.ws.n2,
2764            );
2765
2766            let (a1, a2) = match &lw.attn {
2767                AttnKind::Mla(_) => unreachable!("MLA has no MTP/pair path"),
2768                AttnKind::Kda(_) => unreachable!("KDA has no MTP/pair path"),
2769                AttnKind::Linear(w) => {
2770                    let cfg = self.vmf_cfg.expect("linear layer without vmf_cfg");
2771                    let layer = &mut self.kv_cache.layers[li];
2772                    let (state, scratch) = (&mut layer.linear_state, &mut layer.linear_scratch);
2773                    vmf_phase_pair(
2774                        &self.ws.n1,
2775                        &self.ws.n2,
2776                        w,
2777                        &cfg,
2778                        state,
2779                        scratch,
2780                        self.pool.as_deref(),
2781                    )
2782                }
2783                AttnKind::LinearGdn(w) => {
2784                    let cfg = self.gdn_cfg.expect("gdn layer without gdn_cfg");
2785                    let layer = &mut self.kv_cache.layers[li];
2786                    let (state, scratch) = (&mut layer.linear_state, &mut layer.linear_scratch);
2787                    gdn_pair(
2788                        &self.ws.n1,
2789                        &self.ws.n2,
2790                        w,
2791                        &cfg,
2792                        state,
2793                        scratch,
2794                        self.pool.as_deref(),
2795                    )
2796                }
2797                AttnKind::ShortConv(w) => {
2798                    let cfg = self
2799                        .short_conv_cfg
2800                        .expect("short-conv layer without short_conv_cfg");
2801                    let layer = &mut self.kv_cache.layers[li];
2802                    let (state, scratch) = (&mut layer.linear_state, &mut layer.linear_scratch);
2803                    short_conv_pair(
2804                        &self.ws.n1,
2805                        &self.ws.n2,
2806                        w,
2807                        &cfg,
2808                        state,
2809                        scratch,
2810                        self.pool.as_deref(),
2811                    )
2812                }
2813                AttnKind::Full {
2814                    wq,
2815                    wk,
2816                    wv,
2817                    wo,
2818                    q_norm,
2819                    k_norm,
2820                    output_gate,
2821                    softplus_gate,
2822                    bias,
2823                } => {
2824                    let inv_freq_l = self.layer_inv_freq(li);
2825                    let (nkv_l, hd_l, rd_l) = self.layer_geom(li);
2826                    let cfg = QwenAttnCfg {
2827                        num_heads: self.layer_num_heads(li),
2828                        num_kv_heads: nkv_l,
2829                        head_dim: hd_l,
2830                        hidden_size: hs,
2831                        position,
2832                        inv_freq: &inv_freq_l,
2833                        rotary_dim: rd_l,
2834                        scale: self.attn_scale,
2835                        softcap: self.attn_softcap,
2836                        window: self.layer_window(li),
2837                        v_norm: self.attn_v_norm,
2838                        q_norm: q_norm.as_deref(),
2839                        k_norm: k_norm.as_deref(),
2840                        output_gate: *output_gate,
2841                        softplus_gate: softplus_gate
2842                            .as_ref()
2843                            .map(|(gate, per_head)| (gate, *per_head)),
2844                        rope_scale: self.layer_rope_scale(li),
2845                        bias: bias
2846                            .as_ref()
2847                            .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
2848                        rms_eps: eps,
2849                        norm_style: self.norm_style,
2850                        pool: pool.as_deref(),
2851                    };
2852                    attention::qwen_attention_pair(
2853                        &self.ws.n1,
2854                        &self.ws.n2,
2855                        wq,
2856                        wk,
2857                        wv,
2858                        wo,
2859                        &mut self.kv_cache.layers[li],
2860                        &cfg,
2861                    )
2862                }
2863            };
2864            let (a1, a2) = match &self.weights.layers[self.phys_layer(li)].attn_out_norm {
2865                Some(w) => (
2866                    inference::rms_norm(&a1, w, self.rms_eps, self.norm_style),
2867                    inference::rms_norm(&a2, w, self.rms_eps, self.norm_style),
2868                ),
2869                None => (a1, a2),
2870            };
2871            for i in 0..self.hidden_size {
2872                h1[i] += a1[i];
2873                h2[i] += a2[i];
2874            }
2875            let (mut a1, mut a2) = (a1, a2);
2876            attention::recycle_buf(&mut a1);
2877            attention::recycle_buf(&mut a2);
2878
2879            let lw = &self.weights.layers[self.phys_layer(li)];
2880            inference::rms_norm_into(
2881                &h1,
2882                &lw.post_norm,
2883                self.rms_eps,
2884                self.norm_style,
2885                &mut self.ws.p1,
2886            );
2887            inference::rms_norm_into(
2888                &h2,
2889                &lw.post_norm,
2890                self.rms_eps,
2891                self.norm_style,
2892                &mut self.ws.p2,
2893            );
2894            let (f1, f2) = match &lw.ffn {
2895                // Dual-branch layers need the raw residuals — run the
2896                // two positions through the same fn decode uses.
2897                FfnKind::DenseMoe(dm) => (
2898                    dense_moe_ffn(
2899                        dm,
2900                        &self.ws.p1,
2901                        &h1,
2902                        self.rms_eps,
2903                        self.norm_style,
2904                        self.pool.as_deref(),
2905                    ),
2906                    dense_moe_ffn(
2907                        dm,
2908                        &self.ws.p2,
2909                        &h2,
2910                        self.rms_eps,
2911                        self.norm_style,
2912                        self.pool.as_deref(),
2913                    ),
2914                ),
2915                _ => ffn_forward_pair(
2916                    &lw.ffn,
2917                    &self.ws.p1,
2918                    &self.ws.p2,
2919                    self.pool.as_deref(),
2920                    None,
2921                ),
2922            };
2923            let (f1, f2) = match &self.weights.layers[self.phys_layer(li)].ffn_out_norm {
2924                Some(w) => (
2925                    inference::rms_norm(&f1, w, self.rms_eps, self.norm_style),
2926                    inference::rms_norm(&f2, w, self.rms_eps, self.norm_style),
2927                ),
2928                None => (f1, f2),
2929            };
2930            for i in 0..self.hidden_size {
2931                h1[i] += f1[i];
2932                h2[i] += f2[i];
2933            }
2934            let (mut f1, mut f2) = (f1, f2);
2935            attention::recycle_buf(&mut f1);
2936            attention::recycle_buf(&mut f2);
2937            if let Some(sc) = self.weights.layers[self.phys_layer(li)].layer_scale {
2938                for i in 0..self.hidden_size {
2939                    h1[i] *= sc;
2940                    h2[i] *= sc;
2941                }
2942            }
2943            // Looped Transformer: apply final norm at the end of each loop iteration.
2944            if self.is_loop_end(li) && li + 1 < self.num_layers {
2945                h1 = inference::rms_norm(
2946                    &h1,
2947                    &self.weights.final_norm,
2948                    self.rms_eps,
2949                    self.norm_style,
2950                );
2951                h2 = inference::rms_norm(
2952                    &h2,
2953                    &self.weights.final_norm,
2954                    self.rms_eps,
2955                    self.norm_style,
2956                );
2957            }
2958        }
2959        (h1, h2)
2960    }
2961
2962    /// Commit lane-2 linear states after an accepted draft.
2963    fn commit_linear_scratch(&mut self) {
2964        for layer in &mut self.kv_cache.layers {
2965            if !layer.linear_scratch.is_empty() {
2966                std::mem::swap(&mut layer.linear_state, &mut layer.linear_scratch);
2967                layer.linear_scratch.clear();
2968            }
2969        }
2970    }
2971
2972    /// Forward a full id sequence from a fresh cache and return the
2973    /// logits after the last position (golden-parity harness, bench).
2974    pub fn forward_ids(
2975        &mut self,
2976        ids: &[u32],
2977        task_mask: Option<&TaskMask>,
2978    ) -> Result<Vec<f32>, String> {
2979        if ids.is_empty() {
2980            return Err("empty id sequence".to_string());
2981        }
2982        self.kv_cache.clear();
2983        self.kv_history.clear();
2984        self.o1_begin();
2985        let mut hidden = vec![0.0f32; self.hidden_size];
2986        let mut pos = 0usize;
2987        // Same routing predicate generation uses. Two reasons it must be
2988        // the same one: (1) a GDN hybrid's recurrent state is GPU-
2989        // resident, and a batched CPU prefill would build it on the host
2990        // only — decode then reads buffers the prefill never wrote;
2991        // (2) bench times THIS function and calls the result "prefill",
2992        // so a different path here reports a number production never
2993        // sees (W2 on 2×5090: 8.7 tok/s reported against 125 real).
2994        if self.can_prefill_batched() && !self.graph_prefill_preferred() && ids.len() > 2 {
2995            // prefill-GEMM in chunks; only the last position's hidden is
2996            // needed. (o1-compatible: the batch path attends per position
2997            // through qwen_attention, which carries the collection hook.)
2998            let chunk = prefill_chunk();
2999            let hs = self.hidden_size;
3000            while pos < ids.len() {
3001                let end = (pos + chunk).min(ids.len());
3002                let hb = self.prefill_batch_masked(&ids[pos..end], pos, task_mask);
3003                hidden.copy_from_slice(&hb[(end - pos - 1) * hs..]);
3004                pos = end;
3005            }
3006        }
3007        // Same guards as generation's prefill — INCLUDING the graph one.
3008        // The CPU pair walk was intercepting positions that the resident
3009        // token graph would have run itself: on a GDN hybrid over wgpu
3010        // that is 89 ms of host forward against 7 ms of device submit,
3011        // and it made prefill look 12× slower than it is (W2 on an RTX
3012        // 5090, ctx 512: 11.2 tok/s with the walk, 136.6 without).
3013        // CMF_PAIR=0 opts out; a model whose layers live outside
3014        // `weights.layers` has no pair walk to take.
3015        if task_mask.is_none()
3016            && !self.graph_prefill_preferred()
3017            && !std::env::var("CMF_PAIR").is_ok_and(|v| v == "0")
3018            && self.pair_supported()
3019        {
3020            while pos + 1 < ids.len() {
3021                let e1 = self.embed_single(ids[pos]);
3022                let e2 = self.embed_single(ids[pos + 1]);
3023                let (_, h2) = self.forward_pair(&e1, &e2, pos);
3024                self.commit_linear_scratch();
3025                hidden = h2;
3026                pos += 2;
3027            }
3028        }
3029        while pos < ids.len() {
3030            hidden = self.forward_layers(&self.embed_single(ids[pos]), pos, task_mask);
3031            pos += 1;
3032        }
3033        // Harness contract: after forward_ids the cache is decode-ready —
3034        // under o1 that means sealed (bench measures the seal as part of
3035        // prefill, honestly).
3036        self.o1_seal();
3037        let normed = inference::rms_norm(
3038            &hidden,
3039            &self.weights.final_norm,
3040            self.rms_eps,
3041            self.norm_style,
3042        );
3043        Ok(self.lm_head_forward(&normed))
3044    }
3045
3046    /// Teacher-forced perplexity over a token sequence (phase-C gate:
3047    /// honest quant comparisons instead of prompt vibes).
3048    ///
3049    /// Attention is EXACT even on a model whose layers are flagged for
3050    /// the O(1) kernel — scoring the backbone is the default on purpose
3051    /// (it is the yardstick). `nll_ids_o1` scores the CONVERTED model.
3052    pub fn ppl_ids(&mut self, ids: &[u32]) -> f64 {
3053        let (nll, cnt) = self.nll_ids_from(ids, 0);
3054        (nll / cnt.max(1) as f64).exp()
3055    }
3056
3057    /// DTG-MA calibration pass (Patent 2): run `ids` through the model
3058    /// (CPU path, per position) and return each layer's per-neuron
3059    /// activation mass Σ|silu(gate)·up| — the statistic the task-guided
3060    /// FFN mask is derived from.
3061    pub fn probe_ffn_mass(&mut self, ids: &[u32]) -> Vec<Vec<f64>> {
3062        self.kv_cache.clear();
3063        self.kv_history.clear();
3064        FFN_PROBE.with(|p| {
3065            *p.borrow_mut() = Some(vec![vec![0f64; self.intermediate_size]; self.num_layers]);
3066        });
3067        crate::gpu::cpu_scope(|| {
3068            for (pos, &id) in ids.iter().enumerate() {
3069                let emb = self.embed_single(id);
3070                let _ = self.forward_layers(&emb, pos, None);
3071            }
3072        });
3073        self.kv_cache.clear();
3074        self.kv_history.clear();
3075        FFN_PROBE
3076            .with(|p| p.borrow_mut().take())
3077            .unwrap_or_default()
3078    }
3079
3080    /// Teacher-forced PPL with a task mask active (sparse execution) —
3081    /// the quality gate for a DTG-MA-masked skill. Sequential per
3082    /// position: the batched prefill path is dense-only.
3083    pub fn ppl_ids_masked(&mut self, ids: &[u32], mask: &TaskMask) -> f64 {
3084        self.kv_cache.clear();
3085        self.kv_history.clear();
3086        let mut nll = 0f64;
3087        let mut cnt = 0usize;
3088        let mut hidden = vec![0f32; self.hidden_size];
3089        for (pos, &id) in ids.iter().enumerate() {
3090            if pos > 0 {
3091                inference::rms_norm_into(
3092                    &hidden,
3093                    &self.weights.final_norm,
3094                    self.rms_eps,
3095                    self.norm_style,
3096                    &mut self.ws.n1,
3097                );
3098                let mut logits = self.lm_head_forward(&self.ws.n1);
3099                let max = logits.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
3100                let sum: f64 = logits.iter().map(|&v| ((v - max) as f64).exp()).sum();
3101                let p = ((logits[id as usize] - max) as f64).exp() / sum.max(1e-300);
3102                nll -= p.max(1e-300).ln();
3103                cnt += 1;
3104                attention::recycle_buf(&mut logits);
3105            }
3106            let emb = self.embed_single(id);
3107            hidden = self.forward_layers(&emb, pos, Some(mask));
3108        }
3109        self.kv_cache.clear();
3110        self.kv_history.clear();
3111        (nll / cnt.max(1) as f64).exp()
3112    }
3113
3114    /// Teacher-forced NLL sum + scored-token count over positions
3115    /// `start..len-1`, attention EXACT. Positions below `start` still
3116    /// run — they are the context — they are just not scored, so this
3117    /// pairs with `nll_ids_o1(ids, start)` over the very same tokens.
3118    ///
3119    /// Returning (nll, cnt) rather than a ppl is what lets a windowed
3120    /// caller combine windows before the exp, so every scored token
3121    /// weighs the same regardless of how the windows are cut.
3122    /// `nll_ids_from` with a task mask held active at every position.
3123    ///
3124    /// The batched prefill path does not thread masks, so this walks the
3125    /// per-position forward — slower, but it scores the file exactly the
3126    /// way `run --task` will serve it, which is the point of the gate
3127    /// that calls it. With `None` it defers to the fast path.
3128    /// Masked scoring rides the SAME batched sweep as unmasked scoring —
3129    /// the masked-inference fast path: `prefill_batch_masked` lands the
3130    /// per-visit FFN rows on the activations inside the fused arms. The
3131    /// per-position loop below remains only as the no-batch fallback.
3132    pub fn nll_ids_masked(
3133        &mut self,
3134        ids: &[u32],
3135        start: usize,
3136        task_mask: Option<&TaskMask>,
3137    ) -> (f64, usize) {
3138        self.nll_ids_inner(ids, start, task_mask)
3139    }
3140
3141    pub fn nll_ids_from(&mut self, ids: &[u32], start: usize) -> (f64, usize) {
3142        self.nll_ids_inner(ids, start, None)
3143    }
3144
3145    fn nll_ids_inner(
3146        &mut self,
3147        ids: &[u32],
3148        start: usize,
3149        task_mask: Option<&TaskMask>,
3150    ) -> (f64, usize) {
3151        self.kv_cache.clear();
3152        self.kv_history.clear();
3153        let mut nll = 0f64;
3154        let mut cnt = 0usize;
3155        if self.can_prefill_batched() {
3156            // prefill-GEMM: layer-major position chunks, lm_head batched
3157            // (254MB lm_head read once per chunk, not per position).
3158            // The layer chunk is large (grouping positions by MoE experts
3159            // wins with size), lm_head in sub-blocks (logit buffer
3160            // 32×vocab ≈ 32MB instead of 128×).
3161            const CHUNK: usize = 128;
3162            const LM_SUB: usize = 32;
3163            let n = ids.len().saturating_sub(1);
3164            let hs = self.hidden_size;
3165            let rows = self.weights.lm_head.rows();
3166            let mut pos = 0usize;
3167            while pos < n {
3168                let end = (pos + CHUNK).min(n);
3169                let bsz = end - pos;
3170                let hb = self.prefill_batch_masked(&ids[pos..end], pos, task_mask);
3171                let mut k0 = 0usize;
3172                while k0 < bsz {
3173                    let k1 = (k0 + LM_SUB).min(bsz);
3174                    let sb = k1 - k0;
3175                    // Sub-block entirely below the scored range: the KV
3176                    // it just built is all this pass needed from it.
3177                    if pos + k1 <= start {
3178                        k0 = k1;
3179                        continue;
3180                    }
3181                    let mut normed = vec![0.0f32; sb * hs];
3182                    for k in 0..sb {
3183                        let r = inference::rms_norm(
3184                            &hb[(k0 + k) * hs..(k0 + k + 1) * hs],
3185                            &self.weights.final_norm,
3186                            self.rms_eps,
3187                            self.norm_style,
3188                        );
3189                        normed[k * hs..(k + 1) * hs].copy_from_slice(&r);
3190                    }
3191                    let mut logits = vec![0.0f32; sb * rows];
3192                    self.weights
3193                        .lm_head
3194                        .matmat(&normed, sb, &mut logits, self.pool.as_deref());
3195                    for k in 0..sb {
3196                        if pos + k0 + k < start {
3197                            continue;
3198                        }
3199                        let lg = &mut logits[k * rows..k * rows + self.vocab_size.min(rows)];
3200                        if let Some(mu) = self.logit_multiplier {
3201                            for v in lg.iter_mut() {
3202                                *v *= mu;
3203                            }
3204                        }
3205                        // Gemma-class final-logit soft-capping: the
3206                        // decode paths apply it; scoring must too, or
3207                        // the uncapped softmax misprices every token.
3208                        if let Some(c) = self.final_softcap {
3209                            for v in lg.iter_mut() {
3210                                *v = c * (*v / c).tanh();
3211                            }
3212                        }
3213                        let lg = &logits[k * rows..k * rows + self.vocab_size.min(rows)];
3214                        let target = ids[pos + k0 + k + 1] as usize;
3215                        let max = lg.iter().fold(f32::NEG_INFINITY, |m, &v| m.max(v));
3216                        let lse: f64 = lg
3217                            .iter()
3218                            .map(|&v| ((v - max) as f64).exp())
3219                            .sum::<f64>()
3220                            .ln()
3221                            + max as f64;
3222                        nll += lse - lg[target] as f64;
3223                        cnt += 1;
3224                        if std::env::var("CMF_PPL_TRACE").is_ok() {
3225                            let top = lg
3226                                .iter()
3227                                .enumerate()
3228                                .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
3229                                .map(|(i, _)| i)
3230                                .unwrap_or(0);
3231                            eprintln!(
3232                                "BTRACE pos {} target {} nll {:.4} top {} lg_t {:.3} lg_top {:.3}",
3233                                pos + k0 + k,
3234                                target,
3235                                lse - lg[target] as f64,
3236                                top,
3237                                lg[target],
3238                                lg[top]
3239                            );
3240                        }
3241                    }
3242                    k0 = k1;
3243                }
3244                pos = end;
3245            }
3246            self.kv_cache.clear();
3247            self.kv_history.clear();
3248            return (nll, cnt);
3249        }
3250        for pos in 0..ids.len().saturating_sub(1) {
3251            let hidden = self.forward_layers(&self.embed_single(ids[pos]), pos, task_mask);
3252            // Architectures whose head lives inside their own stack return
3253            // the logits out of band and a zero hidden — DeepSeek-V4 folds
3254            // its hyper-connection copies between the last layer and the
3255            // norm, so it cannot hand back a vector this loop could use.
3256            // Scoring the zeros gave a perplexity of exactly the vocabulary
3257            // size, which is a uniform distribution reported as a
3258            // measurement. `generate` already reads this channel.
3259            let out_of_band = self.graph_logits.take();
3260            if pos < start {
3261                continue;
3262            }
3263            let logits = match out_of_band {
3264                Some(lg) => lg,
3265                None => {
3266                    let normed = inference::rms_norm(
3267                        &hidden,
3268                        &self.weights.final_norm,
3269                        self.rms_eps,
3270                        self.norm_style,
3271                    );
3272                    // lm_head_forward applies the final-logit softcap itself
3273                    // — capping again here double-squashed gemma-class
3274                    // logits (tanh∘tanh) and reported a flattered ppl.
3275                    self.lm_head_forward(&normed)
3276                }
3277            };
3278            let target = ids[pos + 1] as usize;
3279            let max = logits.iter().fold(f32::NEG_INFINITY, |m, &v| m.max(v));
3280            let lse: f64 = logits
3281                .iter()
3282                .map(|&v| ((v - max) as f64).exp())
3283                .sum::<f64>()
3284                .ln()
3285                + max as f64;
3286            let tok_nll = lse - logits[target] as f64;
3287            if std::env::var("CMF_PPL_TRACE").is_ok() && pos < 48 {
3288                let top = logits
3289                    .iter()
3290                    .enumerate()
3291                    .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
3292                    .map(|(i, _)| i)
3293                    .unwrap_or(0);
3294                eprintln!(
3295                    "pos {pos:3} tgt {target:6} nll {tok_nll:7.3} | top1 {top:6} lg[t]={:.2} lg[top]={:.2}",
3296                    logits[target], logits[top]
3297                );
3298            }
3299            nll += tok_nll;
3300            cnt += 1;
3301        }
3302        self.kv_cache.clear();
3303        self.kv_history.clear();
3304        (nll, cnt)
3305    }
3306
3307    /// Teacher-forced NLL of the CONVERTED model: the O(1) Nyström path
3308    /// is ACTIVE over the scored positions. Returns (nll sum, scored
3309    /// count) over `prefill..len-1`.
3310    ///
3311    /// Runtime discipline, deliberately NOT the matrix probe's: the
3312    /// first `prefill` tokens run the exact prompt pass — that pass is
3313    /// what freezes the landmarks and M — and every scored position then
3314    /// goes through `NystromState::step()`, the same code decode runs.
3315    /// So the landmarks are PREFILL-frozen (what ships), not
3316    /// full-sequence oracles (what the published probe measured), and
3317    /// every scored row carries a real far field rather than sitting
3318    /// inside the exact window.
3319    ///
3320    /// Pair with `nll_ids_from(ids, prefill)` for the exact baseline
3321    /// over the identical token set — that ratio is the honest one.
3322    pub fn nll_ids_o1(&mut self, ids: &[u32], prefill: usize) -> (f64, usize) {
3323        self.kv_cache.clear();
3324        self.kv_history.clear();
3325        self.o1_begin();
3326        let n = ids.len().saturating_sub(1);
3327        let p = prefill.min(n);
3328        // Exact prompt pass over ids[..p]: the seal consumes its q/k/v.
3329        let mut pos = 0usize;
3330        if self.can_prefill_batched() {
3331            const CHUNK: usize = 128;
3332            while pos < p {
3333                let end = (pos + CHUNK).min(p);
3334                let _ = self.prefill_batch(&ids[pos..end], pos);
3335                pos = end;
3336            }
3337        } else {
3338            while pos < p {
3339                let _ = self.forward_layers(&self.embed_single(ids[pos]), pos, None);
3340                pos += 1;
3341            }
3342        }
3343        self.o1_seal();
3344
3345        let mut nll = 0f64;
3346        let mut cnt = 0usize;
3347        for pos in p..n {
3348            let hidden = self.forward_layers(&self.embed_single(ids[pos]), pos, None);
3349            let normed = inference::rms_norm(
3350                &hidden,
3351                &self.weights.final_norm,
3352                self.rms_eps,
3353                self.norm_style,
3354            );
3355            // lm_head_forward applies the final-logit softcap itself —
3356            // capping again here double-squashed gemma-class logits
3357            // (tanh∘tanh) and reported a flattered ppl.
3358            let logits = self.lm_head_forward(&normed);
3359            let target = ids[pos + 1] as usize;
3360            let max = logits.iter().fold(f32::NEG_INFINITY, |m, &v| m.max(v));
3361            let lse: f64 = logits
3362                .iter()
3363                .map(|&v| ((v - max) as f64).exp())
3364                .sum::<f64>()
3365                .ln()
3366                + max as f64;
3367            let tok_nll = lse - logits[target] as f64;
3368            if std::env::var("CMF_PPL_TRACE").is_ok() && pos < 48 {
3369                let top = logits
3370                    .iter()
3371                    .enumerate()
3372                    .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
3373                    .map(|(i, _)| i)
3374                    .unwrap_or(0);
3375                eprintln!(
3376                    "pos {pos:3} tgt {target:6} nll {tok_nll:7.3} | top1 {top:6} lg[t]={:.2} lg[top]={:.2}",
3377                    logits[target], logits[top]
3378                );
3379            }
3380            nll += tok_nll;
3381            cnt += 1;
3382        }
3383        self.kv_cache.clear();
3384        self.kv_history.clear();
3385        (nll, cnt)
3386    }
3387
3388    /// Teacher-forced calibration data (B1): for each position, whether the
3389    /// argmax equals the actual next token, and the top-1 softmax prob
3390    /// (Born mass) under EACH temperature in `temps` — all from ONE forward
3391    /// pass (argmax/correctness are temperature-invariant; only p_max
3392    /// reshapes). Feeds `cortiq calibrate` (reliability/ECE + temperature
3393    /// fit): is the model's confidence a true property, or does it need a
3394    /// measured scaling?
3395    pub fn calib_ids(&mut self, ids: &[u32], temps: &[f32]) -> (Vec<bool>, Vec<Vec<f32>>) {
3396        self.kv_cache.clear();
3397        self.kv_history.clear();
3398        let n = ids.len().saturating_sub(1);
3399        let mut correct = Vec::with_capacity(n);
3400        let mut pmax = Vec::with_capacity(n);
3401        for pos in 0..n {
3402            let emb = self.embed_single(ids[pos]);
3403            let hidden = self.forward_layers(&emb, pos, None);
3404            let normed = inference::rms_norm(
3405                &hidden,
3406                &self.weights.final_norm,
3407                self.rms_eps,
3408                self.norm_style,
3409            );
3410            // lm_head_forward applies the final-logit softcap itself —
3411            // capping again here double-squashed gemma-class logits
3412            // (tanh∘tanh) and reported a flattered ppl.
3413            let logits = self.lm_head_forward(&normed);
3414            let target = ids[pos + 1] as usize;
3415            let (mut amax, mut mval) = (0usize, f32::NEG_INFINITY);
3416            for (i, &v) in logits.iter().enumerate() {
3417                if v > mval {
3418                    mval = v;
3419                    amax = i;
3420                }
3421            }
3422            correct.push(amax == target);
3423            let row: Vec<f32> = temps
3424                .iter()
3425                .map(|&t| {
3426                    let tt = t.max(1e-3);
3427                    let s: f32 = logits.iter().map(|&v| ((v - mval) / tt).exp()).sum();
3428                    1.0 / s.max(1e-12) // numerator at the max is exp(0)=1
3429                })
3430                .collect();
3431            pmax.push(row);
3432        }
3433        self.kv_cache.clear();
3434        self.kv_history.clear();
3435        (correct, pmax)
3436    }
3437
3438    /// Teacher-forced PPL with the dynamic router driving per-window
3439    /// skill switches (VMF experiment №2 measurement). Sequential (φ
3440    /// must update per token), returns (ppl, switch_count). The router
3441    /// must be enabled (`enable_dynamic_routing`); else this equals
3442    /// plain `ppl_ids`. The active skill when scoring token t shapes the
3443    /// logits for t+1 — on-policy over the held-out text itself.
3444    pub fn ppl_ids_dynamic(&mut self, ids: &[u32]) -> (f64, usize) {
3445        let mut router = match self.dyn_router.take() {
3446            Some(r) => r,
3447            None => return (self.ppl_ids(ids), 0),
3448        };
3449        router.reset();
3450        self.dyn_phi_seen = 0;
3451        let _ = self.set_active_skill(None);
3452
3453        self.kv_cache.clear();
3454
3455        self.kv_history.clear();
3456        let mut nll = 0f64;
3457        let mut cnt = 0usize;
3458        for pos in 0..ids.len().saturating_sub(1) {
3459            let hidden = self.forward_layers(&self.embed_single(ids[pos]), pos, None);
3460            let normed = inference::rms_norm(
3461                &hidden,
3462                &self.weights.final_norm,
3463                self.rms_eps,
3464                self.norm_style,
3465            );
3466            // lm_head_forward applies the final-logit softcap itself —
3467            // capping again here double-squashed gemma-class logits
3468            // (tanh∘tanh) and reported a flattered ppl.
3469            let logits = self.lm_head_forward(&normed);
3470            let target = ids[pos + 1] as usize;
3471            let max = logits.iter().fold(f32::NEG_INFINITY, |m, &v| m.max(v));
3472            let lse: f64 = logits
3473                .iter()
3474                .map(|&v| ((v - max) as f64).exp())
3475                .sum::<f64>()
3476                .ln()
3477                + max as f64;
3478            let tok_nll = lse - logits[target] as f64;
3479            if std::env::var("CMF_PPL_TRACE").is_ok() && pos < 48 {
3480                let top = logits
3481                    .iter()
3482                    .enumerate()
3483                    .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
3484                    .map(|(i, _)| i)
3485                    .unwrap_or(0);
3486                eprintln!(
3487                    "pos {pos:3} tgt {target:6} nll {tok_nll:7.3} | top1 {top:6} lg[t]={:.2} lg[top]={:.2}",
3488                    logits[target], logits[top]
3489                );
3490            }
3491            nll += tok_nll;
3492            cnt += 1;
3493            // Route on the evolving φ (drives the NEXT token's skill).
3494            let phi = self.dyn_phi_ema.clone();
3495            if let Some(new_active) = router.step(&phi, pos) {
3496                let _ = self.set_active_skill(new_active);
3497            }
3498        }
3499        let switches = router.switches.len();
3500        let _ = self.set_active_skill(None);
3501        self.dyn_router = Some(router);
3502        self.kv_cache.clear();
3503        self.kv_history.clear();
3504        ((nll / cnt.max(1) as f64).exp(), switches)
3505    }
3506
3507    /// Routing probe φ (spec §9): mean-pooled hidden after `layer`.
3508    pub fn probe_phi(&mut self, ids: &[u32], layer: usize) -> Vec<f32> {
3509        self.kv_cache.clear();
3510        self.kv_history.clear();
3511        let mut acc = vec![0f32; self.hidden_size];
3512        for (pos, &id) in ids.iter().enumerate() {
3513            let h = self.forward_layers_upto(&self.embed_single(id), pos, None, Some(layer));
3514            for (a, v) in acc.iter_mut().zip(&h) {
3515                *a += v;
3516            }
3517        }
3518        let n = ids.len().max(1) as f32;
3519        for a in acc.iter_mut() {
3520            *a /= n;
3521        }
3522        self.kv_cache.clear();
3523        self.kv_history.clear();
3524        acc
3525    }
3526
3527    /// Layer-major batched prefill (prefill-GEMM): full-attention —
3528    /// per-position with the existing operators (KV grows naturally,
3529    /// causality preserved), GDN projections / FFN / MoE — batched
3530    /// (a weight row is read from DRAM once per chunk, not per
3531    /// position). Returns the hidden of all positions [b × hidden].
3532    fn prefill_batch(&mut self, ids: &[u32], start_pos: usize) -> Vec<f32> {
3533        self.prefill_batch_masked(ids, start_pos, None)
3534    }
3535
3536    /// `prefill_batch` with a task mask honored on the dense-FFN panels
3537    /// (the masked-inference fast path: full fused compute, mask lands on
3538    /// the activations). The whole-chunk GPU graph is skipped for masked
3539    /// layers by the callers' arms; the per-GEMM device paths stay in
3540    /// play because the zeroing happens on the host between them.
3541    fn prefill_batch_masked(
3542        &mut self,
3543        ids: &[u32],
3544        start_pos: usize,
3545        task_mask: Option<&TaskMask>,
3546    ) -> Vec<f32> {
3547        self.prefill_batch_span(PrefillIn::Ids(ids), start_pos, task_mask, 0, usize::MAX)
3548    }
3549
3550    /// The layer-major batched walk over a layer span [from..upto_excl):
3551    /// the whole prefill machinery (chunk graph, batched attends, GEMM
3552    /// panels) for a PARTIAL stack — the network split's prefill rides
3553    /// the same canon as the local one. Input is token ids (embeds
3554    /// itself, coordinator side) or ready boundary hiddens (worker side).
3555    fn prefill_batch_span(
3556        &mut self,
3557        input: PrefillIn<'_>,
3558        start_pos: usize,
3559        task_mask: Option<&TaskMask>,
3560        from: usize,
3561        upto_excl: usize,
3562    ) -> Vec<f32> {
3563        let hs = self.hidden_size;
3564        let b = match input {
3565            PrefillIn::Ids(ids) => ids.len(),
3566            PrefillIn::Hidden(hb) => hb.len() / hs,
3567        };
3568        let upto_excl = upto_excl.min(self.num_layers);
3569        // The CPU embed is deferred: when the chunk graph takes the run
3570        // from layer 0 it gathers the embeddings on the device instead.
3571        // A hidden input is ready by definition.
3572        let mut h: Vec<f32>;
3573        let mut h_ready;
3574        match input {
3575            PrefillIn::Ids(_) => {
3576                h = vec![0.0; b * hs];
3577                h_ready = false;
3578            }
3579            PrefillIn::Hidden(hb) => {
3580                h = hb.to_vec();
3581                h_ready = true;
3582            }
3583        }
3584        let fill_h = |h: &mut Vec<f32>, me: &Self| {
3585            if let PrefillIn::Ids(ids) = input {
3586                for (bi, &id) in ids.iter().enumerate() {
3587                    let e = me.embed_single(id);
3588                    h[bi * hs..(bi + 1) * hs].copy_from_slice(&e);
3589                }
3590            }
3591        };
3592        let (_nkv, _hd, _rd, eps) = (
3593            self.num_kv_heads,
3594            self.head_dim,
3595            self.rotary_dim,
3596            self.rms_eps,
3597        );
3598        let pool = self.pool.clone();
3599        let norm_style = self.norm_style;
3600
3601        #[cfg(target_os = "macos")]
3602        let mut chunk_skip_until = 0usize;
3603        for li in from..upto_excl {
3604            crate::gpu::set_layer(li as i64); // layer-split GPU/CPU
3605            // GPU chunk graph (default-on under CMF_GPU=1): a run of
3606            // consecutive eligible layers for the whole chunk in ONE
3607            // Metal submission — norm, QKV, RoPE with fused mirror
3608            // append, causal attend, O, FFN, hidden device-resident
3609            // across the run. Any refusal falls through to the CPU path.
3610            #[cfg(target_os = "macos")]
3611            if task_mask.is_none() {
3612                if li < chunk_skip_until {
3613                    continue;
3614                }
3615                // Device-side embedding needs a q8_row embedding matrix;
3616                // with any other layout the CPU fills `h` first and the
3617                // graph starts from a ready hidden (refusing the whole
3618                // run over the embedding alone kept q4t models — the
3619                // whole Nanbeige/Bonsai class — on the CPU prefill).
3620                if !h_ready && li == 0 && self.weights.embed_tokens.q8_row_parts().is_none() {
3621                    fill_h(&mut h, self);
3622                    h_ready = true;
3623                }
3624                let ids_for_embed = match input {
3625                    PrefillIn::Ids(ids) => (!h_ready && li == 0).then_some(ids),
3626                    PrefillIn::Hidden(_) => None,
3627                };
3628                let end = self.chunk_run_gpu(li, &mut h, b, start_pos, ids_for_embed, upto_excl);
3629                if end > li {
3630                    h_ready = true;
3631                    chunk_skip_until = end;
3632                    // Looped Transformer: the graph stopped at a loop
3633                    // boundary — apply final norm before the next iteration.
3634                    if self.is_loop_end(end - 1) && end < self.num_layers {
3635                        for bi in 0..b {
3636                            let normed = inference::rms_norm(
3637                                &h[bi * hs..(bi + 1) * hs],
3638                                &self.weights.final_norm,
3639                                eps,
3640                                norm_style,
3641                            );
3642                            h[bi * hs..(bi + 1) * hs].copy_from_slice(&normed);
3643                        }
3644                    }
3645                    continue;
3646                }
3647            }
3648            if !h_ready {
3649                fill_h(&mut h, self);
3650                h_ready = true;
3651            }
3652            let lw = &self.weights.layers[self.phys_layer(li)];
3653            // ── attention ──
3654            match &lw.attn {
3655                AttnKind::Kda(w) => {
3656                    // Projections batched, recurrence sequential.
3657                    let cfg = self.kda_cfg.expect("kda layer without kda_cfg");
3658                    let mut normed = vec![0.0f32; b * hs];
3659                    for bi in 0..b {
3660                        inference::rms_norm_into(
3661                            &h[bi * hs..(bi + 1) * hs],
3662                            &lw.input_norm,
3663                            eps,
3664                            norm_style,
3665                            &mut normed[bi * hs..(bi + 1) * hs],
3666                        );
3667                    }
3668                    let attn = crate::linear_core::kda_forward_batch(
3669                        &normed,
3670                        b,
3671                        w,
3672                        &cfg,
3673                        &mut self.kv_cache.layers[li].linear_state,
3674                        pool.as_deref(),
3675                    );
3676                    for (dst, &a) in h.iter_mut().zip(&attn) {
3677                        *dst += a;
3678                    }
3679                }
3680                AttnKind::LinearGdn(w) => {
3681                    // Projections batched, recurrence sequential.
3682                    let cfg = self.gdn_cfg.expect("gdn layer without gdn_cfg");
3683                    let mut normed = vec![0.0f32; b * hs];
3684                    for bi in 0..b {
3685                        let r = inference::rms_norm(
3686                            &h[bi * hs..(bi + 1) * hs],
3687                            &lw.input_norm,
3688                            eps,
3689                            norm_style,
3690                        );
3691                        normed[bi * hs..(bi + 1) * hs].copy_from_slice(&r);
3692                    }
3693                    let attn = crate::linear_core::gdn_forward_batch(
3694                        &normed,
3695                        b,
3696                        w,
3697                        &cfg,
3698                        &mut self.kv_cache.layers[li].linear_state,
3699                        pool.as_deref(),
3700                    );
3701                    for (dst, &a) in h.iter_mut().zip(&attn) {
3702                        *dst += a;
3703                    }
3704                }
3705                AttnKind::ShortConv(w) => {
3706                    // Projections batched over the chunk; the conv walks the
3707                    // contiguous positions in order (same ring as decode).
3708                    let cfg = self
3709                        .short_conv_cfg
3710                        .expect("short-conv layer without short_conv_cfg");
3711                    let mut normed = vec![0.0f32; b * hs];
3712                    for bi in 0..b {
3713                        inference::rms_norm_into(
3714                            &h[bi * hs..(bi + 1) * hs],
3715                            &lw.input_norm,
3716                            eps,
3717                            norm_style,
3718                            &mut normed[bi * hs..(bi + 1) * hs],
3719                        );
3720                    }
3721                    let attn = short_conv_forward_batch(
3722                        &normed,
3723                        b,
3724                        w,
3725                        &cfg,
3726                        &mut self.kv_cache.layers[li].linear_state,
3727                        pool.as_deref(),
3728                    );
3729                    for (dst, &a) in h.iter_mut().zip(&attn) {
3730                        *dst += a;
3731                    }
3732                }
3733                AttnKind::Mla(w) => {
3734                    // Per-position prefill (correctness first; latent
3735                    // batching is a later optimization).
3736                    let inv_freq_l = self.layer_inv_freq(li);
3737                    let rs = self.layer_rope_scale(li);
3738                    let mut normed = vec![0.0f32; hs];
3739                    for bi in 0..b {
3740                        inference::rms_norm_into(
3741                            &h[bi * hs..(bi + 1) * hs],
3742                            &lw.input_norm,
3743                            eps,
3744                            norm_style,
3745                            &mut normed,
3746                        );
3747                        let ao = mla_attention(
3748                            w,
3749                            &normed,
3750                            &mut self.kv_cache.layers[li],
3751                            start_pos + bi,
3752                            &inv_freq_l,
3753                            rs,
3754                            eps,
3755                            pool.as_deref(),
3756                        );
3757                        for (dst, &a) in h[bi * hs..(bi + 1) * hs].iter_mut().zip(&ao) {
3758                            *dst += a;
3759                        }
3760                    }
3761                }
3762                AttnKind::Full {
3763                    wq,
3764                    wk,
3765                    wv,
3766                    wo,
3767                    q_norm,
3768                    k_norm,
3769                    output_gate,
3770                    softplus_gate,
3771                    bias,
3772                } => {
3773                    // Chunk-GEMM QKV/O; per-position causal attention
3774                    // inside (roadmap §3 P0 — full-attention prefill no
3775                    // longer re-reads the projection weights b times).
3776                    let mut normed = vec![0.0f32; b * hs];
3777                    for bi in 0..b {
3778                        inference::rms_norm_into(
3779                            &h[bi * hs..(bi + 1) * hs],
3780                            &lw.input_norm,
3781                            eps,
3782                            norm_style,
3783                            &mut normed[bi * hs..(bi + 1) * hs],
3784                        );
3785                    }
3786                    let inv_freq_l = self.layer_inv_freq(li);
3787                    let (nkv_l, hd_l, rd_l) = self.layer_geom(li);
3788                    let cfg = QwenAttnCfg {
3789                        num_heads: self.layer_num_heads(li),
3790                        num_kv_heads: nkv_l,
3791                        head_dim: hd_l,
3792                        hidden_size: hs,
3793                        position: start_pos,
3794                        inv_freq: &inv_freq_l,
3795                        rotary_dim: rd_l,
3796                        scale: self.attn_scale,
3797                        softcap: self.attn_softcap,
3798                        window: self.layer_window(li),
3799                        v_norm: self.attn_v_norm,
3800                        q_norm: q_norm.as_deref(),
3801                        k_norm: k_norm.as_deref(),
3802                        output_gate: *output_gate,
3803                        softplus_gate: softplus_gate
3804                            .as_ref()
3805                            .map(|(gate, per_head)| (gate, *per_head)),
3806                        rope_scale: self.layer_rope_scale(li),
3807                        bias: bias
3808                            .as_ref()
3809                            .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
3810                        rms_eps: eps,
3811                        norm_style,
3812                        pool: pool.as_deref(),
3813                    };
3814                    let mut attn = attention::qwen_attention_batch(
3815                        &normed,
3816                        b,
3817                        wq,
3818                        wk,
3819                        wv,
3820                        wo,
3821                        &mut self.kv_cache.layers[li],
3822                        &cfg,
3823                    );
3824                    if let Some(w) = &lw.attn_out_norm {
3825                        for bi in 0..b {
3826                            inference::rms_norm_into(
3827                                &attn[bi * hs..(bi + 1) * hs],
3828                                w,
3829                                eps,
3830                                norm_style,
3831                                &mut normed[bi * hs..(bi + 1) * hs],
3832                            );
3833                        }
3834                        attn.copy_from_slice(&normed);
3835                    }
3836                    for (dst, &a) in h.iter_mut().zip(&attn) {
3837                        *dst += a;
3838                    }
3839                }
3840                AttnKind::Linear(w) => {
3841                    for bi in 0..b {
3842                        let normed = inference::rms_norm(
3843                            &h[bi * hs..(bi + 1) * hs],
3844                            &lw.input_norm,
3845                            eps,
3846                            norm_style,
3847                        );
3848                        vmf_phase_forward(
3849                            &normed,
3850                            w,
3851                            &self.vmf_cfg.expect("linear layer without vmf_cfg"),
3852                            &mut self.kv_cache.layers[li].linear_state,
3853                            pool.as_deref(),
3854                        )
3855                        .iter()
3856                        .enumerate()
3857                        .for_each(|(i, &a)| h[bi * hs + i] += a);
3858                    }
3859                }
3860            }
3861
3862            // ── FFN batched ──
3863            let lw = &self.weights.layers[self.phys_layer(li)];
3864            let mut post = vec![0.0f32; b * hs];
3865            for bi in 0..b {
3866                let r =
3867                    inference::rms_norm(&h[bi * hs..(bi + 1) * hs], &lw.post_norm, eps, norm_style);
3868                post[bi * hs..(bi + 1) * hs].copy_from_slice(&r);
3869            }
3870            // A restrictive per-visit FFN row lands on the activations
3871            // inside the dense arm; an all-open row costs nothing.
3872            let mask_row = task_mask
3873                .filter(|m| m.ffn_active_count(li) < self.intermediate_size)
3874                .and_then(|m| m.ffn_masks.get(li))
3875                .map(|v| v.as_slice());
3876            let mut ffn = match &lw.ffn {
3877                FfnKind::Dense(d) => dense_ffn_batch(d, &post, b, pool.as_deref(), mask_row),
3878                FfnKind::Moe(m) => moe_ffn_batch(m, &post, b, hs, pool.as_deref(), None),
3879                // Dual-branch layers run per position (the expert branch
3880                // reads the raw residual — nothing to batch yet).
3881                FfnKind::DenseMoe(dm) => {
3882                    let mut out = vec![0.0f32; b * hs];
3883                    for bi in 0..b {
3884                        let r = dense_moe_ffn(
3885                            dm,
3886                            &post[bi * hs..(bi + 1) * hs],
3887                            &h[bi * hs..(bi + 1) * hs],
3888                            eps,
3889                            norm_style,
3890                            pool.as_deref(),
3891                        );
3892                        out[bi * hs..(bi + 1) * hs].copy_from_slice(&r);
3893                    }
3894                    out
3895                }
3896            };
3897            if let Some(w) = &lw.ffn_out_norm {
3898                for bi in 0..b {
3899                    inference::rms_norm_into(
3900                        &ffn[bi * hs..(bi + 1) * hs],
3901                        w,
3902                        eps,
3903                        norm_style,
3904                        &mut post[bi * hs..(bi + 1) * hs],
3905                    );
3906                }
3907                ffn.copy_from_slice(&post);
3908            }
3909            for (dst, &f) in h.iter_mut().zip(&ffn) {
3910                *dst += f;
3911            }
3912            if let Some(sc) = lw.layer_scale {
3913                for v in h.iter_mut() {
3914                    *v *= sc;
3915                }
3916            }
3917            if let Ok(tp) = std::env::var("CMF_TRACE_POS") {
3918                if let Some(t) = tp.parse::<usize>().ok() {
3919                    if t >= start_pos && t < start_pos + b {
3920                        let bi = t - start_pos;
3921                        let row = &h[bi * hs..(bi + 1) * hs];
3922                        let n: f32 = row.iter().map(|x| x * x).sum::<f32>().sqrt();
3923                        eprintln!(
3924                            "BATCH pos {t} after layer {li}: |h| = {n:.6} h0 {:.6} h1 {:.6}",
3925                            row[0], row[1]
3926                        );
3927                    }
3928                }
3929            }
3930            // CMF_DEBUG_LAYERS=1: per-layer hidden-state health of the
3931            // LAST prompt position — the knife for "which layer type
3932            // breaks first" on a new architecture.
3933            if std::env::var("CMF_DEBUG_LAYERS").is_ok() {
3934                let row = &h[(b - 1) * hs..b * hs];
3935                let rms =
3936                    (row.iter().map(|&v| (v as f64) * (v as f64)).sum::<f64>() / hs as f64).sqrt();
3937                let mx = row.iter().fold(0f32, |m, &v| m.max(v.abs()));
3938                eprintln!(
3939                    "layer {li:>3} {:>10} ffn={:<5} rms={rms:>12.4} max={mx:>12.4}",
3940                    match &self.weights.layers[self.phys_layer(li)].attn {
3941                        AttnKind::LinearGdn(_) => "gdn",
3942                        AttnKind::Linear(_) => "vmf",
3943                        AttnKind::ShortConv(_) => "conv",
3944                        _ => "attn",
3945                    },
3946                    match &lw.ffn {
3947                        FfnKind::Moe(_) => "moe",
3948                        FfnKind::Dense(_) => "dense",
3949                        FfnKind::DenseMoe(_) => "dense+moe",
3950                    },
3951                );
3952            }
3953            // Looped Transformer: apply final norm at the end of each loop iteration.
3954            if self.is_loop_end(li) && li + 1 < self.num_layers {
3955                for bi in 0..b {
3956                    let normed = inference::rms_norm(
3957                        &h[bi * hs..(bi + 1) * hs],
3958                        &self.weights.final_norm,
3959                        eps,
3960                        norm_style,
3961                    );
3962                    h[bi * hs..(bi + 1) * hs].copy_from_slice(&normed);
3963                }
3964            }
3965            if std::env::var("CMF_TRACE_H").is_ok() {
3966                let n = h[..hs].iter().map(|v| v.abs()).sum::<f32>() / hs as f32;
3967                let mx = h[..hs].iter().fold(0.0f32, |a, &v| a.max(v.abs()));
3968                eprintln!(
3969                    "layer {li}: mean|h|={n:.4} max|h|={mx:.2} scale={:?}",
3970                    lw.layer_scale
3971                );
3972            }
3973        }
3974        crate::gpu::set_layer(-1); // lm_head/final ops outside layer-split
3975        h
3976    }
3977
3978    /// Embed a single token.
3979    fn embed_single(&self, id: u32) -> Vec<f32> {
3980        let mut out = vec![0.0f32; self.hidden_size];
3981        if (id as usize) < self.weights.embed_tokens.rows() {
3982            self.weights.embed_tokens.row_f32(id as usize, &mut out);
3983        }
3984        if self.embed_multiplier != 1.0 {
3985            for v in out.iter_mut() {
3986                *v *= self.embed_multiplier;
3987            }
3988        }
3989        // DeepSeek-V4's hash layers route by TOKEN ID, so the id has to
3990        // reach the forward. It rides in slot 0 (the forward re-reads the
3991        // real embedding itself from the table).
3992        if self.dsv4.is_some() {
3993            let mut v = vec![0.0f32; self.hidden_size.max(1)];
3994            v[0] = id as f32;
3995            return v;
3996        }
3997        // Gemma-3n: the per-layer-embedding half needs the token ID, so
3998        // it rides appended to the embedding; the g3n forward splits it.
3999        if let Some(b) = &self.g3n {
4000            return b.0.extend_embedding(id, &out, self.pool.as_deref());
4001        }
4002        out
4003    }
4004
4005    /// A run of consecutive prefill layers on the GPU for the whole
4006    /// chunk (default-on under CMF_GPU=1; CMF_GPU_CHUNK=0 disables).
4007    /// Eligibility per layer: q8_row weights, plain full attention
4008    /// (no output gate), F32 KV, no o1/masks/gemma extras. Returns the
4009    /// first layer index NOT processed (== `li0` when the run is empty).
4010    #[cfg(target_os = "macos")]
4011    fn chunk_run_gpu(
4012        &mut self,
4013        li0: usize,
4014        h: &mut [f32],
4015        b: usize,
4016        pos0: usize,
4017        embed_ids: Option<&[u32]>,
4018        cap: usize,
4019    ) -> usize {
4020        // (The old streaming attend needed a depth bound at ~1k; the
4021        // GEMM attention scales like the CPU path and lifted it.)
4022        // CMF_GPU_CHUNK=0 disables the graph.
4023        if !crate::gpu::enabled_here()
4024            || std::env::var("CMF_GPU_CHUNK")
4025                .map(|v| v == "0")
4026                .unwrap_or(false)
4027            || b < 32
4028            || self.swa.is_some()
4029            || self.global_attn.is_some()
4030            || self.attn_v_norm
4031            || (self.attn_scale - 1.0 / (self.head_dim as f32).sqrt()).abs() > 1e-9
4032        {
4033            return li0;
4034        }
4035        let Some(model) = self.model.clone() else {
4036            return li0;
4037        };
4038        let inv_freq = self.inv_freq.clone();
4039        let (nh, nkv, hd, hs) = (
4040            self.num_heads,
4041            self.num_kv_heads,
4042            self.head_dim,
4043            self.hidden_size,
4044        );
4045        // Collect the longest run of consecutive eligible layers.
4046        // Looped Transformer: stop at the loop boundary so the CPU can
4047        // apply loop_final_norm between iterations.
4048        let loop_end = if self.loop_final_norm {
4049            ((li0 / self.physical_layers) + 1) * self.physical_layers
4050        } else {
4051            self.num_layers
4052        };
4053        let mut layers: Vec<crate::gpu_metal::ChunkLayer> = Vec::new();
4054        let mut stored_at: Vec<usize> = Vec::new();
4055        for li in li0..self.num_layers.min(loop_end).min(cap) {
4056            let lw = &self.weights.layers[self.phys_layer(li)];
4057            if lw.attn_out_norm.is_some() || lw.ffn_out_norm.is_some() || lw.layer_scale.is_some() {
4058                break;
4059            }
4060            let AttnKind::Full {
4061                wq,
4062                wk,
4063                wv,
4064                wo,
4065                q_norm,
4066                k_norm,
4067                output_gate: false,
4068                softplus_gate: None,
4069                bias,
4070            } = &lw.attn
4071            else {
4072                break;
4073            };
4074            let FfnKind::Dense(d) = &lw.ffn else { break };
4075            if d.act != Act::Silu {
4076                break;
4077            }
4078            // q8_row (row_scale populated), or q4_tiled / q4tp (row_scale
4079            // empty — their scales are in the payload). Mixing across the
4080            // seven projections of one layer is fine; the encoder branches
4081            // per weight on the tensor's dtype. Anything else refuses.
4082            fn cw(t: &QTensor) -> Option<(usize, usize, usize, &[f32])> {
4083                t.q8_row_parts()
4084                    .or_else(|| t.q4t_parts().map(|(i, r, c)| (i, r, c, &[][..])))
4085                    .or_else(|| t.q4tp_parts().map(|(i, r, c)| (i, r, c, &[][..])))
4086            }
4087            let parts = (
4088                cw(wq),
4089                cw(wk),
4090                cw(wv),
4091                cw(wo),
4092                cw(&d.gate_proj),
4093                cw(&d.up_proj),
4094                cw(&d.down_proj),
4095            );
4096            let (Some(pq), Some(pk), Some(pv), Some(po), Some(pg), Some(pu), Some(pd)) = parts
4097            else {
4098                break;
4099            };
4100            let layer = &self.kv_cache.layers[li];
4101            if layer.mode != crate::kv_cache::KvMode::F32 || layer.o1.is_some() {
4102                break;
4103            }
4104            stored_at.push(layer.head_len(0));
4105            layers.push(crate::gpu_metal::ChunkLayer {
4106                model: &model,
4107                kv_id: self.graph_kv_id,
4108                layer: li,
4109                wq: pq,
4110                wk: pk,
4111                wv: pv,
4112                wo: po,
4113                gate: pg,
4114                up: pu,
4115                down: pd,
4116                input_norm: &lw.input_norm,
4117                post_norm: &lw.post_norm,
4118                bias: bias
4119                    .as_ref()
4120                    .map(|(a, bb, cc)| (a.as_slice(), bb.as_slice(), cc.as_slice())),
4121                q_norm: q_norm.as_deref(),
4122                k_norm: k_norm.as_deref(),
4123                inv_freq: &inv_freq,
4124                rd: self.rotary_dim,
4125                nh,
4126                nkv,
4127                hd,
4128                hs,
4129                inter: d.gate_proj.rows(),
4130                gemma: matches!(self.norm_style, cortiq_core::NormStyle::Gemma),
4131                eps: self.rms_eps as f32,
4132            });
4133        }
4134        if layers.is_empty() {
4135            return li0;
4136        }
4137        let row = nkv * hd;
4138        let mut store: Vec<(Vec<f32>, Vec<f32>, Vec<f32>)> = stored_at
4139            .iter()
4140            .map(|&st| (vec![0f32; b * row], vec![0f32; b * row], vec![0f32; st + b]))
4141            .collect();
4142        let mut io: Vec<crate::gpu_metal::ChunkIo> = Vec::with_capacity(layers.len());
4143        for (i, (ok, ov, oi)) in store.iter_mut().enumerate() {
4144            let li = layers[i].layer;
4145            let layer = &self.kv_cache.layers[li];
4146            io.push(crate::gpu_metal::ChunkIo {
4147                cpu_stored: stored_at[i],
4148                cpu_k: (0..nkv).map(|g| layer.head_keys(g)).collect(),
4149                cpu_v: (0..nkv).map(|g| layer.head_values(g)).collect(),
4150                out_k: ok,
4151                out_v: ov,
4152                imp: oi,
4153            });
4154        }
4155        let n_run = layers.len();
4156        let last = layers.last().map(|l| l.layer + 1).unwrap_or(li0);
4157        // Device-side embedding when the run starts the model and the
4158        // embedding matrix is q8_row-mapped.
4159        let ep = embed_ids.and_then(|ids| {
4160            self.weights
4161                .embed_tokens
4162                .q8_row_parts()
4163                .map(|(idx, rows, _c, rs)| crate::gpu_metal::ChunkEmbed {
4164                    idx,
4165                    rows,
4166                    row_scale: rs,
4167                    ids,
4168                    mult: self.embed_multiplier,
4169                })
4170        });
4171        if embed_ids.is_some() && ep.is_none() {
4172            return li0;
4173        }
4174        if !crate::gpu_metal::chunk_run_gpu(&layers, &mut io, h, b, pos0, ep.as_ref()) {
4175            return li0;
4176        }
4177        drop(io);
4178        drop(layers);
4179        // CPU caches stay the owners of record: append the chunk rows
4180        // and bank the importance masses per layer.
4181        for (i, (ok, ov, oi)) in store.iter().enumerate().take(n_run) {
4182            let li = li0 + i;
4183            let layer = &mut self.kv_cache.layers[li];
4184            for bi in 0..b {
4185                layer.append(
4186                    &ok[bi * row..(bi + 1) * row],
4187                    &ov[bi * row..(bi + 1) * row],
4188                    &[],
4189                );
4190            }
4191            layer.accumulate_imp(oi);
4192        }
4193        last
4194    }
4195
4196    /// Is layer `li` a sliding-window (local-RoPE) layer? Gemma-3:
4197    /// every `pattern`-th layer is global, the rest are local.
4198    fn layer_is_local(&self, li: usize) -> bool {
4199        if let Some(layers) = &self.sliding_layers {
4200            return layers.get(li).copied().unwrap_or(false);
4201        }
4202        match self.swa {
4203            Some((_, pattern)) => (li + 1) % pattern.max(1) != 0,
4204            None => false,
4205        }
4206    }
4207
4208    /// The RoPE table for layer `li` (local layers may have their own;
4209    /// Gemma-4 global layers use the proportional padded table).
4210    fn layer_inv_freq(&self, li: usize) -> std::sync::Arc<Vec<f32>> {
4211        if self.layer_is_local(li) {
4212            if let Some(f) = &self.inv_freq_local {
4213                return f.clone();
4214            }
4215        } else if let Some(f) = &self.inv_freq_global {
4216            return f.clone();
4217        }
4218        self.inv_freq.clone()
4219    }
4220
4221    /// The attend window for layer `li` (None = full context).
4222    fn layer_window(&self, li: usize) -> Option<usize> {
4223        self.swa
4224            .and_then(|(w, _)| self.layer_is_local(li).then_some(w))
4225    }
4226
4227    fn layer_num_heads(&self, li: usize) -> usize {
4228        self.attention_heads_per_layer
4229            .as_ref()
4230            .and_then(|v| v.get(li).copied())
4231            .unwrap_or(self.num_heads)
4232    }
4233
4234    fn layer_rope_scale(&self, li: usize) -> f32 {
4235        if self.layer_is_local(li) {
4236            self.rope_scale_local
4237        } else {
4238            self.rope_scale
4239        }
4240    }
4241
4242    /// Attention geometry of layer `li`: (num_kv_heads, head_dim,
4243    /// rotary_dim). Gemma-4 global layers override all three.
4244    fn layer_geom(&self, li: usize) -> (usize, usize, usize) {
4245        if !self.layer_is_local(li) {
4246            if let Some((ghd, gkv)) = self.global_attn {
4247                return (gkv, ghd, ghd);
4248            }
4249        }
4250        (
4251            self.num_kv_heads,
4252            self.head_dim,
4253            if self.layer_is_local(li) {
4254                self.rotary_dim_local.unwrap_or(self.rotary_dim)
4255            } else {
4256                self.rotary_dim
4257            },
4258        )
4259    }
4260
4261    /// Forward one position through all layers (hybrid dispatch).
4262    fn forward_layers(
4263        &mut self,
4264        hidden: &[f32],
4265        position: usize,
4266        task_mask: Option<&TaskMask>,
4267    ) -> Vec<f32> {
4268        self.forward_layers_upto(hidden, position, task_mask, None)
4269    }
4270
4271    // ── Network pipeline-split building blocks (coordinator/worker) ──
4272    // A remote worker owns layers [from ..= upto] and their KV; the
4273    // coordinator owns the rest plus embed / final norm / head. Attention
4274    // causality is per-layer, so a whole prompt's boundary hiddens ship
4275    // as one batch and decode ships one vector per token.
4276
4277    /// Embed one token id (embed multiplier applied).
4278    pub fn embed_id(&self, id: u32) -> Vec<f32> {
4279        self.embed_single(id)
4280    }
4281
4282    /// Refuse the archs/modes whose forward cannot be cut at a layer
4283    /// boundary. Loud by design: a split that silently changed the math
4284    /// would be a chimera.
4285    pub fn split_supported(&self) -> Result<(), String> {
4286        if self.dsv4.is_some() {
4287            return Err(
4288                "network split: DeepSeek-V4 runs its own fused stack (not splittable yet)".into(),
4289            );
4290        }
4291        if self.g3n.is_some() {
4292            return Err(
4293                "network split: Gemma-3n runs its own AltUp stack (not splittable yet)".into(),
4294            );
4295        }
4296        Ok(())
4297    }
4298
4299    /// Forward `hidden` through layers [from ..= upto] at `position`,
4300    /// appending those layers' KV/state. Both split sides call this
4301    /// over their own range; a task mask applies to the span's own
4302    /// layers (each side masks what it runs).
4303    pub fn forward_span(
4304        &mut self,
4305        hidden: &[f32],
4306        position: usize,
4307        from: usize,
4308        upto: usize,
4309        task_mask: Option<&TaskMask>,
4310    ) -> Result<Vec<f32>, String> {
4311        self.split_supported()?;
4312        if from > upto || upto >= self.num_layers {
4313            return Err(format!(
4314                "forward_span: layer range {from}..={upto} outside 0..{}",
4315                self.num_layers
4316            ));
4317        }
4318        if hidden.len() != self.hidden_size {
4319            return Err(format!(
4320                "forward_span: hidden len {} ≠ hidden_size {}",
4321                hidden.len(),
4322                self.hidden_size
4323            ));
4324        }
4325        Ok(self.forward_layers_span(hidden, position, task_mask, from, Some(upto)))
4326    }
4327
4328    /// Final norm + lm_head over a boundary hidden (the final-logit
4329    /// softcap is applied by lm_head_forward itself).
4330    pub fn logits_from_hidden(&mut self, hidden: &[f32]) -> Vec<f32> {
4331        let normed = inference::rms_norm(
4332            hidden,
4333            &self.weights.final_norm,
4334            self.rms_eps,
4335            self.norm_style,
4336        );
4337        self.lm_head_forward(&normed)
4338    }
4339
4340    /// Sample the next token with this pipeline's sampler state.
4341    pub fn sample_next(&mut self, logits: &[f32], past_tokens: &[u32]) -> u32 {
4342        sampler::sample_with_scratch(
4343            logits,
4344            &self.sampler_config,
4345            past_tokens,
4346            &mut self.rng,
4347            &mut self.sampler_scratch,
4348        )
4349    }
4350
4351    /// Fresh sequence: clear KV, reuse history and device mirrors.
4352    pub fn reset_session(&mut self) {
4353        self.kv_cache.clear();
4354        self.kv_history.clear();
4355        crate::gpu::graph_kv_reset(self.graph_kv_id);
4356    }
4357
4358    /// Batched span prefill from token ids (coordinator side): embed +
4359    /// layers [0 ..= upto]; returns the boundary hiddens of ALL positions
4360    /// (ids.len() × hidden). Rides the same layer-major machinery as the
4361    /// local prefill; falls back to the per-position walk under
4362    /// CMF_PREFILL=seq.
4363    pub fn prefill_span_ids(
4364        &mut self,
4365        ids: &[u32],
4366        start_pos: usize,
4367        upto: usize,
4368        task_mask: Option<&TaskMask>,
4369    ) -> Result<Vec<f32>, String> {
4370        self.split_supported()?;
4371        if upto >= self.num_layers {
4372            return Err(format!(
4373                "prefill_span_ids: upto {upto} outside 0..{}",
4374                self.num_layers
4375            ));
4376        }
4377        // Same predicate as the whole-stack prefill: a span whose GDN
4378        // state lives on the device must walk positions through the
4379        // graph, not through the batched CPU span.
4380        if self.can_prefill_batched() && !self.graph_prefill_preferred() {
4381            Ok(self.prefill_batch_span(PrefillIn::Ids(ids), start_pos, task_mask, 0, upto + 1))
4382        } else {
4383            let hs = self.hidden_size;
4384            let mut out = Vec::with_capacity(ids.len() * hs);
4385            for (i, &id) in ids.iter().enumerate() {
4386                let emb = self.embed_id(id);
4387                out.extend_from_slice(&self.forward_span(
4388                    &emb,
4389                    start_pos + i,
4390                    0,
4391                    upto,
4392                    task_mask,
4393                )?);
4394            }
4395            Ok(out)
4396        }
4397    }
4398
4399    /// Batched span prefill from boundary hiddens (worker side): layers
4400    /// [from ..= upto] for every position in the batch; returns the batch.
4401    pub fn prefill_span_hidden(
4402        &mut self,
4403        hidden: &[f32],
4404        start_pos: usize,
4405        from: usize,
4406        upto: usize,
4407        task_mask: Option<&TaskMask>,
4408    ) -> Result<Vec<f32>, String> {
4409        self.split_supported()?;
4410        let hs = self.hidden_size;
4411        if hidden.is_empty() || hidden.len() % hs != 0 {
4412            return Err(format!(
4413                "prefill_span_hidden: {} floats is not a multiple of hidden {hs}",
4414                hidden.len()
4415            ));
4416        }
4417        if from > upto || upto >= self.num_layers {
4418            return Err(format!(
4419                "prefill_span_hidden: layer range {from}..={upto} outside 0..{}",
4420                self.num_layers
4421            ));
4422        }
4423        if self.can_prefill_batched() && !self.graph_prefill_preferred() {
4424            Ok(self.prefill_batch_span(
4425                PrefillIn::Hidden(hidden),
4426                start_pos,
4427                task_mask,
4428                from,
4429                upto + 1,
4430            ))
4431        } else {
4432            let b = hidden.len() / hs;
4433            let mut out = Vec::with_capacity(hidden.len());
4434            for i in 0..b {
4435                let h = self.forward_span(
4436                    &hidden[i * hs..(i + 1) * hs],
4437                    start_pos + i,
4438                    from,
4439                    upto,
4440                    task_mask,
4441                )?;
4442                out.extend_from_slice(&h);
4443            }
4444            Ok(out)
4445        }
4446    }
4447
4448    /// Build the whole-token wgpu graph for a pure-attention q1 model (every
4449    /// layer Full q1 + dense q1 FFN, no gate/bias). Returns the post-stack
4450    /// hidden (caller does final norm + lm_head), or None to fall back.
4451    fn try_token_graph_wgpu(
4452        &self,
4453        hidden: &[f32],
4454        position: usize,
4455        logits_out: &mut Vec<f32>,
4456        layers_run: &mut usize,
4457    ) -> Option<Vec<f32>> {
4458        self.try_token_graph_wgpu_steps(
4459            hidden,
4460            position,
4461            logits_out,
4462            1,
4463            None,
4464            Some(layers_run),
4465            0,
4466            self.num_layers,
4467        )
4468    }
4469
4470    /// The span twin (network split): the graph covers [from..upto_excl)
4471    /// — one submit per SEGMENT per token. lm_head folds in only when
4472    /// the span reaches the last layer.
4473    fn try_token_graph_wgpu_span(
4474        &self,
4475        hidden: &[f32],
4476        position: usize,
4477        logits_out: &mut Vec<f32>,
4478        from: usize,
4479        upto_excl: usize,
4480        layers_run: &mut usize,
4481    ) -> Option<Vec<f32>> {
4482        self.try_token_graph_wgpu_steps(
4483            hidden,
4484            position,
4485            logits_out,
4486            1,
4487            None,
4488            Some(layers_run),
4489            from,
4490            upto_excl,
4491        )
4492    }
4493
4494    /// Greedy burst: forward `t_next` and let the device pick + re-embed
4495    /// the next k−1 tokens — k frames, ONE submit, k ids back. The ZML
4496    /// trade, on wgpu. None ⇒ caller keeps the per-token path.
4497    fn try_multi_burst(&self, t_next: u32, position: usize, k: usize) -> Option<Vec<u32>> {
4498        if self.o1_active() || self.attn_softcap > 0.0 {
4499            return None;
4500        }
4501        let graph_on = match std::env::var("CMF_GPU_WGPU_GRAPH").ok().as_deref() {
4502            Some("0") => return None,
4503            Some(_) => true,
4504            None => crate::gpu::wgpu_graph_default(),
4505        };
4506        if !graph_on {
4507            return None;
4508        }
4509        let emb = self.embed_single(t_next);
4510        let mut lg = Vec::new();
4511        let mut ids = Vec::new();
4512        self.try_token_graph_wgpu_steps(
4513            &emb,
4514            position,
4515            &mut lg,
4516            k,
4517            Some(&mut ids),
4518            None,
4519            0,
4520            self.num_layers,
4521        )?;
4522        (ids.len() == k).then_some(ids)
4523    }
4524
4525    /// Multi-step greedy: k whole frames in ONE submit, argmax and re-embed
4526    /// on the device. `ids_out` receives the k winner ids; the hidden/logits
4527    /// outputs are NOT produced in that mode.
4528    fn try_token_graph_wgpu_steps(
4529        &self,
4530        hidden: &[f32],
4531        position: usize,
4532        logits_out: &mut Vec<f32>,
4533        steps: usize,
4534        ids_out: Option<&mut Vec<u32>>,
4535        layers_run: Option<&mut usize>,
4536        from: usize,
4537        upto_excl: usize,
4538    ) -> Option<Vec<f32>> {
4539        // O(1) Nyström decode runs off the sealed state, not the KV cache the
4540        // graph mirrors — never take the graph while o1 is active.
4541        let o1_gpu = std::env::var("CMF_O1_GPU").as_deref() == Ok("1");
4542        if (self.o1_active() && !o1_gpu) || self.attn_softcap > 0.0 {
4543            // Softcapped scores have no graph kernel yet — CPU owns them.
4544            // o1 rides the graph only behind CMF_O1_GPU=1 while the port
4545            // proves itself; without it the CPU path owns o1 as before.
4546            return None;
4547        }
4548        // Per-layer sealed o1 state for the graph. During prefill the
4549        // state is still Collecting -> views are None -> the graph
4550        // refuses below and the CPU prefill records the q trace and
4551        // seals, exactly as the o1 design requires.
4552        let o1_views: Vec<Option<Vec<crate::nystrom::O1DeviceView<'_>>>> = (from..upto_excl)
4553            .map(|li| {
4554                if !o1_gpu {
4555                    return None;
4556                }
4557                self.kv_cache.layers[self.phys_layer(li)].o1_views()
4558            })
4559            .collect();
4560        if self.o1_active() && o1_gpu {
4561            // Any o1 layer not sealed (or degenerate exact-only) keeps the
4562            // whole token on the CPU: half-graph forwards would desync.
4563            let want: usize = (from..upto_excl)
4564                .filter(|li| !matches!(self.kv_cache.layers[self.phys_layer(*li)].o1, None))
4565                .count();
4566            let have = o1_views.iter().filter(|v| v.is_some()).count();
4567            if want == 0 || have != want {
4568                return None;
4569            }
4570        }
4571        let nh = self.num_heads;
4572        let (nkv, hd, rd) = self.layer_geom(0);
4573        let gemma = self.norm_style == cortiq_core::NormStyle::Gemma;
4574        let mut layers = Vec::with_capacity(upto_excl - from);
4575        let mut model = None;
4576        let dbg = std::env::var("CMF_GRAPH_DEBUG").is_ok();
4577        fn gw(t: &QTensor) -> Option<crate::gpu::GraphW<'_>> {
4578            if let Some((_, i, kind, rs)) = t.graph_weight() {
4579                return Some(crate::gpu::GraphW {
4580                    idx: i,
4581                    kind,
4582                    row_scale: rs,
4583                    data: &[],
4584                });
4585            }
4586            // Small unquantized projections (GDN in_proj_a/b) stay f32.
4587            t.as_f32().map(|d| crate::gpu::GraphW {
4588                idx: 0,
4589                kind: 4,
4590                row_scale: &[],
4591                data: d,
4592            })
4593        }
4594        for li in from..upto_excl {
4595            let lw = &self.weights.layers[self.phys_layer(li)];
4596            if dbg {
4597                let ak = match &lw.attn {
4598                    AttnKind::Mla(_) => "Mla".into(),
4599                    AttnKind::Full {
4600                        output_gate, bias, ..
4601                    } => format!("Full gate={output_gate} bias={}", bias.is_some()),
4602                    AttnKind::LinearGdn(_) => "LinearGdn".into(),
4603                    AttnKind::Kda(_) => "Kda".into(),
4604                    AttnKind::Linear(_) => "Linear".into(),
4605                    AttnKind::ShortConv(_) => "ShortConv".into(),
4606                };
4607                let fk = match &lw.ffn {
4608                    FfnKind::Dense(_) => "Dense",
4609                    FfnKind::Moe(_) => "Moe",
4610                    FfnKind::DenseMoe(_) => "DenseMoe",
4611                };
4612                eprintln!("graph L{li}: attn={ak} ffn={fk}");
4613            }
4614            let gffn = match &lw.ffn {
4615                FfnKind::DenseMoe(_) => return None, // dual branch: CPU path
4616                FfnKind::Dense(d) => crate::gpu::GraphFfn::Dense {
4617                    gate: gw(&d.gate_proj)?,
4618                    up: gw(&d.up_proj)?,
4619                    down: gw(&d.down_proj)?,
4620                },
4621                FfnKind::Moe(m) => {
4622                    // v1 scope: softmax router + shared expert + uniform
4623                    // q4t expert trios (the MoE-hybrid coder class). The
4624                    // biased/sigmoid routers and adaptive τ keep the CPU
4625                    // path, where they are implemented.
4626                    if m.router_sigmoid
4627                        || m.expert_bias.is_some()
4628                        || m.route_tau.is_some()
4629                        || m.mask.is_some()
4630                    {
4631                        return None;
4632                    }
4633                    let (se, sg) = m.shared.as_ref()?;
4634                    let sgate = gw(sg.as_ref()?)?;
4635                    let router = gw(&m.router)?;
4636                    let inter = m.experts.first()?.gate_proj.rows();
4637                    let mut experts = Vec::with_capacity(m.experts.len() + 1);
4638                    // q4t or q4tp, but not both in one layer — the kernels
4639                    // are picked per layer, not per expert.
4640                    let mut q4tp: Option<bool> = None;
4641                    // The mixed 2-bit profile: q2tp gate/up over a q4tp
4642                    // down. Uniform across the layer, like `q4tp` itself.
4643                    let mut gu_q2: Option<bool> = None;
4644                    for e in m.experts.iter().chain(std::iter::once(se)) {
4645                        if !matches!(e.act, Act::Silu)
4646                            || e.gate_proj.rows() != inter
4647                            || e.up_proj.rows() != inter
4648                        {
4649                            return None;
4650                        }
4651                        let (mm, gi, ui, di, is_p, is_q2) = match e.gate_proj.mapped_q4t() {
4652                            Some((mm, gi)) => (
4653                                mm,
4654                                gi,
4655                                e.up_proj.mapped_q4t()?.1,
4656                                e.down_proj.mapped_q4t()?.1,
4657                                false,
4658                                false,
4659                            ),
4660                            None => match e.gate_proj.mapped_q2tp() {
4661                                Some((mm, gi)) => (
4662                                    mm,
4663                                    gi,
4664                                    e.up_proj.mapped_q2tp()?.1,
4665                                    e.down_proj.mapped_q4tp()?.1,
4666                                    true,
4667                                    true,
4668                                ),
4669                                None => {
4670                                    let (mm, gi) = e.gate_proj.mapped_q4tp()?;
4671                                    (
4672                                        mm,
4673                                        gi,
4674                                        e.up_proj.mapped_q4tp()?.1,
4675                                        e.down_proj.mapped_q4tp()?.1,
4676                                        true,
4677                                        false,
4678                                    )
4679                                }
4680                            },
4681                        };
4682                        if *q4tp.get_or_insert(is_p) != is_p || *gu_q2.get_or_insert(is_q2) != is_q2
4683                        {
4684                            // The shared expert rides in the same packed
4685                            // buffer as the routed ones, so a layer that
4686                            // mixes layouts cannot be indexed by one stride.
4687                            // Say so: the symptom is a whole model quietly
4688                            // running its MoE on the CPU.
4689                            tracing::warn!(
4690                                "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."
4691                            );
4692                            return None;
4693                        }
4694                        model.get_or_insert_with(|| mm.clone());
4695                        experts.push((gi, ui, di));
4696                    }
4697                    crate::gpu::GraphFfn::Moe {
4698                        router,
4699                        shared_gate: sgate,
4700                        experts,
4701                        n_exp: m.experts.len(),
4702                        // CMF_TOPK_PROBE: timing probe only — output is WRONG.
4703                        // Fewer experts shrink the MoE arithmetic while the
4704                        // dispatch count stays identical, which is the only
4705                        // clean way to tell a launch-bound decode from a
4706                        // compute-bound one.
4707                        top_k: std::env::var("CMF_TOPK_PROBE")
4708                            .ok()
4709                            .and_then(|v| v.parse::<usize>().ok())
4710                            .filter(|k| *k > 0 && *k <= m.top_k)
4711                            .unwrap_or(m.top_k),
4712                        inter,
4713                        norm_topk: m.norm_topk_prob,
4714                        q4tp: q4tp?,
4715                        gu_q2: gu_q2.unwrap_or(false),
4716                    }
4717                }
4718            };
4719            let attn = match &lw.attn {
4720                AttnKind::Full {
4721                    wq,
4722                    wk,
4723                    wv,
4724                    wo,
4725                    q_norm,
4726                    k_norm,
4727                    output_gate,
4728                    softplus_gate,
4729                    bias,
4730                } => {
4731                    if softplus_gate.is_some() || self.attention_heads_per_layer.is_some() {
4732                        return None;
4733                    }
4734                    let (m, _, _, _) = wq.graph_weight()?;
4735                    model = Some(m.clone());
4736                    crate::gpu::GraphAttn::Full {
4737                        wq: gw(wq)?,
4738                        wk: gw(wk)?,
4739                        wv: gw(wv)?,
4740                        wo: gw(wo)?,
4741                        q_norm: q_norm.as_deref(),
4742                        k_norm: k_norm.as_deref(),
4743                        bias: bias
4744                            .as_ref()
4745                            .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
4746                        output_gate: *output_gate,
4747                        cpu_k: self.kv_cache.layers[li].k_heads(),
4748                        cpu_v: self.kv_cache.layers[li].v_heads(),
4749                    }
4750                }
4751                AttnKind::LinearGdn(w) => {
4752                    let cfg = self.gdn_cfg?;
4753                    let (m, _, _, _) = w.in_proj_qkv.graph_weight()?;
4754                    model = Some(m.clone());
4755                    crate::gpu::GraphAttn::Gdn {
4756                        qkv: gw(&w.in_proj_qkv)?,
4757                        z: gw(&w.in_proj_z)?,
4758                        a: gw(&w.in_proj_a)?,
4759                        b: gw(&w.in_proj_b)?,
4760                        out: gw(&w.out_proj)?,
4761                        conv1d: &w.conv1d,
4762                        a_log: &w.a_log,
4763                        dt_bias: &w.dt_bias,
4764                        norm: &w.norm,
4765                        nv: cfg.num_v_heads,
4766                        nk: cfg.num_k_heads,
4767                        dk: cfg.key_head_dim,
4768                        dv: cfg.value_head_dim,
4769                        kk: cfg.conv_kernel,
4770                        cpu_state: &self.kv_cache.layers[self.phys_layer(li)].linear_state,
4771                    }
4772                }
4773                _ => return None,
4774            };
4775            layers.push(crate::gpu::GraphLayer {
4776                input_norm: &lw.input_norm,
4777                attn,
4778                post_norm: &lw.post_norm,
4779                ffn: gffn,
4780            });
4781        }
4782        let model = model?;
4783        // Fold final-norm + lm_head into the graph when this call wants logits
4784        // and the lm_head is a graphable (quantized) weight — the graph then
4785        // reads back logits (into logits_out) instead of the hidden, dropping
4786        // the separate CPU/GPU lm_head op + its sync. Never the f32 fallback:
4787        // an unquantized lm_head is vocab·hidden and must not be uploaded.
4788        let lm_gw = if upto_excl == self.num_layers
4789            && self.graph_want_logits
4790            && std::env::var("CMF_GPU_LMHEAD")
4791                .map(|v| v != "0")
4792                .unwrap_or(true)
4793        {
4794            self.weights.lm_head.graph_weight().map(|(_, i, kind, rs)| {
4795                (
4796                    crate::gpu::GraphW {
4797                        idx: i,
4798                        kind,
4799                        row_scale: rs,
4800                        data: &[],
4801                    },
4802                    self.weights.lm_head.rows(),
4803                )
4804            })
4805        } else {
4806            None
4807        };
4808        let lm = lm_gw.as_ref().map(|(gw, rows)| (gw, *rows));
4809        // Multi-step re-embeds the winner on the device.
4810        let emb_gw = if steps > 1 {
4811            self.weights
4812                .embed_tokens
4813                .graph_weight()
4814                .map(|(_, i, kind, rs)| {
4815                    (
4816                        crate::gpu::GraphW {
4817                            idx: i,
4818                            kind,
4819                            row_scale: rs,
4820                            data: &[],
4821                        },
4822                        self.weights.embed_tokens.rows(),
4823                        self.embed_multiplier as f32,
4824                    )
4825                })
4826        } else {
4827            None
4828        };
4829
4830        // Loop boundaries: virtual layer indices after which final_norm is
4831        // applied (mid-stack only; the GLOBAL last layer's norm folds into
4832        // lm_head). Span-relative — the executor compares its enumerate
4833        // index. A span ending mid-stack keeps its boundary norm even when
4834        // it is the span's own last layer.
4835        let loop_norm_at: Vec<usize> = if self.loop_final_norm {
4836            (from..upto_excl.min(self.num_layers - 1))
4837                .filter(|&li| (li + 1) % self.physical_layers == 0)
4838                .map(|li| li - from)
4839                .collect()
4840        } else {
4841            Vec::new()
4842        };
4843        let mut h = hidden.to_vec();
4844        crate::gpu::forward_token_graph(
4845            &model,
4846            self.graph_kv_id,
4847            &layers,
4848            &o1_views,
4849            self.o1_epoch,
4850            &self.inv_freq,
4851            &mut h,
4852            nh,
4853            nkv,
4854            hd,
4855            rd,
4856            self.hidden_size,
4857            self.intermediate_size,
4858            position,
4859            self.kv_cache.max_seq_len,
4860            gemma,
4861            self.rms_eps as f32,
4862            lm,
4863            &self.weights.final_norm,
4864            logits_out,
4865            &loop_norm_at,
4866            steps,
4867            emb_gw.as_ref().map(|(gw, rows, m)| (gw, *rows, *m)),
4868            ids_out,
4869            layers_run,
4870            from,
4871        )
4872        .then_some(h)
4873    }
4874
4875    /// Batched prefill: k contiguous prompt positions through the whole wgpu
4876    /// graph in ONE submit (projections/FFN as GEMMs). `hiddens` is [k·hidden]
4877    /// in/out (embeddings in, layer output out); KV mirror / GDN state advance.
4878    /// false ⇒ unsupported → caller keeps the per-position graph.
4879    fn try_batch_graph_wgpu(
4880        &self,
4881        hiddens: &mut [f32],
4882        positions: &[usize],
4883        k: usize,
4884        spec: Option<crate::gpu::SpecTail<'_>>,
4885    ) -> bool {
4886        let _tb = std::time::Instant::now();
4887        if self.attn_softcap > 0.0 {
4888            return false; // capped scores: no graph kernel — CPU path
4889        }
4890        if self.o1_active() {
4891            return false;
4892        }
4893        let nh = self.num_heads;
4894        let (nkv, hd, rd) = self.layer_geom(0);
4895        let gemma = self.norm_style == cortiq_core::NormStyle::Gemma;
4896        fn gw(t: &QTensor) -> Option<crate::gpu::GraphW<'_>> {
4897            if let Some((_, i, kind, rs)) = t.graph_weight() {
4898                return Some(crate::gpu::GraphW {
4899                    idx: i,
4900                    kind,
4901                    row_scale: rs,
4902                    data: &[],
4903                });
4904            }
4905            t.as_f32().map(|d| crate::gpu::GraphW {
4906                idx: 0,
4907                kind: 4,
4908                row_scale: &[],
4909                data: d,
4910            })
4911        }
4912        let built: Option<(
4913            Vec<crate::gpu::GraphLayer<'_>>,
4914            std::sync::Arc<cortiq_core::CmfModel>,
4915        )> = (|| {
4916            let mut layers = Vec::with_capacity(self.num_layers);
4917            let mut model = None;
4918            for li in 0..self.num_layers {
4919                let lw = &self.weights.layers[self.phys_layer(li)];
4920                // MoE routes per token, so its experts are encoded token by
4921                // token inside the batched submit while attention and the
4922                // projections stay GEMMs. Refusing MoE here is what left
4923                // prefill running one position at a time: 33 tok/s against
4924                // 54 on decode, i.e. reading the prompt was slower than
4925                // writing the answer.
4926                let gffn = match &lw.ffn {
4927                    FfnKind::Dense(d) => crate::gpu::GraphFfn::Dense {
4928                        gate: gw(&d.gate_proj)?,
4929                        up: gw(&d.up_proj)?,
4930                        down: gw(&d.down_proj)?,
4931                    },
4932                    FfnKind::Moe(m) => {
4933                        if m.router_sigmoid
4934                            || m.expert_bias.is_some()
4935                            || m.route_tau.is_some()
4936                            || m.mask.is_some()
4937                        {
4938                            return None;
4939                        }
4940                        let (se, sg) = m.shared.as_ref()?;
4941                        let sgate = gw(sg.as_ref()?)?;
4942                        let router = gw(&m.router)?;
4943                        let inter = m.experts.first()?.gate_proj.rows();
4944                        let mut experts = Vec::with_capacity(m.experts.len() + 1);
4945                        let mut q4tp: Option<bool> = None;
4946                        let mut gu_q2: Option<bool> = None;
4947                        for e in m.experts.iter().chain(std::iter::once(se)) {
4948                            if !matches!(e.act, Act::Silu)
4949                                || e.gate_proj.rows() != inter
4950                                || e.up_proj.rows() != inter
4951                            {
4952                                return None;
4953                            }
4954                            // Same ladder as the token graph: q4t → q2tp
4955                            // (mixed profile: 2-bit gate/up over a q4tp
4956                            // down) → q4tp. Uniform across the layer.
4957                            let (mm, gi, ui, di, is_p, is_q2) = match e.gate_proj.mapped_q4t() {
4958                                Some((mm, gi)) => (
4959                                    mm,
4960                                    gi,
4961                                    e.up_proj.mapped_q4t()?.1,
4962                                    e.down_proj.mapped_q4t()?.1,
4963                                    false,
4964                                    false,
4965                                ),
4966                                None => match e.gate_proj.mapped_q2tp() {
4967                                    Some((mm, gi)) => (
4968                                        mm,
4969                                        gi,
4970                                        e.up_proj.mapped_q2tp()?.1,
4971                                        e.down_proj.mapped_q4tp()?.1,
4972                                        true,
4973                                        true,
4974                                    ),
4975                                    None => {
4976                                        let (mm, gi) = e.gate_proj.mapped_q4tp()?;
4977                                        (
4978                                            mm,
4979                                            gi,
4980                                            e.up_proj.mapped_q4tp()?.1,
4981                                            e.down_proj.mapped_q4tp()?.1,
4982                                            true,
4983                                            false,
4984                                        )
4985                                    }
4986                                },
4987                            };
4988                            if *q4tp.get_or_insert(is_p) != is_p
4989                                || *gu_q2.get_or_insert(is_q2) != is_q2
4990                            {
4991                                return None;
4992                            }
4993                            model.get_or_insert_with(|| mm.clone());
4994                            experts.push((gi, ui, di));
4995                        }
4996                        crate::gpu::GraphFfn::Moe {
4997                            router,
4998                            shared_gate: sgate,
4999                            experts,
5000                            n_exp: m.experts.len(),
5001                            top_k: m.top_k,
5002                            inter,
5003                            norm_topk: m.norm_topk_prob,
5004                            q4tp: q4tp?,
5005                            gu_q2: gu_q2.unwrap_or(false),
5006                        }
5007                    }
5008                    _ => return None,
5009                };
5010                let attn = match &lw.attn {
5011                    AttnKind::Full {
5012                        wq,
5013                        wk,
5014                        wv,
5015                        wo,
5016                        q_norm,
5017                        k_norm,
5018                        output_gate,
5019                        softplus_gate,
5020                        bias,
5021                    } => {
5022                        if softplus_gate.is_some() || self.attention_heads_per_layer.is_some() {
5023                            return None;
5024                        }
5025                        let (m, _, _, _) = wq.graph_weight()?;
5026                        model = Some(m.clone());
5027                        crate::gpu::GraphAttn::Full {
5028                            wq: gw(wq)?,
5029                            wk: gw(wk)?,
5030                            wv: gw(wv)?,
5031                            wo: gw(wo)?,
5032                            q_norm: q_norm.as_deref(),
5033                            k_norm: k_norm.as_deref(),
5034                            bias: bias
5035                                .as_ref()
5036                                .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
5037                            output_gate: *output_gate,
5038                            cpu_k: self.kv_cache.layers[li].k_heads(),
5039                            cpu_v: self.kv_cache.layers[li].v_heads(),
5040                        }
5041                    }
5042                    AttnKind::LinearGdn(w) => {
5043                        let cfg = self.gdn_cfg?;
5044                        let (m, _, _, _) = w.in_proj_qkv.graph_weight()?;
5045                        model = Some(m.clone());
5046                        crate::gpu::GraphAttn::Gdn {
5047                            qkv: gw(&w.in_proj_qkv)?,
5048                            z: gw(&w.in_proj_z)?,
5049                            a: gw(&w.in_proj_a)?,
5050                            b: gw(&w.in_proj_b)?,
5051                            out: gw(&w.out_proj)?,
5052                            conv1d: &w.conv1d,
5053                            a_log: &w.a_log,
5054                            dt_bias: &w.dt_bias,
5055                            norm: &w.norm,
5056                            nv: cfg.num_v_heads,
5057                            nk: cfg.num_k_heads,
5058                            dk: cfg.key_head_dim,
5059                            dv: cfg.value_head_dim,
5060                            kk: cfg.conv_kernel,
5061                            cpu_state: &self.kv_cache.layers[self.phys_layer(li)].linear_state,
5062                        }
5063                    }
5064                    _ => return None,
5065                };
5066                layers.push(crate::gpu::GraphLayer {
5067                    input_norm: &lw.input_norm,
5068                    attn,
5069                    post_norm: &lw.post_norm,
5070                    ffn: gffn,
5071                });
5072            }
5073            Some((layers, model?))
5074        })();
5075        let Some((layers, model)) = built else {
5076            {
5077                use std::sync::atomic::{AtomicBool, Ordering};
5078                static SAID: AtomicBool = AtomicBool::new(false);
5079                if !SAID.swap(true, Ordering::Relaxed) {
5080                    tracing::warn!("batch graph: BUILDER refused (layer weights/kinds)");
5081                }
5082            }
5083            return false;
5084        };
5085        if std::env::var("CMF_GRAPH_SPEC_TIME").is_ok() {
5086            eprintln!("batch-build: {:.1} ms", _tb.elapsed().as_secs_f64() * 1e3);
5087        }
5088        crate::gpu::forward_batch_graph(
5089            &model,
5090            self.graph_kv_id,
5091            &layers,
5092            &self.inv_freq,
5093            hiddens,
5094            nh,
5095            nkv,
5096            hd,
5097            rd,
5098            self.hidden_size,
5099            self.intermediate_size,
5100            positions,
5101            self.kv_cache.max_seq_len,
5102            gemma,
5103            self.rms_eps as f32,
5104            k,
5105            spec,
5106        )
5107    }
5108
5109    /// Same, stopping after layer `upto` inclusive (routing probe φ).
5110/// `CMF_DSV4_DRAFT_PROBE=1` — grade the draft against what the trunk goes on
5111/// to produce. Off by default; it runs a whole draft per decoded token.
5112fn draft_probe() -> bool {
5113    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
5114    *ON.get_or_init(|| std::env::var("CMF_DSV4_DRAFT_PROBE").is_ok_and(|v| v != "0"))
5115}
5116
5117    /// `CMF_DSV4_DRAFT_PROBE=1`: measure how much of the draft the trunk
5118    /// would have agreed with, WITHOUT verifying or rolling anything back.
5119    ///
5120    /// The number this produces decides the whole speculation design — at
5121    /// acceptance a, a block of B positions yields 1 + a + a² + ... tokens
5122    /// per trunk pass — so it is worth measuring before any of the machinery
5123    /// that would exploit it exists. Each draft is parked with the position
5124    /// it was made at, and graded as the real tokens arrive.
5125    /// `CMF_DSV4_SPEC=1` — the DeepSeek-V4 speculative decode: draft five
5126    /// on the card, verify them in one batched trunk pass, commit the
5127    /// accepted prefix, roll the rest back.
5128    #[cfg(feature = "gpu")]
5129    fn dsv4_spec_on() -> bool {
5130        static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
5131        *ON.get_or_init(|| std::env::var("CMF_DSV4_SPEC").map(|v| v != "0").unwrap_or(true))
5132    }
5133
5134    /// One speculative round at the decode tip. `t_next` is the token the
5135    /// sampler just committed for `next_pos`. Returns the EXTRA accepted
5136    /// tokens (possibly none) and the new position, with `graph_logits`
5137    /// left holding the last accepted position's logits — exactly what the
5138    /// loop top expects. `None` means "speculate not this round": nothing
5139    /// was committed, the caller forwards normally.
5140    #[cfg(feature = "gpu")]
5141    fn dsv4_spec_step(
5142        &mut self,
5143        tip_token: u32,
5144        t_next: u32,
5145        next_pos: usize,
5146        drafted: &mut usize,
5147        accepted_ctr: &mut usize,
5148    ) -> Option<(Vec<u32>, usize)> {
5149        let t_all = std::time::Instant::now();
5150        if std::env::var("CMF_DSV4_SPEC_TIME").is_ok() {
5151            thread_local! {
5152                static LAST: std::cell::Cell<Option<std::time::Instant>> =
5153                    const { std::cell::Cell::new(None) };
5154            }
5155            LAST.with(|l| {
5156                if let Some(prev) = l.get() {
5157                    eprintln!("между раундами {:.1} мс", prev.elapsed().as_secs_f64() * 1e3);
5158                }
5159                l.set(Some(std::time::Instant::now()));
5160            });
5161        }
5162        if std::env::var("CMF_DSV4_SPEC_DEBUG").is_ok() {
5163            eprintln!("spec_step: вход pos={next_pos}");
5164        }
5165        let n_layers = self.dsv4.as_ref().map(|b| b.1.len())?;
5166        let cfg = self.dsv4.as_ref().map(|b| b.2)?;
5167        // The draft state and its capture, armed exactly as the probe does.
5168        if self.dspark.is_none() {
5169            let t = crate::dsv4::dspark_targets(&self.dsv4_mtp, &cfg, n_layers);
5170            if t.is_empty() {
5171                return None;
5172            }
5173            crate::dsv4::dspark_arm(&t, cfg.dim);
5174            self.dspark = Some(crate::dsv4::DsparkState::new(
5175                self.dsv4_mtp.len(),
5176                &cfg,
5177                t.len(),
5178            ));
5179        }
5180        let targets = crate::dsv4::dspark_targets(&self.dsv4_mtp, &cfg, n_layers);
5181        let pack = crate::dsv4::dspark_pack_get(&self.dsv4_mtp, &cfg);
5182        if pack.is_none() && std::env::var("CMF_DSV4_SPEC_DEBUG").is_ok() {
5183            eprintln!("spec_step: пак не построился (targets {targets:?})");
5184        }
5185        let pack = pack?;
5186        let block = crate::dsv4::dspark_block();
5187        let b_box = self.dsv4.as_mut()?;
5188        let (g, layers, st) = (&b_box.0, &b_box.1, &mut b_box.3);
5189        let ds = self.dspark.as_mut()?;
5190        // The tip's captures: either this token ran on a normal path that
5191        // filled the thread-local, or the previous spec round left them.
5192        let dbg = std::env::var("CMF_DSV4_SPEC_DEBUG").is_ok();
5193        if !crate::dsv4::dspark_take(&mut ds.main_hidden) && !ds.have_hidden {
5194            if dbg {
5195                eprintln!("spec_step: нет захвата");
5196            }
5197            return None;
5198        }
5199        ds.have_hidden = true;
5200        let tip_pos = next_pos.checked_sub(1)?;
5201        let draft_started = std::time::Instant::now();
5202        let mut conf = Vec::new();
5203        let props = crate::dsv4::dspark_draft_gpu(
5204            g,
5205            &self.dsv4_mtp,
5206            &cfg,
5207            ds,
5208            pack,
5209            st.kv_id,
5210            tip_token,
5211            tip_pos,
5212            self.pool.as_deref(),
5213            &mut conf,
5214        );
5215        self.dspark_draft_ns += draft_started.elapsed().as_nanos();
5216        *drafted += block;
5217        if props.is_empty() || props[0] != t_next {
5218            if dbg {
5219                eprintln!(
5220                    "spec_step: черновик {} (props0={:?} t_next={t_next})",
5221                    if props.is_empty() { "пуст" } else { "мимо" },
5222                    props.first()
5223                );
5224            }
5225            return None;
5226        }
5227        let mut k_verify = crate::dsv4::dspark_verify_k().min(props.len());
5228        // Adaptive depth: positions the draft itself doubts are paid for on
5229        // every verify and delivered almost never (natural-text survival
5230        // [.67 .50 .29 .08 .04]). `CMF_DSPARK_CONF_MIN=p` trims the fed
5231        // prefix at the first proposal whose confidence drops below p; on
5232        // predictable text the confidences stay high and nothing changes.
5233        let conf_min = {
5234            static M: std::sync::OnceLock<f32> = std::sync::OnceLock::new();
5235            *M.get_or_init(|| {
5236                std::env::var("CMF_DSPARK_CONF_MIN")
5237                    .ok()
5238                    .and_then(|v| v.parse().ok())
5239                    .unwrap_or(0.0)
5240            })
5241        };
5242        if conf_min > 0.0 && conf.len() >= props.len() {
5243            let mut keep = 1usize;
5244            while keep < k_verify && conf.get(keep).copied().unwrap_or(0.0) >= conf_min {
5245                keep += 1;
5246            }
5247            k_verify = k_verify.min(keep.max(2));
5248        }
5249        if k_verify < 2 {
5250            return None;
5251        }
5252        let mut fed = Vec::with_capacity(k_verify);
5253        fed.push(t_next);
5254        fed.extend_from_slice(&props[1..k_verify]);
5255        let mut argmax = Vec::new();
5256        let mut logits_all = Vec::new();
5257        let mut walked = Vec::new();
5258        let txn = crate::dsv4::dsv4_verify_chunk(
5259            g,
5260            layers,
5261            &cfg,
5262            st,
5263            &fed,
5264            next_pos,
5265            &self.inv_freq,
5266            self.pool.as_deref(),
5267            &targets,
5268            &mut argmax,
5269            &mut logits_all,
5270            &mut walked,
5271        );
5272        if txn.is_none() && dbg {
5273            eprintln!("spec_step: verify отказал");
5274        }
5275        let txn = txn?;
5276        let b = fed.len();
5277        let mut accepted = 1usize;
5278        while accepted < b && fed[accepted] == argmax[accepted - 1] {
5279            accepted += 1;
5280        }
5281        // `CMF_DSV4_SPEC_FORCE_REJECT=1` — accept nothing beyond the known
5282        // token, every round: the pure rollback exerciser. The output must
5283        // stay byte-identical to the plain walk; anything else is a
5284        // transaction bug, isolated from the acceptance logic.
5285        if std::env::var("CMF_DSV4_SPEC_FORCE_REJECT").is_ok_and(|v| v != "0") {
5286            accepted = 1;
5287        }
5288        if std::env::var("CMF_DSV4_SPEC_TRACE").is_ok() {
5289            eprintln!(
5290                "spec@{next_pos}: fed={fed:?} argmax={argmax:?} accepted={accepted}"
5291            );
5292        }
5293        let t_fin = std::time::Instant::now();
5294        if !crate::dsv4::dsv4_spec_finish(
5295            g,
5296            layers,
5297            &cfg,
5298            st,
5299            txn,
5300            accepted,
5301            &fed,
5302            &self.inv_freq,
5303            self.pool.as_deref(),
5304        ) {
5305            tracing::warn!("dsv4: спекулятивный откат не удался — состояние подозрительно");
5306            return None;
5307        }
5308        if std::env::var("CMF_DSV4_SPEC_TIME").is_ok() {
5309            eprintln!("finish(k={accepted}): {:.1} мс", t_fin.elapsed().as_secs_f64() * 1e3);
5310        }
5311        *accepted_ctr += accepted - 1;
5312        // Captures per accepted token: device targets photographed by the
5313        // batch, host targets from the verify's own walk. The last one
5314        // becomes the new tip's draft input; every one owes the ring an
5315        // entry for its position.
5316        let (hc, dim) = (cfg.hc_mult, cfg.dim);
5317        // A PARTIAL capture layer never rides the chain, so the batch has
5318        // no photograph of it — its tip capture comes from the walk's own
5319        // note like any host layer's. Filtering on the device set alone
5320        // handed the draft a never-written photo slot for exactly the
5321        // most important input (the last layer feeds main_proj), and the
5322        // split configurations drafted at 27% no matter the residency.
5323        let dev_caps: Vec<usize> = targets
5324            .iter()
5325            .copied()
5326            .filter(|&t| {
5327                st.dev_set.get(t).copied().unwrap_or(false)
5328                    && !st.partial_set.get(t).copied().unwrap_or(false)
5329            })
5330            .collect();
5331        let mut caps_all = vec![0.0f32; dev_caps.len() * b * hc * dim];
5332        if !crate::gpu_wgpu::dsv4_spec_cap_read_all(b, dev_caps.len(), hc * dim, &mut caps_all) {
5333            return None;
5334        }
5335        for t in 0..accepted {
5336            let tip = t + 1 == accepted;
5337            for (slot, &tl) in targets.iter().enumerate() {
5338                if let Some(di) = dev_caps.iter().position(|&d| d == tl) {
5339                    let lo = (di * b + t) * hc * dim;
5340                    crate::dsv4::dspark_capture(
5341                        &caps_all[lo..lo + hc * dim],
5342                        &cfg,
5343                        slot,
5344                        &mut ds.main_hidden,
5345                    );
5346                } else if tip
5347                    && crate::dsv4::dspark_peek_slot(slot, dim, {
5348                        let lo = slot * dim;
5349                        &mut ds.main_hidden[lo..lo + dim]
5350                    })
5351                {
5352                    // The tip's host-layer captures are the walk's own
5353                    // per-layer notes — exact. (The walk that ran last ended
5354                    // on exactly this token, on both the accept-all and the
5355                    // rollback path.)
5356                } else {
5357                    // Intermediate tokens: the post-tail state stands in for
5358                    // the per-layer capture on host targets below the last
5359                    // layer. Ring-entry quality only; the tip is exact.
5360                    crate::dsv4::dspark_capture(
5361                        &walked[t * hc * dim..(t + 1) * hc * dim],
5362                        &cfg,
5363                        slot,
5364                        &mut ds.main_hidden,
5365                    );
5366                }
5367            }
5368            crate::dsv4::dspark_ring_append(g, &self.dsv4_mtp, &cfg, ds, next_pos + t, self.pool.as_deref());
5369        }
5370        let row = logits_all[(accepted - 1) * cfg.vocab..accepted * cfg.vocab].to_vec();
5371        self.graph_logits = Some(row);
5372        // The speculative loop never runs the probe, so the trunk tally has
5373        // no other place to cycle. Armed only when someone asked for the
5374        // dump; the host tail is the only tallying path here, which is
5375        // precisely the population a partial pack would serve.
5376        if std::env::var("CMF_DSV4_TRUNK_PICK_DUMP").is_ok() {
5377            crate::dsv4::trunk_freq_note(&crate::dsv4::pick_tally_take());
5378            crate::dsv4::pick_tally_arm();
5379        }
5380        if std::env::var("CMF_DSV4_SPEC_TIME").is_ok() {
5381            eprintln!("spec_step total {:.1} мс (k={accepted})", t_all.elapsed().as_secs_f64() * 1e3);
5382        }
5383        Some((fed[1..accepted].to_vec(), next_pos + accepted))
5384    }
5385
5386    fn dspark_probe(&mut self, position: usize, token_id: u32) {
5387        if self.dsv4_mtp.is_empty() || !Self::draft_probe() {
5388            return;
5389        }
5390        // What the trunk just routed to, for this token.
5391        let trunk_now = crate::dsv4::pick_tally_take();
5392        crate::dsv4::trunk_freq_note(&trunk_now);
5393        if !trunk_now.is_empty() {
5394            self.dspark_trunk_picks.push(trunk_now);
5395            let keep = crate::dsv4::dspark_block();
5396            if self.dspark_trunk_picks.len() > keep {
5397                self.dspark_trunk_picks.remove(0);
5398            }
5399        }
5400        // Grade whatever is waiting: the token just decoded sits at
5401        // `position`, so it answers the draft made at `position - 1 - i`.
5402        for p in std::mem::take(&mut self.dspark_pending) {
5403            let Some(i) = position.checked_sub(p.0 + 1) else {
5404                continue;
5405            };
5406            let mut p = p;
5407            if i < p.1.len() {
5408                if p.2 && p.1[i] == token_id {
5409                    p.3 = i + 1;
5410                } else {
5411                    p.2 = false;
5412                }
5413                if i + 1 < p.1.len() {
5414                    self.dspark_pending.push(p);
5415                    continue;
5416                }
5417            }
5418            self.dspark_hist.push(p.3);
5419            self.dspark_real.push(token_id);
5420        }
5421        let Some(b) = &mut self.dsv4 else { return };
5422        let (g, layers, cfg) = (&b.0, &b.1, b.2);
5423        let n_layers = layers.len();
5424        if self.dspark.is_none() {
5425            let t = crate::dsv4::dspark_targets(&self.dsv4_mtp, &cfg, n_layers);
5426            if t.is_empty() {
5427                return;
5428            }
5429            eprintln!("DSpark: захват со слоёв {t:?}, блок {}", crate::dsv4::dspark_block());
5430            crate::dsv4::dspark_arm(&t, cfg.dim);
5431            self.dspark = Some(crate::dsv4::DsparkState::new(
5432                self.dsv4_mtp.len(),
5433                &cfg,
5434                t.len(),
5435            ));
5436        }
5437        let ds = self.dspark.as_mut().unwrap();
5438        if !crate::dsv4::dspark_take(&mut ds.main_hidden) {
5439            return; // this token ran on a path that captures nothing
5440        }
5441        let mut conf = Vec::new();
5442        crate::dsv4::pick_tally_arm();
5443        // The trunk has already consumed the adaptive VRAM budget. Until the
5444        // draft owns an explicit bounded device pack, its tensors are an
5445        // out-of-core CPU/disk tier by contract: never let per-op probes try
5446        // to squeeze another multi-gigabyte MTP expert cache onto the card.
5447        let draft_started = std::time::Instant::now();
5448        #[cfg(feature = "gpu")]
5449        let gpu_draft = crate::dsv4::dspark_gpu_on();
5450        #[cfg(not(feature = "gpu"))]
5451        let gpu_draft = false;
5452        let props = if gpu_draft {
5453            #[cfg(feature = "gpu")]
5454            {
5455                let kv_id = b.3.kv_id;
5456                match crate::dsv4::dspark_pack_get(&self.dsv4_mtp, &cfg) {
5457                    Some(pk) => crate::dsv4::dspark_draft_gpu(
5458                        g,
5459                        &self.dsv4_mtp,
5460                        &cfg,
5461                        ds,
5462                        pk,
5463                        kv_id,
5464                        token_id,
5465                        position,
5466                        self.pool.as_deref(),
5467                        &mut conf,
5468                    ),
5469                    None => Vec::new(),
5470                }
5471            }
5472            #[cfg(not(feature = "gpu"))]
5473            Vec::new()
5474        } else {
5475            crate::gpu::cpu_scope(|| {
5476                crate::dsv4::dspark_draft(
5477                    g,
5478                    &self.dsv4_mtp,
5479                    &cfg,
5480                    ds,
5481                    token_id,
5482                    position,
5483                    self.pool.as_deref(),
5484                    &mut conf,
5485                )
5486            })
5487        };
5488        self.dspark_draft_ns += draft_started.elapsed().as_nanos();
5489        let draft_picks = crate::dsv4::pick_tally_take();
5490        crate::dsv4::dspark_freq_note(&draft_picks);
5491        // Re-arm for the NEXT trunk token; the probe runs after the forward,
5492        // so this is the only place that can.
5493        crate::dsv4::pick_tally_arm();
5494        if !props.is_empty() {
5495            // Two ratios, side by side: what a batched verify over the trunk
5496            // would read against what it asks for, and the same for the
5497            // draft's three stages. Near 1.0 means a batch amortises nothing.
5498            let (tu, tt) = {
5499                let flat: Vec<(usize, Vec<usize>)> = self
5500                    .dspark_trunk_picks
5501                    .iter()
5502                    .flat_map(|v| v.iter().cloned())
5503                    .collect();
5504                // Per layer, across the window of tokens.
5505                let mut per: std::collections::HashMap<usize, Vec<usize>> =
5506                    std::collections::HashMap::new();
5507                for (li, picks) in flat {
5508                    per.entry(li).or_default().extend(picks);
5509                }
5510                let n = per.len().max(1);
5511                let mut u = 0usize;
5512                let mut t = 0usize;
5513                for (_, v) in per {
5514                    t += v.len();
5515                    u += v.iter().collect::<std::collections::HashSet<_>>().len();
5516                }
5517                (u / n, t / n)
5518            };
5519            let (du, dt) = crate::dsv4::tally_unique(&draft_picks);
5520            self.dspark_exp.push((tu, tt, du, dt));
5521            self.dspark_pending.push((position, props, true, 0));
5522        }
5523        if self.dspark_hist.len() >= 8 && self.dspark_hist.len() % 8 == 0 {
5524            let n = self.dspark_hist.len() as f32;
5525            let mean: f32 = self.dspark_hist.iter().sum::<usize>() as f32 / n;
5526            let block = crate::dsv4::dspark_block();
5527            let mut at = vec![0usize; block + 1];
5528            for &k in &self.dspark_hist {
5529                at[k] += 1;
5530            }
5531            // Prefix survival: S_i = P(the first i positions all held).
5532            let mut surv = Vec::with_capacity(block);
5533            for i in 1..=block {
5534                let k = at[i..].iter().sum::<usize>() as f32 / n;
5535                surv.push(format!("{k:.2}"));
5536            }
5537            let distinct = self
5538                .dspark_real
5539                .iter()
5540                .collect::<std::collections::HashSet<_>>()
5541                .len();
5542            let (tu, tt, du, dt) = self.dspark_exp.iter().fold((0, 0, 0, 0), |a, b| {
5543                (a.0 + b.0, a.1 + b.1, a.2 + b.2, a.3 + b.3)
5544            });
5545            let m = self.dspark_exp.len().max(1);
5546            eprintln!(
5547                "DSpark: черновиков {}, принято в среднем {mean:.2} из {block} \
5548                 (токенов за проход {:.2}), распределение {at:?}, выживание [{}]",
5549                self.dspark_hist.len(),
5550                mean + 1.0,
5551                surv.join(" ")
5552            );
5553            eprintln!(
5554                "DSpark: разных токенов {distinct} из {} (вырожденность), \
5555                 эксперты ствол {}/{} на слой за {block} токенов, \
5556                 черновик {}/{} за блок, draft {:.2} мс/блок",
5557                self.dspark_real.len(),
5558                tu / m,
5559                tt / m,
5560                du / m,
5561                dt / m,
5562                self.dspark_draft_ns as f64 / self.dspark_exp.len().max(1) as f64 / 1e6
5563            );
5564        }
5565    }
5566
5567    fn forward_layers_upto(
5568        &mut self,
5569        hidden: &[f32],
5570        position: usize,
5571        task_mask: Option<&TaskMask>,
5572        upto: Option<usize>,
5573    ) -> Vec<f32> {
5574        // In-process multi-GPU: each segment runs pinned to its card,
5575        // and the only thing crossing the boundary is one hidden vector
5576        // that never leaves this address space. Same layer split the
5577        // network mode does, minus the second process, the socket, the
5578        // serialization and the dir_hash handshake.
5579        if let Some(plan) = self.gpu_plan.clone() {
5580            if upto.is_none() && plan.len() > 1 {
5581                let mut h = hidden.to_vec();
5582                for &(dev, from, upto_incl) in plan.iter() {
5583                    h = crate::gpu::with_device(dev, || {
5584                        self.forward_layers_span(&h, position, task_mask, from, Some(upto_incl))
5585                    });
5586                }
5587                return h;
5588            }
5589        }
5590        self.forward_layers_span(hidden, position, task_mask, 0, upto)
5591    }
5592
5593    /// Split this pipeline's layer stack across local GPUs: segment i
5594    /// runs on `devices[i]`. Contiguous and even by layer count — the
5595    /// VRAM-weighted planner is the next step, and an uneven card pair
5596    /// is why it will be needed. `None` clears the plan.
5597    pub fn set_gpu_plan(&mut self, devices: Option<&[usize]>) -> Result<(), String> {
5598        self.set_gpu_plan_at(devices, None)
5599    }
5600
5601    /// The same, with an explicit first boundary (`--peer-split`): card
5602    /// 0 takes layers `[0..at)`, the rest split what remains. Uneven
5603    /// cards, or an attention-heavy head, are why this knob exists.
5604    pub fn set_gpu_plan_at(
5605        &mut self,
5606        devices: Option<&[usize]>,
5607        at: Option<usize>,
5608    ) -> Result<(), String> {
5609        let Some(devs) = devices.filter(|d| d.len() > 1) else {
5610            self.gpu_plan = None;
5611            return Ok(());
5612        };
5613        self.split_supported()?;
5614        let n = self.num_layers;
5615        if devs.len() > n {
5616            return Err(format!("{} devices for {n} layers", devs.len()));
5617        }
5618        if let Some(k) = at {
5619            if k == 0 || k >= n {
5620                return Err(format!("split at {k}: the model has {n} layers"));
5621            }
5622            if devs.len() == 2 {
5623                self.gpu_plan = Some(std::sync::Arc::new(vec![
5624                    (devs[0], 0, k - 1),
5625                    (devs[1], k, n - 1),
5626                ]));
5627                return Ok(());
5628            }
5629            return Err(format!(
5630                "an explicit split point takes exactly 2 devices, got {}",
5631                devs.len()
5632            ));
5633        }
5634        let per = n.div_ceil(devs.len());
5635        let mut plan = Vec::with_capacity(devs.len());
5636        let mut from = 0usize;
5637        for &d in devs {
5638            if from >= n {
5639                break;
5640            }
5641            let upto = (from + per - 1).min(n - 1);
5642            plan.push((d, from, upto));
5643            from = upto + 1;
5644        }
5645        self.gpu_plan = Some(std::sync::Arc::new(plan));
5646        Ok(())
5647    }
5648
5649    /// The active in-process split, if any: (device, first layer, last).
5650    pub fn gpu_plan(&self) -> Option<Vec<(usize, usize, usize)>> {
5651        self.gpu_plan.as_ref().map(|p| p.as_ref().clone())
5652    }
5653
5654    /// Layer span [from ..= upto] (upto None = last layer): the building
5655    /// block the network pipeline-split rides on. `from > 0` skips the
5656    /// arch escape hatches (the pub `forward_span` refuses those archs
5657    /// first) and the whole-token graph — the plain per-layer loop is
5658    /// the canonical executor for a partial stack.
5659    fn forward_layers_span(
5660        &mut self,
5661        hidden: &[f32],
5662        position: usize,
5663        task_mask: Option<&TaskMask>,
5664        from: usize,
5665        upto: Option<usize>,
5666    ) -> Vec<f32> {
5667        debug_assert!(from == 0 || (self.dsv4.is_none() && self.g3n.is_none()));
5668        // DeepSeek-V4 runs its own stack: the state is hc_mult copies, and
5669        // the forward returns LOGITS, not a hidden — the head is inside it
5670        // (the final fold sits between the last layer and the norm). The
5671        // token id rides in `hidden[0]`, written by embed_single, because
5672        // the hash layers route by id rather than by content.
5673        if let Some(b) = &mut self.dsv4 {
5674            let _ = (task_mask, upto);
5675            let token_id = hidden.first().copied().unwrap_or(0.0) as u32;
5676            let (g, layers, cfg, st) = (&b.0, &b.1, b.2, &mut b.3);
5677            st.pos = position;
5678            let mut logits = Vec::new();
5679            crate::dsv4::forward_token(
5680                g,
5681                layers,
5682                &cfg,
5683                st,
5684                token_id,
5685                &self.inv_freq,
5686                self.pool.as_deref(),
5687                &mut logits,
5688            );
5689            self.graph_logits = Some(logits);
5690            self.dspark_probe(position, token_id);
5691            // The caller expects a hidden; the logits went out of band, as
5692            // with the fused lm_head path.
5693            return vec![0.0; self.hidden_size];
5694        }
5695        // Gemma-3n runs its own stack (4 AltUp replicas don't fit this
5696        // loop); `hidden` is the extended embedding from embed_single.
5697        if let Some(b) = &self.g3n {
5698            let _ = (task_mask, upto);
5699            return crate::g3n::g3n_forward(
5700                &b.0,
5701                &b.1,
5702                hidden,
5703                position,
5704                &mut self.kv_cache.layers,
5705                self.num_heads,
5706                self.num_kv_heads,
5707                self.head_dim,
5708                self.pool.as_deref(),
5709            );
5710        }
5711        let mut h = hidden.to_vec();
5712        // Split borrows: copy scalars / clone handles so the per-layer
5713        // cfg does not hold `&self` while the KV cache is `&mut`.
5714        let (nh, _nkv, _hd, hs, _rd, eps) = (
5715            self.num_heads,
5716            self.num_kv_heads,
5717            self.head_dim,
5718            self.hidden_size,
5719            self.rotary_dim,
5720            self.rms_eps,
5721        );
5722        let pool = self.pool.clone();
5723        // Opt-in wgpu token-graph attention (discrete Vulkan/DX12): the whole
5724        // attention sub-block runs resident in one submit. Off by default.
5725        // Whole-token wgpu graph: eligibility + arbitration.
5726        //  - explicit CMF_GPU_WGPU_GRAPH forces it on/off;
5727        //  - discrete adapters (4090: decode 76 -> 137 tok/s) and GDN
5728        //    hybrids (recurrent state device-resident, no CPU twin to
5729        //    race) TRUST it;
5730        //  - integrated/mobile adapters RACE it against the normal path
5731        //    at generation granularity (gpu::graph_race_*) — tiled
5732        //    mobile GPUs can turn the ~300-dispatch graph into seconds
5733        //    per token, while a fast phone GPU keeps its win.
5734        let graph_env = std::env::var("CMF_GPU_WGPU_GRAPH").ok();
5735        let graph_on = match graph_env.as_deref() {
5736            Some("0") => false,
5737            Some(_) => true,
5738            // Unset: same discrete-only default as every other graph
5739            // site. "Is the GPU on" used to stand in here — which made
5740            // the 0.2 tok/s whole-token graph race-eligible on mobile
5741            // adapters and cost 12-14× on first tokens (cmfmobile
5742            // TUNING.md); integrated GPUs keep the per-op probe path.
5743            None => crate::gpu::wgpu_graph_default(),
5744        };
5745        let graph_trusted =
5746            graph_env.is_some() || crate::gpu::wgpu_graph_default() || self.gdn_cfg.is_some();
5747        let race_eligible = graph_on && upto.is_none() && task_mask.is_none() && from == 0;
5748        let mut tail_start = 0usize;
5749        if race_eligible && crate::gpu::graph_race_use_graph(graph_trusted) {
5750            let t_graph = std::time::Instant::now();
5751            let mut lg = Vec::new();
5752            let mut gl = 0usize;
5753            let built = self.try_token_graph_wgpu(hidden, position, &mut lg, &mut gl);
5754            graph_note(built.is_some());
5755            if let Some(hh) = built {
5756                let dur = t_graph.elapsed();
5757                if std::env::var("CMF_GRAPH_PROF").is_ok() {
5758                    eprintln!("graph-call: {:.2} ms total", dur.as_secs_f64() * 1000.0);
5759                }
5760                if gl > 0 && gl < self.num_layers {
5761                    // Device prefix: the graph ran layers 0..gl and handed
5762                    // back the boundary hidden — the loop below owns the
5763                    // tail. The prefix layers' KV/state advanced on the
5764                    // device; the tail's advances on the host below. One
5765                    // boundary crossing per token.
5766                    h = hh;
5767                    tail_start = gl;
5768                } else if graph_trusted || !crate::gpu::graph_race_first_token_hopeless(dur) {
5769                    if !graph_trusted {
5770                        crate::gpu::graph_race_record(true, dur);
5771                    }
5772                    if !lg.is_empty() {
5773                        // Graph produced logits (final-norm + lm_head folded in) —
5774                        // pad/cap to vocab and hand them to the sampler directly.
5775                        lg.resize(self.vocab_size, 0.0);
5776                        if let Some(c) = self.final_softcap {
5777                            for l in lg.iter_mut() {
5778                                *l = c * (*l / c).tanh();
5779                            }
5780                        }
5781                        self.graph_logits = Some(lg);
5782                    }
5783                    return hh;
5784                }
5785                // Hopeless first graph token: discard it and fall through
5786                // to the normal path. Safe exactly here — the prompt KV is
5787                // still CPU-owned (chunked prefill), so recomputing this
5788                // position is exact; the mirror's extra row is never read
5789                // (the race just settled on the normal path).
5790            }
5791        }
5792        // KIMI-LINEAR HAS NO SPLIT BUG. The 2.6× reported from the
5793        // model rotation (12.2 tok/s on one card against 4.6 on two)
5794        // was a single measurement of a model whose arm arbitration is
5795        // borderline, and it did not survive repetition. Three runs an
5796        // arm, same binary, back to back:
5797        //   probe on : 1 GPU 9.5 / 5.7 / 5.9   2 GPU 7.8 / 13.0 / 13.3
5798        //   pinned   : 1 GPU 5.6 / 5.3 / 5.2   2 GPU 3.5 / 4.2 / 3.4
5799        // With the arms pinned the split costs about 1.45×, which is
5800        // what a layer split costs. With the probe free, TWO CARDS RUN
5801        // FASTER — because for this model the CPU arm wins some op
5802        // classes and the probe finds that.
5803        //
5804        // Two things do stand, and both are measured. The token graph
5805        // builds NOTHING here (`covered 0 of 14 layers [0..14)`), so
5806        // every layer walks per-op on either arm — that is where the
5807        // headroom is, not in the split. And this model's benchmark is
5808        // unusable without `CMF_GPU_PROBE=0`: the arbitration alone
5809        // moves it by more than 2×.
5810        //
5811        // Span runs (network split): the graph covers exactly [from..=upto]
5812        // — one submit per SEGMENT per token. No race: its state is global
5813        // and calibrated on full stacks, so spans take the graph only where
5814        // it is trusted by default (discrete adapters / CMF_GPU_WGPU_GRAPH).
5815        let span = from > 0 || upto.is_some();
5816        if span && graph_on && task_mask.is_none() && graph_trusted {
5817            let upto_excl = upto.map_or(self.num_layers, |u| u + 1);
5818            let mut lg = Vec::new();
5819            let mut gl = 0usize;
5820            let span_res =
5821                self.try_token_graph_wgpu_span(hidden, position, &mut lg, from, upto_excl, &mut gl);
5822            graph_note(span_res.is_some() && gl == upto_excl - from);
5823            if std::env::var("CMF_GPU_DEBUG").is_ok() {
5824                // How much of the span the graph actually covered. A
5825                // prefix of nothing means every layer walks per-op and
5826                // the split's extra cost is elsewhere.
5827                static SEEN: std::sync::atomic::AtomicU32 =
5828                    std::sync::atomic::AtomicU32::new(0);
5829                if SEEN.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 4 {
5830                    eprintln!(
5831                        "span graph: covered {gl} of {} layers [{from}..{upto_excl}) res={}",
5832                        upto_excl - from,
5833                        span_res.is_some()
5834                    );
5835                }
5836            }
5837            if let Some(hh) = span_res {
5838                if gl == upto_excl - from {
5839                    if !lg.is_empty() {
5840                        lg.resize(self.vocab_size, 0.0);
5841                        if let Some(c) = self.final_softcap {
5842                            for l in lg.iter_mut() {
5843                                *l = c * (*l / c).tanh();
5844                            }
5845                        }
5846                        self.graph_logits = Some(lg);
5847                    }
5848                    crate::gpu::set_layer(-1);
5849                    return hh;
5850                }
5851                // Partial device prefix of the span: CPU owns the tail.
5852                h = hh;
5853                tail_start = from + gl;
5854            }
5855        }
5856        let t_race_cpu = (race_eligible && !graph_trusted).then(std::time::Instant::now);
5857
5858        #[cfg(target_os = "macos")]
5859        let mut gpu_skip_until = 0usize;
5860        for li in tail_start.max(from)..self.num_layers {
5861            crate::gpu::set_layer(li as i64); // layer-split GPU/CPU (CMF_GPU_LAYERS)
5862            if let Some(u) = upto {
5863                if li > u {
5864                    break;
5865                }
5866            }
5867            if let Some(mask) = task_mask {
5868                if !mask.layer_alive(li) {
5869                    continue; // dead layer: residual pass-through
5870                }
5871            }
5872            // Whole-block q1 token graph: a run of consecutive q1
5873            // layers — GDN and full attention — executes with one sync
5874            // per CPU attend instead of per op (macOS/Metal).
5875            #[cfg(target_os = "macos")]
5876            {
5877                if li < gpu_skip_until {
5878                    continue;
5879                }
5880                if task_mask.is_none() {
5881                    let end = self.q1_graph_gpu(li, upto, position, &mut h);
5882                    if end > li {
5883                        gpu_skip_until = end;
5884                        // Looped Transformer: the graph stopped at a loop
5885                        // boundary — apply final norm before the next iteration.
5886                        if self.is_loop_end(end - 1) && end < self.num_layers {
5887                            h = inference::rms_norm(
5888                                &h,
5889                                &self.weights.final_norm,
5890                                self.rms_eps,
5891                                self.norm_style,
5892                            );
5893                        }
5894                        continue;
5895                    }
5896                }
5897            }
5898
5899            let lw = &self.weights.layers[self.phys_layer(li)];
5900            if let Ok(tp) = std::env::var("CMF_TRACE_POS") {
5901                if tp.parse::<usize>().ok() == Some(position) {
5902                    let n: f32 = h.iter().map(|x| x * x).sum::<f32>().sqrt();
5903                    eprintln!(
5904                        "TRACE pos {position} layer {li}: |h| = {n:.6} h0 {:.6} h1 {:.6}",
5905                        h[0], h[1]
5906                    );
5907                }
5908            }
5909            // Norm into the pipeline scratch — the returning rms_norm
5910            // allocated twice per layer per token (roadmap §3 P0).
5911            inference::rms_norm_into(
5912                &h,
5913                &lw.input_norm,
5914                self.rms_eps,
5915                self.norm_style,
5916                &mut self.ws.n1,
5917            );
5918
5919            let attn_out = match &lw.attn {
5920                AttnKind::Mla(w) => {
5921                    let inv_freq_l = self.layer_inv_freq(li);
5922                    let rs = self.layer_rope_scale(li);
5923                    let eps = self.rms_eps;
5924                    let pool = self.pool.clone();
5925                    mla_attention(
5926                        w,
5927                        &self.ws.n1,
5928                        &mut self.kv_cache.layers[li],
5929                        position,
5930                        &inv_freq_l,
5931                        rs,
5932                        eps,
5933                        pool.as_deref(),
5934                    )
5935                }
5936                AttnKind::Linear(w) => {
5937                    let cfg = self.vmf_cfg.expect("linear layer without vmf_cfg");
5938                    vmf_phase_forward(
5939                        &self.ws.n1,
5940                        w,
5941                        &cfg,
5942                        &mut self.kv_cache.layers[li].linear_state,
5943                        self.pool.as_deref(),
5944                    )
5945                }
5946                AttnKind::Kda(w) => {
5947                    let cfg = self.kda_cfg.expect("kda layer without kda_cfg");
5948                    crate::linear_core::kda_forward(
5949                        &self.ws.n1,
5950                        w,
5951                        &cfg,
5952                        &mut self.kv_cache.layers[li].linear_state,
5953                        self.pool.as_deref(),
5954                    )
5955                }
5956                AttnKind::LinearGdn(w) => {
5957                    let cfg = self.gdn_cfg.expect("gdn layer without gdn_cfg");
5958                    gdn_forward(
5959                        &self.ws.n1,
5960                        w,
5961                        &cfg,
5962                        &mut self.kv_cache.layers[li].linear_state,
5963                        self.pool.as_deref(),
5964                    )
5965                }
5966                AttnKind::ShortConv(w) => {
5967                    let cfg = self
5968                        .short_conv_cfg
5969                        .expect("short-conv layer without short_conv_cfg");
5970                    short_conv_forward(
5971                        &self.ws.n1,
5972                        w,
5973                        &cfg,
5974                        &mut self.kv_cache.layers[li].linear_state,
5975                        self.pool.as_deref(),
5976                    )
5977                }
5978                AttnKind::Full {
5979                    wq,
5980                    wk,
5981                    wv,
5982                    wo,
5983                    q_norm,
5984                    k_norm,
5985                    output_gate,
5986                    softplus_gate,
5987                    bias,
5988                } if self.kv_cache.layers[li].o1_sealed() => {
5989                    // O(1) override: decode on the sealed Nyström state
5990                    // instead of the growing KV cache.
5991                    let inv_freq_l = self.layer_inv_freq(li);
5992                    let (nkv_l, hd_l, rd_l) = self.layer_geom(li);
5993                    let cfg = QwenAttnCfg {
5994                        num_heads: self.layer_num_heads(li),
5995                        num_kv_heads: nkv_l,
5996                        head_dim: hd_l,
5997                        hidden_size: hs,
5998                        position,
5999                        inv_freq: &inv_freq_l,
6000                        rotary_dim: rd_l,
6001                        scale: self.attn_scale,
6002                        softcap: self.attn_softcap,
6003                        window: None,
6004                        v_norm: self.attn_v_norm,
6005                        q_norm: q_norm.as_deref(),
6006                        k_norm: k_norm.as_deref(),
6007                        output_gate: *output_gate,
6008                        softplus_gate: softplus_gate
6009                            .as_ref()
6010                            .map(|(gate, per_head)| (gate, *per_head)),
6011                        rope_scale: self.layer_rope_scale(li),
6012                        bias: bias
6013                            .as_ref()
6014                            .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
6015                        rms_eps: eps,
6016                        norm_style: self.norm_style,
6017                        pool: pool.as_deref(),
6018                    };
6019                    attention::qwen_attention_nystrom(
6020                        &self.ws.n1,
6021                        wq,
6022                        wk,
6023                        wv,
6024                        wo,
6025                        &mut self.kv_cache.layers[li],
6026                        &cfg,
6027                    )
6028                }
6029                AttnKind::Full {
6030                    wq,
6031                    wk,
6032                    wv,
6033                    wo,
6034                    q_norm,
6035                    k_norm,
6036                    output_gate,
6037                    softplus_gate,
6038                    bias,
6039                } => 'attn: {
6040                    // wgpu token-graph attention (opt-in): whole sub-block in
6041                    // one submit, device K/V mirror. q1 only, no gate/bias/mask.
6042                    if graph_on
6043                        && !*output_gate
6044                        && softplus_gate.is_none()
6045                        && self.attention_heads_per_layer.is_none()
6046                        && bias.is_none()
6047                        && task_mask.is_none()
6048                    {
6049                        let inv_freq_l = self.layer_inv_freq(li);
6050                        let (nkv_l, hd_l, rd_l) = self.layer_geom(li);
6051                        let gemma = self.norm_style == cortiq_core::NormStyle::Gemma;
6052                        if let (Some((gm, qi)), Some((_, ki)), Some((_, vi)), Some((_, oi))) = (
6053                            wq.mapped_q1(),
6054                            wk.mapped_q1(),
6055                            wv.mapped_q1(),
6056                            wo.mapped_q1(),
6057                        ) {
6058                            let gm = gm.clone();
6059                            let mut out = vec![0f32; hs];
6060                            let cache = &self.kv_cache.layers[li];
6061                            if crate::gpu::attn_dropin(
6062                                &gm,
6063                                self.graph_kv_id,
6064                                li,
6065                                &self.ws.n1,
6066                                qi,
6067                                ki,
6068                                vi,
6069                                oi,
6070                                q_norm.as_deref(),
6071                                k_norm.as_deref(),
6072                                &inv_freq_l,
6073                                nh,
6074                                nkv_l,
6075                                hd_l,
6076                                rd_l,
6077                                hs,
6078                                position,
6079                                self.kv_cache.max_seq_len,
6080                                gemma,
6081                                eps as f32,
6082                                cache.k_heads(),
6083                                cache.v_heads(),
6084                                &mut out,
6085                            ) {
6086                                break 'attn out;
6087                            }
6088                        }
6089                    }
6090                    let masked = task_mask
6091                        .map(|m| m.head_flags(li, self.num_heads).iter().any(|&a| !a))
6092                        .unwrap_or(false);
6093                    let f32_view = (wq.as_f32(), wk.as_f32(), wv.as_f32(), wo.as_f32());
6094                    match (masked, f32_view) {
6095                        // Historical masked path (f32 slices; the loader
6096                        // keeps masked models in f32).
6097                        (true, (Some(q), Some(k), Some(v), Some(o))) => {
6098                            let active_heads = task_mask.unwrap().head_flags(li, self.num_heads);
6099                            attention::multi_head_attention(
6100                                &self.ws.n1,
6101                                q,
6102                                k,
6103                                v,
6104                                o,
6105                                &mut self.kv_cache.layers[li],
6106                                self.num_heads,
6107                                self.num_kv_heads,
6108                                self.head_dim,
6109                                self.hidden_size,
6110                                position,
6111                                &active_heads,
6112                                &self.inv_freq,
6113                            )
6114                        }
6115                        (masked, _) => {
6116                            if masked {
6117                                tracing::warn!(
6118                                    "layer {li}: head mask on quantized weights not \
6119                                     supported yet — executing dense"
6120                                );
6121                            }
6122                            let inv_freq_l = self.layer_inv_freq(li);
6123                            let (nkv_l, hd_l, rd_l) = self.layer_geom(li);
6124                            let cfg = QwenAttnCfg {
6125                                num_heads: self.layer_num_heads(li),
6126                                num_kv_heads: nkv_l,
6127                                head_dim: hd_l,
6128                                hidden_size: hs,
6129                                position,
6130                                inv_freq: &inv_freq_l,
6131                                rotary_dim: rd_l,
6132                                scale: self.attn_scale,
6133                                softcap: self.attn_softcap,
6134                                window: self.layer_window(li),
6135                                v_norm: self.attn_v_norm,
6136                                q_norm: q_norm.as_deref(),
6137                                k_norm: k_norm.as_deref(),
6138                                output_gate: *output_gate,
6139                                softplus_gate: softplus_gate
6140                                    .as_ref()
6141                                    .map(|(gate, per_head)| (gate, *per_head)),
6142                                rope_scale: self.layer_rope_scale(li),
6143                                bias: bias
6144                                    .as_ref()
6145                                    .map(|(a, b, c)| (a.as_slice(), b.as_slice(), c.as_slice())),
6146                                rms_eps: eps,
6147                                norm_style: self.norm_style,
6148                                pool: pool.as_deref(),
6149                            };
6150                            attention::qwen_attention(
6151                                &self.ws.n1,
6152                                wq,
6153                                wk,
6154                                wv,
6155                                wo,
6156                                &mut self.kv_cache.layers[li],
6157                                &cfg,
6158                            )
6159                        }
6160                    }
6161                }
6162            };
6163            // Gemma sandwich norm: normalize the attention branch before
6164            // it joins the residual stream.
6165            let attn_out = match &self.weights.layers[self.phys_layer(li)].attn_out_norm {
6166                Some(w) => inference::rms_norm(&attn_out, w, self.rms_eps, self.norm_style),
6167                None => attn_out,
6168            };
6169            let lw = &self.weights.layers[self.phys_layer(li)];
6170            inference::add_rmsnorm_fused_into(
6171                &mut h,
6172                &attn_out,
6173                &lw.post_norm,
6174                self.rms_eps,
6175                self.norm_style,
6176                &mut self.ws.p1,
6177            );
6178            let mut attn_out = attn_out;
6179            attention::recycle_buf(&mut attn_out);
6180            let post_normed = &self.ws.p1;
6181
6182            let ffn_masked = task_mask
6183                .map(|m| m.ffn_active_count(li) < self.intermediate_size)
6184                .unwrap_or(false);
6185            // One masked dense CONTRACT, dispatched by cost. The
6186            // activation-zeroing arm (the batched sweep's, validated
6187            // against the replica to 0.8%) computes the FULL fused FFN
6188            // and zeroes the dead — right whenever most neurons live.
6189            // The sparse arm reads ONLY active rows and down columns —
6190            // per-row dots are slower per element than the fused kernel,
6191            // so it pays only once the mask is deep enough. The 0.5
6192            // crossover is first-principles (fused kernels run ~2x the
6193            // per-row dot throughput); a shallow specialist (95% alive)
6194            // stays fused, a --target-sparsity bake flips arms on its
6195            // own weight.
6196            let ffn_out = match (ffn_masked, &lw.ffn) {
6197                (true, FfnKind::Dense(d)) => {
6198                    let tm = task_mask.unwrap();
6199                    let alive = tm.ffn_active_count(li);
6200                    let deep = alive * 2 <= self.intermediate_size;
6201                    if deep && d.down_proj.sparse_col_ok() {
6202                        let active = tm.ffn_active_indices(li);
6203                        sparse_ffn_quant(
6204                            d,
6205                            post_normed,
6206                            &active,
6207                            self.hidden_size,
6208                            self.pool.as_deref(),
6209                        )
6210                    } else if deep
6211                        && let (Some(g), Some(u), Some(dn)) = (
6212                            d.gate_proj.as_f32(),
6213                            d.up_proj.as_f32(),
6214                            d.down_proj.as_f32(),
6215                        )
6216                    {
6217                        let active = tm.ffn_active_indices(li);
6218                        inference::sparse_ffn_forward(
6219                            post_normed,
6220                            g,
6221                            u,
6222                            dn,
6223                            self.hidden_size,
6224                            self.intermediate_size,
6225                            &active,
6226                            self.pool.as_deref(),
6227                        )
6228                    } else {
6229                        let row = tm.ffn_masks.get(li).map(|v| v.as_slice());
6230                        dense_ffn_batch(d, post_normed, 1, self.pool.as_deref(), row)
6231                    }
6232                }
6233                (true, FfnKind::Moe(m)) => {
6234                    // MoE is sparse by expert selection; a task mask
6235                    // narrows the ROUTABLE set via its expert fields
6236                    // (spec §5) when it carries them.
6237                    let allowed = task_mask.and_then(|tm| tm.expert_flags(li, m.experts.len()));
6238                    ffn_forward(
6239                        &lw.ffn,
6240                        post_normed,
6241                        self.pool.as_deref(),
6242                        allowed.as_deref(),
6243                    )
6244                }
6245                (true, FfnKind::DenseMoe(dm)) => dense_moe_ffn(
6246                    dm,
6247                    post_normed,
6248                    &h,
6249                    self.rms_eps,
6250                    self.norm_style,
6251                    self.pool.as_deref(),
6252                ),
6253                (false, _) => match &lw.ffn {
6254                    FfnKind::DenseMoe(dm) => dense_moe_ffn(
6255                        dm,
6256                        post_normed,
6257                        &h,
6258                        self.rms_eps,
6259                        self.norm_style,
6260                        self.pool.as_deref(),
6261                    ),
6262                    _ => {
6263                        let allowed = match (&lw.ffn, task_mask) {
6264                            (FfnKind::Moe(m), Some(tm)) => tm.expert_flags(li, m.experts.len()),
6265                            _ => None,
6266                        };
6267                        ffn_forward(
6268                            &lw.ffn,
6269                            post_normed,
6270                            self.pool.as_deref(),
6271                            allowed.as_deref(),
6272                        )
6273                    }
6274                },
6275            };
6276            let ffn_out = match &self.weights.layers[self.phys_layer(li)].ffn_out_norm {
6277                Some(w) => inference::rms_norm(&ffn_out, w, self.rms_eps, self.norm_style),
6278                None => ffn_out,
6279            };
6280            for (i, &f) in ffn_out.iter().enumerate() {
6281                h[i] += f;
6282            }
6283            let mut ffn_out = ffn_out;
6284            attention::recycle_buf(&mut ffn_out);
6285
6286            // Gemma-4: the layer output is scaled by a learned scalar.
6287            if let Some(sc) = self.weights.layers[self.phys_layer(li)].layer_scale {
6288                for v in h.iter_mut() {
6289                    *v *= sc;
6290                }
6291            }
6292
6293            // Looped Transformer: apply final norm at the end of each loop iteration.
6294            // Nanbeige 4.2: after layer 21 (virtual), apply norm before looping back to layer 0.
6295            if self.is_loop_end(li) && li + 1 < self.num_layers {
6296                h = inference::rms_norm(
6297                    &h,
6298                    &self.weights.final_norm,
6299                    self.rms_eps,
6300                    self.norm_style,
6301                );
6302            }
6303
6304            // Dynamic routing φ capture (on-policy, fireball-style): the
6305            // EMA of the post-residual hidden at the router's phi_layer,
6306            // updated as the context evolves during decode.
6307            if self.dyn_phi_layer == Some(li) {
6308                self.update_dyn_phi(&h);
6309            }
6310        }
6311        crate::gpu::set_layer(-1); // layers done — lm_head outside layer-split
6312        if let Some(t) = t_race_cpu {
6313            crate::gpu::graph_race_record(false, t.elapsed());
6314        }
6315
6316        h
6317    }
6318
6319    /// EMA of φ at the router layer (rolling, weight 0.2 = ~5-token
6320    /// horizon). First observation seeds it exactly.
6321    fn update_dyn_phi(&mut self, h: &[f32]) {
6322        const A: f32 = 0.2;
6323        if self.dyn_phi_ema.len() != h.len() {
6324            self.dyn_phi_ema = vec![0.0; h.len()];
6325            self.dyn_phi_seen = 0;
6326        }
6327        if self.dyn_phi_seen == 0 {
6328            self.dyn_phi_ema.copy_from_slice(h);
6329        } else {
6330            for (e, &v) in self.dyn_phi_ema.iter_mut().zip(h) {
6331                *e = (1.0 - A) * *e + A * v;
6332            }
6333        }
6334        self.dyn_phi_seen += 1;
6335    }
6336
6337    /// Current router φ (EMA at phi_layer); empty until first capture.
6338    pub fn dyn_phi(&self) -> &[f32] {
6339        &self.dyn_phi_ema
6340    }
6341
6342    /// Enable/disable φ capture at the router layer, reset the EMA.
6343    pub fn set_dyn_phi_layer(&mut self, layer: Option<usize>) {
6344        self.dyn_phi_layer = layer;
6345        self.dyn_phi_ema.clear();
6346        self.dyn_phi_seen = 0;
6347    }
6348
6349    /// Skills eligible for dynamic switching: (index, id, phi_layer).
6350    pub fn dynamic_skills(&self) -> Vec<(usize, String, usize)> {
6351        let Some(model) = &self.model else {
6352            return Vec::new();
6353        };
6354        model
6355            .header
6356            .skills
6357            .iter()
6358            .enumerate()
6359            .filter_map(|(i, sk)| {
6360                let ok = matches!(self.dyn_skill_layers.get(i), Some(Some(_)));
6361                let sel = sk.selection.as_ref()?;
6362                (ok).then(|| (i, sk.id.clone(), sel.phi_layer))
6363            })
6364            .collect()
6365    }
6366
6367    /// Index of the currently overlaid skill (None = backbone).
6368    pub fn active_skill(&self) -> Option<usize> {
6369        self.dyn_active
6370    }
6371
6372    /// Enable dynamic per-token skill routing: build the hysteresis
6373    /// router from the container's routable skills, start φ capture at
6374    /// their (shared) phi_layer. Returns the number of routable skills
6375    /// (0 = nothing to route; router stays off). Idempotent.
6376    pub fn enable_dynamic_routing(&mut self) -> usize {
6377        use crate::swarm::{DynRouter, RoutableSkill};
6378        let Some(model) = self.model.clone() else {
6379            return 0;
6380        };
6381        // A blend materialized f32 working tensors into the layers; there
6382        // is no single skill index to revert from → refuse (honest).
6383        if self.dyn_blend_loaded {
6384            tracing::warn!("dynamic routing unavailable on a blend-loaded pipeline");
6385            return 0;
6386        }
6387        // A statically-overlaid skill that is NOT FFN-eligible can't be
6388        // cheaply reverted at generation start → refuse rather than
6389        // silently keep it overlaid.
6390        if let Some(a) = self.dyn_active {
6391            if !matches!(self.dyn_skill_layers.get(a), Some(Some(_))) {
6392                tracing::warn!("loaded skill is not FFN-eligible — dynamic routing unavailable");
6393                return 0;
6394            }
6395        }
6396        let hidden = self.hidden_size;
6397        let mut skills = Vec::new();
6398        for (idx, id, _phi) in self.dynamic_skills() {
6399            if let Some(sel) = model.header.skills[idx].selection.as_ref() {
6400                if let Some(rs) = RoutableSkill::from_descriptor(idx, id, sel, hidden) {
6401                    skills.push(rs);
6402                }
6403            }
6404        }
6405        if skills.is_empty() {
6406            return 0;
6407        }
6408        // Skills should share a phi_layer; warn (not fail) if they don't.
6409        let phi = skills[0].phi_layer;
6410        if skills.iter().any(|s| s.phi_layer != phi) {
6411            tracing::warn!("routable skills disagree on phi_layer; using {phi}");
6412        }
6413        let n = skills.len();
6414        self.set_dyn_phi_layer(Some(phi));
6415        self.dyn_router = Some(DynRouter::new(skills));
6416        n
6417    }
6418
6419    /// Human-readable switch log from the last dynamic-routed generation.
6420    pub fn route_switches(&self) -> Vec<(usize, Option<String>, Option<String>)> {
6421        self.dyn_router
6422            .as_ref()
6423            .map(|r| r.switches.clone())
6424            .unwrap_or_default()
6425    }
6426
6427    /// LM head: hidden → logits [vocab_size]. The dominant matvec of
6428    /// every decode step — row-parallel on the worker pool.
6429    fn lm_head_forward(&self, hidden: &[f32]) -> Vec<f32> {
6430        let rows = self.weights.lm_head.rows();
6431        let mut logits = attention::take_buf(rows.min(self.vocab_size));
6432        self.weights
6433            .lm_head
6434            .matvec(hidden, &mut logits, self.pool.as_deref());
6435        logits.resize(self.vocab_size, 0.0);
6436        if let Some(m) = self.logit_multiplier {
6437            for l in logits.iter_mut() {
6438                *l *= m;
6439            }
6440        }
6441        if let Some(c) = self.final_softcap {
6442            for l in logits.iter_mut() {
6443                *l = c * (*l / c).tanh();
6444            }
6445        }
6446        logits
6447    }
6448
6449    /// Prefill `ids` and return the next-token logits — what the model
6450    /// would predict next, WITHOUT committing to generation (introspection
6451    /// for `cortiq explain`). Clears and repopulates the KV cache; leaves
6452    /// the active overlay untouched.
6453    pub fn prefill_next_logits(&mut self, ids: &[u32], task_mask: Option<&TaskMask>) -> Vec<f32> {
6454        self.kv_cache.clear();
6455        self.kv_history.clear();
6456        let mut hidden = vec![0.0f32; self.hidden_size];
6457        for (pos, &id) in ids.iter().enumerate() {
6458            let emb = self.embed_single(id);
6459            hidden = self.forward_layers(&emb, pos, task_mask);
6460        }
6461        inference::rms_norm_into(
6462            &hidden,
6463            &self.weights.final_norm,
6464            self.rms_eps,
6465            self.norm_style,
6466            &mut self.ws.n1,
6467        );
6468        self.lm_head_forward(&self.ws.n1)
6469    }
6470}
6471
6472/// Convenience: deterministic tiny pipeline for tests.
6473pub fn create_test_pipeline(
6474    hidden_size: usize,
6475    intermediate_size: usize,
6476    num_heads: usize,
6477    num_kv_heads: usize,
6478    head_dim: usize,
6479    num_layers: usize,
6480    vocab_size: usize,
6481) -> Pipeline {
6482    // Small pseudo-random weights: constant weights make attention
6483    // degenerate and hide indexing bugs.
6484    let synth = |n: usize, salt: usize| -> Vec<f32> {
6485        (0..n)
6486            .map(|i| (((i * 31 + salt * 17 + 7) % 97) as f32 / 97.0 - 0.5) * 0.2)
6487            .collect()
6488    };
6489    let qt = |rows: usize, cols: usize, salt: usize| -> QTensor {
6490        QTensor::from_f32(synth(rows * cols, salt), rows, cols)
6491    };
6492    let layer_weights: Vec<LayerWeights> = (0..num_layers)
6493        .map(|li| LayerWeights {
6494            input_norm: vec![1.0; hidden_size],
6495            post_norm: vec![1.0; hidden_size],
6496            attn_out_norm: None,
6497            ffn_out_norm: None,
6498            layer_scale: None,
6499            ffn: FfnKind::Dense(DenseFfn {
6500                gate_proj: qt(intermediate_size, hidden_size, li * 10 + 5),
6501                up_proj: qt(intermediate_size, hidden_size, li * 10 + 6),
6502                down_proj: qt(hidden_size, intermediate_size, li * 10 + 7),
6503                act: Act::Silu,
6504            }),
6505            attn: AttnKind::Full {
6506                bias: None,
6507                wq: qt(num_heads * head_dim, hidden_size, li * 10 + 1),
6508                wk: qt(num_kv_heads * head_dim, hidden_size, li * 10 + 2),
6509                wv: qt(num_kv_heads * head_dim, hidden_size, li * 10 + 3),
6510                wo: qt(hidden_size, num_heads * head_dim, li * 10 + 4),
6511                q_norm: None,
6512                k_norm: None,
6513                output_gate: false,
6514                softplus_gate: None,
6515            },
6516        })
6517        .collect();
6518
6519    Pipeline::new(
6520        Tokenizer::byte_level(),
6521        PipelineWeights {
6522            embed_tokens: qt(vocab_size, hidden_size, 100),
6523            layers: layer_weights,
6524            lm_head: qt(vocab_size, hidden_size, 200),
6525            final_norm: vec![1.0; hidden_size],
6526        },
6527        hidden_size,
6528        intermediate_size,
6529        num_heads,
6530        num_kv_heads,
6531        head_dim,
6532        num_layers,
6533        num_layers, // physical_layers = num_layers (non-looped)
6534        false,      // loop_final_norm
6535        vocab_size,
6536        1e-6,
6537        10_000.0,
6538        NormStyle::Qwen,
6539        4096,
6540        SamplerConfig {
6541            seed: Some(42),
6542            ..Default::default()
6543        },
6544    )
6545}
6546
6547/// Batched dense-FFN: gate/up/down via matmat (element-wise the same
6548/// math as b × dense_ffn — the same dot kernels).
6549/// One mask bit, LSB-first per byte — `TaskMask::ffn_active_indices`'s
6550/// convention.
6551#[inline]
6552fn mask_bit(row: &[u8], j: usize) -> bool {
6553    (row.get(j >> 3).copied().unwrap_or(0) >> (j & 7)) & 1 != 0
6554}
6555
6556/// Zero the CLOSED neurons' activations in a [rows × inter] panel — the
6557/// masked-inference fast path's whole trick: full fused quant compute,
6558/// then the mask lands on the ACTIVATIONS, which is arithmetically the
6559/// pruned network without touching a quantized weight byte. Whole open
6560/// bytes (0xFF = 8 open neurons) skip in one test.
6561fn zero_masked_cols(g: &mut [f32], rows: usize, inter: usize, row: &[u8]) {
6562    for r in 0..rows {
6563        let base = r * inter;
6564        for (bi, &byte) in row.iter().enumerate() {
6565            if byte == 0xFF {
6566                continue;
6567            }
6568            let j0 = bi * 8;
6569            for bit in 0..8 {
6570                let j = j0 + bit;
6571                if j < inter && byte & (1 << bit) == 0 {
6572                    g[base + j] = 0.0;
6573                }
6574            }
6575        }
6576    }
6577}
6578
6579fn dense_ffn_batch(
6580    d: &DenseFfn,
6581    xs: &[f32],
6582    b: usize,
6583    pool: Option<&Pool>,
6584    mask_row: Option<&[u8]>,
6585) -> Vec<f32> {
6586    let inter = d.gate_proj.rows();
6587    let hidden = d.down_proj.rows();
6588    // Fused on-device SwiGLU when the device is in play: three separate
6589    // `matmat` calls are three round trips per layer, and the gate/up
6590    // panels (b × inter — 22 MB each at a 512-token chunk) cross the bus
6591    // twice for nothing. The kernel already existed for the image DiT;
6592    // the LLM prefill was simply never wired to it. A task mask needs the
6593    // activations on the host between the halves, so it keeps the CPU
6594    // arm below.
6595    if mask_row.is_none()
6596        && d.act == Act::Silu
6597        && b >= 32
6598        && crate::gpu::enabled_here()
6599        && !crate::gpu::mm_killed()
6600    {
6601        if let (Some((model, w1)), Some((_, w3)), Some((_, w2))) = (
6602            d.gate_proj.mapped_q4t(),
6603            d.up_proj.mapped_q4t(),
6604            d.down_proj.mapped_q4t(),
6605        ) {
6606            let mut out = vec![0.0f32; b * hidden];
6607            if crate::gpu::q4t_ffn(model, w1, w3, w2, xs, b, hidden, inter, &mut out) {
6608                return out;
6609            }
6610        }
6611        // The q4tp twin (same kernel family, scale from the row ladder) —
6612        // the DiT has run it in production since the pipeline containers;
6613        // the LLM prefill was simply never wired to it, so a q4tp model's
6614        // prefill panels stayed on the CPU.
6615        if let (Some((model, w1)), Some((_, w3)), Some((_, w2))) = (
6616            d.gate_proj.mapped_q4tp(),
6617            d.up_proj.mapped_q4tp(),
6618            d.down_proj.mapped_q4tp(),
6619        ) {
6620            let mut out = vec![0.0f32; b * hidden];
6621            if crate::gpu::q4tp_ffn(model, w1, w3, w2, xs, b, hidden, inter, &mut out) {
6622                return out;
6623            }
6624        }
6625    }
6626    let mut g = vec![0.0f32; b * inter];
6627    d.gate_proj.matmat(xs, b, &mut g, pool);
6628    let mut u = vec![0.0f32; b * inter];
6629    d.up_proj.matmat(xs, b, &mut u, pool);
6630    for i in 0..b * inter {
6631        g[i] = d.act.combine(g[i], u[i]);
6632    }
6633    if let Some(row) = mask_row {
6634        zero_masked_cols(&mut g, b, inter, row);
6635    }
6636    let mut out = vec![0.0f32; b * hidden];
6637    d.down_proj.matmat(&g, b, &mut out, pool);
6638    out
6639}
6640
6641/// Batched MoE-FFN: router batched, positions are GROUPED by expert —
6642/// an expert's weights are read once for all its positions in the chunk
6643/// (the main prefill-GEMM win on MoE: 960MB/token of 35B experts).
6644/// Accumulate per-channel activation energy for `CMF_RMS_TRACE`.
6645fn accumulate_act(m: &MoeFfn, xs: &[f32], b: usize) {
6646    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
6647    static DUMP: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
6648    let on = *ON.get_or_init(|| std::env::var("CMF_RMS_TRACE").is_ok());
6649    let dump = *DUMP.get_or_init(|| std::env::var("CMF_ACT_DUMP").is_ok());
6650    if (!on && !dump) || b == 0 {
6651        return;
6652    }
6653    let hidden = xs.len() / b;
6654    if on {
6655        let mut acc = m.act_sq.borrow_mut();
6656        if acc.len() < hidden {
6657            acc.resize(hidden, 0.0);
6658        }
6659        for t in 0..b {
6660            let row = &xs[t * hidden..(t + 1) * hidden];
6661            for (a, &v) in acc.iter_mut().zip(row) {
6662                *a += (v as f64) * (v as f64);
6663            }
6664        }
6665    }
6666    if dump {
6667        // Cap the capture: the covariance needs a few thousand rows, and a
6668        // whole prefill of every layer would be gigabytes for no extra rank.
6669        let cap: usize = std::env::var("CMF_ACT_DUMP_ROWS")
6670            .ok()
6671            .and_then(|v| v.parse().ok())
6672            .unwrap_or(4096);
6673        let mut rows = m.act_rows.borrow_mut();
6674        if rows.len() < cap * hidden {
6675            let take = b.min((cap * hidden - rows.len()) / hidden.max(1));
6676            rows.extend_from_slice(&xs[..take * hidden]);
6677        }
6678    }
6679}
6680
6681/// Send-able cursor over a Vec-of-Vecs: each pool worker writes only its
6682/// own slots (disjoint by construction in the caller).
6683#[derive(Clone, Copy)]
6684struct SendVecs(*mut Vec<f32>);
6685unsafe impl Send for SendVecs {}
6686unsafe impl Sync for SendVecs {}
6687impl SendVecs {
6688    #[inline]
6689    fn at(self, i: usize) -> *mut Vec<f32> {
6690        unsafe { self.0.add(i) }
6691    }
6692}
6693
6694fn moe_ffn_batch(
6695    m: &MoeFfn,
6696    xs: &[f32],
6697    b: usize,
6698    hidden: usize,
6699    pool: Option<&Pool>,
6700    allowed: Option<&[bool]>,
6701) -> Vec<f32> {
6702    accumulate_act(m, xs, b);
6703    let ne = m.experts.len();
6704    let mut logits = vec![0.0f32; b * ne];
6705    m.router.matmat(xs, b, &mut logits, pool);
6706
6707    // Assignments: expert → [(position, weight)] — same routing as
6708    // moe_ffn, per position (see `moe_route`).
6709    let mut assign: Vec<Vec<(usize, f32)>> = vec![Vec::new(); ne];
6710    {
6711        let mut st = m.stats.borrow_mut();
6712        if st.len() < ne {
6713            st.resize(ne, 0);
6714        }
6715        for bi in 0..b {
6716            let (idx, p, wsum) = moe_route(&logits[bi * ne..(bi + 1) * ne], m, allowed);
6717            for &e in &idx {
6718                st[e] += 1;
6719                assign[e].push((bi, p[e] / wsum));
6720            }
6721        }
6722    }
6723
6724    let mut out = vec![0.0f32; b * hidden];
6725    let cols = m.experts[0].gate_proj.cols();
6726    let run_expert = |d: &DenseFfn, list: &[(usize, f32)], out: &mut [f32]| {
6727        let sb = list.len();
6728        let mut sub = vec![0.0f32; sb * cols];
6729        for (k, &(bi, _)) in list.iter().enumerate() {
6730            sub[k * cols..(k + 1) * cols].copy_from_slice(&xs[bi * cols..(bi + 1) * cols]);
6731        }
6732        let eo = dense_ffn_batch(d, &sub, sb, pool, None);
6733        for (k, &(bi, w)) in list.iter().enumerate() {
6734            for i in 0..hidden {
6735                out[bi * hidden + i] += w * eo[k * hidden + i];
6736            }
6737        }
6738    };
6739    // Routed experts: the panels are TINY (b·top_k spread over every
6740    // expert — a few positions each), so a pool dispatch per expert is
6741    // pure barrier cost. Invert the parallelism: workers take WHOLE
6742    // experts (serial math inside), then one deterministic scatter in
6743    // expert order — the exact accumulation order the serial loop had.
6744    let active: Vec<usize> = (0..ne).filter(|&e| !assign[e].is_empty()).collect();
6745    if pool.is_some() && active.len() >= 8 {
6746        let mut panels: Vec<Vec<f32>> = vec![Vec::new(); active.len()];
6747        {
6748            let panel_ptr = SendVecs(panels.as_mut_ptr());
6749            // Capture only the expert table: `m` itself carries RefCell
6750            // stats and must not cross the pool boundary.
6751            let experts = &m.experts;
6752            let (active_r, assign_r) = (&active, &assign);
6753            let run = |start: usize, end: usize| {
6754                for ai in start..end {
6755                    let e = active_r[ai];
6756                    let list = &assign_r[e];
6757                    let sb = list.len();
6758                    let mut sub = vec![0.0f32; sb * cols];
6759                    for (k, &(bi, _)) in list.iter().enumerate() {
6760                        sub[k * cols..(k + 1) * cols]
6761                            .copy_from_slice(&xs[bi * cols..(bi + 1) * cols]);
6762                    }
6763                    // SAFETY: each worker owns a disjoint panels[ai].
6764                    unsafe {
6765                        *panel_ptr.at(ai) =
6766                            dense_ffn_batch(&experts[e], &sub, sb, None, None);
6767                    }
6768                }
6769            };
6770            match pool {
6771                Some(p) => p.run_rows(active.len(), &run),
6772                None => run(0, active.len()),
6773            }
6774        }
6775        for (ai, &e) in active.iter().enumerate() {
6776            for (k, &(bi, w)) in assign[e].iter().enumerate() {
6777                let eo = &panels[ai][k * hidden..(k + 1) * hidden];
6778                for i in 0..hidden {
6779                    out[bi * hidden + i] += w * eo[i];
6780                }
6781            }
6782        }
6783    } else {
6784        for &e in &active {
6785            run_expert(&m.experts[e], &assign[e], &mut out);
6786        }
6787    }
6788    if let Some((se, gate)) = &m.shared {
6789        let all: Vec<(usize, f32)> = if let Some(gate) = gate {
6790            let mut gl = vec![0.0f32; b];
6791            gate.matmat(xs, b, &mut gl, pool);
6792            (0..b)
6793                .map(|bi| (bi, 1.0 / (1.0 + (-gl[bi]).exp())))
6794                .collect()
6795        } else {
6796            (0..b).map(|bi| (bi, 1.0)).collect()
6797        };
6798        run_expert(se, &all, &mut out);
6799    }
6800    out
6801}
6802
6803thread_local! {
6804    /// gate/up activation scratch for the dense FFN paths (single uses
6805    /// two slots, the fused pair all four) — these were fresh
6806    /// intermediate-size Vecs on every layer of every token.
6807    static FFN_SCRATCH: std::cell::RefCell<[Vec<f32>; 4]> =
6808        const { std::cell::RefCell::new([Vec::new(), Vec::new(), Vec::new(), Vec::new()]) };
6809}
6810
6811/// Dense SwiGLU FFN through QTensor matvecs (any storage).
6812fn dense_ffn(d: &DenseFfn, x: &[f32], pool: Option<&Pool>) -> Vec<f32> {
6813    // Whole-FFN GPU submit (этап 4.2 increment): gate → silu·up → down
6814    // chained in ONE command buffer with the intermediate activations
6815    // resident on the device — 3 per-op polls become 1 per layer. The
6816    // moe_block backend already implements exactly this chain; a dense
6817    // FFN is one expert with weight 1. Runtime probe: the chain still
6818    // pays one submit+poll per layer — alternate it against the pure-CPU
6819    // FFN and keep whichever is faster on this machine.
6820    // q1 FFNs offload at any practical size: the q1 CPU kernel is
6821    // compute-bound, so the UMA threshold logic does not apply — the
6822    // probe measures and decides either way.
6823    if crate::gpu::enabled_here()
6824        && (d.gate_proj.rows() >= crate::gpu::min_rows() || d.gate_proj.is_q1())
6825    {
6826        let arm = if d.gate_proj.is_q1() && crate::gpu::q1_force() {
6827            crate::gpu::ProbeArm::Gpu
6828        } else {
6829            crate::gpu::probe_arm(crate::gpu::OpClass::Ffn)
6830        };
6831        match arm {
6832            crate::gpu::ProbeArm::Gpu => {
6833                let t0 = std::time::Instant::now();
6834                if let Some(out) = dense_ffn_gpu(d, x, pool) {
6835                    crate::gpu::probe_record(crate::gpu::OpClass::Ffn, true, t0.elapsed());
6836                    return out;
6837                }
6838            }
6839            crate::gpu::ProbeArm::CpuTimed => {
6840                let t0 = std::time::Instant::now();
6841                let out = crate::gpu::cpu_scope(|| dense_ffn_cpu(d, x, pool));
6842                crate::gpu::probe_record(crate::gpu::OpClass::Ffn, false, t0.elapsed());
6843                return out;
6844            }
6845            crate::gpu::ProbeArm::Cpu => {
6846                return crate::gpu::cpu_scope(|| dense_ffn_cpu(d, x, pool));
6847            }
6848        }
6849    }
6850    dense_ffn_cpu(d, x, pool)
6851}
6852
6853/// The pure-CPU dense-FFN body (also the fallback of every GPU refusal).
6854fn dense_ffn_cpu(d: &DenseFfn, x: &[f32], pool: Option<&Pool>) -> Vec<f32> {
6855    let inter = d.gate_proj.rows();
6856    FFN_SCRATCH.with(|s| {
6857        let mut s = s.borrow_mut();
6858        let [g, u, ..] = &mut *s;
6859        g.resize(inter, 0.0);
6860        // Fused gate+up+silu: one dispatch, no separate silu pass.
6861        // Falls back to matvec_many + silu loop for unsupported dtypes.
6862        if d.act == Act::Silu && QTensor::matvec_silu_mul(&d.gate_proj, &d.up_proj, x, g, pool) {
6863            // g now holds silu(gate)·up directly.
6864        } else {
6865            u.resize(inter, 0.0);
6866            // Multi-matrix job: gate+up under one pool dispatch.
6867            QTensor::matvec_many([&d.gate_proj, &d.up_proj], x, [g, u], pool);
6868            for i in 0..inter {
6869                g[i] = d.act.combine(g[i], u[i]);
6870            }
6871        }
6872        // DTG-MA bake probe (Patent 2): accumulate this layer's
6873        // per-neuron activation mass while a probe pass is active.
6874        FFN_PROBE.with(|pr| {
6875            if let Some(acc) = pr.borrow_mut().as_mut() {
6876                let li = crate::gpu::cur_layer();
6877                if li >= 0 {
6878                    if let Some(row) = acc.get_mut(li as usize) {
6879                        for (a, &v) in row.iter_mut().zip(g.iter()) {
6880                            *a += (v as f64).abs();
6881                        }
6882                    }
6883                }
6884            }
6885        });
6886        let mut out = attention::take_buf(d.down_proj.rows());
6887        d.down_proj.matvec(g, &mut out, pool);
6888        out
6889    })
6890}
6891
6892thread_local! {
6893    /// DTG-MA activation probe: per-layer per-neuron Σ|silu(g)·u|
6894    /// accumulator, alive only during `Pipeline::probe_ffn_mass`.
6895    static FFN_PROBE: std::cell::RefCell<Option<Vec<Vec<f64>>>> =
6896        const { std::cell::RefCell::new(None) };
6897}
6898
6899/// `dense_ffn_cpu` with a per-visit mask landing on the activations —
6900/// the masked-inference fast path's decode arm. Full fused quant
6901/// compute, closed neurons zeroed before down: arithmetically the
6902/// pruned network, no dequant, no weight bytes touched.
6903fn dense_ffn_masked(
6904    d: &DenseFfn,
6905    x: &[f32],
6906    pool: Option<&Pool>,
6907    mask_row: &[u8],
6908) -> Vec<f32> {
6909    let inter = d.gate_proj.rows();
6910    FFN_SCRATCH.with(|s| {
6911        let mut s = s.borrow_mut();
6912        let [g, u, ..] = &mut *s;
6913        g.resize(inter, 0.0);
6914        if d.act == Act::Silu && QTensor::matvec_silu_mul(&d.gate_proj, &d.up_proj, x, g, pool) {
6915            // g holds silu(gate)·up.
6916        } else {
6917            u.resize(inter, 0.0);
6918            QTensor::matvec_many([&d.gate_proj, &d.up_proj], x, [g, u], pool);
6919            for i in 0..inter {
6920                g[i] = d.act.combine(g[i], u[i]);
6921            }
6922        }
6923        zero_masked_cols(g, 1, inter, mask_row);
6924        let mut out = attention::take_buf(d.down_proj.rows());
6925        d.down_proj.matvec(g, &mut out, pool);
6926        out
6927    })
6928}
6929
6930/// Dense FFN as one GPU submission via the MoE block path (single
6931/// expert, weight 1.0): gate → silu·up → down chained in one command
6932/// buffer, intermediate activations device-resident. None → weights
6933/// not q8-mapped in the primary shard / over the VRAM budget / backend
6934/// refusal → honest CPU path.
6935fn dense_ffn_gpu(d: &DenseFfn, x: &[f32], _pool: Option<&Pool>) -> Option<Vec<f32>> {
6936    // The GPU block hardcodes SiLU; GeLU FFNs (Gemma) stay on CPU.
6937    if d.act != Act::Silu {
6938        return None;
6939    }
6940    // Threshold: tiny FFNs are not worth a submission (q1 excepted —
6941    // see the caller's gate).
6942    if d.gate_proj.rows() < crate::gpu::min_rows() && !d.gate_proj.is_q1() {
6943        return None;
6944    }
6945    let mut jobs: Vec<crate::gpu::MoeJob> = Vec::with_capacity(1);
6946    let mut model_ref = None;
6947    moe_push_job(d, x, 1.0, &mut jobs, &mut model_ref)?;
6948    let model = model_ref?;
6949    let hidden = jobs[0].down.1;
6950    let mut out = attention::take_buf(hidden);
6951    if crate::gpu::moe_block(&model, &jobs, &mut out) {
6952        Some(out)
6953    } else {
6954        let mut out = out;
6955        attention::recycle_buf(&mut out);
6956        None
6957    }
6958}
6959
6960/// q8-mapped primary-shard tensor parts for a GPU job: q8_2f carries
6961/// its column field, q8_row runs with empty col slices (the backend
6962/// skips the multiply). Shared by the MoE block and the dense-FFN
6963/// single-job path.
6964#[allow(clippy::type_complexity)]
6965#[allow(clippy::type_complexity)]
6966pub(crate) fn moe_parts(
6967    t: &QTensor,
6968) -> Option<(
6969    &std::sync::Arc<cortiq_core::CmfModel>,
6970    usize,
6971    usize,
6972    usize,
6973    &[f32],
6974    &[f32],
6975    bool,
6976    bool,
6977    bool,
6978)> {
6979    match t {
6980        QTensor::Mapped {
6981            model,
6982            idx,
6983            dtype: dt @ (cortiq_core::TensorDtype::Q8_2f | cortiq_core::TensorDtype::Q8Row),
6984            rows,
6985            cols,
6986            row_scale,
6987            col_field,
6988            ..
6989        } if (*dt == cortiq_core::TensorDtype::Q8Row) || !col_field.is_empty() => Some((
6990            model, *idx, *rows, *cols, row_scale, col_field, false, false, false,
6991        )),
6992        // q1: tile-embedded scales — empty rs/col slices, raw xs.
6993        QTensor::Mapped {
6994            model,
6995            idx,
6996            dtype: cortiq_core::TensorDtype::Q1,
6997            rows,
6998            cols,
6999            ..
7000        } => Some((model, *idx, *rows, *cols, &[][..], &[][..], true, false, false)),
7001        // q4_tiled: 18-byte tiles with embedded f16 scales — raw xs.
7002        QTensor::Mapped {
7003            model,
7004            idx,
7005            dtype: cortiq_core::TensorDtype::Q4Tiled,
7006            rows,
7007            cols,
7008            ..
7009        } => Some((model, *idx, *rows, *cols, &[][..], &[][..], false, true, false)),
7010        // q4tp: same raw-xs contract, different stride and scale plane.
7011        QTensor::Mapped {
7012            model,
7013            idx,
7014            dtype: cortiq_core::TensorDtype::Q4TiledP,
7015            rows,
7016            cols,
7017            ..
7018        } => Some((model, *idx, *rows, *cols, &[][..], &[][..], false, true, false)),
7019        // q2tp: the 2-bit expert plane of the mixed profile — q4 family
7020        // for stride bookkeeping, flagged q2 so the trio validation can
7021        // demand a q4tp down.
7022        QTensor::Mapped {
7023            model,
7024            idx,
7025            dtype: cortiq_core::TensorDtype::Q2TiledP,
7026            rows,
7027            cols,
7028            ..
7029        } => Some((model, *idx, *rows, *cols, &[][..], &[][..], false, true, true)),
7030        _ => None,
7031    }
7032}
7033
7034/// Map a softmax-router MoE onto the Metal token graph's contract:
7035/// f32 router, gated shared expert, experts uniformly q4tp (or the
7036/// mixed profile: q2tp gate/up over a q4tp down). Sigmoid/bias/τ
7037/// routers, masks, per-expert scales and Gemma's router-input norm
7038/// refuse here — those semantics stay on the CPU path.
7039#[cfg(target_os = "macos")]
7040fn metal_moe_graph_parts(m: &MoeFfn, hidden: usize) -> Option<crate::gpu::GpuMoe<'_>> {
7041    if m.router_sigmoid
7042        || m.router_input_norm
7043        || m.expert_bias.is_some()
7044        || m.route_tau.is_some()
7045        || m.mask.is_some()
7046        || m.per_expert_scale.is_some()
7047        || m.experts.is_empty()
7048        || m.top_k == 0
7049    {
7050        return None;
7051    }
7052    // The select kernel hard-codes the gated shared expert; an
7053    // ungated one would need its own weight-1 slot.
7054    let (sh, sg) = match &m.shared {
7055        Some((sh, Some(sg))) => (sh, sg),
7056        _ => return None,
7057    };
7058    let (rf, rr, rc) = m.router.f32_parts()?;
7059    if rr != m.experts.len() || rc != hidden {
7060        return None;
7061    }
7062    let (sf, sr, sc) = sg.f32_parts()?;
7063    if sr * sc != hidden {
7064        return None;
7065    }
7066    let inter = m.experts[0].gate_proj.rows();
7067    // The first expert's gate decides the profile; every trio (shared
7068    // included) must agree — the jobs ladder flips ONE kernel for all.
7069    let gu_q2 = m.experts[0].gate_proj.mapped_q2tp().is_some();
7070    let trio = |e: &DenseFfn| -> Option<(usize, usize, usize)> {
7071        if e.act != Act::Silu
7072            || e.gate_proj.rows() != inter
7073            || e.gate_proj.cols() != hidden
7074            || e.up_proj.rows() != inter
7075            || e.up_proj.cols() != hidden
7076            || e.down_proj.rows() != hidden
7077            || e.down_proj.cols() != inter
7078        {
7079            return None;
7080        }
7081        let pick = |t: &QTensor| -> Option<usize> {
7082            if gu_q2 {
7083                t.mapped_q2tp().map(|(_, i)| i)
7084            } else {
7085                t.mapped_q4tp().map(|(_, i)| i)
7086            }
7087        };
7088        Some((
7089            pick(&e.gate_proj)?,
7090            pick(&e.up_proj)?,
7091            e.down_proj.mapped_q4tp().map(|(_, i)| i)?,
7092        ))
7093    };
7094    let experts = m
7095        .experts
7096        .iter()
7097        .map(trio)
7098        .collect::<Option<Vec<_>>>()?;
7099    let shared = trio(sh)?;
7100    Some(crate::gpu::GpuMoe {
7101        router: rf,
7102        sgate: sf,
7103        experts,
7104        shared,
7105        n_exp: m.experts.len(),
7106        top_k: m.top_k,
7107        inter,
7108        norm_topk: m.norm_topk_prob,
7109        route_scale: m.routed_scaling,
7110        gu_q2,
7111    })
7112}
7113
7114/// Build one gate/up/down GPU job from three tensors. `moe_push_job` is the
7115/// DenseFfn-shaped caller; architectures that keep their experts in their own
7116/// structs (DeepSeek-V4) come here directly.
7117pub(crate) fn moe_push_job_parts<'a>(
7118    gate: &'a QTensor,
7119    up: &'a QTensor,
7120    down: &'a QTensor,
7121    x: &[f32],
7122    w: f32,
7123    swiglu_limit: f32,
7124    jobs: &mut Vec<crate::gpu::MoeJob<'a>>,
7125    model_ref: &mut Option<std::sync::Arc<cortiq_core::CmfModel>>,
7126) -> Option<()> {
7127    use crate::qtensor::prescale;
7128    let (gm, gi, gr, gc, grs, gcf, gq1, gq4, gq2) = moe_parts(gate)?;
7129    let (_, ui, ur, uc, urs, ucf, uq1, uq4, uq2) = moe_parts(up)?;
7130    let (_, di, dr, dc, drs, dcf, dq1, dq4, dq2) = moe_parts(down)?;
7131    if gq1 != uq1 || uq1 != dq1 || gq4 != uq4 || uq4 != dq4 || gq2 != uq2 {
7132        return None; // mixed-dtype trio — honest CPU path
7133    }
7134    // The 2-bit profile is gate/up q2tp over a PLAIN q4tp down; any other
7135    // 2-bit arrangement stays on the CPU.
7136    if gq2 && (dq2 || !dq4 || down.mapped_q4tp().is_none()) {
7137        return None;
7138    }
7139    if !gq2 && dq2 {
7140        return None;
7141    }
7142    model_ref.get_or_insert_with(|| gm.clone());
7143    let dt = |cf: &[f32]| {
7144        if cf.is_empty() {
7145            cortiq_core::TensorDtype::Q8Row
7146        } else {
7147            cortiq_core::TensorDtype::Q8_2f
7148        }
7149    };
7150    jobs.push(crate::gpu::MoeJob {
7151        gate: (gi, gr, gc, grs),
7152        up: (ui, ur, uc, urs),
7153        down: (di, dr, dc, drs),
7154        xs_gate: prescale(x, gcf, dt(gcf)).into_owned(),
7155        xs_up: prescale(x, ucf, dt(ucf)).into_owned(),
7156        down_col: dcf,
7157        w,
7158        q1: gq1,
7159        q4t: gq4 && !gq2 && gate.mapped_q4tp().is_none(),
7160        q4tp: gq4 && (gq2 || gate.mapped_q4tp().is_some()),
7161        gu_q2: gq2,
7162        swiglu_limit,
7163    });
7164    Some(())
7165}
7166
7167/// Build one gate/up/down GPU job (see `moe_parts`).
7168fn moe_push_job<'a>(
7169    d: &'a DenseFfn,
7170    x: &[f32],
7171    w: f32,
7172    jobs: &mut Vec<crate::gpu::MoeJob<'a>>,
7173    model_ref: &mut Option<std::sync::Arc<cortiq_core::CmfModel>>,
7174) -> Option<()> {
7175    use crate::qtensor::prescale;
7176    if d.act != Act::Silu {
7177        return None; // GPU block hardcodes SiLU
7178    }
7179    let (gm, gi, gr, gc, grs, gcf, gq1, gq4, gq2) = moe_parts(&d.gate_proj)?;
7180    let (_, ui, ur, uc, urs, ucf, uq1, uq4, uq2) = moe_parts(&d.up_proj)?;
7181    let (_, di, dr, dc, drs, dcf, dq1, dq4, dq2) = moe_parts(&d.down_proj)?;
7182    if gq1 != uq1 || uq1 != dq1 || gq4 != uq4 || uq4 != dq4 || gq2 != uq2 {
7183        return None; // mixed-dtype trio — honest CPU path
7184    }
7185    if gq2 && (dq2 || !dq4 || d.down_proj.mapped_q4tp().is_none()) {
7186        return None;
7187    }
7188    if !gq2 && dq2 {
7189        return None;
7190    }
7191    model_ref.get_or_insert_with(|| gm.clone());
7192    let gdt = if gcf.is_empty() {
7193        cortiq_core::TensorDtype::Q8Row
7194    } else {
7195        cortiq_core::TensorDtype::Q8_2f
7196    };
7197    let udt = if ucf.is_empty() {
7198        cortiq_core::TensorDtype::Q8Row
7199    } else {
7200        cortiq_core::TensorDtype::Q8_2f
7201    };
7202    jobs.push(crate::gpu::MoeJob {
7203        gate: (gi, gr, gc, grs),
7204        up: (ui, ur, uc, urs),
7205        down: (di, dr, dc, drs),
7206        xs_gate: prescale(x, gcf, gdt).into_owned(),
7207        xs_up: prescale(x, ucf, udt).into_owned(),
7208        down_col: dcf,
7209        w,
7210        q1: gq1,
7211        q4t: gq4 && !gq2 && d.gate_proj.mapped_q4tp().is_none(),
7212        q4tp: gq4 && (gq2 || d.gate_proj.mapped_q4tp().is_some()),
7213        gu_q2: gq2,
7214        swiglu_limit: 0.0,
7215    });
7216    Some(())
7217}
7218
7219/// Sparse dense-FFN directly on QUANTIZED weights (mask × mmap): reads
7220/// ONLY the active neurons' gate/up rows and down columns from the mmap
7221/// — no full-matrix dequant, no f32 model copy. This is what lets a
7222/// masked big model run at quantized RSS (the historical mask path
7223/// forced the whole model to f32). Semantics identical to the f32
7224/// sparse path within quant tolerance.
7225fn sparse_ffn_quant(
7226    d: &DenseFfn,
7227    x: &[f32],
7228    active: &[u16],
7229    hidden: usize,
7230    pool: Option<&Pool>,
7231) -> Vec<f32> {
7232    let n = active.len();
7233    let inter = d.gate_proj.rows();
7234    let mut act = vec![0.0f32; n];
7235    // Scratch is needed if EITHER projection is group-packed (q4/vbit);
7236    // gate/up normally share a dtype but sizing on both is robust.
7237    let need_scratch = !(d.gate_proj.sparse_col_ok() && d.up_proj.sparse_col_ok());
7238    let compute = |ai: usize| -> f32 {
7239        let idx = active[ai] as usize;
7240        if idx >= inter {
7241            return 0.0; // defensive parity with the f32 sparse path
7242        }
7243        let mut s = if need_scratch {
7244            vec![0.0f32; hidden]
7245        } else {
7246            Vec::new()
7247        };
7248        let gate = d.gate_proj.row_dot(idx, x, &mut s);
7249        let up = d.up_proj.row_dot(idx, x, &mut s);
7250        d.act.combine(gate, up)
7251    };
7252    match pool {
7253        Some(p) if n >= 256 => {
7254            let ptr = SendMut(act.as_mut_ptr());
7255            p.run(&|widx, nw| {
7256                let chunk = n.div_ceil(nw);
7257                let (s, e) = (widx * chunk, ((widx + 1) * chunk).min(n));
7258                for ai in s..e {
7259                    unsafe { *ptr.at(ai) = compute(ai) };
7260                }
7261            });
7262        }
7263        _ => {
7264            for (ai, a) in act.iter_mut().enumerate() {
7265                *a = compute(ai);
7266            }
7267        }
7268    }
7269    // Scatter through active down columns (reads only those columns).
7270    let mut out = vec![0.0f32; hidden];
7271    for (ai, &idx) in active.iter().enumerate() {
7272        let w = act[ai];
7273        if w.abs() >= 1e-12 && (idx as usize) < inter {
7274            d.down_proj.add_col_scaled(idx as usize, w, &mut out);
7275        }
7276    }
7277    out
7278}
7279
7280/// Test-only re-export of the private sparse-quant FFN (mask × mmap gate).
7281#[doc(hidden)]
7282pub fn sparse_ffn_quant_for_test(
7283    d: &DenseFfn,
7284    x: &[f32],
7285    active: &[u16],
7286    hidden: usize,
7287) -> Vec<f32> {
7288    sparse_ffn_quant(d, x, active, hidden, None)
7289}
7290
7291/// Dequantize a DenseFfn's three matrices to f32 (transient; only the
7292/// q4/vbit-masked fallback uses it — the memory-lean path is
7293/// sparse_ffn_quant). Reuses row_f32 row-by-row.
7294fn dequant_dense_f32(d: &DenseFfn) -> (Vec<f32>, Vec<f32>, Vec<f32>) {
7295    let deq = |t: &QTensor| -> Vec<f32> {
7296        let (rows, cols) = (t.rows(), t.cols());
7297        let mut out = vec![0.0f32; rows * cols];
7298        for r in 0..rows {
7299            t.row_f32(r, &mut out[r * cols..(r + 1) * cols]);
7300        }
7301        out
7302    };
7303    (deq(&d.gate_proj), deq(&d.up_proj), deq(&d.down_proj))
7304}
7305
7306/// Pointer wrapper for the worker-pool scatter (same pattern as qtensor).
7307struct SendMut(*mut f32);
7308unsafe impl Send for SendMut {}
7309unsafe impl Sync for SendMut {}
7310impl SendMut {
7311    #[inline]
7312    // Deliberate unsynchronized scatter: pool workers write disjoint indices
7313    // in parallel, so returning `&mut` from `&self` is intentional here.
7314    #[allow(clippy::mut_from_ref)]
7315    unsafe fn at(&self, i: usize) -> &mut f32 {
7316        unsafe { &mut *self.0.add(i) }
7317    }
7318}
7319
7320/// Router → (selected experts in torch.topk order, per-expert score
7321/// vector, normalizer). The final weight of expert `e` is `p[e] / wsum`.
7322///
7323/// Two regimes share this. Qwen: softmax over ALL experts, top-k of the
7324/// probabilities, optional renorm — `router_sigmoid=false`, no bias,
7325/// scale 1 → bit-identical to the historical path. LFM2-MoE /
7326/// DeepSeek-V3 `noaux_tc`: per-expert sigmoid scores, an optional
7327/// selection bias (top-k CHOICE only; weights stay unbiased), a 1e-6 renorm
7328/// floor and a routed scale.
7329fn moe_route(logits: &[f32], m: &MoeFfn, allowed: Option<&[bool]>) -> (Vec<usize>, Vec<f32>, f32) {
7330    let ne = logits.len();
7331    let p: Vec<f32> = if m.router_sigmoid {
7332        logits.iter().map(|&l| 1.0 / (1.0 + (-l).exp())).collect()
7333    } else {
7334        let mx = logits.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
7335        let mut e: Vec<f32> = logits.iter().map(|&l| (l - mx).exp()).collect();
7336        let s: f32 = e.iter().sum();
7337        for v in &mut e {
7338            *v /= s;
7339        }
7340        e
7341    };
7342    // Expert restriction: the static env mask (CMF_MOE_MASK) AND the
7343    // active task mask's expert fields (spec §5) both narrow the
7344    // candidate set; selection happens over the admitted experts only.
7345    // With norm_topk the kept weights renormalize below; without it
7346    // the excluded mass is honestly dropped.
7347    let admit = |e: usize| {
7348        m.mask.as_ref().is_none_or(|mk| mk[e])
7349            && allowed.is_none_or(|a| a.get(e).copied().unwrap_or(false))
7350    };
7351    let mut idx: Vec<usize> = (0..ne).filter(|&e| admit(e)).collect();
7352    // Descending by selection score, lower index wins ties (torch.topk).
7353    match &m.expert_bias {
7354        Some(b) => idx.sort_unstable_by(|&x, &y| {
7355            (p[y] + b[y])
7356                .partial_cmp(&(p[x] + b[x]))
7357                .unwrap()
7358                .then(x.cmp(&y))
7359        }),
7360        None => idx.sort_unstable_by(|&x, &y| p[y].partial_cmp(&p[x]).unwrap().then(x.cmp(&y))),
7361    }
7362    idx.truncate(m.top_k);
7363    // Adaptive τ-routing: trim the tail experts once the kept mass is
7364    // enough. wsum below renormalizes over the KEPT set, so the output
7365    // stays a proper weighted average.
7366    if let Some(tau) = m.route_tau {
7367        let total: f32 = idx.iter().map(|&e| p[e]).sum();
7368        if total > 0.0 {
7369            let mut acc = 0.0f32;
7370            let mut keep = idx.len();
7371            for (i, &e) in idx.iter().enumerate() {
7372                acc += p[e];
7373                if acc >= tau * total {
7374                    keep = i + 1;
7375                    break;
7376                }
7377            }
7378            idx.truncate(keep);
7379        }
7380    }
7381    let wsum: f32 = if m.norm_topk_prob {
7382        let s: f32 = idx.iter().map(|&e| p[e]).sum();
7383        // LFM2 floors the denom (matches HF `+ 1e-6`); the softmax path's
7384        // probs already sum near 1, so it stays exactly as before.
7385        (if m.router_sigmoid { s + 1e-6 } else { s }) / m.routed_scaling
7386    } else {
7387        1.0 / m.routed_scaling
7388    };
7389    (idx, p, wsum)
7390}
7391
7392/// MoE FFN: router → top-k experts (see `moe_route`). Only selected
7393/// experts' pages are touched in mmap.
7394fn moe_ffn(m: &MoeFfn, x: &[f32], pool: Option<&Pool>, allowed: Option<&[bool]>) -> Vec<f32> {
7395    accumulate_act(m, x, 1);
7396    let ne = m.experts.len();
7397    let mut logits = vec![0.0f32; ne];
7398    m.router.matvec(x, &mut logits, pool);
7399    let (idx, p, wsum) = moe_route(&logits, m, allowed);
7400    {
7401        let mut st = m.stats.borrow_mut();
7402        if st.len() < ne {
7403            st.resize(ne, 0);
7404        }
7405        for &e in &idx {
7406            st[e] += 1;
7407        }
7408    }
7409    // D5: the whole layer MoE block in one GPU command buffer (experts — the
7410    // same mmap via a no-copy buffer; intermediate activations on the GPU).
7411    // Same Ffn probe class as the dense chain: one submit per layer
7412    // either wins on this driver stack or it doesn't.
7413    if crate::gpu::enabled_here() {
7414        match crate::gpu::probe_arm(crate::gpu::OpClass::Ffn) {
7415            crate::gpu::ProbeArm::Gpu => {
7416                let t0 = std::time::Instant::now();
7417                if let Some(out) = moe_ffn_gpu(m, x, &idx, &p, wsum, pool) {
7418                    crate::gpu::probe_record(crate::gpu::OpClass::Ffn, true, t0.elapsed());
7419                    return out;
7420                }
7421            }
7422            crate::gpu::ProbeArm::CpuTimed => {
7423                let t0 = std::time::Instant::now();
7424                let out = crate::gpu::cpu_scope(|| moe_ffn_cpu(m, x, &idx, &p, wsum, pool));
7425                crate::gpu::probe_record(crate::gpu::OpClass::Ffn, false, t0.elapsed());
7426                return out;
7427            }
7428            crate::gpu::ProbeArm::Cpu => {
7429                return crate::gpu::cpu_scope(|| moe_ffn_cpu(m, x, &idx, &p, wsum, pool));
7430            }
7431        }
7432    }
7433    moe_ffn_cpu(m, x, &idx, &p, wsum, pool)
7434}
7435
7436/// One-shot report of whether the whole-token wgpu graph actually formed.
7437/// A refusal silently reverts to the per-op path, which is how a model can
7438/// look "GPU-accelerated" while every layer walks the host.
7439fn graph_note(built: bool) {
7440    use std::sync::atomic::{AtomicBool, Ordering};
7441    if built {
7442        GRAPH_TOK_OK.fetch_add(1, Ordering::Relaxed);
7443    } else {
7444        GRAPH_TOK_MISS.fetch_add(1, Ordering::Relaxed);
7445    }
7446    static SAID: AtomicBool = AtomicBool::new(false);
7447    if !SAID.swap(true, Ordering::Relaxed) {
7448        if built {
7449            tracing::info!("wgpu whole-token graph: ACTIVE");
7450        } else {
7451            tracing::warn!("wgpu whole-token graph refused — per-op path");
7452        }
7453    }
7454}
7455
7456/// Whole-token graph outcomes, process-wide: a benchmark that claims a
7457/// GPU number while MISS climbs is measuring the CPU — the honest-bench
7458/// contract makes that an error, not a footnote.
7459pub static GRAPH_TOK_OK: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
7460pub static GRAPH_TOK_MISS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
7461
7462/// `CMF_MOE_BATCH=0` restores the per-expert serial loop — the A/B lever
7463/// for the batched kernel, and how its bit-identity is checked.
7464fn moe_batch_enabled() -> bool {
7465    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
7466    *ON.get_or_init(|| std::env::var("CMF_MOE_BATCH").as_deref() != Ok("0"))
7467}
7468
7469/// Two-dispatch CPU MoE: every routed expert (and the shared one) fused
7470/// into one gate/up/SiLU dispatch and one down dispatch, instead of two
7471/// pool barriers per expert. Bit-identical to the serial loop below —
7472/// see `moe_gate_up_many` / `moe_down_many`. `None` = the batched kernel
7473/// does not cover this layer, walk the serial path.
7474fn moe_ffn_cpu_batched(
7475    m: &MoeFfn,
7476    x: &[f32],
7477    idx: &[usize],
7478    p: &[f32],
7479    wsum: f32,
7480    pool: Option<&Pool>,
7481) -> Option<Vec<f32>> {
7482    if idx.is_empty() || !moe_batch_enabled() {
7483        return None;
7484    }
7485    // The bake probe reads per-neuron activation mass out of the
7486    // single-expert path; batching would skip it. Rare and offline —
7487    // hand those runs to the serial loop.
7488    if FFN_PROBE.with(|pr| pr.borrow().is_some()) {
7489        return None;
7490    }
7491    let n = idx.len() + usize::from(m.shared.is_some());
7492    let mut pairs = Vec::with_capacity(n);
7493    let mut downs = Vec::with_capacity(n);
7494    let mut ws = Vec::with_capacity(n);
7495    for &e in idx {
7496        let d = &m.experts[e];
7497        if d.act != Act::Silu {
7498            return None;
7499        }
7500        pairs.push((&d.gate_proj, &d.up_proj));
7501        downs.push(&d.down_proj);
7502        ws.push(p[e] / wsum * m.per_expert_scale.as_ref().map_or(1.0, |v| v[e]));
7503    }
7504    // The shared expert goes last, matching the serial loop's order —
7505    // the f32 accumulation order is part of the bit-identity claim.
7506    if let Some((se, gate)) = &m.shared {
7507        if se.act != Act::Silu {
7508            return None;
7509        }
7510        let g = gate.as_ref().map_or(1.0, |gate| {
7511            let mut gl = [0.0f32; 1];
7512            gate.matvec(x, &mut gl, pool);
7513            1.0 / (1.0 + (-gl[0]).exp())
7514        });
7515        pairs.push((&se.gate_proj, &se.up_proj));
7516        downs.push(&se.down_proj);
7517        ws.push(g);
7518    }
7519    let inter = pairs[0].0.rows();
7520    let mut gs: Vec<Vec<f32>> = (0..pairs.len()).map(|_| vec![0f32; inter]).collect();
7521    if !QTensor::moe_gate_up_many(&pairs, x, &mut gs, pool) {
7522        return None;
7523    }
7524    let mut out = attention::take_buf(x.len());
7525    if !QTensor::moe_down_many(&downs, &gs, &ws, &mut out, pool) {
7526        attention::recycle_buf(&mut out);
7527        return None;
7528    }
7529    Some(out)
7530}
7531
7532/// The pure-CPU MoE expert loop (also the fallback of every GPU refusal).
7533fn moe_ffn_cpu(
7534    m: &MoeFfn,
7535    x: &[f32],
7536    idx: &[usize],
7537    p: &[f32],
7538    wsum: f32,
7539    pool: Option<&Pool>,
7540) -> Vec<f32> {
7541    if let Some(out) = moe_ffn_cpu_batched(m, x, idx, p, wsum, pool) {
7542        return out;
7543    }
7544    let mut out = attention::take_buf(x.len());
7545    for &e in idx {
7546        let mut eo = dense_ffn(&m.experts[e], x, pool);
7547        let w = p[e] / wsum * m.per_expert_scale.as_ref().map_or(1.0, |v| v[e]);
7548        for i in 0..out.len() {
7549            out[i] += w * eo[i];
7550        }
7551        attention::recycle_buf(&mut eo);
7552    }
7553    if let Some((se, gate)) = &m.shared {
7554        let mut so = dense_ffn(se, x, pool);
7555        let g = gate.as_ref().map_or(1.0, |gate| {
7556            let mut gl = [0.0f32; 1];
7557            gate.matvec(x, &mut gl, pool);
7558            1.0 / (1.0 + (-gl[0]).exp())
7559        });
7560        for i in 0..out.len() {
7561            out[i] += g * so[i];
7562        }
7563        attention::recycle_buf(&mut so);
7564    }
7565    out
7566}
7567
7568/// DeepSeek-V2 MLA forward, expand-to-MHA form (see `AttnKind::Mla`):
7569/// per token the latent expands to every head's K/V and the ordinary
7570/// cache + grouped attend do the rest. K head layout is [rope | nope]
7571/// (rotary_dim = qk_rope rotates the shared rope key and each q head's
7572/// prefix); V rows are zero-padded to the K head_dim inside the cache
7573/// and the pad is sliced off before O. Born importance is not
7574/// accumulated for MLA yet (no eviction interplay).
7575#[allow(clippy::too_many_arguments)]
7576fn mla_attention(
7577    w: &MlaWeights,
7578    normed: &[f32],
7579    cache: &mut crate::kv_cache::LayerKvCache,
7580    position: usize,
7581    inv_freq: &[f32],
7582    rope_scale: f32,
7583    eps: f64,
7584    pool: Option<&Pool>,
7585) -> Vec<f32> {
7586    let (nh, dr, dn, dv, lora) = (w.nh, w.qk_rope, w.qk_nope, w.v_dim, w.lora);
7587    let hd = dr + dn;
7588    let mut q = vec![0.0f32; nh * hd];
7589    match (&w.q_a, &w.q_a_norm) {
7590        (Some(qa), Some(qn)) => {
7591            let mut t = vec![0.0f32; qa.rows()];
7592            qa.matvec(normed, &mut t, pool);
7593            let tn = inference::rms_norm(&t, qn, eps, NormStyle::Qwen);
7594            w.q_proj.matvec(&tn, &mut q, pool);
7595        }
7596        _ => w.q_proj.matvec(normed, &mut q, pool),
7597    }
7598    let mut ca = vec![0.0f32; lora + dr];
7599    w.kv_a.matvec(normed, &mut ca, pool);
7600    let (c_lat, k_rope) = ca.split_at_mut(lora);
7601    let latn = inference::rms_norm(c_lat, &w.kv_a_norm, eps, NormStyle::Qwen);
7602    let mut kvb = vec![0.0f32; nh * (dn + dv)];
7603    w.kv_b.matvec(&latn, &mut kvb, pool);
7604    if !w.nope {
7605        attention::rope_rotate_scaled(k_rope, position, inv_freq, rope_scale);
7606    }
7607    for h in 0..nh {
7608        if !w.nope {
7609            attention::rope_rotate_scaled(
7610                &mut q[h * hd..h * hd + dr],
7611                position,
7612                inv_freq,
7613                rope_scale,
7614            );
7615        }
7616    }
7617    let mut k = vec![0.0f32; nh * hd];
7618    let mut v = vec![0.0f32; nh * hd];
7619    for h in 0..nh {
7620        k[h * hd..h * hd + dr].copy_from_slice(k_rope);
7621        k[h * hd + dr..(h + 1) * hd].copy_from_slice(&kvb[h * (dn + dv)..h * (dn + dv) + dn]);
7622        v[h * hd..h * hd + dv].copy_from_slice(&kvb[h * (dn + dv) + dn..(h + 1) * (dn + dv)]);
7623    }
7624    cache.append(&k, &v, &vec![true; nh]);
7625    let (ao, mut imp) = attention::attend_all_heads(&q, cache, nh, 1, hd, w.scale, None, 0.0);
7626    attention::recycle_buf(&mut imp);
7627    let mut ov = vec![0.0f32; nh * dv];
7628    for h in 0..nh {
7629        ov[h * dv..(h + 1) * dv].copy_from_slice(&ao[h * hd..h * hd + dv]);
7630    }
7631    let mut out = vec![0.0f32; w.o_proj.rows()];
7632    w.o_proj.matvec(&ov, &mut out, pool);
7633    out
7634}
7635
7636/// Gemma-4 dual-branch FFN (spec: see `FfnKind::DenseMoe`). The dense
7637/// branch reads the pre-FFN-normed activation; the router and the
7638/// expert branch read the RAW residual — the router through a
7639/// scale-less rms norm (its constant gain is folded into the weights),
7640/// the experts through `pre_norm_2`. CPU path; GPU graphs refuse the
7641/// layer kind honestly.
7642fn dense_moe_ffn(
7643    dm: &DenseMoeFfn,
7644    x_normed: &[f32],
7645    h_raw: &[f32],
7646    eps: f64,
7647    norm_style: NormStyle,
7648    pool: Option<&Pool>,
7649) -> Vec<f32> {
7650    let mut d = dense_ffn(&dm.dense, x_normed, pool);
7651    d = inference::rms_norm(&d, &dm.post_norm_1, eps, norm_style);
7652    let m = &dm.moe;
7653    let ne = m.experts.len();
7654    let mut logits = vec![0.0f32; ne];
7655    if m.router_input_norm {
7656        let ss: f32 = h_raw.iter().map(|v| v * v).sum::<f32>() / h_raw.len() as f32;
7657        let inv = 1.0 / (ss + eps as f32).sqrt();
7658        let xr: Vec<f32> = h_raw.iter().map(|v| v * inv).collect();
7659        m.router.matvec(&xr, &mut logits, pool);
7660    } else {
7661        m.router.matvec(h_raw, &mut logits, pool);
7662    }
7663    let (idx, p, wsum) = moe_route(&logits, m, None);
7664    {
7665        let mut st = m.stats.borrow_mut();
7666        if st.len() < ne {
7667            st.resize(ne, 0);
7668        }
7669        for &e in &idx {
7670            st[e] += 1;
7671        }
7672    }
7673    let x2 = inference::rms_norm(h_raw, &dm.pre_norm_2, eps, norm_style);
7674    let mo = moe_ffn_cpu(m, &x2, &idx, &p, wsum, pool);
7675    let mo = inference::rms_norm(&mo, &dm.post_norm_2, eps, norm_style);
7676    for (di, mi) in d.iter_mut().zip(&mo) {
7677        *di += mi;
7678    }
7679    d
7680}
7681
7682/// Building the MoE-layer GPU jobs: all selected experts (+shared) must
7683/// be q8_2f-Mapped from the primary mapping; otherwise None → CPU path.
7684/// One-shot report of why the MoE GPU block refused. A silent `?` here
7685/// sends every expert to the CPU with nothing in the logs to say so —
7686/// which is exactly how a q4tp MoE model looked "GPU-accelerated" while
7687/// running entirely on the host.
7688fn moe_gpu_refused(why: &'static str) {
7689    use std::sync::atomic::{AtomicBool, Ordering};
7690    static SAID: AtomicBool = AtomicBool::new(false);
7691    if !SAID.swap(true, Ordering::Relaxed) {
7692        tracing::warn!("MoE GPU block refused ({why}) — experts run on the CPU");
7693    }
7694}
7695
7696fn moe_ffn_gpu(
7697    m: &MoeFfn,
7698    x: &[f32],
7699    idx: &[usize],
7700    p: &[f32],
7701    wsum: f32,
7702    pool: Option<&Pool>,
7703) -> Option<Vec<f32>> {
7704    use crate::gpu::MoeJob;
7705
7706    let mut jobs: Vec<MoeJob> = Vec::with_capacity(idx.len() + 1);
7707    let mut model_ref = None;
7708    for &e in idx {
7709        if moe_push_job(&m.experts[e], x, p[e] / wsum, &mut jobs, &mut model_ref).is_none() {
7710            moe_gpu_refused("push_job(expert)");
7711            return None;
7712        }
7713    }
7714    if let Some((se, gate)) = &m.shared {
7715        let g = gate.as_ref().map_or(1.0, |gate| {
7716            let mut gl = [0.0f32; 1];
7717            gate.matvec(x, &mut gl, pool);
7718            1.0 / (1.0 + (-gl[0]).exp())
7719        });
7720        if moe_push_job(se, x, g, &mut jobs, &mut model_ref).is_none() {
7721            moe_gpu_refused("push_job(shared)");
7722            return None;
7723        }
7724    }
7725    let Some(model) = model_ref else {
7726        moe_gpu_refused("no model_ref");
7727        return None;
7728    };
7729    let hidden = jobs[0].down.1;
7730    let mut out = vec![0.0f32; hidden];
7731    if crate::gpu::moe_block(&model, &jobs, &mut out) {
7732        Some(out)
7733    } else {
7734        moe_gpu_refused("gpu::moe_block");
7735        None
7736    }
7737}
7738
7739/// Single-position FFN dispatch.
7740fn ffn_forward(
7741    ffn: &FfnKind,
7742    x: &[f32],
7743    pool: Option<&Pool>,
7744    experts_allowed: Option<&[bool]>,
7745) -> Vec<f32> {
7746    match ffn {
7747        FfnKind::Dense(d) => dense_ffn(d, x, pool),
7748        FfnKind::Moe(m) => moe_ffn(m, x, pool, experts_allowed),
7749        // Dual-branch layers need the raw residual — their callers
7750        // dispatch dense_moe_ffn directly; the auxiliary paths that land
7751        // here (MTP draft, o1 replay) do not co-occur with gemma-4 MoE.
7752        FfnKind::DenseMoe(_) => unreachable!("DenseMoe dispatches via dense_moe_ffn"),
7753    }
7754}
7755
7756/// Fused two-position FFN: gate/up/down streamed once (dense). MoE
7757/// falls back to two singles — expert sets differ per position, there
7758/// is nothing to fuse.
7759fn ffn_forward_pair(
7760    ffn: &FfnKind,
7761    x1: &[f32],
7762    x2: &[f32],
7763    pool: Option<&Pool>,
7764    experts_allowed: Option<&[bool]>,
7765) -> (Vec<f32>, Vec<f32>) {
7766    let d = match ffn {
7767        FfnKind::Dense(d) => d,
7768        FfnKind::Moe(m) => {
7769            return (
7770                moe_ffn(m, x1, pool, experts_allowed),
7771                moe_ffn(m, x2, pool, experts_allowed),
7772            );
7773        }
7774        FfnKind::DenseMoe(_) => unreachable!("DenseMoe dispatches via dense_moe_ffn"),
7775    };
7776    let inter = d.gate_proj.rows();
7777    FFN_SCRATCH.with(|s| {
7778        let mut s = s.borrow_mut();
7779        let [g1, g2, u1, u2] = &mut *s;
7780        g1.resize(inter, 0.0);
7781        g2.resize(inter, 0.0);
7782        u1.resize(inter, 0.0);
7783        u2.resize(inter, 0.0);
7784        // Multi-matrix pair job: gate+up under one pool dispatch
7785        // (o1s = lane-1 outputs across tensors, o2s = lane-2).
7786        QTensor::matvec2_many(
7787            [&d.gate_proj, &d.up_proj],
7788            x1,
7789            x2,
7790            [g1.as_mut_slice(), u1.as_mut_slice()],
7791            [g2.as_mut_slice(), u2.as_mut_slice()],
7792            pool,
7793        );
7794        for i in 0..inter {
7795            g1[i] = d.act.combine(g1[i], u1[i]);
7796            g2[i] = d.act.combine(g2[i], u2[i]);
7797        }
7798        let mut o1 = attention::take_buf(d.down_proj.rows());
7799        let mut o2 = attention::take_buf(d.down_proj.rows());
7800        d.down_proj.matvec2(g1, g2, &mut o1, &mut o2, pool);
7801        (o1, o2)
7802    })
7803}
7804
7805#[cfg(test)]
7806mod tests {
7807
7808    #[test]
7809    fn cancel_flag_stops_generation() {
7810        let mut p = create_test_pipeline(16, 32, 2, 2, 8, 2, 32);
7811        // Set before the call: the prefill loops honour it, the run
7812        // returns immediately with the cancelled reason and no tokens.
7813        p.cancel.store(true, std::sync::atomic::Ordering::Relaxed);
7814        let r = p.generate_from_ids(&[1, 2, 3], 8, None, None).unwrap();
7815        assert_eq!(r.finish_reason, "cancelled");
7816        assert!(
7817            r.token_ids.is_empty(),
7818            "no tokens after cancel: {:?}",
7819            r.token_ids
7820        );
7821        // Flag auto-cleared: the next call generates normally.
7822        let r2 = p.generate_from_ids(&[1, 2, 3], 4, None, None).unwrap();
7823        assert_ne!(r2.finish_reason, "cancelled");
7824    }
7825    use super::*;
7826
7827    /// sparse_ffn_quant must equal a dense FFN where inactive neurons are
7828    /// zeroed (mask × mmap correctness). On F32 tensors this is EXACT —
7829    /// it validates the row_dot / add_col_scaled / scatter indexing, the
7830    /// bug-prone part. The q8 branches reuse the golden-tested linear
7831    /// scale, structurally identical to the matvec kernels.
7832    #[test]
7833    fn sparse_ffn_quant_equals_dense_with_inactive_zeroed() {
7834        let (hidden, inter) = (16usize, 40usize);
7835        let synth = |n: usize, salt: usize| -> Vec<f32> {
7836            (0..n)
7837                .map(|i| (((i * 37 + salt * 11 + 3) % 101) as f32 / 101.0 - 0.5) * 0.4)
7838                .collect()
7839        };
7840        let d = DenseFfn {
7841            gate_proj: QTensor::from_f32(synth(inter * hidden, 1), inter, hidden),
7842            up_proj: QTensor::from_f32(synth(inter * hidden, 2), inter, hidden),
7843            down_proj: QTensor::from_f32(synth(hidden * inter, 3), hidden, inter),
7844            act: Act::Silu,
7845        };
7846        let x = synth(hidden, 9);
7847        // Active = every 3rd neuron.
7848        let active: Vec<u16> = (0..inter as u16).filter(|i| i % 3 == 0).collect();
7849
7850        let sparse = sparse_ffn_quant(&d, &x, &active, hidden, None);
7851
7852        // Reference: full dense FFN but g[i]=0 for inactive neurons.
7853        let mut g = vec![0.0f32; inter];
7854        d.gate_proj.matvec(&x, &mut g, None);
7855        let mut u = vec![0.0f32; inter];
7856        d.up_proj.matvec(&x, &mut u, None);
7857        let act_set: std::collections::HashSet<u16> = active.iter().copied().collect();
7858        for i in 0..inter {
7859            g[i] = if act_set.contains(&(i as u16)) {
7860                inference::silu(g[i]) * u[i]
7861            } else {
7862                0.0
7863            };
7864        }
7865        let mut reference = vec![0.0f32; hidden];
7866        d.down_proj.matvec(&g, &mut reference, None);
7867
7868        let max_d = sparse
7869            .iter()
7870            .zip(&reference)
7871            .map(|(a, b)| (a - b).abs())
7872            .fold(0.0f32, f32::max);
7873        assert!(max_d < 1e-5, "sparse != dense-zeroed: max|Δ| = {max_d}");
7874    }
7875
7876    /// Attach a synthetic MTP head (same structure as a main layer).
7877    fn attach_test_mtp(p: &mut Pipeline) {
7878        let (h, inter, heads, kv, hd) = (
7879            p.hidden_size,
7880            p.intermediate_size,
7881            p.num_heads,
7882            p.num_kv_heads,
7883            p.head_dim,
7884        );
7885        let synth = |n: usize, salt: usize| -> Vec<f32> {
7886            (0..n)
7887                .map(|i| (((i * 29 + salt * 23 + 5) % 101) as f32 / 101.0 - 0.5) * 0.2)
7888                .collect()
7889        };
7890        let qt = |rows: usize, cols: usize, salt: usize| -> QTensor {
7891            QTensor::from_f32(synth(rows * cols, salt), rows, cols)
7892        };
7893        p.mtp = Some(MtpModule {
7894            enorm: vec![1.0; h],
7895            hnorm: vec![1.0; h],
7896            eh_proj: qt(h, 2 * h, 301),
7897            layer: LayerWeights {
7898                input_norm: vec![1.0; h],
7899                post_norm: vec![1.0; h],
7900                attn_out_norm: None,
7901                ffn_out_norm: None,
7902                layer_scale: None,
7903                ffn: FfnKind::Dense(DenseFfn {
7904                    gate_proj: qt(inter, h, 315),
7905                    up_proj: qt(inter, h, 316),
7906                    down_proj: qt(h, inter, 317),
7907                    act: Act::Silu,
7908                }),
7909                attn: AttnKind::Full {
7910                    bias: None,
7911                    wq: qt(heads * hd, h, 311),
7912                    wk: qt(kv * hd, h, 312),
7913                    wv: qt(kv * hd, h, 313),
7914                    wo: qt(h, heads * hd, 314),
7915                    q_norm: None,
7916                    k_norm: None,
7917                    output_gate: false,
7918                    softplus_gate: None,
7919                },
7920            },
7921            final_norm: vec![1.0; h],
7922            kv: crate::kv_cache::LayerKvCache::new(kv, hd),
7923        });
7924    }
7925
7926    #[test]
7927    fn speculative_equals_vanilla_greedy() {
7928        // Speculative decode and the wgpu token graph are mutually
7929        // exclusive; a leaked CMF_GPU=wgpu from a parallel gpu test
7930        // would silently disable drafting. Pin the graph off.
7931        unsafe { std::env::set_var("CMF_GPU_WGPU_GRAPH", "0") };
7932        let run = |spec: bool| {
7933            let mut p = create_test_pipeline(8, 16, 2, 1, 4, 2, 260);
7934            p.sampler_config.temperature = 0.0;
7935            attach_test_mtp(&mut p);
7936            p.speculative = spec;
7937            let r = p.generate("abcdef", 12, None, None).unwrap();
7938            (r.token_ids, r.mtp_drafted, r.mtp_accepted)
7939        };
7940        let (vanilla, d0, _) = run(false);
7941        let (spec, d1, a1) = run(true);
7942        assert_eq!(d0, 0, "vanilla path must not draft");
7943        assert!(d1 > 0, "speculative path must draft");
7944        assert_eq!(
7945            vanilla, spec,
7946            "speculative must reproduce the exact greedy sequence (accepted {a1}/{d1})"
7947        );
7948    }
7949
7950    #[test]
7951    fn speculative_accepts_constant_oracle() {
7952        // See speculative_equals_vanilla_greedy: pin the wgpu graph off.
7953        unsafe { std::env::set_var("CMF_GPU_WGPU_GRAPH", "0") };
7954        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
7955        p.sampler_config.temperature = 0.0;
7956        p.sampler_config.repetition_penalty = 1.0;
7957        // Constant lm_head → every logit equal → both the main model and
7958        // the draft head argmax to token 0: acceptance must be 100%.
7959        p.weights.lm_head = QTensor::from_f32(vec![0.01; 64 * 8], 64, 8);
7960        attach_test_mtp(&mut p);
7961        p.speculative = true;
7962        let r = p.generate("abcd", 10, None, None).unwrap();
7963        assert!(r.mtp_drafted > 0);
7964        assert_eq!(
7965            r.mtp_accepted, r.mtp_drafted,
7966            "constant logits → every draft accepted"
7967        );
7968        // Ties resolve to the same token in both the main and draft
7969        // heads — the sequence is one repeated token.
7970        assert!(r.token_ids.windows(2).all(|w| w[0] == w[1]));
7971    }
7972
7973    #[test]
7974    fn empty_prompt_is_an_error_not_a_panic() {
7975        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 260);
7976        let r = p.generate("", 4, None, None);
7977        assert!(r.is_err(), "empty prompt must be a clean error");
7978    }
7979
7980    #[test]
7981    fn every_token_enters_kv_exactly_once() {
7982        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 2, 260);
7983        // Greedy so no RNG variance; byte tokenizer → 3 prompt tokens.
7984        p.sampler_config.temperature = 0.0;
7985        let r = p.generate("abc", 2, None, None).unwrap();
7986        assert_eq!(r.prompt_tokens, 3);
7987        // prompt(3) + first sampled token forwarded before second logits:
7988        // step0 samples from prefill hidden (no extra forward), then
7989        // forwards t1 → cache 4; step1 samples, loop ends (max_tokens).
7990        assert_eq!(
7991            p.kv_cache.seq_len(),
7992            3 + r.tokens_generated - 1,
7993            "each token must be cached exactly once (v1 cached the last prompt token twice)"
7994        );
7995    }
7996
7997    #[test]
7998    fn generation_is_reproducible_with_seed() {
7999        let run = || {
8000            let mut p = create_test_pipeline(8, 16, 2, 1, 4, 2, 260);
8001            p.generate("hello", 8, None, None).unwrap().token_ids
8002        };
8003        assert_eq!(run(), run());
8004    }
8005
8006    #[test]
8007    fn resetting_sampler_restarts_the_seeded_stream() {
8008        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 2, 260);
8009        let config = SamplerConfig {
8010            seed: Some(1234),
8011            ..SamplerConfig::default()
8012        };
8013        p.set_sampler_config(config.clone());
8014        let first = p.generate("hello", 8, None, None).unwrap().token_ids;
8015        p.set_sampler_config(config);
8016        let second = p.generate("hello", 8, None, None).unwrap().token_ids;
8017        assert_eq!(first, second);
8018    }
8019
8020    #[test]
8021    fn eviction_bounds_the_cache() {
8022        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 260);
8023        p.kv_cache.max_seq_len = 6;
8024        p.sampler_config.temperature = 0.0;
8025        let _ = p.generate("abcd", 12, None, None).unwrap();
8026        assert!(
8027            p.kv_cache.seq_len() <= 6 + 1,
8028            "cache must stay bounded by max_seq_len (got {})",
8029            p.kv_cache.seq_len()
8030        );
8031    }
8032
8033    #[test]
8034    fn confidence_matches_tokens_and_is_a_probability() {
8035        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
8036        p.sampler_config.temperature = 0.0;
8037        p.sampler_config.repetition_penalty = 1.0;
8038        let r = p.generate("abcd", 10, None, None).unwrap();
8039        assert_eq!(
8040            r.token_confidence.len(),
8041            r.token_ids.len(),
8042            "one confidence per emitted token"
8043        );
8044        for &c in &r.token_confidence {
8045            assert!((0.0..=1.0).contains(&c), "confidence out of [0,1]: {c}");
8046        }
8047        // top1_prob is a valid softmax probability.
8048        let logits = [1.0f32, 3.0, 0.5, 3.0];
8049        let p0 = top1_prob_t(&logits, 1, 1.0);
8050        let p1 = top1_prob_t(&logits, 3, 1.0);
8051        assert!((p0 - p1).abs() < 1e-6, "equal logits → equal prob");
8052        assert!(p0 > 0.0 && p0 < 1.0);
8053        // Calibration temperature > 1 softens an over-confident peak.
8054        let sharp = top1_prob_t(&logits, 1, 1.0);
8055        let soft = top1_prob_t(&logits, 1, 2.0);
8056        assert!(soft < sharp, "higher temperature lowers peak confidence");
8057    }
8058
8059    #[test]
8060    fn trace_is_opt_in_and_parallels_the_output() {
8061        // Off by default: the runtime is silent unless observation asked.
8062        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
8063        p.sampler_config.temperature = 0.0;
8064        p.sampler_config.repetition_penalty = 1.0;
8065        let r = p.generate("abcd", 10, None, None).unwrap();
8066        assert!(r.traces.is_empty(), "trace must be empty unless enabled");
8067
8068        // On: exactly one row per emitted token, aligned with the output.
8069        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
8070        p.sampler_config.temperature = 0.0;
8071        p.sampler_config.repetition_penalty = 1.0;
8072        p.set_trace(true);
8073        let r = p.generate("abcd", 10, None, None).unwrap();
8074        assert_eq!(r.traces.len(), r.token_ids.len(), "one trace row per token");
8075        for (i, tr) in r.traces.iter().enumerate() {
8076            assert_eq!(tr.t, i, "trace index is sequential");
8077            assert_eq!(tr.token_id, r.token_ids[i], "trace token_id matches output");
8078            assert_eq!(
8079                tr.confidence, r.token_confidence[i],
8080                "trace confidence matches the confidence channel"
8081            );
8082            // No dynamic router in this pipeline → no skill, no coherence.
8083            assert!(tr.active_skill.is_none() && tr.recon.is_none() && !tr.switched);
8084        }
8085    }
8086
8087    #[test]
8088    fn explain_prefill_logits_match_greedy_first_token() {
8089        // `cortiq explain` shows the next-token distribution from
8090        // prefill_next_logits; its argmax must equal what greedy generate
8091        // actually emits first — otherwise explain would lie.
8092        let mut p = create_test_pipeline(8, 16, 2, 1, 4, 1, 64);
8093        p.sampler_config.temperature = 0.0;
8094        p.sampler_config.repetition_penalty = 1.0;
8095        let ids = p.tokenizer.encode("abcd");
8096        let logits = p.prefill_next_logits(&ids, None);
8097        let argmax = logits
8098            .iter()
8099            .enumerate()
8100            .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
8101            .unwrap()
8102            .0 as u32;
8103        let r = p.generate("abcd", 1, None, None).unwrap();
8104        assert_eq!(
8105            argmax, r.token_ids[0],
8106            "explain preview must match greedy emit"
8107        );
8108    }
8109
8110    #[test]
8111    fn laguna_shared_expert_is_unconditionally_added() {
8112        let matrix = |values: Vec<f32>| QTensor::from_f32(values, 2, 2);
8113        let identity = || matrix(vec![1.0, 0.0, 0.0, 1.0]);
8114        let zero_dense = || DenseFfn {
8115            gate_proj: matrix(vec![0.0; 4]),
8116            up_proj: matrix(vec![0.0; 4]),
8117            down_proj: matrix(vec![0.0; 4]),
8118            act: Act::Silu,
8119        };
8120        let shared = DenseFfn {
8121            gate_proj: identity(),
8122            up_proj: identity(),
8123            down_proj: identity(),
8124            act: Act::Silu,
8125        };
8126        let x = [1.0, 2.0];
8127        let expected = dense_ffn(&shared, &x, None);
8128        let moe = MoeFfn {
8129            router: QTensor::from_f32(vec![0.0, 0.0], 1, 2),
8130            experts: vec![zero_dense()],
8131            top_k: 1,
8132            norm_topk_prob: true,
8133            router_sigmoid: true,
8134            expert_bias: None,
8135            routed_scaling: 1.0,
8136            route_tau: None,
8137            shared: Some((shared, None)),
8138            stats: std::cell::RefCell::new(Vec::new()),
8139            act_sq: std::cell::RefCell::new(Vec::new()),
8140            act_rows: std::cell::RefCell::new(Vec::new()),
8141            mask: None,
8142            per_expert_scale: None,
8143            router_input_norm: false,
8144        };
8145        let actual = moe_ffn_cpu(&moe, &x, &[0], &[0.0], 1.0, None);
8146        for (actual, expected) in actual.iter().zip(expected) {
8147            assert!((actual - expected).abs() < 1e-6);
8148        }
8149    }
8150}