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

Implement NNPOps Optimised ANI #21

Closed
wants to merge 7 commits into from
Closed
Changes from 2 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
25 changes: 19 additions & 6 deletions openmmml/models/anipotential.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def addForces(self,
atoms: Optional[Iterable[int]],
forceGroup: int,
filename: str = 'animodel.pt',
implementation: str = "nnpops",
**args):
# Create the TorchANI model.
import torchani
Expand All @@ -82,14 +83,26 @@ def addForces(self,
if atoms is not None:
includedAtoms = [includedAtoms[i] for i in atoms]
atomic_numbers = [atom.element.atomic_number for atom in includedAtoms]
species = torch.tensor(atomic_numbers).unsqueeze(0)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

atomic_numbers is a more meaningful name for this variable.


if implementation == "nnpops":
from NNPOps import OptimizedTorchANI
device = torch.device('cuda')
model = OptimizedTorchANI(model, species).to(device)
elif implementation == "cuaev":
self.model.aev_computer.use_cuda_extension = True
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work

elif implementation == "torchani":
# nothing extra to do here
pass
else:
raise ValueError(f"Unsupported implementation: {implementation}")

class ANIForce(torch.nn.Module):

def __init__(self, model, atomic_numbers, atoms, periodic):
super().__init__()

# Store the atomic numbers
self.atomic_numbers = torch.tensor(atomic_numbers).unsqueeze(0)
def __init__(self, model, species, atoms):
super(ANIForce, self).__init__()
self.model = model
self.species = species
self.energyScale = torchani.units.hartree2kjoulemol(1)

if atoms is None:
Expand Down Expand Up @@ -120,7 +133,7 @@ def forward(self, positions, boxvectors: Optional[torch.Tensor] = None):

return energy * self.energyScale # Hartree --> kJ/mol

aniForce = ANIForce(model, atomic_numbers, atoms, topology.getPeriodicBoxVectors() is not None)
aniForce = ANIForce(model, species, atoms)

# Convert it to TorchScript and save it.

Expand Down