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

Fix bug in scripted SOAP-BPNN without LayerNorm #166

Merged
merged 3 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
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
10 changes: 9 additions & 1 deletion src/metatensor/models/experimental/soap_bpnn/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
DEFAULT_MODEL_HYPERS = DEFAULT_HYPERS["model"]


class Identity(torch.nn.Module):
def __init__(self):
super().__init__()

def forward(self, x: TensorMap) -> TensorMap:
return x


class MLPMap(torch.nn.Module):
def __init__(self, all_species: List[int], hypers: dict) -> None:
super().__init__()
Expand Down Expand Up @@ -248,7 +256,7 @@ def __init__(
if hypers_bpnn["layernorm"]:
self.layernorm = LayerNormMap(self.all_species, soap_size)
else:
self.layernorm = torch.nn.Identity()
self.layernorm = Identity()

self.bpnn = MLPMap(self.all_species, hypers_bpnn)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import copy

import ase
import torch
from metatensor.torch.atomistic import ModelCapabilities, ModelOutput
from metatensor.torch.atomistic import ModelCapabilities, ModelOutput, systems_to_torch

from metatensor.models.experimental.soap_bpnn import DEFAULT_HYPERS, Model

Expand All @@ -18,7 +21,44 @@ def test_torchscript():
},
)
soap_bpnn = Model(capabilities, DEFAULT_HYPERS["model"])
torch.jit.script(soap_bpnn, {"energy": soap_bpnn.capabilities.outputs["energy"]})
soap_bpnn = torch.jit.script(soap_bpnn)

system = ase.Atoms(
"OHCN",
positions=[[0.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 0.0, 2.0], [0.0, 0.0, 3.0]],
)
Comment on lines +26 to +29
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not

system = System(
   species=torch.tensor([8, 1, 6, 7],
   positions=torch.tensor([[0.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 0.0, 2.0], [0.0, 0.0, 3.0]])
   cell=torch.zeros((3, 3)),
)

Copy link
Contributor

Choose a reason for hiding this comment

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

To me it looks fine since all the other tests also take in systems from ase.Atoms, but if it's a matter of dropping more dependencies for the test, then I'm for it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Opened issue

soap_bpnn(
[systems_to_torch(system)],
{"energy": soap_bpnn.capabilities.outputs["energy"]},
)


def test_torchscript_with_identity():
"""Tests that the model can be jitted."""

capabilities = ModelCapabilities(
length_unit="Angstrom",
atomic_types=[1, 6, 7, 8],
outputs={
"energy": ModelOutput(
quantity="energy",
unit="eV",
)
},
)
hypers = copy.deepcopy(DEFAULT_HYPERS["model"])
hypers["bpnn"]["layernorm"] = False
soap_bpnn = Model(capabilities, hypers)
soap_bpnn = torch.jit.script(soap_bpnn)

system = ase.Atoms(
"OHCN",
positions=[[0.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 0.0, 2.0], [0.0, 0.0, 3.0]],
)
soap_bpnn(
[systems_to_torch(system)],
{"energy": soap_bpnn.capabilities.outputs["energy"]},
)


def test_torchscript_save():
Expand All @@ -36,8 +76,6 @@ def test_torchscript_save():
)
soap_bpnn = Model(capabilities, DEFAULT_HYPERS["model"])
torch.jit.save(
torch.jit.script(
soap_bpnn, {"energy": soap_bpnn.capabilities.outputs["energy"]}
),
torch.jit.script(soap_bpnn),
"soap_bpnn.pt",
)