-
Notifications
You must be signed in to change notification settings - Fork 56
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
0c0ee70
to
9a72e53
Compare
@zwpku Thank you for handling merge conflicts coming from To move forward, can you provide a list of commands to install libTorch, so that the feature can be included in the automated tests? I can help integrate them with the GitHub Actions workflows and the hodgepodge of testing scripts in this repo if needed. libTorch should ideally be installed from a precompiled package, unless compilation from source can proceed in a reasonable time (e.g. less than 30 minutes) when no cache is present. |
Hi @zwpku I'd like to move ahead with #643, which will create some merge conflicts with this branch. To avoid that, it may be better to merge this branch first, if you think it's ready for review. Do you have the PyTorch installation instructions suitable for reproducing the results of the tests that you added? |
Hi @giacomofiorin , sorry for being inactive for a while. Once downloaded, Libtorch can be directly linked against when compiling patched MD engines. However, when I tried to repeat all the steps (from downloading to testing) today, some errors were reported during the compiling of engines (both NAMD and Gromacs). I will resolve the problem tomorrow and let you know the steps. |
Hi @giacomofiorin, I updated this branch with master and included the list of commands for installation at the beginning of this page. On my local machine, the installation works for both NAMD 3.0b3 and gromacs-2023.2. It seems that older versions of NAMD (e.g. versions from year 2022) are no longer supported? Let me know if any further information about the installation is needed. |
Thanks for the quick reply! NAMD 2.x (i.e. the |
@zwpku We have now a small conflict in the GROMACS 2023 patch due to a change in Would you have any strong objections against supporting GROMACS >= 2024.0 and only those for this feature? (I can make that change) |
@giacomofiorin, sure, it is fine to only support Gromacs >=2024 for this feature. |
@HanatoK @giacomofiorin I wonder whether one can implement similar CV classes using APIs from other ML packages, e.g. tensorflow or JAX. Any idea? |
@zwpku I am also working on a new NAMD interface that supports GPU-resident calculations, and I also use libtorch as an example. Based on my experience, it depends on how you implement the rotation/translation layer in python. My impression is that if you use the default automatic diffferentiation then it would be slow. I think the performance is better when using manual derivatives (my code is in https://github.com/HanatoK/namd_pytorchforces_example/blob/1f05b007b724502fccc23e06207f5ac865691285/build_rmsd_cv_opt.py#L143-L155). Have you tried to implement a custom As for JAX, I am also looking into it, but it seems lacking documentation of its C++ API. By the way, the pytorch community is moving to its new AOTInductor interface, and TorchScript is in maintenance mode, but again the documentation of AOTInductor is a problem. |
@HanatoK Thanks a lot for this information. The model I used for CV was obtained by learning eigenfunctions associated to the dynamics. I included a transformation after the input layer of the model to make sure the function is invariant under rotation and translation. In the end, it has the form |
@HanatoK @giacomofiorin |
@zwpku The RMSD is something special. I don't think it requires to calculate the derivative of the rotation matrix For other CVs depending on the aligned positions, I think Colvars has indeed the fit gradients for them, but I am not quite sure. |
Hi @zwpku! @HanatoK is correct that the RMSD does not normally need to compute those derivatives, because they are zero as long as the atoms being included in the RMSD are the same over which the roto-translation is computed. If the two are not the same (i.e. if Unfortunately, due to limitations in the current data structures we do not have support for computing/storing the fit gradients for vector-valued variables (#87), which includes Cartesian coordinates. But that limitation should not affect you, because you have already defined the Regarding speed, I would get this PR finished up for setting up NN functions of CVs. But if you want to use Cartesian coordinates directly, I would recommend checking out @HanatoK's preliminary work to that sense. I do acknowledge that you may want to use rotated-frame coordinates, which is not possible now but may become possible later (or is it?). |
@HanatoK @giacomofiorin Thank both of you for the explanations. Now the case of RMSD function is clear to me.
It is not clear whether I can define a function of fitted coodinates by simply turning on the options
I will look into the code to better understand it. |
@zwpku I attach a configuration of binding free-energy (BFEE) calculation here for your reference.
The system has two biomolecules, namely protein and ligand. We want to align the system simulated with respect to the reference positions only using the protein, and then measure the relative Euler theta angle of the ligand with respect to the reference. If you only work with a single molecule then you might not need the |
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.