Repository navigation
Fix numpy_impl weight-gradient norm to Fortran's formula - #217
Merged
Merged
Conversation
4 tasks
3 tasks
self.nd was built from the raw dWtmp block sum: never divided by dgm[h], never +I, never premultiplied by A, never masked by comp_used. It did not use directions[h], the quantity computed two lines above that drives the actual A update. It reported ~5.4e3 on the bundled sample where Fortran reports ~5.7e-2, and sat flat rather than tracking convergence, so min_nd/use_grad_norm was unreachable in this backend. Now mirrors amica15.f90:1731-1743: dAk is the gm-weighted average of the per-model directions mapped through A, divided by zeta, masked by comp_used. Moved to before the A step and the doscaling rescale, since Fortran builds dAk in accum_updates_and_likelihood (:1731-1743) strictly before update_params applies it (:1789). Verified against the reference binary: nd at iteration 1 is 5.717e-2 against Fortran's 5.75e-2, a ratio of 0.994. Refs #212
- Correct a comment that described ndtmpsum as dividing by lrate. It does not, and neither does Fortran; the comment risked someone 'fixing' a nonexistent bug. - Record that self.gm is already updated when dAk is built, where Fortran still holds the previous iteration's value. Invisible for one model and for disjoint comp_list, differs under share_comps. Filed as #219. - Add a test that pins the computation to the pre-step mixing matrix, the one gap the previous two tests could not cover. It derives the expected norm from the step actually taken, (A_before - A_after)/lrate for a single model with rescaling off, so it needs no implementation internals. Verified to fail when A is perturbed by 0.1 percent inside the dAk computation alone. Refs #212, #219
neuromechanist
force-pushed
the
212-numpy-nd-formula
branch
from
August 10, 2026 01:34
8e8e693 to
150c2f2
Compare
ty flagged model.A as Optional. Refs #212
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.
Completes #212. The log-likelihood half landed in #215; this is the gradient-norm half.
The bug
self.ndwas built from the rawdWtmpblock sum. It was never divided bydgm[h], never hadIadded, was never premultiplied byA, and was never masked bycomp_used. It did not usedirections[h]at all, despite that being the quantity computed two lines above which drives the actualAupdate.Result: it reported about 5.4e3 on the bundled sample where Fortran reports about 5.7e-2, and it sat flat across iterations rather than tracking convergence.
min_nd/use_grad_normwas therefore unreachable in this backend, exactly asmin_dllwas before #215. Both Fortran convergence criteria were dead here, not one.The fix
Mirrors
amica15.f90:1731-1743:dAkis thegm-weighted average of the per-model directions mapped throughA, scattered bycomp_list, divided byzeta, then masked bycomp_used.It also moves the computation. Fortran builds
dAkinsideaccum_updates_and_likelihood(:1731-1743) strictly beforeupdate_paramsapplies the step (:1789), so the norm describes the step about to be taken. Ours was computed after theAupdate and after thedoscalingrescale, which measures a different quantity.comp_usedisNoneuntilfit()sets it up, and the M-step is exercised directly in tests before that happens, so the mask falls back to all-used rather than asserting. An earlier attempt that asserted non-None broketest_newton_mstep_matches_numpy_reference.Verification
Against the reference binary on the bundled sample, iteration 1: 5.717e-2 vs Fortran's 5.75e-2, ratio 0.994.
Later iterations diverge, and that is expected rather than a defect.
dAktends to zero at the fixed point, so the norm measures a near-total cancellation and small floating-point and BLAS-order differences are amplified in the residual. Both backends settle on their own noise floor while agreeing on the log-likelihood to 4-5 significant figures. The same pattern holds betweenAMICATorchNGand Fortran.Tests
pamica/tests/test_numpy_grad_norm.py, real bundled EEG, no hardcoded Fortran constants. Both assert properties the bug actually violated, and both were confirmed to fail against the pre-fix implementation (984.0 and 1449.1 against bounds of 1.0 and half the initial mean):A third test comparing
doscalingon and off was written and then removed: it passed against the pre-fix implementation too, because rescaling changesA, which changes the next iteration's sufficient statistics, so the trajectories differ either way. It would have asserted nothing. A comment records that so nobody re-adds it.Exact-value assertions against Fortran are avoided deliberately. Iteration counts for convergence quantities vary by more than 3x across BLAS implementations (measured 326 / 412 / 1076 for the same
min_dllstop across macOS-arm64, Linux-x86_64 and the CI runner in #213), so a golden value would be a snapshot of one machine.Not addressed
min_nd = 1e-7remains unreachable on this dataset for all three implementations, Fortran included: Fortran's own trajectory oscillates around 1e-5 out to iteration 5073 without stopping. That is a property of 30504 samples against 1024+ free parameters, and Fortran ships the same default. It should be documented, not tuned, and is tracked separately.