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