-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathalignn.py
349 lines (291 loc) · 11.1 KB
/
alignn.py
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
"""Atomistic LIne Graph Neural Network.
A prototype crystal line graph network dgl implementation.
"""
from typing import Tuple, Union
import dgl
import dgl.function as fn
import numpy as np
import torch
from dgl.nn import AvgPooling
from typing import Literal
from torch import nn
from torch.nn import functional as F
from alignn.models.utils import RBFExpansion
from pydantic_settings import BaseSettings
class ALIGNNConfig(BaseSettings):
"""Hyperparameter schema for jarvisdgl.models.alignn."""
name: Literal["alignn"]
alignn_layers: int = 4
gcn_layers: int = 4
atom_input_features: int = 92
edge_input_features: int = 80
triplet_input_features: int = 40
embedding_features: int = 64
hidden_features: int = 256
# fc_layers: int = 1
# fc_features: int = 64
output_features: int = 1
# if link == log, apply `exp` to final outputs
# to constrain predictions to be positive
link: Literal["identity", "log", "logit"] = "identity"
zero_inflated: bool = False
classification: bool = False
num_classes: int = 2
extra_features: int = 0
class Config:
"""Configure model settings behavior."""
env_prefix = "jv_model"
class EdgeGatedGraphConv(nn.Module):
"""Edge gated graph convolution from arxiv:1711.07553.
see also arxiv:2003.0098.
This is similar to CGCNN, but edge features only go into
the soft attention / edge gating function, and the primary
node update function is W cat(u, v) + b
"""
def __init__(
self, input_features: int, output_features: int, residual: bool = True
):
"""Initialize parameters for ALIGNN update."""
super().__init__()
self.residual = residual
# CGCNN-Conv operates on augmented edge features
# z_ij = cat(v_i, v_j, u_ij)
# m_ij = σ(z_ij W_f + b_f) ⊙ g_s(z_ij W_s + b_s)
# coalesce parameters for W_f and W_s
# but -- split them up along feature dimension
self.src_gate = nn.Linear(input_features, output_features)
self.dst_gate = nn.Linear(input_features, output_features)
self.edge_gate = nn.Linear(input_features, output_features)
self.bn_edges = nn.BatchNorm1d(output_features)
self.src_update = nn.Linear(input_features, output_features)
self.dst_update = nn.Linear(input_features, output_features)
self.bn_nodes = nn.BatchNorm1d(output_features)
def forward(
self,
g: dgl.DGLGraph,
node_feats: torch.Tensor,
edge_feats: torch.Tensor,
) -> torch.Tensor:
"""Edge-gated graph convolution.
h_i^l+1 = ReLU(U h_i + sum_{j->i} eta_{ij} ⊙ V h_j)
"""
g = g.local_var()
# instead of concatenating (u || v || e) and applying one weight matrix
# split the weight matrix into three, apply, then sum
# see https://docs.dgl.ai/guide/message-efficient.html
# but split them on feature dimensions to update u, v, e separately
# m = BatchNorm(Linear(cat(u, v, e)))
# compute edge updates, equivalent to:
# Softplus(Linear(u || v || e))
g.ndata["e_src"] = self.src_gate(node_feats)
g.ndata["e_dst"] = self.dst_gate(node_feats)
g.apply_edges(fn.u_add_v("e_src", "e_dst", "e_nodes"))
m = g.edata.pop("e_nodes") + self.edge_gate(edge_feats)
g.edata["sigma"] = torch.sigmoid(m)
g.ndata["Bh"] = self.dst_update(node_feats)
g.update_all(
fn.u_mul_e("Bh", "sigma", "m"), fn.sum("m", "sum_sigma_h")
)
g.update_all(fn.copy_e("sigma", "m"), fn.sum("m", "sum_sigma"))
g.ndata["h"] = g.ndata["sum_sigma_h"] / (g.ndata["sum_sigma"] + 1e-6)
x = self.src_update(node_feats) + g.ndata.pop("h")
# softmax version seems to perform slightly worse
# that the sigmoid-gated version
# compute node updates
# Linear(u) + edge_gates ⊙ Linear(v)
# g.edata["gate"] = edge_softmax(g, y)
# g.ndata["h_dst"] = self.dst_update(node_feats)
# g.update_all(fn.u_mul_e("h_dst", "gate", "m"), fn.sum("m", "h"))
# x = self.src_update(node_feats) + g.ndata.pop("h")
# node and edge updates
x = F.silu(self.bn_nodes(x))
y = F.silu(self.bn_edges(m))
if self.residual:
x = node_feats + x
y = edge_feats + y
return x, y
class ALIGNNConv(nn.Module):
"""Line graph update."""
def __init__(
self,
in_features: int,
out_features: int,
):
"""Set up ALIGNN parameters."""
super().__init__()
self.node_update = EdgeGatedGraphConv(in_features, out_features)
self.edge_update = EdgeGatedGraphConv(out_features, out_features)
def forward(
self,
g: dgl.DGLGraph,
lg: dgl.DGLGraph,
x: torch.Tensor,
y: torch.Tensor,
z: torch.Tensor,
):
"""Node and Edge updates for ALIGNN layer.
x: node input features
y: edge input features
z: edge pair input features
"""
g = g.local_var()
lg = lg.local_var()
# Edge-gated graph convolution update on crystal graph
x, m = self.node_update(g, x, y)
# Edge-gated graph convolution update on crystal graph
y, z = self.edge_update(lg, m, z)
return x, y, z
class MLPLayer(nn.Module):
"""Multilayer perceptron layer helper."""
def __init__(self, in_features: int, out_features: int):
"""Linear, Batchnorm, SiLU layer."""
super().__init__()
self.layer = nn.Sequential(
nn.Linear(in_features, out_features),
nn.BatchNorm1d(out_features),
nn.SiLU(),
)
def forward(self, x):
"""Linear, Batchnorm, silu layer."""
return self.layer(x)
class ALIGNN(nn.Module):
"""Atomistic Line graph network.
Chain alternating gated graph convolution updates on crystal graph
and atomistic line graph.
"""
def __init__(self, config: ALIGNNConfig = ALIGNNConfig(name="alignn")):
"""Initialize class with number of input features, conv layers."""
super().__init__()
# print(config)
self.config = config
self.classification = config.classification
self.atom_embedding = MLPLayer(
config.atom_input_features, config.hidden_features
)
self.edge_embedding = nn.Sequential(
RBFExpansion(
vmin=0,
vmax=8.0,
bins=config.edge_input_features,
),
MLPLayer(config.edge_input_features, config.embedding_features),
MLPLayer(config.embedding_features, config.hidden_features),
)
self.angle_embedding = nn.Sequential(
RBFExpansion(
vmin=-1,
vmax=1.0,
bins=config.triplet_input_features,
),
MLPLayer(config.triplet_input_features, config.embedding_features),
MLPLayer(config.embedding_features, config.hidden_features),
)
self.alignn_layers = nn.ModuleList(
[
ALIGNNConv(
config.hidden_features,
config.hidden_features,
)
for idx in range(config.alignn_layers)
]
)
self.gcn_layers = nn.ModuleList(
[
EdgeGatedGraphConv(
config.hidden_features, config.hidden_features
)
for idx in range(config.gcn_layers)
]
)
self.readout = AvgPooling()
self.readout_feat = AvgPooling()
if self.classification:
self.fc = nn.Linear(config.hidden_features, config.num_classes)
self.softmax = nn.LogSoftmax(dim=1)
else:
self.fc = nn.Linear(config.hidden_features, config.output_features)
if config.extra_features != 0:
# Credit for extra_features work:
# Gong et al., https://doi.org/10.48550/arXiv.2208.05039
self.extra_feature_embedding = MLPLayer(
config.extra_features, config.extra_features
)
self.fc3 = nn.Linear(
config.hidden_features + config.extra_features,
config.output_features,
)
self.fc1 = MLPLayer(
config.extra_features + config.hidden_features,
config.extra_features + config.hidden_features,
)
self.fc2 = MLPLayer(
config.extra_features + config.hidden_features,
config.extra_features + config.hidden_features,
)
self.link = None
self.link_name = config.link
if config.link == "identity":
self.link = lambda x: x
elif config.link == "log":
self.link = torch.exp
avg_gap = 0.7 # magic number -- average bandgap in dft_3d
self.fc.bias.data = torch.tensor(
np.log(avg_gap), dtype=torch.float
)
elif config.link == "logit":
self.link = torch.sigmoid
def forward(
self, g: Union[Tuple[dgl.DGLGraph, dgl.DGLGraph], dgl.DGLGraph]
):
"""ALIGNN : start with `atom_features`.
x: atom features (g.ndata)
y: bond features (g.edata and lg.ndata)
z: angle features (lg.edata)
"""
if len(self.alignn_layers) > 0:
# print('features2',features.shape)
g, lg, lat = g
lg = lg.local_var()
# angle features (fixed)
z = self.angle_embedding(lg.edata.pop("h"))
if self.config.extra_features != 0:
features = g.ndata["extra_features"]
# print('g',g)
# print('features1',features.shape)
features = self.extra_feature_embedding(features)
g = g.local_var()
# initial node features: atom feature network...
x = g.ndata.pop("atom_features")
# print("x1", x, x.shape)
x = self.atom_embedding(x)
# print("x2", x, x.shape)
# initial bond features
bondlength = torch.norm(g.edata.pop("r"), dim=1)
y = self.edge_embedding(bondlength)
# ALIGNN updates: update node, edge, triplet features
for alignn_layer in self.alignn_layers:
x, y, z = alignn_layer(g, lg, x, y, z)
# gated GCN updates: update node, edge features
for gcn_layer in self.gcn_layers:
x, y = gcn_layer(g, x, y)
# norm-activation-pool-classify
h = self.readout(g, x)
# print("h", h, h.shape)
# print('features',features.shape)
if self.config.extra_features != 0:
h_feat = self.readout_feat(g, features)
# print('h1',h.shape)
# print('h_feat',h_feat.shape)
h = torch.cat((h, h_feat), 1)
# print('h2',h.shape)
h = self.fc1(h)
h = self.fc2(h)
out = self.fc3(h)
else:
out = self.fc(h)
if self.link:
out = self.link(out)
if self.classification:
# out = torch.round(torch.sigmoid(out))
out = self.softmax(out)
return torch.squeeze(out)