Skip to main content

faster-diffbloch

Drop-in Apple Silicon Metal GPU and optimized CPU acceleration for diffBloch electron crystallography structure refinement.

2.59x faster than PyTorch on the forward-plus-backward pass at 579 beams, through the Metal path on Apple Silicon. An optimized CPU path covers macOS and Linux.

Package Documentation Source repository Original project
PyPI · piwheels diffFlow documentation and benchmarks github.com/godofecht/diffFlow diffbloch.com

Operating System and Platform Support

Operating System / Hardware CPU Acceleration (device="cpu") GPU Acceleration (device="gpu") Backend Runtime
macOS Apple Silicon (M1/M2/M3/M4/Max/Ultra) Supported Supported Native Metal Compute Shaders + Apple Accelerate BLAS
macOS Intel (x86_64) Supported Fallback to CPU Apple Accelerate BLAS
Linux (x86_64 / aarch64) Supported Fallback to CPU OpenBLAS / C11 BLAS
Windows PyTorch Reference PyTorch Reference Pure PyTorch Reference Fallback

Runtime platform guards automatically detect your operating system and hardware configuration. When device="gpu" is requested on Linux, faster-diffbloch automatically selects the optimized CPU backend with an informative warning.


Why faster-diffBloch?

faster-diffbloch keeps the diffBloch workflow and public API intact while moving the expensive propagation and gradient work onto an accelerated native backend. You get the same scientific calculation, with a faster path on supported hardware and a safe PyTorch fallback when the native backend is unavailable.

The package is validated against diffBloch's own test suite. With acceleration enabled it passes all 738 diffBloch tests and 55 conformance tests that compare the accelerated results against diffBloch's, field by field, including gradients. The diffBloch run makes 4855 calls into the native matrix exponential, so the accelerated path is exercised rather than skipped.

It also reproduces the published quartz result. Running diffbloch-fast infer over the 99-rotation Colmey et al. 2026 quartz dataset through the Metal path scores every rotation and gives a mean R_obs of 0.0485, against the 0.0486 the example documents.


Performance

Forward-plus-backward timing on an Apple M4 Max, single-threaded PyTorch, over the same diffBloch quartz workload at five beam counts. Both device paths, best of three runs each.

Beams PyTorch CPU path Metal path CPU Metal
31 1.52 ms 1.42 ms 1.95 ms 1.07x 0.78x
61 2.11 ms 2.07 ms 2.20 ms 1.02x 0.96x
91 3.87 ms 3.61 ms 3.70 ms 1.07x 1.05x
163 10.46 ms 8.60 ms 6.27 ms 1.22x 1.67x
579 123.30 ms 89.17 ms 47.63 ms 1.38x 2.59x

The speedup grows with beam count, and this is the shape of the result rather than noise. The package replaces the matrix exponential, which costs O(N^3), while structure factors and the loss stay in PyTorch. As the system grows the exponential takes a larger share of the runtime, so there is more for the accelerated path to reach.

That has two practical consequences. At 579 beams, CsPbBr3 scale, Metal runs the forward-plus-backward pass in 47.63 ms against PyTorch's 123.30 ms. Below about 91 beams Metal is slower than PyTorch, because the dispatch cost outweighs a small exponential, so enable(device="cpu") is the better choice for small systems.

Forward pass alone at 579 beams: PyTorch 25.90 ms, Metal 15.58 ms, a factor of 1.66. PyTorch MPS has no native kernel for aten::linalg_matrix_exp and falls back to the CPU with host transfers, which is the gap this closes.

The standalone Flow port

The diffFlow repository also holds a standalone port of the whole calculation, compiled from Flow rather than bridged into PyTorch. It avoids the framework overhead and goes further. At 579 beams, forward plus backward, minimum of five runs:

Implementation Forward + Backward
PyTorch 117.21 ms
Mojo port 85.52 ms
Flow port, C backend 82.84 ms
Flow port, MLIR backend 79.74 ms

Running the port directly is how those are reached. See the diffFlow documentation.

These figures come from one workload on one machine. Other crystals, hardware configurations and beam counts will differ.


Installation

pip install faster-diffbloch

Usage

1. Drop-in CLI

Use diffbloch-fast or faster-diffbloch anywhere you would use diffbloch:

diffbloch-fast infer examples/Colmey_et_al_2026/data/quartz-no-abs
diffbloch-fast refine examples/Colmey_et_al_2026/data/quartz-no-abs

2. Python API Injection

Enable acceleration inside any existing diffBloch script:

import faster_diffbloch

# Enable Metal GPU acceleration (macOS Apple Silicon)
faster_diffbloch.enable(device="gpu")

# Or CPU acceleration (macOS and Linux)
faster_diffbloch.enable(device="cpu")

# Run standard diffBloch code
import diffBloch
# All propagate and matrix_exp calls now route through faster-diffBloch

Metadata

Release files for faster-diffbloch 0.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for faster-diffbloch 0.1.6
File Size Uploaded Provenance Actions
faster_diffbloch-0.1.6.tar.gz 24.9 kB Without provenance Details for faster_diffbloch-0.1.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for faster-diffbloch 0.1.6
File Interpreter ABI Platform Actions
faster_diffbloch-0.1.6-py3-none-any.whl Python 3 none any Details for faster_diffbloch-0.1.6-py3-none-any.whl

Total release size: 53.9 kB

Release files / faster_diffbloch-0.1.6.tar.gz

Download URL faster_diffbloch-0.1.6.tar.gz
Size 24.9 kB
Tags Source
SHA-256 checksum
How to use checksums
af195ce4655affcdcc7559a9977e021308d51234cef427a925a240b9e8786528
BLAKE2b-256 checksum
How to use checksums
240415286542bb860b8d7af49188fdfa9a962d12af89008235175240fb03419c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / faster_diffbloch-0.1.6-py3-none-any.whl

Download URL faster_diffbloch-0.1.6-py3-none-any.whl
Size 29.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7adb8eb3f0f76cd2886834240200796efb01574938cd7bf72652dc01b7035c29
BLAKE2b-256 checksum
How to use checksums
0e3841d8d02ec8358dcaa88dd5ae47d9fcc7a33d4a7c795d57e73f905284fd94
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for faster-diffbloch 0.1.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page