Skip to main content

cortiq_engine/
loader.rs

1//! Weight loader: CMF tensor directory → Pipeline.
2//!
3//! Storage rule: models WITH task masks are dequantized to f32 (masked
4//! execution needs f32 row access; skill files are small by design).
5//! Models without masks keep quantized matrices zero-copy from the mmap
6//! (`QTensor::Mapped`) — this is what lets a 15B file run in a few GB
7//! of RSS instead of 60 GB of f32.
8//!
9//! Layer kinds come from `arch.layer_types`: FullAttention loads
10//! `self_attn.*` (with auto-detected Qwen3.5 extras: per-head qk-norm by
11//! tensor presence, output gate by q_proj row count); LinearAttention
12//! loads the canonical core `vmf_attn.*` (folded at convert time).
13
14use crate::kv_cache::LayerKvCache;
15use crate::linear_core::{
16    GdnCfg, GdnWeights, ShortConvCfg, ShortConvWeights, VmfPhaseCfg, VmfPhaseWeights,
17};
18use crate::pipeline::{
19    AttnKind, DenseFfn, FfnKind, LayerWeights, MoeFfn, MtpModule, Pipeline, PipelineWeights,
20};
21use crate::qtensor::QTensor;
22use crate::sampler::SamplerConfig;
23use crate::tokenizer::Tokenizer;
24use cortiq_core::quant::dequant_tensor;
25use cortiq_core::{CmfError, CmfModel, LayerType, ModelArch};
26use std::sync::Arc;
27
28/// Tensor source selector (spec §9): backbone, one skill's overlay, or
29/// a soft superposition of top-m skills (claim 14 working tensors).
30pub enum Overlay<'a> {
31    None,
32    One(&'a str),
33    /// (skill_id, weight); weights sum to 1 (softmax(−E/T) upstream).
34    Blend(&'a [(String, f32)]),
35}
36
37impl Overlay<'_> {
38    fn blend_touches(&self, model: &CmfModel, name: &str) -> bool {
39        match self {
40            Overlay::Blend(list) => list
41                .iter()
42                .any(|(sid, _)| model.tensor(&format!("skill.{sid}.{name}")).is_some()),
43            _ => false,
44        }
45    }
46}
47
48fn dequant_by_name(model: &CmfModel, name: &str) -> Result<Vec<f32>, String> {
49    let entry = model
50        .tensor(name)
51        .ok_or_else(|| format!("tensor '{name}' not found in CMF directory"))?;
52    let mut out = vec![0.0f32; entry.n_elems()];
53    dequant_tensor(entry, model.entry_bytes(entry), &mut out)?;
54    Ok(out)
55}
56
57/// Weighted working tensor (claim 14): Σ wᵢ·Tᵢ, where Tᵢ is the
58/// skill's replacement when present, else the backbone tensor.
59fn blend_f32(model: &CmfModel, name: &str, list: &[(String, f32)]) -> Result<Vec<f32>, String> {
60    let mut acc: Option<Vec<f32>> = None;
61    for (sid, w) in list {
62        let sname = format!("skill.{sid}.{name}");
63        let src = if model.tensor(&sname).is_some() {
64            &sname
65        } else {
66            name
67        };
68        let t = dequant_by_name(model, src)?;
69        match &mut acc {
70            None => {
71                let mut t = t;
72                for v in t.iter_mut() {
73                    *v *= w;
74                }
75                acc = Some(t);
76            }
77            Some(a) => {
78                for (av, tv) in a.iter_mut().zip(&t) {
79                    *av += w * tv;
80                }
81            }
82        }
83    }
84    acc.ok_or_else(|| "empty blend".into())
85}
86
87/// Dequantize a tensor fully into f32 (norms, masked models).
88pub(crate) fn load_f32(model: &CmfModel, name: &str, ov: &Overlay) -> Result<Vec<f32>, String> {
89    if ov.blend_touches(model, name) {
90        if let Overlay::Blend(list) = ov {
91            return blend_f32(model, name, list);
92        }
93    }
94    let skill = match ov {
95        Overlay::One(s) => Some(*s),
96        _ => None,
97    };
98    let entry = model
99        .resolve_tensor(name, skill)
100        .ok_or_else(|| format!("tensor '{name}' not found in CMF directory"))?;
101    let bytes = model.entry_bytes(entry);
102    let mut out = vec![0.0f32; entry.n_elems()];
103    dequant_tensor(entry, bytes, &mut out)?;
104    Ok(out)
105}
106
107/// Build one layer's FFN (dense or MoE) under a given overlay. Shared
108/// by the static loader AND dynamic per-token skill switching
109/// (`Pipeline::set_active_skill`): switching skills = rebuilding the
110/// FFN of the touched layers, cheap because Mapped tensors are just
111/// re-resolved mmap pointers (no dequant, no copy).
112pub(crate) fn build_layer_ffn(
113    model: &Arc<CmfModel>,
114    arch: &ModelArch,
115    li: usize,
116    force_f32: bool,
117    ov: &Overlay,
118) -> Result<FfnKind, CmfError> {
119    build_ffn_at(model, arch, &format!("model.layers.{li}."), force_f32, ov)
120}
121
122/// The FFN under an arbitrary prefix. Split out of `build_layer_ffn` so the
123/// MTP block can reuse it: Qwen3.6's MTP layer carries a full MoE mlp
124/// (router + 256 experts + shared expert), not the dense one the head was
125/// first written against.
126pub(crate) fn build_ffn_at(
127    model: &Arc<CmfModel>,
128    arch: &ModelArch,
129    prefix: &str,
130    force_f32: bool,
131    ov: &Overlay,
132) -> Result<FfnKind, CmfError> {
133    let prefix = prefix.to_string();
134    let load_dense = |p: &str| -> Result<DenseFfn, CmfError> {
135        let gate_proj = load_matrix(model, &format!("{p}gate_proj.weight"), force_f32, ov)?;
136        let up_proj = load_matrix(model, &format!("{p}up_proj.weight"), force_f32, ov)?;
137        let down_proj = load_matrix(model, &format!("{p}down_proj.weight"), force_f32, ov)?;
138        // FFN triple invariant (holds for dense and each MoE expert;
139        // enforced loudly so a malformed defrag/repack — spec §11 — fails
140        // at load instead of silently mis-computing). inter' is per-layer.
141        let inter = gate_proj.rows();
142        if up_proj.rows() != inter || down_proj.cols() != inter {
143            return Err(CmfError::Parse(format!(
144                "{p}: FFN dims disagree (gate.rows={inter}, up.rows={}, \
145                 down.cols={}); all three must equal inter'",
146                up_proj.rows(),
147                down_proj.cols()
148            )));
149        }
150        if down_proj.rows() != arch.hidden_size {
151            return Err(CmfError::Parse(format!(
152                "{p}: down_proj.rows={} != hidden_size={}",
153                down_proj.rows(),
154                arch.hidden_size
155            )));
156        }
157        // The transposed down, when the file carries one: the per-token
158        // sparse path needs a neuron's down weights contiguous.
159        let dt_name = format!("{p}down_proj.t.weight");
160        let down_t = match model.tensor(&dt_name) {
161            Some(_) => Some(load_matrix(model, &dt_name, force_f32, ov)?),
162            None => None,
163        };
164        if let Some(t) = &down_t
165            && (t.rows() != inter || t.cols() != arch.hidden_size)
166        {
167            return Err(CmfError::Parse(format!(
168                "{p}down_proj.t: [{}, {}] != [{inter}, {}]",
169                t.rows(),
170                t.cols(),
171                arch.hidden_size
172            )));
173        }
174        // Task tubes (`…gate_proj.tube1.weight`, …): the neurons only
175        // some tasks compute, each stored as its own triple so every
176        // kernel runs it unchanged and an inactive tube's bytes are
177        // never touched. Numbering is dense from 1; the first gap ends
178        // the tube list.
179        let mut segs = Vec::new();
180        let mut start = inter;
181        for k in 1.. {
182            let gn = format!("{p}gate_proj.tube{k}.weight");
183            if model.tensor(&gn).is_none() {
184                break;
185            }
186            let gate = load_matrix(model, &gn, force_f32, ov)?;
187            let up = load_matrix(model, &format!("{p}up_proj.tube{k}.weight"), force_f32, ov)?;
188            let down = load_matrix(
189                model,
190                &format!("{p}down_proj.tube{k}.weight"),
191                force_f32,
192                ov,
193            )?;
194            let width = gate.rows();
195            if up.rows() != width || down.cols() != width || down.rows() != arch.hidden_size {
196                return Err(CmfError::Parse(format!(
197                    "{p}tube{k}: dims disagree (gate.rows={width}, up.rows={}, \
198                     down=[{}, {}], hidden={})",
199                    up.rows(),
200                    down.rows(),
201                    down.cols(),
202                    arch.hidden_size
203                )));
204            }
205            segs.push(crate::pipeline::FfnSeg {
206                gate,
207                up,
208                down,
209                start,
210                width,
211            });
212            start += width;
213        }
214        Ok(DenseFfn {
215            gate_proj,
216            up_proj,
217            down_proj,
218            act: crate::pipeline::Act::from_arch_full(arch),
219            down_t,
220            segs,
221        })
222    };
223    let router_name = format!("{prefix}mlp.gate.weight");
224    // Cortiq Embryo: resonance-routed experts carry NO gate tensor — the
225    // MoE is keyed on the first expert + the arch flag (a grown genome
226    // appends `mlp.experts.{E}.*` records without rewriting anything).
227    let resonance_moe = model.tensor(&router_name).is_none()
228        && arch.moe.as_ref().is_some_and(|m| m.router_resonance)
229        && model
230            .tensor(&format!("{prefix}mlp.experts.0.gate_proj.weight"))
231            .is_some();
232    if model.tensor(&router_name).is_none() && !resonance_moe {
233        return Ok(FfnKind::Dense(load_dense(&format!("{prefix}mlp."))?));
234    }
235    let cfg = arch.moe.as_ref().ok_or_else(|| {
236        CmfError::Parse(format!(
237            "{router_name} present but header has no arch.moe block"
238        ))
239    })?;
240    // Experts enumerate by TENSOR PRESENCE up to the header count — a
241    // moe-defrag'd specialist keeps a per-layer contiguous prefix of
242    // renumbered experts (fewer than arch.moe.num_experts), with the
243    // router rows sliced to match.
244    let mut experts = Vec::new();
245    for e in 0..cfg.num_experts {
246        if model
247            .tensor(&format!("{prefix}mlp.experts.{e}.gate_proj.weight"))
248            .is_none()
249        {
250            break;
251        }
252        experts.push(load_dense(&format!("{prefix}mlp.experts.{e}."))?);
253    }
254    if experts.is_empty() {
255        return Err(CmfError::Parse(format!(
256            "{prefix}: router present but no expert tensors"
257        )));
258    }
259    let shared = if model
260        .tensor(&format!("{prefix}mlp.shared_expert.gate_proj.weight"))
261        .is_some()
262    {
263        let gate_name = format!("{prefix}mlp.shared_expert_gate.weight");
264        Some((
265            load_dense(&format!("{prefix}mlp.shared_expert."))?,
266            if model.tensor(&gate_name).is_some() {
267                Some(load_matrix(model, &gate_name, force_f32, ov)?)
268            } else {
269                None
270            },
271        ))
272    } else {
273        None
274    };
275    // LFM2-MoE selection bias (`mlp.expert_bias`): present iff the model
276    // routes with a bias; loaded by tensor presence.
277    let bias_name = format!("{prefix}mlp.expert_bias");
278    let expert_bias = if model.tensor(&bias_name).is_some() {
279        Some(load_f32(model, &bias_name, ov).map_err(CmfError::Parse)?)
280    } else {
281        None
282    };
283    // CMF_MOE_TOPK=N (opt-in): route to fewer experts than the header
284    // asks. MoE decode is memory-bound — every selected expert streams
285    // its three matrices per token — so halving k halves that traffic;
286    // the renormalized top-k keeps the mixture a proper average.
287    // Quality is the experiment — measure ppl before trusting.
288    let top_k = std::env::var("CMF_MOE_TOPK")
289        .ok()
290        .and_then(|v| v.parse::<usize>().ok())
291        .filter(|&k| k >= 1 && k <= cfg.top_k)
292        .inspect(|k| tracing::info!("MoE top_k override: {} (header {})", k, cfg.top_k))
293        .unwrap_or(cfg.top_k);
294    // CMF_MOE_TAU=0.x (opt-in): adaptive routing — see MoeFfn::route_tau.
295    let route_tau = std::env::var("CMF_MOE_TAU")
296        .ok()
297        .and_then(|v| v.parse::<f32>().ok())
298        .filter(|&t| t > 0.0 && t < 1.0)
299        .inspect(|t| tracing::info!("MoE adaptive routing: tau {t}"));
300    let mask = moe_task_mask(model, &prefix, experts.len());
301    let router = if resonance_moe {
302        // never read (selection is by descriptors); zero placeholder in RAM
303        QTensor::from_f32(
304            vec![0.0; experts.len() * arch.hidden_size],
305            experts.len(),
306            arch.hidden_size,
307        )
308    } else {
309        load_matrix(model, &router_name, force_f32, ov)?
310    };
311    if router.rows() != experts.len() {
312        return Err(CmfError::Parse(format!(
313            "{router_name}: {} rows != {} experts",
314            router.rows(),
315            experts.len()
316        )));
317    }
318    let top_k = top_k.min(experts.len());
319    // Gemma-4: per-expert weight scale after the top-k renorm; its
320    // presence also marks the scale-less-rms router input (the folded
321    // router gain — see the converter).
322    let pes_name = format!("{prefix}mlp.per_expert_scale");
323    let per_expert_scale = if model.tensor(&pes_name).is_some() {
324        Some(load_f32(model, &pes_name, ov).map_err(CmfError::Parse)?)
325    } else {
326        None
327    };
328    let router_input_norm = per_expert_scale.is_some();
329    // Cortiq Embryo resonance descriptors: per-expert records
330    // `mlp.experts.{e}.desc.{mu,u,bias}` (append-only growth) or the legacy
331    // per-layer `mlp.desc.{mu,u,bias}` [E, ...]. Present → the router is a
332    // placeholder and selection is by reconstruction error.
333    let per_expert = model
334        .tensor(&format!("{prefix}mlp.experts.0.desc.mu"))
335        .is_some();
336    let resonance = if per_expert {
337        let ne_d = experts.len();
338        let hidden = arch.hidden_size;
339        let mut mu = Vec::with_capacity(ne_d * hidden);
340        let mut u = Vec::new();
341        let mut bias = Vec::with_capacity(ne_d);
342        let mut k = 0usize;
343        for e in 0..ne_d {
344            let m = load_f32(model, &format!("{prefix}mlp.experts.{e}.desc.mu"), ov)
345                .map_err(CmfError::Parse)?;
346            if m.len() != hidden {
347                return Err(CmfError::Parse(format!(
348                    "{prefix}mlp.experts.{e}.desc.mu: {} != {hidden}",
349                    m.len()
350                )));
351            }
352            mu.extend_from_slice(&m);
353            let un = format!("{prefix}mlp.experts.{e}.desc.u");
354            if model.tensor(&un).is_some() {
355                let ue = load_f32(model, &un, ov).map_err(CmfError::Parse)?;
356                let ke = ue.len() / hidden.max(1);
357                if e == 0 {
358                    k = ke;
359                }
360                if ke != k {
361                    return Err(CmfError::Parse(format!("{un}: rank {ke} != {k}")));
362                }
363                u.extend_from_slice(&ue);
364            }
365            let bn = format!("{prefix}mlp.experts.{e}.desc.bias");
366            bias.push(if model.tensor(&bn).is_some() {
367                load_f32(model, &bn, ov)
368                    .map_err(CmfError::Parse)?
369                    .first()
370                    .copied()
371                    .unwrap_or(0.0)
372            } else {
373                0.0
374            });
375        }
376        Some(crate::pipeline::Resonance { mu, u, k, bias })
377    } else if model.tensor(&format!("{prefix}mlp.desc.mu")).is_some() {
378        let mu = load_f32(model, &format!("{prefix}mlp.desc.mu"), ov).map_err(CmfError::Parse)?;
379        let ne_d = experts.len();
380        let hidden = arch.hidden_size;
381        if mu.len() != ne_d * hidden {
382            return Err(CmfError::Parse(format!(
383                "{prefix}mlp.desc.mu: {} != {ne_d}×{hidden}",
384                mu.len()
385            )));
386        }
387        let u_name = format!("{prefix}mlp.desc.u");
388        let (u, k) = if model.tensor(&u_name).is_some() {
389            let u = load_f32(model, &u_name, ov).map_err(CmfError::Parse)?;
390            let k = u.len() / (ne_d * hidden).max(1);
391            (u, k)
392        } else {
393            (Vec::new(), 0)
394        };
395        let b_name = format!("{prefix}mlp.desc.bias");
396        let bias = if model.tensor(&b_name).is_some() {
397            load_f32(model, &b_name, ov).map_err(CmfError::Parse)?
398        } else {
399            vec![0.0; ne_d]
400        };
401        Some(crate::pipeline::Resonance { mu, u, k, bias })
402    } else {
403        None
404    };
405    let moe = MoeFfn {
406        router,
407        experts,
408        top_k,
409        route_tau,
410        norm_topk_prob: cfg.norm_topk_prob,
411        router_sigmoid: cfg.router_sigmoid,
412        expert_bias,
413        routed_scaling: cfg.routed_scaling_factor.unwrap_or(1.0),
414        shared,
415        stats: std::cell::RefCell::new(Vec::new()),
416        act_sq: std::cell::RefCell::new(Vec::new()),
417        act_rows: std::cell::RefCell::new(Vec::new()),
418        mask,
419        per_expert_scale,
420        router_input_norm,
421        resonance,
422    };
423    // Gemma-4 dual-branch layer: a dense MLP coexists with the routed
424    // experts, each branch inside its own norm sandwich.
425    if model
426        .tensor(&format!("{prefix}mlp.gate_proj.weight"))
427        .is_some()
428    {
429        let norm = |suffix: &str| -> Result<Vec<f32>, CmfError> {
430            load_f32(model, &format!("{prefix}{suffix}.weight"), ov).map_err(CmfError::Parse)
431        };
432        return Ok(FfnKind::DenseMoe(Box::new(crate::pipeline::DenseMoeFfn {
433            dense: load_dense(&format!("{prefix}mlp."))?,
434            moe,
435            post_norm_1: norm("post_feedforward_layernorm_1")?,
436            pre_norm_2: norm("pre_feedforward_layernorm_2")?,
437            post_norm_2: norm("post_feedforward_layernorm_2")?,
438        })));
439    }
440    Ok(FfnKind::Moe(moe))
441}
442
443/// Task mask over routed experts: DTG-MA applied to MoE.
444///
445/// This is deliberately diagnostic-only. A route-mass sidecar can look good
446/// on a short perplexity window and still collapse during a long
447/// autoregressive generation: excluding a low-mass expert changes the model,
448/// and the changed prefix changes every later route. Consequently a nearby
449/// `<model>.moe-mass.json` is NEVER adopted implicitly. `CMF_MOE_MASK=...`
450/// remains an explicit experiment and accepts both weighted and legacy count
451/// files; production acceleration must preserve the full router and treat a
452/// non-resident winner as an exact cold completion.
453pub(crate) fn moe_task_mask(
454    _model: &std::sync::Arc<CmfModel>,
455    prefix: &str,
456    ne: usize,
457) -> Option<Vec<bool>> {
458    use std::sync::OnceLock;
459    static CFG: OnceLock<Option<(std::collections::HashMap<usize, Vec<u64>>, f64)>> =
460        OnceLock::new();
461    let cfg = CFG.get_or_init(|| {
462        let path = std::path::PathBuf::from(std::env::var("CMF_MOE_MASK").ok()?);
463        let shown = path.display();
464        let text = std::fs::read_to_string(&path)
465            .map_err(|e| tracing::warn!("CMF_MOE_MASK: cannot read {shown}: {e}"))
466            .ok()?;
467        let map: std::collections::HashMap<String, Vec<u64>> = serde_json::from_str(&text)
468            .map_err(|e| tracing::warn!("CMF_MOE_MASK: bad JSON in {shown}: {e}"))
469            .ok()?;
470        // Weighted dumps store normalized route mass in fixed-point units
471        // and therefore have per-layer totals many orders above the old
472        // top-k vote counts. The magnitude distinguishes the two explicit
473        // diagnostic formats without relying on a special filename.
474        let weighted = map
475            .values()
476            .any(|row| row.iter().copied().sum::<u64>() >= 1_000_000);
477        let cover = std::env::var("CMF_MOE_MASK_COVER")
478            .ok()
479            .and_then(|v| v.parse::<f64>().ok())
480            .filter(|&c| c > 0.0 && c <= 1.0)
481            .unwrap_or(if weighted { 0.925 } else { 0.9 });
482        tracing::info!("MoE task mask: {shown}, cover {cover}");
483        Some((
484            map.into_iter()
485                .filter_map(|(k, v)| Some((k.parse::<usize>().ok()?, v)))
486                .collect(),
487            cover,
488        ))
489    });
490    let (stats, cover) = cfg.as_ref()?;
491    // The layer index rides in the tensor prefix ("model.layers.N.").
492    let li: usize = prefix
493        .split("layers.")
494        .nth(1)?
495        .split('.')
496        .next()?
497        .parse()
498        .ok()?;
499    let counts = stats.get(&li)?;
500    if counts.len() != ne {
501        tracing::warn!(
502            "CMF_MOE_MASK: layer {li} has {} counts, model has {ne} experts — skipped",
503            counts.len()
504        );
505        return None;
506    }
507    let total: u64 = counts.iter().sum();
508    if total == 0 {
509        return None;
510    }
511    let mut order: Vec<usize> = (0..ne).collect();
512    order.sort_unstable_by_key(|&e| std::cmp::Reverse(counts[e]));
513    let mut mask = vec![false; ne];
514    let mut acc = 0u64;
515    let mut kept = 0usize;
516    for &e in &order {
517        mask[e] = true;
518        acc += counts[e];
519        kept += 1;
520        if (acc as f64) >= cover * (total as f64) {
521            break;
522        }
523    }
524    tracing::info!(
525        "MoE task mask L{li}: {kept}/{ne} experts for {:.0}% mass",
526        cover * 100.0
527    );
528    Some(mask)
529}
530
531pub(crate) fn load_matrix(
532    model: &Arc<CmfModel>,
533    name: &str,
534    force_f32: bool,
535    ov: &Overlay,
536) -> Result<QTensor, CmfError> {
537    // Claim 14: a blended working tensor is materialized in f32 and
538    // held resident (the overlay-cache slot); single skills stay
539    // zero-copy pointers into the mmap.
540    if ov.blend_touches(model, name) {
541        if let Overlay::Blend(list) = ov {
542            let entry = model
543                .tensor(name)
544                .ok_or_else(|| CmfError::MissingTensor(name.to_string()))?;
545            let data =
546                blend_f32(model, name, list).map_err(|e| CmfError::Parse(format!("blend: {e}")))?;
547            return Ok(QTensor::from_f32(data, entry.shape[0], entry.shape[1]));
548        }
549    }
550    let skill = match ov {
551        Overlay::One(s) => Some(*s),
552        _ => None,
553    };
554    // Tensor-source indirection (spec §9): the skill's replacement is
555    // read in place of the backbone tensor — either/or, never a sum.
556    let name: &str = &match skill {
557        Some(sid) if model.tensor(&format!("skill.{sid}.{name}")).is_some() => {
558            format!("skill.{sid}.{name}")
559        }
560        _ => name.to_string(),
561    };
562    let err = |e: String| CmfError::Parse(format!("weight loading: {e}"));
563    if force_f32 {
564        let entry = model
565            .tensor(name)
566            .ok_or_else(|| CmfError::MissingTensor(name.to_string()))?;
567        if entry.shape.len() != 2 {
568            return Err(err(format!("'{name}' is not 2-D")));
569        }
570        let data = load_f32(model, name, &Overlay::None).map_err(err)?;
571        Ok(QTensor::from_f32(data, entry.shape[0], entry.shape[1]))
572    } else {
573        QTensor::from_model(model, name).map_err(err)
574    }
575}
576
577impl Pipeline {
578    /// Build a runnable pipeline from an opened CMF model.
579    pub fn from_model(
580        model: &Arc<CmfModel>,
581        sampler_config: SamplerConfig,
582    ) -> Result<Self, CmfError> {
583        Self::from_model_with_skill(model, sampler_config, None)
584    }
585
586    /// Same, with a skill overlaid (spec §9): every layer tensor is
587    /// resolved through tensor-source indirection — the skill's
588    /// full-shape replacement is read in place of the backbone tensor.
589    /// No per-skill model is ever assembled: Mapped tensors are
590    /// pointers into the one shared mmap.
591    pub fn from_model_with_skill(
592        model: &Arc<CmfModel>,
593        sampler_config: SamplerConfig,
594        skill: Option<&str>,
595    ) -> Result<Self, CmfError> {
596        match skill {
597            Some(s) => Self::from_model_with_overlay(model, sampler_config, &Overlay::One(s)),
598            None => Self::from_model_with_overlay(model, sampler_config, &Overlay::None),
599        }
600    }
601
602    /// Soft superposition (claim 14): working tensors accumulated from
603    /// the given (skill, weight) list — softmax(−E/T) upstream.
604    pub fn from_model_with_blend(
605        model: &Arc<CmfModel>,
606        sampler_config: SamplerConfig,
607        blend: &[(String, f32)],
608    ) -> Result<Self, CmfError> {
609        Self::from_model_with_overlay(model, sampler_config, &Overlay::Blend(blend))
610    }
611
612    fn skill_file_guard(model: &CmfModel) -> Result<(), CmfError> {
613        // A standalone skill file carries a PARTIAL tensor set cut against
614        // a base; running it would be half a network answering questions.
615        if model.required_features & cortiq_core::format::features::SKILL_FILE != 0 {
616            return Err(CmfError::Parse(
617                "this file is a standalone SKILL, not a runnable model — attach it: \
618                 cortiq skill apply <base.cmf> <this file> -o specialist.cmf"
619                    .into(),
620            ));
621        }
622        Ok(())
623    }
624
625    fn from_model_with_overlay(
626        model: &Arc<CmfModel>,
627        sampler_config: SamplerConfig,
628        ov: &Overlay,
629    ) -> Result<Self, CmfError> {
630        // Small device caches (probe verdicts, compiled pipelines) go
631        // beside the model: it is a directory the caller demonstrably
632        // writes to, which `std::env::temp_dir()` is not inside an
633        // Android app sandbox.
634        if let Some(dir) = model.path.parent() {
635            crate::gpu::set_cache_dir(dir.to_path_buf());
636        }
637        // A new model gets a fresh verdict on whether the token graph can
638        // be built: the refusal is remembered per model, not per process.
639        crate::gpu::graph_unsupported_reset();
640        Self::skill_file_guard(model)?;
641        let skill = match ov {
642            Overlay::One(s) => Some(*s),
643            _ => None,
644        };
645        if let Some(sid) = skill {
646            let known = model.header.skills.iter().any(|s| s.id == sid)
647                || model.skill_tensors(sid).next().is_some();
648            if !known {
649                return Err(CmfError::Parse(format!(
650                    "skill '{sid}' not in this container (header.skills: {:?})",
651                    model
652                        .header
653                        .skills
654                        .iter()
655                        .map(|s| &s.id)
656                        .collect::<Vec<_>>()
657                )));
658            }
659            tracing::info!(
660                "skill '{sid}': {} replacement tensors overlaid",
661                model.skill_tensors(sid).count()
662            );
663        }
664        let arch = model.arch().clone();
665        let err = |e: String| CmfError::Parse(format!("weight loading: {e}"));
666        if let Some(heads) = &arch.attention_heads_per_layer {
667            if heads.len() != arch.num_layers {
668                return Err(CmfError::Parse(format!(
669                    "arch.attention_heads_per_layer has {} entries, expected {}",
670                    heads.len(),
671                    arch.num_layers
672                )));
673            }
674            if let Some((li, &nh)) = heads
675                .iter()
676                .enumerate()
677                .find(|(_, nh)| **nh == 0 || **nh % arch.num_kv_heads != 0)
678            {
679                return Err(CmfError::Parse(format!(
680                    "layer {li} has {nh} Q heads, which must be nonzero and divisible by {} KV heads",
681                    arch.num_kv_heads
682                )));
683            }
684        }
685        if arch
686            .layer_types
687            .iter()
688            .any(|t| matches!(t, LayerType::SlidingAttention))
689            && arch.sliding_window.is_none()
690        {
691            return Err(CmfError::Parse(
692                "model has SlidingAttention layers but no arch.sliding_window".into(),
693            ));
694        }
695
696        // Masks × quantized mmap: only the HEAD-mask path needs f32
697        // slices, so f32 is forced only when some mask actually restricts
698        // attention heads. FFN masks run sparse directly on the quant
699        // bytes (sparse_ffn_quant), and embed/lm_head are never masked.
700        // The old condition forced f32 for ANY mask: a 4.17 B file whose
701        // mask touched only the FFN dequantized to 16.7 GB at load and
702        // ran the bare-f32 GEMMs — 0.0 tok/s on a laptop that swapped,
703        // and a 30× crawl on a 48-core server. A mask with no head rows
704        // costs nothing now.
705        let heads_masked = model.masks.masks.iter().any(|m| {
706            m.head_masks.iter().any(|row| {
707                let mut bits = 0usize;
708                for &b in row.iter() {
709                    bits += b.count_ones() as usize;
710                }
711                !row.is_empty() && bits < arch.num_attention_heads
712            })
713        });
714        let force_f32 = heads_masked; // attention only (head masks)
715
716        // ── Tokenizer: embedded → sidecar → byte-level fallback ──
717        let mut tokenizer = if let Some(vocab_bytes) = &model.vocab {
718            Tokenizer::from_bytes(vocab_bytes)
719                .map_err(|e| CmfError::Parse(format!("embedded tokenizer: {e}")))?
720        } else {
721            let sidecar = model.path.with_file_name("tokenizer.json");
722            if sidecar.exists() {
723                Tokenizer::from_file(&sidecar)
724                    .map_err(|e| CmfError::Parse(format!("sidecar tokenizer: {e}")))?
725            } else {
726                tracing::warn!("no tokenizer in file or sidecar — using byte-level fallback");
727                Tokenizer::byte_level()
728            }
729        };
730        // Chat/eos bundle (spec §6.1): the FILE defines chat behavior.
731        if let Some(tc) = &model.header.tokenizer_config {
732            tokenizer.chat_template = tc.chat_template.clone();
733            tokenizer.extra_eos.extend(tc.eos_token_ids.iter().copied());
734            if tokenizer.bos_token_id.is_none() {
735                tokenizer.bos_token_id = tc.bos_token_id;
736            }
737            tracing::info!(
738                "chat bundle: template {} chars, {} stop ids",
739                tc.chat_template.as_deref().map(str::len).unwrap_or(0),
740                tc.eos_token_ids.len()
741            );
742        }
743        // Gemma's contract requires <bos> at sequence start, but its
744        // tokenizer.json post-processor does not add it (the chat
745        // template does). Raw prompts need it too — word salad without.
746        if arch.arch_name.to_lowercase().contains("gemma") && tokenizer.bos_token_id.is_some() {
747            tokenizer.add_bos = true;
748        }
749
750        // ── Top-level weights (never masked → always quantized) ──
751        let embed_tokens = load_matrix(model, "model.embed_tokens.weight", false, ov)?;
752        let final_norm = if arch.qwen4_exp.is_some() {
753            // qwen4_exp folds its four streams through a learned global
754            // hyper mixer and has no conventional final RMSNorm.
755            vec![0.0; arch.hidden_size]
756        } else {
757            load_f32(model, "model.norm.weight", ov).map_err(err)?
758        };
759        let lm_head = if model.tensor("lm_head.weight").is_some() {
760            load_matrix(model, "lm_head.weight", false, ov)?
761        } else if arch.tie_word_embeddings {
762            // Tied: reuse the embedding matrix (re-open, cheap for Mapped).
763            load_matrix(model, "model.embed_tokens.weight", false, ov)?
764        } else {
765            return Err(CmfError::MissingTensor(
766                "lm_head.weight (and tie_word_embeddings is false)".into(),
767            ));
768        };
769
770        // ── Linear-core geometry (required if any linear layer exists) ──
771        let has_linear = arch
772            .layer_types
773            .iter()
774            .any(|t| matches!(t, LayerType::LinearAttention));
775        let mut vmf_cfg = None;
776        let mut gdn_cfg = None;
777        if has_linear {
778            let lc = arch.linear_core.as_ref().ok_or_else(|| {
779                CmfError::Parse(
780                    "model has LinearAttention layers but no arch.linear_core — \
781                     reconvert with the current converter"
782                        .into(),
783                )
784            })?;
785            let need = |v: Option<usize>, name: &str| {
786                v.ok_or_else(|| CmfError::Parse(format!("linear core needs arch.{name}")))
787            };
788            match lc.kind.as_str() {
789                "vmf_phase" => {
790                    vmf_cfg = Some(VmfPhaseCfg {
791                        num_heads: lc.num_heads,
792                        nphase: need(lc.nphase, "linear_core.nphase")?,
793                        value_head_dim: lc.value_head_dim,
794                        hidden_size: arch.hidden_size,
795                        // θ-mass (η′): default 0 (massless); CMF_PHASE_MASS
796                        // widens the phase kernel for folded-unhealed models.
797                        phase_mass: std::env::var("CMF_PHASE_MASS")
798                            .ok()
799                            .and_then(|v| v.parse().ok())
800                            .unwrap_or(0.0),
801                    });
802                }
803                "gated_delta_net" => {
804                    gdn_cfg = Some(GdnCfg {
805                        num_v_heads: lc.num_heads,
806                        num_k_heads: need(arch.linear_num_key_heads, "linear_num_key_heads")?,
807                        key_head_dim: need(arch.linear_key_head_dim, "linear_key_head_dim")?,
808                        value_head_dim: lc.value_head_dim,
809                        conv_kernel: need(arch.linear_conv_kernel_dim, "linear_conv_kernel_dim")?,
810                        hidden_size: arch.hidden_size,
811                        rms_eps: arch.rms_norm_eps,
812                        output_gate_sigmoid: false,
813                    });
814                }
815                other => {
816                    return Err(CmfError::Parse(format!(
817                        "unknown linear core '{other}' (this runtime executes: \
818                         gated_delta_net, vmf_phase)"
819                    )));
820                }
821            }
822        }
823
824        // ── KDA geometry (Kimi Linear / Kimi-K3 delta-attention layers) ──
825        let has_kda = arch.layer_types.iter().any(|t| matches!(t, LayerType::Kda));
826        let kda_cfg = if has_kda {
827            let need = |v: Option<usize>, name: &str| {
828                v.ok_or_else(|| CmfError::Parse(format!("KDA core needs arch.{name}")))
829            };
830            Some(crate::linear_core::KdaCfg {
831                num_heads: need(arch.linear_num_key_heads, "linear_num_key_heads")?,
832                head_k_dim: need(arch.linear_key_head_dim, "linear_key_head_dim")?,
833                head_v_dim: need(arch.linear_value_head_dim, "linear_value_head_dim")?,
834                conv_kernel: need(arch.linear_conv_kernel_dim, "linear_conv_kernel_dim")?,
835                hidden_size: arch.hidden_size,
836                rms_eps: arch.rms_norm_eps,
837            })
838        } else {
839            None
840        };
841
842        // ── Short-convolution geometry (LFM2 conv mixer layers) ──
843        let has_short_conv = arch
844            .layer_types
845            .iter()
846            .any(|t| matches!(t, LayerType::ShortConv));
847        let short_conv_cfg = if has_short_conv {
848            Some(ShortConvCfg {
849                hidden_size: arch.hidden_size,
850                kernel: arch.linear_conv_kernel_dim.ok_or_else(|| {
851                    CmfError::Parse(
852                        "model has ShortConv layers but no arch.linear_conv_kernel_dim — \
853                         reconvert with the current converter"
854                            .into(),
855                    )
856                })?,
857            })
858        } else {
859            None
860        };
861
862        // ── Layers ──
863        let load_full_attn = |prefix: &str, layer: Option<usize>| -> Result<AttnKind, CmfError> {
864            let t = |suffix: &str| load_matrix(model, &format!("{prefix}{suffix}"), force_f32, ov);
865            let n = |suffix: &str| -> Option<Vec<f32>> {
866                model
867                    .tensor(&format!("{prefix}{suffix}"))
868                    .and_then(|_| load_f32(model, &format!("{prefix}{suffix}"), ov).ok())
869            };
870            // DeepSeek-V2 MLA: the latent projections replace the k/v pair.
871            if let Some(mla) = arch.mla.as_ref() {
872                // Compressed q (K3/V3): q_a → rms → q_b; direct otherwise.
873                let (q_proj, q_a, q_a_norm) = if mla.q_lora_rank.is_some() {
874                    (
875                        t("self_attn.q_b_proj.weight")?,
876                        Some(t("self_attn.q_a_proj.weight")?),
877                        Some(n("self_attn.q_a_layernorm.weight").ok_or_else(|| {
878                            CmfError::Parse(format!("{prefix}: MLA needs q_a_layernorm"))
879                        })?),
880                    )
881                } else {
882                    (t("self_attn.q_proj.weight")?, None, None)
883                };
884                let hd = mla.qk_rope_head_dim + mla.qk_nope_head_dim;
885                let nh = q_proj.rows() / hd;
886                // YaRN mscale²: DeepSeek corrects the softmax scale by
887                // (0.1·mscale_all_dim·ln(factor)+1)².
888                let mut scale = 1.0 / (hd as f32).sqrt();
889                if let Some(y) = arch.yarn.as_ref() {
890                    if let Some(m) = y.mscale_all_dim.filter(|&m| m > 0.0) {
891                        let ms = 0.1 * m * y.factor.ln() + 1.0;
892                        scale *= ms * ms;
893                    }
894                }
895                return Ok(AttnKind::Mla(Box::new(crate::pipeline::MlaWeights {
896                    q_proj,
897                    q_a,
898                    q_a_norm,
899                    kv_a: t("self_attn.kv_a_proj_with_mqa.weight")?,
900                    kv_a_norm: n("self_attn.kv_a_layernorm.weight").ok_or_else(|| {
901                        CmfError::Parse(format!("{prefix}: MLA needs kv_a_layernorm"))
902                    })?,
903                    kv_b: t("self_attn.kv_b_proj.weight")?,
904                    o_proj: t("self_attn.o_proj.weight")?,
905                    nh,
906                    qk_rope: mla.qk_rope_head_dim,
907                    qk_nope: mla.qk_nope_head_dim,
908                    v_dim: mla.v_head_dim,
909                    lora: mla.kv_lora_rank,
910                    scale,
911                    nope: mla.nope,
912                })));
913            }
914            let wq = t("self_attn.q_proj.weight")?;
915            let nh = layer
916                .and_then(|li| {
917                    arch.attention_heads_per_layer
918                        .as_ref()
919                        .and_then(|v| v.get(li).copied())
920                })
921                .unwrap_or(arch.num_attention_heads);
922            // Qwen3.5 output gate: q_proj rows = 2·nh·hd (per-head [q; gate]).
923            // Gemma-4 global layers legitimately have nh·global_head_dim
924            // rows (which can equal 2·nh·hd) — never gated.
925            let output_gate = arch.global_head_dim.is_none() && wq.rows() == 2 * nh * arch.head_dim;
926            // Gemma-4 global layers run MQA at global_head_dim — their
927            // q_proj legitimately carries nh·ghd rows.
928            let is_global_layer = arch.global_head_dim.is_some()
929                && layer.is_some_and(|li| {
930                    arch.sliding_window_pattern
931                        .is_some_and(|p| p > 0 && (li + 1) % p == 0)
932                });
933            let expect = if is_global_layer {
934                nh * arch.global_head_dim.unwrap_or(arch.head_dim)
935            } else {
936                nh * arch.head_dim
937            };
938            if !output_gate && wq.rows() != expect {
939                return Err(CmfError::Parse(format!(
940                    "{prefix}self_attn.q_proj.weight rows={} != heads({nh}) * head_dim({})",
941                    wq.rows(),
942                    expect / nh.max(1)
943                )));
944            }
945            let gate_name = format!("{prefix}self_attn.g_proj.weight");
946            let softplus_gate = if model.tensor(&gate_name).is_some() {
947                let gate = load_matrix(model, &gate_name, force_f32, ov)?;
948                if gate.cols() != arch.hidden_size {
949                    return Err(CmfError::Parse(format!(
950                        "{gate_name} cols={} != hidden_size ({})",
951                        gate.cols(),
952                        arch.hidden_size
953                    )));
954                }
955                let per_head = if gate.rows() == nh {
956                    true
957                } else if gate.rows() == nh * arch.head_dim {
958                    false
959                } else {
960                    return Err(CmfError::Parse(format!(
961                        "{gate_name} rows={} must equal heads ({nh}) or heads*head_dim ({})",
962                        gate.rows(),
963                        nh * arch.head_dim
964                    )));
965                };
966                Some((gate, per_head))
967            } else {
968                None
969            };
970            // Qwen2-family projection biases (by tensor presence).
971            let bias = match (
972                n("self_attn.q_proj.bias"),
973                n("self_attn.k_proj.bias"),
974                n("self_attn.v_proj.bias"),
975            ) {
976                (Some(a), Some(b), Some(c)) => Some((a, b, c)),
977                _ => None,
978            };
979            Ok(AttnKind::Full {
980                wq,
981                wk: t("self_attn.k_proj.weight")?,
982                wv: t("self_attn.v_proj.weight")?,
983                wo: t("self_attn.o_proj.weight")?,
984                q_norm: n("self_attn.q_norm.weight"),
985                k_norm: n("self_attn.k_norm.weight"),
986                output_gate,
987                softplus_gate,
988                bias,
989            })
990        };
991
992        let load_linear_attn = |prefix: &str| -> Result<AttnKind, CmfError> {
993            if gdn_cfg.is_some() {
994                // Faithful vendor operator: tensor names 1:1 with the source.
995                let t = |suffix: &str| {
996                    load_matrix(
997                        model,
998                        &format!("{prefix}linear_attn.{suffix}"),
999                        force_f32,
1000                        ov,
1001                    )
1002                };
1003                let f = |suffix: &str| {
1004                    load_f32(model, &format!("{prefix}linear_attn.{suffix}"), ov).map_err(err)
1005                };
1006                return Ok(AttnKind::LinearGdn(GdnWeights {
1007                    in_proj_qkv: t("in_proj_qkv.weight")?,
1008                    in_proj_z: t("in_proj_z.weight")?,
1009                    in_proj_a: t("in_proj_a.weight")?,
1010                    in_proj_b: t("in_proj_b.weight")?,
1011                    conv1d: f("conv1d.weight")?,
1012                    a_log: f("A_log")?,
1013                    dt_bias: f("dt_bias")?,
1014                    norm: f("norm.weight")?,
1015                    out_proj: t("out_proj.weight")?,
1016                }));
1017            }
1018            let t = |suffix: &str| {
1019                load_matrix(model, &format!("{prefix}vmf_attn.{suffix}"), force_f32, ov)
1020            };
1021            let a_log = load_f32(model, &format!("{prefix}vmf_attn.A_log"), ov).map_err(err)?;
1022            // Selective-write gate κ (hybrid_k core): optional by tensor
1023            // presence — files without it run the classic phase kernel
1024            // bit-identically.
1025            let k_gate = if model
1026                .tensor(&format!("{prefix}vmf_attn.k_gate.weight"))
1027                .is_some()
1028            {
1029                Some((
1030                    t("k_gate.weight")?,
1031                    load_f32(model, &format!("{prefix}vmf_attn.k_gate.bias"), ov).map_err(err)?,
1032                ))
1033            } else {
1034                None
1035            };
1036            // Optional short causal conv (embryo genomes born with
1037            // --conv-k): `[hidden, 1, k]` taps, flattened.
1038            let conv = if model
1039                .tensor(&format!("{prefix}vmf_attn.conv1d.weight"))
1040                .is_some()
1041            {
1042                Some(load_f32(model, &format!("{prefix}vmf_attn.conv1d.weight"), ov).map_err(err)?)
1043            } else {
1044                None
1045            };
1046            Ok(AttnKind::Linear(VmfPhaseWeights {
1047                thq: t("thq.weight")?,
1048                conv,
1049                thk: t("thk.weight")?,
1050                v_proj: t("v_proj.weight")?,
1051                out_proj: t("out_proj.weight")?,
1052                decay: a_log.iter().map(|&a| (-(a as f64).exp()).exp()).collect(),
1053                k_gate,
1054            }))
1055        };
1056
1057        // LFM2 short-conv mixer: in_proj [3·hidden, hidden], a depthwise
1058        // conv (stored f16 as `[hidden, 1, kernel]` → flattened taps), and
1059        // out_proj [hidden, hidden]. Names canonicalized at convert time.
1060        let load_short_conv = |prefix: &str| -> Result<AttnKind, CmfError> {
1061            let t = |suffix: &str| {
1062                load_matrix(
1063                    model,
1064                    &format!("{prefix}short_conv.{suffix}"),
1065                    force_f32,
1066                    ov,
1067                )
1068            };
1069            Ok(AttnKind::ShortConv(ShortConvWeights {
1070                in_proj: t("in_proj.weight")?,
1071                conv: load_f32(model, &format!("{prefix}short_conv.conv.weight"), ov)
1072                    .map_err(err)?,
1073                out_proj: t("out_proj.weight")?,
1074            }))
1075        };
1076
1077        // KDA layer (Kimi Linear / Kimi-K3): faithful vendor tensors under
1078        // the `kda_attn.` canonical prefix. The output gate is full-rank
1079        // (g_proj, K3) or low-rank (g_a/g_b, Kimi-Linear-48B) by presence.
1080        let load_kda = |prefix: &str| -> Result<AttnKind, CmfError> {
1081            let t = |suffix: &str| {
1082                load_matrix(model, &format!("{prefix}kda_attn.{suffix}"), force_f32, ov)
1083            };
1084            let f = |suffix: &str| {
1085                load_f32(model, &format!("{prefix}kda_attn.{suffix}"), ov).map_err(err)
1086            };
1087            let gate = if model
1088                .tensor(&format!("{prefix}kda_attn.g_proj.weight"))
1089                .is_some()
1090            {
1091                crate::linear_core::KdaOutGate::Full(t("g_proj.weight")?)
1092            } else {
1093                crate::linear_core::KdaOutGate::LowRank(
1094                    t("g_a_proj.weight")?,
1095                    t("g_b_proj.weight")?,
1096                )
1097            };
1098            Ok(AttnKind::Kda(Box::new(crate::linear_core::KdaWeights {
1099                q_proj: t("q_proj.weight")?,
1100                k_proj: t("k_proj.weight")?,
1101                v_proj: t("v_proj.weight")?,
1102                conv_q: f("q_conv1d.weight")?,
1103                conv_k: f("k_conv1d.weight")?,
1104                conv_v: f("v_conv1d.weight")?,
1105                f_a: t("f_a_proj.weight")?,
1106                f_b: t("f_b_proj.weight")?,
1107                dt_bias: f("dt_bias")?,
1108                a_log: f("A_log")?,
1109                b_proj: t("b_proj.weight")?,
1110                gate,
1111                o_norm: f("o_norm.weight")?,
1112                o_proj: t("o_proj.weight")?,
1113                gate_lower_bound: arch.kda_gate_lower_bound.map(|v| v as f32),
1114            })))
1115        };
1116
1117        fn anyhow_like(ok: bool) -> Result<(), ()> {
1118            if ok { Ok(()) } else { Err(()) }
1119        }
1120        let mut layers = Vec::with_capacity(arch.num_layers);
1121        let is_g3n = arch.g3n.is_some();
1122        // Architectures that load their own layer stack below. DeepSeek-V4
1123        // has none of the canonical projections — no q/k/v/o_proj, no
1124        // per-layer gate_proj — so the generic loop would demand
1125        // `self_attn.q_proj.weight` and fail before its own loader ever ran.
1126        let owns_its_layers = is_g3n || arch.arch_name == "deepseek_v4" || arch.qwen4_exp.is_some();
1127        for li in 0..(if owns_its_layers { 0 } else { arch.num_layers }) {
1128            let prefix = format!("model.layers.{li}.");
1129            let attn = match arch.layer_types.get(li) {
1130                Some(LayerType::LinearAttention) => load_linear_attn(&prefix)?,
1131                Some(LayerType::Kda) => load_kda(&prefix)?,
1132                Some(LayerType::ShortConv) => load_short_conv(&prefix)?,
1133                _ => load_full_attn(&prefix, Some(li))?,
1134            };
1135            // Gemma-2/3 sandwich: `pre_feedforward_layernorm` present →
1136            // it is the pre-FFN norm, and post_attention/post_feedforward
1137            // норms apply to the branch OUTPUTS before their residuals.
1138            let pre_ffn = format!("{prefix}pre_feedforward_layernorm.weight");
1139            let sandwich = model.tensor(&pre_ffn).is_some();
1140            layers.push(LayerWeights {
1141                input_norm: load_f32(model, &format!("{prefix}input_layernorm.weight"), ov)
1142                    .map_err(err)?,
1143                post_norm: if sandwich {
1144                    load_f32(model, &pre_ffn, ov).map_err(err)?
1145                } else {
1146                    load_f32(
1147                        model,
1148                        &format!("{prefix}post_attention_layernorm.weight"),
1149                        ov,
1150                    )
1151                    .map_err(err)?
1152                },
1153                attn_out_norm: if sandwich {
1154                    Some(
1155                        load_f32(
1156                            model,
1157                            &format!("{prefix}post_attention_layernorm.weight"),
1158                            ov,
1159                        )
1160                        .map_err(err)?,
1161                    )
1162                } else {
1163                    None
1164                },
1165                ffn_out_norm: if sandwich {
1166                    Some(
1167                        load_f32(
1168                            model,
1169                            &format!("{prefix}post_feedforward_layernorm.weight"),
1170                            ov,
1171                        )
1172                        .map_err(err)?,
1173                    )
1174                } else {
1175                    None
1176                },
1177                // Gemma-4: learned scalar multiplying the layer output.
1178                layer_scale: model
1179                    .tensor(&format!("{prefix}layer_scalar"))
1180                    .and_then(|_| {
1181                        load_f32(model, &format!("{prefix}layer_scalar"), ov)
1182                            .ok()
1183                            .and_then(|v| v.first().copied())
1184                    }),
1185                // FFN always quantized — masks run sparse on quant bytes.
1186                ffn: build_layer_ffn(model, &arch, li, false, ov)?,
1187                attn,
1188            });
1189        }
1190
1191        // ── MTP head (optional, spec §2.1) ──
1192        //
1193        // The header declaring an MTP head is not the same as the file
1194        // carrying one. DeepSeek-V4's config announces a next-token predictor
1195        // whose weights the converter does not map (they are spelled `mtp.N.*`
1196        // and have none of the canonical projections), so demanding
1197        // `model.mtp.layers.0.self_attn.q_proj.weight` failed a model that is
1198        // otherwise complete. Presence in the directory decides.
1199        let mtp_present = model
1200            .tensor("model.mtp.layers.0.self_attn.q_proj.weight")
1201            .is_some()
1202            || model.tensor("model.mtp.eh_proj.weight").is_some();
1203        // DeepSeek-V4 writes its own stack under `model.mtp.N.*` — three full
1204        // layers, not a V3-style single block — so it cannot go through the
1205        // path below and is loaded by the dsv4 arm instead. Saying the file
1206        // "carries none" was a false negative worth six gigabytes.
1207        let dsv4_mtp = model.tensor("model.mtp.0.main_proj.weight").is_some();
1208        if arch.mtp.is_some() && !mtp_present && !dsv4_mtp {
1209            tracing::info!(
1210                "header declares an MTP head but the file carries none — \
1211                 loading without it"
1212            );
1213        }
1214        let mtp = if let Some(cfg) = arch.mtp.as_ref().filter(|_| mtp_present) {
1215            if cfg.num_layers != 1 {
1216                return Err(CmfError::Parse(format!(
1217                    "MTP with {} blocks not supported yet (only 1)",
1218                    cfg.num_layers
1219                )));
1220            }
1221            let p = "model.mtp.";
1222            let attn = load_full_attn("model.mtp.layers.0.", None)?;
1223            Some(MtpModule {
1224                enorm: load_f32(model, &format!("{p}enorm.weight"), ov).map_err(err)?,
1225                hnorm: load_f32(model, &format!("{p}hnorm.weight"), ov).map_err(err)?,
1226                eh_proj: load_matrix(model, &format!("{p}eh_proj.weight"), false, ov)?,
1227                layer: LayerWeights {
1228                    attn_out_norm: None,
1229                    ffn_out_norm: None,
1230                    layer_scale: None,
1231                    input_norm: load_f32(model, &format!("{p}layers.0.input_layernorm.weight"), ov)
1232                        .map_err(err)?,
1233                    post_norm: load_f32(
1234                        model,
1235                        &format!("{p}layers.0.post_attention_layernorm.weight"),
1236                        ov,
1237                    )
1238                    .map_err(err)?,
1239                    // Whatever the block actually carries: DeepSeek's MTP
1240                    // layer is dense, Qwen3.6's is a full MoE (router + 256
1241                    // experts + shared). Same builder as a backbone layer.
1242                    ffn: build_ffn_at(model, &arch, &format!("{p}layers.0."), false, ov)?,
1243                    attn,
1244                },
1245                final_norm: load_f32(model, &format!("{p}norm.weight"), ov).map_err(err)?,
1246                kv: LayerKvCache::new(arch.num_kv_heads, arch.head_dim),
1247            })
1248        } else {
1249            None
1250        };
1251
1252        tracing::info!(
1253            "Pipeline loaded: {} | {}L ({} linear) | {:.2}B params | storage: {} | MTP: {}",
1254            arch.arch_name,
1255            arch.num_layers,
1256            arch.layer_types
1257                .iter()
1258                .filter(|t| matches!(t, LayerType::LinearAttention))
1259                .count(),
1260            model.total_param_count() as f64 / 1e9,
1261            if force_f32 {
1262                "f32 (masked)"
1263            } else {
1264                "quantized mmap"
1265            },
1266            if mtp.is_some() { "yes" } else { "no" }
1267        );
1268
1269        // KV window: the descriptor's max, capped for dev-box safety;
1270        // CMF_MAX_SEQ overrides the cap (long-context runs).
1271        // 8192 was the silent quality cliff of the Qwen3.8 bring-up: at
1272        // the cap the wgpu token graph declines, the host evicts half the
1273        // KV, and a GDN hybrid's recurrent state goes stale — the model
1274        // stays fluent and loses its mind (Django internals, a Turkish
1275        // essay, an em-dash loop; one failure, three costumes). 32768
1276        // covers every long-form run we actually ship while keeping the
1277        // graph's device KV mirror affordable beside the weights;
1278        // CMF_MAX_SEQ still overrides in either direction.
1279        let cap = std::env::var("CMF_MAX_SEQ")
1280            .ok()
1281            .and_then(|v| v.parse::<usize>().ok())
1282            .unwrap_or(32_768);
1283        let max_seq_len = arch.max_position_embeddings.min(cap);
1284
1285        // Looped Transformer: total virtual layers = physical × num_loops.
1286        let total_layers = arch.num_layers * arch.num_loops;
1287
1288        let mut pipeline = Pipeline::new(
1289            tokenizer,
1290            PipelineWeights {
1291                embed_tokens,
1292                layers,
1293                lm_head,
1294                final_norm,
1295            },
1296            arch.hidden_size,
1297            arch.intermediate_size,
1298            arch.num_attention_heads,
1299            arch.num_kv_heads,
1300            arch.head_dim,
1301            total_layers,
1302            arch.num_layers, // physical layers in weights
1303            arch.loop_final_norm,
1304            arch.vocab_size,
1305            arch.rms_norm_eps,
1306            arch.rope_theta as f32,
1307            arch.norm_style,
1308            max_seq_len,
1309            sampler_config,
1310        );
1311        let rotary = ((arch.head_dim as f32 * arch.partial_rotary_factor) as usize).max(2);
1312        pipeline.set_rotary(rotary, arch.rope_theta as f32);
1313        pipeline.attention_heads_per_layer = arch.attention_heads_per_layer.clone();
1314        if let Some(yarn) = &arch.yarn {
1315            pipeline.inv_freq = std::sync::Arc::new(crate::attention::yarn_inv_freq(
1316                rotary,
1317                arch.rope_theta as f32,
1318                yarn.factor,
1319                yarn.original_max_position_embeddings,
1320                yarn.beta_fast,
1321                yarn.beta_slow,
1322            ));
1323            pipeline.rope_scale = yarn.attention_factor;
1324        }
1325        // Gemma-family extras: embedding scale, attention-scale
1326        // override, and (Gemma-3) sliding-window layers with their own
1327        // local RoPE base.
1328        pipeline.embed_multiplier = arch.embed_multiplier;
1329        pipeline.logit_multiplier = arch.logit_multiplier;
1330        if let Some(qpas) = arch.query_pre_attn_scalar {
1331            pipeline.attn_scale = 1.0 / (qpas as f32).sqrt();
1332        }
1333        if let (Some(w), Some(p)) = (arch.sliding_window, arch.sliding_window_pattern) {
1334            pipeline.swa = Some((w, p));
1335            if let Some(base) = arch.rope_local_base_freq {
1336                pipeline.inv_freq_local = Some(std::sync::Arc::new(
1337                    crate::attention::rope_inv_freq(rotary, base as f32),
1338                ));
1339            }
1340        }
1341        let explicit_sliding: Vec<bool> = arch
1342            .layer_types
1343            .iter()
1344            .map(|t| matches!(t, cortiq_core::LayerType::SlidingAttention))
1345            .collect();
1346        if explicit_sliding.iter().any(|&v| v) {
1347            pipeline.sliding_layers = Some(explicit_sliding);
1348            if let Some(w) = arch.sliding_window {
1349                pipeline.swa = Some((w, usize::MAX));
1350            }
1351            let local_rotary = ((arch.head_dim as f32
1352                * arch
1353                    .local_partial_rotary_factor
1354                    .unwrap_or(arch.partial_rotary_factor))
1355                as usize)
1356                .max(2);
1357            pipeline.rotary_dim_local = Some(local_rotary);
1358            if let Some(base) = arch.rope_local_base_freq {
1359                pipeline.inv_freq_local = Some(std::sync::Arc::new(
1360                    crate::attention::rope_inv_freq(local_rotary, base as f32),
1361                ));
1362            }
1363        }
1364        // Gemma-4: global layers run their own geometry (MQA at
1365        // global_head_dim) with a proportional RoPE — the first
1366        // factor·head_dim dims rotate, the zero-padded tail is identity.
1367        if let (Some(ghd), Some(gkv)) = (arch.global_head_dim, arch.num_global_kv_heads) {
1368            pipeline.global_attn = Some((ghd, gkv));
1369            let prf = arch.global_partial_rotary_factor.unwrap_or(1.0);
1370            let half = ghd / 2;
1371            let ra = (((prf * ghd as f32) as usize) / 2).min(half);
1372            let mut f = vec![0.0f32; half];
1373            for (i, slot) in f.iter_mut().enumerate().take(ra) {
1374                *slot = 1.0 / (arch.rope_theta as f32).powf(2.0 * i as f32 / ghd as f32);
1375            }
1376            pipeline.inv_freq_global = Some(std::sync::Arc::new(f));
1377            // Re-shape the global layers' KV storage to their geometry.
1378            // An explicit layer_types map wins over the numeric pattern
1379            // (explicit tags set swa's pattern to usize::MAX, which
1380            // would otherwise leave every global cache mis-shaped).
1381            let global_at = |li: usize| -> bool {
1382                match &pipeline.sliding_layers {
1383                    Some(map) => !map.get(li).copied().unwrap_or(false),
1384                    None => pipeline
1385                        .swa
1386                        .map(|(_, p)| p > 0 && p != usize::MAX && (li + 1) % p == 0)
1387                        .unwrap_or(false),
1388                }
1389            };
1390            for li in 0..arch.num_layers {
1391                if global_at(li) {
1392                    pipeline.kv_cache.layers[li] = crate::kv_cache::LayerKvCache::new(gkv, ghd);
1393                }
1394            }
1395        }
1396        // MLA (DeepSeek-V2): the expand-to-MHA cache holds nh heads of
1397        // rope+nope dims; rotary covers the rope prefix.
1398        if let Some(mla) = arch.mla.as_ref() {
1399            let hd = mla.qk_rope_head_dim + mla.qk_nope_head_dim;
1400            pipeline.head_dim = hd;
1401            pipeline.num_kv_heads = arch.num_attention_heads;
1402            pipeline.rotary_dim = mla.qk_rope_head_dim;
1403            let half = mla.qk_rope_head_dim / 2;
1404            let mut f = vec![0.0f32; half];
1405            for (i, slot) in f.iter_mut().enumerate() {
1406                *slot = 1.0
1407                    / (arch.rope_theta as f32).powf(2.0 * i as f32 / mla.qk_rope_head_dim as f32);
1408            }
1409            pipeline.inv_freq = std::sync::Arc::new(f);
1410            for li in 0..arch.num_layers {
1411                pipeline.kv_cache.layers[li] =
1412                    crate::kv_cache::LayerKvCache::new(arch.num_attention_heads, hd);
1413            }
1414        }
1415        // Per-frequency rope divisors (MiniCPM3 longrope short_factor):
1416        // served at the native window with the trained per-dim factors.
1417        // Applied after every inv_freq build (plain, YaRN, MLA).
1418        if let Some(fac) = &arch.rope_freq_factors {
1419            let mut f = pipeline.inv_freq.as_ref().clone();
1420            for (i, v) in f.iter_mut().enumerate() {
1421                if let Some(&d) = fac.get(i) {
1422                    *v /= d as f32;
1423                }
1424            }
1425            pipeline.inv_freq = std::sync::Arc::new(f);
1426        }
1427        pipeline.attn_v_norm = arch.attn_v_norm;
1428        pipeline.final_softcap = arch.final_logit_softcapping.map(|c| c as f32);
1429        // Cortiq Embryo hierarchical head: cluster matrix → two-level log-probs.
1430        if let Some(ncl) = arch.head_clusters {
1431            let cm = load_f32(model, "lm_head.clusters.weight", ov).map_err(err)?;
1432            if cm.len() != ncl * arch.hidden_size {
1433                return Err(CmfError::Parse(format!(
1434                    "lm_head.clusters.weight: {} != {ncl}×{}",
1435                    cm.len(),
1436                    arch.hidden_size
1437                )));
1438            }
1439            pipeline.head_clusters = Some(std::sync::Arc::new(cm));
1440        }
1441        pipeline.attn_softcap = arch.attn_logit_softcapping.unwrap_or(0.0) as f32;
1442        pipeline.vmf_cfg = vmf_cfg;
1443        pipeline.gdn_cfg = gdn_cfg;
1444        pipeline.kda_cfg = kda_cfg;
1445        if arch.qwen4_exp.is_some() {
1446            let (globals, layers, cfg, state) = crate::qwen4_exp::load(model, &arch)?;
1447            pipeline.qwen4_exp = Some(Box::new((globals, layers, cfg, state)));
1448        }
1449        if let Some(gc) = arch.g3n.as_ref() {
1450            use crate::g3n::{G3nAltUp, G3nGlobals, G3nLaurel, G3nLayer};
1451            anyhow_like(gc.altup_num_inputs == crate::g3n::ALTUP_N).map_err(|_| {
1452                CmfError::Parse(format!(
1453                    "g3n: altup_num_inputs {} != supported {}",
1454                    gc.altup_num_inputs,
1455                    crate::g3n::ALTUP_N
1456                ))
1457            })?;
1458            let t = |name: &str| load_matrix(model, name, force_f32, ov);
1459            let f = |name: &str| load_f32(model, name, ov).map_err(err);
1460            let mut altup_proj = Vec::new();
1461            let mut altup_unembed = Vec::new();
1462            for i in 0..crate::g3n::ALTUP_N - 1 {
1463                altup_proj.push(t(&format!("model.altup_projections.{i}.weight"))?);
1464                altup_unembed.push(t(&format!("model.altup_unembed_projections.{i}.weight"))?);
1465            }
1466            let first_shared = arch.num_layers.saturating_sub(gc.num_kv_shared_layers);
1467            let sliding_of = |li: usize| {
1468                matches!(
1469                    arch.layer_types.get(li),
1470                    Some(cortiq_core::LayerType::SlidingAttention)
1471                )
1472            };
1473            let mut g3n_layers = Vec::with_capacity(arch.num_layers);
1474            for li in 0..arch.num_layers {
1475                let pfx = format!("model.layers.{li}.");
1476                let shared = li >= first_shared && first_shared > 0;
1477                let share_src = if shared {
1478                    let want = sliding_of(li);
1479                    (0..first_shared).rev().find(|&j| sliding_of(j) == want)
1480                } else {
1481                    None
1482                };
1483                g3n_layers.push(G3nLayer {
1484                    altup: G3nAltUp {
1485                        router_norm: f(&format!("{pfx}altup.router_norm.weight"))?,
1486                        modality_router: t(&format!("{pfx}altup.modality_router.weight"))?,
1487                        prediction_coefs: t(&format!("{pfx}altup.prediction_coefs.weight"))?,
1488                        correction_coefs: t(&format!("{pfx}altup.correction_coefs.weight"))?,
1489                        correct_output_scale: f(&format!("{pfx}altup.correct_output_scale"))?,
1490                    },
1491                    laurel: G3nLaurel {
1492                        left: t(&format!("{pfx}laurel.linear_left.weight"))?,
1493                        right: t(&format!("{pfx}laurel.linear_right.weight"))?,
1494                        post_norm: f(&format!("{pfx}laurel.post_laurel_norm.weight"))?,
1495                    },
1496                    input_norm: f(&format!("{pfx}input_layernorm.weight"))?,
1497                    post_attn_norm: f(&format!("{pfx}post_attention_layernorm.weight"))?,
1498                    pre_ffw_norm: f(&format!("{pfx}pre_feedforward_layernorm.weight"))?,
1499                    post_ffw_norm: f(&format!("{pfx}post_feedforward_layernorm.weight"))?,
1500                    wq: t(&format!("{pfx}self_attn.q_proj.weight"))?,
1501                    wk: if shared {
1502                        None
1503                    } else {
1504                        Some(t(&format!("{pfx}self_attn.k_proj.weight"))?)
1505                    },
1506                    wv: if shared {
1507                        None
1508                    } else {
1509                        Some(t(&format!("{pfx}self_attn.v_proj.weight"))?)
1510                    },
1511                    wo: t(&format!("{pfx}self_attn.o_proj.weight"))?,
1512                    q_norm: f(&format!("{pfx}self_attn.q_norm.weight"))?,
1513                    k_norm: if shared {
1514                        None
1515                    } else {
1516                        Some(f(&format!("{pfx}self_attn.k_norm.weight"))?)
1517                    },
1518                    kv_share_src: share_src,
1519                    sliding: sliding_of(li),
1520                    gate: t(&format!("{pfx}mlp.gate_proj.weight"))?,
1521                    up: t(&format!("{pfx}mlp.up_proj.weight"))?,
1522                    down: t(&format!("{pfx}mlp.down_proj.weight"))?,
1523                    sparsity: gc.activation_sparsity.get(li).copied().unwrap_or(0.0),
1524                    ple_gate: t(&format!("{pfx}per_layer_input_gate.weight"))?,
1525                    ple_proj: t(&format!("{pfx}per_layer_projection.weight"))?,
1526                    post_ple_norm: f(&format!("{pfx}post_per_layer_input_norm.weight"))?,
1527                });
1528            }
1529            let hd = arch.head_dim;
1530            let globals = G3nGlobals {
1531                altup_proj,
1532                altup_unembed,
1533                ple_embed: t("model.embed_tokens_per_layer.weight")?,
1534                ple_model_proj: t("model.per_layer_model_projection.weight")?,
1535                ple_norm: f("model.per_layer_projection_norm.weight")?,
1536                ple_vocab: gc.ple_vocab,
1537                ple_dim: gc.ple_dim,
1538                num_layers: arch.num_layers,
1539                hidden: arch.hidden_size,
1540                rms_eps: arch.rms_norm_eps,
1541                inv_freq_local: crate::attention::rope_inv_freq(
1542                    hd,
1543                    arch.rope_local_base_freq.unwrap_or(10_000.0) as f32,
1544                ),
1545                inv_freq_global: crate::attention::rope_inv_freq(hd, arch.rope_theta as f32),
1546                window: arch.sliding_window.unwrap_or(512),
1547            };
1548            pipeline.g3n = Some(Box::new((globals, g3n_layers)));
1549        }
1550        // DeepSeek-V4: its own stack, selected by the arch name the
1551        // converter wrote. Loading failure is fatal rather than a silent
1552        // fallback — the generic loop cannot represent this model at all,
1553        // so a fallback would decode noise.
1554        if arch.arch_name == "deepseek_v4" {
1555            let moe = arch
1556                .moe
1557                .as_ref()
1558                .ok_or_else(|| CmfError::Parse("deepseek_v4: no moe config".into()))?;
1559            let cfg = crate::dsv4::Dsv4Cfg {
1560                dim: arch.hidden_size,
1561                n_heads: arch.num_attention_heads,
1562                head_dim: arch.head_dim,
1563                // The rope tail: `partial_rotary_factor` carries it when the
1564                // conversion recorded it (rd/head_dim), which the tensors
1565                // cannot reveal. Files converted before that carry 1.0,
1566                // meaning "unset" here rather than "rotate everything" —
1567                // for those the release's 64 stands in, which is what they
1568                // were converted from.
1569                rope_head_dim: if arch.partial_rotary_factor < 1.0 {
1570                    (((arch.head_dim as f32 * arch.partial_rotary_factor) as usize) & !1)
1571                        .clamp(2, arch.head_dim)
1572                } else {
1573                    64.min(arch.head_dim)
1574                },
1575                // The LoRA ranks and the group count ARE visible in the
1576                // weights, and reading them there means a re-tuned
1577                // checkpoint loads without touching this code.
1578                q_lora_rank: 0,
1579                o_lora_rank: 0,
1580                // Derived below from wo_a's shape — the attention output is
1581                // n_heads*head_dim wide and wo_a takes one group of it per
1582                // row block, so groups = width / wo_a.cols(). A pinned 8 is
1583                // right for the release and wrong for anything else, which
1584                // is exactly what made a toy checkpoint impossible to
1585                // compare against the reference.
1586                o_groups: 8,
1587                hc_mult: 4,
1588                hc_sinkhorn_iters: 20,
1589                hc_eps: 1e-6,
1590                norm_eps: arch.rms_norm_eps as f32,
1591                n_routed_experts: moe.num_experts,
1592                top_k: moe.top_k,
1593                moe_inter: moe.moe_intermediate_size,
1594                route_scale: moe.routed_scaling_factor.unwrap_or(1.0),
1595                // config.json's `swiglu_limit`, which the header has no
1596                // field for. The release ships 10.0; a checkpoint that
1597                // retunes it would need this read from the config, so it
1598                // sits next to the other pinned constants rather than
1599                // hiding inside the expert.
1600                swiglu_limit: 10.0,
1601                window: arch.sliding_window.unwrap_or(128),
1602                index_topk: 512,
1603                vocab: arch.vocab_size,
1604            };
1605            let (g, dl) = crate::dsv4::load(model, &cfg, arch.num_layers)
1606                .map_err(|e| CmfError::Parse(format!("deepseek_v4: {e}")))?;
1607            // Read the ranks off the weights that define them: wq_a's
1608            // rows ARE q_lora_rank, and wo_b's columns are groups x
1609            // o_lora_rank. A header field could disagree with the file;
1610            // these cannot.
1611            let mut cfg = cfg;
1612            if let Some(l0) = dl.first() {
1613                cfg.q_lora_rank = l0.wq_a.rows();
1614                let attn_width = arch.num_attention_heads * arch.head_dim;
1615                if l0.wo_a.cols() > 0 && attn_width % l0.wo_a.cols() == 0 {
1616                    cfg.o_groups = (attn_width / l0.wo_a.cols()).max(1);
1617                }
1618                cfg.o_lora_rank = l0.wo_b.cols() / cfg.o_groups.max(1);
1619                cfg.hc_mult = (l0.hc_attn_fn.len() / l0.hc_attn_base.len().max(1)) / cfg.dim.max(1);
1620                if cfg.hc_mult == 0 {
1621                    cfg.hc_mult = 4;
1622                }
1623            }
1624            // RoPE rides only the last `rope_head_dim` of each head, and the
1625            // reference builds its frequencies over THAT width — not over
1626            // head_dim, which is 512 here. The generic path above sized them
1627            // by head_dim, giving 1/base^(2i/512) where 1/base^(2i/64) is
1628            // wanted: every position rotated by the wrong angle.
1629            //
1630            // YaRN is applied unconditionally by the reference (its guard is
1631            // `original_seq_len > 0`, not the sequence length), so it belongs
1632            // in these frequencies too. Older configs spell the key `type`
1633            // rather than `rope_type`; when the header carries no profile the
1634            // release's own numbers stand in, which is better than silently
1635            // decoding with unscaled frequencies.
1636            let (yf, yo, ybf, ybs) = match &arch.yarn {
1637                Some(y) => (
1638                    y.factor,
1639                    y.original_max_position_embeddings,
1640                    y.beta_fast,
1641                    y.beta_slow,
1642                ),
1643                None => {
1644                    tracing::warn!(
1645                        "deepseek_v4: the header carries no YaRN profile — \
1646                         falling back to the release's (factor 16, original \
1647                         65536, beta 32/1). Re-converting with a build that \
1648                         reads rope_scaling.type would make this exact."
1649                    );
1650                    (16.0, 65536, 32.0, 1.0)
1651                }
1652            };
1653            pipeline.inv_freq = std::sync::Arc::new(crate::attention::yarn_inv_freq(
1654                cfg.rope_head_dim,
1655                arch.rope_theta as f32,
1656                yf,
1657                yo,
1658                ybf,
1659                ybs,
1660            ));
1661            // Keep the working set resident. Everything but the routed
1662            // experts is touched by every token, and of the experts only the
1663            // ones the task actually routes to — the page cache cannot know
1664            // that and evicts by age instead.
1665            if let Ok(stats) = std::env::var("CMF_MOE_PIN") {
1666                let cover = std::env::var("CMF_MOE_PIN_COVER")
1667                    .ok()
1668                    .and_then(|v| v.parse::<f64>().ok())
1669                    .filter(|&c| c > 0.0 && c <= 1.0)
1670                    .unwrap_or(0.95);
1671                let hot = crate::pin::hot_experts(&stats, cover);
1672                let mut names: Vec<String> = Vec::new();
1673                for e in &model.tensors {
1674                    let is_expert = e.name.contains(".mlp.experts.");
1675                    if !is_expert {
1676                        names.push(e.name.clone()); // skeleton: always hot
1677                    }
1678                }
1679                let mut kept_experts = 0usize;
1680                if let Some(hot) = &hot {
1681                    for (li, experts) in hot {
1682                        for e in experts {
1683                            for w in ["gate_proj", "up_proj", "down_proj"] {
1684                                names.push(format!("model.layers.{li}.mlp.experts.{e}.{w}.weight"));
1685                            }
1686                            kept_experts += 1;
1687                        }
1688                    }
1689                }
1690                let r = crate::pin::pin_tensors(model, &names);
1691                tracing::info!(
1692                    "закреплено {:.1} ГБ ({} тензоров, горячих экспертов {kept_experts},                      покрытие {cover}); лимит {}",
1693                    r.bytes as f64 / 1e9,
1694                    r.tensors,
1695                    r.limit
1696                        .map(|l| format!("{:.1} ГБ", l as f64 / 1e9))
1697                        .unwrap_or_else(|| "неизвестен".into())
1698                );
1699                if r.skipped > 0 {
1700                    tracing::warn!("не закреплено тензоров: {}", r.skipped);
1701                }
1702            }
1703            let st = crate::dsv4::Dsv4State::new(arch.num_layers);
1704            // The speculation stack, if the file carries one. Reading it is
1705            // metadata only — the expert weights stay in the mapping until a
1706            // draft actually runs — so it costs nothing to know it is there.
1707            let depth = std::env::var("CMF_DSV4_MTP_DEPTH")
1708                .ok()
1709                .and_then(|v| v.parse::<usize>().ok())
1710                .unwrap_or(3);
1711            pipeline.dsv4_mtp = crate::dsv4::load_mtp(model, &cfg, depth);
1712            // Before any trunk pack is built: leave the draft its VRAM.
1713            crate::dsv4::dspark_reserve_note(&pipeline.dsv4_mtp, &cfg, &dl);
1714            pipeline.dsv4 = Some(Box::new((g, dl, cfg, st)));
1715        }
1716        pipeline.short_conv_cfg = short_conv_cfg;
1717        pipeline.mtp = mtp;
1718        pipeline.install_dynamic_routing(model, false);
1719        // Record the load-time overlay so a later set_active_skill(None)
1720        // correctly reverts it (the union-diff assumes dyn_active mirrors
1721        // the live overlay). Blend loads have no single index to revert.
1722        match ov {
1723            Overlay::One(sid) => {
1724                pipeline.dyn_active = model.header.skills.iter().position(|s| &s.id == sid);
1725            }
1726            Overlay::Blend(_) => pipeline.dyn_blend_loaded = true,
1727            Overlay::None => {}
1728        }
1729        // B1: apply the measured confidence-calibration temperature, if the
1730        // file carries one (softmax(logits / T) for reported Born mass).
1731        if let Some(c) = &model.header.calibration {
1732            pipeline.set_calib_temp(c.temperature);
1733        }
1734        // O(1) Nyström attention (runtime-level, no format change):
1735        // env CMF_O1 decides; unset falls through to the converter hint
1736        // in header.provenance.o1_attn (`cortiq convert --o1`), and
1737        // CMF_O1=off force-disables even the hint. CLI flags override
1738        // later via set_o1().
1739        let o1 = match crate::nystrom::o1_from_env() {
1740            crate::nystrom::O1Env::Off => None,
1741            crate::nystrom::O1Env::On(cfg) => Some(cfg),
1742            crate::nystrom::O1Env::Unset => model
1743                .header
1744                .provenance
1745                .as_ref()
1746                .and_then(|p| p.get("o1_attn"))
1747                .and_then(crate::nystrom::O1Cfg::from_json),
1748        };
1749        if o1.is_some() {
1750            if pipeline.attn_softcap > 0.0 {
1751                return Err(CmfError::Parse(
1752                    "--o1 with attention-logit soft-capping (Gemma-2) is not supported: \
1753                     the streaming operator has no capped-score form"
1754                        .into(),
1755                ));
1756            }
1757            pipeline.set_o1(o1);
1758        }
1759        Ok(pipeline)
1760    }
1761
1762    /// Record per-skill dynamic-routing metadata: which FFN layers each
1763    /// skill actually replaces (derived from the tensors present, not
1764    /// the meta `layers` field), and whether the skill is eligible for
1765    /// cheap dynamic switching (FFN-only). Called once at load.
1766    pub(crate) fn install_dynamic_routing(&mut self, model: &Arc<CmfModel>, force_f32: bool) {
1767        self.model = Some(model.clone());
1768        self.dyn_force_f32 = force_f32;
1769        let mut per_skill = Vec::with_capacity(model.header.skills.len());
1770        for sk in &model.header.skills {
1771            let mut ffn_layers = std::collections::BTreeSet::new();
1772            let mut non_ffn = false;
1773            let prefix = format!("skill.{}.", sk.id);
1774            for t in model.skill_tensors(&sk.id) {
1775                let rel = &t.name[prefix.len()..]; // e.g. model.layers.20.mlp.down_proj.weight
1776                let toks: Vec<&str> = rel.split('.').collect();
1777                if toks.len() >= 5 && toks[0] == "model" && toks[1] == "layers" && toks[3] == "mlp"
1778                {
1779                    if let Ok(li) = toks[2].parse::<usize>() {
1780                        ffn_layers.insert(li);
1781                        continue;
1782                    }
1783                }
1784                non_ffn = true; // replaces attention / embed / lm_head
1785            }
1786            if non_ffn {
1787                tracing::warn!(
1788                    "skill '{}' replaces non-FFN tensors — excluded from dynamic \
1789                     routing (static overlay still works)",
1790                    sk.id
1791                );
1792                per_skill.push(None);
1793            } else {
1794                per_skill.push(Some(ffn_layers.into_iter().collect::<Vec<_>>()));
1795            }
1796        }
1797        self.dyn_skill_layers = per_skill;
1798    }
1799
1800    /// Switch the overlaid skill for subsequent forwards (dynamic
1801    /// routing). `idx` = index into model.header.skills; None = backbone.
1802    /// Rebuilds the FFN of the union of the old and new skill's touched
1803    /// layers with the new overlay — tensor-source indirection made
1804    /// dynamic. Cheap: Mapped tensors are re-resolved mmap pointers.
1805    /// Result is bit-identical to loading the pipeline with that skill.
1806    pub fn set_active_skill(&mut self, idx: Option<usize>) -> Result<(), CmfError> {
1807        // Overlay swap changes weights → every cached K/V is stale.
1808        self.kv_cache.clear();
1809        self.kv_history.clear();
1810        if self.dyn_active == idx {
1811            return Ok(());
1812        }
1813        let model = self.model.clone().ok_or_else(|| {
1814            CmfError::Parse("dynamic routing needs a model-backed pipeline".into())
1815        })?;
1816        let mut union: std::collections::BTreeSet<usize> = std::collections::BTreeSet::new();
1817        if let Some(old) = self.dyn_active {
1818            if let Some(Some(ls)) = self.dyn_skill_layers.get(old) {
1819                union.extend(ls.iter().copied());
1820            }
1821        }
1822        let new_id: Option<String> = match idx {
1823            Some(n) => match self.dyn_skill_layers.get(n) {
1824                Some(Some(ls)) => {
1825                    union.extend(ls.iter().copied());
1826                    Some(model.header.skills[n].id.clone())
1827                }
1828                _ => {
1829                    return Err(CmfError::Parse(format!(
1830                        "skill index {n} not dynamic-eligible"
1831                    )));
1832                }
1833            },
1834            None => None,
1835        };
1836        let ov = match &new_id {
1837            Some(s) => Overlay::One(s),
1838            None => Overlay::None,
1839        };
1840        let arch = model.arch();
1841        for li in union {
1842            self.weights.layers[li].ffn =
1843                build_layer_ffn(&model, arch, li, self.dyn_force_f32, &ov)?;
1844        }
1845        self.dyn_active = idx;
1846        Ok(())
1847    }
1848}