gam-sae 0.3.130

Sparse-autoencoder latent-manifold terms for the gam penalized-likelihood engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
// [#780 line-count gate] Cohesive per-row jet / reconstruction-channel
// assembly for the streaming-exact arrow log-det, split out of
// `construction.rs` (which sits against the 10k-line gate). These are the
// `SaeManifoldTerm` methods that turn the converged cache into the per-row
// `SaeRowJets` the streaming log-det consumes: the row reconstruction program
// builder, the const-generic reconstruction / β-border channel fills (and
// their dynamic dispatchers), the scalar and 4-row-SIMD-batch row-jet
// builders, and the bounded look-ahead window refill. Included via `include!`
// from `construction.rs` so they keep the SAME module scope (`use super::*`),
// the same `impl SaeManifoldTerm` surface, and full private-field access.

impl SaeManifoldTerm {
    fn reconstruction_row_program_for_logdet(
        &self,
        rho: &SaeManifoldRho,
        row: usize,
        vars: &[SaeLocalRowVar],
        assignments: ArrayView1<'_, f64>,
        second_jets: &[Array4<f64>],
    ) -> Result<crate::row_jet_program::SaeReconstructionRowProgram, String> {
        use crate::row_jet_program::{
            AtomRowBasisJet, RowGate, SAE_FIXED_COORD_SLOT, SaeReconstructionRowProgram,
        };

        let p = self.output_dim();
        let k_atoms = self.k_atoms();
        if assignments.len() != k_atoms {
            return Err(format!(
                "reconstruction_row_program_for_logdet: assignments length {} != K={k_atoms}",
                assignments.len()
            ));
        }
        if second_jets.len() != k_atoms {
            return Err(format!(
                "reconstruction_row_program_for_logdet: second_jets length {} != K={k_atoms}",
                second_jets.len()
            ));
        }

        let mut logit_slot = vec![None; k_atoms];
        let mut coord_slot: Vec<Vec<usize>> = self
            .atoms
            .iter()
            .map(|atom| vec![SAE_FIXED_COORD_SLOT; atom.latent_dim])
            .collect();
        for (slot, var) in vars.iter().enumerate() {
            match *var {
                SaeLocalRowVar::Logit { atom } => {
                    if atom >= k_atoms {
                        return Err(format!(
                            "reconstruction_row_program_for_logdet: logit atom {atom} outside K={k_atoms}"
                        ));
                    }
                    logit_slot[atom] = Some(slot);
                }
                SaeLocalRowVar::Coord { atom, axis } => {
                    if atom >= k_atoms || axis >= coord_slot[atom].len() {
                        return Err(format!(
                            "reconstruction_row_program_for_logdet: coord ({atom},{axis}) outside atom layout"
                        ));
                    }
                    coord_slot[atom][axis] = slot;
                }
            }
        }

        let atoms: Vec<AtomRowBasisJet> = self
            .atoms
            .iter()
            .enumerate()
            .map(|(atom_idx, atom)| {
                let m = atom.basis_size();
                let d = atom.latent_dim;
                let second = &second_jets[atom_idx];
                AtomRowBasisJet {
                    phi: (0..m)
                        .map(|basis_col| atom.basis_values[[row, basis_col]])
                        .collect(),
                    d_phi: (0..m)
                        .map(|basis_col| {
                            (0..d)
                                .map(|axis| atom.basis_jacobian[[row, basis_col, axis]])
                                .collect()
                        })
                        .collect(),
                    d2_phi: (0..m)
                        .map(|basis_col| {
                            (0..d)
                                .map(|axis_a| {
                                    (0..d)
                                        .map(|axis_b| second[[row, basis_col, axis_a, axis_b]])
                                        .collect()
                                })
                                .collect()
                        })
                        .collect(),
                    decoder: (0..m)
                        .map(|basis_col| {
                            (0..p)
                                .map(|out_col| atom.decoder_coefficients[[basis_col, out_col]])
                                .collect()
                        })
                        .collect(),
                    latent_dim: d,
                }
            })
            .collect();

        let logits = self.assignment.logits.row(row).to_vec();
        let (gate, gate_shift, gate_scale) = match self.assignment.mode {
            AssignmentMode::Softmax { temperature, .. } => (
                RowGate::Softmax {
                    inv_tau: 1.0 / temperature,
                },
                vec![0.0; k_atoms],
                vec![1.0; k_atoms],
            ),
            AssignmentMode::IBPMap {
                temperature, alpha, ..
            } => {
                let effective_alpha = self
                    .assignment
                    .mode
                    .resolved_ibp_alpha(rho)
                    .unwrap_or(alpha);
                (
                    RowGate::PerAtomLogistic {
                        inv_tau: 1.0 / temperature,
                    },
                    vec![0.0; k_atoms],
                    ordered_geometric_shrinkage_prior(k_atoms, effective_alpha).to_vec(),
                )
            }
            AssignmentMode::JumpReLU {
                temperature,
                threshold,
            } => (
                RowGate::PerAtomLogistic {
                    inv_tau: 1.0 / temperature,
                },
                vec![threshold; k_atoms],
                logits
                    .iter()
                    .map(|&logit| if logit > threshold { 1.0 } else { 0.0 })
                    .collect(),
            ),
        };

        Ok(SaeReconstructionRowProgram {
            atoms,
            gate_value: assignments.to_vec(),
            logits,
            gate_scale,
            gate_shift,
            gate,
            logit_slot,
            coord_slot,
            n_primaries: vars.len(),
        })
    }

