-
Notifications
You must be signed in to change notification settings - Fork 548
Expand file tree
/
Copy pathblas.cu
More file actions
389 lines (334 loc) · 14.4 KB
/
blas.cu
File metadata and controls
389 lines (334 loc) · 14.4 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
/*******************************************************
* 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>
#include <arith.hpp>
#include <common/cast.hpp>
#include <common/err_common.hpp>
#include <common/half.hpp>
#include <complex.hpp>
#include <copy.hpp>
#include <cublas.hpp>
#include <cublas_v2.h>
#include <cudaDataType.hpp>
#include <cuda_runtime.h>
#include <err_cuda.hpp>
#include <math.hpp>
#include <platform.hpp>
#include <reduce.hpp>
#include <tile.hpp>
#include <transpose.hpp>
#include <types.hpp>
#include <cassert>
#include <functional>
#include <stdexcept>
#include <string>
#include <vector>
using arrayfire::common::half;
using arrayfire::common::kernel_type;
using std::is_same;
using std::vector;
namespace arrayfire {
namespace cuda {
cublasOperation_t toCblasTranspose(af_mat_prop opt) {
cublasOperation_t out = CUBLAS_OP_N;
switch (opt) {
case AF_MAT_NONE: out = CUBLAS_OP_N; break;
case AF_MAT_TRANS: out = CUBLAS_OP_T; break;
case AF_MAT_CTRANS: out = CUBLAS_OP_C; break;
default: AF_ERROR("INVALID af_mat_prop", AF_ERR_ARG);
}
return out;
}
template<typename T>
using gemm_func_def = std::function<cublasStatus_t(
cublasHandle_t, cublasOperation_t, cublasOperation_t, int, int, int,
const T *, const T *, int, const T *, int, const T *, T *, int)>;
template<typename T>
using gemmBatched_func_def = std::function<cublasStatus_t(
cublasHandle_t, cublasOperation_t, cublasOperation_t, int, int, int,
const T *, const T **, int, const T **, int, const T *, T **, int, int)>;
template<typename T>
using trsm_func_def = std::function<cublasStatus_t(
cublasHandle_t, cublasSideMode_t, cublasFillMode_t, cublasOperation_t,
cublasDiagType_t, int, int, const T *, const T *, int, T *, int)>;
#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 &cublas##PREFIX##FUNC; \
}
BLAS_FUNC_DEF(gemm)
BLAS_FUNC(gemm, float, S)
BLAS_FUNC(gemm, cfloat, C)
BLAS_FUNC(gemm, double, D)
BLAS_FUNC(gemm, cdouble, Z)
BLAS_FUNC(gemm, __half, H)
BLAS_FUNC_DEF(gemmBatched)
BLAS_FUNC(gemmBatched, float, S)
BLAS_FUNC(gemmBatched, cfloat, C)
BLAS_FUNC(gemmBatched, double, D)
BLAS_FUNC(gemmBatched, cdouble, Z)
BLAS_FUNC(gemmBatched, __half, H)
template<>
gemm_func_def<schar> gemm_func<schar>() {
TYPE_ERROR(3, af_dtype::s8);
return gemm_func_def<schar>();
}
template<>
gemmBatched_func_def<schar> gemmBatched_func<schar>() {
TYPE_ERROR(3, af_dtype::s8);
return gemmBatched_func_def<schar>();
}
BLAS_FUNC_DEF(trsm)
BLAS_FUNC(trsm, float, S)
BLAS_FUNC(trsm, cfloat, C)
BLAS_FUNC(trsm, double, D)
BLAS_FUNC(trsm, cdouble, Z)
#undef BLAS_FUNC
#undef BLAS_FUNC_DEF
template<typename T, bool conjugate>
struct dot_func_def_t {
typedef cublasStatus_t (*dot_func_def)(cublasHandle_t, int, const T *, int,
const T *, int, T *);
};
#define BLAS_FUNC_DEF(FUNC) \
template<typename T, bool conjugate> \
typename FUNC##_func_def_t<T, conjugate>::FUNC##_func_def FUNC##_func();
#define BLAS_FUNC(FUNC, TYPE, CONJUGATE, PREFIX) \
template<> \
typename FUNC##_func_def_t<TYPE, CONJUGATE>::FUNC##_func_def \
FUNC##_func<TYPE, CONJUGATE>() { \
return (FUNC##_func_def_t<TYPE, CONJUGATE>::FUNC##_func_def) & \
cublas##PREFIX##FUNC; \
}
BLAS_FUNC_DEF(dot)
BLAS_FUNC(dot, float, true, S)
BLAS_FUNC(dot, double, true, D)
BLAS_FUNC(dot, float, false, S)
BLAS_FUNC(dot, double, false, D)
#undef BLAS_FUNC
#define BLAS_FUNC(FUNC, TYPE, CONJUGATE, PREFIX, SUFFIX) \
template<> \
typename FUNC##_func_def_t<TYPE, CONJUGATE>::FUNC##_func_def \
FUNC##_func<TYPE, CONJUGATE>() { \
return (FUNC##_func_def_t<TYPE, CONJUGATE>::FUNC##_func_def) & \
cublas##PREFIX##FUNC##SUFFIX; \
}
BLAS_FUNC_DEF(dot)
BLAS_FUNC(dot, cfloat, true, C, c)
BLAS_FUNC(dot, cdouble, true, Z, c)
BLAS_FUNC(dot, cfloat, false, C, u)
BLAS_FUNC(dot, cdouble, false, Z, u)
#undef BLAS_FUNC
#undef BLAS_FUNC_DEF
template<typename T>
cublasGemmAlgo_t selectGEMMAlgorithm() {
return CUBLAS_GEMM_DEFAULT;
}
template<>
cublasGemmAlgo_t selectGEMMAlgorithm<common::half>() {
auto dev = getDeviceProp(getActiveDeviceId());
cublasGemmAlgo_t algo = CUBLAS_GEMM_DEFAULT;
if (dev.major >= 7) { algo = CUBLAS_GEMM_DEFAULT_TENSOR_OP; }
return algo;
}
template<>
cublasGemmAlgo_t selectGEMMAlgorithm<__half>() {
return selectGEMMAlgorithm<common::half>();
}
template<typename Ti, typename To = Ti>
cublasStatus_t gemmDispatch(BlasHandle handle, cublasOperation_t lOpts,
cublasOperation_t rOpts, int M, int N, int K,
const To *alpha, const Array<Ti> &lhs, dim_t lStride,
const Array<Ti> &rhs, dim_t rStride, const To *beta,
Array<To> &out, dim_t oleading) {
auto prop = getDeviceProp(getActiveDeviceId());
#if __CUDACC_VER_MAJOR__ >= 10
if (prop.major > 3 && __CUDACC_VER_MAJOR__ >= 10) {
return cublasGemmEx(
blasHandle(), lOpts, rOpts, M, N, K, alpha, lhs.get(), getType<Ti>(),
lStride, rhs.get(), getType<Ti>(), rStride, beta, out.get(),
getType<To>(), out.strides()[1],
getComputeType<To>(), // Compute type
// NOTE: When using the CUBLAS_GEMM_DEFAULT_TENSOR_OP algorithm
// for the cublasGemm*Ex functions, the performance of the
// fp32 numbers seem to increase dramatically. Their numerical
// accuracy is also different compared to regular gemm fuctions.
// The CUBLAS_GEMM_DEFAULT algorithm selection does not experience
// this change. Does this imply that the TENSOR_OP function
// performs the computation in fp16 bit even when the compute
// type is CUDA_R_32F?
selectGEMMAlgorithm<Ti>());
} else {
#endif
using Nt = typename common::kernel_type<Ti>::native;
return gemm_func<Nt>()(blasHandle(), lOpts, rOpts, M, N, K, (Nt *)alpha,
(Nt *)lhs.get(), lStride, (Nt *)rhs.get(),
rStride, (Nt *)beta, (Nt *)out.get(), oleading);
#if __CUDACC_VER_MAJOR__ >= 10
}
#endif
}
template<typename Ti, typename To = Ti>
cublasStatus_t gemmBatchedDispatch(BlasHandle handle, cublasOperation_t lOpts,
cublasOperation_t rOpts, int M, int N, int K,
const To *alpha, const Ti **lptrs,
int lStrides, const Ti **rptrs, int rStrides,
const To *beta, To **optrs, int oStrides,
int batchSize) {
auto prop = getDeviceProp(getActiveDeviceId());
#if __CUDACC_VER_MAJOR__ >= 10
if (prop.major > 3) {
return cublasGemmBatchedEx(
blasHandle(), lOpts, rOpts, M, N, K, alpha, (const void **)lptrs,
getType<Ti>(), lStrides, (const void **)rptrs, getType<Ti>(),
rStrides, beta, (void **)optrs, getType<Ti>(), oStrides, batchSize,
getComputeType<Ti>(), // compute type
// NOTE: When using the CUBLAS_GEMM_DEFAULT_TENSOR_OP algorithm
// for the cublasGemm*Ex functions, the performance of the
// fp32 numbers seem to increase dramatically. Their numerical
// accuracy is also different compared to regular gemm fuctions.
// The CUBLAS_GEMM_DEFAULT algorithm selection does not experience
// this change. Does this imply that the TENSOR_OP function
// performs the computation in fp16 bit even when the compute
// type is CUDA_R_32F?
selectGEMMAlgorithm<Ti>());
} else {
#endif
using Nt = typename common::kernel_type<Ti>::native;
return gemmBatched_func<Nt>()(
blasHandle(), lOpts, rOpts, M, N, K, (const Nt *)alpha,
(const Nt **)lptrs, lStrides, (const Nt **)rptrs, rStrides,
(const Nt *)beta, (Nt **)optrs, oStrides, batchSize);
#if __CUDACC_VER_MAJOR__ >= 10
}
#endif
}
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 cublasOperation_t lOpts = toCblasTranspose(optLhs);
const cublasOperation_t rOpts = toCblasTranspose(optRhs);
const int aRowDim = (lOpts == CUBLAS_OP_N) ? 0 : 1;
const int aColDim = (lOpts == CUBLAS_OP_N) ? 1 : 0;
const int bColDim = (rOpts == CUBLAS_OP_N) ? 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();
dim4 lStrides = lhs.strides();
dim4 rStrides = rhs.strides();
dim4 oStrides = out.strides();
if (oDims.ndims() <= 2) {
CUBLAS_CHECK((gemmDispatch<Ti, To>(blasHandle(), lOpts, rOpts, M, N, K, alpha,
lhs, lStrides[1], rhs, rStrides[1], beta,
out, oStrides[1])));
} else {
int batchSize = oDims[2] * oDims[3];
vector<const Ti *> lptrs(batchSize);
vector<const Ti *> rptrs(batchSize);
vector<To *> optrs(batchSize);
bool is_l_d2_batched = oDims[2] == lDims[2];
bool is_l_d3_batched = oDims[3] == lDims[3];
bool is_r_d2_batched = oDims[2] == rDims[2];
bool is_r_d3_batched = oDims[3] == rDims[3];
const Ti *lptr = lhs.get();
const Ti *rptr = rhs.get();
To *optr = out.get();
for (int n = 0; n < batchSize; n++) {
int w = n / oDims[2];
int z = n - w * oDims[2];
int loff = z * (is_l_d2_batched * lStrides[2]) +
w * (is_l_d3_batched * lStrides[3]);
int roff = z * (is_r_d2_batched * rStrides[2]) +
w * (is_r_d3_batched * rStrides[3]);
lptrs[n] = lptr + loff;
rptrs[n] = rptr + roff;
optrs[n] = optr + z * oStrides[2] + w * oStrides[3];
}
size_t bytes = batchSize * sizeof(Ti **);
auto d_lptrs = memAlloc<uchar>(bytes);
auto d_rptrs = memAlloc<uchar>(bytes);
auto d_optrs = memAlloc<uchar>(bytes);
CUDA_CHECK(cudaMemcpyAsync(d_lptrs.get(), lptrs.data(), bytes,
cudaMemcpyHostToDevice, getActiveStream()));
CUDA_CHECK(cudaMemcpyAsync(d_rptrs.get(), rptrs.data(), bytes,
cudaMemcpyHostToDevice, getActiveStream()));
CUDA_CHECK(cudaMemcpyAsync(d_optrs.get(), optrs.data(), bytes,
cudaMemcpyHostToDevice, getActiveStream()));
// Call this before the gemm call so that you don't have to wait for the
// computation. Even though it would make more sense to put it
// afterwards
CUDA_CHECK(cudaStreamSynchronize(getActiveStream()));
using Nt = typename common::kernel_type<Ti>::native;
CUBLAS_CHECK(gemmBatchedDispatch(
blasHandle(), lOpts, rOpts, M, N, K, alpha,
(const Ti **)d_lptrs.get(), lStrides[1], (const Ti **)d_rptrs.get(),
rStrides[1], beta, (To **)d_optrs.get(), oStrides[1], batchSize));
}
}
template<typename T>
Array<T> dot(const Array<T> &lhs, const Array<T> &rhs, af_mat_prop optLhs,
af_mat_prop optRhs) {
auto lhs_ = (optLhs == AF_MAT_NONE ? lhs : conj<T>(lhs));
auto rhs_ = (optRhs == AF_MAT_NONE ? rhs : conj<T>(rhs));
auto temp = arithOp<T, af_mul_t>(lhs_, rhs_, lhs_.dims());
return reduce<af_add_t, T, T>(temp, 0, false, 0);
}
template<typename T>
void trsm(const Array<T> &lhs, Array<T> &rhs, af_mat_prop trans, bool is_upper,
bool is_left, bool is_unit) {
// dim4 lDims = lhs.dims();
dim4 rDims = rhs.dims();
int M = rDims[0];
int N = rDims[1];
T alpha = scalar<T>(1);
dim4 lStrides = lhs.strides();
dim4 rStrides = rhs.strides();
CUBLAS_CHECK(trsm_func<T>()(
blasHandle(), is_left ? CUBLAS_SIDE_LEFT : CUBLAS_SIDE_RIGHT,
is_upper ? CUBLAS_FILL_MODE_UPPER : CUBLAS_FILL_MODE_LOWER,
toCblasTranspose(trans),
is_unit ? CUBLAS_DIAG_UNIT : CUBLAS_DIAG_NON_UNIT, M, N, &alpha,
lhs.get(), lStrides[1], rhs.get(), rStrides[1]));
}
#define INSTANTIATE_GEMM(TYPE, OUTTYPE) \
template void gemm<TYPE>(Array<OUTTYPE> & out, af_mat_prop optLhs, \
af_mat_prop optRhs, const OUTTYPE *alpha, \
const Array<TYPE> &lhs, const Array<TYPE> &rhs, \
const OUTTYPE *beta);
INSTANTIATE_GEMM(float, float)
INSTANTIATE_GEMM(cfloat, cfloat)
INSTANTIATE_GEMM(double, double)
INSTANTIATE_GEMM(cdouble, cdouble)
INSTANTIATE_GEMM(half, half)
INSTANTIATE_GEMM(schar, float)
#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)
INSTANTIATE_DOT(half)
#define INSTANTIATE_TRSM(TYPE) \
template void trsm<TYPE>(const Array<TYPE> &lhs, Array<TYPE> &rhs, \
af_mat_prop trans, bool is_upper, bool is_left, \
bool is_unit);
INSTANTIATE_TRSM(float)
INSTANTIATE_TRSM(cfloat)
INSTANTIATE_TRSM(double)
INSTANTIATE_TRSM(cdouble)
} // namespace cuda
} // namespace arrayfire