Skip to main content

TorchMD-NET provides state-of-the-art neural networks potentials for biomolecular systems

Project description

Code style: black CI Documentation Status

TorchMD-NET

TorchMD-NET provides state-of-the-art neural networks potentials (NNPs) and a mechanism to train them. It offers efficient and fast implementations if several NNPs and it is integrated in GPU-accelerated molecular dynamics code like ACEMD, OpenMM and TorchMD. TorchMD-NET exposes its NNPs as PyTorch modules.

Documentation

Documentation is available at https://torchmd-net.readthedocs.io

Available architectures

Installation

TorchMD-Net is available as a pip installable wheel as well as in conda-forge

TorchMD-Net provides builds for CPU-only, CUDA 11.8 and CUDA 12.4. CPU versions are only provided as reference, as the performance will be extremely limited. Depending on which variant you wish to install, you can install it with one of the following commands:

# The following will install the CUDA 12.4 version by default
pip install torchmd-net 
# The following will install the CUDA 11.8 version
pip install torchmd-net --extra-index-url https://download.pytorch.org/whl/cu118 --extra-index-url https://us-central1-python.pkg.dev/pypi-packages-455608/cu118/simple
# The following will install the CUDA 12.4 version
pip install torchmd-net --extra-index-url https://download.pytorch.org/whl/cu124 --extra-index-url https://us-central1-python.pkg.dev/pypi-packages-455608/cu124/simple
# The following will install the CPU only version (not recommended)
pip install torchmd-net --extra-index-url https://download.pytorch.org/whl/cpu --extra-index-url https://us-central1-python.pkg.dev/pypi-packages-455608/cpu/simple   

Alternatively it can be installed with conda or mamba with one of the following commands. We recommend using Miniforge instead of anaconda.

mamba install torchmd-net cuda-version=11.8
mamba install torchmd-net cuda-version=12.4

Install from source

TorchMD-Net is installed using pip, but you will need to install some dependencies before. Check this documentation page.

Usage

Specifying training arguments can either be done via a configuration yaml file or through command line arguments directly. Several examples of architectural and training specifications for some models and datasets can be found in examples/. Note that if a parameter is present both in the yaml file and the command line, the command line version takes precedence. GPUs can be selected by setting the CUDA_VISIBLE_DEVICES environment variable. Otherwise, the argument --ngpus can be used to select the number of GPUs to train on (-1, the default, uses all available GPUs or the ones specified in CUDA_VISIBLE_DEVICES). Keep in mind that the GPU ID reported by nvidia-smi might not be the same as the one CUDA_VISIBLE_DEVICES uses.
For example, to train the Equivariant Transformer on the QM9 dataset with the architectural and training hyperparameters described in the paper, one can run:

mkdir output
CUDA_VISIBLE_DEVICES=0 torchmd-train --conf torchmd-net/examples/ET-QM9.yaml --log-dir output/

Run torchmd-train --help to see all available options and their descriptions.

Pretrained models

See here for instructions on how to load pretrained models.

Creating a new dataset

If you want to train on custom data, first have a look at torchmdnet.datasets.Custom, which provides functionalities for loading a NumPy dataset consisting of atom types and coordinates, as well as energies, forces or both as the labels. Alternatively, you can implement a custom class according to the torch-geometric way of implementing a dataset. That is, derive the Dataset or InMemoryDataset class and implement the necessary functions (more info here). The dataset must return torch-geometric Data objects, containing at least the keys z (atom types) and pos (atomic coordinates), as well as y (label), neg_dy (negative derivative of the label w.r.t atom coordinates) or both.

Custom prior models

In addition to implementing a custom dataset class, it is also possible to add a custom prior model to the model. This can be done by implementing a new prior model class in torchmdnet.priors and adding the argument --prior-model <PriorModelName>. As an example, have a look at torchmdnet.priors.Atomref.

Multi-Node Training

