File size: 12,971 Bytes
45e5a32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
from typing import Literal, Union, Sequence

import torch
from torch import Tensor, nn
import os
from pathlib import Path
from collections.abc import Sequence

from onescience.datapipes.materials.matris import RadiusGraph, GraphConverter, datatype
from onescience.modules.func_utils.matris_reference import AtomRef
from onescience.modules.func_utils.matris_graph import process_graphs
from onescience.modules.embedding.matris_embedding import (
    ThreebodyFourierExpansion, 
    AtomTypeEmbedding, 
    EdgeBasisEmbedding, 
    ThreebodyEmbedding
)
from onescience.modules.func_utils.matris_func_utils import (
    MLP,
    GatedMLP,
    get_normalization
)
from onescience.modules.layer.matris_interaction import Interaction_Block
from onescience.modules.head.matris_head import (
    EnergyHead,
    MagmomHead,
    ForceStressHead,
)


class MatRIS(nn.Module):
    """ Init MatRIS Potential """
    
    def __init__(
        self,
        num_layers: int = 6,
        node_feat_dim: int = 128,
        edge_feat_dim: int = 128,
        three_body_feat_dim: int = 128,
        mlp_hidden_dims: Union[int, Sequence[int]] = (128, 128),
        dropout: float = 0.0,
        use_bias: bool = False, 
        distance_expansion: str = "Bessel", 
        three_body_expansion: str = "SH",
        num_radial: int = 7,
        num_angular: int = 7,
        max_l: int = 4,
        max_n: int = 4,
        envelope_exponent: int = 8,
        graph_conv_mlp: str = "GateMLP",
        activation_type: str = "silu",
        norm_type: str = "rms",
        pairwise_cutoff: float = 6,
        three_body_cutoff: float = 4,
        use_smoothed_for_delta_edge: bool = False,
        learnable_basis: bool = True,
        is_intensive: bool = True,
        is_conservation: bool = True,
        reference_energy: str | None = None,
    ):
        """
        Args:
            num_layers (int): message passing layers.
            node_feat_dim (int): atom feature embedding dim.
            edge_feat_dim (int): edge(pairwise) feature embedding dim.
            three_body_feat_dim (int): angle(three body) feature embedding dim.
            mlp_hidden_dims (List or int): hidden dims of MLP. 
                Can be 'int' or 'list'.
            dropout (float): dropout rate in MLP.
            use_bias (bool): whether use bias in Interaction block.
            distance_expansion (str):  The function of pairwise basis. 
                Can be "Bessel" or "Gaussian".
            three_body_expansion (str): The function of three body basis. 
                Can be "Fourier(fourier)" or "Spherical Harmonics(sh)".
            num_radial (int): number of radial basis used in Bessel and Gaussian basis.
            num_angular (int): number of three_body basis used in Fourier basis.
            max_l (int): Maximum l value for Spherical Harmonics basis (SH).
            max_n (int): Maximum n value for Spherical Harmonics basis (SH).
            envelope_exponent (int): exponent of 'PolynomialEnvelope'.
            graph_conv_mlp (str): The type of MLP in mp layers. 
                Can be "MLP", "GatedMLP" and "MoE". 
                See fucntion.py for more informations.
            activation_type (str): activation function. 
                Can be "SiLU(silu)", "Sigmoid(sigmoid)", "ReLU(relu)"...
                See fucntion.py for more informations.
            norm_type (str): normalization function used in MLP.
                Can be "LayerNorm(layer)", "BatchNorm(batch)", "RMSNorm(rms)"...
                See fucntion.py for more informations.
            pairwise_cutoff (float): The cutoff of Atom graph.
            three_body_cutoff (float): The cutoff of Line graph.
            use_smoothed_for_delta_edge (bool): Whether to use the smoothed features for edge feature update.
            learnable_basis (bool): Whether the basis functions are learnable.
            is_intensive (bool): whether the model outputs energy per atom (True) or total energy (False).
            is_conservation (bool): whether use conservate force and stress.
            reference_energy (str): refernece energy of 'str'(eg. MPtrj, OMat..) dataset(Caculated by linear regression).
                more details can be found at reference_energy.py.
        """
        
        super().__init__()
        # model configs
        self.config = { k: v for k, v in locals().items() if k not in ["self", "__class__"] }

        self.is_intensive = is_intensive
        
        self.reference_energy = None
        if reference_energy is not None:
            self.reference_energy = AtomRef(
                reference_energy=reference_energy,
                is_intensive=is_intensive
            ) 
        
        # Define Graph Converter
        self.graph_converter = GraphConverter(
            atom_graph_cutoff=pairwise_cutoff,
            line_graph_cutoff=three_body_cutoff,
        )

        # ====== embedding layers ========
        self.atom_embedding = AtomTypeEmbedding(atom_feat_dim=node_feat_dim)
        self.edge_embedding = EdgeBasisEmbedding(
            pairwise_cutoff=pairwise_cutoff,
            three_body_cutoff=three_body_cutoff,
            num_radial=num_radial,
            edge_feat_dim=edge_feat_dim,
            envelope_exponent=envelope_exponent,
            learnable=learnable_basis,
            distance_expansion=distance_expansion,
        )
        self.three_body_embedding = ThreebodyEmbedding(
            num_angular = num_angular, # Fourier
            max_n=max_n, max_l=max_l, cutoff=pairwise_cutoff, # Spherical Harmonics
            three_body_feat_dim = three_body_feat_dim,
            three_body_expansion = three_body_expansion,
            learnable = learnable_basis
        )
        # ====== Interaction layers ========
        interaction_block = [
            Interaction_Block(
                node_feat_dim=node_feat_dim, 
                edge_feat_dim=edge_feat_dim,
                three_body_feat_dim=three_body_feat_dim,
                num_radial=num_radial,
                num_angular=num_angular,
                dropout=dropout,
                use_bias=use_bias,
                use_smoothed_for_delta_edge=use_smoothed_for_delta_edge,
                mlp_type=graph_conv_mlp,
                norm_type=norm_type,
                activation_type=activation_type,
            )
            for _ in range(num_layers)
        ]
        self.interaction_block = nn.ModuleList(interaction_block)

        # ====== Readout layers ======== 
        self.readout_norm = get_normalization(norm_type, dim=node_feat_dim)

        self.energy_head = EnergyHead(
            feat_dim = node_feat_dim,
            hidden_dim = mlp_hidden_dims,
            output_dim = 1,
            mlp_type = "mlp",
            activation_type = activation_type,
        )
        self.magmom_head = MagmomHead(
            feat_dim = node_feat_dim,
            hidden_dim = 2 * node_feat_dim,
            output_dim = 1,
            mlp_type = "mlp",
            activation_type = activation_type,
        )
        self.force_stress_head = ForceStressHead(
            is_conservation = is_conservation,
            feat_dim = edge_feat_dim, # is_conservation == False
            hidden_dim = mlp_hidden_dims, # is_conservation == False
            output_dim = 3, # is_conservation == False
            mlp_type = "mlp", # is_conservation == False
            activation_type = activation_type, # is_conservation == False
        )
        
        if not torch.distributed.is_initialized() or torch.distributed.get_rank() == 0:
            print(f"MatRIS initialized with {self.get_params()} parameters")

    def forward(
        self,
        graphs: Sequence[RadiusGraph],
        task: str = "ef",
        is_training: bool = False,
    ) -> dict[str, Tensor]:
        """
        Args:
            graphs (List): a list of RadiusGraph.
            task (str): the prediction task. Can be 'e', 'em', 'ef', 'efs', 'efsm'.
        """
        prediction = {}
        # ======== Graph processing ========
        batch_graph = process_graphs(graphs, compute_stress="s" in task)
        
        # ======== Feature embedding ========
        node_feat = self.atom_embedding( batch_graph['atomic_numbers'] - 1 ) # atom type feature init (use 0 for 'H')
        edge_feat, smooth_weight = self.edge_embedding(graphs=batch_graph) # pairwise feature init
        threebody_feat = None 
        if len(batch_graph['line_graph_dict']['line_graph']) != 0:
            threebody_feat = self.three_body_embedding(graphs=batch_graph) # three body feature init
        
        # ======== Interaction Block =======
        for mp_layer in self.interaction_block:
            node_feat, edge_feat, threebody_feat = mp_layer(
                batch_graph=batch_graph,
                node_feat=node_feat,
                edge_feat=edge_feat,
                threebody_feat=threebody_feat,
                smooth_weight=smooth_weight,
            )
        
        # ======== Readout Block ======= 
        node_feat = self.readout_norm(node_feat)
        
        total_energy = self.energy_head(batch_graph = batch_graph, node_feat = node_feat)
        
        force_stress_dict = self.force_stress_head(
            batch_graph = batch_graph, 
            compute_force="f" in task,
            compute_stress="s" in task,
            total_energy = total_energy, 
            node_feat = node_feat, 
            edge_feat = edge_feat, 
            is_training = is_training)
        prediction.update(force_stress_dict)
        
        if "m" in task:
            magmom = self.magmom_head(batch_graph = batch_graph, node_feat = node_feat)
            prediction["m"] = magmom
        
        atoms_per_graph_tensor = torch.tensor(batch_graph['atoms_per_graph'], 
                                                  dtype=torch.int32, 
                                                  device=total_energy.device)
        if self.is_intensive:
            energy_per_atom = total_energy / atoms_per_graph_tensor
            prediction["e"] = energy_per_atom
        else:
            prediction["e"] = total_energy

        prediction["atoms_per_graph"] = atoms_per_graph_tensor 

        ref_energy = (
            0 if self.reference_energy is None else self.reference_energy(graphs)
        )
        prediction["e"] += ref_energy
        prediction["ref_energy"] = ref_energy
        return prediction
    
    def get_params(self) -> int:
        """Return the number of parameters in the model."""
        return sum(p.numel() for p in self.parameters())

    @classmethod
    def from_dict(cls, dct: dict):
        matris = MatRIS(**dct["config"])
        matris.load_state_dict(dct["state_dict"])
        return matris
    
    @classmethod
    def load(
        cls,
        model_name: str = "matris_10m_oam",
        device: str | None = None,
    ):
        """Load pretrained model."""
        model_name = model_name.lower()
        supported_models = ["matris_10m_oam", "matris_10m_mp"]
        if model_name not in supported_models:
            raise ValueError(f"Unsupported model_name: {model_name}. Supported models are: {supported_models}")

        if device is None:
            device = "cuda" if torch.cuda.is_available() else "cpu"
        
        # 默认权重目录:本仓库根目录下的 weight/;可通过 ONESCIENCE_MODELS_DIR 覆盖
        default_weight_dir = Path(__file__).resolve().parent.parent / "weight"
        cache_dir = os.environ.get("ONESCIENCE_MODELS_DIR", str(default_weight_dir))
        os.makedirs(cache_dir, exist_ok=True)

        checkpoint_files = {
            "matris_10m_omat": "MatRIS_10M_OMAT.pth.tar",
            "matris_10m_oam": "MatRIS_10M_OAM.pth.tar",
            "matris_10m_mp": "MatRIS_10M_MP.pth.tar",
            "matris_6m_mp": "MatRIS_6M_MP.pth.tar",
        }

        DOWNLOAD_URLS = {
            "matris_10m_omat": "",  # TODO
            "matris_10m_oam": "https://figshare.com/ndownloader/files/59142728",
            "matris_10m_mp": "https://figshare.com/ndownloader/files/59143058",
            "matris_6m_mp": "",  # TODO
        }

        ckpt_filename = checkpoint_files[model_name]

        ckpt_path = os.path.join(cache_dir, ckpt_filename)
        if not os.path.exists(ckpt_path):
            url = DOWNLOAD_URLS.get(model_name)
            if not url:
                raise ValueError(f"No download URL provided for model: {model_name}")

            print(f"Checkpoint not found, downloading to {ckpt_path} ...")
            torch.hub.download_url_to_file(url, ckpt_path)
        
        
        ckpt_state = torch.load(
            ckpt_path, 
            map_location=torch.device("cpu"), 
            weights_only=False
        )
        model = MatRIS.from_dict(ckpt_state)
        
        model = model.to(device)
        print(f"Loading {model_name} successfully, running on {device}.")
        
        return model