The PYTORCH module is an interface between PyTorch machine learning library and PLUMED. It implements the PYTORCH_MODEL class, which is a subclass of Function class. PYTORCH_MODEL provide the ability to load models defined in Pytorch and compiled with TorchScript.
For instance, this allows one to use the outputs of a neural network as collective variables, as done in [bonati2020data] and in [bonati2021deep]. Furthermore, the PYTORCH_MODEL outputs can also be used as inputs for other collective variables and for data analysis tools.
This module is not installed by default. It requires the PyTorch C++ APIs (LibTorch) to be linked against PLUMED.
Download LibTorch C++ API library
You can download the pre-built LibTorch library from their website. For example, the following script downloads the 1.13.1 version (CPU, with C++11 ABI compatibility).
wget https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-1.13.1%2Bcpu.zip
unzip libtorch-cxx11-abi-shared-with-deps-1.13.1+cpu.zip ;
LIBTORCH=${PWD}/libtorch
The location of the include and library files need to be exported in the environment. For convenience, we can save them in a file sourceme.sh inside the libtorch folder:
> echo "export CPATH=${LIBTORCH}/include/torch/csrc/api/include/:${LIBTORCH}/include/:${LIBTORCH}/include/torch:$CPATH" >> ${LIBTORCH}/sourceme.sh
> echo "export INCLUDE=${LIBTORCH}/include/torch/csrc/api/include/:${LIBTORCH}/include/:${LIBTORCH}/include/torch:$INCLUDE" >> ${LIBTORCH}/sourceme.sh
> echo "export LIBRARY_PATH=${LIBTORCH}/lib:$LIBRARY_PATH" >> ${LIBTORCH}/sourceme.sh
> echo "export LD_LIBRARY_PATH=${LIBTORCH}/lib:$LD_LIBRARY_PATH" >> ${LIBTORCH}/sourceme.sh
> . ${LIBTORCH}/sourceme.sh
Remember to source the sourceme.sh file in your ~/.bashrc or ~/.bash_profile file.
Configure PLUMED
In order to install the PYTORCH module when compiling PLUMED we need to (1) specify to look for libtorch (--enable-libtorch) and (2) enable the related module (--enable-modules=pytorch or also --enable-modules=all):
> ./configure --enable-libtorch --enable-modules=pytorch
Additional notes
Currently, all features of the PYTORCH module are included in a single function: PYTORCH_MODEL
mlcvs is a Python package (under development) that can be used to optimize different kinds of neural-networks based CVs, e.g. that discriminate between states @cite bonati2020data or that approximate the slow dynamical modes of the system [bonati2021deep]. The CVs are optimized in Python and the resulting model is compiled with TorchScript, in order to allowed the models to be employed without Python dependencies.