-
Notifications
You must be signed in to change notification settings - Fork 250
Expand file tree
/
Copy pathbatch_matmul_kernels.cu
More file actions
145 lines (138 loc) · 5.6 KB
/
batch_matmul_kernels.cu
File metadata and controls
145 lines (138 loc) · 5.6 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
/* Copyright 2023 CMU, Facebook, LANL, MIT, NVIDIA, and Stanford (alphabetical)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "internal/device.h"
#include "kernels/batch_matmul_kernels_gpu.h"
namespace FlexFlow {
namespace Kernels {
namespace BatchMatmul {
void gpu_forward_kernel(cudaStream_t stream,
PerDeviceFFHandle const &handle,
float *output_ptr,
float const *a_input_ptr,
float const *b_input_ptr,
int m,
int n,
int k,
int batch,
int a_seq_length_dim,
int b_seq_length_dim,
int seq_length) {
checkCUBLAS(cublasSetStream(handle.blas, stream));
checkCUDNN(cudnnSetStream(handle.dnn, stream));
int lda = k;
int ldb = m;
int ldo = m;
long long int strideA = (long long int)n * k;
long long int strideB = (long long int)k * m;
long long int strideO = (long long int)n * m;
if ((a_seq_length_dim == 0) && (seq_length >= 0)) {
assert(seq_length <= k);
k = seq_length;
assert(b_seq_length_dim == 1);
} else if ((a_seq_length_dim == 1) && (seq_length >= 0)) {
assert(seq_length <= n);
n = seq_length;
} else {
// currently only support a_seq_length_dim = 0 or 1
assert((a_seq_length_dim < 0) || (seq_length < 0));
}
if ((b_seq_length_dim == 0) && (seq_length >= 0)) {
assert(seq_length <= m);
m = seq_length;
} else if ((b_seq_length_dim == 1) && (seq_length >= 0)) {
assert(a_seq_length_dim == 0);
assert(k == seq_length);
} else {
// currently only support a_seq_length_dim = 0 or 1
assert((b_seq_length_dim < 0) || (seq_length < 0));
}
float alpha = 1.0f, beta = 0.0f;
checkCUBLAS(cublasSgemmStridedBatched(handle.blas,
CUBLAS_OP_N,
CUBLAS_OP_N,
m,
n,
k,
&alpha,
b_input_ptr,
ldb,
strideB,
a_input_ptr,
lda,
strideA,
&beta,
output_ptr,
ldo,
strideO,
batch));
}
void gpu_backward_kernel(cudaStream_t stream,
PerDeviceFFHandle const &handle,
float const *o_ptr,
float const *o_grad_ptr,
float const *a_ptr,
float *a_grad_ptr,
float const *b_ptr,
float *b_grad_ptr,
int m,
int n,
int k,
int batch) {
checkCUBLAS(cublasSetStream(handle.blas, stream));
checkCUDNN(cudnnSetStream(handle.dnn, stream));
int a_stride = n * k;
int b_stride = m * k;
int o_stride = n * m;
float alpha = 1.0f;
checkCUBLAS(cublasSgemmStridedBatched(handle.blas,
CUBLAS_OP_T,
CUBLAS_OP_N,
k,
n,
m,
&alpha,
b_ptr,
m,
b_stride,
o_grad_ptr,
m,
o_stride,
&alpha,
a_grad_ptr,
k,
a_stride,
batch));
checkCUBLAS(cublasSgemmStridedBatched(handle.blas,
CUBLAS_OP_N,
CUBLAS_OP_T,
m,
k,
n,
&alpha,
o_grad_ptr,
m,
o_stride,
a_ptr,
k,
a_stride,
&alpha,
b_grad_ptr,
m,
b_stride,
batch));
}
} // namespace BatchMatmul
} // namespace Kernels
} // namespace FlexFlow