In order to train models on multiple nodes some environment variables have to be set, which provide all necessary information to PyTorch Lightning. In the following we provide an example bash script to start training on two machines with two GPUs each. The script has to be started once on each node. Once torchmd-train is started on all nodes, a network connection between the nodes will be established using NCCL.

In addition to the environment variables the argument --num-nodes has to be specified with the number of nodes involved during training.

export NODE_RANK=0
export MASTER_ADDR=hostname1
export MASTER_PORT=12910

mkdir -p output
CUDA_VISIBLE_DEVICES=0,1 torchmd-train --conf torchmd-net/examples/ET-QM9.yaml.yaml --num-nodes 2 --log-dir output/
  • NODE_RANK : Integer indicating the node index. Must be 0 for the main node and incremented by one for each additional node.
  • MASTER_ADDR : Hostname or IP address of the main node. The same for all involved nodes.
  • MASTER_PORT : A free network port for communication between nodes. PyTorch Lightning suggests port 12910 as a default.

Known Limitations

  • Due to the way PyTorch Lightning calculates the number of required DDP processes, all nodes must use the same number of GPUs. Otherwise training will not start or crash.
  • We observe a 50x decrease in performance when mixing nodes with different GPU architectures (tested with RTX 2080 Ti and RTX 3090).
  • Some CUDA systems might hang during a multi-GPU parallel training. Try export NCCL_P2P_DISABLE=1, which disables direct peer to peer GPU communication.

Cite

If you use TorchMD-NET in your research, please cite the following papers:

Main reference

@misc{pelaez2024torchmdnet,
title={TorchMD-Net 2.0: Fast Neural Network Potentials for Molecular Simulations}, 
author={Raul P. Pelaez and Guillem Simeon and Raimondas Galvelis and Antonio Mirarchi and Peter Eastman and Stefan Doerr and Philipp Thölke and Thomas E. Markland and Gianni De Fabritiis},
year={2024},
eprint={2402.17660},
archivePrefix={arXiv},
primaryClass={cs.LG}
}

TensorNet

