emle-engine 2.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of emle-engine might be problematic. Click here for more details.
- emle/__init__.py +39 -0
- emle/_analyzer.py +337 -0
- emle/_backends/__init__.py +14 -0
- emle/_backends/_ace.py +141 -0
- emle/_backends/_aimnet2.py +136 -0
- emle/_backends/_backend.py +148 -0
- emle/_backends/_deepmd.py +186 -0
- emle/_backends/_omnip2x.py +349 -0
- emle/_backends/_orca.py +322 -0
- emle/_backends/_sander.py +232 -0
- emle/_backends/_sqm.py +230 -0
- emle/_backends/_xtb.py +126 -0
- emle/_compiler.py +279 -0
- emle/_csrc/ops.cpp +350 -0
- emle/_csrc/ops_cuda.cu +454 -0
- emle/_ops.py +173 -0
- emle/_orca_parser.py +331 -0
- emle/_resources.py +32 -0
- emle/_socket.py +211 -0
- emle/_tarball_to_extXYZ.py +236 -0
- emle/_units.py +16 -0
- emle/_utils.py +57 -0
- emle/calculator.py +1974 -0
- emle/models/__init__.py +13 -0
- emle/models/_ani.py +315 -0
- emle/models/_deepmd.py +410 -0
- emle/models/_emle.py +629 -0
- emle/models/_emle_aev_computer.py +616 -0
- emle/models/_emle_base.py +1063 -0
- emle/models/_mace.py +1368 -0
- emle/models/_patches.py +66 -0
- emle/models/_utils.py +279 -0
- emle/train/__init__.py +8 -0
- emle/train/_extxyz.py +63 -0
- emle/train/_gpr.py +205 -0
- emle/train/_ivm.py +126 -0
- emle/train/_loss.py +357 -0
- emle/train/_trainer.py +709 -0
- emle/train/_utils.py +92 -0
- emle_engine-2.0.0.data/scripts/emle-analyze +175 -0
- emle_engine-2.0.0.data/scripts/emle-compile +162 -0
- emle_engine-2.0.0.data/scripts/emle-server +777 -0
- emle_engine-2.0.0.data/scripts/emle-stop +21 -0
- emle_engine-2.0.0.data/scripts/emle-train +127 -0
- emle_engine-2.0.0.data/scripts/orca +95 -0
- emle_engine-2.0.0.data/scripts/tarball-to-extxyz +6 -0
- emle_engine-2.0.0.dist-info/METADATA +49 -0
- emle_engine-2.0.0.dist-info/RECORD +51 -0
- emle_engine-2.0.0.dist-info/WHEEL +5 -0
- emle_engine-2.0.0.dist-info/licenses/LICENSE +21 -0
- emle_engine-2.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,616 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2023-2026 The EMLE-Engine developers
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
"""EMLE AEVComputer implementation."""
|
|
5
|
+
|
|
6
|
+
__author__ = "Kirill Zinovjev"
|
|
7
|
+
__email__ = "kzinovjev@gmail.com"
|
|
8
|
+
|
|
9
|
+
import numpy as _np
|
|
10
|
+
import torch as _torch
|
|
11
|
+
import torchani as _torchani
|
|
12
|
+
from torchani.aev import ANIRadial as _ANIRadial, ANIAngular as _ANIAngular
|
|
13
|
+
|
|
14
|
+
from torch import Tensor
|
|
15
|
+
from typing import Tuple
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# Whether cuAEV works, for each CUDA device capability that has been checked.
|
|
19
|
+
_cuaev_works = {}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _cuaev_available(aev_computer):
|
|
23
|
+
"""
|
|
24
|
+
Whether TorchANI's fused cuAEV implementation can be used by the AEV
|
|
25
|
+
computer. Models that can use it select it in forward for CUDA inputs,
|
|
26
|
+
and the pure PyTorch implementation otherwise, since cuAEV doesn't
|
|
27
|
+
support CPU tensors. TorchANI's builds don't include kernels for every
|
|
28
|
+
GPU architecture, so cuAEV is only used if it works on the current
|
|
29
|
+
CUDA device.
|
|
30
|
+
|
|
31
|
+
Parameters
|
|
32
|
+
----------
|
|
33
|
+
|
|
34
|
+
aev_computer: torchani.AEVComputer
|
|
35
|
+
The AEV computer.
|
|
36
|
+
|
|
37
|
+
Returns
|
|
38
|
+
-------
|
|
39
|
+
|
|
40
|
+
available: bool
|
|
41
|
+
Whether cuAEV can be used.
|
|
42
|
+
"""
|
|
43
|
+
if not (
|
|
44
|
+
isinstance(aev_computer, _torchani.AEVComputer)
|
|
45
|
+
and aev_computer._cuaev_is_avail
|
|
46
|
+
and _torch.cuda.is_available()
|
|
47
|
+
):
|
|
48
|
+
return False
|
|
49
|
+
|
|
50
|
+
capability = _torch.cuda.get_device_capability()
|
|
51
|
+
if capability not in _cuaev_works:
|
|
52
|
+
_cuaev_works[capability] = _check_cuaev(aev_computer)
|
|
53
|
+
return _cuaev_works[capability]
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _check_cuaev(aev_computer):
|
|
57
|
+
"""
|
|
58
|
+
Check that cuAEV gives the same AEVs as the pure PyTorch implementation
|
|
59
|
+
for a small molecule on the current CUDA device.
|
|
60
|
+
|
|
61
|
+
Parameters
|
|
62
|
+
----------
|
|
63
|
+
|
|
64
|
+
aev_computer: torchani.AEVComputer
|
|
65
|
+
The AEV computer whose terms are used for the check.
|
|
66
|
+
|
|
67
|
+
Returns
|
|
68
|
+
-------
|
|
69
|
+
|
|
70
|
+
works: bool
|
|
71
|
+
Whether cuAEV works.
|
|
72
|
+
"""
|
|
73
|
+
import copy as _copy
|
|
74
|
+
|
|
75
|
+
try:
|
|
76
|
+
computer = _torchani.AEVComputer(
|
|
77
|
+
_copy.deepcopy(aev_computer.radial),
|
|
78
|
+
_copy.deepcopy(aev_computer.angular),
|
|
79
|
+
num_species=aev_computer.num_species,
|
|
80
|
+
).to(device="cuda", dtype=_torch.float32)
|
|
81
|
+
species = _torch.tensor([[0, 1, 2, 3]], device="cuda")
|
|
82
|
+
xyz = _torch.tensor(
|
|
83
|
+
[[[0.0, 0.0, 0.0], [1.1, 0.0, 0.0], [0.0, 1.2, 0.0], [0.3, 0.4, 1.1]]],
|
|
84
|
+
device="cuda",
|
|
85
|
+
)
|
|
86
|
+
expected = computer(species, xyz)
|
|
87
|
+
computer.set_strategy("cuaev")
|
|
88
|
+
aev = computer(species, xyz)
|
|
89
|
+
_torch.cuda.synchronize()
|
|
90
|
+
return bool(_torch.allclose(aev, expected, rtol=1e-4, atol=1e-6))
|
|
91
|
+
except Exception:
|
|
92
|
+
return False
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
# Default hyperparameters for AEVComputer. Taken from ANI2x.
|
|
96
|
+
_DEFAULT_HYPERS_DICT = {
|
|
97
|
+
"Rcr": 5.1000e00,
|
|
98
|
+
"Rca": 3.5000e00,
|
|
99
|
+
"EtaR": _np.array([1.9700000e01]),
|
|
100
|
+
"ShfR": _np.array(
|
|
101
|
+
[
|
|
102
|
+
8.0000000e-01,
|
|
103
|
+
1.0687500e00,
|
|
104
|
+
1.3375000e00,
|
|
105
|
+
1.6062500e00,
|
|
106
|
+
1.8750000e00,
|
|
107
|
+
2.1437500e00,
|
|
108
|
+
2.4125000e00,
|
|
109
|
+
2.6812500e00,
|
|
110
|
+
2.9500000e00,
|
|
111
|
+
3.2187500e00,
|
|
112
|
+
3.4875000e00,
|
|
113
|
+
3.7562500e00,
|
|
114
|
+
4.0250000e00,
|
|
115
|
+
4.2937500e00,
|
|
116
|
+
4.5625000e00,
|
|
117
|
+
4.8312500e00,
|
|
118
|
+
]
|
|
119
|
+
),
|
|
120
|
+
"Zeta": _np.array([1.4100000e01]),
|
|
121
|
+
"ShfZ": _np.array([3.9269908e-01, 1.1780972e00, 1.9634954e00, 2.7488936e00]),
|
|
122
|
+
"EtaA": _np.array([1.2500000e01]),
|
|
123
|
+
"ShfA": _np.array(
|
|
124
|
+
[
|
|
125
|
+
8.0000000e-01,
|
|
126
|
+
1.1375000e00,
|
|
127
|
+
1.4750000e00,
|
|
128
|
+
1.8125000e00,
|
|
129
|
+
2.1500000e00,
|
|
130
|
+
2.4875000e00,
|
|
131
|
+
2.8250000e00,
|
|
132
|
+
3.1625000e00,
|
|
133
|
+
]
|
|
134
|
+
),
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def get_default_hypers(device, dtype):
|
|
139
|
+
"""
|
|
140
|
+
Get default hyperparameters for AEVComputer
|
|
141
|
+
"""
|
|
142
|
+
hypers = {}
|
|
143
|
+
for key, value in _DEFAULT_HYPERS_DICT.items():
|
|
144
|
+
if isinstance(value, _np.ndarray):
|
|
145
|
+
hypers[key] = _torch.tensor(value, device=device, dtype=dtype)
|
|
146
|
+
else:
|
|
147
|
+
hypers[key] = value
|
|
148
|
+
return hypers
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
class EMLEAEVComputer(_torch.nn.Module):
|
|
152
|
+
"""
|
|
153
|
+
Wrapper for AEVCalculator from torchani
|
|
154
|
+
(not a subclass to make sure it works with TorchScript)
|
|
155
|
+
"""
|
|
156
|
+
|
|
157
|
+
# TorchScript constants, so that the dense, fused and cuAEV branches of
|
|
158
|
+
# forward are only compiled when used.
|
|
159
|
+
__constants__ = ["_dense", "_fused", "_cuaev"]
|
|
160
|
+
|
|
161
|
+
def __init__(
|
|
162
|
+
self,
|
|
163
|
+
num_species=7,
|
|
164
|
+
hypers=None,
|
|
165
|
+
mask=None,
|
|
166
|
+
is_external=False,
|
|
167
|
+
zid_map=None,
|
|
168
|
+
device=None,
|
|
169
|
+
dtype=None,
|
|
170
|
+
dense=False,
|
|
171
|
+
fused=False,
|
|
172
|
+
):
|
|
173
|
+
"""
|
|
174
|
+
Constructor.
|
|
175
|
+
|
|
176
|
+
Parameters
|
|
177
|
+
----------
|
|
178
|
+
|
|
179
|
+
num_species: int
|
|
180
|
+
Number of supported species.
|
|
181
|
+
|
|
182
|
+
hypers: dict
|
|
183
|
+
Hyperparameters for the wrapped AEVComputer.
|
|
184
|
+
|
|
185
|
+
mask: torch.BoolTensor
|
|
186
|
+
Mask for the features returned from wrapped AEVComputer.
|
|
187
|
+
|
|
188
|
+
is_external: bool
|
|
189
|
+
Whether the features are calculated externally.
|
|
190
|
+
|
|
191
|
+
zid_map: dict or torch.tensor
|
|
192
|
+
Map from zid provided here to the ones passed to AEVComputer.
|
|
193
|
+
|
|
194
|
+
device: torch.device
|
|
195
|
+
The device on which to run the model.
|
|
196
|
+
|
|
197
|
+
dtype: torch.dtype
|
|
198
|
+
The data type to use for the models floating point tensors.
|
|
199
|
+
|
|
200
|
+
dense: bool
|
|
201
|
+
Whether to compute the AEVs with a dense implementation that
|
|
202
|
+
evaluates all atom pairs and triples, rather than with TorchANI.
|
|
203
|
+
It gives the same AEVs, involves no data-dependent shapes or
|
|
204
|
+
host synchronisation (so a model using it can be captured in a
|
|
205
|
+
CUDA graph), but its cost and memory grow with the cube of the
|
|
206
|
+
number of atoms, so it is only suitable for small QM regions
|
|
207
|
+
(up to ~60 atoms).
|
|
208
|
+
|
|
209
|
+
fused: bool
|
|
210
|
+
Whether to compute the AEVs on CUDA devices with the fused
|
|
211
|
+
torch.ops.emle.aev operator (see emle._ops), which gives the same
|
|
212
|
+
AEVs, is faster than TorchANI's implementations and can be
|
|
213
|
+
captured in a CUDA graph. On other devices TorchANI is used.
|
|
214
|
+
"""
|
|
215
|
+
super().__init__()
|
|
216
|
+
|
|
217
|
+
# Validate the input.
|
|
218
|
+
|
|
219
|
+
if device is not None:
|
|
220
|
+
if not isinstance(device, _torch.device):
|
|
221
|
+
raise TypeError("'device' must be of type 'torch.device'")
|
|
222
|
+
else:
|
|
223
|
+
device = _torch.get_default_device()
|
|
224
|
+
self._device = device
|
|
225
|
+
|
|
226
|
+
if dtype is not None:
|
|
227
|
+
if not isinstance(dtype, _torch.dtype):
|
|
228
|
+
raise TypeError("'dtype' must be of type 'torch.dtype'")
|
|
229
|
+
else:
|
|
230
|
+
dtype = _torch.get_default_dtype()
|
|
231
|
+
|
|
232
|
+
if mask is not None:
|
|
233
|
+
if not isinstance(mask, _torch.Tensor):
|
|
234
|
+
raise TypeError("'mask' must be of type 'torch.Tensor'")
|
|
235
|
+
if len(mask.shape) != 1:
|
|
236
|
+
raise ValueError("'mask' must be a 1D tensor")
|
|
237
|
+
if not mask.dtype == _torch.bool:
|
|
238
|
+
raise ValueError("'mask' must have dtype 'torch.bool'")
|
|
239
|
+
self._mask = mask
|
|
240
|
+
self._set_mask_index()
|
|
241
|
+
|
|
242
|
+
if not isinstance(is_external, bool):
|
|
243
|
+
raise TypeError("'is_external' must be of type 'bool'")
|
|
244
|
+
self._is_external = is_external
|
|
245
|
+
self._cuaev = False
|
|
246
|
+
|
|
247
|
+
if not isinstance(dense, bool):
|
|
248
|
+
raise TypeError("'dense' must be of type 'bool'")
|
|
249
|
+
self._dense = dense
|
|
250
|
+
|
|
251
|
+
if not isinstance(fused, bool):
|
|
252
|
+
raise TypeError("'fused' must be of type 'bool'")
|
|
253
|
+
if fused and is_external:
|
|
254
|
+
raise ValueError("'fused' AEVs cannot be used with external AEVs")
|
|
255
|
+
self._fused = fused
|
|
256
|
+
if fused:
|
|
257
|
+
from .._ops import load_ops
|
|
258
|
+
|
|
259
|
+
load_ops()
|
|
260
|
+
|
|
261
|
+
# Initalise an empty AEV tensor to use to store the AEVs in parent models.
|
|
262
|
+
# If AEVs are computed externally, then this tensor will be set by the
|
|
263
|
+
# parent.
|
|
264
|
+
self._aev = _torch.empty(0, dtype=dtype, device=device)
|
|
265
|
+
|
|
266
|
+
if not isinstance(num_species, int):
|
|
267
|
+
raise TypeError("'num_species' must be of type 'int'")
|
|
268
|
+
if num_species < 1:
|
|
269
|
+
raise ValueError("'num_species' must be greater than 0")
|
|
270
|
+
|
|
271
|
+
if hypers is not None:
|
|
272
|
+
if not isinstance(hypers, dict):
|
|
273
|
+
raise TypeError("'hypers' must be of type 'dict' or None")
|
|
274
|
+
|
|
275
|
+
# Create the AEV computer.
|
|
276
|
+
if not self._is_external:
|
|
277
|
+
hypers = hypers or get_default_hypers(device, dtype)
|
|
278
|
+
|
|
279
|
+
def _to_float(x):
|
|
280
|
+
return float(x[0]) if hasattr(x, "__len__") else float(x)
|
|
281
|
+
|
|
282
|
+
def _to_list(x):
|
|
283
|
+
return x.tolist() if hasattr(x, "tolist") else list(x)
|
|
284
|
+
|
|
285
|
+
radial = _ANIRadial(
|
|
286
|
+
_to_float(hypers["EtaR"]),
|
|
287
|
+
_to_list(hypers["ShfR"]),
|
|
288
|
+
float(hypers["Rcr"]),
|
|
289
|
+
)
|
|
290
|
+
angular = _ANIAngular(
|
|
291
|
+
_to_float(hypers["EtaA"]),
|
|
292
|
+
_to_float(hypers["Zeta"]),
|
|
293
|
+
_to_list(hypers["ShfA"]),
|
|
294
|
+
_to_list(hypers["ShfZ"]),
|
|
295
|
+
float(hypers["Rca"]),
|
|
296
|
+
)
|
|
297
|
+
self._aev_computer = _torchani.AEVComputer(
|
|
298
|
+
radial,
|
|
299
|
+
angular,
|
|
300
|
+
num_species=num_species,
|
|
301
|
+
).to(device=device, dtype=dtype)
|
|
302
|
+
# Whether TorchANI's cuAEV can be used for CUDA inputs (see forward).
|
|
303
|
+
self._cuaev = _cuaev_available(self._aev_computer)
|
|
304
|
+
|
|
305
|
+
# Parameters for the fused AEV operator, taken from TorchANI's
|
|
306
|
+
# terms so that the AEVs are identical. The operator is compiled
|
|
307
|
+
# for the ANI-2x AEV layout (7 species, 16 radial shifts, 8
|
|
308
|
+
# angular shifts and 4 angle sections).
|
|
309
|
+
if fused:
|
|
310
|
+
if num_species != 7:
|
|
311
|
+
raise ValueError("'fused' AEVs are only supported for 7 species")
|
|
312
|
+
radial = self._aev_computer.radial
|
|
313
|
+
angular = self._aev_computer.angular
|
|
314
|
+
self._fused_params = (
|
|
315
|
+
_to_list(radial.shifts.flatten()),
|
|
316
|
+
_to_list(angular.shifts.flatten()),
|
|
317
|
+
_to_list(angular.sections.flatten()),
|
|
318
|
+
float(radial.eta),
|
|
319
|
+
float(radial.cutoff),
|
|
320
|
+
float(angular.eta),
|
|
321
|
+
float(angular.zeta),
|
|
322
|
+
float(angular.cutoff),
|
|
323
|
+
)
|
|
324
|
+
# Create a dummy function to use in forward.
|
|
325
|
+
else:
|
|
326
|
+
self._aev_computer = self._dummy_aev_computer
|
|
327
|
+
|
|
328
|
+
if zid_map is None:
|
|
329
|
+
zid_map = {i: i for i in range(num_species)}
|
|
330
|
+
if isinstance(zid_map, dict):
|
|
331
|
+
self._zid_map = -_torch.ones(
|
|
332
|
+
num_species + 1, dtype=_torch.int, device=device
|
|
333
|
+
)
|
|
334
|
+
for self_atom_zid, aev_atom_zid in zid_map.items():
|
|
335
|
+
self._zid_map[self_atom_zid] = aev_atom_zid
|
|
336
|
+
elif isinstance(zid_map, _torch.Tensor):
|
|
337
|
+
self._zid_map = zid_map
|
|
338
|
+
elif isinstance(zid_map, (list, tuple, _np.ndarray)):
|
|
339
|
+
self._zid_map = _torch.tensor(zid_map, dtype=_torch.int64, device=device)
|
|
340
|
+
else:
|
|
341
|
+
raise ValueError("zid_map must be a dict, torch.Tensor, list or tuple")
|
|
342
|
+
|
|
343
|
+
def forward(self, zid, xyz):
|
|
344
|
+
"""
|
|
345
|
+
Evaluate the AEVs.
|
|
346
|
+
|
|
347
|
+
Parameters
|
|
348
|
+
----------
|
|
349
|
+
|
|
350
|
+
zid: torch.Tensor (N_BATCH, MAX_N_ATOMS)
|
|
351
|
+
The species indices.
|
|
352
|
+
|
|
353
|
+
xyz: torch.Tensor (N_BATCH, MAX_N_ATOMS, 3)
|
|
354
|
+
The atomic coordinates.
|
|
355
|
+
|
|
356
|
+
Returns
|
|
357
|
+
-------
|
|
358
|
+
|
|
359
|
+
aevs: torch.Tensor (N_BATCH, MAX_N_ATOMS, N_AEV_COMPONENTS)
|
|
360
|
+
The atomic environment vectors.
|
|
361
|
+
"""
|
|
362
|
+
if not self._is_external:
|
|
363
|
+
zid_aev = self._zid_map[zid]
|
|
364
|
+
if self._cuaev:
|
|
365
|
+
self._aev_computer._strategy = "cuaev" if xyz.is_cuda else "pyaev"
|
|
366
|
+
if self._fused:
|
|
367
|
+
if xyz.is_cuda:
|
|
368
|
+
p = self._fused_params
|
|
369
|
+
aev = _torch.ops.emle.aev(
|
|
370
|
+
zid_aev, xyz, p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]
|
|
371
|
+
)
|
|
372
|
+
else:
|
|
373
|
+
aev = self._aev_computer(zid_aev, xyz)
|
|
374
|
+
elif self._dense:
|
|
375
|
+
radial = self._aev_computer.radial
|
|
376
|
+
angular = self._aev_computer.angular
|
|
377
|
+
aev = self._dense_aev(
|
|
378
|
+
zid_aev,
|
|
379
|
+
xyz,
|
|
380
|
+
self._aev_computer.num_species,
|
|
381
|
+
radial.eta,
|
|
382
|
+
radial.shifts,
|
|
383
|
+
radial.cutoff,
|
|
384
|
+
angular.eta,
|
|
385
|
+
angular.zeta,
|
|
386
|
+
angular.shifts,
|
|
387
|
+
angular.sections,
|
|
388
|
+
angular.cutoff,
|
|
389
|
+
)
|
|
390
|
+
else:
|
|
391
|
+
aev = self._aev_computer(zid_aev, xyz)
|
|
392
|
+
else:
|
|
393
|
+
aev = self._aev
|
|
394
|
+
|
|
395
|
+
norm = _torch.linalg.norm(aev, dim=2, keepdim=True)
|
|
396
|
+
|
|
397
|
+
aev = self._apply_mask(_torch.where(zid.unsqueeze(2) > -1, aev / norm, 0.0))
|
|
398
|
+
|
|
399
|
+
return aev
|
|
400
|
+
|
|
401
|
+
@staticmethod
|
|
402
|
+
def _dense_aev(
|
|
403
|
+
species: Tensor,
|
|
404
|
+
xyz: Tensor,
|
|
405
|
+
num_species: int,
|
|
406
|
+
radial_eta: Tensor,
|
|
407
|
+
radial_shifts: Tensor,
|
|
408
|
+
radial_cutoff: float,
|
|
409
|
+
angular_eta: Tensor,
|
|
410
|
+
angular_zeta: Tensor,
|
|
411
|
+
angular_shifts: Tensor,
|
|
412
|
+
angular_sections: Tensor,
|
|
413
|
+
angular_cutoff: float,
|
|
414
|
+
) -> Tensor:
|
|
415
|
+
"""
|
|
416
|
+
Compute ANI-style AEVs over all atom pairs and triples, with the same
|
|
417
|
+
terms and feature layout as TorchANI's AEVComputer (cosine cutoff).
|
|
418
|
+
|
|
419
|
+
Parameters
|
|
420
|
+
----------
|
|
421
|
+
|
|
422
|
+
species: torch.Tensor (N_BATCH, MAX_N_ATOMS)
|
|
423
|
+
The AEV species indices (-1 for padding).
|
|
424
|
+
|
|
425
|
+
xyz: torch.Tensor (N_BATCH, MAX_N_ATOMS, 3)
|
|
426
|
+
The atomic coordinates.
|
|
427
|
+
|
|
428
|
+
num_species, radial_*, angular_*:
|
|
429
|
+
The AEV parameters (taken from TorchANI's radial and angular
|
|
430
|
+
terms).
|
|
431
|
+
|
|
432
|
+
Returns
|
|
433
|
+
-------
|
|
434
|
+
|
|
435
|
+
aevs: torch.Tensor (N_BATCH, MAX_N_ATOMS, N_AEV_COMPONENTS)
|
|
436
|
+
The atomic environment vectors.
|
|
437
|
+
"""
|
|
438
|
+
n_batch, n_atoms = species.shape
|
|
439
|
+
pi: float = 3.141592653589793
|
|
440
|
+
|
|
441
|
+
# One-hot species, and the mask of valid (distinct, real) pairs.
|
|
442
|
+
real = species >= 0
|
|
443
|
+
onehot = (
|
|
444
|
+
species.unsqueeze(-1) == _torch.arange(num_species, device=xyz.device)
|
|
445
|
+
).to(xyz.dtype)
|
|
446
|
+
not_eye = ~_torch.eye(n_atoms, dtype=_torch.bool, device=xyz.device)
|
|
447
|
+
pair = real.unsqueeze(2) & real.unsqueeze(1) & not_eye
|
|
448
|
+
|
|
449
|
+
# Displacements x_j - x_i and distances, made safe for invalid pairs.
|
|
450
|
+
v = (xyz.unsqueeze(1) - xyz.unsqueeze(2)) * pair.unsqueeze(-1)
|
|
451
|
+
d2 = (v * v).sum(-1)
|
|
452
|
+
r = _torch.where(pair, d2, _torch.ones_like(d2)).sqrt()
|
|
453
|
+
|
|
454
|
+
# Radial terms, summed over the neighbours of each species.
|
|
455
|
+
fc = _torch.where(
|
|
456
|
+
pair & (r <= radial_cutoff),
|
|
457
|
+
0.5 * _torch.cos(pi * r / radial_cutoff) + 0.5,
|
|
458
|
+
_torch.zeros_like(r),
|
|
459
|
+
)
|
|
460
|
+
terms = (
|
|
461
|
+
0.25
|
|
462
|
+
* _torch.exp(-radial_eta * (r.unsqueeze(-1) - radial_shifts) ** 2)
|
|
463
|
+
* fc.unsqueeze(-1)
|
|
464
|
+
)
|
|
465
|
+
radial_aev = _torch.einsum("bijf,bjs->bisf", terms, onehot)
|
|
466
|
+
|
|
467
|
+
# Angular terms for all (ordered) pairs of distinct neighbours j, k.
|
|
468
|
+
fc = _torch.where(
|
|
469
|
+
pair & (r <= angular_cutoff),
|
|
470
|
+
0.5 * _torch.cos(pi * r / angular_cutoff) + 0.5,
|
|
471
|
+
_torch.zeros_like(r),
|
|
472
|
+
)
|
|
473
|
+
fc = fc.unsqueeze(3) * fc.unsqueeze(2) * not_eye
|
|
474
|
+
cos = (v.unsqueeze(3) * v.unsqueeze(2)).sum(-1) / _torch.clamp(
|
|
475
|
+
r.unsqueeze(3) * r.unsqueeze(2), min=1e-10
|
|
476
|
+
)
|
|
477
|
+
angles = _torch.acos(0.95 * cos).unsqueeze(-1)
|
|
478
|
+
angular = 2 * ((1 + _torch.cos(angles - angular_sections)) / 2) ** angular_zeta
|
|
479
|
+
mean = 0.5 * (r.unsqueeze(3) + r.unsqueeze(2)).unsqueeze(-1)
|
|
480
|
+
radial = _torch.exp(-angular_eta * (mean - angular_shifts) ** 2)
|
|
481
|
+
terms = (radial.unsqueeze(-1) * angular.unsqueeze(-2)).flatten(-2)
|
|
482
|
+
terms = terms * fc.unsqueeze(-1)
|
|
483
|
+
|
|
484
|
+
# Sum over the species of j and k, then over unordered species pairs
|
|
485
|
+
# (each unordered pair of neighbours appears twice in the sum above).
|
|
486
|
+
per_pair = _torch.einsum(
|
|
487
|
+
"bijcf,bja->biacf",
|
|
488
|
+
_torch.einsum("bijkf,bkc->bijcf", terms, onehot),
|
|
489
|
+
onehot,
|
|
490
|
+
)
|
|
491
|
+
triu = _torch.triu_indices(num_species, num_species, device=xyz.device)
|
|
492
|
+
a = triu[0]
|
|
493
|
+
c = triu[1]
|
|
494
|
+
angular_aev = _torch.where(
|
|
495
|
+
(a == c).view(1, 1, -1, 1),
|
|
496
|
+
0.5 * per_pair[:, :, a, c],
|
|
497
|
+
0.5 * (per_pair[:, :, a, c] + per_pair[:, :, c, a]),
|
|
498
|
+
)
|
|
499
|
+
|
|
500
|
+
return _torch.cat(
|
|
501
|
+
(
|
|
502
|
+
radial_aev.reshape(n_batch, n_atoms, -1),
|
|
503
|
+
angular_aev.reshape(n_batch, n_atoms, -1),
|
|
504
|
+
),
|
|
505
|
+
dim=-1,
|
|
506
|
+
)
|
|
507
|
+
|
|
508
|
+
@staticmethod
|
|
509
|
+
def _dummy_aev_computer(elem_idxs: Tensor, coords: Tensor) -> Tensor:
|
|
510
|
+
"""
|
|
511
|
+
Dummy function to use in forward if AEVs are computed externally.
|
|
512
|
+
|
|
513
|
+
Parameters
|
|
514
|
+
----------
|
|
515
|
+
|
|
516
|
+
elem_idxs: torch.Tensor (N_BATCH, MAX_N_ATOMS)
|
|
517
|
+
The species indices.
|
|
518
|
+
|
|
519
|
+
coords: torch.Tensor (N_BATCH, MAX_N_ATOMS, 3)
|
|
520
|
+
The atomic coordinates.
|
|
521
|
+
|
|
522
|
+
Returns
|
|
523
|
+
-------
|
|
524
|
+
|
|
525
|
+
aevs: torch.Tensor (N_BATCH, MAX_N_ATOMS, N_AEV_COMPONENTS)
|
|
526
|
+
The atomic environment vectors.
|
|
527
|
+
"""
|
|
528
|
+
return _torch.empty(0, dtype=_torch.float32)
|
|
529
|
+
|
|
530
|
+
def _apply_mask(self, aev):
|
|
531
|
+
"""
|
|
532
|
+
Apply the mask to the AEVs.
|
|
533
|
+
|
|
534
|
+
Parameters
|
|
535
|
+
----------
|
|
536
|
+
|
|
537
|
+
aev: torch.Tensor
|
|
538
|
+
The AEVs to mask.
|
|
539
|
+
|
|
540
|
+
Returns
|
|
541
|
+
-------
|
|
542
|
+
|
|
543
|
+
aev: torch.Tensor
|
|
544
|
+
The masked AEVs.
|
|
545
|
+
"""
|
|
546
|
+
# Precomputed indices avoid a nonzero, and a device synchronisation, on
|
|
547
|
+
# every call.
|
|
548
|
+
return aev.index_select(2, self._mask_index) if self._mask is not None else aev
|
|
549
|
+
|
|
550
|
+
def _set_mask_index(self):
|
|
551
|
+
"""
|
|
552
|
+
Store the indices of the features selected by the mask.
|
|
553
|
+
"""
|
|
554
|
+
if self._mask is not None:
|
|
555
|
+
self._mask_index = _torch.nonzero(self._mask).squeeze(-1)
|
|
556
|
+
else:
|
|
557
|
+
self._mask_index = _torch.empty(0, dtype=_torch.int64)
|
|
558
|
+
|
|
559
|
+
def to(self, *args, **kwargs):
|
|
560
|
+
"""
|
|
561
|
+
Performs Tensor dtype and/or device conversion on the model.
|
|
562
|
+
"""
|
|
563
|
+
if not self._is_external:
|
|
564
|
+
self._aev_computer = self._aev_computer.to(*args, **kwargs)
|
|
565
|
+
if self._mask is not None:
|
|
566
|
+
self._mask = self._mask.to(*args, **kwargs)
|
|
567
|
+
self._set_mask_index()
|
|
568
|
+
self._zid_map = self._zid_map.to(*args, **kwargs)
|
|
569
|
+
|
|
570
|
+
# Check for a device type in args and update the device attribute.
|
|
571
|
+
for arg in args:
|
|
572
|
+
if isinstance(arg, _torch.device):
|
|
573
|
+
self._device = arg
|
|
574
|
+
break
|
|
575
|
+
|
|
576
|
+
return self
|
|
577
|
+
|
|
578
|
+
def cuda(self, **kwargs):
|
|
579
|
+
"""
|
|
580
|
+
Move all model parameters and buffers to CUDA memory.
|
|
581
|
+
"""
|
|
582
|
+
if not self._is_external:
|
|
583
|
+
self._aev_computer = self._aev_computer.cuda(**kwargs)
|
|
584
|
+
if self._mask is not None:
|
|
585
|
+
self._mask = self._mask.cuda(**kwargs)
|
|
586
|
+
self._set_mask_index()
|
|
587
|
+
self._zid_map = self._zid_map.cuda(**kwargs)
|
|
588
|
+
return self
|
|
589
|
+
|
|
590
|
+
def cpu(self, **kwargs):
|
|
591
|
+
"""
|
|
592
|
+
Move all model parameters and buffers to CPU memory.
|
|
593
|
+
"""
|
|
594
|
+
if not self._is_external:
|
|
595
|
+
self._aev_computer = self._aev_computer.cpu(**kwargs)
|
|
596
|
+
if self._mask is not None:
|
|
597
|
+
self._mask = self._mask.cpu(**kwargs)
|
|
598
|
+
self._set_mask_index()
|
|
599
|
+
self._zid_map = self._zid_map.cpu(**kwargs)
|
|
600
|
+
return self
|
|
601
|
+
|
|
602
|
+
def double(self):
|
|
603
|
+
"""
|
|
604
|
+
Casts all floating point model parameters and buffers to float64 precision.
|
|
605
|
+
"""
|
|
606
|
+
if not self._is_external:
|
|
607
|
+
self._aev_computer = self._aev_computer.double()
|
|
608
|
+
return self
|
|
609
|
+
|
|
610
|
+
def float(self):
|
|
611
|
+
"""
|
|
612
|
+
Casts all floating point model parameters and buffers to float32 precision.
|
|
613
|
+
"""
|
|
614
|
+
if not self._is_external:
|
|
615
|
+
self._aev_computer = self._aev_computer.float()
|
|
616
|
+
return self
|