Skip to content

Phase 17: unify the lrate default and fix raw-backend device auto-selection (#354) - #355

Merged
neuromechanist merged 11 commits into
feature/issue-324-epic-mlx-first-classfrom
feature/issue-354-phase17-defaults
Sep 24, 2026
Merged

neuromechanist merged 11 commits into
feature/issue-324-epic-mlx-first-classfrom
feature/issue-354-phase17-defaults

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

Part of #324, closes #354 on the epic merge.

Summary

Every pamica entry point now shares one set of defaults, plus two related fixes from the documentation audit (#352) and a defaults table.

  1. One lrate default across the wrapper and the backends.
    AMICA.fit restated its own fallback defaults, and its lrate (0.05, EEGLAB's runamica15.m value) differed from every backend's (0.1, the compiled amica15 default, amica15_header.f90:68).
    The wrapper now reads the defaults of max_iter, lrate, do_mean, do_sphere and do_newton from the selected backend's signatures (pamica.amica._fit_defaults, via inspect).
    Only lrate moves; AMICAICA forwards to AMICA.fit and follows. Explicit and params-file values are unaffected.
    Behavior change: on the bundled sample (PyTorch, seed 42, 100 iterations) a default wrapper fit ends at LL -3.42768 instead of -3.43566 (old vs new sources: mean Hungarian |corr| 0.983, min 0.931).
  2. AMICA_NumPy joins the shared set. numpy_impl/params.json set do_newton on and max_iter 2000, the only shared settings where it differed; now off and 100, as is the constructor's max_iter fallback (reached by the CLI).
    Behavior change for default NumPy fits longer than newt_start (20); shorter fits are byte-identical (checked at 19 iterations).
  3. AMICANative joins the shared set. native/engine.py wrote the bundled input.param's values (lrate 0.05, Newton on, max_iter 2000, block_size 512, ...).
    It now derives every setting the binary has a keyword for from AMICATorchNG's signature through pamica.fortran_params.FORTRAN_TO_PAMICA_KEY; binary-only knobs (max_threads, byte_size, writestep, load_*, update_*) are unchanged. Settings without a binary keyword (keep_best, mineig_rel, ...) and None defaults (pcadb, seed) are not written.
    • Deviation from the literal instruction, block_size: the binary's block_size is one thread's share of a block, and it processes n_samples // (max_threads * block_size) blocks (amica15.f90:1215-1227). Writing 8192 with the default max_threads 10 gives zero blocks on any recording under 81,920 samples, an all-NaN fit (issue Fortran binary silently produces all-NaN fit when block_size exceeds frames per thread #292): every default native test failed that way on the bundled sample. The default is therefore pamica's 8192-sample block, capped at the data's length, divided by max_threads (819 on the bundled sample). A given block_size is written as is.
    • The native oracles (pamica/tests/native_oracle.py, test_schedule_native_oracle.py) relied on the old defaults; they now start from the bundled input.param itself, which renders byte-identical parameter files.
  4. Raw AMICATorchNG automatic device choice. AMICATorchNG(n_channels=4) raised ValueError on every Apple Silicon Mac. The MPS/float64 fallback moved from the wrapper into the backend constructor (a logger.warning from pamica.torch_impl.core); the wrapper's copy is removed. Explicit device="mps" at float64 still raises; device=None at float32 still picks MPS. Torch-only: MLX has no device choice of this kind.
  5. Dead package data. Removed "pamica" = ["data/*"]; the wheel's file list is unchanged, it imports from a clean venv, and params.json is inside.
  6. Documentation. docs/guides/amica-differences.md gains Default settings (row 17 links it): pamica against the compiled binary and runamica15.m (sccn/amica 509c8be) with source line numbers, why pamica follows the binary and its exceptions, and how to reproduce an EEGLAB run with AMICA.from_params_file or with AMICANative fed the file's settings. API pages, guides, README and the changelog are updated; NumPy and AMICANative join the opening "Default fits differ" warning.

Tests

  • New pamica/tests/test_default_settings.py: NumPy (through a default construction and its own params.json loader), MLX (signatures) and AMICANative (its input.param built without a binary and read back through read_params_file) hold every setting they share with AMICATorchNG to its default; every writable native setting is written; the native block is pamica's block split over the threads, and a given block_size is kept.
  • test_wrapper_backends.py: the wrapper resolves each backend's defaults; a default wrapper fit is the default backend fit, bit for bit on torch and MLX; an explicit lrate wins.
  • mne_tests/test_mne_backends.py: an AMICAICA fit without lrate runs at the backend default; the stale degenerate-fit comment (nan_ll at iteration 3) now gives the measured stop (nan_params, iteration 2, 16/16 runs on both backends at either lrate).
  • torch_tests/test_amica_ng_wrapper.py: raw default construction and fit on MPS resolve to CPU with one warning; float32 auto keeps MPS; explicit MPS at float64 raises; AMICA.load with device=None uses the backend's choice. All ran and passed on the macOS 26 CI job.
  • Pins and updates: test_pamica.py::test_amica_initialization expected NumPy's old max_iter; test_native_engine.py::test_native_engine_degenerate_fit_raises_clearly got its NaN weights from the zero-block case at the old block_size 512, so it pins block_size=512 with that reason. No test relied on the wrapper's 0.05; two torch-vs-MLX docstring figures were re-measured.
  • benchmarks/, examples/, validate_implementations.py: their wrapper and NumPy fits pass sample_params.json's settings explicitly; the harness's Fortran run writes the bundled input.param, not the engine's defaults. benchmarks/reproduce_table1.py is left to Phase 15 (Re-measure parity and sweep docs for epic #324 #351).
  • The documented NumPy-vs-Fortran figures (0.981, -3.4039) come from test_sample_data_numpy_vs_fortran, which sets do_newton, newt_start and max_iter explicitly, so they do not depend on the old NumPy defaults.

Gates

  • uv run ruff check ., uv run ruff format --check .
  • uv run ty check . (also on an env without mlx/mne, as CI runs it)
  • typos, uv run mkdocs build --strict
  • Opt-in native and oracle tests with the pinned v0.3.3 binary (AMICA_RUN_FORTRAN=1, PAMICA_NATIVE_BINARY): 356 passed
  • uv run pytest -q (full suite, MLX and MNE installed): 1974 passed, 40 skipped (opt-in Fortran oracles and native-binary tests), 3 xfailed

Follow-ups found, not fixed here

  • AMICANative(threads=...) sets OMP_NUM_THREADS, but the binary calls omp_set_num_threads(max_threads) from input.param (amica15.f90:95-100), so it always runs max_threads threads (10 by default); threads=2 has no effect.
  • "Match block_size on both sides" for bit-for-bit comparisons compares different quantities: the binary's is per thread, pamica's per block.
  • An explicit block_size passed to AMICANative can still hit the Fortran binary silently produces all-NaN fit when block_size exceeds frames per thread #292 zero-block case; the engine's non-finite-weights check reports it as degenerate.

AMICA.fit restated max_iter/lrate/do_mean/do_sphere/do_newton, and its
lrate (0.05, runamica15.m's) differed from every backend's 0.1. It now
reads them from the selected backend's signatures, so AMICA and AMICAICA
fits without lrate run at 0.1. Tests for torch and MLX: resolved
defaults, a default wrapper fit equals the default backend fit, and one
set of constructor defaults across the backends.
AMICATorchNG(n_channels) with default arguments raised ValueError on
Apple Silicon: device=None picked MPS, which has no float64. The
constructor now uses the CPU for that case and logs a warning; the
wrapper and AMICA.load pass device through and drop their copy. An
explicit device='mps' at float64 still raises. Torch-only (MLX has no
device choice). Tested on MPS: raw construction and fit, float32 auto,
explicit MPS, load with device=None.
No pamica/data directory exists. Built with uv build: the wheel's file
list is unchanged, it imports from a clean venv, and
numpy_impl/params.json is inside.
Add a defaults table to the differences guide: pamica against the
compiled amica15 binary and EEGLAB's runamica15.m, why pamica follows
the binary, and how to reproduce an EEGLAB run. Update the pages that
stated the wrapper's lrate or the raw backend's MPS raise. mkdocs build
--strict passes.
The wrapper lrate change joins the 'Default fits differ from 0.3.3'
warning; entries for the backend device fallback and the package-data
cleanup.
params.json set do_newton on and max_iter 2000, the only shared settings
where NumPy differed from AMICATorchNG; it now sets them off and 100, as
does the constructor's max_iter fallback (reached by the CLI). Fits
shorter than newt_start are byte-identical. New test_default_settings.py
holds NumPy and MLX to every shared AMICATorchNG default (the MLX check
moves there from test_wrapper_backends.py).
The comment said torch stops on nan_ll at iteration 3; both backends
stop on nan_params with iteration 2 in all 16 measured runs, at either
lrate. The test asserts only a degenerate stop.
Defaults section, validation guide's stops table, NumPy API page, and a
changelog entry (NumPy joins the opening warning's defaults line).
The engine wrote the bundled input.param's values (lrate 0.05, Newton
on, max_iter 2000, block_size 512, ...). It now takes every setting the
binary has a keyword for from AMICATorchNG's signature through
FORTRAN_TO_PAMICA_KEY; binary-only knobs are unchanged. The default
block is pamica's 8192 samples split over max_threads and capped at the
data, since the binary's block_size is per thread and a block longer
than the data gives an all-NaN fit (#292). The native oracles now start
from the bundled input.param itself (byte-identical files). Tested with
the v0.3.3 binary: native, oracle and default-settings tests pass.
AMICANative joins the defaults table's pamica column; the section and
the native API page cover the settings it cannot write, the per-thread
block size, and running the binary as an input.param configures it. The
changelog records the behavior change.
@neuromechanist
neuromechanist merged commit bb22df6 into feature/issue-324-epic-mlx-first-class Sep 24, 2026
8 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-354-phase17-defaults branch September 24, 2026 05:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant