Skip to content

Run on Apple Silicon (MPS) - #1

Closed
lukaspoloki wants to merge 1 commit into
rkinas:mainfrom
lukaspoloki:macos-mps
Closed

lukaspoloki wants to merge 1 commit into
rkinas:mainfrom
lukaspoloki:macos-mps

Conversation

@lukaspoloki

@lukaspoloki lukaspoloki commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Adds an mps serving mode for M-series Macs. It keeps the shared-prefix packing and token-budget batching of fast, but runs plain forward passes instead of CUDA graphs. The default device is now cuda → mps → cpu, and basal-serve falls back to mps when there is no CUDA. basal-bench also works without CUDA.

Measured on an M3 Pro (18 GB) with the bundled examples, both option orders: 1.5B takes 224 ms per decision (4.0/s, matches fp32 on 43 of 44); 4.5B takes 660 ms (1.5/s, 10.1 GB). That's 1.35–1.4× faster than eager.

New test: the mps path equals separate forward passes, on CPU and MPS. The CUDA code paths are unchanged, but I had no NVIDIA GPU to re-run them on.

Summary by Sourcery

Add Apple Silicon support with an automatically selected MPS backend and CUDA-independent benchmarking.

New Features:

  • Add an MPS serving mode for Apple Silicon with shared-prefix packing and token-budget batching.
  • Select CUDA, MPS, or CPU automatically and expose explicit device selection for supported modes.

Bug Fixes:

  • Allow basal-bench to run and report timings and memory without CUDA.

Enhancements:

  • Add MPS-aware synchronization, memory measurement, and benchmarking support.
  • Preserve eager inference as a reference path across CUDA, MPS, and CPU.

Documentation:

  • Document Apple Silicon installation, serving, supported modes, memory requirements, and benchmark results.

Tests:

  • Verify shared-backend outputs match separate forward passes on CPU and available MPS devices.

@sourcery-ai

sourcery-ai Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

This PR adds an Apple Silicon/MPS serving path based on shared-prefix packing and token-budget batching with ordinary forward passes, introduces device-aware serving and benchmarking with CUDA→MPS→CPU fallback, documents MPS usage and constraints, and verifies numerical equivalence on CPU and MPS.

Sequence diagram for MPS shared-prefix inference

sequenceDiagram
    participant Client
    participant Server
    participant SharedBackend
    participant MPS
    Client->>Server: Request with option orders
    Server->>SharedBackend: run_shared(groups)
    SharedBackend->>SharedBackend: _pack(prompts)
    SharedBackend->>SharedBackend: _chunks(token budget)
    SharedBackend->>MPS: _forward_masked(ids, mask, pos)
    MPS-->>SharedBackend: logits for packed rows
    SharedBackend->>SharedBackend: _readout(option logits)
    SharedBackend-->>Server: probabilities
    Server-->>Client: Decision response
Loading

File-Level Changes

Change Details Files
Adds a shared-prefix, token-budget batching backend that runs ordinary PyTorch forwards without CUDA graphs or compilation.
  • Introduces SharedBackend with packed option orders, length bucketing, right padding, and MPS/CPU device support.
  • Preserves the existing CUDA graph backends while reusing shared packing logic for the new path.
  • Adds MPS warm-up forwards and device synchronization for asynchronous execution.
basal/engine.py
Makes serving select an appropriate backend and device automatically across CUDA, MPS, and CPU.
  • Adds mps mode and routes it to SharedBackend.
  • Changes the default mode to fast on CUDA and mps otherwise.
  • Adds explicit device selection for eager and shared modes.
basal/server.py
Extends benchmarking to measure non-CUDA devices and MPS-specific modes.
  • Adds shared and mps-* benchmark backend construction.
  • Replaces CUDA-only synchronization and memory handling with device-aware helpers.
  • Reports device names, MPS driver memory, and timings for CUDA, MPS, and CPU.
basal/bench.py
Documents Apple Silicon installation, supported modes, resource requirements, and measured performance.
  • Adds MPS setup and serving examples using standard PyPI PyTorch.
  • Explains mode limitations, shared-memory requirements, and CUDA-only features.
  • Updates the mode capability table for MPS and CPU support.
README.md
Adds equivalence coverage for the shared backend on CPU and available MPS hardware.
  • Tests packed and padded batches against separate model forward passes.
  • Skips the MPS case when the Metal backend is unavailable.
tests/test_engine.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="basal/bench.py" line_range="157" />
<code_context>
     ap.add_argument("--model", default="Remek/basal-1.0-4.5B")
     ap.add_argument("--vllm-model", dest="vllm_model", default=None, help="checkpoint for --modes vllm (e.g. the NVFP4 repo)")
     ap.add_argument("--modes", nargs="+", default=["eager-fp32", "fast"],
-                    help="eager-fp32, eager, fast, fast-nocompile, fp8, nvfp4, vllm, fast-exit@off|0.999|0.995|0.99|0.98")
+                    help="eager-fp32, eager, fast, fast-nocompile, fp8, nvfp4, vllm, fast-exit@off|0.999|0.995|0.99|0.98; "
</code_context>
<issue_to_address>
**issue (broader_impact):** `basal-bench` still defaults to `['eager-fp32', 'fast']`, so running it without an explicit `--modes` on a Mac or CPU constructs `GraphBackend` for `fast`; `GraphBackend.__init__` unconditionally calls `.cuda()` and CUDA graph APIs, causing the benchmark to fail before measuring anything.

**Triggers:** When `basal-bench` is run without `--modes` on a machine without CUDA.

**Suggested fix:** Select `mps` instead of `fast` in the benchmark's default mode list when `default_device()` is not CUDA, or translate the default `fast` mode to `mps` on such devices.

```suggestion
    ap.add_argument("--modes", nargs="+", default=["eager-fp32", "fast" if default_device() == "cuda" else "mps"],
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread basal/bench.py
@@ -120,7 +155,8 @@ def main():
ap.add_argument("--model", default="Remek/basal-1.0-4.5B")
ap.add_argument("--vllm-model", dest="vllm_model", default=None, help="checkpoint for --modes vllm (e.g. the NVFP4 repo)")
ap.add_argument("--modes", nargs="+", default=["eager-fp32", "fast"],

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (broader_impact): basal-bench still defaults to ['eager-fp32', 'fast'], so running it without an explicit --modes on a Mac or CPU constructs GraphBackend for fast; GraphBackend.__init__ unconditionally calls .cuda() and CUDA graph APIs, causing the benchmark to fail before measuring anything.

Triggers: When basal-bench is run without --modes on a machine without CUDA.

Suggested fix: Select mps instead of fast in the benchmark's default mode list when default_device() is not CUDA, or translate the default fast mode to mps on such devices.

Suggested change
ap.add_argument("--modes", nargs="+", default=["eager-fp32", "fast"],
ap.add_argument("--modes", nargs="+", default=["eager-fp32", "fast" if default_device() == "cuda" else "mps"],

@lukaspoloki lukaspoloki mentioned this pull request Sep 29, 2026
rkinas pushed a commit that referenced this pull request Sep 30, 2026
@rkinas

rkinas commented Oct 14, 2026

Copy link
Copy Markdown
Owner

Thanks a lot for this, and sorry for the slow reply. Apple Silicon support landed through #4 (MLX and MPS) in 1.5.0 and is in v2.0.0: --mode mps with shared-prefix packing, --device cuda|mps|cpu with the cuda → mps → cpu fallback, and basal-bench without CUDA. Since this overlaps that work and now conflicts with the 2.0 engine, I'm closing it. Thank you for the contribution.

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.

2 participants