enable use of CVs defined by PyTorch neural network models #570
enable use of CVs defined by PyTorch neural network models #570giacomofiorin merged 109 commits intomasterfrom
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
The following is a list of pull requests relevant to LAMMPS in the Colvars repository since 2024-08-06: - 752 New tool poisson_integrator_conv Colvars/colvars#752 (@jhenin) - 733 Custom grids for all biases Colvars/colvars#733 (@giacomofiorin, @jhenin) - 776 Avoid error in acos and asin with fast-math Colvars/colvars#776 (@jhenin) - 773 fix: fix the clang build test failure of OPES Colvars/colvars#773 (@HanatoK) - 768 fix: clamp the input values of asin and acos in case of fast math on aarch64 Colvars/colvars#768 (@HanatoK) - 761 Add debug code for the Jacobi failure Colvars/colvars#761 (@HanatoK) - 759 min_image fix; Saves long runs from crashes; Colvars/colvars#759 (@PolyachenkoYA) - 757 Fix MSVC OpenMP issue Colvars/colvars#757 (@HanatoK) - 755 Fix indentation of 'Init CVC' message in standard output Colvars/colvars#755 (@jhenin) - 750 Optimize and simplify the calculation of dihedral gradients Colvars/colvars#750 (@HanatoK) - 749 Add references to new Colvars paper Colvars/colvars#749 (@jhenin, @giacomofiorin) - 740 Report the specific C++ standard at init time, stop warning about C++97/03 Colvars/colvars#740 (@giacomofiorin) - 731 Improve detection of hard/mathematical boundaries Colvars/colvars#731 (@giacomofiorin) - 729 Optimize the fit gradients Colvars/colvars#729 (@HanatoK, @jhenin) - 728 Fix undefined behavior when getting the current working directory from std::filesystem Colvars/colvars#728 (@giacomofiorin) - 727 Add patchversion scripting command Colvars/colvars#727 (@giacomofiorin) - 724 Fix gradients and metric functions of distanceDir Colvars/colvars#724 (@giacomofiorin) - 715 Add missing rotation in orientation component Colvars/colvars#715 (@giacomofiorin) - 713 fix: try to solve lammps#87 for non-scala components Colvars/colvars#713 (@HanatoK) - 709 Implementation of OPES in Colvars Colvars/colvars#709 (@HanatoK, @giacomofiorin, @jhenin) - 706 BUGFIX for Segmentation fault in colvarbias_meta::calc_energy() with useGrids off Colvars/colvars#706 (@alphataubio) - 570 enable use of CVs defined by PyTorch neural network models Colvars/colvars#570 (@zwpku, @giacomofiorin, @HanatoK, @jhenin) Authors: @alphataubio, @EzryStIago, @giacomofiorin, @HanatoK, @jhenin, @PolyachenkoYA, @zwpku
The following is a list of pull requests relevant to LAMMPS in the Colvars repository since 2024-08-06: - 752 New tool poisson_integrator_conv Colvars/colvars#752 (@jhenin) - 733 Custom grids for all biases Colvars/colvars#733 (@giacomofiorin, @jhenin) - 776 Avoid error in acos and asin with fast-math Colvars/colvars#776 (@jhenin) - 773 fix: fix the clang build test failure of OPES Colvars/colvars#773 (@HanatoK) - 768 fix: clamp the input values of asin and acos in case of fast math on aarch64 Colvars/colvars#768 (@HanatoK) - 761 Add debug code for the Jacobi failure Colvars/colvars#761 (@HanatoK) - 759 min_image fix; Saves long runs from crashes; Colvars/colvars#759 (@PolyachenkoYA) - 757 Fix MSVC OpenMP issue Colvars/colvars#757 (@HanatoK) - 755 Fix indentation of 'Init CVC' message in standard output Colvars/colvars#755 (@jhenin) - 750 Optimize and simplify the calculation of dihedral gradients Colvars/colvars#750 (@HanatoK) - 749 Add references to new Colvars paper Colvars/colvars#749 (@jhenin, @giacomofiorin) - 740 Report the specific C++ standard at init time, stop warning about C++97/03 Colvars/colvars#740 (@giacomofiorin) - 731 Improve detection of hard/mathematical boundaries Colvars/colvars#731 (@giacomofiorin) - 729 Optimize the fit gradients Colvars/colvars#729 (@HanatoK, @jhenin) - 728 Fix undefined behavior when getting the current working directory from std::filesystem Colvars/colvars#728 (@giacomofiorin) - 727 Add patchversion scripting command Colvars/colvars#727 (@giacomofiorin) - 724 Fix gradients and metric functions of distanceDir Colvars/colvars#724 (@giacomofiorin) - 715 Add missing rotation in orientation component Colvars/colvars#715 (@giacomofiorin) - 713 fix: try to solve #87 for non-scala components Colvars/colvars#713 (@HanatoK) - 709 Implementation of OPES in Colvars Colvars/colvars#709 (@HanatoK, @giacomofiorin, @jhenin) - 706 BUGFIX for Segmentation fault in colvarbias_meta::calc_energy() with useGrids off Colvars/colvars#706 (@alphataubio) - 570 enable use of CVs defined by PyTorch neural network models Colvars/colvars#570 (@zwpku, @giacomofiorin, @HanatoK, @jhenin) Authors: @alphataubio, @EzryStIago, @giacomofiorin, @HanatoK, @jhenin, @PolyachenkoYA, @zwpku
The following is a list of pull requests relevant to LAMMPS in the Colvars repository since 2024-08-06: - 752 New tool poisson_integrator_conv Colvars/colvars#752 (@jhenin) - 733 Custom grids for all biases Colvars/colvars#733 (@giacomofiorin, @jhenin) - 776 Avoid error in acos and asin with fast-math Colvars/colvars#776 (@jhenin) - 773 fix: fix the clang build test failure of OPES Colvars/colvars#773 (@HanatoK) - 768 fix: clamp the input values of asin and acos in case of fast math on aarch64 Colvars/colvars#768 (@HanatoK) - 761 Add debug code for the Jacobi failure Colvars/colvars#761 (@HanatoK) - 759 min_image fix; Saves long runs from crashes; Colvars/colvars#759 (@PolyachenkoYA) - 757 Fix MSVC OpenMP issue Colvars/colvars#757 (@HanatoK) - 755 Fix indentation of 'Init CVC' message in standard output Colvars/colvars#755 (@jhenin) - 750 Optimize and simplify the calculation of dihedral gradients Colvars/colvars#750 (@HanatoK) - 749 Add references to new Colvars paper Colvars/colvars#749 (@jhenin, @giacomofiorin) - 740 Report the specific C++ standard at init time, stop warning about C++97/03 Colvars/colvars#740 (@giacomofiorin) - 731 Improve detection of hard/mathematical boundaries Colvars/colvars#731 (@giacomofiorin) - 729 Optimize the fit gradients Colvars/colvars#729 (@HanatoK, @jhenin) - 728 Fix undefined behavior when getting the current working directory from std::filesystem Colvars/colvars#728 (@giacomofiorin) - 727 Add patchversion scripting command Colvars/colvars#727 (@giacomofiorin) - 724 Fix gradients and metric functions of distanceDir Colvars/colvars#724 (@giacomofiorin) - 715 Add missing rotation in orientation component Colvars/colvars#715 (@giacomofiorin) - 713 fix: try to solve lammps#87 for non-scala components Colvars/colvars#713 (@HanatoK) - 709 Implementation of OPES in Colvars Colvars/colvars#709 (@HanatoK, @giacomofiorin, @jhenin) - 706 BUGFIX for Segmentation fault in colvarbias_meta::calc_energy() with useGrids off Colvars/colvars#706 (@alphataubio) - 570 enable use of CVs defined by PyTorch neural network models Colvars/colvars#570 (@zwpku, @giacomofiorin, @HanatoK, @jhenin) Authors: @alphataubio, @EzryStIago, @giacomofiorin, @HanatoK, @jhenin, @PolyachenkoYA, @zwpku
The following is a list of pull requests relevant to LAMMPS in the Colvars repository since 2024-08-06: - 752 New tool poisson_integrator_conv Colvars/colvars#752 (@jhenin) - 733 Custom grids for all biases Colvars/colvars#733 (@giacomofiorin, @jhenin) - 776 Avoid error in acos and asin with fast-math Colvars/colvars#776 (@jhenin) - 773 fix: fix the clang build test failure of OPES Colvars/colvars#773 (@HanatoK) - 768 fix: clamp the input values of asin and acos in case of fast math on aarch64 Colvars/colvars#768 (@HanatoK) - 761 Add debug code for the Jacobi failure Colvars/colvars#761 (@HanatoK) - 759 min_image fix; Saves long runs from crashes; Colvars/colvars#759 (@PolyachenkoYA) - 757 Fix MSVC OpenMP issue Colvars/colvars#757 (@HanatoK) - 755 Fix indentation of 'Init CVC' message in standard output Colvars/colvars#755 (@jhenin) - 750 Optimize and simplify the calculation of dihedral gradients Colvars/colvars#750 (@HanatoK) - 749 Add references to new Colvars paper Colvars/colvars#749 (@jhenin, @giacomofiorin) - 740 Report the specific C++ standard at init time, stop warning about C++97/03 Colvars/colvars#740 (@giacomofiorin) - 731 Improve detection of hard/mathematical boundaries Colvars/colvars#731 (@giacomofiorin) - 729 Optimize the fit gradients Colvars/colvars#729 (@HanatoK, @jhenin) - 728 Fix undefined behavior when getting the current working directory from std::filesystem Colvars/colvars#728 (@giacomofiorin) - 727 Add patchversion scripting command Colvars/colvars#727 (@giacomofiorin) - 724 Fix gradients and metric functions of distanceDir Colvars/colvars#724 (@giacomofiorin) - 715 Add missing rotation in orientation component Colvars/colvars#715 (@giacomofiorin) - 713 fix: try to solve lammps#87 for non-scala components Colvars/colvars#713 (@HanatoK) - 709 Implementation of OPES in Colvars Colvars/colvars#709 (@HanatoK, @giacomofiorin, @jhenin) - 706 BUGFIX for Segmentation fault in colvarbias_meta::calc_energy() with useGrids off Colvars/colvars#706 (@alphataubio) - 570 enable use of CVs defined by PyTorch neural network models Colvars/colvars#570 (@zwpku, @giacomofiorin, @HanatoK, @jhenin) Authors: @alphataubio, @EzryStIago, @giacomofiorin, @HanatoK, @jhenin, @PolyachenkoYA, @zwpku
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
torchannclass. 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.