Skip to main content

cortiq_engine/
skillbake.rs

1//! Native DTG-MA skill bake (Patent 2) — no Python, no torch.
2//!
3//! The certified recipe of `converter/make_skill_l1fcd.py`, in Rust on
4//! the `FcdModel` f32 replica:
5//!
6//! - **Phase A** — a trainable L1 mask over FFN neurons (one logit per
7//!   neuron, applied to the input of down_proj as σ(m)): pure LM loss
8//!   on the task corpus + a progressive L1 penalty. Every 30 steps the
9//!   binarized mask (σ>τ) is scored on held-out chunks; the best
10//!   checkpoint — the *denoising bottom* — is restored at the end.
11//!   Pruning noise neurons IMPROVES the model before it starts to hurt.
12//! - **Phase B** — FCD: the FFN of the last N layers trains against the
13//!   same LM loss with the hard mask active (cosine LR), held-out
14//!   gated, best checkpoint restored.
15//!
16//! Attention (softmax and GDN alike) is FROZEN and carries no gradient
17//! — exactly like the reference recipe (`torch.no_grad()` around the
18//! attention branch): the backward walks the residual stream through
19//! the FFN chain only, which is what makes a pure-Rust backward small.
20
21use crate::fcd::{FcdModel, LnFfn};
22use crate::fcd_ops as ops;
23use crate::sampler::SplitMix64;
24use cortiq_core::CmfModel;
25use std::sync::Arc;
26
27/// Hyper-parameters — defaults are the certified recipe.
28#[derive(Clone, Debug)]
29pub struct BakeHyper {
30    pub steps_a: usize,
31    pub steps_b: usize,
32    pub l1_init: f64,
33    pub l1_step: f64,
34    pub eval_every: usize,
35    pub lr_a: f64,
36    pub lr_b: f64,
37    pub tau: f32,
38    pub fcd_layers: usize,
39    pub seed: u64,
40    /// Target sparsity (0..1). When >0, the best checkpoint must have
41    /// at least this fraction of neurons pruned; if none qualifies the
42    /// highest-sparsity checkpoint is used.
43    pub target_sparsity: f64,
44    /// L1 aggression multiplier: scales both l1_init and l1_step.
45    /// >1.0 = harder pruning push, <1.0 = softer.
46    pub l1_mult: f64,
47    /// Round each layer's kept-neuron count UP to a multiple of this
48    /// (0/1 = off). 32 keeps the defragged FFN on grouped codecs
49    /// (in % 32 == 0) and SIMD kernels off their scalar tails.
50    pub align: usize,
51    /// Force one FFN width across all layers (the max aligned count) —
52    /// the whole-token GPU graphs require a uniform intermediate size.
53    pub uniform_inter: bool,
54}
55
56impl Default for BakeHyper {
57    fn default() -> Self {
58        Self {
59            steps_a: 240,
60            steps_b: 120,
61            l1_init: 0.01,
62            l1_step: 0.005,
63            eval_every: 30,
64            lr_a: 0.1,
65            lr_b: 1e-5,
66            tau: 0.5,
67            fcd_layers: 4,
68            seed: 0,
69            target_sparsity: 0.0,
70            l1_mult: 1.0,
71            align: 32,
72            uniform_inter: false,
73        }
74    }
75}
76
77/// What the bake measured and produced.
78pub struct BakeReport {
79    /// Held-out PPL of the untouched backbone.
80    pub backbone: f64,
81    /// Held-out PPL with the best hard mask (the denoising bottom).
82    pub masked: f64,
83    /// Held-out PPL after FCD (the final specialist).
84    pub overlaid: f64,
85    pub pruned_ratio: f64,
86    pub kept_per_layer: Vec<usize>,
87    pub sec: f64,
88}
89
90/// The trained artifacts: everything the defrag writer needs, f32.
91pub struct BakeArtifacts {
92    /// Per-PHYSICAL-layer live flags: the union over visits — a weight
93    /// row is removable from disk only when no visit keeps it.
94    pub keep: Vec<Vec<bool>>,
95    /// Per-VIRTUAL-layer live flags (physical × loops, pass-major): the
96    /// mask the file ships and the runtime applies per visit.
97    pub keep_visits: Vec<Vec<bool>>,
98    /// Per-layer down_proj `[hidden, inter]` with dead columns zeroed
99    /// (FCD layers: the trained weights; others: the backbone's).
100    pub down: Vec<Vec<f32>>,
101    /// Trained gate/up for the FCD layers (`None` elsewhere).
102    pub gate_up: Vec<Option<(Vec<f32>, Vec<f32>)>>,
103    /// Which layers went through Phase B.
104    pub fcd_layers: Vec<usize>,
105    /// The trained mask logits, per virtual layer — a CONTINUOUS
106    /// per-neuron importance the hard keep flags throw away. The tube
107    /// planner ranks and orders neurons by these, not by raw
108    /// activation mass.
109    pub logits: Vec<Vec<f32>>,
110}
111
112const CLIP: f64 = 1.0;
113const B1: f64 = 0.9;
114const B2: f64 = 0.999;
115const EPS: f64 = 1e-8;
116
117/// Plain Adam over a set of f32 tensors (masks are tiny, FFN mid-size).
118struct Adam {
119    m: Vec<Vec<f64>>,
120    v: Vec<Vec<f64>>,
121    t: i32,
122    lr: f64,
123}
124
125impl Adam {
126    fn new(sizes: &[usize], lr: f64) -> Self {
127        Self {
128            m: sizes.iter().map(|&n| vec![0.0; n]).collect(),
129            v: sizes.iter().map(|&n| vec![0.0; n]).collect(),
130            t: 0,
131            lr,
132        }
133    }
134
135    /// Global-norm clip + Adam step. `params[i].len() == grads[i].len()`.
136    fn step(&mut self, params: &mut [&mut [f32]], grads: &[Vec<f64>], lr_scale: f64) {
137        let gn: f64 = grads
138            .iter()
139            .flat_map(|g| g.iter().map(|x| x * x))
140            .sum::<f64>()
141            .sqrt();
142        let clip = if gn > CLIP { CLIP / gn } else { 1.0 };
143        self.t += 1;
144        let (bc1, bc2) = (1.0 - B1.powi(self.t), 1.0 - B2.powi(self.t));
145        for (pi, p) in params.iter_mut().enumerate() {
146            for j in 0..p.len() {
147                let g = grads[pi][j] * clip;
148                let m = &mut self.m[pi][j];
149                let v = &mut self.v[pi][j];
150                *m = B1 * *m + (1.0 - B1) * g;
151                *v = B2 * *v + (1.0 - B2) * g * g;
152                let upd = (*m / bc1) / ((*v / bc2).sqrt() + EPS);
153                p[j] -= (self.lr * lr_scale * upd) as f32;
154            }
155        }
156    }
157}
158
159/// Mask logit at step zero, solved for the loop depth.
160///
161/// The gate multiplies the FFN once per VISIT, so a Looped Transformer
162/// applies it `loops` times per token and the factor compounds. What
163/// must be held constant across depths is the EFFECTIVE start — the
164/// product the stack actually sees — at the value the recipe was
165/// validated with on ordinary models, σ(2.0) = 0.881:
166///
167/// ```text
168/// σ(m0)^loops = σ(2.0)   →   m0 = logit( σ(2.0)^(1/loops) )
169/// ```
170///
171/// `loops = 1` returns 2.0 exactly, so nothing regresses. Two known
172/// wrong answers this replaces: the old hardcoded 2.0, which at two
173/// visits compounds to 0.776 and took Nanbeige 4.2 from a baseline of
174/// 4.187 to 278.4 at step 30; and a start pushed to identity, which
175/// cannot learn because the update carries σ'(m) = σ(1−σ), worth 5e-4
176/// at σ = 0.9995 against 0.105 at 2.0.
177pub fn mask_init_logit(loops: usize) -> f32 {
178    let base = 1.0f32 / (1.0 + (-2.0f32).exp());
179    let per_visit = base.powf(1.0 / loops.max(1) as f32);
180    (per_visit / (1.0 - per_visit)).ln()
181}
182
183/// Learning-rate scale for the mask step, given the loop depth.
184///
185/// The backward accumulates every visit of a physical layer into the
186/// same mask gradient, so an unscaled step is `loops` times the tuned
187/// one. One step should mean one token's worth of movement at any depth.
188pub fn mask_step_scale(loops: usize) -> f64 {
189    1.0 / loops.max(1) as f64
190}
191
192fn sigmoid(x: f32) -> f32 {
193    1.0 / (1.0 + (-x).exp())
194}
195
196/// One forward + CE(+optionally backward through the FFN chain).
197/// Returns (nll_sum, tokens). `dmask`/`dffn` accumulate when given.
198struct Pass<'a> {
199    fm: &'a FcdModel,
200    tau: f32,
201    /// σ(m) per layer when soft; binarized when `hard`.
202    logits: &'a [Vec<f32>],
203    hard: bool,
204    /// Phase-B replacement FFN weights per layer (trained copies).
205    ffn: &'a [Option<(Vec<f32>, Vec<f32>, Vec<f32>)>],
206}
207
208impl Pass<'_> {
209    fn gates(&self, li: usize) -> Vec<f32> {
210        self.logits[li]
211            .iter()
212            .map(|&l| {
213                let s = sigmoid(l);
214                if self.hard {
215                    if s > self.tau { 1.0 } else { 0.0 }
216                } else {
217                    s
218                }
219            })
220            .collect()
221    }
222
223    fn wts<'b>(&'b self, li: usize, mats: &'b crate::fcd::LayerMats) -> LnFfn<'b> {
224        let l = &self.fm.layers[li];
225        match &self.ffn[li] {
226            Some((g, u, d)) => LnFfn {
227                iln: &l.iln,
228                pln: &l.pln,
229                gate: g,
230                up: u,
231                down: d,
232                // Trained copies move every Adam step — no prebuilt concat.
233                gu: None,
234            },
235            None => LnFfn {
236                iln: &l.iln,
237                pln: &l.pln,
238                gate: &[],
239                up: &[],
240                down: &mats.down,
241                gu: Some(&mats.gu),
242            },
243        }
244    }
245
246    /// Teacher-forced NLL over one chunk; when `grad` is set, backprop
247    /// through the FFN chain into the mask grads (and FFN grads for
248    /// Phase-B layers).
249    #[allow(clippy::too_many_arguments)]
250    fn chunk(
251        &self,
252        ids: &[u32],
253        grad: Option<(
254            &mut [Vec<f64>],
255            &mut [Option<(Vec<f64>, Vec<f64>, Vec<f64>)>],
256        )>,
257    ) -> (f64, usize) {
258        self.chunk_batch(ids, 1, grad)
259    }
260
261    /// `chunk` over `b` equal-length sequences flattened into `ids`.
262    ///
263    /// Everything under this level was batch-aware all along
264    /// (`layer_forward_scaled` and every attention fwd/bwd take `b`);
265    /// only this wrapper hardcoded 1. Evaluation is where it pays: the
266    /// held set is 12 chunks scored one at a time, which on a 4 B model
267    /// meant 12× the GEMM submits for the same arithmetic.
268    fn chunk_batch(
269        &self,
270        ids: &[u32],
271        b: usize,
272        grad: Option<(
273            &mut [Vec<f64>],
274            &mut [Option<(Vec<f64>, Vec<f64>, Vec<f64>)>],
275        )>,
276    ) -> (f64, usize) {
277        let fm = self.fm;
278        let hsz = fm.hidden;
279        debug_assert!(ids.len() % b.max(1) == 0, "ragged batch");
280        // Training differentiates one chunk at a time — the recipe's
281        // gradient accumulation is per-chunk by design.
282        debug_assert!(grad.is_none() || b == 1, "grads are per-chunk");
283        let t = ids.len() / b.max(1);
284        let n = b * t;
285        let nl = fm.layers.len();
286        // Embed.
287        let mut h = vec![0f32; n * hsz];
288        for (r, &id) in ids.iter().enumerate() {
289            h[r * hsz..(r + 1) * hsz]
290                .copy_from_slice(&fm.embed[id as usize * hsz..(id as usize + 1) * hsz]);
291        }
292        // Forward over VIRTUAL layers: a Looped Transformer runs the
293        // stack `fm.loops` times, with a final_norm at each loop
294        // boundary when the file says so. Everything below indexes
295        // activations by the virtual step and weights/grads by the
296        // physical layer `vl % nl` — so a physical layer visited twice
297        // accumulates both visits' gradients, which is what the loop
298        // means mathematically.
299        let loops = fm.loops.max(1);
300        let vn = nl * loops;
301        let mut h_ins = Vec::with_capacity(vn);
302        let mut acts = Vec::with_capacity(vn);
303        let mut masks = Vec::with_capacity(vn);
304        // Loop-boundary norms, saved for the backward: (input, inv).
305        let mut lnorms: Vec<Option<(Vec<f32>, Vec<f32>)>> = vec![None; vn];
306        for vl in 0..vn {
307            let li = vl % nl;
308            // The gate is PER VISIT: the two passes of a loop are
309            // different computations sharing one set of weights, so the
310            // mask must be allowed to differ between them. Weights stay
311            // indexed by the physical layer.
312            let g = self.gates(vl);
313            let mats_hold = fm.mats(li).expect("layer mats");
314            let wts = self.wts(li, &mats_hold);
315            let want = grad.is_some();
316            let (h2, a) = fm.layer_forward_scaled(li, &h, b, t, &wts, false, want, Some(&g));
317            h_ins.push(if want { h } else { Vec::new() });
318            acts.push(a);
319            masks.push(g);
320            h = h2;
321            // Mid-stack norm at every loop boundary except the last —
322            // the final one folds into the head below.
323            if fm.loop_norm && li + 1 == nl && vl + 1 < vn {
324                let mut hn = vec![0f32; n * hsz];
325                let mut inv = vec![0f32; n];
326                ops::rmsnorm_fwd(&h, &fm.final_norm, fm.eps, fm.gemma, &mut hn, &mut inv);
327                if want {
328                    lnorms[vl] = Some((h, inv));
329                }
330                h = hn;
331            }
332        }
333        // Final norm + tied LM head, CE summed over positions 1..t.
334        let mut hn = vec![0f32; n * hsz];
335        let mut inv = vec![0f32; n];
336        ops::rmsnorm_fwd(&h, &fm.final_norm, fm.eps, fm.gemma, &mut hn, &mut inv);
337        let lm: &[f32] = fm.lm_head.as_deref().unwrap_or(&fm.embed);
338        let vocab = lm.len() / hsz;
339        let pool = fm.pool.as_deref();
340        let mut nll = 0f64;
341        let mut dh_n = vec![0f32; n * hsz]; // dL/d hn
342        // Chunk the vocab matmul over positions to bound the logits buf.
343        // Positions are walked PER SEQUENCE: the last position of chunk
344        // i must not be scored against the first token of chunk i+1.
345        const POS_CHUNK: usize = 64;
346        let scored = b * (t - 1);
347        for bi in 0..b {
348            let base = bi * t;
349            let mut p0 = 0usize;
350            while p0 < t - 1 {
351                let pc = POS_CHUNK.min(t - 1 - p0);
352                let mut logits = vec![0f32; pc * vocab];
353                ops::gemm_nt(
354                    &hn[(base + p0) * hsz..(base + p0 + pc) * hsz],
355                    lm,
356                    &mut logits,
357                    pc,
358                    hsz,
359                    vocab,
360                    pool,
361                );
362                for r in 0..pc {
363                    let target = ids[base + p0 + r + 1] as usize;
364                    let row = &mut logits[r * vocab..(r + 1) * vocab];
365                    let mx = row.iter().cloned().fold(f32::NEG_INFINITY, f32::max) as f64;
366                    let mut sum = 0f64;
367                    for v in row.iter() {
368                        sum += ((*v as f64) - mx).exp();
369                    }
370                    nll += mx + sum.ln() - row[target] as f64;
371                    if grad.is_some() {
372                        // dCE/dlogit = softmax − onehot, scaled by 1/scored.
373                        let inv_n = 1.0 / scored as f64;
374                        for v in row.iter_mut() {
375                            *v = ((((*v as f64) - mx).exp() / sum) * inv_n) as f32;
376                        }
377                        row[target] -= inv_n as f32;
378                    }
379                }
380                if grad.is_some() {
381                    ops::gemm_dx(
382                        &logits,
383                        lm,
384                        &mut dh_n[(base + p0) * hsz..(base + p0 + pc) * hsz],
385                        pc,
386                        hsz,
387                        vocab,
388                        pool,
389                    );
390                }
391                p0 += pc;
392            }
393        }
394        let Some((dmask, dffn)) = grad else {
395            return (nll, scored);
396        };
397        // Backward: final norm, then the FFN chain layer by layer.
398        let t_bwd = std::time::Instant::now();
399        let mut dh = vec![0f32; n * hsz];
400        ops::rmsnorm_bwd(&h, &fm.final_norm, &inv, &dh_n, fm.gemma, &mut dh, None);
401        for vl in (0..vn).rev() {
402            let li = vl % nl;
403            // Undo the loop-boundary norm this step fed into.
404            if let Some((hb, inv)) = lnorms[vl].as_ref() {
405                let mut dprev = vec![0f32; n * hsz];
406                ops::rmsnorm_bwd(hb, &fm.final_norm, inv, &dh, fm.gemma, &mut dprev, None);
407                dh = dprev;
408            }
409            let a = acts[vl].as_ref().expect("acts saved in grad mode");
410            let g = &masks[vl];
411            let inter = fm.layers[li].inter;
412            let mats_hold = fm.mats(li).expect("layer mats");
413            let wts = self.wts(li, &mats_hold);
414            // h2 = h1 + act2 @ downᵀ  →  dact2 = dh @ down.
415            let mut dact2 = vec![0f32; t * inter];
416            ops::gemm_dx(&dh, wts.down, &mut dact2, t, inter, hsz, fm.pool.as_deref());
417            if let Some((_, _, dd)) = dffn[li].as_mut() {
418                // dW_down += dhᵀ · act2 (act2 = act·g).
419                let mut act2 = a.act.clone();
420                for r in 0..t {
421                    for (x, &gv) in act2[r * inter..(r + 1) * inter].iter_mut().zip(g) {
422                        *x *= gv;
423                    }
424                }
425                let mut dw = vec![0f32; hsz * inter];
426                ops::gemm_dw(&dh, &act2, &mut dw, t, inter, hsz, fm.pool.as_deref());
427                for (o, &x) in dd.iter_mut().zip(&dw) {
428                    *o += x as f64;
429                }
430            }
431            // Mask grad: dm = Σ_t dact2·act · σ'(m)  (soft; STE-equal).
432            // Indexed by the VIRTUAL layer: each visit's mask row gets
433            // exactly its own visit's gradient, no cross-visit sum.
434            {
435                let dm = &mut dmask[vl];
436                for r in 0..t {
437                    let da = &dact2[r * inter..(r + 1) * inter];
438                    let aa = &a.act[r * inter..(r + 1) * inter];
439                    for j in 0..inter {
440                        dm[j] += da[j] as f64 * aa[j] as f64;
441                    }
442                }
443                // σ'(m) folded in once per chunk (constant per neuron).
444                for (j, d) in dm.iter_mut().enumerate() {
445                    let _ = j;
446                    let _ = d;
447                }
448            }
449            // dact = dact2 · g;  silu·mul backward.
450            let mut dg_pre = vec![0f32; t * inter];
451            let mut du_pre = vec![0f32; t * inter];
452            for r in 0..t {
453                for j in 0..inter {
454                    let i = r * inter + j;
455                    let da = dact2[i] * g[j];
456                    let sg = ops::silu(a.gpre[i]);
457                    dg_pre[i] = da * a.upre[i] * ops::silu_bwd(a.gpre[i]);
458                    du_pre[i] = da * sg;
459                }
460            }
461            // dn2 = dg_pre @ gate + du_pre @ up — one fused submit when
462            // the frozen concat exists; the trained-copy path keeps two.
463            let mut dn2 = vec![0f32; t * hsz];
464            if let Some(gu) = wts.gu {
465                let mut dgu = vec![0f32; t * 2 * inter];
466                for r in 0..t {
467                    let row = &mut dgu[r * 2 * inter..(r + 1) * 2 * inter];
468                    row[..inter].copy_from_slice(&dg_pre[r * inter..(r + 1) * inter]);
469                    row[inter..].copy_from_slice(&du_pre[r * inter..(r + 1) * inter]);
470                }
471                ops::gemm_dx(&dgu, gu, &mut dn2, t, hsz, 2 * inter, fm.pool.as_deref());
472            } else {
473                ops::gemm_dx(
474                    &dg_pre,
475                    wts.gate,
476                    &mut dn2,
477                    t,
478                    hsz,
479                    inter,
480                    fm.pool.as_deref(),
481                );
482                let mut dn2b = vec![0f32; t * hsz];
483                ops::gemm_dx(
484                    &du_pre,
485                    wts.up,
486                    &mut dn2b,
487                    t,
488                    hsz,
489                    inter,
490                    fm.pool.as_deref(),
491                );
492                for (x, &y) in dn2.iter_mut().zip(&dn2b) {
493                    *x += y;
494                }
495            }
496            if let Some((dgw, duw, _)) = dffn[li].as_mut() {
497                let mut dw = vec![0f32; inter * hsz];
498                ops::gemm_dw(&dg_pre, &a.n2, &mut dw, t, hsz, inter, fm.pool.as_deref());
499                for (o, &x) in dgw.iter_mut().zip(&dw) {
500                    *o += x as f64;
501                }
502                dw.fill(0.0);
503                ops::gemm_dw(&du_pre, &a.n2, &mut dw, t, hsz, inter, fm.pool.as_deref());
504                for (o, &x) in duw.iter_mut().zip(&dw) {
505                    *o += x as f64;
506                }
507            }
508            // Post-norm backward into h1; the attention branch carries
509            // no gradient (frozen), so dh1 flows straight to dh_in.
510            let mut dh1 = dh.clone(); // residual h2 = h1 + ffn
511            ops::rmsnorm_bwd(&a.h1, wts.pln, &a.inv2, &dn2, fm.gemma, &mut dh1, None);
512            dh = dh1;
513            let _ = &h_ins[vl];
514        }
515        crate::fcd::prof::add(&crate::fcd::prof::BWD, t_bwd);
516        (nll, scored)
517    }
518}
519
520/// Held-out PPL with the hard mask (and Phase-B weights when present).
521fn held_ppl(pass: &Pass, held: &[Vec<u32>]) -> f64 {
522    // One batched pass over the whole held set: same arithmetic, one
523    // GEMM per weight instead of one per chunk. On the 4 B looped model
524    // the sequential version spent 12× the submits for identical math.
525    if held.is_empty() {
526        return f64::NAN;
527    }
528    let t = held[0].len();
529    if held.iter().all(|c| c.len() == t) {
530        let flat: Vec<u32> = held.iter().flatten().copied().collect();
531        let (l, k) = pass.chunk_batch(&flat, held.len(), None);
532        return (l / k.max(1) as f64).exp();
533    }
534    let mut nll = 0f64;
535    let mut n = 0usize;
536    for c in held {
537        let (l, k) = pass.chunk(c, None);
538        nll += l;
539        n += k;
540    }
541    (nll / n.max(1) as f64).exp()
542}
543
544/// Score a WRITTEN specialist through the replica's own math (f32
545/// dequant of whatever the file carries) with the file's binary mask
546/// held hard — the decomposition probe that tells "the requant at write
547/// cost the quality" from "the runtime applies the mask differently".
548/// Returns (bare, masked) held-PPL over the chunks.
549pub fn replica_score_file_mask(
550    model: &Arc<CmfModel>,
551    chunks: &[Vec<u32>],
552) -> Result<(f64, f64), String> {
553    let o1_off = crate::nystrom::O1Cfg {
554        layers: crate::nystrom::O1Layers::List(Vec::new()),
555        m: 4,
556        w: 8,
557        sink: 1,
558        rect: crate::nystrom::O1_DEFAULT_RECT,
559    };
560    let fm = FcdModel::from_cmf(model, &o1_off, false)?;
561    let nl = fm.layers.len();
562    let loops = fm.loops.max(1);
563    let vn = nl * loops;
564    let inter = fm.layers[0].inter;
565    let ffn: Vec<Option<(Vec<f32>, Vec<f32>, Vec<f32>)>> = vec![None; nl];
566    // Binary mask → logits at ±50: σ crosses any τ exactly as the bit says.
567    let task = &model.masks.default_task;
568    let mask = model
569        .masks
570        .masks
571        .iter()
572        .find(|m| &m.name == task)
573        .or_else(|| model.masks.masks.first());
574    let open: Vec<Vec<f32>> = vec![vec![50.0; inter]; vn];
575    let masked_logits: Vec<Vec<f32>> = match mask {
576        Some(m) => (0..vn)
577            .map(|vl| {
578                let row = m.ffn_masks.get(vl).map(|v| v.as_slice()).unwrap_or(&[]);
579                (0..inter)
580                    .map(|j| {
581                        if (row.get(j >> 3).copied().unwrap_or(0) >> (j & 7)) & 1 != 0 {
582                            50.0
583                        } else {
584                            -50.0
585                        }
586                    })
587                    .collect()
588            })
589            .collect(),
590        None => open.clone(),
591    };
592    let score = |logits: &[Vec<f32>]| -> f64 {
593        let pass = Pass {
594            fm: &fm,
595            tau: 0.5,
596            logits,
597            hard: true,
598            ffn: &ffn,
599        };
600        held_ppl(&pass, chunks)
601    };
602    Ok((score(&open), score(&masked_logits)))
603}
604
605/// The whole recipe. `log` receives progress lines.
606pub fn skill_bake(
607    model: &Arc<CmfModel>,
608    chunks: &[Vec<u32>],
609    held_n: usize,
610    hy: &BakeHyper,
611    mut log: impl FnMut(&str),
612) -> Result<(BakeReport, BakeArtifacts), String> {
613    let t0 = std::time::Instant::now();
614    let o1_off = crate::nystrom::O1Cfg {
615        layers: crate::nystrom::O1Layers::List(Vec::new()),
616        m: 4,
617        w: 8,
618        sink: 1,
619        rect: crate::nystrom::O1_DEFAULT_RECT,
620    };
621    let fm = FcdModel::from_cmf(model, &o1_off, false)?;
622    let nl = fm.layers.len();
623    let inter = fm.layers.iter().map(|l| l.inter).max().unwrap_or(0);
624    if fm.layers.iter().any(|l| l.inter != inter) {
625        return Err("skill bake: non-uniform FFN widths".into());
626    }
627    let held: Vec<Vec<u32>> = chunks[..held_n.min(chunks.len())].to_vec();
628    let calib: Vec<Vec<u32>> = chunks[held_n.min(chunks.len())..].to_vec();
629    if calib.len() < 12 {
630        return Err(format!(
631            "skill bake: corpus too small ({} calib chunks)",
632            calib.len()
633        ));
634    }
635    let fcd: Vec<usize> = (nl.saturating_sub(hy.fcd_layers)..nl).collect();
636    let _rng = SplitMix64::new(hy.seed);
637
638    // Trainables. The gate starts as close to OPEN as the arithmetic
639    // allows, because step zero must be the backbone and nothing else.
640    //
641    // It used to start at 2.0, and σ(2.0) = 0.881 — every FFN neuron
642    // scaled to seven eighths before a single gradient. On an ordinary
643    // stack that costs a few percent of perplexity and hides. On a
644    // LOOPED Transformer it does not: Nanbeige 4.2 runs its 22 layers
645    // twice, so each physical FFN is visited twice and the factor
646    // compounds to 0.881² = 0.776 per layer over 44 visits. Measured:
647    // baseline 4.187 → 278.4 at step 30 with 0% pruned. Nothing had been
648    // pruned; the mask had simply turned the model down.
649    //
650    // So solve for the init instead of hardcoding it — but solve for the
651    // right target. Pushing σ(m0)^loops to 0.999 starts at the backbone
652    // and cannot move: the gradient carries σ'(m) = σ(1−σ), which at
653    // σ = 0.9995 is 5e-4 against 0.105 at the old 2.0, and BOTH the data
654    // term and the L1 term are scaled by it (see the update below). That
655    // was tried: 60 steps, 0% pruned, hard-PPL equal to the baseline to
656    // three digits. Identity that cannot learn is not an improvement.
657    //
658    // The quantity to preserve is the EFFECTIVE start — what the stack
659    // actually multiplies by, once per visit compounded over the loop —
660    // at the value the recipe was validated with on ordinary models:
661    // σ(m0)^loops = σ(2.0) = 0.881. One loop reproduces the old constant
662    // exactly, so nothing regresses; two loops open the per-visit gate to
663    // 0.9385 so the compounded factor is again 0.881, with σ' = 0.058
664    // rather than 0.0005.
665    let loops = fm.loops.max(1);
666    let m0 = mask_init_logit(loops);
667    // One mask row per VIRTUAL layer: nl × loops. Unlooped: vn == nl.
668    let vn = nl * loops;
669    let mut logits: Vec<Vec<f32>> = vec![vec![m0; inter]; vn];
670    let mut ffn: Vec<Option<(Vec<f32>, Vec<f32>, Vec<f32>)>> = vec![None; nl];
671
672    // Baseline (no mask): even σ(m0) is not exactly 1, so measure with
673    // gates forced open via hard mask over +∞… simplest: logits +50.
674    let open: Vec<Vec<f32>> = vec![vec![50.0; inter]; vn];
675    let base_pass = Pass {
676        fm: &fm,
677        tau: hy.tau,
678        logits: &open,
679        hard: true,
680        ffn: &ffn,
681    };
682    let backbone = held_ppl(&base_pass, &held);
683    log(&format!("baseline (full): {backbone:.3}"));
684
685    // ── Phase A: mask training ──
686    let mut adam_a = Adam::new(&vec![inter; vn], hy.lr_a);
687    let mut l1 = hy.l1_init * hy.l1_mult;
688    let l1_step_eff = hy.l1_step * hy.l1_mult;
689    // best = (ppl, logits_snapshot, sparsity)
690    let mut best: (f64, Option<Vec<Vec<f32>>>, f64) = (f64::MAX, None, 0.0);
691    // Track the highest-sparsity checkpoint as fallback.
692    let mut max_sp: (f64, Option<Vec<Vec<f32>>>, f64) = (f64::MAX, None, 0.0);
693    let mut prev_alive: Option<Vec<Vec<bool>>> = None;
694    // In-process phase timers: this loop was estimated three different
695    // ways and every estimate came out under a fifth of the measured
696    // step time. Measure, then optimize the top line, not the guess.
697    let mut acc_chunk = 0f64;
698    let mut acc_adam = 0f64;
699    // Phase A trains in strict f32: the mask SELECTS neurons by its
700    // gradient, and f16 operand rounding on that signal — fine for every
701    // forward and eval in this file — compounds over ~90 steps into
702    // closing the wrong neurons (measured: hard-PPL 5.207 vs 4.293 at the
703    // same 2.56% sparsity; the f32 run retraces the reference trajectory
704    // to the third decimal). Evals inside the loop lift the restriction —
705    // their tensor-core numbers match f32 at print precision.
706    crate::gpu::bake_precision_strict(true);
707    for step in 0..hy.steps_a {
708        let t_step = std::time::Instant::now();
709        let chunk = &calib[step % calib.len()];
710        let mut dmask: Vec<Vec<f64>> = vec![vec![0.0; inter]; vn];
711        let mut dffn: Vec<Option<(Vec<f64>, Vec<f64>, Vec<f64>)>> = vec![None; nl];
712        let pass = Pass {
713            fm: &fm,
714            tau: hy.tau,
715            logits: &logits,
716            hard: false,
717            ffn: &ffn,
718        };
719        let _ = pass.chunk(chunk, Some((&mut dmask, &mut dffn)));
720        // Fold σ'(m) into the mask grads + add the L1 term.
721        let l1_per = l1 / (inter as f64 * nl as f64);
722        for li in 0..vn {
723            for j in 0..inter {
724                let s = sigmoid(logits[li][j]) as f64;
725                dmask[li][j] = dmask[li][j] * s * (1.0 - s) + l1_per * s * (1.0 - s);
726            }
727        }
728        // One gradient per VISIT, one step per token. A Looped
729        // Transformer visits each physical layer `loops` times and the
730        // backward accumulates every visit into the same mask, so an
731        // unnormalised step is `loops` times the one the recipe was
732        // tuned with — the mask overshoots, neurons cross tau within the
733        // first evaluation window, and each of them is then missing from
734        // both passes. Dividing by the visit count makes a step mean the
735        // same thing at any loop depth.
736        // Per-visit rows: each mask logit receives exactly one visit's
737        // gradient, so the step needs no visit normalisation here — that
738        // scale now belongs to Phase B alone, where the FFN weights ARE
739        // shared across visits.
740        let t_chunk = t_step.elapsed().as_secs_f64();
741        let mut params: Vec<&mut [f32]> = logits.iter_mut().map(|v| v.as_mut_slice()).collect();
742        adam_a.step(&mut params, &dmask, 1.0);
743        acc_chunk += t_chunk;
744        acc_adam += t_step.elapsed().as_secs_f64() - t_chunk;
745        if (step + 1) % hy.eval_every == 0 {
746            l1 += l1_step_eff;
747            let pass = Pass {
748                fm: &fm,
749                tau: hy.tau,
750                logits: &logits,
751                hard: true,
752                ffn: &ffn,
753            };
754            crate::gpu::bake_precision_strict(false);
755            let hp = held_ppl(&pass, &held);
756            crate::gpu::bake_precision_strict(true);
757            // Name the neurons that crossed τ since the last eval. At
758            // 0.01% pruned = ~24 neurons for a 135 held-PPL, WHICH 24 is
759            // the whole diagnosis: it decides between "this model has no
760            // noise neurons" and "a shared mask cannot spare a neuron
761            // that only one visit of the loop needs".
762            let cur: Vec<Vec<bool>> = logits
763                .iter()
764                .map(|l| l.iter().map(|&x| sigmoid(x) > hy.tau).collect())
765                .collect();
766            if let Some(prev) = &prev_alive {
767                let died: Vec<String> = cur
768                    .iter()
769                    .zip(prev)
770                    .enumerate()
771                    .flat_map(|(li, (c, p))| {
772                        c.iter()
773                            .zip(p.iter())
774                            .enumerate()
775                            .filter(|&(_, (&cj, &pj))| pj && !cj)
776                            .map(move |(j, _)| format!("L{li}:{j}"))
777                    })
778                    .collect();
779                if !died.is_empty() {
780                    log(&format!(
781                        "    closed since last eval: {}: {}{}",
782                        died.len(),
783                        died.iter().take(32).cloned().collect::<Vec<_>>().join(" "),
784                        if died.len() > 32 { " …" } else { "" }
785                    ));
786                }
787            }
788            let alive: usize = cur.iter().map(|l| l.iter().filter(|&&b| b).count()).sum();
789            prev_alive = Some(cur);
790            let sp = 1.0 - alive as f64 / (vn * inter) as f64;
791            // Track highest-sparsity checkpoint.
792            if sp > max_sp.2 {
793                max_sp = (hp, Some(logits.clone()), sp);
794            }
795            // Best checkpoint selection: respect target_sparsity.
796            if hy.target_sparsity > 0.0 {
797                if sp >= hy.target_sparsity && hp < best.0 {
798                    best = (hp, Some(logits.clone()), sp);
799                }
800            } else if hp < best.0 {
801                best = (hp, Some(logits.clone()), sp);
802            }
803            log(&format!(
804                "  [A] step {}: L1={l1:.3} pruned={:.2}% hard-PPL={hp:.3} (bottom {}@{:.2}%) [fwd+bwd {:.1}s, adam {:.2}s per step]",
805                step + 1,
806                sp * 100.0,
807                if best.0 == f64::MAX {
808                    "—".to_string()
809                } else {
810                    format!("{:.3}", best.0)
811                },
812                best.2 * 100.0,
813                acc_chunk / (step + 1) as f64,
814                acc_adam / (step + 1) as f64
815            ));
816        }
817    }
818    // If target_sparsity was set but no checkpoint qualified, fall back
819    // to the highest-sparsity checkpoint.
820    // Phase A is over — phase B and every eval after run on the fast arms.
821    crate::gpu::bake_precision_strict(false);
822    if hy.target_sparsity > 0.0 && best.1.is_none() {
823        log(&format!(
824            "[A] target sparsity {:.0}% not reached; using max-sparsity checkpoint ({:.0}%)",
825            hy.target_sparsity * 100.0,
826            max_sp.2 * 100.0
827        ));
828        best = max_sp;
829    }
830    // Phase totals, printed unconditionally — a 5-step measurement run
831    // must report even though no eval fired.
832    {
833        use crate::fcd::prof;
834        let (a, f, bw, g, gc) = (
835            prof::take(&prof::ATTN_FWD),
836            prof::take(&prof::FFN_FWD),
837            prof::take(&prof::BWD),
838            prof::take(&prof::GEMM),
839            prof::GEMM_CALLS.swap(0, std::sync::atomic::Ordering::Relaxed),
840        );
841        log(&format!(
842            "[prof] phase A over {} step(s): attn-fwd {a:.1}s | ffn-fwd {f:.1}s | bwd {bw:.1}s |              gemm total {g:.1}s in {gc} calls ({:.1} ms/call)",
843            hy.steps_a,
844            if gc > 0 { g * 1000.0 / gc as f64 } else { 0.0 }
845        ));
846        log(&format!("[prof] gemm shapes:\n{}", prof::shape_report(6)));
847    }
848    if let Some(b) = best.1.take() {
849        logits = b;
850    }
851    let pass = Pass {
852        fm: &fm,
853        tau: hy.tau,
854        logits: &logits,
855        hard: true,
856        ffn: &ffn,
857    };
858    let masked = held_ppl(&pass, &held);
859    log(&format!(
860        "[A] {:.0}s: masked-PPL {masked:.3}",
861        t0.elapsed().as_secs_f64()
862    ));
863
864    // ── Phase B: FCD of the last N layers' FFN (hard mask active) ──
865    for &li in &fcd {
866        let p = format!("model.layers.{li}.");
867        ffn[li] = Some((
868            crate::fcd::deq_pub(&fm.src, &format!("{p}mlp.gate_proj.weight"))
869                .map_err(|e| format!("phase-B gate: {e}"))?,
870            crate::fcd::deq_pub(&fm.src, &format!("{p}mlp.up_proj.weight"))
871                .map_err(|e| format!("phase-B up: {e}"))?,
872            crate::fcd::deq_pub(&fm.src, &format!("{p}mlp.down_proj.weight"))
873                .map_err(|e| format!("phase-B down: {e}"))?,
874        ));
875    }
876    let sizes: Vec<usize> = fcd
877        .iter()
878        .flat_map(|&li| {
879            let (g, u, d) = ffn[li].as_ref().expect("phase-B masters");
880            [g.len(), u.len(), d.len()]
881        })
882        .collect();
883    let mut adam_b = Adam::new(&sizes, hy.lr_b);
884    let mut best_b: (f64, Option<Vec<Option<(Vec<f32>, Vec<f32>, Vec<f32>)>>>) = (masked, None);
885    for step in 0..hy.steps_b {
886        let chunk = &calib[step % calib.len()];
887        let mut dmask: Vec<Vec<f64>> = vec![vec![0.0; inter]; vn];
888        let mut dffn: Vec<Option<(Vec<f64>, Vec<f64>, Vec<f64>)>> = (0..nl)
889            .map(|li| {
890                ffn[li]
891                    .as_ref()
892                    .map(|(g, u, d)| (vec![0.0; g.len()], vec![0.0; u.len()], vec![0.0; d.len()]))
893            })
894            .collect();
895        let pass = Pass {
896            fm: &fm,
897            tau: hy.tau,
898            logits: &logits,
899            hard: true,
900            ffn: &ffn,
901        };
902        let _ = pass.chunk(chunk, Some((&mut dmask, &mut dffn)));
903        // Cosine LR.
904        let lr_scale = 0.5 * (1.0 + (std::f64::consts::PI * step as f64 / hy.steps_b as f64).cos());
905        let first_fcd = fcd[0];
906        let mut params: Vec<&mut [f32]> = Vec::new();
907        let mut grads: Vec<Vec<f64>> = Vec::new();
908        for (off, slot) in ffn[first_fcd..].iter_mut().enumerate() {
909            let li = first_fcd + off;
910            let Some((g, u, d)) = slot.as_mut() else {
911                continue;
912            };
913            let (dg, du, dd) = dffn[li].take().unwrap();
914            params.push(g.as_mut_slice());
915            grads.push(dg);
916            params.push(u.as_mut_slice());
917            grads.push(du);
918            params.push(d.as_mut_slice());
919            grads.push(dd);
920        }
921        // Same visit normalisation as Phase A: dffn accumulates every
922        // visit of a physical layer, and an FFN update perturbs BOTH
923        // passes of the loop, so per-step damage is `loops` times what
924        // lr_b was tuned for on ordinary stacks.
925        adam_b.step(&mut params, &grads, lr_scale * mask_step_scale(loops));
926        if (step + 1) % hy.eval_every == 0 {
927            let pass = Pass {
928                fm: &fm,
929                tau: hy.tau,
930                logits: &logits,
931                hard: true,
932                ffn: &ffn,
933            };
934            let cur = held_ppl(&pass, &held);
935            if cur < best_b.0 {
936                best_b = (cur, Some(ffn.clone()));
937            }
938            log(&format!(
939                "  [B] step {}: held-PPL {cur:.3} (best {:.3})",
940                step + 1,
941                best_b.0
942            ));
943        }
944    }
945    if let Some(b) = best_b.1.take() {
946        ffn = b;
947    }
948    let overlaid = best_b.0;
949
950    // ── Export artifacts ──
951    // Per-visit keep flags are the mask that ships; the PHYSICAL keep is
952    // their union, because a weight row can only be removed from disk if
953    // no visit needs it.
954    let keep_visits = keep_masks(&logits, hy.tau, hy.align, hy.uniform_inter);
955    let keep: Vec<Vec<bool>> = (0..nl)
956        .map(|li| {
957            (0..inter)
958                .map(|j| (0..loops).any(|v| keep_visits[v * nl + li][j]))
959                .collect()
960        })
961        .collect();
962    if hy.align > 1 || hy.uniform_inter {
963        let raw: usize = logits
964            .iter()
965            .map(|l| l.iter().filter(|&&x| sigmoid(x) > hy.tau).count())
966            .sum();
967        // Compare like with like: raw σ-counts are over the VIRTUAL
968        // rows, so the padded count must be too — the union rows are
969        // fewer and the subtraction would underflow.
970        let padded: usize = keep_visits
971            .iter()
972            .map(|a| a.iter().filter(|&&x| x).count())
973            .sum::<usize>()
974            .saturating_sub(raw);
975        log(&format!(
976            "align: +{padded} neurons resurrected (align {}, uniform {})",
977            hy.align, hy.uniform_inter
978        ));
979    }
980    let mut down_out = Vec::with_capacity(nl);
981    let mut gate_up = Vec::with_capacity(nl);
982    let mut kept_per_layer = Vec::with_capacity(nl);
983    for li in 0..nl {
984        let alive = &keep[li];
985        kept_per_layer.push(alive.iter().filter(|&&a| a).count());
986        let mut down = match &ffn[li] {
987            Some((_, _, d)) => d.clone(),
988            None => fm.mats(li).expect("layer mats").down.clone(),
989        };
990        let hsz = fm.hidden;
991        for r in 0..hsz {
992            for (c, &a) in alive.iter().enumerate() {
993                if !a {
994                    down[r * inter + c] = 0.0;
995                }
996            }
997        }
998        gate_up.push(ffn[li].as_ref().map(|(g, u, _)| (g.clone(), u.clone())));
999        down_out.push(down);
1000    }
1001    let total: usize = keep_visits
1002        .iter()
1003        .map(|a| a.iter().filter(|&&x| x).count())
1004        .sum();
1005    let report = BakeReport {
1006        backbone,
1007        masked,
1008        overlaid,
1009        pruned_ratio: 1.0 - total as f64 / (vn * inter) as f64,
1010        kept_per_layer,
1011        sec: t0.elapsed().as_secs_f64(),
1012    };
1013    let arts = BakeArtifacts {
1014        keep,
1015        keep_visits,
1016        down: down_out,
1017        gate_up,
1018        fcd_layers: fcd,
1019        logits: logits.clone(),
1020    };
1021    Ok((report, arts))
1022}
1023
1024/// Hard-threshold keep masks from the trained logits, then resurrect
1025/// the highest-logit pruned neurons until each layer's kept count is a
1026/// multiple of `align` (rounding UP — the resurrected neurons are the
1027/// ones the mask ranked closest to the threshold, so this only moves
1028/// toward the full backbone). `uniform` additionally raises every layer
1029/// to the max layer's aligned count. A layer with 0 live neurons gets
1030/// `align.max(1)` — the defrag writer rejects empty layers.
1031fn keep_masks(logits: &[Vec<f32>], tau: f32, align: usize, uniform: bool) -> Vec<Vec<bool>> {
1032    let inter = logits[0].len();
1033    let round = |n: usize| -> usize {
1034        let n = n.max(1);
1035        if align <= 1 {
1036            n.min(inter)
1037        } else {
1038            (n.div_ceil(align) * align).min(inter)
1039        }
1040    };
1041    let mut want: Vec<usize> = logits
1042        .iter()
1043        .map(|l| round(l.iter().filter(|&&x| sigmoid(x) > tau).count()))
1044        .collect();
1045    if uniform {
1046        let k = want.iter().copied().max().unwrap_or(inter);
1047        want = vec![k; logits.len()];
1048    }
1049    logits
1050        .iter()
1051        .zip(&want)
1052        .map(|(l, &k)| {
1053            let mut idx: Vec<usize> = (0..inter).collect();
1054            idx.sort_unstable_by(|&a, &b| l[b].total_cmp(&l[a]));
1055            let mut alive = vec![false; inter];
1056            for &i in idx.iter().take(k) {
1057                alive[i] = true;
1058            }
1059            alive
1060        })
1061        .collect()
1062}
1063
1064#[cfg(test)]
1065mod tests {
1066    use super::*;
1067
1068    fn kept(masks: &[Vec<bool>]) -> Vec<usize> {
1069        masks
1070            .iter()
1071            .map(|m| m.iter().filter(|&&a| a).count())
1072            .collect()
1073    }
1074
1075    /// align=32 rounds each layer UP by resurrecting the largest
1076    /// pruned logits; the originally-alive set stays alive.
1077    #[test]
1078    fn keep_masks_aligns_up_and_preserves_alive() {
1079        let inter = 96;
1080        // Layer 0: 40 alive (logits > 0 → σ > 0.5), the rest ramp
1081        // below threshold so resurrection order is deterministic.
1082        let l0: Vec<f32> = (0..inter)
1083            .map(|i| if i < 40 { 1.0 } else { -1.0 - i as f32 * 0.01 })
1084            .collect();
1085        // Layer 1: 64 alive — already aligned, must stay exactly 64.
1086        let l1: Vec<f32> = (0..inter)
1087            .map(|i| if i < 64 { 2.0 } else { -3.0 })
1088            .collect();
1089        let masks = keep_masks(&[l0.clone(), l1], 0.5, 32, false);
1090        assert_eq!(kept(&masks), vec![64, 64]);
1091        // The 40 originally-alive stay; resurrected are the top pruned
1092        // logits (indices 40..64 — the least-negative of the ramp).
1093        for i in 0..64 {
1094            assert!(masks[0][i], "neuron {i} should be kept");
1095        }
1096        for i in 64..inter {
1097            assert!(!masks[0][i], "neuron {i} should stay pruned");
1098        }
1099    }
1100
1101    /// uniform=true raises every layer to the max aligned count.
1102    #[test]
1103    fn keep_masks_uniform_takes_max() {
1104        let inter = 96;
1105        let l0: Vec<f32> = (0..inter)
1106            .map(|i| if i < 10 { 1.0 } else { -2.0 })
1107            .collect();
1108        let l1: Vec<f32> = (0..inter)
1109            .map(|i| if i < 70 { 1.0 } else { -2.0 })
1110            .collect();
1111        let masks = keep_masks(&[l0, l1], 0.5, 32, true);
1112        assert_eq!(kept(&masks), vec![96, 96]);
1113    }
1114
1115    /// align capped at inter; align=1 (off) keeps the raw threshold
1116    /// count; an all-pruned layer still keeps at least one neuron.
1117    #[test]
1118    fn keep_masks_edges() {
1119        let inter = 48;
1120        let l: Vec<f32> = (0..inter)
1121            .map(|i| if i < 47 { 1.0 } else { -2.0 })
1122            .collect();
1123        let masks = keep_masks(&[l.clone()], 0.5, 32, false);
1124        assert_eq!(kept(&masks), vec![48]); // 47 → 64 capped to 48
1125        let masks = keep_masks(&[l], 0.5, 1, false);
1126        assert_eq!(kept(&masks), vec![47]);
1127        let dead: Vec<f32> = vec![-5.0; inter];
1128        let masks = keep_masks(&[dead], 0.5, 32, false);
1129        assert_eq!(kept(&masks), vec![32]); // max(1) → rounded to 32
1130    }
1131}