-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathoutput_modules.py
191 lines (152 loc) · 6.37 KB
/
output_modules.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
from abc import abstractmethod, ABCMeta
from torch_scatter import scatter
from typing import Optional, Tuple, Union
from torchmdnet.models.utils import act_class_mapping, GatedEquivariantBlock
from torchmdnet.utils import atomic_masses
from torch_scatter import scatter
import torch
from torch import nn, Tensor
__all__ = ["Scalar", "DipoleMoment", "ElectronicSpatialExtent"]
class OutputModel(nn.Module, metaclass=ABCMeta):
def __init__(self, allow_prior_model, reduce_op):
super(OutputModel, self).__init__()
self.allow_prior_model = allow_prior_model
self.reduce_op = reduce_op
def reset_parameters(self):
pass
@abstractmethod
def pre_reduce(self, x, v, z, pos, batch):
return
def reduce(self, x, batch):
return scatter(x, batch, dim=0, reduce=self.reduce_op)
def post_reduce(self, x):
return x
class Scalar(OutputModel):
def __init__(
self,
hidden_channels,
activation="silu",
allow_prior_model=True,
reduce_op="sum",
dtype=torch.float
):
super(Scalar, self).__init__(
allow_prior_model=allow_prior_model, reduce_op=reduce_op
)
act_class = act_class_mapping[activation]
self.output_network = nn.Sequential(
nn.Linear(hidden_channels, hidden_channels // 2, dtype=dtype),
act_class(),
nn.Linear(hidden_channels // 2, 1, dtype=dtype),
)
self.reset_parameters()
def reset_parameters(self):
nn.init.xavier_uniform_(self.output_network[0].weight)
self.output_network[0].bias.data.fill_(0)
nn.init.xavier_uniform_(self.output_network[2].weight)
self.output_network[2].bias.data.fill_(0)
def pre_reduce(self, x, v: Union[Tensor, Tuple[Tensor,Tensor,Tensor], None], z, pos, batch):
return self.output_network(x)
class EquivariantScalar(OutputModel):
def __init__(
self,
hidden_channels,
activation="silu",
allow_prior_model=True,
reduce_op="sum",
dtype=torch.float
):
super(EquivariantScalar, self).__init__(
allow_prior_model=allow_prior_model, reduce_op=reduce_op
)
self.output_network = nn.ModuleList(
[
GatedEquivariantBlock(
hidden_channels,
hidden_channels // 2,
activation=activation,
scalar_activation=True,
dtype=dtype
),
GatedEquivariantBlock(hidden_channels // 2, 1, activation=activation, dtype=dtype),
]
)
self.reset_parameters()
def reset_parameters(self):
for layer in self.output_network:
layer.reset_parameters()
def pre_reduce(self, x, v, z, pos, batch):
for layer in self.output_network:
x, v = layer(x, v)
# include v in output to make sure all parameters have a gradient
return x + v.sum() * 0
class DipoleMoment(Scalar):
def __init__(self, hidden_channels, activation="silu", reduce_op="sum", dtype=torch.float):
super(DipoleMoment, self).__init__(
hidden_channels, activation, allow_prior_model=False, reduce_op=reduce_op, dtype=dtype
)
atomic_mass = torch.from_numpy(atomic_masses).to(dtype)
self.register_buffer("atomic_mass", atomic_mass)
def pre_reduce(self, x, v: Optional[torch.Tensor], z, pos, batch):
x = self.output_network(x)
# Get center of mass.
mass = self.atomic_mass[z].view(-1, 1)
c = scatter(mass * pos, batch, dim=0) / scatter(mass, batch, dim=0)
x = x * (pos - c[batch])
return x
def post_reduce(self, x):
return torch.norm(x, dim=-1, keepdim=True)
class EquivariantDipoleMoment(EquivariantScalar):
def __init__(self, hidden_channels, activation="silu", reduce_op="sum", dtype=torch.float):
super(EquivariantDipoleMoment, self).__init__(
hidden_channels, activation, allow_prior_model=False, reduce_op=reduce_op, dtype=dtype
)
atomic_mass = torch.from_numpy(atomic_masses).to(dtype)
self.register_buffer("atomic_mass", atomic_mass)
def pre_reduce(self, x, v, z, pos, batch):
for layer in self.output_network:
x, v = layer(x, v)
# Get center of mass.
mass = self.atomic_mass[z].view(-1, 1)
c = scatter(mass * pos, batch, dim=0) / scatter(mass, batch, dim=0)
x = x * (pos - c[batch])
return x + v.squeeze()
def post_reduce(self, x):
return torch.norm(x, dim=-1, keepdim=True)
class ElectronicSpatialExtent(OutputModel):
def __init__(self, hidden_channels, activation="silu", reduce_op="sum", dtype=torch.float):
super(ElectronicSpatialExtent, self).__init__(
allow_prior_model=False, reduce_op=reduce_op
)
act_class = act_class_mapping[activation]
self.output_network = nn.Sequential(
nn.Linear(hidden_channels, hidden_channels // 2, dtype=dtype),
act_class(),
nn.Linear(hidden_channels // 2, 1, dtype=dtype),
)
atomic_mass = torch.from_numpy(atomic_masses).to(dtype)
self.register_buffer("atomic_mass", atomic_mass)
self.reset_parameters()
def reset_parameters(self):
nn.init.xavier_uniform_(self.output_network[0].weight)
self.output_network[0].bias.data.fill_(0)
nn.init.xavier_uniform_(self.output_network[2].weight)
self.output_network[2].bias.data.fill_(0)
def pre_reduce(self, x, v: Optional[torch.Tensor], z, pos, batch):
x = self.output_network(x)
# Get center of mass.
mass = self.atomic_mass[z].view(-1, 1)
c = scatter(mass * pos, batch, dim=0) / scatter(mass, batch, dim=0)
x = torch.norm(pos - c[batch], dim=1, keepdim=True) ** 2 * x
return x
class EquivariantElectronicSpatialExtent(ElectronicSpatialExtent):
pass
class EquivariantVectorOutput(EquivariantScalar):
def __init__(self, hidden_channels, activation="silu", reduce_op="sum", dtype=torch.float):
super(EquivariantVectorOutput, self).__init__(
hidden_channels, activation, allow_prior_model=False, reduce_op="sum", dtype=dtype
)
def pre_reduce(self, x, v, z, pos, batch):
for layer in self.output_network:
x, v = layer(x, v)
return v.squeeze()