Deployment#

A trained model can be served four ways. All four take the same inputs: a checkpoint, a model directory, a model hub name, a URL or a DOI. The deployment channels are:

  • ASE calculator, which enables molecular dynamics, relaxations and analysis in Python,

  • TorchScript file, which is compatible with LAMMPS and other engines, with no xnn installed,

  • OpenMM force, for OpenMM simulations, alone or as the ML part of an ML/MM system, and

  • MDI engine which works with any MD driver that can communicate with the MolSSI Driver Interface.

ASE calculator#

The XNNCalculator (requires ase extra dependencies installed) provides energy, forces and stress, plus charges and dipole for models that can predict them:

from xnn.common.deploy import XNNCalculator

atoms.calc = XNNCalculator.from_pretrained("mace-mp-0-medium", device="cuda")
atoms.calc = XNNCalculator(model, cutoff=5.0)       #  A model object you hold

The net charge and spin multiplicity can come from atoms.info entries in an ASE *.xyz file. See the Run Molecular Dynamics with ASE guide for more information. On a GPU, cuda_graph=True replays each evaluation as one CUDA graph for DimeNet, DimeNet++, GemNet, PhysNet, SpookyNet, MACE, AIMNet2, TensorNet, QET and QRNN, which is faster for small structures (see Run Models on Fused GPU Kernels).

TorchScript#

The xnn export command writes one .pt file that carries its own neighbor list:

xnn export --ckpt runs/exp/best.pt --out deployed.pt

You can load it in Python and call forward with the same arguments as the original model:

model = torch.jit.load("deployed.pt")
out = model(pos, atomic_numbers, cell, pbc)      # energy, forces, stress, ...

The forward_lammps(pos, edge_index, cell_shifts, atomic_numbers, cell) takes the engine’s own neighbor list instead, as the pair_nequip / pair_allegro / pair_mace pair styles expect. Note that:

  • There should be one structure per call here. Using torch.no_grad() will work but torch.inference_mode() will not.

  • The net charge and spin multiplicity are fixed at export time (by setting --total-charge and --spin-multiplicity flags).

  • A model with long-range terms (LES, D4, SpookyNet, or the charges of AIMNet2, BAMBOO, PhysNet and HDNNP 3G / 4G) needs the whole atomistic system on one rank, so drive it through forward, fix external or the MDI engine rather than a domain-decomposed pair style.

For more technical details and options, see the Export to LAMMPS and TorchScript.

OpenMM#

from openmmml import MLPotential

potential = MLPotential("xnn", model="mace-off23-small")       # or a checkpoint, or a model object
system = potential.createSystem(topology)                      # every atom with the model
mixed = potential.createMixedSystem(topology, mm_system, ligand_atoms)   # ML/MM
  • Installing xnn registers the "xnn" potential with openmm-ml.

  • OpenMM needs pip install openmm openmmtorch openmmml.

  • The openmmtorch wheels need the CUDA build of torch they were built with (torch 2.9 for openmmtorch 1.5). Conda-forge installs a matching pair.

  • In a mixed system, xnn models the selected atoms and the force field models the rest.

  • Optional arguments: total_charge, spin_multiplicity, cutoff and dtype.

  • Without openmm-ml, create_torch_force() returns the force itself.

  • The force builds its neighbor list over all atom pairs. Keep the xnn region to a few thousand atoms.

MDI engine#

The xnn mdi (requires mdi extra dependency installed) serves a checkpoint to LAMMPS fix mdi/qm or any other MDI compatible MD driver:

xnn mdi --ckpt runs/exp/best.pt --device cuda:0 \
        -mdi "-role ENGINE -name xnn -method TCP -port 8021 -hostname localhost"

You can use the --dispersion d4 flag to add a dispersion term to a plain model. Set the --eeq-reuse to speed up D4 in an MD run. For AMOEBA, HIPPO and ByteFF-Pol the engine predicts each step’s induced dipoles from the last steps (--dipole-predictor, ASPC by default). On a GPU it also evaluates their steps on a Verlet pair list (--skin, 1 angstrom by default) and replays each step as one CUDA graph. An atom that the driver wraps back into the cell keeps its pair list. The --step-graph off flag disables this behavior. For ByteFF-Pol the engine also reads each step’s checks during the next step, so the GPU never waits (--late-checks, on by default for ByteFF-Pol only).