@inproceedings{simeon2023tensornet,
title={TensorNet: Cartesian Tensor Representations for Efficient Learning of Molecular Potentials},
author={Guillem Simeon and Gianni De Fabritiis},
booktitle={Thirty-seventh Conference on Neural Information Processing Systems},
year={2023},
url={https://openreview.net/forum?id=BEHlPdBZ2e}
}

Equivariant Transformer

@inproceedings{
tholke2021equivariant,
title={Equivariant Transformers for Neural Network based Molecular Potentials},
author={Philipp Th{\"o}lke and Gianni De Fabritiis},
booktitle={International Conference on Learning Representations},
year={2022},
url={https://openreview.net/forum?id=zNHzqZ9wrRB}
}

Graph Network

@article{Majewski2023,
  title = {Machine learning coarse-grained potentials of protein thermodynamics},
  volume = {14},
  ISSN = {2041-1723},
  url = {http://dx.doi.org/10.1038/s41467-023-41343-1},
  DOI = {10.1038/s41467-023-41343-1},
  number = {1},
  journal = {Nature Communications},
  publisher = {Springer Science and Business Media LLC},
  author = {Majewski,  Maciej and Pérez,  Adrià and Th\"{o}lke,  Philipp and Doerr,  Stefan and Charron,  Nicholas E. and Giorgino,  Toni and Husic,  Brooke E. and Clementi,  Cecilia and Noé,  Frank and De Fabritiis,  Gianni},
  year = {2023},
  month = sep 
}

Developer guide

Implementing a new architecture

To implement a new architecture, you need to follow these steps:
1. Create a new class in torchmdnet.models that inherits from torch.nn.Model. Follow TorchMD_ET as a template. This is a minimum implementation of a model:

class MyModule(nn.Module):
  def __init__(self, parameter1, parameter2):
	super(MyModule, self).__init__()
	# Define your model here
	self.layer1 = nn.Linear(10, 10)
	...
	# Initialize your model parameters here
	self.reset_parameters()

    def reset_parameters(self):
      # Initialize your model parameters here
	  nn.init.xavier_uniform_(self.layer1.weight)
	...
	
  def forward(self,
        z: Tensor, # Atomic numbers, shape (n_atoms, 1)
        pos: Tensor, # Atomic positions, shape (n_atoms, 3)
        batch: Tensor, # Batch vector, shape (n_atoms, 1). All atoms in the same molecule have the same value and are contiguous.
        q: Optional[Tensor] = None, # Atomic charges, shape (n_atoms, 1)
        s: Optional[Tensor] = None, # Atomic spins, shape (n_atoms, 1)
    ) -> Tuple[Tensor, Tensor, Tensor, Tensor, Tensor]:
	# Define your forward pass here
	scalar_features = ...
	vector_features = ...
	# Return the scalar and vector features, as well as the atomic numbers, positions and batch vector
	return scalar_features, vector_features, z, pos, batch

2. Add the model to the __all__ list in torchmdnet.models.__init__.py. This will make the tests pick your model up.
3. Tell models.model.create_model how to initialize your module by adding a new entry, for instance:

    elif args["model"] == "mymodule":
       from torchmdnet.models.torchmd_mymodule import MyModule
       is_equivariant = False # Set to True if your model is equivariant
       representation_model = MyModule(
           parameter1=args["parameter1"],
           parameter2=args["parameter2"],
           **shared_args, # Arguments typically shared by all models
       )

4. Add any new parameters required to initialize your module to scripts.train.get_args. For instance:

  parser.add_argument('--parameter1', type=int, default=32, help='Parameter1 required by MyModule')
  ...

5. Add an example configuration file to torchmd-net/examples that uses your model.
6. Make tests use your configuration file by adding a case to tests.utils.load_example_args. For instance:

if model_name == "mymodule":
       config_file = join(dirname(dirname(__file__)), "examples", "MyModule-QM9.yaml")

At this point, if your module is missing some feature the tests will let you know, and you can add it. If you add a new feature to the package, please add a test for it.

Code style

We use black. Please run black on your modified files before committing.

Testing

To run the tests, install the package and run pytest in the root directory of the repository. Tests are a good source of knowledge on how to use the different components of the package.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

torchmd_net_cu11-2.4.12-cp313-cp313-win_amd64.whl (612.4 kB view details)

Uploaded CPython 3.13Windows x86-64

torchmd_net_cu11-2.4.12-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (5.5 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

torchmd_net_cu11-2.4.12-cp312-cp312-win_amd64.whl (612.4 kB view details)

Uploaded CPython 3.12Windows x86-64

torchmd_net_cu11-2.4.12-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (5.5 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

torchmd_net_cu11-2.4.12-cp311-cp311-win_amd64.whl (612.4 kB view details)

Uploaded CPython 3.11Windows x86-64

torchmd_net_cu11-2.4.12-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (5.5 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

torchmd_net_cu11-2.4.12-cp310-cp310-win_amd64.whl (612.4 kB view details)

Uploaded CPython 3.10Windows x86-64

torchmd_net_cu11-2.4.12-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (5.5 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

torchmd_net_cu11-2.4.12-cp39-cp39-win_amd64.whl (612.4 kB view details)

Uploaded CPython 3.9Windows x86-64

torchmd_net_cu11-2.4.12-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (5.5 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

File details

Details for the file torchmd_net_cu11-2.4.12-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for torchmd_net_cu11-2.4.12-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 208be43f9d2d9066d37843835e7e30b3c00a30a0452f77d9f9bd5d04ab4546d3
MD5 2951c521d3e50cb0a705a8f438060462
BLAKE2b-256 8e8860787d6777ffe7c94afeb34e8e81ee831f9c6148fe90d2d0f419624a39d7

See more details on using hashes here.

File details

Details for the file torchmd_net_cu11-2.4.12-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for torchmd_net_cu11-2.4.12-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 acdb9db0a7eeadf50fedef29b1734983b5f5d37e0698bd78d5094bbbc20f0709
MD5 e75ce1b24582b773583765a077155a84
BLAKE2b-256 e3118f95b30fe7f7b8fa72fbda7db63d380ab1d4e18bed9b92b185a542b4c609

See more details on using hashes here.

File details

Details for the file torchmd_net_cu11-2.4.12-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for torchmd_net_cu11-2.4.12-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 e413058eab334ab17fe4cbae056c4344ebc886701bc8c69d32fe4b80424be813
MD5 cf4ea166d04c9d7273f186d0fe2a0f49
BLAKE2b-256 efce8b9d17073e539694ef25dd2fe903afbbf549e7d5f3a5987c24a33a118ce6

See more details on using hashes here.

File details

Details for the file torchmd_net_cu11-2.4.12-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for torchmd_net_cu11-2.4.12-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 92bdfd5df5404430ac70436441d68787ebcea2b2dae6d47fb3bd27d4294158b9
MD5 a6fb4c27889e37c76407f53b16855db2
BLAKE2b-256 1cebf5ead91590ca9d32ac7db95a5a8a06f189138f09212138d75f75e14a2f33

See more details on using hashes here.

File details

Details for the file torchmd_net_cu11-2.4.12-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for torchmd_net_cu11-2.4.12-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c2d0da085d9187a40cc3f4886f75c2ed37a0005054a401f4b1d7c0fa8afb4ac9
MD5 a0a57281cb207c721fb4cb84a582a4fa
BLAKE2b-256 7095a34b74802a6c3fed6184565677a794836ec6ecf1927a763d4e4ffdb269ef

See more details on using hashes here.

File details

Details for the file torchmd_net_cu11-2.4.12-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for torchmd_net_cu11-2.4.12-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d8c1fb18bd164f22403b39dbd130a5561c3154f34cb4bcd938093c2a91ad1dd5
MD5 f9f1bf4ef48a87de34242abf715a8af1
BLAKE2b-256 fb1b7ea1cfb0305a660c6da776a9838c817f6fb1d9fe432027a8c3ee5c6faae2

See more details on using hashes here.

File details

Details for the file torchmd_net_cu11-2.4.12-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for torchmd_net_cu11-2.4.12-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 52b6d6b4af561db83372d211a90a38848e19dd5ab50ba8d31a62442948718a33
MD5 f99387639e9d66fb1d6d08d800ac3e94
BLAKE2b-256 d9a423a3395f2e9534c8dc9ab9b3dd7c414fca7dab6eea107d96af6436594f8f

See more details on using hashes here.

File details

Details for the file torchmd_net_cu11-2.4.12-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for torchmd_net_cu11-2.4.12-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f4ab395ee5c17417afaee332c1c91b09febbeef926d12176e81fef52df46a481
MD5 ddca5bbcbb5a388b7a14035bf91d9309
BLAKE2b-256 be66013a7612fe365fb69551aa9bd4f35ca4096b6bc21fe86465a4c62e7edc27

See more details on using hashes here.

File details

Details for the file torchmd_net_cu11-2.4.12-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for torchmd_net_cu11-2.4.12-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 5b5bb195f5ec71eb66ac3585dff6b6ce7dd42dc6a5130aef4740426201b2684f
MD5 37aa256b15904d794c5a2a485b371472
BLAKE2b-256 5ca22f601c08bf5aa9d46f270142e5d3b85b203e9621740a2a7b25c7d4c4cfec

See more details on using hashes here.

File details

Details for the file torchmd_net_cu11-2.4.12-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for torchmd_net_cu11-2.4.12-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 97883d426003f82babd93aa0d2c5a4ad7ccf34f6adcefbac8424238063d8fd72
MD5 fb449fed6fa428cc1b31924d8b0c6f7e
BLAKE2b-256 a139a85315e51bff723c9339de6ec0524be696bf530524e36da03bdff48afe27

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page