    fn fill_reconstruction_channels_from_program<const K: usize>(
        program: &crate::row_jet_program::SaeReconstructionRowProgram,
        sqrt_row_w: f64,
        first: &mut [Vec<f64>],
        second: &mut [Vec<Vec<f64>>],
    ) {
        // Build every output column at once with the per-atom gate / basis jets
        // hoisted out of the column loop (#932 perf): the softmax gate jet and
        // the basis jets are column-independent, so this removes the `out_dim×`
        // redundant recomputation the per-column path incurred (~9× faster at
        // K=8, out_dim=16). Bit-identical to per-column `_packed` assembly.
        let columns = program.reconstruction_all_columns_packed::<K>();
        for (out_col, tower) in columns.iter().enumerate() {
            let g = tower.g();
            let h = tower.h();
            for a in 0..K {
                first[a][out_col] = sqrt_row_w * g[a];
                for b in 0..K {
                    second[a][b][out_col] = sqrt_row_w * h[a][b];
                }
            }
        }
    }

    fn fill_reconstruction_channels_from_program_dynamic(
        program: &crate::row_jet_program::SaeReconstructionRowProgram,
        sqrt_row_w: f64,
        first: &mut [Vec<f64>],
        second: &mut [Vec<Vec<f64>>],
    ) -> Result<(), String> {
        macro_rules! dispatch {
            ($($k:literal),* $(,)?) => {
                match program.n_primaries {
                    $(
                        $k => {
                            Self::fill_reconstruction_channels_from_program::<$k>(
                                program,
                                sqrt_row_w,
                                first,
                                second,
                            );
                            Ok(())
                        }
                    )*
                    q => Err(format!(
                        "SAE row reconstruction Tower4 production path supports at most 16 row primaries, got {q}"
                    )),
                }
            };
        }
        dispatch!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
    }

