Skip to content

block_size=512 default costs up to 32x on Apple GPUs and 9x on CPU; backend comparisons are uncontrolled #216

Closed

Description

@neuromechanist

Measured on an Apple M4 Pro (14 cores), bundled 32-channel sample (30504 frames), float32, n_mix=3, pdftype=0, seed 42, warm, per-iteration cost in ms:

block_size PyTorch-MPS MLX PyTorch-CPU
512 (shipped default) 431.3 29.5 144.0
2048 89.7 12.4 49.3
8192 30.5 11.3 21.7
30504 (single block) 13.5 11.4 15.8

All three backends are dispatch/overhead-bound at the default. MPS improves 32x, CPU 9x, MLX 2.6x.

Log-likelihood after 40 iterations is unchanged across every block size and both devices (-3.43856 to six significant digits), so this is a pure throughput knob on this data, not a correctness one. The comparison across block sizes is therefore valid.

Why this matters beyond performance

Several published claims compare backends at a fixed block_size=512, while the backends' sensitivity to that parameter differs by more than an order of magnitude. That makes the comparison uncontrolled in the same way #209's precision comparison was:

  • paper.md states MLX runs at ~25 ms/iteration against ~255 for PyTorch-Metal, "which never beats the CPU it runs beside". At a tuned block size MPS reaches 13.5 ms/iteration and does beat CPU (15.8).
  • paper.md's Software design section justifies the existence of the MLX backend with "MLX exists because PyTorch's Metal backend is slower than the CPU on Apple hardware". True at 512, false at 30504.
  • AGENTS.md states "PyTorch-MPS never wins (162-255 ms/it, at or worse than CPU), so use MLX, not device=\"mps\", on Apple hardware."
  • A comment posted to Add AMICA (Adaptive Mixture ICA) as ICA method mne-tools/mne-python#13819 repeats the 255 ms figure and the never-beats-CPU claim.

MLX remains the fastest Apple backend, so that conclusion survives. The magnitude does not: roughly 1.2x, not 10-15x.

The default itself

block_size=512 was set in #63, raised from 128 for about -18% on CPU/CUDA. The sweep evidently did not continue upward, and was never repeated on Apple hardware. On this dataset the CPU alone leaves a 9x speedup on the table, which is directly relevant to the CPU-runtime discussion in mne-tools/mne-python#13819.

Work

  • Sweep block_size per device and data size, including CUDA on a large recording, not just this 30504-frame sample. Larger blocks cost memory proportional to samples x mixtures, so the best value is data-dependent and the default must stay safe for large inputs.
  • Decide whether the default should be device-aware rather than a single constant.
  • Re-measure the published throughput table at a tuned block size per backend, and state which block size each number used.
  • Correct the claims listed above, including AGENTS.md, and post a correction to the MNE thread.
  • Check whether the MPS result changes the "PyTorch-MPS is never a win" guidance in docs/guides/backends.md.

Not yet investigated: whether torch.compile (#211) adds anything once block size is tuned. Given the profile is dispatch overhead and larger blocks already remove most of it, compilation may have far less headroom than it appeared to.

Activity

  1. neuromechanist commented on Aug 10, 2026

    @neuromechanist
    MemberAuthor

    Decision: keep the 512 default and document the tradeoff, with a strong recommendation to use 1024 or higher where memory allows.

    Rationale, and one correction to the premise.

    Memory is the constraint, and it is worse than per-sample

    Large blocks are not reachable on resource-limited machines. Intermediates scale with block size and with the number of models and mixture components, so a multi-model fit multiplies the footprint: n_models * n_mix * block_size is the shape that matters, not block_size alone. A default that is fast on a 32-channel sample can exhaust memory on a many-model fit of a long recording. 512 stays the conservative default for that reason.

    Correction: 512 is not the Fortran default

    It is worth recording that this is a pamica choice, not a parity constraint:

    • Fortran defaults to block_size = 128 (amica15_header.f90:81).
    • Fortran also defaults to do_opt_block = .true. (amica15_header.f90:20), meaning it auto-tunes the block size at runtime.
    • pamica/sample_data/input.param sets block_size 512 and do_opt_block 0, pinning both sides so the comparison is controlled. That is a benchmark control, not evidence about defaults.
    • pamica's 512 came from Optimize AMICATorchNG per-iteration cost (pow hot spot + CPU threading) #63's own CPU/CUDA sweep, raised from 128 for about -18%.

    Block size also does not affect the result: log-likelihood after 40 iterations is identical to six significant digits across 512 / 2048 / 8192 / 30504 on both CPU and MPS. So nothing about parity depends on this value.

    Work

    • Document the tradeoff in docs/guides/backends.md: what block size costs in memory (including the n_models * n_mix multiplier), what it buys in throughput per backend, and a clear recommendation of 1024 or higher when memory allows.
    • State the measured numbers so the recommendation is checkable rather than asserted.
    • Correct the claims listed in the issue body that compare backends at a fixed 512, and say which block size each published number used.
    • Post a correction to Add AMICA (Adaptive Mixture ICA) as ICA method mne-tools/mne-python#13819. The CPU figure improves about 9x at a larger block size, which is directly relevant to the runtime concern raised there.

    Deferred, worth its own issue

    Fortran's do_opt_block auto-sizing is the genuinely Fortran-faithful answer here and pamica has no equivalent. That would remove the guesswork entirely rather than moving it into documentation, but it is a larger change than this issue needs.

  2. neuromechanist commented on Aug 22, 2026

    @neuromechanist
    MemberAuthor

    #229 addressed the default-value part of this issue (512 -> 8192, ~6x on CPU). Superseding the earlier keep-512-and-document proposal in this thread: the default did change in #229. Still open here: sweeping block_size across devices/data including CUDA, deciding a device-aware default (split out to #232), and correcting the block_size-confounded MPS/MLX claims in AGENTS.md, docs/guides/backends.md, and paper.md (also tracked in epic #230). Keeping open with that reduced scope.

  3. neuromechanist commented on Aug 23, 2026

    @neuromechanist
    MemberAuthor

    Closing per the 2026-08-23 decisions: the default stays a single constant block_size=8192 across backends (raised from 512 in #229; device-aware defaults considered and rejected — the memory-safety rationale in AGENTS.md stands and the docs now carry the guidance instead). The stale performance claims measured at 512 were corrected in-repo by PR #280 (AGENTS.md, backends.md, validation.md, paper.md — the paper now cites the documentation, and the full sweep table lives in docs/guides/validation.md#block-size-sensitivity). The remaining sweep-adjacent work item, the do_opt_block auto-tuner with an OOM-safe fallback, is tracked separately in #232 and is in progress. The public MNE-thread correction is drafted and with the maintainer to post.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions