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.

Files changed (51) hide show
  1. emle/__init__.py +39 -0
  2. emle/_analyzer.py +337 -0
  3. emle/_backends/__init__.py +14 -0
  4. emle/_backends/_ace.py +141 -0
  5. emle/_backends/_aimnet2.py +136 -0
  6. emle/_backends/_backend.py +148 -0
  7. emle/_backends/_deepmd.py +186 -0
  8. emle/_backends/_omnip2x.py +349 -0
  9. emle/_backends/_orca.py +322 -0
  10. emle/_backends/_sander.py +232 -0
  11. emle/_backends/_sqm.py +230 -0
  12. emle/_backends/_xtb.py +126 -0
  13. emle/_compiler.py +279 -0
  14. emle/_csrc/ops.cpp +350 -0
  15. emle/_csrc/ops_cuda.cu +454 -0
  16. emle/_ops.py +173 -0
  17. emle/_orca_parser.py +331 -0
  18. emle/_resources.py +32 -0
  19. emle/_socket.py +211 -0
  20. emle/_tarball_to_extXYZ.py +236 -0
  21. emle/_units.py +16 -0
  22. emle/_utils.py +57 -0
  23. emle/calculator.py +1974 -0
  24. emle/models/__init__.py +13 -0
  25. emle/models/_ani.py +315 -0
  26. emle/models/_deepmd.py +410 -0
  27. emle/models/_emle.py +629 -0
  28. emle/models/_emle_aev_computer.py +616 -0
  29. emle/models/_emle_base.py +1063 -0
  30. emle/models/_mace.py +1368 -0
  31. emle/models/_patches.py +66 -0
  32. emle/models/_utils.py +279 -0
  33. emle/train/__init__.py +8 -0
  34. emle/train/_extxyz.py +63 -0
  35. emle/train/_gpr.py +205 -0
  36. emle/train/_ivm.py +126 -0
  37. emle/train/_loss.py +357 -0
  38. emle/train/_trainer.py +709 -0
  39. emle/train/_utils.py +92 -0
  40. emle_engine-2.0.0.data/scripts/emle-analyze +175 -0
  41. emle_engine-2.0.0.data/scripts/emle-compile +162 -0
  42. emle_engine-2.0.0.data/scripts/emle-server +777 -0
  43. emle_engine-2.0.0.data/scripts/emle-stop +21 -0
  44. emle_engine-2.0.0.data/scripts/emle-train +127 -0
  45. emle_engine-2.0.0.data/scripts/orca +95 -0
  46. emle_engine-2.0.0.data/scripts/tarball-to-extxyz +6 -0
  47. emle_engine-2.0.0.dist-info/METADATA +49 -0
  48. emle_engine-2.0.0.dist-info/RECORD +51 -0
  49. emle_engine-2.0.0.dist-info/WHEEL +5 -0
  50. emle_engine-2.0.0.dist-info/licenses/LICENSE +21 -0
  51. emle_engine-2.0.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,13 @@
