use super::tests::{TestPeriodicEvaluator, periodic_basis};
use super::*;
use crate::assignment::{AssignmentMode, SaeAssignment};
use gam_terms::latent::LatentManifold;
use ndarray::array;
use std::sync::Arc;
fn framed_circle_term() -> (SaeManifoldTerm, Array2<f64>, SaeManifoldRho) {
let n = 64usize;
let p = 16usize;
let coords = Array2::from_shape_fn((n, 1), |(row, _)| (row as f64 + 0.5) / n as f64);
let (phi, jet) = periodic_basis(&coords);
let m = phi.ncols();
let decoder = Array2::from_shape_fn((m, p), |(b, c)| {
(1.0 / (1.0 + b as f64)) * ((b as f64 + 1.0) * (c as f64 + 1.0)).cos()
});
let atom = SaeManifoldAtom::new(
"iso_dev_1783",
SaeAtomBasisKind::Periodic,
1,
phi.clone(),
jet.clone(),
decoder.clone(),
Array2::<f64>::eye(m),
)
.unwrap()
.with_basis_evaluator(Arc::new(TestPeriodicEvaluator));
let target = phi.dot(&decoder);
let assignment = SaeAssignment::from_blocks_with_mode_and_manifolds(
Array2::<f64>::zeros((n, 1)),
vec![coords],
vec![LatentManifold::Circle { period: 1.0 }],
AssignmentMode::softmax(1.0),
)
.unwrap();
let term = SaeManifoldTerm::new(vec![atom], assignment).unwrap();
let rho = SaeManifoldRho::new(0.0, 0.8_f64.ln(), vec![array![1.0_f64.ln()]]);
(term, target, rho)
}
fn isometry_registry(n: usize) -> gam_terms::analytic_penalties::AnalyticPenaltyRegistry {
use gam_terms::analytic_penalties::{
AnalyticPenaltyKind, AnalyticPenaltyRegistry, IsometryPenalty, PsiSlice,
};
let mut registry = AnalyticPenaltyRegistry::new();
registry.push(AnalyticPenaltyKind::Isometry(Arc::new(
IsometryPenalty::new_euclidean(PsiSlice::full(n, Some(1)), 1),
)));
registry
}
#[test]
fn framed_circle_isometry_fit_builds_device_sae_pcg_data_1783() {
let (mut term, target, rho) = framed_circle_term();
let n = target.nrows();
let activated = term
.auto_activate_decoder_frames()
.expect("frame auto-activation must succeed");
assert!(
activated > 0 && term.frames_active(),
"the framed β-tier must engage for this fixture (activated={activated}); \
otherwise the test would exercise the full-B path where device data is \
always built and the #1783 skip never applied"
);
let registry = isometry_registry(n);
let sys_deferred = term
.assemble_arrow_schur_scaled_with_beta_penalty_probe_threshold(
target.view(),
&rho,
Some(®istry),
1.0,
1,
)
.expect("framed deferred-factored circle + isometry assemble must succeed");
assert!(
sys_deferred.device_sae_pcg.is_some(),
"#1783: the deferred-factored (large-K) framed circle + isometry regime — \
the reporter's actual B200 shape — must carry device_sae_pcg data so the \
device-resident SAE PCG seam is reachable (was None: silent CPU, GPU 0%)"
);
assert!(
sys_deferred.device_sae_pcg.as_ref().unwrap().frame.is_some(),
"the framed fit must carry the factored (Grassmann-frame) device payload"
);
let sys_bare_deferred = term
.assemble_arrow_schur_scaled_with_beta_penalty_probe_threshold(
target.view(),
&rho,
None,
1.0,
1,
)
.expect("framed deferred bare assemble must succeed");
assert!(
sys_bare_deferred.device_sae_pcg.is_some(),
"the deferred framed circle fit without penalties must also carry device \
data (baseline for the isometry parity)"
);
assert_eq!(
sys_deferred.k, sys_bare_deferred.k,
"the isometry gauge must not change the factored border width"
);
let sys_dense_iso = term
.assemble_arrow_schur(target.view(), &rho, Some(®istry))
.expect("framed dense circle + isometry assemble must succeed");
assert!(
sys_dense_iso.device_sae_pcg.is_none(),
"#1783 regression pin: on the DENSE β-tier the isometry writes a real dense \
H_ββ the framed device kernel cannot model, so the device must decline \
(CPU fallback) — and `dense_written` must be set so that curvature is \
still applied, not silently dropped"
);
let sys_dense_bare = term
.assemble_arrow_schur(target.view(), &rho, None)
.expect("framed dense bare assemble must succeed");
assert!(
sys_dense_bare.device_sae_pcg.is_some(),
"on the DENSE β-tier a fit with NO dense penalty carries no border dense \
term, so the device data is installed — proving it is the isometry's dense \
H_ββ (not the frame layout) that declines the dense-tier device path"
);
}
#[test]
fn offload_gate_admits_d_atom_1_at_token_scale_1783() {
let policy = gam_gpu::policy::GpuDispatchPolicy::default();
assert!(
policy.reduced_schur_matvec_should_offload(40_456, 256, 1, 1),
"#1783: the reporter's realistic d_atom=1 shape (n=40456, k=256, d=1) must \
clear the offload gate at a single CG apply — the gate was never the \
blocker for thin curve atoms"
);
assert!(
policy.reduced_schur_matvec_should_offload(
24_576,
64,
1,
gam_gpu::policy::GpuDispatchPolicy::MATVEC_OFFLOAD_MIN_CG_ITERS,
),
"#1783: the earlier K=64 d_atom=1 shape must also clear the offload gate"
);
assert!(
!policy.reduced_schur_matvec_should_offload(64, 9, 1, 1),
"a tiny framed circle fixture must NOT engage the device (honest fallback)"
);
}