-
Notifications
You must be signed in to change notification settings - Fork 555
Expand file tree
/
Copy pathblas.cpp
More file actions
405 lines (355 loc) · 15.7 KB
/
Copy pathblas.cpp
File metadata and controls
405 lines (355 loc) · 15.7 KB
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
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <blas.hpp>
#ifdef USE_MKL
#include <mkl_cblas.h>
#endif
#include <Array.hpp>
#include <Param.hpp>
#include <common/blas_headers.hpp>
#include <common/cast.hpp>
#include <common/complex.hpp>
#include <common/err_common.hpp>
#include <common/half.hpp>
#include <copy.hpp>
#include <kernel/dot.hpp>
#include <platform.hpp>
#include <types.hpp>
#include <af/defines.h>
#include <af/dim4.hpp>
#include <af/traits.hpp>
#include <algorithm>
#include <type_traits>
#include <vector>
using af::dtype_traits;
using arrayfire::common::cast;
using arrayfire::common::half;
using arrayfire::common::is_complex;
using std::conditional;
using std::vector;
namespace arrayfire {
namespace cpu {
// clang-format off
// Some implementations of BLAS require void* for complex pointers while others
// use float*/double*
//
// Sample cgemm API
// OpenBLAS
// void cblas_cgemm(OPENBLAS_CONST enum CBLAS_ORDER Order,
// OPENBLAS_CONST enum CBLAS_TRANSPOSE TransA,
// OPENBLAS_CONST enum CBLAS_TRANSPOSE TransB,
// OPENBLAS_CONST blasint M,
// OPENBLAS_CONST blasint N,
// OPENBLAS_CONST blasint K,
// OPENBLAS_CONST float *alpha, OPENBLAS_CONST float *A,
// OPENBLAS_CONST blasint lda,
// OPENBLAS_CONST float *B, OPENBLAS_CONST blasint ldb,
// OPENBLAS_CONST float *beta,
// float *C, OPENBLAS_CONST blasint ldc);
//
// MKL
// void cblas_cgemm(const CBLAS_LAYOUT Layout,
// const CBLAS_TRANSPOSE TransA, const CBLAS_TRANSPOSE TransB,
// const MKL_INT M, const MKL_INT N, const MKL_INT K,
// const void *alpha, const void *A, const MKL_INT lda,
// const void *B, const MKL_INT ldb, const void *beta,
// void *C, const MKL_INT ldc);
// void cblas_cgemm_batch(const CBLAS_LAYOUT Layout,
// const CBLAS_TRANSPOSE* TransA,
// const CBLAS_TRANSPOSE* TransB,
// const MKL_INT* M, const MKL_INT* N, const MKL_INT* K,
// const void *alpha, const void **A, const MKL_INT* lda,
// const void **B, const MKL_INT* ldb, const void *beta,
// void **C, const MKL_INT* ldc,
// const MKL_INT group_count, const MKL_INT* group_size);
//
// atlas cblas
// void cblas_cgemm(const enum CBLAS_ORDER Order,
// const enum CBLAS_TRANSPOSE TransA,
// const enum CBLAS_TRANSPOSE TransB,
// const int M, const int N, const int K,
// const void *alpha, const void *A, const int lda,
// const void *B, const int ldb, const void *beta,
// void *C, const int ldc);
//
// LAPACKE
// void cblas_cgemm(const enum CBLAS_ORDER Order,
// const enum CBLAS_TRANSPOSE TransA,
// const enum CBLAS_TRANSPOSE TransB,
// const int M, const int N, const int K,
// const void *alpha, const void *A, const int lda,
// const void *B, const int ldb, const void *beta,
// void *C, const int ldc);
// clang-format on
template<typename T>
struct blas_base {
using type =
typename conditional<is_complex<T>::value && cplx_void_ptr, void,
typename dtype_traits<T>::base_type>::type;
};
template<typename T>
using cptr_type =
typename conditional<is_complex<T>::value,
const typename blas_base<T>::type *, const T *>::type;
template<typename T>
using ptr_type = typename conditional<is_complex<T>::value,
typename blas_base<T>::type *, T *>::type;
template<typename T, bool batched = false>
class scale_type {
const T val;
public:
explicit scale_type(const T *val_ptr) : val(*val_ptr) {}
using api_type = const typename conditional<
is_complex<T>::value, const typename blas_base<T>::type *,
const typename conditional<batched, const T *, const T>::type>::type;
api_type getScale() const { // NOLINT(readability-const-return-type)
return val;
}
};
#define INSTANTIATE_BATCHED(TYPE) \
template<> \
typename scale_type<TYPE, true>::api_type \
scale_type<TYPE, true>::getScale() const { \
return &val; \
}
INSTANTIATE_BATCHED(float); // NOLINT(readability-const-return-type)
INSTANTIATE_BATCHED(double); // NOLINT(readability-const-return-type)
#undef INSTANTIATE_BATCHED
#define INSTANTIATE_COMPLEX(TYPE, BATCHED) \
template<> \
scale_type<TYPE, BATCHED>::api_type scale_type<TYPE, BATCHED>::getScale() \
const { \
return reinterpret_cast<const blas_base<TYPE>::type *const>(&val); \
}
INSTANTIATE_COMPLEX(cfloat, true); // NOLINT(readability-const-return-type)
INSTANTIATE_COMPLEX(cfloat, false); // NOLINT(readability-const-return-type)
INSTANTIATE_COMPLEX(cdouble, true); // NOLINT(readability-const-return-type)
INSTANTIATE_COMPLEX(cdouble, false); // NOLINT(readability-const-return-type)
#undef INSTANTIATE_COMPLEX
template<typename T>
using gemm_func_def = void (*)(const CBLAS_ORDER, const CBLAS_TRANSPOSE,
const CBLAS_TRANSPOSE, const blasint,
const blasint, const blasint,
typename scale_type<T>::api_type, cptr_type<T>,
const blasint, cptr_type<T>, const blasint,
typename scale_type<T>::api_type, ptr_type<T>,
const blasint);
template<typename T>
using gemv_func_def = void (*)(const CBLAS_ORDER, const CBLAS_TRANSPOSE,
const blasint, const blasint,
typename scale_type<T>::api_type, cptr_type<T>,
const blasint, cptr_type<T>, const blasint,
typename scale_type<T>::api_type, ptr_type<T>,
const blasint);
#ifdef USE_MKL
template<typename T>
using gemm_batch_func_def = void (*)(
const CBLAS_LAYOUT, const CBLAS_TRANSPOSE *, const CBLAS_TRANSPOSE *,
const MKL_INT *, const MKL_INT *, const MKL_INT *,
typename scale_type<T, true>::api_type, cptr_type<T> *, const MKL_INT *,
cptr_type<T> *, const MKL_INT *, typename scale_type<T, true>::api_type,
ptr_type<T> *, const MKL_INT *, const MKL_INT, const MKL_INT *);
#endif
#define BLAS_FUNC_DEF(FUNC) \
template<typename T> \
FUNC##_func_def<T> FUNC##_func();
#define BLAS_FUNC(FUNC, TYPE, PREFIX) \
template<> \
FUNC##_func_def<TYPE> FUNC##_func<TYPE>() { \
return (FUNC##_func_def<TYPE>)&cblas_##PREFIX##FUNC; \
}
BLAS_FUNC_DEF(gemm)
BLAS_FUNC(gemm, float, s)
BLAS_FUNC(gemm, double, d)
BLAS_FUNC(gemm, cfloat, c)
BLAS_FUNC(gemm, cdouble, z)
BLAS_FUNC_DEF(gemv)
BLAS_FUNC(gemv, float, s)
BLAS_FUNC(gemv, double, d)
BLAS_FUNC(gemv, cfloat, c)
BLAS_FUNC(gemv, cdouble, z)
#ifdef USE_MKL
BLAS_FUNC_DEF(gemm_batch)
BLAS_FUNC(gemm_batch, float, s)
BLAS_FUNC(gemm_batch, double, d)
BLAS_FUNC(gemm_batch, cfloat, c)
BLAS_FUNC(gemm_batch, cdouble, z)
#endif
CBLAS_TRANSPOSE
toCblasTranspose(af_mat_prop opt) {
CBLAS_TRANSPOSE out = CblasNoTrans;
switch (opt) {
case AF_MAT_NONE: out = CblasNoTrans; break;
case AF_MAT_TRANS: out = CblasTrans; break;
case AF_MAT_CTRANS: out = CblasConjTrans; break;
default: AF_ERROR("INVALID af_mat_prop", AF_ERR_ARG);
}
return out;
}
template<typename Ti, typename To>
void gemm(Array<To> &out, af_mat_prop optLhs, af_mat_prop optRhs,
const To *alpha, const Array<Ti> &lhs, const Array<Ti> &rhs,
const To *beta) {
const CBLAS_TRANSPOSE lOpts = toCblasTranspose(optLhs);
const CBLAS_TRANSPOSE rOpts = toCblasTranspose(optRhs);
const int aRowDim = (lOpts == CblasNoTrans) ? 0 : 1;
const int aColDim = (lOpts == CblasNoTrans) ? 1 : 0;
const int bColDim = (rOpts == CblasNoTrans) ? 1 : 0;
const dim4 &lDims = lhs.dims();
const dim4 &rDims = rhs.dims();
const int M = lDims[aRowDim];
const int N = rDims[bColDim];
const int K = lDims[aColDim];
const dim4 oDims = out.dims();
using BT = typename blas_base<Ti>::type;
using CBT = const typename blas_base<Ti>::type;
auto alpha_ = scale_type<Ti, false>(alpha);
auto beta_ = scale_type<Ti, false>(beta);
#ifdef USE_MKL
auto alpha_batched = scale_type<Ti, true>(alpha);
auto beta_batched = scale_type<Ti, true>(beta);
#endif
auto func = [=](Param<Ti> output, CParam<Ti> left, CParam<Ti> right) {
dim4 lStrides = left.strides();
dim4 rStrides = right.strides();
dim4 oStrides = output.strides();
if (output.dims().ndims() <= 2) {
if (right.dims()[bColDim] == 1) {
dim_t incr =
(optRhs == AF_MAT_NONE) ? rStrides[0] : rStrides[1];
gemv_func<Ti>()(
CblasColMajor, lOpts, lDims[0], lDims[1], alpha_.getScale(),
reinterpret_cast<CBT *>(left.get()), lStrides[1],
reinterpret_cast<CBT *>(right.get()), incr,
beta_.getScale(), reinterpret_cast<BT *>(output.get()),
oStrides[0]);
} else {
gemm_func<Ti>()(
CblasColMajor, lOpts, rOpts, M, N, K, alpha_.getScale(),
reinterpret_cast<CBT *>(left.get()), lStrides[1],
reinterpret_cast<CBT *>(right.get()), rStrides[1],
beta_.getScale(), reinterpret_cast<BT *>(output.get()),
oStrides[1]);
}
} else {
int batchSize = static_cast<int>(oDims[2] * oDims[3]);
const bool is_l_d2_batched = oDims[2] == lDims[2];
const bool is_l_d3_batched = oDims[3] == lDims[3];
const bool is_r_d2_batched = oDims[2] == rDims[2];
const bool is_r_d3_batched = oDims[3] == rDims[3];
vector<CBT *> lptrs(batchSize);
vector<CBT *> rptrs(batchSize);
vector<BT *> optrs(batchSize);
for (int n = 0; n < batchSize; n++) {
ptrdiff_t w = n / oDims[2];
ptrdiff_t z = n - w * oDims[2];
ptrdiff_t loff = z * (is_l_d2_batched * lStrides[2]) +
w * (is_l_d3_batched * lStrides[3]);
ptrdiff_t roff = z * (is_r_d2_batched * rStrides[2]) +
w * (is_r_d3_batched * rStrides[3]);
lptrs[n] = reinterpret_cast<CBT *>(left.get() + loff);
rptrs[n] = reinterpret_cast<CBT *>(right.get() + roff);
optrs[n] = reinterpret_cast<BT *>(
output.get() + z * oStrides[2] + w * oStrides[3]);
}
#ifdef USE_MKL
// MKL can handle multiple groups of batches
// However, for ArrayFire's use case, the group_count=1
const MKL_INT lda = lStrides[1];
const MKL_INT ldb = rStrides[1];
const MKL_INT ldc = oStrides[1];
gemm_batch_func<Ti>()(CblasColMajor, &lOpts, &rOpts, &M, &N, &K,
alpha_batched.getScale(), lptrs.data(), &lda,
rptrs.data(), &ldb, beta_batched.getScale(),
optrs.data(), &ldc, 1, &batchSize);
#else
for (int n = 0; n < batchSize; n++) {
if (rDims[bColDim] == 1) {
dim_t incr =
(optRhs == AF_MAT_NONE) ? rStrides[0] : rStrides[1];
gemv_func<Ti>()(CblasColMajor, lOpts, lDims[0], lDims[1],
alpha_.getScale(), lptrs[n], lStrides[1],
rptrs[n], incr, beta_.getScale(), optrs[n],
oStrides[0]);
} else {
gemm_func<Ti>()(CblasColMajor, lOpts, rOpts, M, N, K,
alpha_.getScale(), lptrs[n], lStrides[1],
rptrs[n], rStrides[1], beta_.getScale(),
optrs[n], oStrides[1]);
}
}
#endif
}
};
getQueue().enqueue(func, out, lhs, rhs);
}
template<>
void gemm<half>(Array<half> &out, af_mat_prop optLhs, af_mat_prop optRhs,
const half *alpha, const Array<half> &lhs,
const Array<half> &rhs, const half *beta) {
Array<float> outArr = createValueArray<float>(out.dims(), 0);
const auto float_alpha = static_cast<float>(*alpha);
const auto float_beta = static_cast<float>(*beta);
gemm<float>(outArr, optLhs, optRhs, &float_alpha, cast<float>(lhs),
cast<float>(rhs), &float_beta);
copyArray(out, outArr);
}
template<>
void gemm<schar, float>(Array<float> &out, af_mat_prop optLhs,
af_mat_prop optRhs, const float *alpha,
const Array<schar> &lhs, const Array<schar> &rhs,
const float *beta) {
TYPE_ERROR(3, af_dtype::s8);
}
template<typename T>
Array<T> dot(const Array<T> &lhs, const Array<T> &rhs, af_mat_prop optLhs,
af_mat_prop optRhs) {
Array<T> out = createEmptyArray<T>(af::dim4(1));
if (optLhs == AF_MAT_CONJ && optRhs == AF_MAT_CONJ) {
getQueue().enqueue(kernel::dot<T, false, true>, out, lhs, rhs, optLhs,
optRhs);
} else if (optLhs == AF_MAT_CONJ && optRhs == AF_MAT_NONE) {
getQueue().enqueue(kernel::dot<T, true, false>, out, lhs, rhs, optLhs,
optRhs);
} else if (optLhs == AF_MAT_NONE && optRhs == AF_MAT_CONJ) {
getQueue().enqueue(kernel::dot<T, true, false>, out, rhs, lhs, optRhs,
optLhs);
} else {
getQueue().enqueue(kernel::dot<T, false, false>, out, lhs, rhs, optLhs,
optRhs);
}
return out;
}
template<>
Array<half> dot<half>(const Array<half> &lhs, const Array<half> &rhs,
af_mat_prop optLhs, af_mat_prop optRhs) {
Array<float> out = dot(cast<float>(lhs), cast<float>(rhs), optLhs, optRhs);
return cast<half>(out);
}
#undef BT
#undef REINTEPRET_CAST
#define INSTANTIATE_GEMM(TYPE) \
template void gemm<TYPE>(Array<TYPE> & out, af_mat_prop optLhs, \
af_mat_prop optRhs, const TYPE *alphas, \
const Array<TYPE> &lhs, const Array<TYPE> &rhs, \
const TYPE *beta)
INSTANTIATE_GEMM(float);
INSTANTIATE_GEMM(cfloat);
INSTANTIATE_GEMM(double);
INSTANTIATE_GEMM(cdouble);
#define INSTANTIATE_DOT(TYPE) \
template Array<TYPE> dot<TYPE>(const Array<TYPE> &lhs, \
const Array<TYPE> &rhs, af_mat_prop optLhs, \
af_mat_prop optRhs)
INSTANTIATE_DOT(float);
INSTANTIATE_DOT(double);
INSTANTIATE_DOT(cfloat);
INSTANTIATE_DOT(cdouble);
} // namespace cpu
} // namespace arrayfire