Skip to main content

faster-diffbloch

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

Up to 3.4x faster than PyTorch on the forward-plus-backward pass, 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.47 ms 0.72 ms 1.07 ms 2.03x 1.37x
61 2.00 ms 1.33 ms 1.33 ms 1.50x 1.50x
91 3.68 ms 2.24 ms 1.67 ms 1.65x 2.21x
163 10.34 ms 5.78 ms 3.05 ms 1.79x 3.39x
579 117.44 ms 81.55 ms 39.87 ms 1.44x 2.95x

Two kernels carry this. The matrix exponential is O(N^3) and dominates at large beam counts. The structure factors dominate at small ones: at 31 beams they are about 80% of the forward pass while the exponential is 15%. Both now run natively, which is why the speedup holds across the range rather than appearing only at scale.

Accuracy is not traded for any of it. The structure factors run in float64 and agree with diffBloch to 4e-15, their gradients to 1e-14, and the systematic absences land on the same exact zeros. End to end the loss is bit-identical to diffBloch's own.

bench/pkgbench.py reproduces the table.

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 runs on the CPU and avoids the framework overhead. 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

The port is the fastest CPU route. This package on Metal is faster still, at 39.87 ms for the same case, because the port has no GPU path. 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.7

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.7
File Size Uploaded Provenance Actions
faster_diffbloch-0.1.7.tar.gz 27.9 kB Without provenance Details for faster_diffbloch-0.1.7.tar.gz

Built distribution (wheel)

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

Total release size: 60.4 kB

Release files / faster_diffbloch-0.1.7.tar.gz

Download URL faster_diffbloch-0.1.7.tar.gz
Size 27.9 kB
Tags Source
SHA-256 checksum
How to use checksums
97c4fb7d198a83c3a42efca54286002425e15c5c65418d8b788922d9a13b505c
BLAKE2b-256 checksum
How to use checksums
90d015d9fffb02861cc5c0945f7270987fcbad6bf37b31e1b0bb514b717d8aea
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.7-py3-none-any.whl

Download URL faster_diffbloch-0.1.7-py3-none-any.whl
Size 32.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7cafac9de5615dc1570db2384a038ccdce02b9a5d55d149f06dce496809fc415
BLAKE2b-256 checksum
How to use checksums
123f64463b30d55f61eb7f0d6ff7de62b68eb4bc10a2ec77e164ce6930a98a0c
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.7

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