Metadata-Version: 2.2
Name: deepmd-gnn
Version: 0.2.0
Summary: DeePMD-kit plugin for graph neural network models.
Author-Email: Jinzhe Zeng <jinzhe.zeng@ustc.edu.cn>
License:                    GNU LESSER GENERAL PUBLIC LICENSE
                                Version 3, 29 June 2007
         
          Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
          Everyone is permitted to copy and distribute verbatim copies
          of this license document, but changing it is not allowed.
         
         
           This version of the GNU Lesser General Public License incorporates
         the terms and conditions of version 3 of the GNU General Public
         License, supplemented by the additional permissions listed below.
         
           0. Additional Definitions.
         
           As used herein, "this License" refers to version 3 of the GNU Lesser
         General Public License, and the "GNU GPL" refers to version 3 of the GNU
         General Public License.
         
           "The Library" refers to a covered work governed by this License,
         other than an Application or a Combined Work as defined below.
         
           An "Application" is any work that makes use of an interface provided
         by the Library, but which is not otherwise based on the Library.
         Defining a subclass of a class defined by the Library is deemed a mode
         of using an interface provided by the Library.
         
           A "Combined Work" is a work produced by combining or linking an
         Application with the Library.  The particular version of the Library
         with which the Combined Work was made is also called the "Linked
         Version".
         
           The "Minimal Corresponding Source" for a Combined Work means the
         Corresponding Source for the Combined Work, excluding any source code
         for portions of the Combined Work that, considered in isolation, are
         based on the Application, and not on the Linked Version.
         
           The "Corresponding Application Code" for a Combined Work means the
         object code and/or source code for the Application, including any data
         and utility programs needed for reproducing the Combined Work from the
         Application, but excluding the System Libraries of the Combined Work.
         
           1. Exception to Section 3 of the GNU GPL.
         
           You may convey a covered work under sections 3 and 4 of this License
         without being bound by section 3 of the GNU GPL.
         
           2. Conveying Modified Versions.
         
           If you modify a copy of the Library, and, in your modifications, a
         facility refers to a function or data to be supplied by an Application
         that uses the facility (other than as an argument passed when the
         facility is invoked), then you may convey a copy of the modified
         version:
         
            a) under this License, provided that you make a good faith effort to
            ensure that, in the event an Application does not supply the
            function or data, the facility still operates, and performs
            whatever part of its purpose remains meaningful, or
         
            b) under the GNU GPL, with none of the additional permissions of
            this License applicable to that copy.
         
           3. Object Code Incorporating Material from Library Header Files.
         
           The object code form of an Application may incorporate material from
         a header file that is part of the Library.  You may convey such object
         code under terms of your choice, provided that, if the incorporated
         material is not limited to numerical parameters, data structure
         layouts and accessors, or small macros, inline functions and templates
         (ten or fewer lines in length), you do both of the following:
         
            a) Give prominent notice with each copy of the object code that the
            Library is used in it and that the Library and its use are
            covered by this License.
         
            b) Accompany the object code with a copy of the GNU GPL and this license
            document.
         
           4. Combined Works.
         
           You may convey a Combined Work under terms of your choice that,
         taken together, effectively do not restrict modification of the
         portions of the Library contained in the Combined Work and reverse
         engineering for debugging such modifications, if you also do each of
         the following:
         
            a) Give prominent notice with each copy of the Combined Work that
            the Library is used in it and that the Library and its use are
            covered by this License.
         
            b) Accompany the Combined Work with a copy of the GNU GPL and this license
            document.
         
            c) For a Combined Work that displays copyright notices during
            execution, include the copyright notice for the Library among
            these notices, as well as a reference directing the user to the
            copies of the GNU GPL and this license document.
         
            d) Do one of the following:
         
                0) Convey the Minimal Corresponding Source under the terms of this
                License, and the Corresponding Application Code in a form
                suitable for, and under terms that permit, the user to
                recombine or relink the Application with a modified version of
                the Linked Version to produce a modified Combined Work, in the
                manner specified by section 6 of the GNU GPL for conveying
                Corresponding Source.
         
                1) Use a suitable shared library mechanism for linking with the
                Library.  A suitable mechanism is one that (a) uses at run time
                a copy of the Library already present on the user's computer
                system, and (b) will operate properly with a modified version
                of the Library that is interface-compatible with the Linked
                Version.
         
            e) Provide Installation Information, but only if you would otherwise
            be required to provide such information under section 6 of the
            GNU GPL, and only to the extent that such information is
            necessary to install and execute a modified version of the
            Combined Work produced by recombining or relinking the
            Application with a modified version of the Linked Version. (If
            you use option 4d0, the Installation Information must accompany
            the Minimal Corresponding Source and Corresponding Application
            Code. If you use option 4d1, you must provide the Installation
            Information in the manner specified by section 6 of the GNU GPL
            for conveying Corresponding Source.)
         
           5. Combined Libraries.
         
           You may place library facilities that are a work based on the
         Library side by side in a single library together with other library
         facilities that are not Applications and are not covered by this
         License, and convey such a combined library under terms of your
         choice, if you do both of the following:
         
            a) Accompany the combined library with a copy of the same work based
            on the Library, uncombined with any other library facilities,
            conveyed under the terms of this License.
         
            b) Give prominent notice with the combined library that part of it
            is a work based on the Library, and explaining where to find the
            accompanying uncombined form of the same work.
         
           6. Revised Versions of the GNU Lesser General Public License.
         
           The Free Software Foundation may publish revised and/or new versions
         of the GNU Lesser General Public License from time to time. Such new
         versions will be similar in spirit to the present version, but may
         differ in detail to address new problems or concerns.
         
           Each version is given a distinguishing version number. If the
         Library as you received it specifies that a certain numbered version
         of the GNU Lesser General Public License "or any later version"
         applies to it, you have the option of following the terms and
         conditions either of that published version or of any later version
         published by the Free Software Foundation. If the Library as you
         received it does not specify a version number of the GNU Lesser
         General Public License, you may choose any version of the GNU Lesser
         General Public License ever published by the Free Software Foundation.
         
           If the Library as you received it specifies that a proxy can decide
         whether future versions of the GNU Lesser General Public License shall
         apply, that proxy's public statement of acceptance of any version is
         permanent authorization for you to choose that version for the
         Library.
         
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Operating System :: POSIX :: Linux
Classifier: Operating System :: MacOS :: MacOS X
Classifier: Operating System :: Microsoft :: Windows
Classifier: License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)
Project-URL: repository, https://gitlab.com/RutgersLBSR/deepmd-gnn
Requires-Python: >=3.10
Requires-Dist: torch>=2.10
Requires-Dist: deepmd-kit[torch]>=3.0.0
Requires-Dist: mace-torch>=0.3.5
Requires-Dist: nequip<0.7
Requires-Dist: e3nn
Requires-Dist: dargs
Provides-Extra: cueq
Requires-Dist: cuequivariance>=0.10.0; extra == "cueq"
Requires-Dist: cuequivariance-torch>=0.10.0; extra == "cueq"
Requires-Dist: cuequivariance-ops-torch-cu12>=0.10.0; extra == "cueq"
Provides-Extra: cueq-cu11
Requires-Dist: cuequivariance>=0.10.0; extra == "cueq-cu11"
Requires-Dist: cuequivariance-torch>=0.10.0; extra == "cueq-cu11"
Requires-Dist: cuequivariance-ops-torch-cu11>=0.10.0; extra == "cueq-cu11"
Provides-Extra: cueq-cu12
Requires-Dist: cuequivariance>=0.10.0; extra == "cueq-cu12"
Requires-Dist: cuequivariance-torch>=0.10.0; extra == "cueq-cu12"
Requires-Dist: cuequivariance-ops-torch-cu12>=0.10.0; extra == "cueq-cu12"
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Requires-Dist: pytest-cov; extra == "test"
Requires-Dist: dargs>=0.4.8; extra == "test"
Provides-Extra: docs
Requires-Dist: sphinx; extra == "docs"
Requires-Dist: sphinx-autoapi; extra == "docs"
Requires-Dist: myst-parser; extra == "docs"
Requires-Dist: deepmodeling-sphinx>=0.3.0; extra == "docs"
Requires-Dist: sphinx-book-theme; extra == "docs"
Requires-Dist: dargs; extra == "docs"
Description-Content-Type: text/markdown

