Skip to content

Adaptive per-atom cutoffs and Cartesian inputs - #1

Open
sirmarcel wants to merge 1 commit into
Chronum94:model-improvementsfrom
sirmarcel:adaptive-cutoff
Open

sirmarcel wants to merge 1 commit into
Chronum94:model-improvementsfrom
sirmarcel:adaptive-cutoff

Conversation

@sirmarcel

Copy link
Copy Markdown

Adaptive per-atom cutoffs (PET-style) and Cartesian training inputs, on top of apax-hub#565 — the two overlap (stress, self-images), so this targets your branch rather than main.

Adaptive cutoff

Ported from pet-jax (metatrain's "solver" method). Each atom gets a cutoff such that it sees ~num_neighbors neighbors (smooth counting + cubic baseline, Newton-bisection root find). Pairs within the mean of both atomic cutoffs are kept, tapered to zero at their pair cutoff, and packed into a fixed n_atoms * k_sel edge list. Dense environments stay bounded; sparse ones (dimers, molecules, clusters) get cutoffs close to r_max. The goal is to train on e.g. MAD, with bulk and dimers in one dataset.

model:
  basis: {r_max: 6.0}        # maximum cutoff, and neighbor list cutoff
  adaptive_cutoff:
    num_neighbors: 16
  • Gradients flow through the per-atom cutoffs via an implicit-function-theorem step ("shadow forces"), so forces/stress are exact derivatives; checked against finite differences for GMNN and equiv-MP, gas phase and periodic.
  • The adaptive taper replaces the descriptor's fixed r_max cutoff function (GMNN incl. FactorizedRadialFunction, equiv-MP, so3krates take an optional per-edge cutoffs). Off by default; nothing changes without adaptive_cutoff.
  • k_sel is a shape, not saved: sized from the data in training/eval/BAL/batch_eval; the ASE calculator re-sizes on overflow; jax-md MD tracks the max selected count per chunk and redoes the chunk with a larger capacity. OpenMM raises NotImplementedError for adaptive models.
  • Periodic self-images are selected like any other neighbor, consistent with your self-image masking (also applied to so3krates' mask here).

Cartesian inputs

A local model only needs edge vectors, so the training path (and eval, BAL, batch_eval, and the vesin fallback of the ASE/OpenMM calculators) now uses Cartesian positions + Cartesian periodic shifts (S @ cell, zero in gas phase). Periodic and gas-phase structures share one distance path and can be mixed in a dataset; the "no mixed datasets" check is gone. The jax-md inference paths keep fractional coordinates. Existing checkpoints are unaffected (descriptors see identical edge vectors).

  • Stress: your one-backward-pass dE/dF is kept; on the Cartesian path the deformation acts on the full edge vector incl. shift (F r_ij, jax-md convention, same as your fix).
  • Heads/corrections that use absolute positions (l1/l2 heads, latent Ewald) now get Cartesian positions on the jax-md path too; before, training and inference were consistent but both fractional.
  • tests/unit_tests/layers/test_periodic_cells.py: the training-path input helper now builds Cartesian inputs.

Also

  • MD trajectory: frames are flushed only once their chunk is accepted; a redo after neighbor-list/adaptive overflow drops only that chunk's frames (previously all unflushed frames were lost). NL reallocation keeps extra_capacity.

Tests

New: tests/unit_tests/layers/test_adaptive_cutoff.py (solver, packing, FD forces with/without shadow term, FD stress on a triclinic cell, small-cell self-images, continuity), tests/unit_tests/model/test_cartesian_inputs.py (jax-md fractional path == Cartesian path incl. a dipole head), tests/integration_tests/md/test_adaptive_cutoff.py (training on bulk Cu + Cu2 dimers, calculator FD forces, overflow resize, batch_eval, MD overflow redo).

Locally (CPU): 136 passed, 2 skipped, 1 xfailed, 5 failed. The 5 failures (test_ntk_linear, 4× bal/test_api.py::test_kernel_selection) also fail on model-improvements alone. so3krates is untested here (needs myrto); OpenMM tests not run (openmm not installed).

🤖 Generated with Claude Code

Adaptive cutoff (ported from pet-jax, "solver" method):
- apax/layers/adaptive_cutoff.py: per-atom cutoffs via Newton-bisection
  root find with an implicit-function-theorem step, so gradients flow
  through the cutoffs (shadow forces). Pairs within the mean of both atomic
  cutoffs are selected, tapered at their pair cutoff, and packed into
  n_atoms * k_sel edges, bounding the per-atom environment. Sparse systems
  (dimers, molecules) get cutoffs close to r_max.
- Optional `adaptive_cutoff` model config; basis.r_max becomes the maximum
  (and neighbor list) cutoff. The taper replaces the descriptors' fixed
  r_max cutoff (GMNN incl. the factorized radial function, equiv-MP and
  so3krates take per-edge `cutoffs`). Periodic self-images are selected
  like other neighbors, consistent with the self-image masking.
- k_sel is a shape, not saved: sized from the data in training, eval, BAL
  and batch_eval; the ASE calculator resizes on overflow, jax-md MD tracks
  the max selected count per chunk and redoes the chunk with a larger
  capacity. OpenMM raises NotImplementedError for adaptive models.

Cartesian inputs:
- Training, eval, BAL, batch_eval and the vesin inference fallbacks use
  Cartesian positions and Cartesian periodic shifts. Periodic and gas-phase
  structures share one distance path and can be mixed in a dataset (e.g.
  bulk together with dimers). The strain F acts on the full edge vector,
  periodic shift included, as on the jax-md path.
- Heads/corrections using absolute positions receive Cartesian positions
  on the jax-md (fractional) inference path as well.
- so3krates keeps periodic self-image pairs, like GMNN and equiv-MP.

MD trajectory: frames are flushed only once their chunk is accepted; an
overflow redo drops only that chunk's frames (previously all unflushed
frames). Neighbor list reallocation now keeps extra_capacity.

Tests: finite-difference checks for forces (incl. shadow term) and stress,
solver/packing unit tests, self-images under selection, fractional vs
Cartesian path consistency, and an end-to-end test (training on bulk +
dimers, calculator, overflow, MD).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant