-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Use rascaline inside metatensor's atomistic models
- Loading branch information
Showing
9 changed files
with
208 additions
and
5 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
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,96 @@ | ||
import os | ||
from typing import Dict, List | ||
|
||
import torch | ||
from metatensor.torch import Labels, TensorBlock | ||
from metatensor.torch.atomistic import ( | ||
MetatensorAtomisticModule, | ||
ModelCapabilities, | ||
ModelRunOptions, | ||
System, | ||
) | ||
|
||
from rascaline.torch import SoapPowerSpectrum, metatensor_system_to_rascaline | ||
|
||
|
||
HYPERS = { | ||
"cutoff": 3.6, | ||
"max_radial": 12, | ||
"max_angular": 3, | ||
"atomic_gaussian_width": 0.2, | ||
"center_atom_weight": 1.0, | ||
"radial_basis": {"Gto": {}}, | ||
"cutoff_function": {"ShiftedCosine": {"width": 0.3}}, | ||
} | ||
|
||
|
||
class Model(torch.nn.Module): | ||
def __init__(self, species: List[int]): | ||
super().__init__() | ||
self.calculator = SoapPowerSpectrum(**HYPERS) | ||
self.species_neighbors = torch.IntTensor( | ||
[(s1, s2) for s1 in species for s2 in species if s1 < s2] | ||
) | ||
|
||
n_max = HYPERS["max_radial"] | ||
l_max = HYPERS["max_angular"] | ||
in_features = (n_max * len(species)) ** 2 * l_max | ||
self.linear = torch.nn.Linear(in_features=in_features, out_features=1) | ||
|
||
def forward( | ||
self, system: System, run_options: ModelRunOptions | ||
) -> Dict[str, TensorBlock]: | ||
if "energy" not in run_options.outputs: | ||
return {} | ||
|
||
options = run_options.outputs["energy"] | ||
|
||
selected_atoms = run_options.selected_atoms | ||
if selected_atoms is None: | ||
selected_samples = None | ||
else: | ||
selected_tensor = torch.tensor(selected_atoms, dtype=torch.int32) | ||
selected_samples = Labels("center", selected_tensor.reshape(-1, 1)) | ||
|
||
soap = self.calculator( | ||
systems=metatensor_system_to_rascaline(system), | ||
selected_samples=selected_samples, | ||
) | ||
|
||
soap = soap.keys_to_properties( | ||
Labels(["species_neighbor_1", "species_neighbor_2"], self.species_neighbors) | ||
) | ||
soap = soap.keys_to_samples("species_center") | ||
|
||
features = soap.block().values | ||
samples = system.positions.samples | ||
|
||
if not options.per_atom: | ||
features = soap.block().values.sum(dim=0, keepdim=True) | ||
samples = Labels(["_"], torch.IntTensor([[0]])) | ||
|
||
return { | ||
"energy": TensorBlock( | ||
values=self.linear(features), | ||
samples=samples, | ||
components=[], | ||
properties=Labels(["energy"], torch.IntTensor([[0]])), | ||
) | ||
} | ||
|
||
|
||
def test_export_as_metatensor_module(tmpdir): | ||
model = Model(species=[1, 6, 8]) | ||
model.eval() | ||
|
||
export = MetatensorAtomisticModule(model, ModelCapabilities()) | ||
|
||
# Check we are requesting the right set of neighbors | ||
neighbors = export.requested_neighbors_lists() | ||
assert len(neighbors) == 1 | ||
assert neighbors[0].cutoff == HYPERS["cutoff"] | ||
assert not neighbors[0].full_list | ||
assert neighbors[0].requestors() == ["rascaline", "Model.calculator"] | ||
|
||
# check we can save the model | ||
export.export(os.path.join(tmpdir, "model.pt")) |
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
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