1
+ # SPDX-FileCopyrightText: 2023-2026 The EMLE-Engine developers
2
+ # SPDX-License-Identifier: MIT
3
+
4
+ """
5
+ .. currentmodule:: emle.models
6
+ """
7
+
8
+ from ._emle_aev_computer import EMLEAEVComputer
9
+ from ._emle_base import EMLEBase
10
+ from ._emle import EMLE
11
+ from ._ani import ANI2xEMLE
12
+ from ._deepmd import DeePMDEMLE
13
+ from ._mace import MACEEMLE, MACEEMLEJoint
emle/models/_ani.py ADDED
@@ -0,0 +1,315 @@
1
+ # SPDX-FileCopyrightText: 2023-2026 The EMLE-Engine developers
2
+ # SPDX-License-Identifier: MIT
3
+
4
+ """ANI2xEMLE model implementation."""
5
+
6
+ __author__ = "Lester Hedges"
7
+ __email__ = "lester.hedges@gmail.com"
8
+
9
+ __all__ = ["ANI2xEMLE"]
10
+
11
+ import torch as _torch
12
+ import torchani as _torchani
13
+
14
+ from torch import Tensor
15
+ from typing import Optional, Tuple
16
+
17
+ from ._emle import EMLE as _EMLE
18
+ from . import _patches
19
+ from ._emle_aev_computer import _cuaev_available
20
+
21
+
22
+ # Module-level hook so TorchScript can access its source during compilation.
23
+ # TorchANI >= 2.7.9: AEVComputer.forward(elem_idxs, coords, cell, pbc) -> Tensor.
24
+ def _aev_hook(
25
+ module,
26
+ input: Tuple[Tensor, Optional[Tensor], Optional[Tensor], Optional[Tensor]],
27
+ output: Tensor,
28
+ ):
29
+ module._aev = output
30
+
31
+
32
+ class ANI2xEMLE(_torch.nn.Module):
33
+ """
34
+ Combined ANI2x and EMLE model. Predicts the in vacuo ANI2x energy along with
35
+ static and induced EMLE energy components.
36
+ """
37
+
38
+ # A TorchScript constant, so that the cuAEV strategy selection in forward
39
+ # is only compiled for ANI2x models that can use it.
40
+ __constants__ = ["_cuaev"]
41
+
42
+ # Class attributes.
43
+
44
+ # A flag for type inference. TorchScript doesn't support inheritance, so
45
+ # we need to check for an object of type torch.nn.Module, and that it has
46
+ # the required _is_emle attribute.
47
+ _is_emle = True
48
+
49
+ def __init__(
50
+ self,
51
+ emle_model=None,
52
+ emle_method="electrostatic",
53
+ alpha_mode="fixed",
54
+ mm_charges=None,
55
+ qm_charge=0,
56
+ model_index=None,
57
+ ani2x_model=None,
58
+ device=None,
59
+ dtype=None,
60
+ ):
61
+ """
62
+ Constructor.
63
+
64
+ Parameters
65
+ ----------
66
+
67
+ emle_model: str
68
+ Path to a custom EMLE model parameter file. If None, then the
69
+ default model for the specified 'alpha_mode' will be used.
70
+
71
+ emle_method: str
72
+ The desired embedding method. Options are:
73
+ "electrostatic":
74
+ Full ML electrostatic embedding.
75
+ "mechanical":
76
+ ML predicted charges for the core, but zero valence charge.
77
+ "nonpol":
78
+ Non-polarisable ML embedding. Here the induced component of
79
+ the potential is zeroed.
80
+ "mm":
81
+ MM charges are used for the core charge and valence charges
82
+ are set to zero.
83
+
84
+ alpha_mode: str
85
+ How atomic polarizabilities are calculated.
86
+ "fixed":
87
+ one volume scaling factor is used for each species
88
+ "flexible":
89
+ scaling factors are obtained with GPR using the values learned
90
+ for each reference environment
91
+
92
+ mm_charges: List[float], Tuple[Float], numpy.ndarray, torch.Tensor
93
+ List of MM charges for atoms in the QM region in units of mod
94
+ electron charge. This is required if the 'mm' method is specified.
95
+
96
+ qm_charge: int
97
+ The charge on the QM region. This can also be passed when calling
98
+ the forward method. The non-default value will take precendence.
99
+
100
+ model_index: int
101
+ The index of the ANI2x model to use. If None, then the full 8 model
102
+ ensemble will be used.
103
+
104
+ ani2x_model: torchani.models.ANI2x
105
+ An existing ANI2x model to use. If None, a new ANI2x model will be
106
+ created. Please ensure that the model was created using
107
+ periodic_table_index=True.
108
+
109
+ device: torch.device
110
+ The device on which to run the model.
111
+
112
+ dtype: torch.dtype
113
+ The data type to use for the models floating point tensors.
114
+ """
115
+
116
+ # Call the base class constructor.
117
+ super().__init__()
118
+
119
+ if model_index is not None:
120
+ if not isinstance(model_index, int):
121
+ raise TypeError("'model_index' must be of type 'int'")
122
+ if model_index < 0 or model_index > 7:
123
+ raise ValueError("'model_index' must be in the range [0, 7]")
124
+ self._model_index = model_index
125
+
126
+ if device is not None:
127
+ if not isinstance(device, _torch.device):
128
+ raise TypeError("'device' must be of type 'torch.device'")
129
+ else:
130
+ device = _torch.get_default_device()
131
+ self._device = device
132
+
133
+ if dtype is not None:
134
+ if not isinstance(dtype, _torch.dtype):
135
+ raise TypeError("'dtype' must be of type 'torch.dtype'")
136
+ else:
137
+ dtype = _torch.get_default_dtype()
138
+
139
+ # Create an instance of the EMLE model.
140
+ self._emle = _EMLE(
141
+ model=emle_model,
142
+ method=emle_method,
143
+ alpha_mode=alpha_mode,
144
+ mm_charges=mm_charges,
145
+ qm_charge=qm_charge,
146
+ device=device,
147
+ dtype=dtype,
148
+ create_aev_calculator=False,
149
+ )
150
+
151
+ if ani2x_model is not None:
152
+ if not isinstance(ani2x_model, _torch.nn.Module):
153
+ raise TypeError("'ani2x_model' must be a torch.nn.Module")
154
+
155
+ if (
156
+ hasattr(ani2x_model, "periodic_table_index")
157
+ and not ani2x_model.periodic_table_index
158
+ ):
159
+ raise ValueError(
160
+ "The ANI2x model must be created with 'periodic_table_index=True'"
161
+ )
162
+
163
+ raw_ani2x = ani2x_model.to(device)
164
+ if dtype == _torch.float64:
165
+ raw_ani2x = raw_ani2x.double()
166
+ else:
167
+ # Create the ANI2x model.
168
+ raw_ani2x = _torchani.models.ANI2x(
169
+ periodic_table_index=True, model_index=model_index
170
+ ).to(device)
171
+ if dtype == _torch.float64:
172
+ raw_ani2x = raw_ani2x.double()
173
+
174
+ # Wrap in PatchedANI2x so that aev_computer is a direct attribute
175
+ # (not a property) — this gives TorchScript a single unambiguous path
176
+ # and allows forward hooks to be shared correctly.
177
+ self._ani2x = _patches.PatchedANI2x(raw_ani2x)
178
+
179
+ # Whether to use cuAEV for CUDA inputs, only for ANI2x models created
180
+ # here.
181
+ self._cuaev = ani2x_model is None and _cuaev_available(
182
+ self._ani2x.aev_computer
183
+ )
184
+
185
+ # Register a forward hook on aev_computer to capture AEVs.
186
+ self._ani2x.aev_computer._aev = _torch.empty(0, device=self._device)
187
+ self._aev_hook = self._ani2x.aev_computer.register_forward_hook(_aev_hook)
188
+
189
+ def to(self, *args, **kwargs):
190
+ """
191
+ Performs Tensor dtype and/or device conversion on the model.
192
+ """
193
+ self._emle = self._emle.to(*args, **kwargs)
194
+ self._ani2x = self._ani2x.to(*args, **kwargs)
195
+
196
+ # Check for a device type in args and update the device attribute.
197
+ for arg in args:
198
+ if isinstance(arg, _torch.device):
199
+ self._device = arg
200
+ break
201
+
202
+ return self
203
+
204
+ def cpu(self, **kwargs):
205
+ """
206
+ Move all model parameters and buffers to CPU memory.
207
+ """
208
+ self._emle = self._emle.cpu(**kwargs)
209
+ self._ani2x = self._ani2x.cpu(**kwargs)
210
+ self._device = _torch.device("cpu")
211
+ return self
212
+
213
+ def cuda(self, **kwargs):
214
+ """
215
+ Move all model parameters and buffers to CUDA memory.
216
+ """
217
+ self._emle = self._emle.cuda(**kwargs)
218
+ self._ani2x = self._ani2x.cuda(**kwargs)
219
+ self._device = _torch.device("cuda")
220
+ return self
221
+
222
+ def double(self):
223
+ """
224
+ Casts all model parameters and buffers to float64 precision.
225
+ """
226
+ self._emle = self._emle.double()
227
+ self._ani2x = self._ani2x.double()
228
+ return self
229
+
230
+ def float(self):
231
+ """
232
+ Casts all model parameters and buffers to float32 precision.
233
+ """
234
+ self._emle = self._emle.float()
235
+ # Using .float() or .to(torch.float32) is broken for ANI2x models.
236
+ raw_ani2x = _torchani.models.ANI2x(
237
+ periodic_table_index=True,
238
+ model_index=self._model_index,
239
+ ).to(self._device)
240
+
241
+ self._ani2x = _patches.PatchedANI2x(raw_ani2x)
242
+ self._cuaev = _cuaev_available(self._ani2x.aev_computer)
243
+ self._ani2x.aev_computer._aev = _torch.empty(0, device=self._device)
244
+ self._aev_hook = self._ani2x.aev_computer.register_forward_hook(_aev_hook)
245
+
246
+ return self
247
+
248
+ def forward(
249
+ self,
250
+ atomic_numbers: Tensor,
251
+ charges_mm: Tensor,
252
+ xyz_qm: Tensor,
253
+ xyz_mm: Tensor,
254
+ cell: Optional[Tensor] = None,
255
+ qm_charge: int = 0,
256
+ ) -> Tensor:
257
+ """
258
+ Compute the the ANI2x and static and induced EMLE energy components.
259
+
260
+ Parameters
261
+ ----------
262
+
263
+ atomic_numbers: torch.Tensor (N_QM_ATOMS,) or (BATCH, N_QM_ATOMS)
264
+ Atomic numbers of QM atoms. A non-existent atom is represented by -1.
265
+
266
+ charges_mm: torch.Tensor (max_mm_atoms,) or (BATCH, max_mm_atoms)
267
+ MM point charges in atomic units.
268
+
269
+ xyz_qm: torch.Tensor (N_QM_ATOMS, 3) or (BATCH, N_QM_ATOMS, 3)
270
+ Positions of QM atoms in Angstrom.
271
+
272
+ xyz_mm: torch.Tensor (N_MM_ATOMS, 3) or (BATCH, N_MM_ATOMS, 3)
273
+ Positions of MM atoms in Angstrom.
274
+
275
+ cell: torch.Tensor (3, 3) or (BATCH, 3, 3), optional
276
+ The simulation cell vectors in Angstrom.
277
+
278
+ qm_charge: int or torch.Tensor (BATCH,)
279
+ The charge on the QM region.
280
+
281
+ Returns
282
+ -------
283
+
284
+ result: torch.Tensor (3,) or (3, BATCH)
285
+ The ANI2x and static and induced EMLE energy components in Hartree.
286
+ """
287
+ # Batch the inputs if necessary.
288
+ if atomic_numbers.ndim == 1:
289
+ atomic_numbers = atomic_numbers.unsqueeze(0)
290
+ xyz_qm = xyz_qm.unsqueeze(0)
291
+ xyz_mm = xyz_mm.unsqueeze(0)
292
+ charges_mm = charges_mm.unsqueeze(0)
293
+
294
+ # Get the in vacuo energy.
295
+ if self._cuaev:
296
+ self._ani2x.aev_computer._strategy = "cuaev" if xyz_qm.is_cuda else "pyaev"
297
+ E_vac = self._ani2x((atomic_numbers, xyz_qm)).energies
298
+
299
+ # If there are no point charges, return the in vacuo energy and zeros
300
+ # for the static and induced terms.
301
+ if xyz_mm.shape[1] == 0:
302
+ zero = _torch.zeros(
303
+ atomic_numbers.shape[0], dtype=xyz_qm.dtype, device=xyz_qm.device
304
+ )
305
+ return _torch.stack((E_vac, zero, zero))
306
+
307
+ # Set the AEVs captured by the forward hook as an attribute of the
308
+ # EMLE AEVComputer instance.
309
+ self._emle._emle_base._emle_aev_computer._aev = self._ani2x.aev_computer._aev
310
+
311
+ # Get the EMLE energy components.
312
+ E_emle = self._emle(atomic_numbers, charges_mm, xyz_qm, xyz_mm, cell, qm_charge)
313
+
314
+ # Return the ANI2x and EMLE energy components.
315
+ return _torch.stack((E_vac, E_emle[0], E_emle[1]))