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

update to gemnet-oc hydra force head to work with amp #825

Merged
merged 4 commits into from
Sep 5, 2024
Merged
Changes from 1 commit
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
16 changes: 14 additions & 2 deletions src/fairchem/core/models/gemnet_oc/gemnet_oc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1489,12 +1489,17 @@ def forward(
@registry.register_model("gemnet_oc_force_head")
class GemNetOCForceHead(nn.Module, HeadInterface):
def __init__(
self, backbone, num_global_out_layers: int, output_init: str = "HeOrthogonal"
self,
backbone,
num_global_out_layers: int,
use_amp: bool = True,
output_init: str = "HeOrthogonal",
):
super().__init__()

self.direct_forces = backbone.direct_forces
self.forces_coupled = backbone.forces_coupled
self._use_amp = use_amp

emb_size_edge = backbone.edge_emb.dense.linear.out_features
if self.direct_forces:
Expand Down Expand Up @@ -1523,11 +1528,18 @@ def __init__(
out_initializer = get_initializer(output_init)
self.out_forces.reset_parameters(out_initializer)

@property
def use_amp(self):
return self._use_amp

def forward(
self, data: Batch, emb: dict[str, torch.Tensor]
) -> dict[str, torch.Tensor]:
if self.direct_forces:
x_F = self.out_mlp_F(torch.cat(emb["xs_F"], dim=-1))
if self.use_amp:
x_F = self.out_mlp_F(torch.cat(emb["xs_F"], dim=-1))
else:
x_F = self.out_mlp_F(torch.cat(emb["xs_F"], dim=-1).float())
with torch.cuda.amp.autocast(False):
F_st = self.out_forces(x_F.float())

Expand Down
Loading