Skip to content

Commit

Permalink
added regtests for pytorch module
Browse files Browse the repository at this point in the history
  • Loading branch information
luigibonati committed Feb 16, 2022
1 parent fea6c95 commit 0053e98
Show file tree
Hide file tree
Showing 34 changed files with 383 additions and 0 deletions.
1 change: 1 addition & 0 deletions regtest/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
!/tools
!/sasa
!/s2cm
!/pytorch
# These files we just want to ignore completely
tmp
report.txt
2 changes: 2 additions & 0 deletions regtest/pytorch/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include ../scripts/module.make

14 changes: 14 additions & 0 deletions regtest/pytorch/rt-pytorch_model/COLVAR.reference
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions regtest/pytorch/rt-pytorch_model/DERIVATIVES.reference
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions regtest/pytorch/rt-pytorch_model/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../scripts/test.make
Binary file added regtest/pytorch/rt-pytorch_model/alanine.xtc
Binary file not shown.
10 changes: 10 additions & 0 deletions regtest/pytorch/rt-pytorch_model/config
Original file line number Diff line number Diff line change
@@ -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
#}
43 changes: 43 additions & 0 deletions regtest/pytorch/rt-pytorch_model/create-pytorch-model.py
Original file line number Diff line number Diff line change
@@ -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)

14 changes: 14 additions & 0 deletions regtest/pytorch/rt-pytorch_model/plumed.dat
Original file line number Diff line number Diff line change
@@ -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
Binary file added regtest/pytorch/rt-pytorch_model/torch_model.ptc
Binary file not shown.
16 changes: 16 additions & 0 deletions regtest/pytorch/rt-pytorch_model_2d/COLVAR.reference
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions regtest/pytorch/rt-pytorch_model_2d/DERIVATIVES.reference
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions regtest/pytorch/rt-pytorch_model_2d/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../scripts/test.make
Binary file added regtest/pytorch/rt-pytorch_model_2d/alanine.xtc
Binary file not shown.
10 changes: 10 additions & 0 deletions regtest/pytorch/rt-pytorch_model_2d/config
Original file line number Diff line number Diff line change
@@ -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
#}
45 changes: 45 additions & 0 deletions regtest/pytorch/rt-pytorch_model_2d/create-pytorch-model.py
Original file line number Diff line number Diff line change
@@ -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)

15 changes: 15 additions & 0 deletions regtest/pytorch/rt-pytorch_model_2d/plumed.dat
Original file line number Diff line number Diff line change
@@ -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
Binary file not shown.
14 changes: 14 additions & 0 deletions regtest/pytorch/rt-pytorch_model_derivatives/COLVAR.reference
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions regtest/pytorch/rt-pytorch_model_derivatives/DERIVATIVES.reference
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions regtest/pytorch/rt-pytorch_model_derivatives/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../scripts/test.make
Binary file not shown.
10 changes: 10 additions & 0 deletions regtest/pytorch/rt-pytorch_model_derivatives/config
Original file line number Diff line number Diff line change
@@ -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
#}
Original file line number Diff line number Diff line change
@@ -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)

17 changes: 17 additions & 0 deletions regtest/pytorch/rt-pytorch_model_derivatives/plumed.dat
Original file line number Diff line number Diff line change
@@ -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
Binary file not shown.
14 changes: 14 additions & 0 deletions regtest/pytorch/rt-pytorch_model_script/COLVAR.reference
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions regtest/pytorch/rt-pytorch_model_script/DERIVATIVES.reference
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions regtest/pytorch/rt-pytorch_model_script/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../scripts/test.make
Binary file not shown.
10 changes: 10 additions & 0 deletions regtest/pytorch/rt-pytorch_model_script/config
Original file line number Diff line number Diff line change
@@ -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
#}
28 changes: 28 additions & 0 deletions regtest/pytorch/rt-pytorch_model_script/create-pytorch-model.py
Original file line number Diff line number Diff line change
@@ -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)

Loading

0 comments on commit 0053e98

Please sign in to comment.