Skip to content

Evaluate torch.compile for AMICATorchNG #211

Closed

Description

@neuromechanist

Suggested by @DerAndereJohannes in #206, who reports significant speedups from compiled graphs in his own PyTorch AMICA port, particularly on longer runs.

Never evaluated here: grep -rn "torch.compile\|dynamo" over the codebase returns nothing.

Why it is worth doing now

CPU throughput is the live objection to Python AMICA implementations in the MNE-Python adoption thread (mne-tools/mne-python#13819), where maintainers have specifically asked for CPU numbers rather than GPU ones. torch.compile is the cheapest lever we have not pulled, and the E-step is a long chain of small elementwise ops over a fixed-shape block, which is the shape of workload it tends to help most.

Scope

  • Benchmark torch.compile on the AMICATorchNG E-step and M-step across CPU, CUDA, and float32/float64, on real EEG via benchmarks/benchmark_dimsweep.py.
  • Measure compile overhead against total fit time; AMICA runs thousands of iterations, so a one-off graph compile should amortize, but confirm rather than assume.
  • Check for recompilation triggers. block_size is fixed at 512 by default, but the final block of a recording is ragged, and outlier rejection (do_reject) changes the sample count mid-fit.
  • Verify parity is unaffected: the compiled path must reproduce the same log-likelihood and components as the eager path, checked against the Fortran binary through validate_implementations.py, not just against eager torch.

Constraint

If compilation perturbs numerics at all, it is opt-in, not default. Parity with the Fortran reference is the definition of correctness here, and a speedup that moves the numbers is not a speedup we can take by default.

Activity

  1. neuromechanist commented on Aug 15, 2026

    @neuromechanist
    MemberAuthor

    Evaluated. Recommendation: do not adopt. torch.compile does not pay for itself here, and it perturbs numerics, which under this project's parity rule rules it out as a default regardless of speed.

    Measurements

    Real bundled EEG (32 ch x 30504), CPU, 4 intra-op threads, torch 2.12.1, 30 iterations, seed 0.

    config cold 1st iter ms/iter (warm) final LL
    f64, block=512, eager 0.18 s 116.3 -3.440665051044
    f64, block=512, compiled 15.66 s 112.2 -3.440665087513
    f64, block=8192, eager 0.07 s 43.1 -3.440665333144
    f64, block=8192, compiled 1.26 s 62.6 -3.440666225402
    f32, block=512, eager 0.14 s 95.1 -3.440663099289
    f32, block=512, compiled 1.13 s 75.9 -3.440660238266
    f32, block=8192, eager 0.05 s 30.7 -3.440666198730
    f32, block=8192, compiled 1.09 s 31.3 -3.440663099289

    _forward (the per-block E-step) was the compile target, with dynamic=False. The warm column runs with a hot dynamo cache; cold compile cost is the first column.

    What it says

    The win is inconsistent and small where it exists. The best case is f32/block=512 at -20%. At block=8192 in float64 compiled is 45% slower than eager. There is no configuration where compilation produces a large gain.

    Compile cost is not always amortized. The f64/block=512 cold compile is 15.7 s against a 4 ms/iteration saving: ~3,900 iterations to break even, more than a default 2000-iteration run. The other configurations compile in ~1 s and amortize quickly, but they are also the configurations where the speedup is near zero.

    It changes the numbers. Same seed, same configuration, eager vs compiled differ in the log-likelihood at the 8th decimal (f64/block=512: -3.440665051044 vs -3.440665087513). Fusion reassociates floating-point operations, so this is expected rather than a bug — but per the constraint in this issue, a speedup that moves the numbers cannot be the default, and these speedups are far too small to justify an opt-in path plus its parity caveat.

    The lever that actually matters is block_size, not compilation. Eager f64 goes 116.3 -> 43.1 ms/iteration (2.7x) purely by moving block_size from 512 to 8192 — an order of magnitude more than anything compilation offered, at zero numerical or maintenance cost. That is #216, and it is where the CPU-throughput answer for mne-tools/mne-python#13819 should come from.

    Not covered

    • CUDA. No NVIDIA hardware on the machine used. Given that Epic: Cross-platform native-Fortran + CPU-scaling + CUDA benchmark #84 found CUDA overhead-bound at this problem size (f32 == f64, ~36 ms), kernel fusion is unlikely to change the picture, but that is inference, not measurement.
    • mode="max-autotune" and torch.compile over the M-step as well. Both plausible, neither likely to close a gap this shape when the default mode is already slower than eager in half the configurations.

    Reopen with a CUDA box if someone wants the NVIDIA half of the table. Closing as evaluated; the CPU throughput work continues in #216.

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