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
emle/_csrc/ops.cpp ADDED
@@ -0,0 +1,350 @@
1
+ // SPDX-FileCopyrightText: 2023-2026 The EMLE-Engine developers
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ // Fused operators for EMLE: operator definitions, CPU kernels and autograd.
5
+ //
6
+ // emle::electrostatics(xq, xm, q, s, q_core, q_val, mask)
7
+ // -> (E_static (B,), fields (B, N, 3), F0 (B, N, 3))
8
+ // for QM positions xq (B, N, 3) and MM positions xm (B, M, 3) in Bohr, MM
9
+ // charges q (B, M), valence widths s, core and valence charges (B, N) and the
10
+ // mask of real QM atoms (B, N):
11
+ // E_static = sum_nm q_m (q_core_n R_nm + q_val_n T0(r_nm, s_n))
12
+ // fields_n = sum_m q_m f1(r'_nm, 2 s_n) T1_nm
13
+ // F0_n = sum_m q_m T1_nm
14
+ // with d = x_m - x_n, r = |d|, R = 1 / (r + 1e-10), r' = 1 / R, T1 = -d R^3,
15
+ // T0(r, s) = (1 - (1 + r / (2 s)) exp(-r / s)) / r and
16
+ // f1(r, S) = 1 - (1 + x + x^2 / 2) exp(-x), x = r / S
17
+ // (the same expressions as EMLEBase._get_mesh_data, get_static_energy and
18
+ // _get_mu_ind). The (N, M) pair terms are never stored.
19
+ //
20
+ // emle::aev(species, xyz, ...) -> (B, N, 1008) TorchANI-style AEVs (CUDA).
21
+ #include <torch/extension.h>
22
+ #include <torch/library.h>
23
+ #include <torch/autograd.h>
24
+ #include <ATen/Parallel.h>
25
+ #include <cmath>
26
+ #include <vector>
27
+ #ifdef _OPENMP
28
+ #include <omp.h>
29
+ #endif
30
+
31
+ using torch::Tensor;
32
+ using torch::autograd::AutogradContext;
33
+ using torch::autograd::tensor_list;
34
+
35
+ namespace {
36
+
37
+ constexpr double EPS = 1e-10;
38
+
39
+ template <typename scalar_t>
40
+ void electrostatics_forward_cpu_impl(const Tensor& xq, const Tensor& xm, const Tensor& q, const Tensor& s,
41
+ const Tensor& qc, const Tensor& qv, const Tensor& mask, Tensor& E,
42
+ Tensor& fields, Tensor& F0) {
43
+ const int64_t B = xq.size(0), N = xq.size(1), M = xm.size(1);
44
+ auto XQ = xq.accessor<scalar_t, 3>(); auto XM = xm.accessor<scalar_t, 3>();
45
+ auto Q = q.accessor<scalar_t, 2>(); auto S = s.accessor<scalar_t, 2>();
46
+ auto QC = qc.accessor<scalar_t, 2>(); auto QV = qv.accessor<scalar_t, 2>();
47
+ auto MK = mask.accessor<bool, 2>();
48
+ auto FA = fields.accessor<scalar_t, 3>(); auto F0A = F0.accessor<scalar_t, 3>();
49
+ std::vector<double> e_rows(B * N, 0.0);
50
+ at::parallel_for(0, B * N, 1, [&](int64_t begin, int64_t end) {
51
+ for (int64_t bn = begin; bn < end; ++bn) {
52
+ const int64_t b = bn / N, n = bn % N;
53
+ double es = 0, f[3] = {0, 0, 0}, f0[3] = {0, 0, 0};
54
+ if (MK[b][n]) {
55
+ const double sn = S[b][n], qcn = QC[b][n], qvn = QV[b][n];
56
+ const double x0 = XQ[b][n][0], x1 = XQ[b][n][1], x2 = XQ[b][n][2];
57
+ for (int64_t m = 0; m < M; ++m) {
58
+ const double d0 = XM[b][m][0] - x0, d1 = XM[b][m][1] - x1, d2 = XM[b][m][2] - x2;
59
+ const double r = std::sqrt(d0 * d0 + d1 * d1 + d2 * d2);
60
+ const double qm = Q[b][m];
61
+ const double R = 1.0 / (r + EPS);
62
+ const double T0 = (1.0 - (1.0 + r / (sn * 2.0)) * std::exp(-r / sn)) / r;
63
+ es += qm * (qcn * R + qvn * T0);
64
+ const double R3 = R * R * R;
65
+ const double x = (1.0 / R) / (2.0 * sn);
66
+ const double f1 = 1.0 - (1.0 + x * (1.0 + 0.5 * x)) * std::exp(-x);
67
+ const double w0 = -qm * R3, w1 = w0 * f1;
68
+ f0[0] += w0 * d0; f0[1] += w0 * d1; f0[2] += w0 * d2;
69
+ f[0] += w1 * d0; f[1] += w1 * d1; f[2] += w1 * d2;
70
+ }
71
+ }
72
+ e_rows[bn] = es;
73
+ for (int k = 0; k < 3; ++k) { FA[b][n][k] = f[k]; F0A[b][n][k] = f0[k]; }
74
+ }
75
+ });
76
+ auto EA = E.accessor<scalar_t, 1>();
77
+ for (int64_t b = 0; b < B; ++b) {
78
+ double e = 0;
79
+ for (int64_t n = 0; n < N; ++n) e += e_rows[b * N + n];
80
+ EA[b] = e;
81
+ }
82
+ }
83
+
84
+ // Gradient contributions of one (QM, MM) pair; see the CUDA version.
85
+ inline void pair_grad(double d0, double d1, double d2, double qm, double sn, double qcn, double qvn, double ge,
86
+ const double* gf, const double* gf0, double* dd, double& dq, double& ds, double& dqc,
87
+ double& dqv) {
88
+ const double r = std::sqrt(d0 * d0 + d1 * d1 + d2 * d2);
89
+ if (r == 0.0) { dd[0] = dd[1] = dd[2] = dq = ds = dqc = dqv = 0.0; return; }
90
+ const double R = 1.0 / (r + EPS), R3 = R * R * R;
91
+ const double xs = r / sn, ex = std::exp(-xs);
92
+ const double T0 = (1.0 - (1.0 + r / (sn * 2.0)) * ex) / r;
93
+ const double dT0_dr = ex * (1.0 + xs) / (2.0 * sn * r) - T0 / r;
94
+ const double dT0_ds = -ex * (1.0 + xs) / (2.0 * sn * sn);
95
+ dqc = ge * qm * R;
96
+ dqv = ge * qm * T0;
97
+ ds = ge * qm * qvn * dT0_ds;
98
+ double dL_dr = ge * qm * (-qcn * R * R + qvn * dT0_dr);
99
+ dq = ge * (qcn * R + qvn * T0);
100
+ const double x = (1.0 / R) / (2.0 * sn), e2 = std::exp(-x);
101
+ const double f1 = 1.0 - (1.0 + x * (1.0 + 0.5 * x)) * e2;
102
+ const double df1_dx = 0.5 * x * x * e2;
103
+ const double d[3] = {d0, d1, d2};
104
+ double G[3], gfT1 = 0, gT1 = 0, dG = 0;
105
+ for (int k = 0; k < 3; ++k) {
106
+ const double T1 = -d[k] * R3;
107
+ G[k] = qm * (f1 * gf[k] + gf0[k]);
108
+ gfT1 += gf[k] * T1;
109
+ gT1 += gf0[k] * T1;
110
+ dG += d[k] * G[k];
111
+ }
112
+ const double dL_df1 = qm * gfT1;
113
+ dq += f1 * gfT1 + gT1;
114
+ dL_dr += dL_df1 * df1_dx / (2.0 * sn);
115
+ ds += dL_df1 * df1_dx * (-x / sn);
116
+ const double c = dL_dr / r + 3.0 * R3 * R * dG / r;
117
+ for (int k = 0; k < 3; ++k) dd[k] = c * d[k] - R3 * G[k];
118
+ }
119
+
120
+ template <typename scalar_t>
121
+ void electrostatics_backward_cpu_impl(const Tensor& xq, const Tensor& xm, const Tensor& q, const Tensor& s,
122
+ const Tensor& qc, const Tensor& qv, const Tensor& mask, const Tensor& gE,
123
+ const Tensor& gF, const Tensor& gF0, Tensor& g_xq, Tensor& g_xm, Tensor& g_q,
124
+ Tensor& g_s, Tensor& g_qc, Tensor& g_qv) {
125
+ const int64_t B = xq.size(0), N = xq.size(1), M = xm.size(1);
126
+ auto XQ = xq.accessor<scalar_t, 3>(); auto XM = xm.accessor<scalar_t, 3>();
127
+ auto Q = q.accessor<scalar_t, 2>(); auto S = s.accessor<scalar_t, 2>();
128
+ auto QC = qc.accessor<scalar_t, 2>(); auto QV = qv.accessor<scalar_t, 2>();
129
+ auto MK = mask.accessor<bool, 2>();
130
+ auto GE = gE.accessor<scalar_t, 1>(); auto GF = gF.accessor<scalar_t, 3>(); auto GF0 = gF0.accessor<scalar_t, 3>();
131
+ auto GXQ = g_xq.accessor<scalar_t, 3>(); auto GS = g_s.accessor<scalar_t, 2>();
132
+ auto GQC = g_qc.accessor<scalar_t, 2>(); auto GQV = g_qv.accessor<scalar_t, 2>();
133
+ auto GXM = g_xm.accessor<scalar_t, 3>(); auto GQ = g_q.accessor<scalar_t, 2>();
134
+ // Per-QM-atom gradients: reduce over MM atoms.
135
+ at::parallel_for(0, B * N, 1, [&](int64_t begin, int64_t end) {
136
+ for (int64_t bn = begin; bn < end; ++bn) {
137
+ const int64_t b = bn / N, n = bn % N;
138
+ double gx[3] = {0, 0, 0}, gs = 0, gqc = 0, gqv = 0;
139
+ if (MK[b][n]) {
140
+ const double gf[3] = {GF[b][n][0], GF[b][n][1], GF[b][n][2]};
141
+ const double gf0[3] = {GF0[b][n][0], GF0[b][n][1], GF0[b][n][2]};
142
+ for (int64_t m = 0; m < M; ++m) {
143
+ double dd[3], dq, ds, dqc, dqv;
144
+ pair_grad(XM[b][m][0] - XQ[b][n][0], XM[b][m][1] - XQ[b][n][1], XM[b][m][2] - XQ[b][n][2], Q[b][m],
145
+ S[b][n], QC[b][n], QV[b][n], GE[b], gf, gf0, dd, dq, ds, dqc, dqv);
146
+ for (int k = 0; k < 3; ++k) gx[k] -= dd[k];
147
+ gs += ds; gqc += dqc; gqv += dqv;
148
+ }
149
+ }
150
+ for (int k = 0; k < 3; ++k) GXQ[b][n][k] = gx[k];
151
+ GS[b][n] = gs; GQC[b][n] = gqc; GQV[b][n] = gqv;
152
+ }
153
+ });
154
+ // Per-MM-atom gradients: reduce over QM atoms.
155
+ at::parallel_for(0, B * M, 64, [&](int64_t begin, int64_t end) {
156
+ for (int64_t bm = begin; bm < end; ++bm) {
157
+ const int64_t b = bm / M, m = bm % M;
158
+ double a[4] = {0, 0, 0, 0};
159
+ for (int64_t n = 0; n < N; ++n) {
160
+ if (!MK[b][n]) continue;
161
+ const double gf[3] = {GF[b][n][0], GF[b][n][1], GF[b][n][2]};
162
+ const double gf0[3] = {GF0[b][n][0], GF0[b][n][1], GF0[b][n][2]};
163
+ double dd[3], dq, ds, dqc, dqv;
164
+ pair_grad(XM[b][m][0] - XQ[b][n][0], XM[b][m][1] - XQ[b][n][1], XM[b][m][2] - XQ[b][n][2], Q[b][m],
165
+ S[b][n], QC[b][n], QV[b][n], GE[b], gf, gf0, dd, dq, ds, dqc, dqv);
166
+ for (int k = 0; k < 3; ++k) a[k] += dd[k];
167
+ a[3] += dq;
168
+ }
169
+ for (int k = 0; k < 3; ++k) GXM[b][m][k] = a[k];
170
+ GQ[b][m] = a[3];
171
+ }
172
+ });
173
+ }
174
+
175
+ std::tuple<Tensor, Tensor, Tensor> electrostatics_fwd_cpu(const Tensor& xq_, const Tensor& xm_, const Tensor& q_,
176
+ const Tensor& s_, const Tensor& qc_, const Tensor& qv_,
177
+ const Tensor& mask_) {
178
+ auto xq = xq_.contiguous(), xm = xm_.contiguous(), q = q_.contiguous(), s = s_.contiguous();
179
+ auto qc = qc_.contiguous(), qv = qv_.contiguous(), mask = mask_.contiguous();
180
+ auto E = torch::zeros({xq.size(0)}, xq.options());
181
+ auto fields = torch::zeros_like(xq), F0 = torch::zeros_like(xq);
182
+ AT_DISPATCH_FLOATING_TYPES(xq.scalar_type(), "electrostatics_fwd_cpu", [&] {
183
+ electrostatics_forward_cpu_impl<scalar_t>(xq, xm, q, s, qc, qv, mask, E, fields, F0);
184
+ });
185
+ return {E, fields, F0};
186
+ }
187
+
188
+ std::tuple<Tensor, Tensor, Tensor, Tensor, Tensor, Tensor> electrostatics_bwd_cpu(
189
+ const Tensor& xq_, const Tensor& xm_, const Tensor& q_, const Tensor& s_, const Tensor& qc_, const Tensor& qv_,
190
+ const Tensor& mask_, const Tensor& gE_, const Tensor& gF_, const Tensor& gF0_) {
191
+ auto xq = xq_.contiguous(), xm = xm_.contiguous(), q = q_.contiguous(), s = s_.contiguous();
192
+ auto qc = qc_.contiguous(), qv = qv_.contiguous(), mask = mask_.contiguous();
193
+ auto gE = gE_.contiguous(), gF = gF_.contiguous(), gF0 = gF0_.contiguous();
194
+ auto g_xq = torch::zeros_like(xq), g_xm = torch::zeros_like(xm), g_q = torch::zeros_like(q);
195
+ auto g_s = torch::zeros_like(s), g_qc = torch::zeros_like(qc), g_qv = torch::zeros_like(qv);
196
+ AT_DISPATCH_FLOATING_TYPES(xq.scalar_type(), "electrostatics_bwd_cpu", [&] {
197
+ electrostatics_backward_cpu_impl<scalar_t>(xq, xm, q, s, qc, qv, mask, gE, gF, gF0, g_xq, g_xm, g_q, g_s,
198
+ g_qc, g_qv);
199
+ });
200
+ return {g_xq, g_xm, g_q, g_s, g_qc, g_qv};
201
+ }
202
+
203
+ // Autograd wrappers: the forward and backward kernels are separate operators
204
+ // (dispatched to the CPU or CUDA implementation).
205
+ class ElectrostaticsFunction : public torch::autograd::Function<ElectrostaticsFunction> {
206
+ public:
207
+ static tensor_list forward(AutogradContext* ctx, Tensor xq, Tensor xm, Tensor q, Tensor s, Tensor qc, Tensor qv,
208
+ Tensor mask) {
209
+ static auto op = c10::Dispatcher::singleton()
210
+ .findSchemaOrThrow("emle::electrostatics_fwd", "")
211
+ .typed<std::tuple<Tensor, Tensor, Tensor>(const Tensor&, const Tensor&, const Tensor&,
212
+ const Tensor&, const Tensor&, const Tensor&,
213
+ const Tensor&)>();
214
+ at::AutoDispatchBelowADInplaceOrView guard;
215
+ auto out = op.call(xq, xm, q, s, qc, qv, mask);
216
+ ctx->save_for_backward({xq, xm, q, s, qc, qv, mask});
217
+ return {std::get<0>(out), std::get<1>(out), std::get<2>(out)};
218
+ }
219
+
220
+ static tensor_list backward(AutogradContext* ctx, tensor_list g) {
221
+ TORCH_CHECK(!torch::GradMode::is_enabled(), "The fused EMLE operators do not support double backward");
222
+ static auto op = c10::Dispatcher::singleton()
223
+ .findSchemaOrThrow("emle::electrostatics_bwd", "")
224
+ .typed<std::tuple<Tensor, Tensor, Tensor, Tensor, Tensor, Tensor>(
225
+ const Tensor&, const Tensor&, const Tensor&, const Tensor&, const Tensor&,
226
+ const Tensor&, const Tensor&, const Tensor&, const Tensor&, const Tensor&)>();
227
+ auto v = ctx->get_saved_variables();
228
+ auto gE = g[0].defined() ? g[0] : torch::zeros({v[0].size(0)}, v[0].options());
229
+ auto gF = g[1].defined() ? g[1] : torch::zeros_like(v[0]);
230
+ auto gF0 = g[2].defined() ? g[2] : torch::zeros_like(v[0]);
231
+ auto r = op.call(v[0], v[1], v[2], v[3], v[4], v[5], v[6], gE, gF, gF0);
232
+ return {std::get<0>(r), std::get<1>(r), std::get<2>(r), std::get<3>(r), std::get<4>(r), std::get<5>(r),
233
+ Tensor()};
234
+ }
235
+ };
236
+
237
+ std::tuple<Tensor, Tensor, Tensor> electrostatics_autograd(const Tensor& xq, const Tensor& xm, const Tensor& q,
238
+ const Tensor& s, const Tensor& qc, const Tensor& qv,
239
+ const Tensor& mask) {
240
+ auto r = ElectrostaticsFunction::apply(xq, xm, q, s, qc, qv, mask);
241
+ return {r[0], r[1], r[2]};
242
+ }
243
+
244
+ std::tuple<Tensor, Tensor, Tensor> electrostatics_no_autograd(const Tensor& xq, const Tensor& xm, const Tensor& q,
245
+ const Tensor& s, const Tensor& qc, const Tensor& qv,
246
+ const Tensor& mask) {
247
+ static auto op = c10::Dispatcher::singleton()
248
+ .findSchemaOrThrow("emle::electrostatics_fwd", "")
249
+ .typed<std::tuple<Tensor, Tensor, Tensor>(const Tensor&, const Tensor&, const Tensor&,
250
+ const Tensor&, const Tensor&, const Tensor&,
251
+ const Tensor&)>();
252
+ return op.call(xq, xm, q, s, qc, qv, mask);
253
+ }
254
+
255
+ class AEVFunction : public torch::autograd::Function<AEVFunction> {
256
+ public:
257
+ static tensor_list forward(AutogradContext* ctx, Tensor species, Tensor xyz, std::vector<double> shf_r,
258
+ std::vector<double> shf_a, std::vector<double> shf_z, double eta_r, double rc_r,
259
+ double eta_a, double zeta, double rc_a) {
260
+ static auto op = c10::Dispatcher::singleton()
261
+ .findSchemaOrThrow("emle::aev_fwd", "")
262
+ .typed<Tensor(const Tensor&, const Tensor&, c10::ArrayRef<double>, c10::ArrayRef<double>,
263
+ c10::ArrayRef<double>, double, double, double, double, double)>();
264
+ at::AutoDispatchBelowADInplaceOrView guard;
265
+ auto out = op.call(species, xyz, shf_r, shf_a, shf_z, eta_r, rc_r, eta_a, zeta, rc_a);
266
+ ctx->save_for_backward({species, xyz});
267
+ ctx->saved_data["shf_r"] = shf_r; ctx->saved_data["shf_a"] = shf_a; ctx->saved_data["shf_z"] = shf_z;
268
+ ctx->saved_data["eta_r"] = eta_r; ctx->saved_data["rc_r"] = rc_r; ctx->saved_data["eta_a"] = eta_a;
269
+ ctx->saved_data["zeta"] = zeta; ctx->saved_data["rc_a"] = rc_a;
270
+ return {out};
271
+ }
272
+
273
+ static tensor_list backward(AutogradContext* ctx, tensor_list g) {
274
+ TORCH_CHECK(!torch::GradMode::is_enabled(), "The fused EMLE operators do not support double backward");
275
+ static auto op = c10::Dispatcher::singleton()
276
+ .findSchemaOrThrow("emle::aev_bwd", "")
277
+ .typed<Tensor(const Tensor&, const Tensor&, const Tensor&, c10::ArrayRef<double>,
278
+ c10::ArrayRef<double>, c10::ArrayRef<double>, double, double, double, double,
279
+ double)>();
280
+ auto v = ctx->get_saved_variables();
281
+ auto& d = ctx->saved_data;
282
+ auto shf_r = d["shf_r"].toDoubleVector(), shf_a = d["shf_a"].toDoubleVector(), shf_z = d["shf_z"].toDoubleVector();
283
+ auto gx = op.call(v[0], v[1], g[0], shf_r, shf_a, shf_z, d["eta_r"].toDouble(), d["rc_r"].toDouble(),
284
+ d["eta_a"].toDouble(), d["zeta"].toDouble(), d["rc_a"].toDouble());
285
+ return {Tensor(), gx, Tensor(), Tensor(), Tensor(), Tensor(), Tensor(), Tensor(), Tensor(), Tensor()};
286
+ }
287
+ };
288
+
289
+ Tensor aev_autograd(const Tensor& species, const Tensor& xyz, c10::ArrayRef<double> shf_r,
290
+ c10::ArrayRef<double> shf_a, c10::ArrayRef<double> shf_z, double eta_r, double rc_r,
291
+ double eta_a, double zeta, double rc_a) {
292
+ return AEVFunction::apply(species, xyz, shf_r.vec(), shf_a.vec(), shf_z.vec(), eta_r, rc_r, eta_a, zeta,
293
+ rc_a)[0];
294
+ }
295
+
296
+ Tensor aev_no_autograd(const Tensor& species, const Tensor& xyz, c10::ArrayRef<double> shf_r,
297
+ c10::ArrayRef<double> shf_a, c10::ArrayRef<double> shf_z, double eta_r, double rc_r,
298
+ double eta_a, double zeta, double rc_a) {
299
+ static auto op = c10::Dispatcher::singleton()
300
+ .findSchemaOrThrow("emle::aev_fwd", "")
301
+ .typed<Tensor(const Tensor&, const Tensor&, c10::ArrayRef<double>, c10::ArrayRef<double>,
302
+ c10::ArrayRef<double>, double, double, double, double, double)>();
303
+ return op.call(species, xyz, shf_r, shf_a, shf_z, eta_r, rc_r, eta_a, zeta, rc_a);
304
+ }
305
+
306
+ } // namespace
307
+
308
+ TORCH_LIBRARY(emle, m) {
309
+ m.def("electrostatics_fwd(Tensor xq, Tensor xm, Tensor q, Tensor s, Tensor q_core, Tensor q_val, Tensor mask)"
310
+ " -> (Tensor, Tensor, Tensor)");
311
+ m.def("electrostatics_bwd(Tensor xq, Tensor xm, Tensor q, Tensor s, Tensor q_core, Tensor q_val, Tensor mask,"
312
+ " Tensor grad_E, Tensor grad_fields, Tensor grad_F0) -> (Tensor, Tensor, Tensor, Tensor, Tensor, Tensor)");
313
+ m.def("electrostatics(Tensor xq, Tensor xm, Tensor q, Tensor s, Tensor q_core, Tensor q_val, Tensor mask)"
314
+ " -> (Tensor, Tensor, Tensor)");
315
+ m.def("aev_fwd(Tensor species, Tensor xyz, float[] shf_r, float[] shf_a, float[] shf_z, float eta_r,"
316
+ " float rc_r, float eta_a, float zeta, float rc_a) -> Tensor");
317
+ m.def("aev_bwd(Tensor species, Tensor xyz, Tensor grad, float[] shf_r, float[] shf_a, float[] shf_z,"
318
+ " float eta_r, float rc_r, float eta_a, float zeta, float rc_a) -> Tensor");
319
+ m.def("aev(Tensor species, Tensor xyz, float[] shf_r, float[] shf_a, float[] shf_z, float eta_r, float rc_r,"
320
+ " float eta_a, float zeta, float rc_a) -> Tensor");
321
+ }
322
+
323
+ TORCH_LIBRARY_IMPL(emle, CPU, m) {
324
+ m.impl("electrostatics_fwd", &electrostatics_fwd_cpu);
325
+ m.impl("electrostatics_bwd", &electrostatics_bwd_cpu);
326
+ m.impl("electrostatics", &electrostatics_no_autograd);
327
+ }
328
+
329
+ TORCH_LIBRARY_IMPL(emle, Autograd, m) {
330
+ m.impl("electrostatics", &electrostatics_autograd);
331
+ m.impl("aev", &aev_autograd);
332
+ }
333
+
334
+
335
+ // The CUDA dispatch of the composite ops (when autograd is not involved).
336
+ TORCH_LIBRARY_IMPL(emle, CUDA, m) {
337
+ m.impl("electrostatics", &electrostatics_no_autograd);
338
+ m.impl("aev", &aev_no_autograd);
339
+ }
340
+
341
+ // Python module, so that an ahead-of-time build (setup.py) can be imported as
342
+ // emle._C, which registers the operators above.
343
+ PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
344
+ m.doc() = "Fused EMLE operators (registered as torch.ops.emle.*).";
345
+ #ifdef EMLE_WITH_CUDA
346
+ m.attr("with_cuda") = true;
347
+ #else
348
+ m.attr("with_cuda") = false;
349
+ #endif
350
+ }