diff --git a/regtest/.gitignore b/regtest/.gitignore index 7e71e66bea..606374a229 100644 --- a/regtest/.gitignore +++ b/regtest/.gitignore @@ -31,6 +31,7 @@ !/tools !/sasa !/s2cm +!/pytorch # These files we just want to ignore completely tmp report.txt diff --git a/regtest/pytorch/Makefile b/regtest/pytorch/Makefile new file mode 100644 index 0000000000..42480767ae --- /dev/null +++ b/regtest/pytorch/Makefile @@ -0,0 +1,2 @@ +include ../scripts/module.make + diff --git a/regtest/pytorch/rt-pytorch_model/COLVAR.reference b/regtest/pytorch/rt-pytorch_model/COLVAR.reference new file mode 100644 index 0000000000..de11fe158c --- /dev/null +++ b/regtest/pytorch/rt-pytorch_model/COLVAR.reference @@ -0,0 +1,14 @@ +#! FIELDS time phi model.node-0 +#! SET min_phi -pi +#! SET max_phi pi + 0.000000 -2.85656 -0.281187 + 5.000000 -2.29224 -0.750853 + 10.000000 -2.13168 -0.846786 + 15.000000 -2.92489 -0.215007 + 20.000000 -2.56135 -0.548228 + 25.000000 -1.10925 -0.895364 + 30.000000 1.47871 0.995763 + 35.000000 -1.27313 -0.956024 + 40.000000 -1.10608 -0.893949 + 45.000000 -3.03879 -0.102622 + 50.000000 -1.69099 -0.992786 diff --git a/regtest/pytorch/rt-pytorch_model/DERIVATIVES.reference b/regtest/pytorch/rt-pytorch_model/DERIVATIVES.reference new file mode 100644 index 0000000000..7b5269019b --- /dev/null +++ b/regtest/pytorch/rt-pytorch_model/DERIVATIVES.reference @@ -0,0 +1,12 @@ +#! FIELDS time parameter model.node-0 + 0.000000 0 -0.9596529007 + 5.000000 0 -0.6604691148 + 10.000000 0 -0.5319328904 + 15.000000 0 -0.9766126275 + 20.000000 0 -0.8363288045 + 25.000000 0 0.4453351200 + 30.000000 0 0.0919610485 + 35.000000 0 0.2932883203 + 40.000000 0 0.4481694996 + 45.000000 0 -0.9947203994 + 50.000000 0 -0.1199020892 diff --git a/regtest/pytorch/rt-pytorch_model/Makefile b/regtest/pytorch/rt-pytorch_model/Makefile new file mode 100644 index 0000000000..3703b27cea --- /dev/null +++ b/regtest/pytorch/rt-pytorch_model/Makefile @@ -0,0 +1 @@ +include ../../scripts/test.make diff --git a/regtest/pytorch/rt-pytorch_model/alanine.xtc b/regtest/pytorch/rt-pytorch_model/alanine.xtc new file mode 100644 index 0000000000..182058e94c Binary files /dev/null and b/regtest/pytorch/rt-pytorch_model/alanine.xtc differ diff --git a/regtest/pytorch/rt-pytorch_model/config b/regtest/pytorch/rt-pytorch_model/config new file mode 100644 index 0000000000..da5a9cd874 --- /dev/null +++ b/regtest/pytorch/rt-pytorch_model/config @@ -0,0 +1,10 @@ +plumed_needs=libtorch +plumed_modules=pytorch +type=driver +arg="--plumed plumed.dat --mf_xtc alanine.xtc" + +# note: model has been previously created with create-pytorch-model.py +# the following crashes in CI +# function plumed_regtest_before(){ +# python create-pytorch-model.py +#} diff --git a/regtest/pytorch/rt-pytorch_model/create-pytorch-model.py b/regtest/pytorch/rt-pytorch_model/create-pytorch-model.py new file mode 100644 index 0000000000..2442615499 --- /dev/null +++ b/regtest/pytorch/rt-pytorch_model/create-pytorch-model.py @@ -0,0 +1,43 @@ +import torch +print(torch.__version__) + +def my_torch_cv(x): + ''' + Here goes the definition of the CV. + + Inputs: + x (torch.tensor): input, either scalar or 1-D array + Return: + y (torch.tensor): collective variable (scalar) + ''' + # CV definition + y = torch.sin(x) + + return y + +input_size = 1 + +# -- DEFINE INPUT -- +#random +#x = torch.rand(input_size, dtype=torch.float32, requires_grad=True).unsqueeze(0) +#or by choosing the value(s) of the array +x = torch.tensor([0.], dtype=torch.float32, requires_grad=True) + +# -- CALCULATE CV -- +y = my_torch_cv(x) + +# -- CALCULATE DERIVATIVES -- +for yy in y: + dy = torch.autograd.grad(yy, x, create_graph=True) + # -- PRINT -- + print('CV TEST') + print('n_input\t: {}'.format(input_size)) + print('x\t: {}'.format(x)) + print('cv\t: {}'.format(yy)) + print('der\t: {}'.format(dy)) + +# Compile via tracing +traced_cv = torch.jit.trace ( my_torch_cv, example_inputs=x ) +filename='torch_model.pt' +traced_cv.save(filename) + diff --git a/regtest/pytorch/rt-pytorch_model/plumed.dat b/regtest/pytorch/rt-pytorch_model/plumed.dat new file mode 100644 index 0000000000..9d011dafa9 --- /dev/null +++ b/regtest/pytorch/rt-pytorch_model/plumed.dat @@ -0,0 +1,14 @@ +# vim:ft=plumed + +#define input x +phi: TORSION ATOMS=5,7,9,15 + +#load model computing y=sin(x) +model: PYTORCH_MODEL FILE=torch_model.ptc ARG=phi + +#output derivatives dy/dx +DUMPDERIVATIVES ARG=model.* STRIDE=5 FILE=DERIVATIVES + +#print colvar +PRINT FMT=%g STRIDE=5 FILE=COLVAR ARG=phi,model.* +ENDPLUMED diff --git a/regtest/pytorch/rt-pytorch_model/torch_model.ptc b/regtest/pytorch/rt-pytorch_model/torch_model.ptc new file mode 100644 index 0000000000..1e06d5e504 Binary files /dev/null and b/regtest/pytorch/rt-pytorch_model/torch_model.ptc differ diff --git a/regtest/pytorch/rt-pytorch_model_2d/COLVAR.reference b/regtest/pytorch/rt-pytorch_model_2d/COLVAR.reference new file mode 100644 index 0000000000..a499ef1958 --- /dev/null +++ b/regtest/pytorch/rt-pytorch_model_2d/COLVAR.reference @@ -0,0 +1,16 @@ +#! FIELDS time phi psi model.node-0 model.node-1 +#! SET min_phi -pi +#! SET max_phi pi +#! SET min_psi -pi +#! SET max_psi pi + 0.000000 -2.85656 2.7909 -0.281187 0.343545 + 5.000000 -2.29224 1.14276 -0.750853 0.909781 + 10.000000 -2.13168 1.89325 -0.846786 0.94846 + 15.000000 -2.92489 2.86227 -0.215007 0.275709 + 20.000000 -2.56135 0.91674 -0.548228 0.793622 + 25.000000 -1.10925 1.61448 -0.895364 0.999046 + 30.000000 1.47871 -1.34139 0.995763 -0.973801 + 35.000000 -1.27313 0.919978 -0.956024 0.795588 + 40.000000 -1.10608 0.941929 -0.893949 0.808695 + 45.000000 -3.03879 -2.43909 -0.102622 -0.646133 + 50.000000 -1.69099 1.06944 -0.992786 0.876932 diff --git a/regtest/pytorch/rt-pytorch_model_2d/DERIVATIVES.reference b/regtest/pytorch/rt-pytorch_model_2d/DERIVATIVES.reference new file mode 100644 index 0000000000..6b5e219050 --- /dev/null +++ b/regtest/pytorch/rt-pytorch_model_2d/DERIVATIVES.reference @@ -0,0 +1,23 @@ +#! FIELDS time parameter model.node-0 model.node-1 + 0.000000 0 -0.9596529007 0.0000000000 + 0.000000 1 0.0000000000 -0.9391362667 + 5.000000 0 -0.6604691148 0.0000000000 + 5.000000 1 0.0000000000 0.4150882959 + 10.000000 0 -0.5319328904 0.0000000000 + 10.000000 1 0.0000000000 -0.3168977797 + 15.000000 0 -0.9766126275 0.0000000000 + 15.000000 1 0.0000000000 -0.9612411261 + 20.000000 0 -0.8363288045 0.0000000000 + 20.000000 1 0.0000000000 0.6084106565 + 25.000000 0 0.4453351200 0.0000000000 + 25.000000 1 0.0000000000 -0.0436719432 + 30.000000 0 0.0919610485 0.0000000000 + 30.000000 1 0.0000000000 0.2274037302 + 35.000000 0 0.2932883203 0.0000000000 + 35.000000 1 0.0000000000 0.6058377624 + 40.000000 0 0.4481694996 0.0000000000 + 40.000000 1 0.0000000000 0.5882287621 + 45.000000 0 -0.9947203994 0.0000000000 + 45.000000 1 0.0000000000 -0.7632246017 + 50.000000 0 -0.1199020892 0.0000000000 + 50.000000 1 0.0000000000 0.4806151688 diff --git a/regtest/pytorch/rt-pytorch_model_2d/Makefile b/regtest/pytorch/rt-pytorch_model_2d/Makefile new file mode 100644 index 0000000000..3703b27cea --- /dev/null +++ b/regtest/pytorch/rt-pytorch_model_2d/Makefile @@ -0,0 +1 @@ +include ../../scripts/test.make diff --git a/regtest/pytorch/rt-pytorch_model_2d/alanine.xtc b/regtest/pytorch/rt-pytorch_model_2d/alanine.xtc new file mode 100644 index 0000000000..182058e94c Binary files /dev/null and b/regtest/pytorch/rt-pytorch_model_2d/alanine.xtc differ diff --git a/regtest/pytorch/rt-pytorch_model_2d/config b/regtest/pytorch/rt-pytorch_model_2d/config new file mode 100644 index 0000000000..da5a9cd874 --- /dev/null +++ b/regtest/pytorch/rt-pytorch_model_2d/config @@ -0,0 +1,10 @@ +plumed_needs=libtorch +plumed_modules=pytorch +type=driver +arg="--plumed plumed.dat --mf_xtc alanine.xtc" + +# note: model has been previously created with create-pytorch-model.py +# the following crashes in CI +# function plumed_regtest_before(){ +# python create-pytorch-model.py +#} diff --git a/regtest/pytorch/rt-pytorch_model_2d/create-pytorch-model.py b/regtest/pytorch/rt-pytorch_model_2d/create-pytorch-model.py new file mode 100644 index 0000000000..b3ec622a3e --- /dev/null +++ b/regtest/pytorch/rt-pytorch_model_2d/create-pytorch-model.py @@ -0,0 +1,45 @@ +import torch +print(torch.__version__) + +def my_torch_cv(x): + ''' + Here goes the definition of the CV. + + Inputs: + x (torch.tensor): input, either scalar or 1-D array + Return: + y (torch.tensor): collective variable (scalar) + ''' + # CV definition + #y1 = torch.sin(x) + #y2 = torch.cos(x) + #y = torch.cat((y1,y2)) + y = torch.sin(x) + return y + +input_size = 2 + +# -- DEFINE INPUT -- +#random +#x = torch.rand(input_size, dtype=torch.float32, requires_grad=True).unsqueeze(0) +#or by choosing the value(s) of the array +x = torch.tensor([0.,1.57], dtype=torch.float32, requires_grad=True) + +# -- CALCULATE CV -- +y = my_torch_cv(x) + +# -- CALCULATE DERIVATIVES -- +for yy in y: + dy = torch.autograd.grad(yy, x, create_graph=True) + # -- PRINT -- + print('CV TEST') + print('n_input\t: {}'.format(input_size)) + print('x\t: {}'.format(x)) + print('cv\t: {}'.format(yy)) + print('der\t: {}'.format(dy)) + +# Compile via tracing +traced_cv = torch.jit.trace ( my_torch_cv, example_inputs=x ) +filename='torch_model.pt' +traced_cv.save(filename) + diff --git a/regtest/pytorch/rt-pytorch_model_2d/plumed.dat b/regtest/pytorch/rt-pytorch_model_2d/plumed.dat new file mode 100644 index 0000000000..03f8c3bda3 --- /dev/null +++ b/regtest/pytorch/rt-pytorch_model_2d/plumed.dat @@ -0,0 +1,15 @@ +# vim:ft=plumed + +#define input x +phi: TORSION ATOMS=5,7,9,15 +psi: TORSION ATOMS=7,9,15,17 + +#load model computing [sin(x),cos(x)] +model: PYTORCH_MODEL FILE=torch_model.ptc ARG=phi,psi + +#output derivatives dy/dx +DUMPDERIVATIVES ARG=model.* STRIDE=5 FILE=DERIVATIVES + +#print colvar +PRINT FMT=%g STRIDE=5 FILE=COLVAR ARG=phi,psi,model.* +ENDPLUMED diff --git a/regtest/pytorch/rt-pytorch_model_2d/torch_model.ptc b/regtest/pytorch/rt-pytorch_model_2d/torch_model.ptc new file mode 100644 index 0000000000..a5fe64483a Binary files /dev/null and b/regtest/pytorch/rt-pytorch_model_2d/torch_model.ptc differ diff --git a/regtest/pytorch/rt-pytorch_model_derivatives/COLVAR.reference b/regtest/pytorch/rt-pytorch_model_derivatives/COLVAR.reference new file mode 100644 index 0000000000..bd9f1e7e15 --- /dev/null +++ b/regtest/pytorch/rt-pytorch_model_derivatives/COLVAR.reference @@ -0,0 +1,14 @@ +#! FIELDS time phi model.node-0 sinphi +#! SET min_phi -pi +#! SET max_phi pi + 0.000000 -2.85656 -0.281187 -0.281187 + 5.000000 -2.29224 -0.750853 -0.750853 + 10.000000 -2.13168 -0.846786 -0.846787 + 15.000000 -2.92489 -0.215007 -0.215006 + 20.000000 -2.56135 -0.548228 -0.548228 + 25.000000 -1.10925 -0.895364 -0.895364 + 30.000000 1.47871 0.995763 0.995763 + 35.000000 -1.27313 -0.956024 -0.956024 + 40.000000 -1.10608 -0.893949 -0.893949 + 45.000000 -3.03879 -0.102622 -0.102622 + 50.000000 -1.69099 -0.992786 -0.992786 diff --git a/regtest/pytorch/rt-pytorch_model_derivatives/DERIVATIVES.reference b/regtest/pytorch/rt-pytorch_model_derivatives/DERIVATIVES.reference new file mode 100644 index 0000000000..61d843cf8c --- /dev/null +++ b/regtest/pytorch/rt-pytorch_model_derivatives/DERIVATIVES.reference @@ -0,0 +1,12 @@ +#! FIELDS time parameter model.node-0 + 0.000000 0 -0.95965 + 5.000000 0 -0.66047 + 10.000000 0 -0.53193 + 15.000000 0 -0.97661 + 20.000000 0 -0.83633 + 25.000000 0 0.44534 + 30.000000 0 0.09196 + 35.000000 0 0.29329 + 40.000000 0 0.44817 + 45.000000 0 -0.99472 + 50.000000 0 -0.11990 diff --git a/regtest/pytorch/rt-pytorch_model_derivatives/Makefile b/regtest/pytorch/rt-pytorch_model_derivatives/Makefile new file mode 100644 index 0000000000..3703b27cea --- /dev/null +++ b/regtest/pytorch/rt-pytorch_model_derivatives/Makefile @@ -0,0 +1 @@ +include ../../scripts/test.make diff --git a/regtest/pytorch/rt-pytorch_model_derivatives/alanine.xtc b/regtest/pytorch/rt-pytorch_model_derivatives/alanine.xtc new file mode 100644 index 0000000000..182058e94c Binary files /dev/null and b/regtest/pytorch/rt-pytorch_model_derivatives/alanine.xtc differ diff --git a/regtest/pytorch/rt-pytorch_model_derivatives/config b/regtest/pytorch/rt-pytorch_model_derivatives/config new file mode 100644 index 0000000000..da5a9cd874 --- /dev/null +++ b/regtest/pytorch/rt-pytorch_model_derivatives/config @@ -0,0 +1,10 @@ +plumed_needs=libtorch +plumed_modules=pytorch +type=driver +arg="--plumed plumed.dat --mf_xtc alanine.xtc" + +# note: model has been previously created with create-pytorch-model.py +# the following crashes in CI +# function plumed_regtest_before(){ +# python create-pytorch-model.py +#} diff --git a/regtest/pytorch/rt-pytorch_model_derivatives/create-pytorch-model.py b/regtest/pytorch/rt-pytorch_model_derivatives/create-pytorch-model.py new file mode 100644 index 0000000000..2442615499 --- /dev/null +++ b/regtest/pytorch/rt-pytorch_model_derivatives/create-pytorch-model.py @@ -0,0 +1,43 @@ +import torch +print(torch.__version__) + +def my_torch_cv(x): + ''' + Here goes the definition of the CV. + + Inputs: + x (torch.tensor): input, either scalar or 1-D array + Return: + y (torch.tensor): collective variable (scalar) + ''' + # CV definition + y = torch.sin(x) + + return y + +input_size = 1 + +# -- DEFINE INPUT -- +#random +#x = torch.rand(input_size, dtype=torch.float32, requires_grad=True).unsqueeze(0) +#or by choosing the value(s) of the array +x = torch.tensor([0.], dtype=torch.float32, requires_grad=True) + +# -- CALCULATE CV -- +y = my_torch_cv(x) + +# -- CALCULATE DERIVATIVES -- +for yy in y: + dy = torch.autograd.grad(yy, x, create_graph=True) + # -- PRINT -- + print('CV TEST') + print('n_input\t: {}'.format(input_size)) + print('x\t: {}'.format(x)) + print('cv\t: {}'.format(yy)) + print('der\t: {}'.format(dy)) + +# Compile via tracing +traced_cv = torch.jit.trace ( my_torch_cv, example_inputs=x ) +filename='torch_model.pt' +traced_cv.save(filename) + diff --git a/regtest/pytorch/rt-pytorch_model_derivatives/plumed.dat b/regtest/pytorch/rt-pytorch_model_derivatives/plumed.dat new file mode 100644 index 0000000000..088471c90d --- /dev/null +++ b/regtest/pytorch/rt-pytorch_model_derivatives/plumed.dat @@ -0,0 +1,17 @@ +# vim:ft=plumed + +#define input x +phi: TORSION ATOMS=5,7,9,15 + +#load model computing y=sin(x) +model: PYTORCH_MODEL FILE=torch_model.ptc ARG=phi +#compute sin(x) with python +sinphi: CUSTOM ARG=phi FUNC=sin(x) PERIODIC=NO + +#output derivatives dy/dx (copy the latter to reference file and change header) +DUMPDERIVATIVES ARG=model.* STRIDE=5 FILE=DERIVATIVES FMT=%10.5f +DUMPDERIVATIVES ARG=sinphi STRIDE=5 FILE=DERIVATIVES_REF FMT=%10.5f + +#print colvar +PRINT FMT=%g STRIDE=5 FILE=COLVAR ARG=phi,model.*,sinphi +ENDPLUMED diff --git a/regtest/pytorch/rt-pytorch_model_derivatives/torch_model.ptc b/regtest/pytorch/rt-pytorch_model_derivatives/torch_model.ptc new file mode 100644 index 0000000000..1e06d5e504 Binary files /dev/null and b/regtest/pytorch/rt-pytorch_model_derivatives/torch_model.ptc differ diff --git a/regtest/pytorch/rt-pytorch_model_script/COLVAR.reference b/regtest/pytorch/rt-pytorch_model_script/COLVAR.reference new file mode 100644 index 0000000000..af18c7b489 --- /dev/null +++ b/regtest/pytorch/rt-pytorch_model_script/COLVAR.reference @@ -0,0 +1,14 @@ +#! FIELDS time phi model.node-0 +#! SET min_phi -pi +#! SET max_phi pi + 0.000000 -2.85656 0.29301 + 5.000000 -2.29224 1.13685 + 10.000000 -2.13168 1.5919 + 15.000000 -2.92489 0.220155 + 20.000000 -2.56135 0.655518 + 25.000000 -1.10925 -2.01054 + 30.000000 1.47871 0.995763 + 35.000000 -1.27313 -3.25967 + 40.000000 -1.10608 -1.99467 + 45.000000 -3.03879 0.103167 + 50.000000 -1.69099 8.27997 diff --git a/regtest/pytorch/rt-pytorch_model_script/DERIVATIVES.reference b/regtest/pytorch/rt-pytorch_model_script/DERIVATIVES.reference new file mode 100644 index 0000000000..3aa135c56d --- /dev/null +++ b/regtest/pytorch/rt-pytorch_model_script/DERIVATIVES.reference @@ -0,0 +1,12 @@ +#! FIELDS time parameter model.node-0 + 0.000000 0 1.08585 + 5.000000 0 2.29242 + 10.000000 0 3.53416 + 15.000000 0 1.04847 + 20.000000 0 1.42970 + 25.000000 0 5.04227 + 30.000000 0 0.09196 + 35.000000 0 11.62547 + 40.000000 0 4.97869 + 45.000000 0 1.01064 + 50.000000 0 69.55791 diff --git a/regtest/pytorch/rt-pytorch_model_script/Makefile b/regtest/pytorch/rt-pytorch_model_script/Makefile new file mode 100644 index 0000000000..3703b27cea --- /dev/null +++ b/regtest/pytorch/rt-pytorch_model_script/Makefile @@ -0,0 +1 @@ +include ../../scripts/test.make diff --git a/regtest/pytorch/rt-pytorch_model_script/alanine.xtc b/regtest/pytorch/rt-pytorch_model_script/alanine.xtc new file mode 100644 index 0000000000..182058e94c Binary files /dev/null and b/regtest/pytorch/rt-pytorch_model_script/alanine.xtc differ diff --git a/regtest/pytorch/rt-pytorch_model_script/config b/regtest/pytorch/rt-pytorch_model_script/config new file mode 100644 index 0000000000..da5a9cd874 --- /dev/null +++ b/regtest/pytorch/rt-pytorch_model_script/config @@ -0,0 +1,10 @@ +plumed_needs=libtorch +plumed_modules=pytorch +type=driver +arg="--plumed plumed.dat --mf_xtc alanine.xtc" + +# note: model has been previously created with create-pytorch-model.py +# the following crashes in CI +# function plumed_regtest_before(){ +# python create-pytorch-model.py +#} diff --git a/regtest/pytorch/rt-pytorch_model_script/create-pytorch-model.py b/regtest/pytorch/rt-pytorch_model_script/create-pytorch-model.py new file mode 100644 index 0000000000..0233a5a838 --- /dev/null +++ b/regtest/pytorch/rt-pytorch_model_script/create-pytorch-model.py @@ -0,0 +1,28 @@ +import torch +print(torch.__version__) + +def my_torch_cv(x): + ''' + Here goes the definition of the CV. + + Inputs: + x (torch.tensor): input, either scalar or 1-D array + Return: + y (torch.tensor): collective variable (scalar) + ''' + if x > 0: + # CV definition + y = torch.sin(x) + else: + y = torch.tan(x) + + return y + +input_size = 1 + +# Compile via scripting +scripted_cv = torch.jit.script( my_torch_cv ) + +filename='torch_model.pt' +scripted_cv.save(filename) + diff --git a/regtest/pytorch/rt-pytorch_model_script/plumed.dat b/regtest/pytorch/rt-pytorch_model_script/plumed.dat new file mode 100644 index 0000000000..2eeafc8ad7 --- /dev/null +++ b/regtest/pytorch/rt-pytorch_model_script/plumed.dat @@ -0,0 +1,14 @@ +# vim:ft=plumed + +#define input x +phi: TORSION ATOMS=5,7,9,15 + +#load model computing y=sin(x) +model: PYTORCH_MODEL FILE=torch_model.ptc ARG=phi + +#output derivatives dy/dx +DUMPDERIVATIVES ARG=model.* STRIDE=5 FILE=DERIVATIVES FMT=%10.5f + +#print colvar +PRINT FMT=%g STRIDE=5 FILE=COLVAR ARG=phi,model.* +ENDPLUMED diff --git a/regtest/pytorch/rt-pytorch_model_script/torch_model.ptc b/regtest/pytorch/rt-pytorch_model_script/torch_model.ptc new file mode 100644 index 0000000000..c5da0fd385 Binary files /dev/null and b/regtest/pytorch/rt-pytorch_model_script/torch_model.ptc differ