Repository navigation
Add Newton coverage: 3-model, rank-reduced, three-way - #284
Merged
Merged
Conversation
Closes the #272 gap: MLX Newton tests only ran n_models<=2, which cannot distinguish a correct per-model curvature index from a transposition error that happens to line up for exactly two models.
The existing test_newton_multimodel_finite_and_shaped stops at n_models=2, which cannot distinguish a correct per-model curvature index from a transposition error that happens to line up for exactly two models (issue #272).
Extends the existing 2-model MLX-vs-float64-torch curvature/M-step comparison to a third model. Cheap: _warm_model/_torch_twin already parametrize on n_models, so this closes the #272 cross-backend gap with only the argument changed.
The three features (share_comps, do_newton, a non-default pdftype) only have pairwise coverage: sharing x Newton, Newton x families, and sharing x switcher each live in separate tests. Pin the composition directly with a fixed-family (pdftype=2) and an adaptive-switcher (pdftype=1) variant (issue #277).
The torch equivalent of the MLX three-way test: share_comps=True, do_newton=True, and a non-default pdftype together had only pairwise coverage until now (issue #277).
The do_newton=False comment was stale: it described a shape crash that was issue #267 itself, fixed in PR #271 (dgm[None, :]) and already regression-tested up to 3 models. Turning Newton on here (newt_start=3) closes the numpy leg of the rank-reduction x sharing x Newton three-way gap noted in PR #284 -- a share pass collapses 7 components on the rank-16 sphere and the fit still completes finite and converged.
test_newton_three_model_finite_and_shaped now checks the full A/W/mu/alpha/beta/rho/gm/c finiteness loop the numpy/MLX #272 tests already carry, instead of only c and A. Both hardcoded ("nan_ll", "singular_ll") tuples in this file (the one this PR added and the pre-existing one in test_multimodel_rejection_keeps_bias_c_finite) now reference AMICATorchNG._DEGENERATE_STOP_REASONS instead of duplicating the literal.
test_rank_reduced_newton_fit_completes checked only A and the unmixing matrix. Add the full W/mu/alpha/beta/rho/gm/c finiteness loop so the torch #273 test carries the same depth as its numpy and MLX counterparts.
test_three_model_newton_curvature_matches_float64_twin was missing the n_newton_fallbacks parity check and the _ndtmpsum agreement (rel=1e-4) that its 2-model sibling (test_multimodel_newton_mstep_matches_float64_twin) already carries.
comp_thresh=0.9 + seed=42 sits in the near-total-collapse regime .context/issue-264/newton_findings.md documents as trajectory chaos (26-31 of 32 cross-model pairs merging on a 2-model fit, seed- and config-dependent nan_params on MLX). Moved both three-way tests to the doc's verified stable zone, comp_thresh=0.99/seed=3 (the same seed test_low_rank_projected_data_share_fit_completes uses). Verified locally that merges still fire well short of the chaos threshold: 11 of 32 pairs for the fixed-family variant, 8 of 32 for the adaptive switcher, so the comp_used guard stays non-vacuous.
Same fix as the MLX three-way tests: comp_thresh=0.9/seed=42 sits in the near-total-collapse regime .context/issue-264/newton_findings.md documents as trajectory chaos. Moved to the doc's verified stable zone, comp_thresh=0.99/seed=3. Verified locally: 11 of 32 pairs merge for the fixed-family variant, 8 of 32 for the adaptive switcher.
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Test-only PR closing the three Newton coverage gaps epic #260's reviews found (PR #271, PR #276). No production code changed.
#272 -- 3-model Newton
The curvature arrays are per-(source, model) and the direction/fallback loop iterates over models, so a 2-model fit cannot distinguish a correct per-model index from a transposition error that happens to line up for exactly two models.
torch:
test_newton_three_model_finite_and_shapedadded totests/torch_tests/test_ng_backend.py-- curvature shapes(NW, 3), non-degenerate stop, the fullA/W/mu/alpha/beta/rho/gm/cfiniteness loop, all 3 unmixing matrices finite.MLX:
test_three_model_newton_mstep_and_fit_are_finiteadded totests/mlx_tests/test_mlx_newton.py, mirroring the torch test.numpy: the existing
tests/test_numpy_newton_multimodel.py::test_multimodel_newton_fit_finalizes_curvature(issue #267) already parametrizesnum_modelsup to 3 and asserted curvature shape/finiteness, but not a non-degenerate stop or the non-curvature parameters -- extended minimally withmodel.converged/stop_reasonand full-parameter finiteness checks to match the depth of the new torch/MLX tests.Cross-backend: added, since it was cheap --
test_three_model_newton_curvature_matches_float64_twinintests/test_mlx_newton_cross_backend.pyextends the existing 2-model MLX-vs-float64-torch curvature/M-step comparison ton_models=3by only changing the_warm_model/_torch_twinargument (both already parametrize onn_models), and now also carries then_newton_fallbacksparity and_ndtmpsum(rel=1e-4) checks its 2-model sibling has, so the two tests are at matched depth.#273 -- rank-reduced Newton
No backend had ever run Newton on rank-reduced data: real EEG projected onto a rank-20 subspace (what Maxwell filtering does to MEG, the #221 report), fitted with automatic rank detection.
Per backend, one new test asserting completion, finite parameters, and a non-degenerate stop:
test_rank_reduced_newton_fit_completesintests/torch_tests/test_ng_rank_deficient.py(now also carries the fullA/W/mu/alpha/beta/rho/gm/cfiniteness loop, matching numpy/MLX),tests/mlx_tests/test_mlx_newton.py, andtests/test_numpy_newton_multimodel.py.All three passed without any production change. I probed each backend by hand before committing to the tests (per the NO-MOCKS/real-repro standard): the Newton curvature and 2x2 direction solve are allocated at the model's working dimensionality (post rank-detection), not the input channel count, so nothing in the reduction touches the Newton arithmetic.
tests/test_numpy_share_comps.py::test_rank_reduced_numpy_sharing_completesalso now runs withdo_newton=True(newt_start=3), closing the numpy leg of the rank-reduction x sharing x Newton three-way as well. It previously kept Newton off behind a comment describing a shape crash on that exact combination -- that crash was issue #267 itself (a share pass collapsing components on rank-reduced data was the scenario the issue was filed from), fixed in PR #271 (dgm[None, :], matching the torch backend'sdgm.unsqueeze(0)) and already regression-tested up to 3 models intests/test_numpy_newton_multimodel.py. With the fix in place the fit completes and converges with finite parameters: a share pass collapses 7 components on the rank-16 sphere (comp_used25 of 32), well inside the merges-fired invariant the test already checked.#277 -- three-way: sharing x Newton x pdf family
The three features (
share_comps=True,do_newton=True, a non-defaultpdftype) had only pairwise coverage: sharing x Newton, Newton x families, and sharing x switcher each live in separate tests.MLX:
test_sharing_newton_and_fixed_family_fit_completes(pdftype=2, n_mix=3) andtest_sharing_newton_and_adaptive_switcher_fit_completes(pdftype=1, n_mix=1) added totests/mlx_tests/test_mlx_pdf.py.torch: the equivalent pair added to
tests/torch_tests/test_ng_pdf_families.py, per the issue's note that torch also has only pairwise coverage.All four assert: finite params, non-degenerate stop, merges fired (
comp_usedsum <n_comps), Newton fallbacks permitted (not asserted to zero).Safe-zone config, not the pairwise tests' loose cutoff. All four three-way tests run at
comp_thresh=0.99/seed=3, not thecomp_thresh=0.9/seed=42the first version used..context/issue-264/newton_findings.md's "Non-criterion observation: an aggressiveshare_compscutoff" section measurescomp_thresh=0.9on a 2-model fit merging 26-31 of 32 cross-model pairs -- near-total model collapse -- and documents that regime as trajectory chaos: Newton can drive a component to zero curvature there, seed- and config-dependently (seed 42 specifically producesnan_paramson MLX at a nearby configuration; a fresh review reproduced the original config completing, but at full 32/32 collapse, inside the chaos zone rather than the two-near-collinear-pairs case the doc calls safe).comp_thresh=0.99is the doc's verified stable zone, andseed=3is the doc-verified stable seed the siblingtest_low_rank_projected_data_share_fit_completesalready uses. Verified locally before committing: at this config 11 of 32 pairs merge for the fixed-family variant and 8 of 32 for the adaptive switcher on both backends -- comfortably short of the chaos threshold, so thecomp_usedguard stays non-vacuous without courting the documented degeneracy.Machine robustness
Every new test asserts behavioral invariants only -- finite, non-degenerate stop, shapes, merges-fired with a forgiving threshold -- never exact trajectories or machine-dependent decrease timing, per the epic's Phase 3 CI lesson (
.context/issue-264/newton_findings.md). The three-way tests'comp_thresh/seedchoice above is part of the same discipline: avoiding a documented chaos regime, not just avoiding hardcoded timing.Also cleaned up while touching this pattern:
test_ng_backend.py's two hardcoded("nan_ll", "singular_ll")degenerate-stop tuples (the one this PR added and one pre-existing) now referenceAMICATorchNG._DEGENERATE_STOP_REASONSinstead of duplicating the literal.Gates
uv run pytest pamica/tests/mlx_tests/ pamica/tests/test_mlx_newton_cross_backend.py pamica/tests/torch_tests/test_ng_backend.py pamica/tests/test_numpy_newton_multimodel.py pamica/tests/torch_tests/test_ng_rank_deficient.py pamica/tests/torch_tests/test_ng_pdf_families.py pamica/tests/test_numpy_share_comps.py -q --no-cov: 199 passed, 5 skipped.uv run pytest -m "not slow" -n auto: 529 passed, 15 skipped, 1 xfailed.ruff check/ruff format --check/ty check(whole project): zero diagnostics.Closes #272
Closes #273
Closes #277