Skip to main content

TorchRef

A PyTorch-based crystallographic Refinement library

Tests Python 3.10+ PyTorch License: MIT Documentation Forum CUDA Apple Silicon MPS

TorchRef is a crystallographic refinement package built entirely on PyTorch. Autograd and GPU acceleration make it composable with machine-learning workflows and cheap to extend with new targets.

Scope. TorchRef is mainly a library/framework to build and experiment with. It is not intended to replace mainline refinement programs for standard problems.

Benchmark

TorchRef AlphaFold-start refinement benchmark

Refinement of Phaser-placed AlphaFold models against experimental data, on a conserved set of 723 PDB structures (1.40–3.00 Å). All engines start from the same placed models and are scored by one common validator (PHENIX).

(A) R-work and R-free per engine. Median R-free 0.3189 (TorchRef), 0.3164 (PHENIX), 0.3136 (REFMAC5); paired median difference, TorchRef minus reference, +0.0008 against PHENIX and +0.0047 against REFMAC. (B) Geometry RMS Z against REFMAC restraints, ideal 1.0: bond 0.58 (REFMAC 0.58, PHENIX 0.83), main-chain B 1.55 (REFMAC 0.90, PHENIX 1.03). (C) Wall-clock runtime per structure on 4 CPU cores: median 0.77 min (REFMAC 0.25, PHENIX 3.09). (D) Fraction of the total R-free improvement reached per macrocycle.

Key Features

  • Native PyTorch Integration: Built on PyTorch's nn.Module architecture, so TorchRef composes with PyTorch models, optimizers, and devices.

  • Automatic Differentiation: No hand-written gradients. Define a new refinement target's forward pass and PyTorch supplies the derivatives.

  • Modular Architecture: Custom targets, restraints, and optimizers plug in without modifying core code.

  • GPU Acceleration: CUDA for structure factors, scaling, and optimization. Apple Silicon works through PyTorch's MPS backend — unsupported ops fall back to CPU automatically via PYTORCH_ENABLE_MPS_FALLBACK=1, which TorchRef sets on import.

  • FFT-based Structure Factors: F_calc via FFT, so large unit cells stay tractable.

Getting Started

Notebook Description
Open In Colab Quickstart — MTZ + PDB to refined structure, refined MTZ and CCP4 map; selection- and parameter-type-based refinement
Open In Colab Structure factors — one-liner, SfFFT class, and manual voxel pipeline; autograd
Open In Colab Targets and weighting — standard targets, static group weights, X-ray mode comparison, custom targets, driving an optimizer from a LossState

Installation

pip install torchref

For development:

git clone --filter=blob:none --sparse https://github.com/HatPdotS/TorchRef.git
cd TorchRef
git sparse-checkout set torchref tests kernels
pip install ./kernels        # the prebuilt CPU kernels; needs a Rust toolchain (rustup.rs)
pip install -e ".[dev]"

Skipping pip install ./kernels is fine when the published torchref-kernels wheel matches the checkout; pip then installs it from PyPI. Rebuild after editing anything in kernels/.

This fetches ~40 MB instead of the full history (~800 MB, mostly test data and the paper figures that lived in paper/ before v0.7.0). Files outside the checkout are fetched on demand, so add paths later with git sparse-checkout add docs, or git sparse-checkout disable for all of it. Requires Git ≥ 2.27.

Dependencies

Python ≥ 3.10, PyTorch ≥ 2.4, NumPy ≥ 2.0, Pandas ≥ 2.0, SciPy ≥ 1.10, Gemmi ≥ 0.5, reciprocalspaceship ≥ 0.9.18, Matplotlib ≥ 3.7. pyproject.toml carries the authoritative pinned ranges; upper bounds are set one minor version above the tested maximum, so a newer dependency will refuse to install rather than fail at runtime.

Testing

pytest tests/                      # all tests
pytest tests/ --cov=torchref       # with coverage
pytest tests/unit/                 # fast unit tests only

Slow tests need --run-slow. Accelerator tests are not opt-in: they run wherever CUDA or MPS is available and are skipped when it is not.

Contributing

Contributions are welcome. Please use NumPy docstring style, add tests for new functionality, and make sure the suite passes before submitting.

Questions, feature ideas and usage discussion are welcome on the TorchRef category of the rs-station forum.

Citing

If you use TorchRef in published work, please cite

Seidel, H.-P., Standfuss, J. & Weinert, T. (2026). TorchRef: an open-source PyTorch framework for crystallographic refinement. J. Appl. Cryst. 59(6). https://doi.org/10.1107/S1600576726009258

The figure scripts, per-panel source data and the author accepted manuscript are in HatPdotS/TorchRef-paper.

License

MIT. See LICENSE.

Metadata

Release files for torchref 0.8.0

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

Source distribution (sdist)

Source distribution for torchref 0.8.0
File Size Uploaded Provenance Actions
torchref-0.8.0.tar.gz 3.2 MB With provenance Details for torchref-0.8.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for torchref 0.8.0
File Interpreter ABI Platform Actions
torchref-0.8.0-py3-none-any.whl Python 3 none any Details for torchref-0.8.0-py3-none-any.whl

Total release size: 6.6 MB

Release files / torchref-0.8.0.tar.gz

Download URL torchref-0.8.0.tar.gz
Size 3.2 MB
Tags Source
SHA-256 checksum
How to use checksums
440efe89af54f8ab6af235ccaf8bbced9370d039a24cb72ff73177791ed21f0d
BLAKE2b-256 checksum
How to use checksums
67b65443427b91c719d3ea44ab161ac4f8e342d5d10ada21aaa662e5341da35b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 5, 2026.

Transparency log

Release files / torchref-0.8.0-py3-none-any.whl

Download URL torchref-0.8.0-py3-none-any.whl
Size 3.4 MB
Tags Python 3
SHA-256 checksum
How to use checksums
a405b870bc11ed62ab33c1697ca27f50090d22652827e40794f9d10453dbc975
BLAKE2b-256 checksum
How to use checksums
9e1a758335a22b568f1a027cf025e4cc9dc2efd8d7786b61131e18f9b7a5de63
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 5, 2026.

Transparency log

Security for torchref 0.8.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for torchref-0.8.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 5, 2026.

Transparency log

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.

Release history Release notifications | RSS feed

This release

0.8.0 This release

2 release files Full provenance

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