Fix neighbor_list_from_ase(compute_distances=True) crashing on periodic structures - #827
Merged
shyuep merged 3 commits intoAug 25, 2026
Conversation
…ic structures _compute_distances multiplied the integer unit-shift matrix returned by the neighbor-list kernel with the float cell; integer x float matmul is not defined in torch, so the DEFAULT arguments of neighbor_list_from_ase raised 'expected mat1 and mat2 to have the same dtype' on any structure with periodic boundary conditions. Non-periodic structures dodge the path (unit_shifts is None), which is presumably how it survived smoke tests. Cast the shifts to the position dtype at the point of use. The new test runs the default path on a periodic rocksalt supercell and checks the returned distances against a float64 recomputation (1e-4) and the cutoff, not merely the absence of a crash. While auditing this path we also cross-checked the neighbor list itself: on a dense triclinic 1,248-atom cell at 5.0 A the returned edge set is bit-identical (shift vectors included) to an exact O(N^2) minimum-image reference -- 25,242 edges, zero missing, zero spurious. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
toohardtogetname
force-pushed
the
fix-alchmtk-compute-distances
branch
from
August 25, 2026 05:29
8ad0604 to
f979a26
Compare
toohardtogetname
marked this pull request as ready for review
August 25, 2026 05:31
toohardtogetname
pushed a commit
to toohardtogetname/matgl
that referenced
this pull request
Aug 25, 2026
…; adopt materialyzeai#825's captured-local lambda style in item-5 hunks Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
matgl.ext._alchmtk._compute_distancesdoeswith the
unit_shiftstensor exactly as the nvalchemiops neighbor-listkernel returns it — an integer tensor. Integer × float
matmulis notdefined in PyTorch, so the call raises
Because
compute_distances=Trueis the default ofneighbor_list_from_ase, the default call crashes on any structurewith periodic boundary conditions.
neighbor_list_from_structuresharesthe same sink and is hit even harder: a pymatgen
Structureis alwaysperiodic, so its default call crashes on every input. Non-periodic
inputs dodge the path (
unit_shifts is None), which is how it survivedsmoke tests — and notably, all three internal callers in
matgl.ext.pymatgen/matgl.ext.aseexplicitly passcompute_distances=False, so the default path had zero coverage anywhere.The one-line fix at
_compute_distancesrepairs all three public entrypoints at once.
Who hits this — and who doesn't
Nothing in matgl's own pipelines calls this branch today: model
training/inference and MD go through
Structure2Graph, which computes bonddistances elsewhere. Existing results are unaffected, which is also why the
crash went unnoticed. It bites the first direct user of the public API on a
periodic system, e.g.:
block (an MD engine, an adapter, a new-model prototype) — the natural call
neighbor_list_from_ase(atoms, cutoff)crashes on any crystal (this is howwe found it, while auditing neighbor lists);
statistics) — exactly the use case the returned
distancesexist for, andalmost always on periodic cells;
against matgl's;
distances — fixing it now removes that landmine before it can reach the
main pipeline.
Fix
One line — cast the shifts to the position dtype at the point of use:
Test
tests/ext/test_alchmtk_neighborlist.py: a periodic rocksalt supercell through thedefault
compute_distances=Truepath. It asserts not only that the callsurvives but that the returned distances match a float64 recomputation
from the returned
(positions, shifts, cell)to 1e-4 and respect thecutoff.
Verified on H200 / CUDA 12.4 / matgl 4.0.3: the test fails before the
patch with the RuntimeError above and passes after it.
While auditing this path
We also cross-checked the neighbor list itself (not part of this PR,
reported for confidence): on a dense triclinic 1,248-atom cell at 5.0 Å,
neighbor_list_from_asereturned an edge set bit-identical (shiftvectors included) to an exact O(N²) minimum-image reference — 25,242
edges, zero missing, zero spurious. The
_safe_nlretry-on-overflow logicworks as designed.
🤖 Generated with Claude Code