-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize SpeciesConverter of TorchANI (#39)
* Implement TorchANISpeciesConverter * Add test for TorchANISpeciesConverter * Update README.md * Remove the TorchANI import from the top level (#44)
- Loading branch information
Raimondas Galvelis
authored
Dec 20, 2021
1 parent
cacb4f8
commit 7041172
Showing
4 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
# | ||
|
||
import torch | ||
from torch import Tensor | ||
from typing import Optional, Tuple | ||
|
||
class TorchANISpeciesConverter(torch.nn.Module): | ||
|
||
from torchani.nn import SpeciesConverter | ||
|
||
def __init__(self, converter: SpeciesConverter, atomicNumbers: Tensor) -> None: | ||
|
||
super().__init__() | ||
|
||
# Convert atomic numbers to a list of species | ||
species = converter((atomicNumbers, torch.empty(0))).species | ||
self.register_buffer('species', species) | ||
|
||
self.conv_tensor = converter.conv_tensor # Just to make TorchScript happy :) | ||
|
||
def forward(self, species_coordinates: Tuple[Tensor, Tensor], | ||
cell: Optional[Tensor] = None, | ||
pbc: Optional[Tensor] = None) -> Tuple[Tensor, Tensor]: | ||
|
||
_, coordinates = species_coordinates | ||
|
||
return self.species, coordinates |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# | ||
# Copyright (c) 2020-2021 Acellera | ||
# Authors: Raimondas Galvelis | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
# | ||
|
||
import mdtraj | ||
import os | ||
import pytest | ||
import tempfile | ||
import torch | ||
import torchani | ||
|
||
molecules = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'molecules') | ||
|
||
def test_import(): | ||
import NNPOps | ||
import NNPOps.SpeciesConverter | ||
|
||
@pytest.mark.parametrize('deviceString', ['cpu', 'cuda']) | ||
@pytest.mark.parametrize('molFile', ['1hvj', '1hvk', '2iuz', '3hkw', '3hky', '3lka', '3o99']) | ||
def test_compare_with_native(deviceString, molFile): | ||
|
||
if deviceString == 'cuda' and not torch.cuda.is_available(): | ||
pytest.skip('CUDA is not available') | ||
|
||
from NNPOps.SpeciesConverter import TorchANISpeciesConverter | ||
|
||
device = torch.device(deviceString) | ||
|
||
mol = mdtraj.load(os.path.join(molecules, f'{molFile}_ligand.mol2')) | ||
atomicNumbers = torch.tensor([[atom.element.atomic_number for atom in mol.top.atoms]], device=device) | ||
atomicPositions = torch.tensor(mol.xyz * 10, dtype=torch.float32, requires_grad=True, device=device) | ||
|
||
nnp = torchani.models.ANI2x(periodic_table_index=True).to(device) | ||
energy_ref = nnp((atomicNumbers, atomicPositions)).energies | ||
energy_ref.backward() | ||
grad_ref = atomicPositions.grad.clone() | ||
|
||
nnp.species_converter = TorchANISpeciesConverter(nnp.species_converter, atomicNumbers).to(device) | ||
energy = nnp((atomicNumbers, atomicPositions)).energies | ||
atomicPositions.grad.zero_() | ||
energy.backward() | ||
grad = atomicPositions.grad.clone() | ||
|
||
energy_error = torch.abs((energy - energy_ref)/energy_ref) | ||
grad_error = torch.max(torch.abs((grad - grad_ref)/grad_ref)) | ||
|
||
assert energy_error < 5e-7 | ||
assert grad_error < 5e-3 | ||
|
||
@pytest.mark.parametrize('deviceString', ['cpu', 'cuda']) | ||
@pytest.mark.parametrize('molFile', ['1hvj', '1hvk', '2iuz', '3hkw', '3hky', '3lka', '3o99']) | ||
def test_model_serialization(deviceString, molFile): | ||
|
||
if deviceString == 'cuda' and not torch.cuda.is_available(): | ||
pytest.skip('CUDA is not available') | ||
|
||
from NNPOps.SpeciesConverter import TorchANISpeciesConverter | ||
|
||
device = torch.device(deviceString) | ||
|
||
mol = mdtraj.load(os.path.join(molecules, f'{molFile}_ligand.mol2')) | ||
atomicNumbers = torch.tensor([[atom.element.atomic_number for atom in mol.top.atoms]], device=device) | ||
atomicPositions = torch.tensor(mol.xyz * 10, dtype=torch.float32, requires_grad=True, device=device) | ||
|
||
nnp_ref = torchani.models.ANI2x(periodic_table_index=True).to(device) | ||
nnp_ref.species_converter = TorchANISpeciesConverter(nnp_ref.species_converter, atomicNumbers).to(device) | ||
|
||
energy_ref = nnp_ref((atomicNumbers, atomicPositions)).energies | ||
energy_ref.backward() | ||
grad_ref = atomicPositions.grad.clone() | ||
|
||
with tempfile.NamedTemporaryFile() as fd: | ||
|
||
torch.jit.script(nnp_ref).save(fd.name) | ||
nnp = torch.jit.load(fd.name) | ||
|
||
energy = nnp((atomicNumbers, atomicPositions)).energies | ||
atomicPositions.grad.zero_() | ||
energy.backward() | ||
grad = atomicPositions.grad.clone() | ||
|
||
energy_error = torch.abs((energy - energy_ref)/energy_ref) | ||
grad_error = torch.max(torch.abs((grad - grad_ref)/grad_ref)) | ||
|
||
assert energy_error < 5e-7 | ||
assert grad_error < 5e-3 |