# DeePMD-kit plugin for various graph neural network models

[![DOI:10.1021/acs.jcim.4c02441](https://img.shields.io/badge/DOI-10.1021%2Facs.jcim.4c02441-blue)](https://doi.org/10.1021/acs.jcim.4c02441)
[![Citations](https://citations.njzjz.win/10.1021/acs.jcim.4c02441)](https://doi.org/10.1021/acs.jcim.4c02441)
[![conda install](https://img.shields.io/conda/dn/conda-forge/deepmd-gnn?label=conda%20install)](https://anaconda.org/conda-forge/deepmd-gnn)
[![PyPI - Version](https://img.shields.io/pypi/v/deepmd-gnn)](https://pypi.org/p/deepmd-gnn)

`deepmd-gnn` is a [DeePMD-kit](https://github.com/deepmodeling/deepmd-kit) plugin for various graph neural network (GNN) models, which connects DeePMD-kit and atomistic GNN packages by enabling GNN models in DeePMD-kit.

Supported packages and models include:

- [MACE](https://github.com/ACEsuit/mace) (PyTorch version)
- [NequIP](https://github.com/mir-group/nequip) (PyTorch version)

After [installing the plugin](#installation), you can train the GNN models using DeePMD-kit, run active learning cycles for the GNN models using [DP-GEN](https://github.com/deepmodeling/dpgen), and perform simulations with MACE and NequIP models using molecular dynamic packages supported by DeePMD-kit, such as [LAMMPS](https://github.com/lammps/lammps) and [AMBER](https://ambermd.org/).
You can follow [DeePMD-kit documentation](https://docs.deepmodeling.com/projects/deepmd/en/latest/) to train the GNN models using its PyTorch backend, after using the specific [model parameters](#parameters).

## Highlights

The official MACE and NequIP packages remain the reference implementations of
their model architectures. DeePMD-GNN focuses on what is useful beyond the
standalone official workflows: bringing those equivariant GNN models into the
DeePMD-kit ecosystem for training, active learning, and production molecular
simulations.

- **Unified DeePMD-kit workflow**: train and freeze MACE or NequIP models with
  `dp --pt train` and `dp --pt freeze`, using DeePMD-kit data pipelines,
  inputs, losses, and model serialization instead of maintaining a separate
  workflow for each upstream package.
- **Broader MD engine access**: run frozen GNN models through molecular dynamics
  interfaces supported by DeePMD-kit, including LAMMPS and AMBER, rather than
  relying only on the engines and plugins maintained by the official MACE or
  NequIP projects.
- **Parallel periodic simulations**: use DeePMD-kit's message-passing
  communication path for LAMMPS/MPI runs. MACE and NequIP models advertise
  their message passing to DeePMD-kit, so ghost-atom features can be exchanged
  between message-passing layers through `border_op`.
- **Active learning and QM/MM integration**: plug MACE or NequIP models into
  DP-GEN active learning loops and DeePMD-kit DPRc/AmberTools workflows through
  the same interface used by other DeePMD-kit models.
- **Deployment-oriented MACE extras**: export MACE models with the
  DeePMD-kit/LAMMPS PyTorch exportable backend (`pt_expt`), train with optional
  cuEquivariance acceleration, use DeePMD-kit's `torch.compile` path, and
  conservatively convert selected official MACE-OFF checkpoints for downstream
  validation in DeePMD-kit-supported engines.

## Credits

If you use this software, please cite the following paper:

- Jinzhe Zeng, Timothy J. Giese, Duo Zhang, Han Wang, Darrin M. York, DeePMD-GNN: A DeePMD-kit Plugin for External Graph Neural Network Potentials, _J. Chem. Inf. Model._, 2025, 65, 7, 3154-3160, DOI: [10.1021/acs.jcim.4c02441](https://doi.org/10.1021/acs.jcim.4c02441). [![Citations](https://citations.njzjz.win/10.1021/acs.jcim.4c02441)](https://badge.dimensions.ai/details/doi/10.1021/acs.jcim.4c02441)

## Installation

### Install via conda

If you are in a [conda environment](https://docs.deepmodeling.com/faq/conda.html) where DeePMD-kit is already installed from the conda-forge channel,
you can use `conda` to install the DeePMD-GNN plugin:

```sh
conda install deepmd-gnn -c conda-forge
```

### Build from source

First, clone this repository:

```sh
git clone https://gitlab.com/RutgersLBSR/deepmd-gnn
cd deepmd-gnn
```

#### Python interface plugin

Python 3.10 or above is required. A C++ compiler that supports C++ 17 is required.
NVCC is required to build CUDA OPs.

```sh
pip install .
```

Only PyTorch 2.10 or above is supported.

#### C++ interface plugin

DeePMD-kit version should be v3.0.0b4 or later.

Follow [DeePMD-kit documentation](https://docs.deepmodeling.com/projects/deepmd/en/latest/install/install-from-source.html#install-the-c-interface) to install DeePMD-kit C++ interface with PyTorch backend support and other related MD packages.
After that, you can build the plugin

```sh
# Assume libtorch has been contained in CMAKE_PREFIX_PATH
mkdir -p build
cd build
cmake .. -D CMAKE_INSTALL_PREFIX=/prefix/to/install
cmake --build . -j8
cmake --install .
```

`libdeepmd_gnn.so` will be installed into the directory you assign.
When using any DeePMD-kit C++ interface, set the following environment variable in advance:

```sh
export DP_PLUGIN_PATH=/prefix/to/install/lib/libdeepmd_gnn.so
```

## Usage

Follow [Parameters section](#parameters) to prepare a DeePMD-kit input file.

```sh
dp --pt train input.json
dp --pt freeze
```

A frozen model file named `frozen_model.pth` will be generated. You can use it in the MD packages or other interfaces.
For details, follow [DeePMD-kit documentation](https://docs.deepmodeling.com/projects/deepmd/en/latest/).

### Exporting MACE models with the PyTorch exportable backend

MACE models can also be trained and frozen with DeePMD-kit's PyTorch exportable
backend (`pt_expt`):

```sh
dp --pt-expt train input.json
dp --pt-expt freeze -o frozen_model.pt2
```

Use this path when you need a `.pt2` model for the DeePMD-kit/LAMMPS
PyTorch exportable runtime. The `pt_expt` path currently supports MACE models;
NequIP models should still use the regular `--pt` workflow.

The `pt_expt` workflow requires DeePMD-kit 3.2.0 or later. Multi-layer MACE
models include the extra communication artifact needed by LAMMPS/MPI inside the
exported `.pt2` package, so the resulting file can be passed to LAMMPS in the
same way as other DeePMD-kit frozen models.

#### Training MACE with cuEquivariance

MACE training can use cuEquivariance kernels when the installed MACE package
provides `CuEquivarianceConfig` and the cuEquivariance runtime packages are
available. Install the matching extra for the CUDA runtime used by PyTorch:

```sh
pip install "deepmd-gnn[cueq]"
```

The `cueq` extra installs the CUDA 12 cuEquivariance op package. For CUDA 11
environments, use `deepmd-gnn[cueq-cu11]` instead. When installing from a
source checkout, use `pip install ".[cueq]"` or `pip install ".[cueq-cu11]"`.

Then enable it in the `model` section:

```json
"model": {
  "type": "mace",
  "enable_cueq": true
}
```

Then train with either PyTorch backend:

```sh
dp --pt train input.json
dp --pt-expt train input.json
```

On a single RTX 5090, using the water example data with `sel = "auto"`,
`hidden_irreps = "128x0e + 128x1o"`, `batch_size = 1`, and
`disp_freq = 100`, 2000-step runs were used to sample steady-state training
speed:

| backend        | cuEquivariance | steady avg after step 300 | speedup |
| -------------- | -------------: | ------------------------: | ------: |
| `dp --pt`      |             no |             0.1955 s/step |   1.00x |
| `dp --pt`      |            yes |             0.1061 s/step |   1.84x |
| `dp --pt-expt` |             no |             0.2025 s/step |   1.00x |
| `dp --pt-expt` |            yes |             0.1007 s/step |   2.01x |

The cuEquivariance effect is similar in both backends, but not bit-for-bit
identical: in this run, the steady cuEquivariance speed differed by about 5%.
The first cuEquivariance step includes a one-time kernel initialization cost
(about 56 s in this run), which is amortized over long training jobs.

cuEquivariance accelerates training only. When freezing a checkpoint whose
input had `"enable_cueq": true`, deepmd-gnn disables cuEquivariance for the
frozen model and converts the MACE weights back to the e3nn format. This lets
both `dp --pt freeze` and `dp --pt-expt freeze` export normally, but the frozen
model does not use cuEquivariance kernels.

#### Training MACE with `torch.compile`

MACE training through the `pt_expt` backend can use DeePMD-kit's native
`torch.compile` path. Enable it in the `training` section:

```json
"training": {
  "enable_compile": true
}
```

Then run the regular exportable-backend training command:

```sh
dp --pt-expt train input.json
```

On a single RTX 5090, using the water example data with `sel = "auto"`,
`hidden_irreps = "128x0e + 128x1o"`, `batch_size = 1`, and
`disp_freq = 100`, a 2000-step run was used to sample the steady-state training
speed:

| mode            | steady avg after step 200 | speedup |
| --------------- | ------------------------: | ------: |
| eager `pt_expt` |             0.2030 s/step |   1.00x |
| `torch.compile` |             0.1495 s/step |   1.36x |

The first compiled step includes a one-time Inductor trace/compile cost
(97.17 s in this run), which is amortized over normal long training jobs.

Do not enable `enable_cueq` and `enable_compile` together for now. With
cuEquivariance enabled, DeePMD-kit's symbolic `make_fx` compile trace fails in
the cuEquivariance `uniform_1d` fake-tensor path with a data-dependent symbolic
shape guard.

### Running LAMMPS + GNN models with period boundary conditions

GNN models use message passing neural networks,
so the neighbor list built with traditional cutoff radius will not work,
since the ghost atoms also need to build neighbor list.
The MACE and NequIP models work like regular DeePMD-kit message-passing models.
No extra environment variable is needed when freezing the model.
They advertise message passing to DeePMD-kit, so LAMMPS can use the regular
neighbor list with a cutoff radius of $r_c$ and DeePMD-kit communicates
the ghost-atom features through MPI between message-passing layers.
This requires a DeePMD-kit build whose LAMMPS/PyTorch interface provides
the message-passing communication op, `border_op`.

When `border_op` communication is available, DeePMD-kit/LAMMPS uses that
MPI-capable path. The atom-map path is a serial fallback for runs that do not
receive a DeePMD message-passing `comm_dict`; it maps ghost atoms to their
corresponding real atoms on the same process and is not a substitute for MPI
communication. Request the mapping when using this fallback in LAMMPS
(also requires DeePMD-kit v3.0.0rc0 or above).

```lammps
atom_modify map array
```

## Parameters

### MACE

To use the MACE model, set `"type": "mace"` in the `model` section of the training script.
Below is default values for the MACE model, most of which follows default values in the MACE package:

```json
"model": {
  "type": "mace",
  "type_map": [
    "O",
    "H"
  ],
  "r_max": 5.0,
  "sel": "auto",
  "num_radial_basis": 8,
  "num_cutoff_basis": 5,
  "max_ell": 3,
  "interaction": "RealAgnosticResidualInteractionBlock",
  "num_interactions": 2,
  "hidden_irreps": "128x0e + 128x1o",
  "pair_repulsion": false,
  "distance_transform": "None",
  "correlation": 3,
  "gate": "silu",
  "MLP_irreps": "16x0e",
  "radial_type": "bessel",
  "radial_MLP": [64, 64, 64],
  "std": 1.0,
  "precision": "float32",
  "enable_cueq": false
}
```

### NequIP

```json
"model": {
  "type": "nequip",
  "type_map": [
    "O",
    "H"
  ],
  "r_max": 5.0,
  "sel": "auto",
  "num_layers": 4,
  "l_max": 2,
  "num_features": 32,
  "nonlinearity_type": "gate",
  "parity": true,
  "num_basis": 8,
  "BesselBasis_trainable": true,
  "PolynomialCutoff_p": 6,
  "invariant_layers": 2,
  "invariant_neurons": 64,
  "use_sc": true,
  "irreps_edge_sh": "0e + 1e",
  "feature_irreps_hidden": "32x0o + 32x0e + 32x1o + 32x1e",
  "chemical_embedding_irreps_out": "32x0e",
  "conv_to_output_hidden_irreps_out": "16x0e",
  "precision": "float32"
}
```

## DPRc support

In `deepmd-gnn`, the GNN model can be used in a [DPRc](https://docs.deepmodeling.com/projects/deepmd/en/latest/model/dprc.html) way.
Type maps that starts with `m` (such as `mH`) or `OW` or `HW` will be recognized as MM types.
Two MM atoms will not build edges with each other.
Such GNN+DPRc model can be directly used in AmberTools24.

## Conservative MACE-OFF checkpoint loading

`deepmd_gnn.mace_off` provides a conservative bridge from selected official
MACE-OFF checkpoints into DeePMD-GNN. The goal is to recover the parts that are
really stored in the checkpoint while avoiding guesses about wrapper-only
runtime semantics.

In practice, this path intentionally does less inference than the broader
DPRc/QM/MM machinery:

- it infers ordinary element `type_map` entries from checkpoint
  `atomic_numbers`
- it restores checkpoint-side model semantics such as `avg_num_neighbors` from
  the original `ScaleShiftMACE` object instead of reusing DeePMD's runtime
  neighbor cap
- it requires an explicit `sel` value, because `sel` is a DeePMD runtime
  neighbor-list cap and is not stored in the MACE-OFF checkpoint
- it does **not** infer DPRc/MM labels such as `mH`, `mC`, `HW`, or `OW`
- `load_mace_off_model()` keeps the original eager MACE checkpoint model inside
  the wrapper for closer native/wrapper parity, while
  `convert_mace_off_to_deepmd()` scripts the model only at export time
- it uses `torch.load(..., weights_only=False)` to recover the original
  `ScaleShiftMACE` object, so callers should only use trusted checkpoints

Example:

```sh
deepmd-gnn mace-off convert mace_off23_small_dp.pt --model off23_small --sel 64
```

This produces a scripted DeePMD-GNN wrapper. That serialization step is useful,
but it is still a good idea to validate the final downstream workflow you care
about (for example in LAMMPS or AMBER).

## Examples

- [examples/water](examples/water)
- [examples/dprc](examples/dprc)
