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