-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add test * fix pairwise models * use `e3nn` jit * don't use non ascii chars * tests * fix nequip tests * improve test * test coverage
- Loading branch information
1 parent
d85cf6c
commit da7f627
Showing
10 changed files
with
200 additions
and
55 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
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,62 @@ | ||
from __future__ import annotations | ||
|
||
from pathlib import Path | ||
|
||
import pytest | ||
import torch | ||
from ase.build import molecule | ||
from graph_pes.core import GraphPESModel | ||
from graph_pes.data.io import to_atomic_graph | ||
from graph_pes.deploy import deploy_model | ||
from graph_pes.graphs.operations import number_of_atoms | ||
from graph_pes.models import ALL_MODELS, NequIP | ||
|
||
CUTOFF = 1.5 | ||
graph = to_atomic_graph(molecule("CH3CH2OH"), cutoff=CUTOFF) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"model_klass", | ||
ALL_MODELS, | ||
ids=[model.__name__ for model in ALL_MODELS], | ||
) | ||
def test_deploy(model_klass: type[GraphPESModel], tmp_path: Path): | ||
# 1. instantiate the model | ||
kwargs = {"n_elements": 3} if model_klass is NequIP else {} | ||
model = model_klass(**kwargs) | ||
model.pre_fit([graph]) # required by some models before making predictions | ||
|
||
# 2. deploy the model | ||
save_path = tmp_path / "model.pt" | ||
deploy_model(model, cutoff=CUTOFF, path=save_path) | ||
|
||
# 3. load the model back in | ||
loaded_model = torch.jit.load(save_path) | ||
assert isinstance(loaded_model, torch.jit.ScriptModule) | ||
assert loaded_model.get_cutoff() == CUTOFF | ||
|
||
# 4. test outputs | ||
outputs = loaded_model( | ||
# mock the graph that would be passed through from LAMMPS | ||
{ | ||
**graph, | ||
"compute_virial": torch.tensor(True), | ||
"debug": torch.tensor(False), | ||
} | ||
) | ||
assert isinstance(outputs, dict) | ||
assert set(outputs.keys()) == { | ||
"total_energy", | ||
"local_energies", | ||
"forces", | ||
"virial", | ||
} | ||
assert outputs["total_energy"].shape == torch.Size([]) | ||
assert outputs["local_energies"].shape == (number_of_atoms(graph),) | ||
assert outputs["forces"].shape == graph["_positions"].shape | ||
assert outputs["virial"].shape == (3, 3) | ||
|
||
# 5. test that the deployment process hasn't changed the model's predictions | ||
with torch.no_grad(): | ||
original_energy = model(graph).double() | ||
assert torch.allclose(original_energy, outputs["total_energy"]) |
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,49 @@ | ||
import pytest | ||
import torch | ||
from ase.build import molecule | ||
from graph_pes.core import get_predictions | ||
from graph_pes.data.io import to_atomic_graph | ||
from graph_pes.deploy import LAMMPSModel | ||
from graph_pes.graphs import keys | ||
from graph_pes.models import LennardJones | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"compute_virial", | ||
[True, False], | ||
) | ||
def test_lammps_model(compute_virial: bool): | ||
# generate a structure | ||
structure = molecule("CH3CH2OH") | ||
if compute_virial: | ||
# ensure the structure has a cell | ||
structure.center(vacuum=5.0) | ||
graph = to_atomic_graph(structure, cutoff=1.5) | ||
|
||
# create a normal model, and get normal predictions | ||
model = LennardJones() | ||
props: list[keys.LabelKey] = ["energy", "forces"] | ||
if compute_virial: | ||
props.append("stress") | ||
outputs = get_predictions(model, graph, properties=props, training=False) | ||
|
||
# create a LAMMPS model, and get LAMMPS predictions | ||
lammps_model = LAMMPSModel(model) | ||
lammps_graph: dict[str, torch.Tensor] = { | ||
**graph, | ||
"compute_virial": torch.tensor(compute_virial), | ||
"debug": torch.tensor(False), | ||
} # type: ignore | ||
lammps_outputs = lammps_model(lammps_graph) | ||
|
||
# check outputs | ||
if compute_virial: | ||
assert "virial" in lammps_outputs | ||
assert ( | ||
outputs["stress"].shape == lammps_outputs["virial"].shape == (3, 3) | ||
) | ||
|
||
assert torch.allclose( | ||
outputs["energy"].float(), | ||
lammps_outputs["total_energy"].float(), | ||
) |
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
Oops, something went wrong.