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
emle/_csrc/ops_cuda.cu
ADDED
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2023-2026 The EMLE-Engine developers
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
// Fused operators for EMLE: CUDA kernels (see ops.cpp for the definitions).
|
|
5
|
+
#include <torch/extension.h>
|
|
6
|
+
#include <torch/library.h>
|
|
7
|
+
#include <c10/cuda/CUDAStream.h>
|
|
8
|
+
#include <c10/cuda/CUDAException.h>
|
|
9
|
+
#include <c10/cuda/CUDAGuard.h>
|
|
10
|
+
#include <tuple>
|
|
11
|
+
#include <vector>
|
|
12
|
+
|
|
13
|
+
namespace {
|
|
14
|
+
|
|
15
|
+
// ---- QM/MM electrostatics ----
|
|
16
|
+
|
|
17
|
+
#define EPS 1e-10
|
|
18
|
+
|
|
19
|
+
template <typename T> __device__ __forceinline__ T dexp(T x);
|
|
20
|
+
template <> __device__ __forceinline__ float dexp<float>(float x) { return expf(x); }
|
|
21
|
+
template <> __device__ __forceinline__ double dexp<double>(double x) { return exp(x); }
|
|
22
|
+
|
|
23
|
+
template <typename T, int NV>
|
|
24
|
+
__device__ void block_reduce(T (&v)[NV], T* sh) {
|
|
25
|
+
// sh: blockDim.x * NV
|
|
26
|
+
const int t = threadIdx.x;
|
|
27
|
+
for (int k = 0; k < NV; ++k) sh[k * blockDim.x + t] = v[k];
|
|
28
|
+
__syncthreads();
|
|
29
|
+
for (int s = blockDim.x / 2; s > 0; s >>= 1) {
|
|
30
|
+
if (t < s)
|
|
31
|
+
for (int k = 0; k < NV; ++k) sh[k * blockDim.x + t] += sh[k * blockDim.x + t + s];
|
|
32
|
+
__syncthreads();
|
|
33
|
+
}
|
|
34
|
+
for (int k = 0; k < NV; ++k) v[k] = sh[k * blockDim.x];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// One block per (b, n).
|
|
38
|
+
template <typename T>
|
|
39
|
+
__global__ void fwd_kernel(const T* __restrict__ xq, const T* __restrict__ xm, const T* __restrict__ q,
|
|
40
|
+
const T* __restrict__ s, const T* __restrict__ qc, const T* __restrict__ qv,
|
|
41
|
+
const bool* __restrict__ mask, int N, int M,
|
|
42
|
+
T* __restrict__ e_rows, T* __restrict__ fields, T* __restrict__ F0) {
|
|
43
|
+
extern __shared__ unsigned char smem[];
|
|
44
|
+
T* sh = reinterpret_cast<T*>(smem);
|
|
45
|
+
const int bn = blockIdx.x, b = bn / N;
|
|
46
|
+
T v[7] = {0, 0, 0, 0, 0, 0, 0}; // es, f0..2, f00..02
|
|
47
|
+
if (mask[bn]) {
|
|
48
|
+
const T sn = s[bn], qcn = qc[bn], qvn = qv[bn];
|
|
49
|
+
const T x0 = xq[bn * 3], x1 = xq[bn * 3 + 1], x2 = xq[bn * 3 + 2];
|
|
50
|
+
for (int m = threadIdx.x; m < M; m += blockDim.x) {
|
|
51
|
+
const int bm = b * M + m;
|
|
52
|
+
const T d0 = xm[bm * 3] - x0, d1 = xm[bm * 3 + 1] - x1, d2 = xm[bm * 3 + 2] - x2;
|
|
53
|
+
const T r = sqrt(d0 * d0 + d1 * d1 + d2 * d2);
|
|
54
|
+
const T qm = q[bm];
|
|
55
|
+
const T R = T(1) / (r + T(EPS));
|
|
56
|
+
const T T0 = (T(1) - (T(1) + r / (sn * T(2))) * dexp<T>(-r / sn)) / r;
|
|
57
|
+
v[0] += qm * (qcn * R + qvn * T0);
|
|
58
|
+
const T R3 = R * R * R;
|
|
59
|
+
const T x = (T(1) / R) / (T(2) * sn);
|
|
60
|
+
const T f1 = T(1) - (T(1) + x * (T(1) + T(0.5) * x)) * dexp<T>(-x);
|
|
61
|
+
const T w0 = -qm * R3, w1 = w0 * f1;
|
|
62
|
+
v[1] += w1 * d0; v[2] += w1 * d1; v[3] += w1 * d2;
|
|
63
|
+
v[4] += w0 * d0; v[5] += w0 * d1; v[6] += w0 * d2;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
block_reduce<T, 7>(v, sh);
|
|
67
|
+
if (threadIdx.x == 0) {
|
|
68
|
+
e_rows[bn] = v[0];
|
|
69
|
+
for (int k = 0; k < 3; ++k) { fields[bn * 3 + k] = v[1 + k]; F0[bn * 3 + k] = v[4 + k]; }
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Shared per-pair backward terms. Returns dL/dd (3), dL/dq_m, and per-n terms.
|
|
74
|
+
template <typename T>
|
|
75
|
+
__device__ __forceinline__ void pair_grad(T d0, T d1, T d2, T qm, T sn, T qcn, T qvn, T ge,
|
|
76
|
+
const T* gf, const T* gf0, T* dd, T& dq, T& ds, T& dqc, T& dqv) {
|
|
77
|
+
const T r = sqrt(d0 * d0 + d1 * d1 + d2 * d2);
|
|
78
|
+
if (r == T(0)) { dd[0] = dd[1] = dd[2] = dq = ds = dqc = dqv = T(0); return; }
|
|
79
|
+
const T R = T(1) / (r + T(EPS)), R3 = R * R * R;
|
|
80
|
+
const T xs = r / sn, ex = dexp<T>(-xs);
|
|
81
|
+
const T T0 = (T(1) - (T(1) + r / (sn * T(2))) * ex) / r;
|
|
82
|
+
const T dT0_dr = ex * (T(1) + xs) / (T(2) * sn * r) - T0 / r;
|
|
83
|
+
const T dT0_ds = -ex * (T(1) + xs) / (T(2) * sn * sn);
|
|
84
|
+
dqc = ge * qm * R;
|
|
85
|
+
dqv = ge * qm * T0;
|
|
86
|
+
ds = ge * qm * qvn * dT0_ds;
|
|
87
|
+
T dL_dr = ge * qm * (-qcn * R * R + qvn * dT0_dr);
|
|
88
|
+
dq = ge * (qcn * R + qvn * T0);
|
|
89
|
+
const T x = (T(1) / R) / (T(2) * sn), e2 = dexp<T>(-x);
|
|
90
|
+
const T f1 = T(1) - (T(1) + x * (T(1) + T(0.5) * x)) * e2;
|
|
91
|
+
const T df1_dx = T(0.5) * x * x * e2;
|
|
92
|
+
const T d[3] = {d0, d1, d2};
|
|
93
|
+
T G[3], gfT1 = 0, gT1 = 0, dG = 0;
|
|
94
|
+
for (int k = 0; k < 3; ++k) {
|
|
95
|
+
const T T1 = -d[k] * R3;
|
|
96
|
+
G[k] = qm * (f1 * gf[k] + gf0[k]);
|
|
97
|
+
gfT1 += gf[k] * T1;
|
|
98
|
+
gT1 += gf0[k] * T1;
|
|
99
|
+
dG += d[k] * G[k];
|
|
100
|
+
}
|
|
101
|
+
const T dL_df1 = qm * gfT1;
|
|
102
|
+
dq += f1 * gfT1 + gT1;
|
|
103
|
+
dL_dr += dL_df1 * df1_dx / (T(2) * sn);
|
|
104
|
+
ds += dL_df1 * df1_dx * (-x / sn);
|
|
105
|
+
const T c = dL_dr / r + T(3) * R3 * R * dG / r;
|
|
106
|
+
for (int k = 0; k < 3; ++k) dd[k] = c * d[k] - R3 * G[k];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Per-QM-atom gradients: one block per (b, n), reduce over m.
|
|
110
|
+
template <typename T>
|
|
111
|
+
__global__ void bwd_qm_kernel(const T* __restrict__ xq, const T* __restrict__ xm, const T* __restrict__ q,
|
|
112
|
+
const T* __restrict__ s, const T* __restrict__ qc, const T* __restrict__ qv,
|
|
113
|
+
const bool* __restrict__ mask, const T* __restrict__ gE, const T* __restrict__ gF,
|
|
114
|
+
const T* __restrict__ gF0, int N, int M, T* g_xq, T* g_s, T* g_qc, T* g_qv) {
|
|
115
|
+
extern __shared__ unsigned char smem[];
|
|
116
|
+
T* sh = reinterpret_cast<T*>(smem);
|
|
117
|
+
const int bn = blockIdx.x, b = bn / N;
|
|
118
|
+
T v[6] = {0, 0, 0, 0, 0, 0}; // gx0..2, gs, gqc, gqv
|
|
119
|
+
if (mask[bn]) {
|
|
120
|
+
const T sn = s[bn], qcn = qc[bn], qvn = qv[bn], ge = gE[b];
|
|
121
|
+
const T gf[3] = {gF[bn * 3], gF[bn * 3 + 1], gF[bn * 3 + 2]};
|
|
122
|
+
const T gf0[3] = {gF0[bn * 3], gF0[bn * 3 + 1], gF0[bn * 3 + 2]};
|
|
123
|
+
const T x0 = xq[bn * 3], x1 = xq[bn * 3 + 1], x2 = xq[bn * 3 + 2];
|
|
124
|
+
for (int m = threadIdx.x; m < M; m += blockDim.x) {
|
|
125
|
+
const int bm = b * M + m;
|
|
126
|
+
T dd[3], dq, ds, dqc, dqv;
|
|
127
|
+
pair_grad<T>(xm[bm * 3] - x0, xm[bm * 3 + 1] - x1, xm[bm * 3 + 2] - x2, q[bm], sn, qcn, qvn, ge, gf, gf0,
|
|
128
|
+
dd, dq, ds, dqc, dqv);
|
|
129
|
+
v[0] -= dd[0]; v[1] -= dd[1]; v[2] -= dd[2];
|
|
130
|
+
v[3] += ds; v[4] += dqc; v[5] += dqv;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
block_reduce<T, 6>(v, sh);
|
|
134
|
+
if (threadIdx.x == 0) {
|
|
135
|
+
for (int k = 0; k < 3; ++k) g_xq[bn * 3 + k] = v[k];
|
|
136
|
+
g_s[bn] = v[3]; g_qc[bn] = v[4]; g_qv[bn] = v[5];
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Per-MM-atom gradients: one thread per (b, m), loop over n.
|
|
141
|
+
template <typename T>
|
|
142
|
+
__global__ void bwd_mm_kernel(const T* __restrict__ xq, const T* __restrict__ xm, const T* __restrict__ q,
|
|
143
|
+
const T* __restrict__ s, const T* __restrict__ qc, const T* __restrict__ qv,
|
|
144
|
+
const bool* __restrict__ mask, const T* __restrict__ gE, const T* __restrict__ gF,
|
|
145
|
+
const T* __restrict__ gF0, int B, int N, int M, T* g_xm, T* g_q) {
|
|
146
|
+
const int bm = blockIdx.x * blockDim.x + threadIdx.x;
|
|
147
|
+
if (bm >= B * M) return;
|
|
148
|
+
const int b = bm / M;
|
|
149
|
+
const T y0 = xm[bm * 3], y1 = xm[bm * 3 + 1], y2 = xm[bm * 3 + 2], qm = q[bm], ge = gE[b];
|
|
150
|
+
T a[4] = {0, 0, 0, 0};
|
|
151
|
+
for (int n = 0; n < N; ++n) {
|
|
152
|
+
const int bn = b * N + n;
|
|
153
|
+
if (!mask[bn]) continue;
|
|
154
|
+
const T gf[3] = {gF[bn * 3], gF[bn * 3 + 1], gF[bn * 3 + 2]};
|
|
155
|
+
const T gf0[3] = {gF0[bn * 3], gF0[bn * 3 + 1], gF0[bn * 3 + 2]};
|
|
156
|
+
T dd[3], dq, ds, dqc, dqv;
|
|
157
|
+
pair_grad<T>(y0 - xq[bn * 3], y1 - xq[bn * 3 + 1], y2 - xq[bn * 3 + 2], qm, s[bn], qc[bn], qv[bn], ge, gf, gf0,
|
|
158
|
+
dd, dq, ds, dqc, dqv);
|
|
159
|
+
a[0] += dd[0]; a[1] += dd[1]; a[2] += dd[2]; a[3] += dq;
|
|
160
|
+
}
|
|
161
|
+
for (int k = 0; k < 3; ++k) g_xm[bm * 3 + k] = a[k];
|
|
162
|
+
g_q[bm] = a[3];
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
static const int THREADS = 256;
|
|
166
|
+
|
|
167
|
+
std::tuple<torch::Tensor, torch::Tensor, torch::Tensor> electrostatics_fwd_cuda(const torch::Tensor& xq_in, const torch::Tensor& xm_in, const torch::Tensor& q_in, const torch::Tensor& s_in,
|
|
168
|
+
const torch::Tensor& qc_in, const torch::Tensor& qv_in, const torch::Tensor& mask_in) {
|
|
169
|
+
torch::Tensor xq = xq_in;
|
|
170
|
+
torch::Tensor xm = xm_in;
|
|
171
|
+
torch::Tensor q = q_in;
|
|
172
|
+
torch::Tensor s = s_in;
|
|
173
|
+
torch::Tensor qc = qc_in;
|
|
174
|
+
torch::Tensor qv = qv_in;
|
|
175
|
+
torch::Tensor mask = mask_in;
|
|
176
|
+
const c10::cuda::CUDAGuard guard(xq.device());
|
|
177
|
+
xq = xq.contiguous(); xm = xm.contiguous(); q = q.contiguous(); s = s.contiguous();
|
|
178
|
+
qc = qc.contiguous(); qv = qv.contiguous(); mask = mask.contiguous();
|
|
179
|
+
const int B = xq.size(0), N = xq.size(1), M = xm.size(1);
|
|
180
|
+
auto e_rows = torch::empty({B, N}, xq.options());
|
|
181
|
+
auto fields = torch::empty_like(xq), F0 = torch::empty_like(xq);
|
|
182
|
+
auto stream = c10::cuda::getCurrentCUDAStream();
|
|
183
|
+
AT_DISPATCH_FLOATING_TYPES(xq.scalar_type(), "fused_forward_cuda", [&] {
|
|
184
|
+
fwd_kernel<scalar_t><<<B * N, THREADS, THREADS * 7 * sizeof(scalar_t), stream>>>(
|
|
185
|
+
xq.data_ptr<scalar_t>(), xm.data_ptr<scalar_t>(), q.data_ptr<scalar_t>(), s.data_ptr<scalar_t>(),
|
|
186
|
+
qc.data_ptr<scalar_t>(), qv.data_ptr<scalar_t>(), mask.data_ptr<bool>(), N, M,
|
|
187
|
+
e_rows.data_ptr<scalar_t>(), fields.data_ptr<scalar_t>(), F0.data_ptr<scalar_t>());
|
|
188
|
+
C10_CUDA_KERNEL_LAUNCH_CHECK();
|
|
189
|
+
});
|
|
190
|
+
return {e_rows.sum(1), fields, F0};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
std::tuple<torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor> electrostatics_bwd_cuda(const torch::Tensor& xq_in, const torch::Tensor& xm_in, const torch::Tensor& q_in, const torch::Tensor& s_in,
|
|
194
|
+
const torch::Tensor& qc_in, const torch::Tensor& qv_in, const torch::Tensor& mask_in,
|
|
195
|
+
const torch::Tensor& gE_in, const torch::Tensor& gF_in, const torch::Tensor& gF0_in) {
|
|
196
|
+
torch::Tensor xq = xq_in;
|
|
197
|
+
torch::Tensor xm = xm_in;
|
|
198
|
+
torch::Tensor q = q_in;
|
|
199
|
+
torch::Tensor s = s_in;
|
|
200
|
+
torch::Tensor qc = qc_in;
|
|
201
|
+
torch::Tensor qv = qv_in;
|
|
202
|
+
torch::Tensor mask = mask_in;
|
|
203
|
+
torch::Tensor gE = gE_in;
|
|
204
|
+
torch::Tensor gF = gF_in;
|
|
205
|
+
torch::Tensor gF0 = gF0_in;
|
|
206
|
+
const c10::cuda::CUDAGuard guard(xq.device());
|
|
207
|
+
xq = xq.contiguous(); xm = xm.contiguous(); q = q.contiguous(); s = s.contiguous();
|
|
208
|
+
qc = qc.contiguous(); qv = qv.contiguous(); mask = mask.contiguous();
|
|
209
|
+
gE = gE.contiguous(); gF = gF.contiguous(); gF0 = gF0.contiguous();
|
|
210
|
+
const int B = xq.size(0), N = xq.size(1), M = xm.size(1);
|
|
211
|
+
auto g_xq = torch::empty_like(xq), g_xm = torch::empty_like(xm), g_q = torch::empty_like(q);
|
|
212
|
+
auto g_s = torch::empty_like(s), g_qc = torch::empty_like(qc), g_qv = torch::empty_like(qv);
|
|
213
|
+
auto stream = c10::cuda::getCurrentCUDAStream();
|
|
214
|
+
AT_DISPATCH_FLOATING_TYPES(xq.scalar_type(), "fused_backward_cuda", [&] {
|
|
215
|
+
bwd_qm_kernel<scalar_t><<<B * N, THREADS, THREADS * 6 * sizeof(scalar_t), stream>>>(
|
|
216
|
+
xq.data_ptr<scalar_t>(), xm.data_ptr<scalar_t>(), q.data_ptr<scalar_t>(), s.data_ptr<scalar_t>(),
|
|
217
|
+
qc.data_ptr<scalar_t>(), qv.data_ptr<scalar_t>(), mask.data_ptr<bool>(), gE.data_ptr<scalar_t>(),
|
|
218
|
+
gF.data_ptr<scalar_t>(), gF0.data_ptr<scalar_t>(), N, M, g_xq.data_ptr<scalar_t>(),
|
|
219
|
+
g_s.data_ptr<scalar_t>(), g_qc.data_ptr<scalar_t>(), g_qv.data_ptr<scalar_t>());
|
|
220
|
+
C10_CUDA_KERNEL_LAUNCH_CHECK();
|
|
221
|
+
const int blocks = (B * M + THREADS - 1) / THREADS;
|
|
222
|
+
bwd_mm_kernel<scalar_t><<<blocks, THREADS, 0, stream>>>(
|
|
223
|
+
xq.data_ptr<scalar_t>(), xm.data_ptr<scalar_t>(), q.data_ptr<scalar_t>(), s.data_ptr<scalar_t>(),
|
|
224
|
+
qc.data_ptr<scalar_t>(), qv.data_ptr<scalar_t>(), mask.data_ptr<bool>(), gE.data_ptr<scalar_t>(),
|
|
225
|
+
gF.data_ptr<scalar_t>(), gF0.data_ptr<scalar_t>(), B, N, M, g_xm.data_ptr<scalar_t>(), g_q.data_ptr<scalar_t>());
|
|
226
|
+
C10_CUDA_KERNEL_LAUNCH_CHECK();
|
|
227
|
+
});
|
|
228
|
+
return {g_xq, g_xm, g_q, g_s, g_qc, g_qv};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
// ---- AEVs ----
|
|
233
|
+
|
|
234
|
+
#define AEV_PI 3.14159265358979323846
|
|
235
|
+
#define NS 7 // number of species
|
|
236
|
+
#define NR 16 // radial shifts
|
|
237
|
+
#define NA 8 // angular radial shifts
|
|
238
|
+
#define NZ 4 // angle sections
|
|
239
|
+
#define RADIAL_LEN (NS * NR) // 112
|
|
240
|
+
#define NPAIRS (NS * (NS + 1) / 2) // 28
|
|
241
|
+
#define AEV_LEN (RADIAL_LEN + NPAIRS * NA * NZ) // 1008
|
|
242
|
+
|
|
243
|
+
struct Params {
|
|
244
|
+
double eta_r, rc_r, eta_a, zeta, rc_a;
|
|
245
|
+
double shf_r[NR], shf_a[NA], shf_z[NZ];
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
__device__ __forceinline__ int pair_index(int a, int c) {
|
|
249
|
+
// Row-major upper triangle (incl. diagonal) of NS x NS, as torch.triu_indices.
|
|
250
|
+
if (a > c) { int t = a; a = c; c = t; }
|
|
251
|
+
return a * NS - a * (a - 1) / 2 + (c - a);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
template <typename T>
|
|
255
|
+
__device__ __forceinline__ T fc(T r, T rc) { return T(0.5) * cos(T(AEV_PI) * r / rc) + T(0.5); }
|
|
256
|
+
template <typename T>
|
|
257
|
+
__device__ __forceinline__ T dfc(T r, T rc) { return -T(0.5) * T(AEV_PI) / rc * sin(T(AEV_PI) * r / rc); }
|
|
258
|
+
|
|
259
|
+
template <typename T>
|
|
260
|
+
__global__ void aev_fwd(const int* __restrict__ species, const T* __restrict__ xyz, int N, Params p, T* __restrict__ out) {
|
|
261
|
+
extern __shared__ unsigned char smem[];
|
|
262
|
+
T* acc = reinterpret_cast<T*>(smem);
|
|
263
|
+
const int bi = blockIdx.x, b = bi / N, i = bi % N;
|
|
264
|
+
for (int f = threadIdx.x; f < AEV_LEN; f += blockDim.x) acc[f] = 0;
|
|
265
|
+
__syncthreads();
|
|
266
|
+
if (species[bi] >= 0) {
|
|
267
|
+
const T xi = xyz[bi * 3], yi = xyz[bi * 3 + 1], zi = xyz[bi * 3 + 2];
|
|
268
|
+
const int* sp = species + b * N;
|
|
269
|
+
const T* X = xyz + b * N * 3;
|
|
270
|
+
// Radial.
|
|
271
|
+
for (int j = threadIdx.x; j < N; j += blockDim.x) {
|
|
272
|
+
if (j == i || sp[j] < 0) continue;
|
|
273
|
+
const T dx = X[j * 3] - xi, dy = X[j * 3 + 1] - yi, dz = X[j * 3 + 2] - zi;
|
|
274
|
+
const T r = sqrt(dx * dx + dy * dy + dz * dz);
|
|
275
|
+
if (r > T(p.rc_r)) continue;
|
|
276
|
+
const T f = fc<T>(r, T(p.rc_r));
|
|
277
|
+
for (int k = 0; k < NR; ++k) {
|
|
278
|
+
const T u = r - T(p.shf_r[k]);
|
|
279
|
+
atomicAdd(&acc[sp[j] * NR + k], T(0.25) * exp(-T(p.eta_r) * u * u) * f);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
// Angular: unordered pairs j < k.
|
|
283
|
+
const int npairs = N * (N - 1) / 2;
|
|
284
|
+
for (int t = threadIdx.x; t < npairs; t += blockDim.x) {
|
|
285
|
+
// Map t -> (j, k), j < k.
|
|
286
|
+
int j = int((2 * N - 1 - sqrt(T((2 * N - 1) * (2 * N - 1) - 8 * t))) / 2);
|
|
287
|
+
while (j > 0 && t < j * (2 * N - j - 1) / 2) --j;
|
|
288
|
+
while (t >= (j + 1) * (2 * N - j - 2) / 2) ++j;
|
|
289
|
+
const int k = t - j * (2 * N - j - 1) / 2 + j + 1;
|
|
290
|
+
if (j == i || k == i || sp[j] < 0 || sp[k] < 0) continue;
|
|
291
|
+
const T ax = X[j * 3] - xi, ay = X[j * 3 + 1] - yi, az = X[j * 3 + 2] - zi;
|
|
292
|
+
const T bx = X[k * 3] - xi, by = X[k * 3 + 1] - yi, bz = X[k * 3 + 2] - zi;
|
|
293
|
+
const T r1 = sqrt(ax * ax + ay * ay + az * az), r2 = sqrt(bx * bx + by * by + bz * bz);
|
|
294
|
+
if (r1 > T(p.rc_a) || r2 > T(p.rc_a)) continue;
|
|
295
|
+
const T c = (ax * bx + ay * by + az * bz) / max(r1 * r2, T(1e-10));
|
|
296
|
+
const T theta = acos(T(0.95) * c);
|
|
297
|
+
const T F = fc<T>(r1, T(p.rc_a)) * fc<T>(r2, T(p.rc_a));
|
|
298
|
+
const T mean = T(0.5) * (r1 + r2);
|
|
299
|
+
T Az[NZ];
|
|
300
|
+
for (int z = 0; z < NZ; ++z) Az[z] = T(2) * pow((T(1) + cos(theta - T(p.shf_z[z]))) / T(2), T(p.zeta));
|
|
301
|
+
const int base = RADIAL_LEN + pair_index(sp[j], sp[k]) * NA * NZ;
|
|
302
|
+
for (int a = 0; a < NA; ++a) {
|
|
303
|
+
const T u = mean - T(p.shf_a[a]);
|
|
304
|
+
const T Ra = exp(-T(p.eta_a) * u * u) * F;
|
|
305
|
+
for (int z = 0; z < NZ; ++z) atomicAdd(&acc[base + a * NZ + z], Ra * Az[z]);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
__syncthreads();
|
|
310
|
+
for (int f = threadIdx.x; f < AEV_LEN; f += blockDim.x) out[bi * AEV_LEN + f] = acc[f];
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
template <typename T>
|
|
314
|
+
__global__ void aev_bwd(const int* __restrict__ species, const T* __restrict__ xyz, const T* __restrict__ grad,
|
|
315
|
+
int N, Params p, T* __restrict__ gx) {
|
|
316
|
+
extern __shared__ unsigned char smem[];
|
|
317
|
+
T* g = reinterpret_cast<T*>(smem);
|
|
318
|
+
const int bi = blockIdx.x, b = bi / N, i = bi % N;
|
|
319
|
+
for (int f = threadIdx.x; f < AEV_LEN; f += blockDim.x) g[f] = grad[bi * AEV_LEN + f];
|
|
320
|
+
__syncthreads();
|
|
321
|
+
if (species[bi] < 0) return;
|
|
322
|
+
const T xi = xyz[bi * 3], yi = xyz[bi * 3 + 1], zi = xyz[bi * 3 + 2];
|
|
323
|
+
const int* sp = species + b * N;
|
|
324
|
+
const T* X = xyz + b * N * 3;
|
|
325
|
+
T* GX = gx + b * N * 3;
|
|
326
|
+
// Radial.
|
|
327
|
+
for (int j = threadIdx.x; j < N; j += blockDim.x) {
|
|
328
|
+
if (j == i || sp[j] < 0) continue;
|
|
329
|
+
const T d[3] = {X[j * 3] - xi, X[j * 3 + 1] - yi, X[j * 3 + 2] - zi};
|
|
330
|
+
const T r = sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]);
|
|
331
|
+
if (r > T(p.rc_r)) continue;
|
|
332
|
+
const T f = fc<T>(r, T(p.rc_r)), df = dfc<T>(r, T(p.rc_r));
|
|
333
|
+
T gr = 0;
|
|
334
|
+
for (int k = 0; k < NR; ++k) {
|
|
335
|
+
const T u = r - T(p.shf_r[k]), e = exp(-T(p.eta_r) * u * u);
|
|
336
|
+
gr += g[sp[j] * NR + k] * T(0.25) * e * (-T(2) * T(p.eta_r) * u * f + df);
|
|
337
|
+
}
|
|
338
|
+
for (int c = 0; c < 3; ++c) {
|
|
339
|
+
const T v = gr * d[c] / r;
|
|
340
|
+
atomicAdd(&GX[j * 3 + c], v);
|
|
341
|
+
atomicAdd(&GX[i * 3 + c], -v);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
// Angular.
|
|
345
|
+
const int npairs = N * (N - 1) / 2;
|
|
346
|
+
for (int t = threadIdx.x; t < npairs; t += blockDim.x) {
|
|
347
|
+
int j = int((2 * N - 1 - sqrt(T((2 * N - 1) * (2 * N - 1) - 8 * t))) / 2);
|
|
348
|
+
while (j > 0 && t < j * (2 * N - j - 1) / 2) --j;
|
|
349
|
+
while (t >= (j + 1) * (2 * N - j - 2) / 2) ++j;
|
|
350
|
+
const int k = t - j * (2 * N - j - 1) / 2 + j + 1;
|
|
351
|
+
if (j == i || k == i || sp[j] < 0 || sp[k] < 0) continue;
|
|
352
|
+
const T a[3] = {X[j * 3] - xi, X[j * 3 + 1] - yi, X[j * 3 + 2] - zi};
|
|
353
|
+
const T bv[3] = {X[k * 3] - xi, X[k * 3 + 1] - yi, X[k * 3 + 2] - zi};
|
|
354
|
+
const T r1 = sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
|
|
355
|
+
const T r2 = sqrt(bv[0] * bv[0] + bv[1] * bv[1] + bv[2] * bv[2]);
|
|
356
|
+
if (r1 > T(p.rc_a) || r2 > T(p.rc_a)) continue;
|
|
357
|
+
const T dot = a[0] * bv[0] + a[1] * bv[1] + a[2] * bv[2];
|
|
358
|
+
const T rr = max(r1 * r2, T(1e-10));
|
|
359
|
+
const T c = dot / rr;
|
|
360
|
+
const T theta = acos(T(0.95) * c);
|
|
361
|
+
const T dtheta_dc = -T(0.95) / sqrt(T(1) - T(0.9025) * c * c);
|
|
362
|
+
const T f1 = fc<T>(r1, T(p.rc_a)), f2 = fc<T>(r2, T(p.rc_a));
|
|
363
|
+
const T df1 = dfc<T>(r1, T(p.rc_a)), df2 = dfc<T>(r2, T(p.rc_a));
|
|
364
|
+
const T F = f1 * f2, mean = T(0.5) * (r1 + r2);
|
|
365
|
+
T Az[NZ], dAz[NZ];
|
|
366
|
+
for (int z = 0; z < NZ; ++z) {
|
|
367
|
+
const T dt = theta - T(p.shf_z[z]);
|
|
368
|
+
const T h = (T(1) + cos(dt)) / T(2);
|
|
369
|
+
Az[z] = T(2) * pow(h, T(p.zeta));
|
|
370
|
+
dAz[z] = -T(p.zeta) * pow(h, T(p.zeta) - T(1)) * sin(dt);
|
|
371
|
+
}
|
|
372
|
+
const int base = RADIAL_LEN + pair_index(sp[j], sp[k]) * NA * NZ;
|
|
373
|
+
T dL_dc = 0, dL_dm = 0, dL_dF = 0;
|
|
374
|
+
for (int ia = 0; ia < NA; ++ia) {
|
|
375
|
+
const T u = mean - T(p.shf_a[ia]);
|
|
376
|
+
const T R = exp(-T(p.eta_a) * u * u), dR = -T(2) * T(p.eta_a) * u * R;
|
|
377
|
+
for (int z = 0; z < NZ; ++z) {
|
|
378
|
+
const T G = g[base + ia * NZ + z];
|
|
379
|
+
dL_dc += G * R * F * dAz[z] * dtheta_dc;
|
|
380
|
+
dL_dm += G * Az[z] * F * dR;
|
|
381
|
+
dL_dF += G * Az[z] * R;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
const T dL_dr1 = T(0.5) * dL_dm + dL_dF * df1 * f2;
|
|
385
|
+
const T dL_dr2 = T(0.5) * dL_dm + dL_dF * f1 * df2;
|
|
386
|
+
for (int cc = 0; cc < 3; ++cc) {
|
|
387
|
+
const T va = dL_dr1 * a[cc] / r1 + dL_dc * (bv[cc] / rr - c * a[cc] / (r1 * r1));
|
|
388
|
+
const T vb = dL_dr2 * bv[cc] / r2 + dL_dc * (a[cc] / rr - c * bv[cc] / (r2 * r2));
|
|
389
|
+
atomicAdd(&GX[j * 3 + cc], va);
|
|
390
|
+
atomicAdd(&GX[k * 3 + cc], vb);
|
|
391
|
+
atomicAdd(&GX[i * 3 + cc], -(va + vb));
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
static Params make_params(c10::ArrayRef<double> shf_r, c10::ArrayRef<double> shf_a,
|
|
397
|
+
c10::ArrayRef<double> shf_z, double eta_r, double rc_r, double eta_a,
|
|
398
|
+
double zeta, double rc_a) {
|
|
399
|
+
TORCH_CHECK(shf_r.size() == NR && shf_a.size() == NA && shf_z.size() == NZ,
|
|
400
|
+
"emle::aev supports ", NR, " radial shifts, ", NA, " angular shifts and ", NZ,
|
|
401
|
+
" angle sections");
|
|
402
|
+
Params p;
|
|
403
|
+
p.eta_r = eta_r; p.rc_r = rc_r; p.eta_a = eta_a; p.zeta = zeta; p.rc_a = rc_a;
|
|
404
|
+
for (int k = 0; k < NR; ++k) p.shf_r[k] = shf_r[k];
|
|
405
|
+
for (int k = 0; k < NA; ++k) p.shf_a[k] = shf_a[k];
|
|
406
|
+
for (int k = 0; k < NZ; ++k) p.shf_z[k] = shf_z[k];
|
|
407
|
+
return p;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
torch::Tensor aev_fwd_cuda(const torch::Tensor& species_in, const torch::Tensor& xyz_in, c10::ArrayRef<double> shf_r, c10::ArrayRef<double> shf_a,
|
|
411
|
+
c10::ArrayRef<double> shf_z, double eta_r, double rc_r, double eta_a, double zeta, double rc_a) {
|
|
412
|
+
torch::Tensor species = species_in;
|
|
413
|
+
torch::Tensor xyz = xyz_in;
|
|
414
|
+
const c10::cuda::CUDAGuard guard(xyz.device());
|
|
415
|
+
species = species.to(torch::kInt).contiguous(); xyz = xyz.contiguous();
|
|
416
|
+
const int B = xyz.size(0), N = xyz.size(1);
|
|
417
|
+
auto out = torch::empty({B, N, AEV_LEN}, xyz.options());
|
|
418
|
+
Params p = make_params(shf_r, shf_a, shf_z, eta_r, rc_r, eta_a, zeta, rc_a);
|
|
419
|
+
AT_DISPATCH_FLOATING_TYPES(xyz.scalar_type(), "aev_forward", [&] {
|
|
420
|
+
aev_fwd<scalar_t><<<B * N, 128, AEV_LEN * sizeof(scalar_t), c10::cuda::getCurrentCUDAStream()>>>(
|
|
421
|
+
species.data_ptr<int>(), xyz.data_ptr<scalar_t>(), N, p, out.data_ptr<scalar_t>());
|
|
422
|
+
C10_CUDA_KERNEL_LAUNCH_CHECK();
|
|
423
|
+
});
|
|
424
|
+
return out;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
torch::Tensor aev_bwd_cuda(const torch::Tensor& species_in, const torch::Tensor& xyz_in, const torch::Tensor& grad_in, c10::ArrayRef<double> shf_r,
|
|
428
|
+
c10::ArrayRef<double> shf_a, c10::ArrayRef<double> shf_z, double eta_r, double rc_r, double eta_a,
|
|
429
|
+
double zeta, double rc_a) {
|
|
430
|
+
torch::Tensor species = species_in;
|
|
431
|
+
torch::Tensor xyz = xyz_in;
|
|
432
|
+
torch::Tensor grad = grad_in;
|
|
433
|
+
const c10::cuda::CUDAGuard guard(xyz.device());
|
|
434
|
+
species = species.to(torch::kInt).contiguous(); xyz = xyz.contiguous(); grad = grad.contiguous();
|
|
435
|
+
const int B = xyz.size(0), N = xyz.size(1);
|
|
436
|
+
auto gx = torch::zeros_like(xyz);
|
|
437
|
+
Params p = make_params(shf_r, shf_a, shf_z, eta_r, rc_r, eta_a, zeta, rc_a);
|
|
438
|
+
AT_DISPATCH_FLOATING_TYPES(xyz.scalar_type(), "aev_backward", [&] {
|
|
439
|
+
aev_bwd<scalar_t><<<B * N, 128, AEV_LEN * sizeof(scalar_t), c10::cuda::getCurrentCUDAStream()>>>(
|
|
440
|
+
species.data_ptr<int>(), xyz.data_ptr<scalar_t>(), grad.data_ptr<scalar_t>(), N, p, gx.data_ptr<scalar_t>());
|
|
441
|
+
C10_CUDA_KERNEL_LAUNCH_CHECK();
|
|
442
|
+
});
|
|
443
|
+
return gx;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
} // namespace
|
|
448
|
+
|
|
449
|
+
TORCH_LIBRARY_IMPL(emle, CUDA, m) {
|
|
450
|
+
m.impl("electrostatics_fwd", &electrostatics_fwd_cuda);
|
|
451
|
+
m.impl("electrostatics_bwd", &electrostatics_bwd_cuda);
|
|
452
|
+
m.impl("aev_fwd", &aev_fwd_cuda);
|
|
453
|
+
m.impl("aev_bwd", &aev_bwd_cuda);
|
|
454
|
+
}
|
emle/_ops.py
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2023-2026 The EMLE-Engine developers
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
"""
|
|
5
|
+
Optional fused C++/CUDA operators.
|
|
6
|
+
|
|
7
|
+
The operators (torch.ops.emle.electrostatics and torch.ops.emle.aev) are
|
|
8
|
+
loaded from the emle._C extension when emle-engine was installed with them
|
|
9
|
+
(EMLE_BUILD_EXTENSIONS=1 pip install --no-build-isolation ., see setup.py).
|
|
10
|
+
Otherwise they are compiled from emle/_csrc the first time they are requested
|
|
11
|
+
and cached by PyTorch (in ~/.cache/torch_extensions, or TORCH_EXTENSIONS_DIR),
|
|
12
|
+
which needs a C++ compiler and, for the CUDA kernels, a CUDA toolkit matching
|
|
13
|
+
PyTorch's CUDA version (CUDA_HOME). Set EMLE_JIT_EXTENSIONS=0 to disable
|
|
14
|
+
the run-time compilation.
|
|
15
|
+
|
|
16
|
+
Once loaded, the operators are registered with PyTorch's dispatcher, so
|
|
17
|
+
TorchScript models that use them work in this process (including from C++,
|
|
18
|
+
e.g. OpenMM-Torch, in the same process).
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
__all__ = ["compiler_flags", "load_ops"]
|
|
22
|
+
|
|
23
|
+
import glob as _glob
|
|
24
|
+
import os as _os
|
|
25
|
+
import sys as _sys
|
|
26
|
+
|
|
27
|
+
_loaded = None
|
|
28
|
+
_error = None
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def compiler_flags():
|
|
32
|
+
"""
|
|
33
|
+
Host compiler and linker flags for the operators. The CPU kernels are
|
|
34
|
+
parallelised with ATen's parallel_for, which uses OpenMP in PyTorch's
|
|
35
|
+
Linux builds; elsewhere they are compiled without it.
|
|
36
|
+
"""
|
|
37
|
+
if _sys.platform.startswith("linux"):
|
|
38
|
+
return ["-O3", "-fopenmp"], ["-fopenmp"]
|
|
39
|
+
if _sys.platform == "win32":
|
|
40
|
+
return ["/O2"], []
|
|
41
|
+
return ["-O3"], []
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _check_cuda_toolkit(cuda_home):
|
|
45
|
+
"""
|
|
46
|
+
Raise an error if the major version of the CUDA toolkit differs from that
|
|
47
|
+
of PyTorch's CUDA, since PyTorch can't load kernels built with it.
|
|
48
|
+
|
|
49
|
+
Parameters
|
|
50
|
+
----------
|
|
51
|
+
|
|
52
|
+
cuda_home: str
|
|
53
|
+
The location of the CUDA toolkit.
|
|
54
|
+
"""
|
|
55
|
+
import re as _re
|
|
56
|
+
import subprocess as _subprocess
|
|
57
|
+
|
|
58
|
+
import torch as _torch
|
|
59
|
+
|
|
60
|
+
global _error
|
|
61
|
+
|
|
62
|
+
nvcc = _os.path.join(cuda_home, "bin", "nvcc")
|
|
63
|
+
try:
|
|
64
|
+
output = _subprocess.check_output([nvcc, "--version"], text=True)
|
|
65
|
+
version = _re.search(r"release (\d+)\.(\d+)", output).group(1, 2)
|
|
66
|
+
except Exception:
|
|
67
|
+
return
|
|
68
|
+
|
|
69
|
+
toolkit = ".".join(version)
|
|
70
|
+
if version[0] != _torch.version.cuda.split(".")[0]:
|
|
71
|
+
_error = RuntimeError(
|
|
72
|
+
f"Unable to build the fused EMLE operators: The CUDA toolkit in "
|
|
73
|
+
f"'{cuda_home}' is version {toolkit}, but PyTorch uses CUDA "
|
|
74
|
+
f"{_torch.version.cuda}. Set CUDA_HOME to a CUDA "
|
|
75
|
+
f"{_torch.version.cuda} toolkit."
|
|
76
|
+
)
|
|
77
|
+
raise _error
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def load_ops(verbose=False):
|
|
81
|
+
"""
|
|
82
|
+
Compile (if needed) and load the fused EMLE operators.
|
|
83
|
+
|
|
84
|
+
Returns
|
|
85
|
+
-------
|
|
86
|
+
|
|
87
|
+
with_cuda: bool
|
|
88
|
+
Whether the CUDA kernels are available.
|
|
89
|
+
"""
|
|
90
|
+
global _loaded, _error
|
|
91
|
+
|
|
92
|
+
if _loaded is not None:
|
|
93
|
+
return _loaded
|
|
94
|
+
|
|
95
|
+
# Raise the error from an earlier failed attempt, rather than retrying.
|
|
96
|
+
if _error is not None:
|
|
97
|
+
raise RuntimeError(str(_error))
|
|
98
|
+
|
|
99
|
+
import torch as _torch
|
|
100
|
+
|
|
101
|
+
# Prefer an ahead-of-time build.
|
|
102
|
+
try:
|
|
103
|
+
from . import _C
|
|
104
|
+
|
|
105
|
+
_loaded = bool(_C.with_cuda)
|
|
106
|
+
return _loaded
|
|
107
|
+
except ImportError:
|
|
108
|
+
pass
|
|
109
|
+
|
|
110
|
+
if _os.environ.get("EMLE_JIT_EXTENSIONS", "1") == "0":
|
|
111
|
+
_error = RuntimeError(
|
|
112
|
+
"Unable to load the fused EMLE operators: emle._C is not installed "
|
|
113
|
+
"and EMLE_JIT_EXTENSIONS=0"
|
|
114
|
+
)
|
|
115
|
+
raise _error
|
|
116
|
+
|
|
117
|
+
from torch.utils import cpp_extension as _cpp_extension
|
|
118
|
+
|
|
119
|
+
here = _os.path.join(_os.path.dirname(_os.path.abspath(__file__)), "_csrc")
|
|
120
|
+
sources = [_os.path.join(here, "ops.cpp")]
|
|
121
|
+
cuda_home = _cpp_extension.CUDA_HOME
|
|
122
|
+
with_cuda = (
|
|
123
|
+
_torch.cuda.is_available()
|
|
124
|
+
and cuda_home is not None
|
|
125
|
+
and bool(
|
|
126
|
+
_glob.glob(_os.path.join(cuda_home, "include", "cuda_runtime.h"))
|
|
127
|
+
+ _glob.glob(
|
|
128
|
+
_os.path.join(cuda_home, "targets", "*", "include", "cuda_runtime.h")
|
|
129
|
+
)
|
|
130
|
+
)
|
|
131
|
+
)
|
|
132
|
+
if with_cuda:
|
|
133
|
+
_check_cuda_toolkit(cuda_home)
|
|
134
|
+
sources.append(_os.path.join(here, "ops_cuda.cu"))
|
|
135
|
+
|
|
136
|
+
# PyTorch does not rebuild a cached extension when the GPU architecture
|
|
137
|
+
# changes, so include the target architectures in its name (caches may be
|
|
138
|
+
# shared between machines with different GPUs).
|
|
139
|
+
if with_cuda:
|
|
140
|
+
archs = _os.environ.get("TORCH_CUDA_ARCH_LIST")
|
|
141
|
+
if archs is None:
|
|
142
|
+
archs = sorted(
|
|
143
|
+
{
|
|
144
|
+
"%d%d" % _torch.cuda.get_device_capability(i)
|
|
145
|
+
for i in range(_torch.cuda.device_count())
|
|
146
|
+
}
|
|
147
|
+
)
|
|
148
|
+
else:
|
|
149
|
+
archs = sorted(
|
|
150
|
+
"".join(c for c in a if c.isalnum())
|
|
151
|
+
for a in archs.replace(";", " ").split()
|
|
152
|
+
)
|
|
153
|
+
name = "emle_ops_cuda_" + "_".join(archs)
|
|
154
|
+
else:
|
|
155
|
+
name = "emle_ops_cpu"
|
|
156
|
+
|
|
157
|
+
cflags, ldflags = compiler_flags()
|
|
158
|
+
try:
|
|
159
|
+
_cpp_extension.load(
|
|
160
|
+
name=name,
|
|
161
|
+
sources=sources,
|
|
162
|
+
extra_cflags=cflags + (["-DEMLE_WITH_CUDA"] if with_cuda else []),
|
|
163
|
+
extra_ldflags=ldflags,
|
|
164
|
+
extra_cuda_cflags=["-O3"],
|
|
165
|
+
is_python_module=False,
|
|
166
|
+
verbose=verbose,
|
|
167
|
+
)
|
|
168
|
+
except Exception as e:
|
|
169
|
+
_error = RuntimeError(f"Unable to build the fused EMLE operators: {e}")
|
|
170
|
+
raise _error from e
|
|
171
|
+
|
|
172
|
+
_loaded = with_cuda
|
|
173
|
+
return _loaded
|