    fn fill_beta_border_channels_from_program<const K: usize>(
        program: &crate::row_jet_program::SaeReconstructionRowProgram,
        sqrt_row_w: f64,
        border: &[SaeBorderChannel],
        beta: &mut [Vec<f64>],
        beta_deriv: &mut [Vec<Vec<f64>>],
        beta_l_deriv: &mut [Vec<Vec<f64>>],
    ) {
        let p = program.out_dim();
        // s = ζ_k(ℓ)·Φ_b(t_k) over the local (logit/coord) primaries, built from
        // the SAME gate_tower / basis_tower primitives as the reconstruction
        // column. Build all channels at once with the per-atom gate jet hoisted
        // (#932 perf): border channels sharing an atom reuse one gate jet instead
        // of recomputing it. The reconstruction is LINEAR in β, so this consumer
        // reads only the value (`beta`) and gradient (`beta_deriv` /
        // `beta_l_deriv`) channels — never a Hessian — so the jets are built as
        // first-order `Order1<K>` (value + grad), skipping the K×K Hessian the
        // `Order2` path would compute and discard. `Order1`'s value/grad are
        // bit-identical to `Order2`'s (#1591 order1 oracle).
        let chans: Vec<(usize, usize)> = border.iter().map(|c| (c.atom, c.basis_col)).collect();
        let sjets = program.beta_border_order1_packed::<K>(&chans);
        for (beta_pos, channel) in border.iter().enumerate() {
            let s = &sjets[beta_pos];
            let s_v = s.value();
            let s_g = s.g();
            for out_col in 0..p {
                let out_c = channel.output[out_col];
                beta[beta_pos][out_col] = sqrt_row_w * s_v * out_c;
                for a in 0..K {
                    // Reconstruction is linear in β, so beta_deriv and
                    // beta_l_deriv are the identical mixed ∂²ẑ_c/∂β∂p_a channel.
                    let mixed = sqrt_row_w * s_g[a] * out_c;
                    beta_deriv[a][beta_pos][out_col] = mixed;
                    beta_l_deriv[a][beta_pos][out_col] = mixed;
                }
            }
        }
    }

    fn fill_beta_border_channels_from_program_dynamic(
        program: &crate::row_jet_program::SaeReconstructionRowProgram,
        sqrt_row_w: f64,
        border: &[SaeBorderChannel],
        beta: &mut [Vec<f64>],
        beta_deriv: &mut [Vec<Vec<f64>>],
        beta_l_deriv: &mut [Vec<Vec<f64>>],
    ) -> Result<(), String> {
        macro_rules! dispatch {
            ($($k:literal),* $(,)?) => {
                match program.n_primaries {
                    $(
                        $k => {
                            Self::fill_beta_border_channels_from_program::<$k>(
                                program,
                                sqrt_row_w,
                                border,
                                beta,
                                beta_deriv,
                                beta_l_deriv,
                            );
                            Ok(())
                        }
                    )*
                    q => Err(format!(
                        "SAE β border Tower4 production path supports at most 16 row primaries, got {q}"
                    )),
                }
            };
        }
        dispatch!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
    }

    pub(crate) fn row_jets_for_logdet(
        &self,
        rho: &SaeManifoldRho,
        row: usize,
        vars: Vec<SaeLocalRowVar>,
        assignments: ArrayView1<'_, f64>,
        second_jets: &[Array4<f64>],
        border: &[SaeBorderChannel],
    ) -> Result<SaeRowJets, String> {
        let p = self.output_dim();
        let q = vars.len();
        let sqrt_row_w = self
            .row_loss_weights
            .as_deref()
            .map_or(1.0, |w| w[row].sqrt());
        let mut first = vec![vec![0.0_f64; p]; q];
        let mut second = vec![vec![vec![0.0_f64; p]; q]; q];
        let program =
            self.reconstruction_row_program_for_logdet(rho, row, &vars, assignments, second_jets)?;
        Self::fill_reconstruction_channels_from_program_dynamic(
            &program,
            sqrt_row_w,
            &mut first,
            &mut second,
        )?;

        // β BORDER CHANNELS (#932): single-sourced through the SAME
        // reconstruction row program used above. A β border channel is one free
        // decoder coefficient whose per-row contribution to output column `c` is
        // ζ_k(ℓ)·Φ_b(t_k)·output_c — linear in β — so the value channel is
        // `beta_border_tower.v · output_c` and the mixed ∂²ẑ_c/∂β∂p_a channel
        // (both `beta_deriv` and `beta_l_deriv`, identical because the map is
        // linear in β) is `beta_border_tower.g[a] · output_c`. The former hand
        // packing — with its own gate first-derivative recursion
        // (`gate_first_derivatives_for_row`) and term-by-term basis/jacobian
        // reads — is replaced by the tower, which carries every gate/basis
        // derivative automatically and is pinned to the prior hand path by
        // `sae_row_jet_program_matches_production_row_jets_on_converged_cache`.
        let mut beta = vec![vec![0.0_f64; p]; border.len()];
        let mut beta_deriv = vec![vec![vec![0.0_f64; p]; border.len()]; q];
        let mut beta_l_deriv = vec![vec![vec![0.0_f64; p]; border.len()]; q];
        Self::fill_beta_border_channels_from_program_dynamic(
            &program,
            sqrt_row_w,
            border,
            &mut beta,
            &mut beta_deriv,
            &mut beta_l_deriv,
        )?;

        Ok(SaeRowJets {
            vars,
            first,
            second,
            beta,
            beta_deriv,
            beta_l_deriv,
        })
    }

