Skip to content

Phase 3: Route runica matmuls through scipy BLAS under Emscripten #376

Open

Description

@neuromechanist

Phase 3 of #324.
Base: epic/324-pyodide-browser.
Depends on: phase 2, which decides whether this phase is worth its full size.
Parallel with: phases 4 and 6.

Why

Pyodide builds numpy without an optimized BLAS and builds scipy against OpenBLAS.
Matmul-bound code therefore runs materially faster through scipy.linalg.blas than through numpy @ under Emscripten.
runica is matmul-bound in its inner loop, and it is the ICA path we keep for EEGLAB parity.

Grounding, verified against the current tree

runica.py:33 already defines a _matmul helper, but it exists for a different reason: it wraps @ in np.errstate(...) to keep Accelerate from surfacing floating-point warnings.
It is also bypassed exactly where it would matter most.
The hot loops use a bare @:

684, 692, 718, 721, 871, 882, 1014, 1021, 1042, 1044, 1175, 1184.

The _matmul call sites at 509, 525, 549, 558, 771, 821, 920, 967, 1081, 1128, 1211, 1252, 1306, 1308, 1311, 1313, 1326, 1336 are mostly setup, convergence scalars, and teardown, which are not the bottleneck.

So this phase is as much about making the existing helper cover the hot path as it is about adding BLAS routing.
Keep the existing errstate suppression when you extend it; it was added deliberately.

Scope boundary, important

eeg_picard is not in scope for routing, and the epic's phrasing that one helper would be "used by runica and eeg_picard" does not survive contact with the code.
picard's hot matmuls are np.dot calls inside the third-party package (picard/_core_picard.py, picard/_tools.py, picard/solver.py).
eeg_picard.py itself has two cheap post-processing matmuls, at :63 and :65, whose cost is negligible.
Routing those two changes nothing measurable.

Decide, using phase 2's benchmark, between:

  • accept numpy's fallback for picard under Pyodide and document it, or
  • open an upstream issue or pull request against python-picard.

Do not monkeypatch numpy.dot, and do not vendor picard.
Both are the kind of compatibility hack AGENTS.md rules out.

Scope

  • One small helper module for matmul selection, resolving the backend once at import, not per call.
    Under Emscripten use scipy.linalg.blas; everywhere else use @.
    sys.platform == "emscripten" is the discriminator.
  • Use it consistently in runica's hot loops.
  • Preserve the existing floating-point warning suppression.
  • Native numerics must not change at all.

Non-goals

No change to picard's inner loop.
No new configuration surface or environment variable for backend selection; resolve it from the platform.
No broad reformatting of runica.py.

Acceptance criteria

  • Backend resolved once at import; no per-call branching in the inner loop.
  • Every hot-loop matmul listed above goes through the helper.
  • Existing np.errstate suppression preserved.
  • Native results bit-for-bit unchanged: existing tests/test_runica.py and tests/test_runica_optimization.py pass unmodified, and the MATLAB parity test still passes where MATLAB is available.
  • A test asserts the two backends agree to numerical tolerance on the same input, without relaxing any existing tolerance.
  • Pyodide speedup measured and reported against phase 2's baseline. If the measured gain does not justify the change, say so and reduce this phase to a documented finding.
  • The picard limitation is documented in docs/source/, with whichever follow-up was chosen.
  • Pre-commit, ruff, ruff format, ty clean.

/goal prompt

/goal Make runica matmul-bound work route through an optimized BLAS under Emscripten, with zero change to native numerics.

Source of truth: src/eegprep/functions/sigprocfunc/runica.py, the hot-loop line numbers listed on this issue, and phase 2's committed benchmark. Read AGENTS.md first, especially the rules on resolving environment-dependent defaults once, avoiding compatibility hacks, and making the smallest change that solves the request.

Constraints: do not touch picard's inner loop, do not monkeypatch numpy, do not vendor python-picard, do not add an environment variable to select the backend, and do not reformat unrelated parts of runica.py.

Finish line: hot-loop matmuls go through a single helper whose backend is resolved once at import; native runica tests and MATLAB parity pass unmodified with no tolerance changes; a cross-backend agreement test exists; the Pyodide speedup is measured against phase 2's baseline and reported on the issue; and the picard constraint is documented with a chosen follow-up.

If the benchmark says the gain is not worth the change, reduce the phase to a documented finding and say so rather than shipping a helper for its own sake. Report any review finding you chose not to fix, with the reason.

Activity

  1. added
    phase
    on Sep 17, 2026
  2. neuromechanist commented on Sep 17, 2026

    @neuromechanist
    MemberAuthor

    🤖 Phase 2 live CI benchmark completed in run 35245625332. Phase 3 was evaluated but is not being implemented from this gate.

    Pyodide scipy BLAS speedups over NumPy @ were: activation float64 1.76x, activation float32 0.81x, weight update float64 2.20x, and weight update float32 1.62x. The Phase 3 gate requires at least 1.2x for every tested shape and dtype, so the float32 activation case fails the pre-registered gate.

    Decision: reduce this phase to a documented/deferred finding for now. Do not add a helper solely for the passing cases. The Phase 2 report also confirms that one runica or Picard solve is single-threaded; parallelism remains an independent-job/Web Worker concern. Picard inner-loop routing remains out of scope.

  3. neuromechanist commented on Sep 17, 2026

    @neuromechanist
    MemberAuthor

    🤖 Follow-up disposition after reviewing the Phase 2 result: runica coerces its input to float64 at src/eegprep/functions/sigprocfunc/runica.py:230, so the float64 BLAS microbenchmarks are the runtime-relevant cases and passed (1.76x for activation, 2.20x for weight update).

    The original pre-registered Phase 3 gate still failed because it required at least 1.2x for every tested shape and dtype, and float32 activation measured 0.81x. That failure is not being silently reinterpreted as a pass.

    Decision: keep the implementation as a valid but lower-priority follow-up. A future implementation should be re-scoped around an end-to-end float64 runica measurement, hot-loop numerical parity, and the actual total runtime benefit; it should not add a helper solely for the passing microbenchmarks or relax the recorded gate.

    Priority is lower while browser ICLabel work proceeds. For accuracy, current pop_runica defaults to runica; Picard is available as an option, but Phase 2 did not retain Picard as the browser default. Picard inner-loop routing remains out of scope for this issue.

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

    phase

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions