-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathklujax.cpp
433 lines (386 loc) · 14.4 KB
/
klujax.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// version: 0.4.0
// Imports
#include <cmath>
#include "klu.h"
#include "pybind11/pybind11.h"
#include "xla/ffi/api/ffi.h"
namespace py = pybind11;
namespace ffi = xla::ffi;
#include <cstring> // for memset
ffi::Error validate_dot_f64_args(
const ffi::Buffer<ffi::DataType::S32> &Ai,
const ffi::Buffer<ffi::DataType::S32> &Aj,
const ffi::AnyBuffer::Dimensions ds_Ax,
const ffi::AnyBuffer::Dimensions ds_x) {
int d_x = ds_x.size();
if (d_x != 3) {
return ffi::Error::InvalidArgument("x is not 3D.");
}
int n_lhs = (int)ds_x[0];
int n_col = (int)ds_x[1];
int d_Ax = ds_Ax.size();
if (d_Ax != 2) {
return ffi::Error::InvalidArgument("Ax is not 2D.");
}
int n_lhs_bis = (int)ds_Ax[0];
int n_nz = (int)ds_Ax[1];
if (n_lhs != n_lhs_bis) {
return ffi::Error::InvalidArgument(
"n_lhs mismatch: Ax.shape[0] != x.shape[0]: Got " + std::to_string(n_lhs_bis) + " != " + std::to_string(n_lhs));
}
auto ds_Ai = Ai.dimensions();
int d_Ai = ds_Ai.size();
if (d_Ai != 1) {
return ffi::Error::InvalidArgument("Ai is not 1D.");
}
int n_nz_bis = (int)ds_Ai[0];
if (n_nz != n_nz_bis) {
return ffi::Error::InvalidArgument(
"n_lhs mismatch: Ai.shape[0] != Ax.shape[1]: Got " + std::to_string(n_nz_bis) + " != " + std::to_string(n_nz));
}
auto ds_Aj = Aj.dimensions();
int d_Aj = ds_Aj.size();
if (d_Aj != 1) {
return ffi::Error::InvalidArgument("Aj is not 1D.");
}
n_nz_bis = (int)ds_Aj[0];
if (n_nz != n_nz_bis) {
return ffi::Error::InvalidArgument(
"n_lhs mismatch: Aj.shape[0] != Ax.shape[1]: Got " + std::to_string(n_nz_bis) + " != " + std::to_string(n_nz));
}
int i;
int j;
const int *_Ai = Ai.typed_data();
const int *_Aj = Aj.typed_data();
for (int n = 0; n < n_nz; n++) {
i = _Ai[n];
if (i >= n_col) {
return ffi::Error::InvalidArgument("Ai.max() >= n_col");
}
j = _Aj[n];
if (j >= n_col) {
return ffi::Error::InvalidArgument("Aj.max() >= n_col");
}
}
return ffi::Error::Success();
}
void coo_to_csc_analyze(
const int n_col,
const int n_nz,
const int *Ai,
const int *Aj,
int *Bi,
int *Bp,
int *Bk) {
// compute number of non-zero entries per row of A
for (int n = 0; n < n_nz; n++) {
Bp[Aj[n]] += 1;
}
// cumsum the n_nz per row to get Bp
int cumsum = 0;
int temp = 0;
for (int j = 0; j <= n_col; j++) {
temp = Bp[j];
Bp[j] = cumsum;
cumsum += temp;
}
// write Ai, Aj into Bi, Bk
int col = 0;
int dest = 0;
for (int n = 0; n < n_nz; n++) {
col = Aj[n];
dest = Bp[col];
Bi[dest] = Ai[n];
Bk[dest] = n;
Bp[col] += 1;
}
int last = 0;
for (int i = 0; i <= n_col; i++) {
temp = Bp[i];
Bp[i] = last;
last = temp;
}
}
ffi::Error dot_f64(
const ffi::Buffer<ffi::DataType::S32> Ai,
const ffi::Buffer<ffi::DataType::S32> Aj,
const ffi::Buffer<ffi::DataType::F64> Ax,
const ffi::Buffer<ffi::DataType::F64> x,
ffi::Result<ffi::Buffer<ffi::DataType::F64>> b) {
auto ds_x = x.dimensions();
auto ds_Ax = Ax.dimensions();
ffi::Error err = validate_dot_f64_args(Ai, Aj, ds_Ax, ds_x);
if (err.failure()) {
return err;
}
int n_lhs = (int)ds_x[0];
int n_col = (int)ds_x[1];
int n_rhs = (int)ds_x[2];
int n_nz = (int)ds_Ax[1];
const int *_Ai = Ai.typed_data();
const int *_Aj = Aj.typed_data();
const double *_Ax = Ax.typed_data();
const double *_x = x.typed_data();
double *_b = b->typed_data();
// initialize empty result
for (int i = 0; i < n_lhs * n_col * n_rhs; i++) {
_b[i] = 0.0;
}
// fill result (all multi-dim arrays are row-major)
// x_mik = A_mij × x_mjk (einsum)
// sizes: m<n_lhs; i<n_col<--Ai; j<n_col<--Aj; k<n_rhs
int i;
int j;
for (int n = 0; n < n_nz; n++) {
i = _Ai[n];
j = _Aj[n];
for (int m = 0; m < n_lhs; m++) {
for (int k = 0; k < n_rhs; k++) {
_b[m * n_col * n_rhs + i * n_rhs + k] += _Ax[m * n_nz + n] * _x[m * n_col * n_rhs + j * n_rhs + k];
}
}
}
return ffi::Error::Success();
}
XLA_FFI_DEFINE_HANDLER_SYMBOL( // b = A x
dot_f64_handler, dot_f64,
ffi::Ffi::Bind()
.Arg<ffi::Buffer<ffi::DataType::S32>>() // Ai
.Arg<ffi::Buffer<ffi::DataType::S32>>() // Aj
.Arg<ffi::Buffer<ffi::DataType::F64>>() // Ax
.Arg<ffi::Buffer<ffi::DataType::F64>>() // x
.Ret<ffi::Buffer<ffi::DataType::F64>>() // b
);
ffi::Error dot_c128(
const ffi::Buffer<ffi::DataType::S32> Ai,
const ffi::Buffer<ffi::DataType::S32> Aj,
const ffi::Buffer<ffi::DataType::C128> Ax,
const ffi::Buffer<ffi::DataType::C128> x,
ffi::Result<ffi::Buffer<ffi::DataType::C128>> b) {
auto ds_x = x.dimensions();
auto ds_Ax = Ax.dimensions();
ffi::Error err = validate_dot_f64_args(Ai, Aj, ds_Ax, ds_x);
if (err.failure()) {
return err;
}
int n_lhs = (int)ds_x[0];
int n_col = (int)ds_x[1];
int n_rhs = (int)ds_x[2];
int n_nz = (int)ds_Ax[1];
const int *_Ai = Ai.typed_data();
const int *_Aj = Aj.typed_data();
const double *_Ax = (double *)Ax.typed_data();
const double *_x = (double *)x.typed_data();
double *_b = (double *)b->typed_data();
// initialize empty result
for (int i = 0; i < 2 * n_lhs * n_col * n_rhs; i++) {
_b[i] = 0.0;
}
// fill result (all multi-dim arrays are row-major)
// x_mik = A_mij × x_mjk (einsum)
// sizes: m<n_lhs; i<n_col<--Ai; j<n_col<--Aj; k<n_rhs
int i;
int j;
for (int n = 0; n < n_nz; n++) {
i = _Ai[n];
j = _Aj[n];
for (int m = 0; m < n_lhs; m++) {
for (int k = 0; k < n_rhs; k++) {
_b[2 * (m * n_col * n_rhs + i * n_rhs + k)] += // real
_Ax[2 * (m * n_nz + n)] * _x[2 * (m * n_col * n_rhs + j * n_rhs + k)] // real*real
- _Ax[2 * (m * n_nz + n) + 1] * _x[2 * (m * n_col * n_rhs + j * n_rhs + k) + 1]; // imag*imag
_b[2 * (m * n_col * n_rhs + i * n_rhs + k) + 1] += // imag
_Ax[2 * (m * n_nz + n)] * _x[2 * (m * n_col * n_rhs + j * n_rhs + k) + 1] // real*imag
+ _Ax[2 * (m * n_nz + n) + 1] * _x[2 * (m * n_col * n_rhs + j * n_rhs + k)]; // imag*real
}
}
}
return ffi::Error::Success();
}
XLA_FFI_DEFINE_HANDLER_SYMBOL( // b = A x
dot_c128_handler, dot_c128,
ffi::Ffi::Bind()
.Arg<ffi::Buffer<ffi::DataType::S32>>() // Ai
.Arg<ffi::Buffer<ffi::DataType::S32>>() // Aj
.Arg<ffi::Buffer<ffi::DataType::C128>>() // Ax
.Arg<ffi::Buffer<ffi::DataType::C128>>() // x
.Ret<ffi::Buffer<ffi::DataType::C128>>() // b
);
ffi::Error solve_f64(
ffi::Buffer<ffi::DataType::S32> Ai,
ffi::Buffer<ffi::DataType::S32> Aj,
ffi::Buffer<ffi::DataType::F64> Ax,
ffi::Buffer<ffi::DataType::F64> b,
ffi::Result<ffi::Buffer<ffi::DataType::F64>> x) {
auto ds_b = b.dimensions();
auto ds_Ax = Ax.dimensions();
ffi::Error err = validate_dot_f64_args(Ai, Aj, ds_Ax, ds_b);
if (err.failure()) {
return err;
}
int n_lhs = (int)ds_b[0];
int n_col = (int)ds_b[1];
int n_rhs = (int)ds_b[2];
int n_nz = (int)ds_Ax[1];
const int *_Ai = Ai.typed_data();
const int *_Aj = Aj.typed_data();
const double *_Ax = Ax.typed_data();
const double *_b = b.typed_data();
double *_x = x->typed_data();
// get COO -> CSC transformation information
int *_Bk = new int[n_nz](); // Ax -> Bx transformation indices
int *_Bi = new int[n_nz]();
int *_Bp = new int[n_col + 1]();
double *_Bx = new double[n_nz]();
coo_to_csc_analyze(n_col, n_nz, _Ai, _Aj, _Bi, _Bp, _Bk);
// copy _b into _x_temp and transpose the last two dimensions since KLU expects col-major layout
// _b itself won't be used anymore. KLU works on _x_temp in-place.
double *_x_temp = new double[n_lhs * n_col * n_rhs]();
for (int m = 0; m < n_lhs; m++) {
for (int n = 0; n < n_col; n++) {
for (int p = 0; p < n_rhs; p++) {
_x_temp[m * n_rhs * n_col + p * n_col + n] = _b[m * n_col * n_rhs + n * n_rhs + p];
}
}
}
// initialize KLU for given sparsity pattern
klu_symbolic *Symbolic;
klu_numeric *Numeric;
klu_common Common;
klu_defaults(&Common);
Symbolic = klu_analyze(n_col, _Bp, _Bi, &Common);
// solve for all elements in batch:
// NOTE: same sparsity pattern for each element in batch assumed
for (int i = 0; i < n_lhs; i++) {
int m = i * n_nz;
int n = i * n_rhs * n_col;
// convert COO Ax to CSC Bx
for (int k = 0; k < n_nz; k++) {
_Bx[k] = _Ax[m + _Bk[k]];
}
// solve using KLU
Numeric = klu_factor(_Bp, _Bi, _Bx, Symbolic, &Common);
klu_solve(Symbolic, Numeric, n_col, n_rhs, &_x_temp[n], &Common);
}
// copy _x_temp into _x and transpose the last two dimensions since JAX expects row-major layout
// NOTE: it feels a bit weird to have to do all this copying and transposing here. This might actually be
// pretty inefficient. Ideally I'd like to get rid of this transpose. Maybe just represent b/x in python
// as n_lhs x n_rhs x n_col in stead of n_lhs x n_col x n_rhs?
for (int m = 0; m < n_lhs; m++) {
for (int n = 0; n < n_col; n++) {
for (int p = 0; p < n_rhs; p++) {
_x[m * n_col * n_rhs + n * n_rhs + p] = _x_temp[m * n_rhs * n_col + p * n_col + n];
}
}
}
// clean up
klu_free_symbolic(&Symbolic, &Common);
klu_free_numeric(&Numeric, &Common);
delete[] _Bk;
delete[] _Bi;
delete[] _Bp;
delete[] _Bx;
delete[] _x_temp;
return ffi::Error::Success();
}
XLA_FFI_DEFINE_HANDLER_SYMBOL( // b = A x
solve_f64_handler, solve_f64,
ffi::Ffi::Bind()
.Arg<ffi::Buffer<ffi::DataType::S32>>() // Ai
.Arg<ffi::Buffer<ffi::DataType::S32>>() // Aj
.Arg<ffi::Buffer<ffi::DataType::F64>>() // Ax
.Arg<ffi::Buffer<ffi::DataType::F64>>() // b
.Ret<ffi::Buffer<ffi::DataType::F64>>() // x
);
ffi::Error solve_c128(
const ffi::Buffer<ffi::DataType::S32> Ai,
const ffi::Buffer<ffi::DataType::S32> Aj,
const ffi::Buffer<ffi::DataType::C128> Ax,
const ffi::Buffer<ffi::DataType::C128> b,
ffi::Result<ffi::Buffer<ffi::DataType::C128>> x) {
auto ds_x = b.dimensions();
auto ds_Ax = Ax.dimensions();
ffi::Error err = validate_dot_f64_args(Ai, Aj, ds_Ax, ds_x);
if (err.failure()) {
return err;
}
int n_lhs = (int)ds_x[0];
int n_col = (int)ds_x[1];
int n_rhs = (int)ds_x[2];
int n_nz = (int)ds_Ax[1];
const int *_Ai = Ai.typed_data();
const int *_Aj = Aj.typed_data();
const double *_Ax = (double *)Ax.typed_data();
const double *_b = (double *)b.typed_data();
double *_x = (double *)x->typed_data();
// get COO -> CSC transformation information
int *_Bk = new int[n_nz](); // Ax -> Bx transformation indices
int *_Bi = new int[n_nz](); // CSC row indices
int *_Bp = new int[n_col + 1](); // CSC column pointers
double *_Bx = new double[2 * n_nz]();
coo_to_csc_analyze(n_col, n_nz, _Ai, _Aj, _Bi, _Bp, _Bk);
// copy _b into _x_temp and transpose the last two dimensions since KLU expects col-major layout
// _b itself won't be used anymore. KLU works on _x_temp in-place.
double *_x_temp = new double[2 * n_lhs * n_col * n_rhs]();
for (int m = 0; m < n_lhs; m++) {
for (int n = 0; n < n_col; n++) {
for (int p = 0; p < n_rhs; p++) {
_x_temp[2 * (m * n_rhs * n_col + p * n_col + n)] = _b[2 * (m * n_col * n_rhs + n * n_rhs + p)];
_x_temp[2 * (m * n_rhs * n_col + p * n_col + n) + 1] = _b[2 * (m * n_col * n_rhs + n * n_rhs + p) + 1];
}
}
}
// initialize KLU for given sparsity pattern
klu_symbolic *Symbolic;
klu_numeric *Numeric;
klu_common Common;
klu_defaults(&Common);
Symbolic = klu_analyze(n_col, _Bp, _Bi, &Common);
// solve for all elements in batch:
// NOTE: same sparsity pattern for each element in batch assumed
for (int i = 0; i < n_lhs; i++) {
int m = i * n_nz;
int n = i * n_rhs * n_col;
// convert COO Ax to CSC Bx
for (int k = 0; k < n_nz; k++) {
_Bx[2 * k] = _Ax[2 * (m + _Bk[k])];
_Bx[2 * k + 1] = _Ax[2 * (m + _Bk[k]) + 1];
}
// solve using KLU
Numeric = klu_z_factor(_Bp, _Bi, _Bx, Symbolic, &Common);
klu_z_solve(Symbolic, Numeric, n_col, n_rhs, &_x_temp[2 * n], &Common);
}
// copy _x_temp into _x and transpose the last two dimensions since JAX expects row-major layout
// NOTE: it feels a bit weird to have to do all this copying and transposing here. This might actually be
// pretty inefficient. Ideally I'd like to get rid of this transpose. Maybe just represent b/x in python
// as n_lhs x n_rhs x n_col in stead of n_lhs x n_col x n_rhs?
for (int m = 0; m < n_lhs; m++) {
for (int n = 0; n < n_col; n++) {
for (int p = 0; p < n_rhs; p++) {
_x[2 * (m * n_col * n_rhs + n * n_rhs + p)] = _x_temp[2 * (m * n_rhs * n_col + p * n_col + n)];
_x[2 * (m * n_col * n_rhs + n * n_rhs + p) + 1] = _x_temp[2 * (m * n_rhs * n_col + p * n_col + n) + 1];
}
}
}
return ffi::Error::Success();
}
XLA_FFI_DEFINE_HANDLER_SYMBOL( // b = A x
solve_c128_handler, solve_c128,
ffi::Ffi::Bind()
.Arg<ffi::Buffer<ffi::DataType::S32>>() // Ai
.Arg<ffi::Buffer<ffi::DataType::S32>>() // Aj
.Arg<ffi::Buffer<ffi::DataType::C128>>() // Ax
.Arg<ffi::Buffer<ffi::DataType::C128>>() // b
.Ret<ffi::Buffer<ffi::DataType::C128>>() // x
);
// Python wrappers
PYBIND11_MODULE(klujax_cpp, m) {
m.def("dot_f64",
[]() { return py::capsule((void *)&dot_f64_handler); });
m.def("dot_c128",
[]() { return py::capsule((void *)&dot_c128_handler); });
m.def("solve_f64",
[]() { return py::capsule((void *)&solve_f64_handler); });
m.def("solve_c128",
[]() { return py::capsule((void *)&solve_c128_handler); });
}