forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSparseBlasImpl.cpp
486 lines (429 loc) · 14.8 KB
/
SparseBlasImpl.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#include <ATen/Dispatch.h>
#include <ATen/SparseCsrTensorImpl.h>
#include <ATen/Tensor.h>
#include <ATen/mkl/Sparse.h>
#include <ATen/native/LinearAlgebraUtils.h>
#include <ATen/native/mkl/SparseBlasImpl.h>
#include <c10/core/ScalarType.h>
#include <c10/util/MaybeOwned.h>
#if AT_USE_MKL_SPARSE()
#include <ATen/mkl/SparseBlas.h>
#include <ATen/mkl/SparseDescriptors.h>
#include <ATen/mkl/Utils.h>
#endif
namespace at {
namespace native {
namespace sparse {
namespace impl {
namespace mkl {
namespace {
c10::MaybeOwned<Tensor> prepare_dense_matrix_for_mkl(
const Tensor& tensor) {
if (tensor.is_non_overlapping_and_dense() ||
is_blas_compatible_row_major_order(tensor) ||
is_blas_compatible_column_major_order(tensor)) {
return c10::MaybeOwned<Tensor>::borrowed(tensor);
} else {
return c10::MaybeOwned<Tensor>::owned(
tensor.clone(at::MemoryFormat::Contiguous));
}
}
/*
Get row-major or column-major matrix.
Args:
* `tensor` - 2D strided Tensor.
* `row_major` - controls the memory layout.
*/
c10::MaybeOwned<Tensor> prepare_dense_matrix_for_mkl(
const Tensor& tensor,
bool row_major) {
if (is_blas_compatible_row_major_order(tensor) && row_major) {
return c10::MaybeOwned<Tensor>::borrowed(tensor);
} else {
if (row_major) {
return c10::MaybeOwned<Tensor>::owned(
tensor.clone(at::MemoryFormat::Contiguous));
} else {
return c10::MaybeOwned<Tensor>::owned(cloneBatchedColumnMajor(tensor));
}
}
}
c10::MaybeOwned<Tensor> inline prepare_dense_vector_for_mkl(
const Tensor& tensor) {
if (tensor.is_non_overlapping_and_dense()) {
return c10::MaybeOwned<Tensor>::borrowed(tensor);
} else {
return c10::MaybeOwned<Tensor>::owned(
tensor.clone(at::MemoryFormat::Contiguous));
}
}
void inline indices_to_mkl_compatible_inplace(const Tensor& input) {
#ifdef MKL_ILP64
// ILP64 is a 64-bit API version of MKL
// Indices tensor must have ScalarType::Long type
static_cast<SparseCsrTensorImpl*>(input.unsafeGetTensorImpl())
->set_member_tensors(
input.crow_indices().to(kLong),
input.col_indices().to(kLong),
input.values(),
input.sizes());
#else
// LP64 is a 32-bit API version of MKL
// Indices tensor must have ScalarType::Int type
static_cast<SparseCsrTensorImpl*>(input.unsafeGetTensorImpl())
->set_member_tensors(
input.crow_indices().to(kInt),
input.col_indices().to(kInt),
input.values(),
input.sizes());
#endif
}
void inline col_indices_and_values_resize_(const Tensor& input, int64_t nnz) {
static_cast<SparseCsrTensorImpl*>(input.unsafeGetTensorImpl())
->set_member_tensors(
input.crow_indices(),
input.col_indices().resize_({nnz}),
input.values().resize_({nnz}),
input.sizes());
}
/*
Resizes `input` tensor and fills it with the data from MKL.
*/
#if AT_USE_MKL_SPARSE()
template <typename scalar_t>
void mkl_result_copy_(const Tensor& input, sparse_matrix_t mkl_desc) {
sparse_index_base_t indexing = SPARSE_INDEX_BASE_ZERO;
MKL_INT rows, cols;
MKL_INT *rows_start = nullptr, *rows_end = nullptr, *columns = nullptr;
scalar_t* values = nullptr;
at::mkl::sparse::export_csr(
mkl_desc,
&indexing,
&rows,
&cols,
&rows_start,
&rows_end,
&columns,
&values);
// Resize input using nnz information from MKL
MKL_INT nnz = rows_end[rows - 1];
col_indices_and_values_resize_(input, nnz);
auto crow_indices = input.crow_indices();
auto col_indices = input.col_indices();
auto input_values = input.values();
// MKL Sparse Inspector-Executor doesn't have a way to provide external
// buffers So we have to copy the memory allocated by MKL
std::memcpy(
input_values.data_ptr<scalar_t>(), values, nnz * sizeof(scalar_t));
std::memcpy(col_indices.data_ptr<MKL_INT>(), columns, nnz * sizeof(MKL_INT));
std::memcpy(
crow_indices.data_ptr<MKL_INT>(), rows_start, rows * sizeof(MKL_INT));
crow_indices.data_ptr<MKL_INT>()[rows] = nnz;
}
#endif
/*
Computes a sparse matrix-dense matrix product defined as
C <- alpha*(A*B) + beta*C
Args:
* `A` - Sparse Tensor storing m x k matrix.
* `B` - Dense Tensor storing k x n matrix.
* `C` - [in] Dense Tensor storing matrix of size m x n.
[out] result of the operation.
*/
void addmm_dense_result(
const Tensor& A,
const Tensor& B,
const Scalar& beta,
const Scalar& alpha,
const Tensor& C) {
#if !AT_USE_MKL_SPARSE()
TORCH_CHECK(
false,
"Calling addmm on a sparse CPU tensor requires Linux platform. ",
"Please use PyTorch built with MKL on Linux.");
#else
c10::MaybeOwned<Tensor> C_ = prepare_dense_matrix_for_mkl(C);
IntArrayRef C_strides = C_->strides();
auto ndim = C_->dim();
bool is_C_row_major = (C_strides[ndim - 1] == 1);
// MKL requires same storage layout of matrices
c10::MaybeOwned<Tensor> B_ = prepare_dense_matrix_for_mkl(B, is_C_row_major);
IntArrayRef B_strides = B_->strides();
bool is_B_row_major = (B_strides[ndim - 1] == 1);
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(!(is_C_row_major ^ is_B_row_major));
auto order =
is_C_row_major ? SPARSE_LAYOUT_ROW_MAJOR : SPARSE_LAYOUT_COLUMN_MAJOR;
auto ldc = is_C_row_major ? C_strides[ndim - 2] : C_strides[ndim - 1];
auto ldb = is_B_row_major ? B_strides[ndim - 2] : B_strides[ndim - 1];
auto columns_C = mkl_int_cast(C.size(-1), "columns_C");
matrix_descr descrA;
descrA.type = SPARSE_MATRIX_TYPE_GENERAL;
AT_DISPATCH_FLOATING_AND_COMPLEX_TYPES(
C.scalar_type(), "addmm_out_sparse_csr_impl_mkl", [&] {
auto beta_ = beta.to<scalar_t>();
auto alpha_ = alpha.to<scalar_t>();
auto mkl_sparse_mat =
at::mkl::sparse::MklSparseCsrDescriptor<scalar_t>(A);
at::mkl::sparse::mm<scalar_t>(
SPARSE_OPERATION_NON_TRANSPOSE,
alpha_,
mkl_sparse_mat.descriptor(),
descrA,
order,
B_->data_ptr<scalar_t>(),
columns_C,
ldb,
beta_,
C_->data_ptr<scalar_t>(),
ldc);
});
if (!C.is_same(*C_)) {
C.copy_(*C_);
}
#endif
}
/*
Computes a sparse matrix-sparse matrix product defined as
C <- alpha*(A*B) + beta*C
Args:
* `mat1` - Sparse CSR Tensor storing m x k matrix A.
* `mat2` - Sparse CSR Tensor storing k x n matrix B.
* `result` - [in] Sparse CSR Tensor storing matrix C of size m x n.
[out] result of the operation.
*/
void addmm_sparse_result(
const Tensor& mat1,
const Tensor& mat2,
const Scalar& beta,
const Scalar& alpha,
const Tensor& result) {
#if !AT_USE_MKL_SPARSE()
TORCH_CHECK(
false,
"Calling add on a sparse CPU tensor requires Linux platform. ",
"Please use PyTorch built with MKL on Linux.");
#else
// Compute beta*result because MKL doesn't do it
// If beta is zero NaN and Inf should not be propagated to the result
if (beta.toComplexDouble() == 0.) {
result.values().zero_();
} else {
result.values().mul_(beta);
}
// MKL doesn't work with empty matrices
if (mat1._nnz() == 0 || mat2._nnz() == 0) {
return;
}
// MKL doesn't have an interface to compute alpha*(A*B) + beta*C at once
Tensor mat1_mat2 = at::empty(result.sizes(), result.options());
indices_to_mkl_compatible_inplace(mat1_mat2);
AT_DISPATCH_FLOATING_AND_COMPLEX_TYPES(
result.scalar_type(), "addmm_out_sparse_csr_impl_mkl_sparse", [&] {
auto mkl_sparse_mat1 =
at::mkl::sparse::MklSparseCsrDescriptor<scalar_t>(mat1);
auto mkl_sparse_mat2 =
at::mkl::sparse::MklSparseCsrDescriptor<scalar_t>(mat2);
auto mkl_result = at::mkl::sparse::MklSparseCsrDescriptor<scalar_t>();
auto result_desc = mkl_result.descriptor();
TORCH_MKLSPARSE_CHECK(mkl_sparse_spmm(
SPARSE_OPERATION_NON_TRANSPOSE,
mkl_sparse_mat1.descriptor(),
mkl_sparse_mat2.descriptor(),
&result_desc));
// copy the data from MKL, otherwise computed result will be destroyed
// together with `mkl_result`
mkl_result_copy_<scalar_t>(mat1_mat2, result_desc);
});
result.add_(mat1_mat2, alpha);
#endif
}
} // anonymous namespace
/*
Computes a matrix-matrix product defined as
C <- alpha*(A*B) + beta*C
Args:
* `mat1` - Tensor storing m x k matrix A.
* `mat2` - Tensor storing k x n matrix B.
* `result` - [in] Tensor storing matrix C of size m x n.
[out] result of the operation.
*/
void addmm_out_sparse_csr(
const Tensor& mat1,
const Tensor& mat2,
const Scalar& beta,
const Scalar& alpha,
const Tensor& result) {
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(mat1.dim() == 2 && mat2.dim() == 2 && result.dim() == 2);
if (mat2.layout() == kStrided && result.layout() == kStrided) {
return addmm_dense_result(mat1, mat2, beta, alpha, result);
} else if (mat2.is_sparse_csr() && result.is_sparse_csr()) {
return addmm_sparse_result(mat1, mat2, beta, alpha, result);
} else {
TORCH_INTERNAL_ASSERT(
false, "addmm: Received unexpected tensor layouts as input.");
}
}
/*
Computes a sparse matrix-dense vector product defined as
y <- alpha*op(A)*x + beta*y
Args:
* `mat` - Tensor storing sparse m x n matrix A.
* `vec` - Tensor storing dense vector x of size n.
* `result` - [in] Tensor storing dense vector y of size m.
[out] result of the operation.
*/
void addmv_out_sparse_csr(
const Tensor& mat,
const Tensor& vec,
const Scalar& beta,
const Scalar& alpha,
const Tensor& result) {
#if !AT_USE_MKL_SPARSE()
TORCH_CHECK(
false,
"Calling addmv on a sparse CPU tensor requires Linux platform. ",
"Please use PyTorch built with MKL on Linux.");
#else
c10::MaybeOwned<Tensor> result_ = prepare_dense_vector_for_mkl(result);
c10::MaybeOwned<Tensor> vec_ = prepare_dense_vector_for_mkl(vec);
sparse_operation_t opA = SPARSE_OPERATION_NON_TRANSPOSE;
matrix_descr descrA;
descrA.type = SPARSE_MATRIX_TYPE_GENERAL;
AT_DISPATCH_FLOATING_AND_COMPLEX_TYPES(
result.scalar_type(), "addmv_out_sparse_csr_impl_mkl", [&] {
auto beta_ = beta.to<scalar_t>();
auto alpha_ = alpha.to<scalar_t>();
auto mkl_sparse_mat =
at::mkl::sparse::MklSparseCsrDescriptor<scalar_t>(mat);
at::mkl::sparse::mv<scalar_t>(
opA,
alpha_,
mkl_sparse_mat.descriptor(),
descrA,
vec_->data_ptr<scalar_t>(),
beta_,
result_->data_ptr<scalar_t>());
});
if (!result.is_same(*result_)) {
result.copy_(*result_);
}
#endif
}
void add_out_sparse_csr(
const Tensor& mat1,
const Tensor& mat2,
const Scalar& alpha,
const Tensor& result) {
#if !AT_USE_MKL_SPARSE()
TORCH_CHECK(
false,
"Calling add on a sparse CPU tensor requires Linux platform. ",
"Please use PyTorch built with MKL on Linux.");
#else
// MKL doesn't work with empty matrices
if (mat2._nnz() == 0) {
col_indices_and_values_resize_(result, mat1._nnz());
result.copy_(mat1);
return;
} else if (mat1._nnz() == 0) {
col_indices_and_values_resize_(result, mat2._nnz());
result.copy_(mat2);
result.values().mul_(alpha);
return;
}
// Modify `result` tensor in-place to swap indices tensors with 32-bit (or
// 64-bit) variants
indices_to_mkl_compatible_inplace(result);
sparse_operation_t opA = SPARSE_OPERATION_NON_TRANSPOSE;
AT_DISPATCH_FLOATING_AND_COMPLEX_TYPES(
result.scalar_type(), "add_out_sparse_csr_impl_mkl", [&] {
auto alpha_ = alpha.to<scalar_t>();
auto mkl_mat1 = at::mkl::sparse::MklSparseCsrDescriptor<scalar_t>(mat1);
auto mkl_mat2 = at::mkl::sparse::MklSparseCsrDescriptor<scalar_t>(mat2);
auto mkl_result = at::mkl::sparse::MklSparseCsrDescriptor<scalar_t>();
// Note that the order the order of mat1 and mat2 arguments is swapped
// because MKL computes alpha*mat1 + mat2 while PyTorch needs mat1 +
// alpha*mat2
auto result_desc = mkl_result.descriptor();
at::mkl::sparse::add<scalar_t>(
opA,
mkl_mat2.descriptor(),
alpha_,
mkl_mat1.descriptor(),
&result_desc);
// now copy data from `result_desc` to `result`
mkl_result_copy_<scalar_t>(result, result_desc);
});
#endif
}
void triangular_solve_out_sparse_csr(
const Tensor& A,
const Tensor& B,
const Tensor& X,
bool upper,
bool transpose,
bool unitriangular) {
#if !AT_USE_MKL_SPARSE()
TORCH_CHECK(
false,
"Calling triangular_solve on a sparse CPU tensor requires Linux platform. ",
"Please use PyTorch built with MKL on Linux.");
#else
if (B.numel() == 0 || X.numel() == 0 || A._nnz() == 0) {
return;
}
c10::MaybeOwned<Tensor> X_ = prepare_dense_matrix_for_mkl(X);
IntArrayRef X_strides = X_->strides();
auto ndim = X_->dim();
bool is_X_row_major = (ndim > 1) ? (X_strides[ndim - 1] == 1) : true;
// MKL requires same storage layout of matrices
c10::MaybeOwned<Tensor> B_ = prepare_dense_matrix_for_mkl(B, is_X_row_major);
sparse_operation_t opA = transpose ? SPARSE_OPERATION_TRANSPOSE : SPARSE_OPERATION_NON_TRANSPOSE;
matrix_descr descrA;
descrA.type = SPARSE_MATRIX_TYPE_TRIANGULAR;
descrA.mode = upper ? SPARSE_FILL_MODE_UPPER : SPARSE_FILL_MODE_LOWER;
descrA.diag = unitriangular ? SPARSE_DIAG_UNIT : SPARSE_DIAG_NON_UNIT;
AT_DISPATCH_FLOATING_AND_COMPLEX_TYPES(
X.scalar_type(), "triangular_solve_out_sparse_csr_impl_mkl", [&] {
auto mkl_sparse_mat =
at::mkl::sparse::MklSparseCsrDescriptor<scalar_t>(A);
scalar_t alpha = 1;
if (B.size(-1) == 1) {
at::mkl::sparse::trsv<scalar_t>(
opA,
alpha,
mkl_sparse_mat.descriptor(),
descrA,
B_->data_ptr<scalar_t>(),
X_->data_ptr<scalar_t>());
} else {
IntArrayRef B_strides = B_->strides();
bool is_B_row_major = (B_strides[ndim - 1] == 1);
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(!(is_X_row_major ^ is_B_row_major));
auto order = is_X_row_major ? SPARSE_LAYOUT_ROW_MAJOR : SPARSE_LAYOUT_COLUMN_MAJOR;
auto nrhs = mkl_int_cast(B.size(-1), "nrhs");
auto ldx = is_X_row_major ? X_strides[ndim - 2] : X_strides[ndim - 1];
auto ldb = is_B_row_major ? B_strides[ndim - 2] : B_strides[ndim - 1];
at::mkl::sparse::trsm<scalar_t>(
opA,
alpha,
mkl_sparse_mat.descriptor(),
descrA,
order,
B_->data_ptr<scalar_t>(),
nrhs,
ldb,
X_->data_ptr<scalar_t>(),
ldx);
}
});
if (!X.is_same(*X_)) {
X.copy_(*X_);
}
#endif
}
} // namespace mkl
} // namespace impl
} // namespace sparse
} // namespace native
} // namespace at