forked from tspeterkim/flash-attention-minimal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
233 lines (213 loc) · 8.2 KB
/
main.cpp
File metadata and controls
233 lines (213 loc) · 8.2 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
#include "flash/launch.hpp"
#include <chrono>
#include <torch/torch.h>
struct AttentionParameters {
int batch_size;
int num_heads;
int seq_len;
int head_embd;
};
torch::Tensor manual_attn(torch::Tensor q, torch::Tensor k, torch::Tensor v) {
auto att = (q.matmul(k.transpose(-2, -1)) * (1.0 / sqrt(k.size(-1))));
att = torch::nn::functional::softmax(att, -1);
auto y = att.matmul(v);
return y;
}
torch::Tensor torch_attn2(torch::Tensor q, torch::Tensor k, torch::Tensor v) {
// return torch::nn::functional::scaled_dot_product_attention(q, k, v);
return torch::scaled_dot_product_attention(q, k, v);
}
auto generate_data(auto const &p) {
torch::manual_seed(0);
std::cout << "Generate sample: " << "Batch " << p.batch_size
<< " Num Heads " << p.num_heads << " Seq len " << p.seq_len
<< " Head embedding " << p.head_embd
<< std::endl;
auto q = torch::randn({p.batch_size, p.num_heads, p.seq_len, p.head_embd}).cuda();
auto k = torch::randn({p.batch_size, p.num_heads, p.seq_len, p.head_embd}).cuda();
auto v = torch::randn({p.batch_size, p.num_heads, p.seq_len, p.head_embd}).cuda();
return std::make_tuple(q, k, v);
}
bool run_and_compare(auto test_name, auto reference, double atol, double rtol,
auto &&kernel) {
auto result = kernel();
bool test_result = torch::allclose(result, reference, atol, rtol);
if (!test_result) {
std::cout << test_name << ": Test failed [☓ ]" << std::endl;
int mismatch_count = 0;
for (int b = 0; b < result.size(0); b++) {
for (int h = 0; h < result.size(1); h++) {
for (int n = 0; n < result.size(2); n++) {
for (int d = 0; d < result.size(3); d++) {
auto ref = reference[b][h][n][d].template item<float>();
auto res = result[b][h][n][d].template item<float>();
if (std::fabs(res - ref) > atol + rtol * std::fabs(ref) ||
std::isnan(res)) {
std::cout << "Mismatch[" << b << "," << h << "," << n << "," << d
<< "] = "
<< "ref = " << ref << ", res = " << res << std::endl;
if (mismatch_count++ > 5)
return false;
}
}
}
}
}
} else {
std::cout << test_name << ": Test passed [✓ ]" << std::endl;
}
return test_result;
}
void test_alg(AttentionParameters const ¶ms) {
std::cout << "======= Running test ("
<< "batch = " << params.batch_size << ", "
<< "heads = " << params.num_heads << ", "
<< "seq_len = " << params.seq_len << ", "
<< "embedding dim = " << params.head_embd
<< ") =======" << std::endl;
auto [q, k, v] = generate_data(params);
auto manual_result = manual_attn(q, k, v);
// std::cout << "Reference: " << manual_result << std::endl;
double atol = 1e-4;
double rtol = 1e-2;
bool ret = true;
ret &= run_and_compare("Naive", manual_result, atol, rtol, [&] {
return flash::forward(q, k, v, flash::KernelType::naive1D);
});
ret &= run_and_compare("Scalar 2D block", manual_result, atol, rtol, [&] {
return flash::forward(q, k, v, flash::KernelType::scalar2D);
});
ret &= run_and_compare("Scalar 2D row tile", manual_result, atol, rtol, [&] {
return flash::forward(q, k, v, flash::KernelType::scalar2D_row_tile);
});
ret &= run_and_compare("Single-warp wmma", manual_result, atol, rtol, [&] {
return flash::forward(q, k, v, flash::KernelType::warp_wmma);
});
if (params.head_embd < 128) { // it fails but whatever, not gonna debug slow code
ret &= run_and_compare("Block wmma", manual_result, atol, rtol, [&] {
return flash::forward(q, k, v, flash::KernelType::block_wmma);
});
}
if (params.head_embd < 128) { // it fails but whatever, not gonna debug slow code
ret &= run_and_compare("wmma row-block", manual_result, atol, rtol, [&] {
return flash::forward(q, k, v, flash::KernelType::wmma_row_block);
});
}
ret &= run_and_compare("mma", manual_result, atol, rtol, [&] {
return flash::forward(q, k, v, flash::KernelType::mma);
});
ret &= run_and_compare("mma swizzle", manual_result, atol, rtol, [&] {
return flash::forward(q, k, v, flash::KernelType::mma_swizzle);
});
ret &= run_and_compare("Block wmma async", manual_result, atol, rtol, [&] {
return flash::forward(q, k, v, flash::KernelType::block_wmma_async);
});
if (params.head_embd % 2 == 0) { // otherwise misaligned address
ret &= run_and_compare("mma qreg", manual_result, atol, rtol, [&] {
return flash::forward(q, k, v, flash::KernelType::mma_qreg);
});
}
if (params.head_embd % 4 == 0) { // otherwise misaligned address
ret &= run_and_compare("mma qreg vload", manual_result, atol, rtol, [&] {
return flash::forward(q, k, v, flash::KernelType::mma_qreg_f32x4load);
});
ret &= run_and_compare("mma qreg async", manual_result, atol, rtol, [&] {
return flash::forward(q, k, v, flash::KernelType::mma_qreg_async);
});
}
if (!ret) {
std::cout << "Test failed!" << std::endl;
exit(EXIT_FAILURE);
}
}
template <typename KernelType>
auto time_kernel(torch::Tensor const & q, torch::Tensor const & k, torch::Tensor const & v,
std::string const & kernel_name, KernelType && kernel)
{
using namespace std::chrono;
auto const start_time = high_resolution_clock::now();
kernel(q,k,v);
auto const end_time = high_resolution_clock::now();
auto const duration = (duration_cast<milliseconds>(end_time - start_time)).count();
std::cout << "Benchmark \'" << kernel_name << "\'"
<< " took " << (double)duration << " [ms]" << std::endl;}
auto time_kernel(torch::Tensor const & q, torch::Tensor const & k, torch::Tensor const & v,
flash::KernelType kernelType) {
auto func = [kernelType](auto const &q, auto const &k, auto const &v) {
return flash::forward(q, k, v, kernelType);
};
time_kernel(q, k, v, to_string(kernelType), func);
}
auto main(int argc, char *argv[]) -> int {
if (argc == 1) { // test only
// small aliged
test_alg(AttentionParameters{
.batch_size = 1,
.num_heads = 1,
.seq_len = 50,
.head_embd = 32,
});
// unaligned seq len
test_alg(AttentionParameters{
.batch_size = 1,
.num_heads = 1,
.seq_len = 100, // larger than tile size
.head_embd = 32,
});
// unaligned head embedding dim
test_alg(AttentionParameters{
.batch_size = 1,
.num_heads = 1,
.seq_len = 64,
.head_embd = 50,
});
// Bigger test with everything unaligned
test_alg(AttentionParameters{
.batch_size = 5,
.num_heads = 12,
.seq_len = 53,
.head_embd = 69,
});
// some kernels launch more warps for head_emb > 64
test_alg(AttentionParameters{
.batch_size = 1,
.num_heads = 1,
.seq_len = 30,
.head_embd = 128,
});
}
else { // profile or benchmark
bool ncu = argv[1] == std::string("ncu");
auto [q, k, v] = generate_data(AttentionParameters{
// gpt3
.batch_size = 4,
.num_heads = 96,
.seq_len = 2048,
.head_embd = 128,
// gpt2
// .batch_size = 8,
// .num_heads = 12,
// .seq_len = 1024,
// .head_embd = 64,
});
if (!ncu) {
time_kernel(q, k, v, flash::KernelType::naive1D);
time_kernel(q, k, v, flash::KernelType::scalar2D);
time_kernel(q, k, v, flash::KernelType::scalar2D_row_tile);
time_kernel(q, k, v, flash::KernelType::warp_wmma);
time_kernel(q, k, v, flash::KernelType::block_wmma);
time_kernel(q, k, v, flash::KernelType::wmma_row_block);
time_kernel(q, k, v, flash::KernelType::mma);
time_kernel(q, k, v, flash::KernelType::mma_swizzle);
}
time_kernel(q, k, v, flash::KernelType::mma_qreg);
time_kernel(q, k, v, flash::KernelType::mma_qreg_f32x4load);
time_kernel(q, k, v, flash::KernelType::mma_qreg_async);
if (!ncu) {
time_kernel(q, k, v, flash::KernelType::block_wmma_async);
time_kernel(q, k, v, "manual attention", manual_attn);
time_kernel(q, k, v, "torch attention v2", torch_attn2);
}
}
return EXIT_SUCCESS;
}