-
Notifications
You must be signed in to change notification settings - Fork 8
Add torch-sim integration as a separate metatomic-torchsim package #167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| name: TorchSim integration | ||
|
|
||
| on: | ||
| push: | ||
| branches: [main] | ||
| pull_request: | ||
| # Check all PR | ||
|
|
||
| concurrency: | ||
| group: torchsim-tests-${{ github.ref }} | ||
| cancel-in-progress: ${{ github.ref != 'refs/heads/main' }} | ||
|
|
||
| jobs: | ||
| tests: | ||
| runs-on: ubuntu-24.04 | ||
| name: tests | ||
| steps: | ||
| - uses: actions/checkout@v6 | ||
| with: | ||
| fetch-depth: 0 | ||
|
|
||
| - name: setup Python | ||
| uses: actions/setup-python@v6 | ||
| with: | ||
| python-version: "3.13" | ||
|
|
||
| - name: Setup sccache | ||
| uses: mozilla-actions/sccache-action@v0.0.9 | ||
| with: | ||
| version: "v0.10.0" | ||
|
|
||
| - name: Setup sccache environnement variables | ||
| run: | | ||
| echo "SCCACHE_GHA_ENABLED=true" >> $GITHUB_ENV | ||
| echo "RUSTC_WRAPPER=sccache" >> $GITHUB_ENV | ||
| echo "CMAKE_C_COMPILER_LAUNCHER=sccache" >> $GITHUB_ENV | ||
| echo "CMAKE_CXX_COMPILER_LAUNCHER=sccache" >> $GITHUB_ENV | ||
|
|
||
| - name: install tests dependencies | ||
| run: | | ||
| python -m pip install --upgrade pip | ||
| python -m pip install tox coverage | ||
|
|
||
| - name: run tests | ||
| run: tox -e torchsim-tests | ||
| env: | ||
| PIP_EXTRA_INDEX_URL: https://download.pytorch.org/whl/cpu | ||
|
|
||
| - name: combine Python coverage files | ||
| shell: bash | ||
| run: | | ||
| coverage combine .tox/*/.coverage | ||
| coverage xml | ||
|
|
||
| - name: upload to codecov.io | ||
| uses: codecov/codecov-action@v5 | ||
| with: | ||
| fail_ci_if_error: true | ||
| files: coverage.xml | ||
| token: ${{ secrets.CODECOV_TOKEN }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Rhys Goodall | ||
| Guillaume Fraux | ||
| Filippo Bigi | ||
| Rohit Goswami |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| # Architecture | ||
|
|
||
| This page explains how `metatomic_torchsim.MetatomicModel` bridges TorchSim and | ||
| metatomic. | ||
|
|
||
| ## SimState vs list of System | ||
|
|
||
| TorchSim represents a simulation as a single batched `SimState` containing all | ||
| atoms from all systems, with a `system_idx` tensor tracking ownership. Metatomic | ||
| expects a `list[System]` where each `System` holds one periodic structure. | ||
|
|
||
| `MetatomicModel.forward` converts between these representations: | ||
|
|
||
| 1. Split the batched positions and atomic numbers by `system_idx` | ||
| 2. Create one `System` per sub-structure with its own cell | ||
| 3. Call the model on the list of systems | ||
| 4. Concatenate results back into batched tensors | ||
|
|
||
| ## Forces via autograd | ||
|
|
||
| Metatomic models typically output only total energies. Forces are computed as | ||
| the negative gradient of the energy with respect to atomic positions: | ||
|
|
||
| F_i = -dE/dr_i | ||
|
|
||
| Before calling the model, each system\'s positions are detached and set to | ||
| `requires_grad_(True)`. After the forward pass, `torch.autograd.grad` computes | ||
| the derivatives. | ||
|
|
||
| ## Stress via the strain trick | ||
|
|
||
| Stress is computed using the Knuth strain trick. An identity strain tensor (3x3, | ||
| `requires_grad=True`) is applied to both positions and cell vectors: | ||
|
|
||
| r' = r @ strain | ||
| h' = h @ strain | ||
|
|
||
| The stress per system is then: | ||
|
|
||
| sigma = (1/V) * dE/d(strain) | ||
|
|
||
| where V is the cell volume. This gives the full 3x3 stress tensor without finite | ||
| differences. | ||
|
|
||
| ## Neighbor lists | ||
|
|
||
| Models specify what neighbor lists they need via | ||
| `model.requested_neighbor_lists()`, which returns a list of | ||
| `NeighborListOptions` (cutoff radius, full vs half list). | ||
|
|
||
| The wrapper computes these using: | ||
|
|
||
| - **vesin**: Default backend for both CPU and GPU. Handles half and full | ||
| neighbor lists. Systems on non-CPU/CUDA devices are temporarily moved to CPU | ||
| for the computation. | ||
| - **nvalchemiops**: Used automatically on CUDA for full neighbor lists when | ||
| installed. Keeps everything on GPU, avoiding host-device transfers. | ||
|
|
||
| The decision happens per-call in `_compute_requested_neighbors`: if all systems | ||
| are on CUDA and nvalchemiops is available, full-list requests go through | ||
| nvalchemi while half-list requests still use vesin. | ||
|
|
||
| ## Why a separate package | ||
|
|
||
| metatomic-torchsim has its own versioning, release schedule, and dependency set | ||
| (`torch-sim-atomistic`). Keeping it separate from metatomic-torch avoids forcing | ||
| a torch-sim dependency on users who only need the ASE calculator or other | ||
| integrations. | ||
|
|
||
| The package is pure Python with no compiled extensions, making it lightweight to | ||
| install. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Changelog | ||
|
|
||
| All notable changes to metatomic-torchsim are documented here, following the | ||
| [keep a changelog](https://keepachangelog.com/en/1.1.0/) format. This project | ||
| follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html). | ||
|
|
||
| <!-- Possible sections for each package: | ||
| ### Added | ||
|
|
||
| ### Fixed | ||
|
|
||
| ### Changed | ||
|
|
||
| ### Removed | ||
| --> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| include AUTHORS | ||
| include git_version_info |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # metatomic-torchsim | ||
|
|
||
| TorchSim integration for metatomic atomistic models. | ||
|
|
||
| Wraps metatomic models as TorchSim `ModelInterface` instances, enabling their | ||
| use in TorchSim molecular dynamics and other simulation workflows. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| pip install metatomic-torchsim | ||
| ``` | ||
|
|
||
| For universal potential models, see | ||
| [upet](https://github.com/lab-cosmo/upet). | ||
|
|
||
| ## Usage | ||
|
|
||
| ```python | ||
| from metatomic_torchsim import MetatomicModel | ||
|
|
||
| # From a saved .pt model | ||
| model = MetatomicModel("model.pt", device="cuda") | ||
|
|
||
| # Use with TorchSim | ||
| output = model(sim_state) | ||
| energy = output["energy"] | ||
| forces = output["forces"] | ||
| stress = output["stress"] | ||
| ``` | ||
|
|
||
| For full documentation, see the | ||
| [torch-sim engine page](https://docs.metatensor.org/metatomic/latest/engines/torch-sim.html). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| from ._model import MetatomicModel | ||
|
|
||
|
|
||
| __all__ = ["MetatomicModel"] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.