-
Notifications
You must be signed in to change notification settings - Fork 59
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
base: master
Are you sure you want to change the base?
Conversation
Torchann1
Two items would help here:
|
There are two ways: (i) Colvars provides a mechanism to load library dynamically, just like the |
Of interest here, a libtorch interface is making its way into the Gromacs build system: https://gitlab.com/gromacs/gromacs/-/merge_requests/4551 |
Also allow torchANN components to be declared as periodic to fix the regtest
@zwpku I have made changes to enable building with Gromacs 2024. Had you tested that? |
@zwpku Did you mean to close this PR? |
@jhenin @HanatoK I don't know whether I understood dynamical loading correctly. One issue to me is that, in order to be able to call functions in the loaded library, one has to declare in the C++ source code of the library with extern "C" so that the names are not mangled when creating the .so file. I found many such declarations in plumed, but only a few in libtorch. So I not sure whether this is supported by libtorch. Also, it would be good to have a compatible solution with the development of libtorch interface in Gromacs. |
Sorry, I clicked the wrong button. |
You don't have to declare everything in
If you have access to the NAMD Gitlab repository, you can have a look with the proposed PytorchForces implementation that also uses the dynamic loading: |
@HanatoK Thanks! So this means that we compile colvarcomp_torchann class together with libtorch to build a dynamic library, say libcovarcomp_torchann.so. Then Colvars loads this library at runtime? |
Yes. That is what I mean. libcovarcomp_torchann.so is linked to libtorch.so (either dynamic or static linking), and Colvars loads libcovarcomp_torchann.so dynamically. I am not sure if Colvars maintainers agree with this idea, though. |
This is actually a question very easy to answer (especially compared to others that you asked recently on this repo 😉). The only arrangements that we have ever followed are set by the maintainers of the packages that Colvars is distributed with. In this specific case, if GROMACS is considering to link libTorch (see above), IMO Colvars should link it in the same manner. That would be statically, dynamically, etc. @HanatoK have you discussed with your colleagues in the NAMD team about how to package libTorch with NAMD? It would be awesome if it could be packaged statically as you do already for the NVIDIA libraries, FFTW, etc |
But it seems @jhenin wants to dynamically load libtorch, not dynamically or statically link to it. I also think that dynamic loading appears to be the most general and flexible solution since it does not require the MD engine to link to libtorch.
We are not going to package or redistribute libtorch, because we want to use libtorch with CUDA, and libtorch may be compiled with different CUDA architectures. More specifically, I plan to distribute the PytorchForces and NAMD headers in its source code form in the NAMD binary tarball, and let the users download and compile libtorch following their official instruction, and then compile the binary plugin of PytorchForces linking with libtorch. |
@jhenin's suggestion was meant to avoid maintaining patched versions of the build recipes for each engine, which this branch contains right now. Loading would allow the use of a precompiled libTorch in the CI jobs, i.e. loading would be preferable for the specific purpose of running tests. As far as distribution goes, as long as the user is required to build dependencies there is no perfect approach 😄 |
I did mention dynamic loading. However, linking at build time should be supported when possible. In Gromacs and LAMMPS, we have the benefit of a separate CMake file, so there we can relatively easily detect if torch is available at build time - especially if we coordinate with the Gromacs folks. In NAMD, it's more intrusive, so I don't know how easily the patch to the config script will be accepted. The build system of VMD is its own beast, and distribution is a problem, so dynamic loading makes more sense there. |
Do you want to support both dynamic loading and direct linkage? That would make the code more complicated and I cannot see the benefit of using both. You would need a lot of ifdefs to do that. For dynamic loading, if GROMACS is linked to libtorch, then |
@jhenin I tried to patch Gromacs 2024 with this branch. It seems that Gromacs 2024 is viewed as GROMACS-DEV in |
Had to make separate patch for the mdmodules version
Thanks @zwpku , the patch was not in the right place. |
The current failures to build gromacs in regtests are due to the lack of downstream changes. Merging master should help. |
@jhenin It looks like you have committed directly onto |
@@ -38,6 +38,19 @@ gmx_option_multichoice(GMX_USE_COLVARS | |||
INTERNAL NONE) | |||
mark_as_advanced(GMX_USE_COLVARS) | |||
|
|||
function(gmx_set_colvars_torch) | |||
find_package(Torch) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: This line should be removed for GMX 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.