Skip to content
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

Test if a PyTorch module receives corrects arguments #50

Merged
merged 3 commits into from
Jan 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions python/tests/TestTorchForce.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import openmmtorch as ot
import numpy as np
import unittest
import pytest
import torch as pt
from tempfile import NamedTemporaryFile

class TestTorchForce(unittest.TestCase):

Expand Down Expand Up @@ -31,6 +34,59 @@ def testForce(self):
assert np.allclose(-2*positions, state.getForces(asNumpy=True))


@pytest.mark.parametrize('deviceString', ['cpu', 'cuda:0', 'cuda:1'])
@pytest.mark.parametrize('precision', ['single', 'mixed', 'double'])
def testModuleArguments(deviceString, precision):

if pt.cuda.device_count() < 1 and deviceString == 'cuda:0':
pytest.skip('A CUDA device is not available')
if pt.cuda.device_count() < 2 and deviceString == 'cuda:1':
pytest.skip('Two CUDA devices are not available')

class TestModule(pt.nn.Module):

def __init__(self, device, dtype, positions):
super().__init__()
self.device = device
self.dtype = dtype
self.positions = pt.tensor(positions).to(self.device).to(self.dtype)

def forward(self, positions):
assert positions.device == self.device
assert positions.dtype == self.dtype
assert pt.all(positions == self.positions)
return pt.sum(positions)

with NamedTemporaryFile() as fd:

numParticles = 10
system = mm.System()
positions = np.random.rand(numParticles, 3)
for _ in range(numParticles):
system.addParticle(1.0)

device = pt.device(deviceString)
if device.type == 'cpu' or precision == 'double':
dtype = pt.float64
else:
dtype = pt.float32
module = TestModule(device, dtype, positions)
pt.jit.script(module).save(fd.name)
force = ot.TorchForce(fd.name)
system.addForce(force)

integrator = mm.VerletIntegrator(1.0)
platform = mm.Platform.getPlatformByName(device.type.upper())
properties = {}
if device.type == 'cuda':
properties['DeviceIndex'] = str(device.index)
properties['Precision'] = precision
context = mm.Context(system, integrator, platform, properties)

context.setPositions(positions)
context.getState(getEnergy=True, getForces=True)


if __name__ == '__main__':
unittest.main()