An AMOEBA or HIPPO engine can serve part of the model, for a driver that runs RESPA (multiple time steps):

xnn mdi --ckpt hippo.pt --device cuda:0 --terms nonbonded --dtype mixed \
        -mdi "-role ENGINE -name xnn -method TCP -port 8021 -hostname localhost"
  • --terms valence serves the bonded terms.

  • --terms nonbonded serves everything else, ion terms included.

  • The two add up to the default --terms all, forces and stress included.

  • --dtype mixed runs the model in mixed precision, which is faster. Use float64 for NVE runs.

The The Command Line guide lists every option available to you.

There are example notebooks in notebooks/examples/common/deploy which illustrate how to drive the engine from Python and LAMMPS deployment interfaces.

Dispersion removed from the labels#

A model trained on E_ref - E_D4 needs the D4 term added back wherever it is adopted for inference. If you record the subtracted_dispersion section in the training config, every deployment channel will automatically add the subtracted dispersion term back to the predicted energies at the inference time:

subtracted_dispersion:
  name: d4                   # Or d3, with the damping parameters of the labels
  s8: 1.20065498
  a1: 0.40085597
  a2: 5.02928789
  cutoff_pair: 12.0          # Settings for periodic runs
  switch_width_pair: 2.0

You can use the --no-dispersion flag to serve or export your model without it.

One structure on several GPUs#

# torchrun --nproc-per-node=8 run.py
import torch
import torch.distributed as dist
from xnn.common.deploy import DomainParallel

dist.init_process_group("nccl")
torch.cuda.set_device(dist.get_rank() % torch.cuda.device_count())
dp = DomainParallel(model.cuda().eval(), skin=1.0)
out = dp(graph.cuda())          # energy, forces and stress on every process
  • DomainParallel cuts the box into one sub-domain per GPU. The results equal a single-GPU evaluation.

  • Each GPU evaluates its own atoms plus copies of their neighbors (ghosts). MACE, TensorNet and AIMNet2 need one cutoff of ghosts: the ghosts’ features are refreshed from their owners before every layer (halo="layer"). Other models take ghosts out to their receptive field (halo="field").

  • Each GPU computes the forces of its own atoms. Only the energy and the virial are summed over the GPUs.

  • A plan (owners, ghosts, neighbor list) is reused while the atoms have moved less than the skin allows.

  • The memory per GPU falls with the number of GPUs, so a box too large for one GPU fits on several.

  • Models with long-range terms (Coulomb sums, LES, D4) are refused.

  • AIMNet2 works without its Coulomb term.

When each GPU keeps only its own atoms, the forces are not gathered:

plan = dp.plan(graph)                        # once, from the whole structure
pos, vel = graph.pos[plan.owned], vel[plan.owned]
for step in range(n_steps):
    if not dp.holds(plan, pos):
        plan, pos, vel = dp.reshard(plan, pos, vel)
    out = dp.evaluate(plan, pos)             # forces of the owned atoms
    ...                                      # move the owned atoms

Which model deploys how#

  • ASE and MDI: every model is supported, with or without a D3 / D4 term.

  • OpenMM: the models of the TorchScript export below.

  • TorchScript / LAMMPS: SchNet, DimeNet, GemNet, PaiNN, TensorNet, ET, Equiformer, EquiformerV2, SpookyNet, BAMBOO, NequIP, MACE, Allegro, CACE, AIMNet2, ANI, PhysNet and HDNNP, with or without a D3 / D4 term, and PM3 / PM6 / PM6-ORG / PM7.

  • LAMMPS inputs: OPLS, AMBER, CHARMM, DREIDING, ByteFF, classical ReaxFF, AMOEBA and HIPPO, through write_lammps_input() (see Export to LAMMPS and TorchScript).