Skip to content

Use native MPS SVD when available; document MPS usage in README - #261

Open
fnachon wants to merge 9 commits into
HannesStark:mainfrom
fnachon:feat/native-mps-svd-when-available
Open

Use native MPS SVD when available; document MPS usage in README#261
fnachon wants to merge 9 commits into
HannesStark:mainfrom
fnachon:feat/native-mps-svd-when-available

Conversation

@fnachon

@fnachon fnachon commented Jul 10, 2026

Copy link
Copy Markdown

Follow-up to #145.

weighted_rigid_align unconditionally round-trips the covariance matrix through CPU for torch.linalg.svd on MPS. That's necessary on stable PyTorch, which silently does the same round-trip internally (with a UserWarning) since it has no native MPS kernel for this op. PyTorch nightlies after the 2.8 branch cut do have a native kernel.

Adds a cached runtime check (torch.backends.mps.is_available() + a canary SVD call with warnings escalated to exceptions so the fallback is detectable) and skips the manual round-trip whenever the installed PyTorch already runs it natively — saving a GPU↔CPU sync on every diffusion step for anyone on nightly, no behavior change on stable.

Verified against both:

  • stable torch 2.13.0 — correctly detected as no native support, uses the existing CPU round-trip.
  • nightly torch 2.14.0.dev20260710 — detected as native, runs directly on mps:0, output matches the CPU path to float32 precision (max abs diff ~9.5e-7).

Also adds a README section on actually running BoltzGen on MPS — every step config hardcodes trainer.accelerator: gpu and there was no documented way to override it per step, nor any mention of the nightly-torch performance tip.

fnachon and others added 9 commits January 10, 2026 15:54
Changes made to run without errors on the Mac MPS device: torch.autocast, number of devices and workers to use on M1-5 chips, workaround for CUDA-specific code, handling of float64 incompatibilities for MPS.
Replace hardcoded torch.autocast("cuda") with device-agnostic
device_type=tensor.device.type in confidence_utils, inverse_fold,
and writer modules introduced in the upstream merge.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Python pickle does not preserve RDKit atom-level SetProp values. When
PyTorch DataLoader spawns worker processes (default num_workers=1 on
macOS), self.canonicals is pickled and all atom 'name' properties are
lost, causing KeyError in process_atom_features.

Fix: load all required molecules directly from the moldir zip inside
each get_sample() / get_feat() call instead of using the pickled
self.canonicals. The moldir zip handle is cached per-process by
_get_zipfile(), so there is no repeated I/O overhead.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ders

- Disable pin_memory on MPS (unsupported, causes UserWarning)
- Enable persistent_workers when num_workers > 0 (avoids repeated
  worker init overhead and the PL suggestion warning)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
torch.linalg.svd on MPS silently round-trips through CPU on stable
PyTorch (with a UserWarning), and boltzgen's weighted_rigid_align
already did that round-trip manually and unconditionally. PyTorch
nightlies after the 2.8 branch cut ship a native MPS kernel instead.
Add a cached runtime capability check (torch.backends.mps.is_available()
plus a canary SVD call with warnings escalated to exceptions) so the
manual CPU round-trip is skipped whenever the installed PyTorch
already supports it natively, saving a GPU<->CPU sync every diffusion
step.

Verified against both an installed stable release (torch 2.13.0,
correctly falls back) and a nightly build (torch 2.14.0.dev20260710,
runs natively on-device, matches CPU output to float32 precision).

Also documents how to actually run BoltzGen on MPS in the README —
every step config hardcodes trainer.accelerator: gpu, and there was
no existing guidance on overriding it per step, or on the nightly
torch tip for this SVD path.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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