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
//! #2027 — deterministic repro for the K≥2 whitened dictionary CO-COLLAPSE, and
//! the regression guard for the disjoint-subspace / ownership-anchor / reseed-
//! hysteresis fix.
//!
//! Two planted circles live in DISJOINT 2-planes of an ambient `p`-dim cloud; the
//! per-column-standardized ("whitened") target is their sum, so a faithful K=2
//! reconstruction REQUIRES both atoms to carry signal on different subspaces.
//! Before the fix the joint decoder refit at the co-collapse reseed re-spread one
//! residual direction across both atoms and the gate let them trade rows, so the
//! dictionary re-symmetrised into a single shared basin and the reconstruction EV
//! sat near the signal-free null floor. With the greedy disjoint-subspace decoder
//! refit + soft row-ownership anchor + reseed cooldown the two atoms hold distinct
//! territories and the fit recovers a materially positive EV.
use super::tests::deterministic_circle_noise;
use super::*;
/// Whitened two-circle target: circle A lives on the even ambient columns, circle
/// B on the odd ones (disjoint deterministic near-orthonormal 2-frames), driven by
/// two INCOMMENSURATE phases so the circles are not row-aligned. Each column is
/// standardized to zero mean / unit variance (the whitening proxy that puts both
/// circles on a common scale, the regime the real-data co-collapse lives in).
fn two_circle_whitened_target(n: usize, p: usize, sigma: f64) -> Array2<f64> {
let mut fa = Array2::<f64>::zeros((2, p));
let mut fb = Array2::<f64>::zeros((2, p));
for j in 0..p {
if j % 2 == 0 {
fa[[0, j]] = deterministic_circle_noise(j, 0);
fa[[1, j]] = deterministic_circle_noise(j, 1);
} else {
fb[[0, j]] = deterministic_circle_noise(j, 2);
fb[[1, j]] = deterministic_circle_noise(j, 3);
}
}
for f in [&mut fa, &mut fb] {
for r in 0..2 {
let nrm = (0..p).map(|j| f[[r, j]] * f[[r, j]]).sum::<f64>().sqrt();
for j in 0..p {
f[[r, j]] /= nrm.max(1.0e-300);
}
}
}
let mut z = Array2::<f64>::zeros((n, p));
for row in 0..n {
let ta = std::f64::consts::TAU * (row as f64) / (n as f64);
let tb = std::f64::consts::TAU * (2.0 * row as f64 + 0.37) / (n as f64);
let (ca, sa) = (ta.cos(), ta.sin());
let (cb, sb) = (tb.cos(), tb.sin());
for j in 0..p {
z[[row, j]] = ca * fa[[0, j]]
+ sa * fa[[1, j]]
+ cb * fb[[0, j]]
+ sb * fb[[1, j]]
+ sigma * deterministic_circle_noise(row, j + 7);
}
}
for j in 0..p {
let mut mean = 0.0_f64;
for row in 0..n {
mean += z[[row, j]];
}
mean /= n as f64;
let mut var = 0.0_f64;
for row in 0..n {
let d = z[[row, j]] - mean;
var += d * d;
}
let sd = (var / n as f64).sqrt().max(1.0e-12);
for row in 0..n {
z[[row, j]] = (z[[row, j]] - mean) / sd;
}
}
z
}
/// Build a fresh K=2 periodic term seeded (production PCA seed) from the whitened
/// two-circle target, decoders cold at zero.
fn two_circle_k2_term(n: usize, p: usize, m: usize) -> (SaeManifoldTerm, Array2<f64>) {
let d = 1usize;
let k = 2usize;
let target = two_circle_whitened_target(n, p, 0.05);
let basis_kinds = vec![SaeAtomBasisKind::Periodic; k];
let dims = vec![d; k];
let seed = sae_pca_seed_initial_coords(target.view(), &basis_kinds, &dims).unwrap();
let evaluator = Arc::new(PeriodicHarmonicEvaluator::new(m).unwrap());
let mut basis_values = Array3::<f64>::zeros((k, n, m));
let mut basis_jacobian = Array4::<f64>::zeros((k, n, m, d));
let decoder = Array3::<f64>::zeros((k, m, p));
let mut penalties = Array3::<f64>::zeros((k, m, m));
let mut coords_vec: Vec<Array2<f64>> = Vec::new();
for atom in 0..k {
let coords = seed.slice(s![atom, .., 0..d]).to_owned();
let (phi, jet) = evaluator.evaluate(coords.view()).unwrap();
basis_values.slice_mut(s![atom, .., ..]).assign(&phi);
basis_jacobian.slice_mut(s![atom, .., .., ..]).assign(&jet);
penalties
.slice_mut(s![atom, .., ..])
.assign(&Array2::<f64>::eye(m));
coords_vec.push(coords);
}
let logits = Array2::<f64>::zeros((n, k));
let mut evaluators: Vec<Option<Arc<dyn SaeBasisSecondJet>>> = Vec::new();
for _ in 0..k {
evaluators.push(Some(evaluator.clone()));
}
let term = term_from_padded_blocks_with_mode(
n,
p,
&basis_kinds,
basis_values.view(),
basis_jacobian.view(),
&vec![m; k],
&dims,
decoder.view(),
penalties.view(),
logits.view(),
&coords_vec,
AssignmentMode::ibp_map(1.0, 1.0, false),
&evaluators,
)
.unwrap();
(term, target)
}
/// The K=2 whitened two-circle fit must recover a materially positive
/// reconstruction EV — NOT co-collapse to the signal-free null floor. Two disjoint
/// circles together span a rank-4 subspace of the whitened cloud, so an honest K=2
/// dictionary explains a large fraction of the variance (the torch proxy reaches
/// ≈0.47 on the sibling nursery experiment). The disjoint-subspace decoder refit,
/// row-ownership anchor, and reseed cooldown keep the two atoms on distinct
/// territories through the joint solve.
#[test]
pub(crate) fn two_circle_whitened_k2_recovers_disjoint_signal_2027() {
let n = 96usize;
let p = 16usize;
let m = 5usize; // [1, sin2πt, cos2πt, sin4πt, cos4πt]
let (mut term, target) = two_circle_k2_term(n, p, m);
let mut rho = SaeManifoldRho::new(
0.0,
-6.0,
vec![Array1::<f64>::zeros(1), Array1::<f64>::zeros(1)],
);
let loss = term
.run_joint_fit_arrow_schur(target.view(), &mut rho, None, 60, 0.05, 1.0e-3, 1.0e-3)
.unwrap();
assert!(loss.total().is_finite(), "loss must stay finite");
let ev = term
.dictionary_reconstruction_ev(target.view(), &rho)
.unwrap();
eprintln!(
"[#2027 repro] K=2 whitened two-circle EV = {ev:.4}, cocollapse_reseeds = {}",
term.dictionary_cocollapse_reseeds
);
assert!(
ev > 0.20,
"K=2 whitened two-circle dictionary co-collapsed: EV={ev:.4} (expected > 0.20; \
two disjoint circles span a rank-4 subspace that an honest K=2 fit recovers)"
);
}
/// The greedy disjoint-subspace decoder refit must, on a co-collapsed reseed,
/// leave BOTH atoms carrying material decoder norm — never let one atom take all
/// the residual while the other stays ≈0 (the relative-norm collapse the joint
/// refit permitted). A direct unit check of `refit_decoder_sequential_deflation`.
#[test]
pub(crate) fn sequential_deflation_gives_both_atoms_material_norm_2027() {
let n = 96usize;
let p = 16usize;
let m = 5usize;
let (mut term, target) = two_circle_k2_term(n, p, m);
term.refit_decoder_sequential_deflation(target.view())
.unwrap();
let mut norms = [0.0_f64; 2];
for (atom_idx, atom) in term.atoms.iter().enumerate() {
norms[atom_idx] = atom
.decoder_coefficients
.iter()
.map(|v| v * v)
.sum::<f64>()
.sqrt();
}
let (lo, hi) = if norms[0] <= norms[1] {
(norms[0], norms[1])
} else {
(norms[1], norms[0])
};
eprintln!("[#2027 repro] deflation decoder norms = {norms:?}");
assert!(hi > 0.0, "at least one atom must carry decoder norm");
assert!(
lo > 1.0e-3 * hi,
"both atoms must carry material decoder norm after deflation: norms={norms:?}"
);
}
/// #2027 WIDTH-SCALING + STRUCTURE-RECOVERY guard — the discriminating test.
///
/// Two facts from the sibling nursery evidence make raw EV an INSUFFICIENT guard:
/// 1. The pathology is WIDTH-dependent — the REML control converges at `p = 16`
/// but hangs / co-collapses at `p ≈ 96`. A fix must be checked at BOTH widths:
/// the narrow arm must stay healthy, the wide arm is the regime being rescued.
/// 2. Its fingerprint is a co-collapse that posts a DECENT reconstruction EV while
/// recovering NEITHER planted circle — both atoms pile into one shared subspace
/// and ride one circle plus noise (torch proxy: EV 0.63, adjacency 0.43/0.25).
/// Asserting EV alone therefore passes a co-collapsed fit.
///
/// The two circles are planted on DISJOINT ambient column PARITIES (circle A on the
/// even output channels, circle B on the odd), so an honest K=2 dictionary MUST
/// separate: one atom's decoder concentrates its Frobenius energy on the even
/// channels, the other on the odd. Co-collapse piles both atoms onto the same
/// channels — detected here as both atoms landing on the SAME side of the 0.5
/// even-energy split. We require, at both widths: finite loss (no thrash), a
/// materially positive EV, and the two atoms SEPARATED onto opposite-parity
/// subspaces (the structure the disjoint-deflation + ownership-anchor fix restores).
///
/// NOTE: this exercises the INNER joint solve (`run_joint_fit_arrow_schur` at a
/// fixed ρ) — the co-collapse / structure-recovery layer the seeding + anchoring fix
/// lives in — not the outer REML ρ-search whose non-PD-Hessian retries are the
/// separate Python-side "hang" at wide `p`.
#[test]
pub(crate) fn two_circle_separates_at_narrow_and_wide_widths_2027() {
let m = 5usize;
// (n, p): a NARROW arm that must stay healthy and the WIDE arm being rescued.
for &(n, p) in &[(96usize, 16usize), (120usize, 96usize)] {
let (mut term, target) = two_circle_k2_term(n, p, m);
let mut rho = SaeManifoldRho::new(
0.0,
-6.0,
vec![Array1::<f64>::zeros(1), Array1::<f64>::zeros(1)],
);
let loss = term
.run_joint_fit_arrow_schur(target.view(), &mut rho, None, 60, 0.05, 1.0e-3, 1.0e-3)
.unwrap();
assert!(
loss.total().is_finite(),
"p={p}: joint fit must return a finite loss (no thrash / NaN)"
);
let ev = term
.dictionary_reconstruction_ev(target.view(), &rho)
.unwrap();
// Per-atom decoder energy split across even vs odd output channels: circle A
// lives on even channels, circle B on odd.
let mut even_frac = [0.0_f64; 2];
for (atom_idx, atom) in term.atoms.iter().enumerate() {
let b = &atom.decoder_coefficients; // (m × p)
let mut e_even = 0.0_f64;
let mut e_odd = 0.0_f64;
for col in 0..b.nrows() {
for out in 0..p {
let v = b[[col, out]] * b[[col, out]];
if out % 2 == 0 {
e_even += v;
} else {
e_odd += v;
}
}
}
even_frac[atom_idx] = e_even / (e_even + e_odd).max(1.0e-300);
}
eprintln!(
"[#2027 repro] p={p}: EV={ev:.4}, per-atom even-energy fraction={even_frac:?}, \
cocollapse_reseeds={}",
term.dictionary_cocollapse_reseeds
);
assert!(
ev > 0.20,
"p={p}: dictionary co-collapsed to the null floor (EV={ev:.4} <= 0.20)"
);
// STRUCTURE RECOVERY: the atoms must land on OPPOSITE planted subspaces — one
// even-dominant, one odd-dominant. Both on the same side of 0.5 is the
// co-collapse signature (acceptable EV, neither circle recovered).
let (lo, hi) = if even_frac[0] <= even_frac[1] {
(even_frac[0], even_frac[1])
} else {
(even_frac[1], even_frac[0])
};
assert!(
lo < 0.5 && hi > 0.5,
"p={p}: atoms did NOT separate onto the two planted circles \
(even-energy fractions {even_frac:?} both on one side of 0.5 = co-collapse: \
EV looks fine but neither circle is recovered)"
);
}
}
/// #2082/#2132/#1893 — the STRUCTURAL coherence detector fires on FUNCTIONAL
/// REDUNDANCY (two atoms that reconstruct the SAME rows — a genuine duplicate),
/// NOT on mere output-subspace sharing. Three cases pin the contract:
///
/// (1) ORTHOGONAL output subspaces → frames don't even overlap → NOT flagged.
/// (2) SAME output subspace but DIFFERENT charts (identical decoder, distinct
/// phases) → the atoms decode DIFFERENT rows, so their gated contributions
/// `Y_k = diag(a)ΦB` are NOT collinear → benign, NOT flagged. This is the
/// over-complete (`K > rank`) regime the old frame-coherence detector
/// false-positived on (the `ibp_default_alpha` regression: healthy EV≈0.99,
/// frame coherence ≈1, contribution cosine ≈ the independence null): several
/// curved atoms MUST share the ≤`p`-dim output space while encoding distinct
/// structure.
/// (3) TRUE DUPLICATE (identical decoder AND identical chart) → `Y_0 ∝ Y_1` →
/// contribution cosine → 1 → FLAGGED.
#[test]
pub(crate) fn structural_coherence_detector_fires_on_duplicate_not_orthogonal_2082() {
let n = 48usize;
let p = 8usize;
let m = 5usize;
// (1) ORTHOGONAL output subspaces: atom 0 decodes only EVEN output channels,
// atom 1 only ODD → orthogonal frames → not a candidate → NOT flagged.
let (mut term, _target) = two_circle_k2_term(n, p, m);
for atom in 0..2 {
let mut b = Array2::<f64>::zeros((m, p));
for col in 0..m {
let out = (if atom == 0 { 0 } else { 1 }) + 2 * (col % (p / 2));
if out < p {
b[[col, out]] = 1.0;
}
}
term.atoms[atom].decoder_coefficients = b;
}
assert!(
term.structural_coherence_collapse_detected()
.unwrap()
.is_none(),
"orthogonal-subspace atoms must NOT be flagged as structurally collapsed"
);
// (2) SAME output subspace, DIFFERENT charts: identical decoder, but the two
// atoms keep their distinct PCA-seeded phases → same output frame (coherence
// ≈1) yet DIFFERENT per-row contributions → NOT functional redundancy → the
// functional-redundancy detector must stay SILENT (the old frame-only detector
// wrongly fired here).
let mut dup = Array2::<f64>::zeros((m, p));
dup[[1, 0]] = 1.0;
dup[[2, 1]] = 1.0;
term.atoms[0].decoder_coefficients = dup.clone();
term.atoms[1].decoder_coefficients = dup.clone();
let mut shifted = term.assignment.coords[0].as_matrix().to_owned();
for t in shifted.iter_mut() {
*t = (*t + 0.25).rem_euclid(1.0);
}
let shifted_flat: Array1<f64> = shifted.iter().copied().collect();
term.assignment.coords[1].set_flat(shifted_flat.view());
term.atoms[1].refresh_basis(shifted.view()).unwrap();
assert!(
term.structural_coherence_collapse_detected()
.unwrap()
.is_none(),
"same output subspace with DIFFERENT charts is benign over-completeness and \
must NOT be flagged (the ibp_default_alpha false positive)"
);
// (3) TRUE DUPLICATE: identical decoder AND identical chart (copy atom 0's
// coords onto atom 1) → the two atoms reconstruct the SAME rows → contribution
// cosine → 1 → FLAGGED as the genuine high-EV co-collapse.
let coords0 = term.assignment.coords[0].as_matrix().to_owned();
let flat0: Array1<f64> = coords0.iter().copied().collect();
term.assignment.coords[1].set_flat(flat0.view());
term.atoms[1]
.refresh_basis(term.assignment.coords[1].as_matrix().view())
.unwrap();
let hit = term
.structural_coherence_collapse_detected()
.unwrap()
.expect("a true duplicate (identical decoder AND chart) must be flagged");
assert_eq!((hit.0, hit.1), (0, 1), "the offending pair is (0, 1)");
assert!(
hit.2 > 0.9,
"true-duplicate contribution cosine must be ~1, got {}",
hit.2
);
}