    /// Build [`SaeRowJets`] for FOUR rows at once via the 4-row SIMD batch
    /// (#932), returning `None` (so the caller falls back to the scalar per-row
    /// `row_jets_for_logdet`) when the four rows are not softmax-aligned (same
    /// primary layout / temperature). Each lane's `SaeRowJets` is BIT-IDENTICAL
    /// to `row_jets_for_logdet` on that row: the batch primitives
    /// (`reconstruction_all_columns_batch4` / `beta_border_order1_batch4`) are
    /// proven lane-`i` `to_bits`-identical to the scalar `*_packed` paths, and the
    /// `√w` / `output_c` scaling here mirrors the scalar fills term-for-term.
    fn row_jets_for_logdet_batch4(
        &self,
        rho: &SaeManifoldRho,
        rows: [usize; 4],
        cache: &ArrowFactorCache,
        second_jets: &[Array4<f64>],
        border: &[SaeBorderChannel],
    ) -> Result<Option<[SaeRowJets; 4]>, String> {
        let p = self.output_dim();
        let k_atoms = self.k_atoms();
        let mut progs: Vec<crate::row_jet_program::SaeReconstructionRowProgram> =
            Vec::with_capacity(4);
        let mut vars_each: Vec<Vec<SaeLocalRowVar>> = Vec::with_capacity(4);
        let mut sqrt_w = [1.0_f64; 4];
        for (i, &row) in rows.iter().enumerate() {
            let vars = self.row_vars_for_cache_row(row, cache)?;
            let mut a = Array1::<f64>::zeros(k_atoms);
            self.assignment.try_assignments_row_for_rho_into(
                row,
                rho,
                a.as_slice_mut().expect("contiguous assignment scratch"),
            )?;
            let prog =
                self.reconstruction_row_program_for_logdet(rho, row, &vars, a.view(), second_jets)?;
            sqrt_w[i] = self
                .row_loss_weights
                .as_deref()
                .map_or(1.0, |w| w[row].sqrt());
            vars_each.push(vars);
            progs.push(prog);
        }
        let refs = [&progs[0], &progs[1], &progs[2], &progs[3]];
        macro_rules! dispatch {
            ($($k:literal),* $(,)?) => {
                match progs[0].n_primaries {
                    $( $k => Self::batch4_assemble::<$k>(refs, &vars_each, &sqrt_w, border, p), )*
                    _ => Ok(None),
                }
            };
        }
        dispatch!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
    }

    /// Assemble the four lanes of a SIMD batch into per-row [`SaeRowJets`],
    /// applying the identical `√w` / `output_c` scaling the scalar fills use.
    /// Returns `None` if the rows are not batchable (the batch primitives
    /// decline), so the caller can fall back to scalar.
    fn batch4_assemble<const K: usize>(
        rows: [&crate::row_jet_program::SaeReconstructionRowProgram; 4],
        vars_each: &[Vec<SaeLocalRowVar>],
        sqrt_w: &[f64; 4],
        border: &[SaeBorderChannel],
        p: usize,
    ) -> Result<Option<[SaeRowJets; 4]>, String> {
        use crate::row_jet_program::SaeReconstructionRowProgram;
        let recon = match SaeReconstructionRowProgram::reconstruction_all_columns_batch4::<K>(rows) {
            Some(r) => r,
            None => return Ok(None),
        };
        let chans: Vec<(usize, usize)> = border.iter().map(|c| (c.atom, c.basis_col)).collect();
        let bjets = match SaeReconstructionRowProgram::beta_border_order1_batch4::<K>(rows, &chans) {
            Some(b) => b,
            None => return Ok(None),
        };
        let mut outs: Vec<SaeRowJets> = Vec::with_capacity(4);
        for lane in 0..4 {
            let sqrt = sqrt_w[lane];
            let mut first = vec![vec![0.0_f64; p]; K];
            let mut second = vec![vec![vec![0.0_f64; p]; K]; K];
            for (out_col, tower) in recon[lane].iter().enumerate() {
                let g = tower.g();
                let h = tower.h();
                for a in 0..K {
                    first[a][out_col] = sqrt * g[a];
                    for b in 0..K {
                        second[a][b][out_col] = sqrt * h[a][b];
                    }
                }
            }
            let mut beta = vec![vec![0.0_f64; p]; border.len()];
            let mut beta_deriv = vec![vec![vec![0.0_f64; p]; border.len()]; K];
            let mut beta_l_deriv = vec![vec![vec![0.0_f64; p]; border.len()]; K];
            for (beta_pos, channel) in border.iter().enumerate() {
                let s = &bjets[lane][beta_pos];
                let s_v = s.value();
                let s_g = s.g();
                for out_col in 0..p {
                    let out_c = channel.output[out_col];
                    beta[beta_pos][out_col] = sqrt * s_v * out_c;
                    for a in 0..K {
                        let mixed = sqrt * s_g[a] * out_c;
                        beta_deriv[a][beta_pos][out_col] = mixed;
                        beta_l_deriv[a][beta_pos][out_col] = mixed;
                    }
                }
            }
            outs.push(SaeRowJets {
                vars: vars_each[lane].clone(),
                first,
                second,
                beta,
                beta_deriv,
                beta_l_deriv,
            });
        }
        let arr: [SaeRowJets; 4] = outs
            .try_into()
            .map_err(|_| "batch4_assemble produced wrong lane count".to_string())?;
        Ok(Some(arr))
    }

    /// Refill the bounded (≤4-row) look-ahead jet window starting at `start`,
    /// using the 4-row SIMD batch when the next four rows are softmax-aligned and
    /// falling back to one scalar row otherwise (also the `<4` remainder).
    /// Returns the next unbuilt row index. Bit-identical to per-row
    /// `row_jets_for_logdet` either way.
    fn refill_jet_window(
        &self,
        rho: &SaeManifoldRho,
        start: usize,
        n: usize,
        cache: &ArrowFactorCache,
        second_jets: &[Array4<f64>],
        border: &[SaeBorderChannel],
        window: &mut std::collections::VecDeque<SaeRowJets>,
    ) -> Result<usize, String> {
        if start + 4 <= n {
            if let Some(batch) = self.row_jets_for_logdet_batch4(
                rho,
                [start, start + 1, start + 2, start + 3],
                cache,
                second_jets,
                border,
            )? {
                window.extend(batch);
                return Ok(start + 4);
            }
        }
        let vars = self.row_vars_for_cache_row(start, cache)?;
        let mut a = Array1::<f64>::zeros(self.k_atoms());
        self.assignment.try_assignments_row_for_rho_into(
            start,
            rho,
            a.as_slice_mut().expect("contiguous assignment scratch"),
        )?;
        let jets = self.row_jets_for_logdet(rho, start, vars, a.view(), second_jets, border)?;
        window.push_back(jets);
        Ok(start + 1)
    }
}