-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor eqV2 heads * refactor init_weights * add output_name attribute to eqV2 heads * fix deprecated registry names * remove debug breakpoint * add default name for rank2 head
- Loading branch information
Showing
8 changed files
with
231 additions
and
138 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from __future__ import annotations | ||
|
||
from .rank2 import Rank2SymmetricTensorHead | ||
from .scalar import EqV2ScalarHead | ||
from .vector import EqV2VectorHead | ||
|
||
__all__ = ["EqV2ScalarHead", "EqV2VectorHead", "Rank2SymmetricTensorHead"] |
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,66 @@ | ||
""" | ||
Copyright (c) Meta, Inc. and its affiliates. | ||
This source code is licensed under the MIT license found in the | ||
LICENSE file in the root directory of this source tree. | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from functools import partial | ||
from typing import TYPE_CHECKING | ||
|
||
import torch | ||
from torch import nn | ||
|
||
from fairchem.core.common import gp_utils | ||
from fairchem.core.common.registry import registry | ||
from fairchem.core.models.base import GraphData, HeadInterface | ||
from fairchem.core.models.equiformer_v2.transformer_block import FeedForwardNetwork | ||
from fairchem.core.models.equiformer_v2.weight_initialization import eqv2_init_weights | ||
|
||
if TYPE_CHECKING: | ||
from torch_geometric.data import Batch | ||
|
||
|
||
@registry.register_model("equiformerV2_scalar_head") | ||
class EqV2ScalarHead(nn.Module, HeadInterface): | ||
def __init__(self, backbone, output_name: str = "energy", reduce: str = "sum"): | ||
super().__init__() | ||
self.output_name = output_name | ||
self.reduce = reduce | ||
self.avg_num_nodes = backbone.avg_num_nodes | ||
self.energy_block = FeedForwardNetwork( | ||
backbone.sphere_channels, | ||
backbone.ffn_hidden_channels, | ||
1, | ||
backbone.lmax_list, | ||
backbone.mmax_list, | ||
backbone.SO3_grid, | ||
backbone.ffn_activation, | ||
backbone.use_gate_act, | ||
backbone.use_grid_mlp, | ||
backbone.use_sep_s2_act, | ||
) | ||
self.apply(partial(eqv2_init_weights, weight_init=backbone.weight_init)) | ||
|
||
def forward(self, data: Batch, emb: dict[str, torch.Tensor | GraphData]): | ||
node_output = self.energy_block(emb["node_embedding"]) | ||
node_output = node_output.embedding.narrow(1, 0, 1) | ||
if gp_utils.initialized(): | ||
node_output = gp_utils.gather_from_model_parallel_region(node_output, dim=0) | ||
output = torch.zeros( | ||
len(data.natoms), | ||
device=node_output.device, | ||
dtype=node_output.dtype, | ||
) | ||
|
||
output.index_add_(0, data.batch, node_output.view(-1)) | ||
if self.reduce == "sum": | ||
return {self.output_name: output / self.avg_num_nodes} | ||
elif self.reduce == "mean": | ||
return {self.output_name: output / data.natoms} | ||
else: | ||
raise ValueError( | ||
f"reduce can only be sum or mean, user provided: {self.reduce}" | ||
) |
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,84 @@ | ||
""" | ||
Copyright (c) Meta, Inc. and its affiliates. | ||
This source code is licensed under the MIT license found in the | ||
LICENSE file in the root directory of this source tree. | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from functools import partial | ||
from typing import TYPE_CHECKING | ||
|
||
import torch | ||
from torch import nn | ||
|
||
from fairchem.core.common import gp_utils | ||
from fairchem.core.common.registry import registry | ||
from fairchem.core.models.base import HeadInterface | ||
from fairchem.core.models.equiformer_v2.transformer_block import ( | ||
SO2EquivariantGraphAttention, | ||
) | ||
from fairchem.core.models.equiformer_v2.weight_initialization import eqv2_init_weights | ||
|
||
if TYPE_CHECKING: | ||
from torch_geometric.data import Batch | ||
|
||
from fairchem.core.models.base import BackboneInterface | ||
|
||
|
||
@registry.register_model("equiformerV2_vector_head") | ||
class EqV2VectorHead(nn.Module, HeadInterface): | ||
def __init__(self, backbone: BackboneInterface, output_name: str = "forces"): | ||
super().__init__() | ||
self.output_name = output_name | ||
self.activation_checkpoint = backbone.activation_checkpoint | ||
self.force_block = SO2EquivariantGraphAttention( | ||
backbone.sphere_channels, | ||
backbone.attn_hidden_channels, | ||
backbone.num_heads, | ||
backbone.attn_alpha_channels, | ||
backbone.attn_value_channels, | ||
1, | ||
backbone.lmax_list, | ||
backbone.mmax_list, | ||
backbone.SO3_rotation, | ||
backbone.mappingReduced, | ||
backbone.SO3_grid, | ||
backbone.max_num_elements, | ||
backbone.edge_channels_list, | ||
backbone.block_use_atom_edge_embedding, | ||
backbone.use_m_share_rad, | ||
backbone.attn_activation, | ||
backbone.use_s2_act_attn, | ||
backbone.use_attn_renorm, | ||
backbone.use_gate_act, | ||
backbone.use_sep_s2_act, | ||
alpha_drop=0.0, | ||
) | ||
self.apply(partial(eqv2_init_weights, weight_init=backbone.weight_init)) | ||
|
||
def forward(self, data: Batch, emb: dict[str, torch.Tensor]): | ||
if self.activation_checkpoint: | ||
output = torch.utils.checkpoint.checkpoint( | ||
self.force_block, | ||
emb["node_embedding"], | ||
emb["graph"].atomic_numbers_full, | ||
emb["graph"].edge_distance, | ||
emb["graph"].edge_index, | ||
emb["graph"].node_offset, | ||
use_reentrant=not self.training, | ||
) | ||
else: | ||
output = self.force_block( | ||
emb["node_embedding"], | ||
emb["graph"].atomic_numbers_full, | ||
emb["graph"].edge_distance, | ||
emb["graph"].edge_index, | ||
node_offset=emb["graph"].node_offset, | ||
) | ||
output = output.embedding.narrow(1, 1, 3) | ||
output = output.view(-1, 3).contiguous() | ||
if gp_utils.initialized(): | ||
output = gp_utils.gather_from_model_parallel_region(output, dim=0) | ||
return {self.output_name: output} |
Oops, something went wrong.