Skip to content

Add MLX backend for Apple Silicon processors - #790

Open
spichardo wants to merge 3 commits into
waltsims:masterfrom
ProteusMRIgHIFU:MLX-k-Wave-binary
Open

spichardo wants to merge 3 commits into
waltsims:masterfrom
ProteusMRIgHIFU:MLX-k-Wave-binary

Conversation

@spichardo

@spichardo spichardo commented Nov 7, 2026 •

Copy link
Copy Markdown

Use the MLX GPU binary for device="gpu" / is_gpu_simulation=True on Apple Silicon

What

macOS has no CUDA. With this PR, GPU runs of the C++ backend on Apple Silicon use kspaceFirstOrder-MLX, the k-Wave
CUDA solver ported to MLX/Metal. It is released by kspacefirstorder-unified as kspaceFirstOrder-MLX-darwin plus
mlx.metallib (the PR adding it to kspacefirstorder-unified is waltsims/kspacefirstorder-unified#31).

  • kwave/__init__.py:
    • New mlx architecture, darwin only, which downloads kspaceFirstOrder-MLX-darwin (saved as
      kspaceFirstOrder-MLX) and mlx.metallib into BINARY_PATH. Intel Macs keep getting no C++ binaries, as before.
    • MLX is an optional architecture: a failed download (e.g. a pinned release without the asset) logs a warning
      instead of aborting import kwave, so the OMP backend stays usable.
  • SimulationExecutionOptions.binary_name, CppSimulation._resolve_binary_path:
    • GPU on darwin resolves to kspaceFirstOrder-MLX.
    • "MLX" is accepted as a backend value and maps to cpp/gpu in compat.
  • Messages:
    • The "GPU not supported on macOS" errors now say that the MLX binary (Apple Silicon) is needed.
    • The CUDA compute-capability warning is skipped for the MLX binary.
  • Docs: docstrings mention MLX on macOS.
  • Tests:
    • Updated the two macOS-GPU error tests.
    • Added tests for MLX selection, darwin URLs and local filenames, and the non-fatal optional download.

Before merging

BINARY_VERSION must point to the first kspacefirstorder-unified release that contains kspaceFirstOrder-MLX-darwin
and mlx.metallib. This PR keeps v1.4.2, which has neither: macOS users then just get a warning and no GPU binary.

Testing

  • pytest tests/test__init__.py tests/test_executor.py tests/test_cpp_simulation.py tests/test_checks.py tests/test_compute_capability_warning.py: 43 passed, 2 skipped (macOS).
  • Manual, on an Apple M3 Max with a locally built release binary in kwave/bin/darwin:
    • kspaceFirstOrder(..., backend="cpp", device="gpu") matches device="cpu" (v1.4.2 OMP binary) to 1e-6 to 1.2e-5
      on ivp_3D_simulation, ivp_heterogeneous_medium and tvsp_homogeneous_medium_monopole.
    • The legacy kspace_first_order_2d_gpu (at_array_as_source) runs on the MLX binary.

RetriggerView in GreptileConfidence Score: 5/5

The latest test fix appears safe to merge, with no remaining review findings.

Summary

Adds MLX binary downloads and GPU selection for Apple Silicon. The latest change fixes the macOS-specific test on Windows.

  • The macOS installer looks for the MLX binary and its Metal library.
  • C++ GPU simulations use MLX on Apple Silicon Macs.

Reviews (4) · Last reviewed commit: "Mock macOS platform in MLX binary test" · Reviewed by Greptile

Comment thread kwave/__init__.py Resolved
Comment thread kwave/__init__.py Resolved
@spichardo

Copy link
Copy Markdown
Author

@greptileai review

Ensure the MLX backend selection test runs consistently on non-macOS platforms.
@spichardo

Copy link
Copy Markdown
Author

@greptileai review

This branch has not been deployed

No deployments
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