-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enable use of CVs defined by PyTorch neural network models #570
Conversation
Torchann1
The latest GROMACS test error is unrelated to Colvars: https://gitlab.com/gromacs/gromacs/-/issues/5204 |
Hi there! GROMACS 2025 runs without errors the See the outputs here: |
@giacomofiorin thanks for the work! |
@zwpku Yes, the reference files currently in that folder were produced came from another build, with a different version of libTorch. Would you expect this kind of difference? It is small, but it did exceed our threshold (1.0e-6 relative error). |
@giacomofiorin If I see correctly, the torch model in that test is simply the identity map and the CV is a dihedral angle. So I expect there should be little difference due to different versions of libTorch. Could it also be caused by some changes in the source code or in the config files of that test? I can try to build and examine the test on my local machine. |
@zwpku Yes, if you could please check as soon as possible that would be very helpful! We may have a chance to convince the GROMACS people to include this in the 2025 release, but timing is very tight. |
Updated reference files accordingly.
Still getting deviations from the reference files that you just uploaded in the CI tests. It is probably due to differences in libTorch versions. The one in the container is 2.4: colvars/devel-tools/containers/CentOS9-devel.def Lines 65 to 68 in 4651a59
Is the |
@giacomofiorin I guess the original deviation you encountered was (partially) due to random seed. The seed was fixed in gromacs/tests/library/Common/test.mdp in a previous commit by @jhenin, but somehow it was reverted again, possibly due to a merge with master. I tried libtorch 2.0.1, 2.3.0, and 2.4.1, and the results are the same. The |
@giacomofiorin besides, I saw some differences (e.g. in colvar.cpp, colvarmodule.cpp, colvarmodule_refs.h) when I ran |
Yes. I just did that. If the tests pass (thank you for addressing the GROMACS precision issue!) we can proceed to merge this PR into |
PR merged! Thanks so much @zwpku and everyone who helped getting this PR done! |
FYI Lukas Mullender from the GROMACS team raised a couple of comments on the code regarding the use of GPU models and precision: |
This MR includes small fixes and improvements to the copy of the Colvars library in `src/external`, as well as one feature (the `torchANN` collective variable type). The Torch-Colvars interface was previously not included in !4611 because of failures in the Colvars CI runners. We have since confirmed with the main author of the feature that the culprit was the precision of the GROMACS build and he confirmed that the numerical results are consistent across libTorch versions (see Colvars/colvars#570 (comment)). Matches [this commit in the Colvars repo](Colvars/colvars@3023d8e). CC @HubLot @jhenin
This branch implements a class called
torchANN
, which allows to define cv components by loading pretrained PyTorch neural network models.Installation Steps
Download LibTorch. This package is required in order to enable the
torchann
class. First, download the code and unzip it.In this way, the library is uncompressed under the current directory. Let's say it is located at /path/to/libtorch.
Patch MD engine. This step is done as usual using the script update-colvars-code.sh. Enter the source code of Colvars package, and run:
Compilation. This step depends on the engine to be compiled.
NAMD: add "--with-colvars-torch --torch-prefix path/to/libtorch" to the argument of ./config
Assume packages that are required to build NAMD, e.g. charm, tcl/tcl-threaded, are already prepared.
Then, one can compile the NAMD package with the following commands:
An example of the command is:
and set the variable Torch_DIR in the file CMakeCache.txt. When a cpu version of libtorch library is used, it may
also be necessary to set MKL path to empty:
Alternatively, one could combine these steps in one command:
After that, run make and make install to compile and install the package.
The class has only been tested using simple neural network models (i.e. an autoencoder on alanine dipeptide), under NAMD and GROMACS engines. Feedbacks are welcome!
A (trivial) example
This Python script simply creates a model which is an identity map and save it to a file named identity.pt.
This file defines two CVs using torchann class taking other cv components (here dihedral angles) as inputs.