Skip to content

Epic: PyTorch backend parity with Fortran AMICA (natural-gradient EM rewrite) #9

Closed
6 of 6 issues completed

Description

@neuromechanist

Goal

Bring the PyTorch backend to numerical parity with the Fortran reference (amica15mac), and fix the memory/scaling problems that come from the current Adam+autograd framing.

Root cause

The default torch backend reframes AMICA as "minimize NLL with Adam over reparameterized tensors," but AMICA is a fixed-point EM / natural-gradient method with closed-form sufficient-statistic updates. Adam on the reparameterized surface follows a different trajectory and converges to a different fixed point. The NumPy pyAMICA.py is a faithful port and is the spec.

See design review: .context/decisions/0001-torch-backend-natural-gradient-em.md and the "2026-07-02 design review" section of .context/research.md.

Concrete defects

  • Swapped mixture factorization (amica_torch.py:176-233): sources summed before per-source mixture reduction.
  • Per-source alpha collapsed to a scalar mean (amica_torch.py:187).
  • LL normalized by n_samples instead of n_samples * nw (amica_torch.py:205).
  • Newton double-steps and NaNs at newt_start.
  • pinv(A.T).T per forward; float32 default; Python loops in the hot path; no sample blocking.

Phases

  1. Validation harness & test repair (foundation gate)
  2. Isolated correctness fixes (3 math bugs)
  3. Natural-gradient EM rewrite (new module, swap at parity)
  4. Newton + adaptive PDF + outlier rejection

Definition of done

Component correlation vs Fortran > 0.95 on the sample EEG data; per-channel LL matching Fortran within tolerance; validation harness green as the parity gate.

Sub-issues

Sub-issues

6 of 6 Issues completed
Loading 6 sub-issues

Activity

  1. added
    feature
    epic
    on Jul 2, 2026
  2. neuromechanist commented on Jul 2, 2026

    @neuromechanist
    MemberAuthor

    Phase 3 merged (PR #20). Natural-gradient EM backend (AMICATorchNG) landed opt-in via backend="ng"; default unchanged. Sufficient statistics proven to match the NumPy reference to 1e-14; end-to-end correlation 0.69 (up from 0.43). The >0.95 target is carried into Phase 4 (Newton) as a strict xfail. Phases 1-3 complete; Phase 4 (#13) is next.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    epicfeature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions