-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathhelpers.cpp
More file actions
501 lines (461 loc) · 17.2 KB
/
Copy pathhelpers.cpp
File metadata and controls
501 lines (461 loc) · 17.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
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
* SPDX-FileCopyrightText: Copyright (c) 2020 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include "./utils/helpers.h"
size_t
get_compute_capability() {
struct cudaDeviceProp prop;
checkCudaErrors(cudaGetDeviceProperties(&prop, 0));
return prop.major * 10 + prop.minor;
}
bool
is_ampere_arch() {
auto cc = get_compute_capability();
return (80 <= cc) && (cc < 89);
}
bool
is_ada_arch() {
auto cc = get_compute_capability();
return (cc == 89);
}
bool
is_hopper_arch() {
auto cc = get_compute_capability();
return (90 <= cc && cc < 100);
}
bool
is_blackwell_computing_arch() {
auto cc = get_compute_capability();
return (100 <= cc && cc < 110);
}
bool
is_blackwell_gaming_arch() {
auto cc = get_compute_capability();
return (120 <= cc && cc < 130);
}
bool
is_arch_supported_by_cudnn() {
if (cudnnGetVersion() < 8600 && (is_hopper_arch() || is_ada_arch())) {
return false;
}
return true;
}
bool
check_device_arch_newer_than(std::string const& arch) {
size_t arch_major = 6;
size_t arch_minor = 0;
if (arch == "blackwell") {
arch_major = 10;
}
if (arch == "hopper") {
arch_major = 9;
}
if (arch == "ampere") {
arch_major = 8;
}
if (arch == "turing") {
arch_major = 7;
arch_minor = 5;
}
if (arch == "volta") {
arch_major = 7;
}
if (arch == "pascal") {
arch_major = 6;
}
auto queried_version = arch_major * 10 + arch_minor;
if (get_compute_capability() >= queried_version) {
return true;
}
return false;
}
// Generate uniform numbers [0,1)
void
initImage(float* image, int64_t imageSize) {
static unsigned seed = 123456789;
for (int64_t index = 0; index < imageSize; index++) {
seed = (1103515245 * seed + 12345) & 0xffffffff;
image[index] = float(seed) * 2.3283064e-10f; // 2^-32
}
}
void
testinitImage(half1* image, int64_t imageSize, int test) {
static unsigned seed = 123456789;
for (int64_t index = 0; index < imageSize; index++) {
seed = (1103515245 * seed + 12345) & 0xffffffff;
// image[index] = cpu_float2half_rn(float(seed) * 2.3283064e-10f); // 2^-32
if (test)
image[index] = cpu_float2half_rn(static_cast<float>((index + 1) * 2)); // 2^-32
else
image[index] = cpu_float2half_rn(static_cast<float>(index + 1)); // 2^-32
}
}
void
initImage(half1* image, int64_t imageSize) {
static unsigned seed = 123456789;
for (int64_t index = 0; index < imageSize; index++) {
seed = (1103515245 * seed + 12345) & 0xffffffff;
image[index] = cpu_float2half_rn(float(seed) * 2.3283064e-10f); // 2^-32
}
}
// Currently set to generate uniform integers [-2, 2] to avoid int8 overflow
void
initImage(int8_t* image, int64_t imageSize) {
static unsigned seed = 123456789;
for (int64_t index = 0; index < imageSize; index++) {
seed = (1103515245 * seed + 12345) & 0xffffffff;
// Takes floats from [0, 1), scales and casts to ints from [0, 4], then subtracts from 2
image[index] = 2 - (int8_t)(5 * float(seed) * 2.3283064e-10f); // 2^-32
}
}
// Currently set to generate random integers [0, 50] to avoid uint8 overflow
void
initImage(uint8_t* image, int64_t imageSize) {
static unsigned seed = 123456789;
for (int64_t index = 0; index < imageSize; index++) {
seed = (1103515245 * seed + 12345) & 0xffffffff;
// Takes floats from [0, 1), scales and casts to ints from [0, 50]
image[index] = (uint8_t)(50 * float(seed) * 2.3283064e-10f); // 2^-32
}
}
// Currently set to generate uniform integers [0,1]
void
initImage(int32_t* image, int64_t imageSize) {
static unsigned seed = 123456789;
for (int64_t index = 0; index < imageSize; index++) {
seed = (1103515245 * seed + 12345) & 0xffffffff;
// Takes floats from [0, 1), scales and casts to ints from [0, 4], then divides by 4
image[index] = ((int32_t)(5.f * float(seed) * 2.3283064e-10f)) / 4; // 2^-32
}
}
// Currently set to generate uniform integers [0,1]
void
initImage(int64_t* image, int64_t imageSize) {
static unsigned seed = 123456789;
for (int64_t index = 0; index < imageSize; index++) {
seed = (1103515245 * seed + 12345) & 0xffffffff;
// Takes floats from [0, 1), scales and casts to ints from [0, 4], then divides by 4
image[index] = ((int64_t)(5.f * float(seed) * 2.3283064e-10f)) / 4; // 2^-32
}
}
// Currently set to generate booleans
void
initImage(bool* image, int64_t imageSize) {
static unsigned seed = 123456789;
for (int64_t index = 0; index < imageSize; index++) {
seed = (1103515245 * seed + 12345) & 0xffffffff;
// Takes floats from [0, 1), scales and casts to ints from [0, 4], then divides by 4
int64_t val = ((int32_t)(5.f * float(seed) * 2.3283064e-10f)) / 4; // 2^-32
// val is 0 or 1
image[index] = (val == 1);
}
}
void
initImagePadded(int8_t* image, int64_t dimA[], int64_t dimPadded[], int64_t stridePadded[], cudnnDataType_t dataType) {
static unsigned seed = 123456789;
int64_t resizeFactor = (dataType == CUDNN_DATA_INT8x4) ? 4 : 32;
int64_t totalSize = dimPadded[0] * dimPadded[1] * dimPadded[2] * dimPadded[3];
// #pragma omp parallel for
for (int64_t i = 0; i < totalSize; i++) {
int64_t n = (i / stridePadded[0]) % dimPadded[0];
int64_t c1 = (i / (stridePadded[1] * resizeFactor)) % (dimPadded[1] / resizeFactor);
int64_t c2 = i % resizeFactor;
int64_t c = c1 * resizeFactor + c2;
if (n < dimA[0] && c < dimA[1]) {
image[i] = 2 - (int8_t)(5 * float(seed) * 2.3283064e-10); // 2^-32
} else {
image[i] = 0;
}
}
}
int64_t
checkCudaError(cudaError_t code, const char* expr, const char* file, int line) {
if (code) {
printf("CUDA error at %s:%d, code=%d (%s) in '%s'", file, line, (int)code, cudaGetErrorString(code), expr);
return 1;
}
return 0;
}
int64_t
checkCudnnError(cudnnStatus_t code, const char* expr, const char* file, int line) {
if (code) {
printf("CUDNN error at %s:%d, code=%d (%s) in '%s'\n", file, line, (int)code, cudnnGetErrorString(code), expr);
return 1;
}
return 0;
}
void
generateStrides(const int64_t* dimA, int64_t* strideA, int64_t nbDims, cudnnTensorFormat_t filterFormat) {
// For INT8x4 and INT8x32 we still compute standard strides here to input
// into the cuDNN functions. We will manually scale by resizeFactor in the cpu ref.
if (filterFormat == CUDNN_TENSOR_NCHW) {
strideA[nbDims - 1] = 1;
for (int64_t d = nbDims - 2; d >= 0; d--) {
strideA[d] = strideA[d + 1] * dimA[d + 1];
}
} else {
// Here we assume that the format is CUDNN_TENSOR_NHWC
strideA[1] = 1;
strideA[nbDims - 1] = strideA[1] * dimA[1];
for (int64_t d = nbDims - 2; d >= 2; d--) {
strideA[d] = strideA[d + 1] * dimA[d + 1];
}
strideA[0] = strideA[2] * dimA[2];
}
}
// Used for MHA
void
generateMHAStrides(int64_t b,
int64_t h,
int64_t s_q,
int64_t s_kv,
int64_t d,
int64_t* strideA,
MHA_Layout layout,
MHA_Matrix matrix) {
CUDNN_FRONTEND_UNUSED(b);
constexpr int batch_dim_idx = 0;
constexpr int head_dim_idx = 1;
constexpr int seqlen_dim_idx = 2;
constexpr int hidden_dim_idx = 3;
constexpr int seqlen_transpose_dim_idx = 3;
constexpr int hidden_transpose_dim_idx = 2;
constexpr int seqlen_q_dim_idx = 2;
constexpr int seqlen_kv_dim_idx = 3;
switch (matrix) {
case MHA_Matrix::Q_Matrix:
if (layout == MHA_Layout::QKV_INTERLEAVED) {
strideA[seqlen_dim_idx] = 3 * h * d;
strideA[hidden_dim_idx] = 1;
strideA[head_dim_idx] = d;
strideA[batch_dim_idx] = s_q * 3 * h * d;
} else if (layout == MHA_Layout::SBH_INTERLEAVED) {
strideA[seqlen_dim_idx] = 3 * h * d * b;
strideA[hidden_dim_idx] = 1;
strideA[head_dim_idx] = 3 * d;
strideA[batch_dim_idx] = 3 * h * d;
} else {
strideA[seqlen_dim_idx] = h * d;
strideA[hidden_dim_idx] = 1;
strideA[head_dim_idx] = d;
strideA[batch_dim_idx] = s_q * h * d;
}
break;
case MHA_Matrix::K_Matrix:
if (layout == MHA_Layout::QKV_INTERLEAVED) {
strideA[seqlen_dim_idx] = 3 * h * d;
strideA[hidden_dim_idx] = 1;
strideA[head_dim_idx] = d;
strideA[batch_dim_idx] = s_kv * 3 * h * d;
} else if (layout == MHA_Layout::KV_INTERLEAVED) {
strideA[seqlen_dim_idx] = 2 * h * d;
strideA[hidden_dim_idx] = 1;
strideA[head_dim_idx] = d;
strideA[batch_dim_idx] = s_kv * 2 * h * d;
} else if (layout == MHA_Layout::SBH_INTERLEAVED) {
strideA[seqlen_dim_idx] = 3 * h * d * b;
strideA[hidden_dim_idx] = 1;
strideA[head_dim_idx] = 3 * d;
strideA[batch_dim_idx] = 3 * h * d;
} else {
strideA[seqlen_dim_idx] = h * d;
strideA[hidden_dim_idx] = 1;
strideA[head_dim_idx] = d;
strideA[batch_dim_idx] = s_kv * h * d;
}
break;
case MHA_Matrix::K_Matrix_Transpose:
if (layout == MHA_Layout::QKV_INTERLEAVED) {
strideA[seqlen_transpose_dim_idx] = 3 * h * d;
strideA[hidden_transpose_dim_idx] = 1;
strideA[head_dim_idx] = d;
strideA[batch_dim_idx] = s_kv * 3 * h * d;
} else if (layout == MHA_Layout::KV_INTERLEAVED) {
strideA[seqlen_transpose_dim_idx] = 2 * h * d;
strideA[hidden_transpose_dim_idx] = 1;
strideA[head_dim_idx] = d;
strideA[batch_dim_idx] = s_kv * 2 * h * d;
} else if (layout == MHA_Layout::SBH_INTERLEAVED) {
strideA[seqlen_transpose_dim_idx] = 3 * h * d * b;
strideA[hidden_transpose_dim_idx] = 1;
strideA[head_dim_idx] = 3 * d;
strideA[batch_dim_idx] = 3 * h * d;
} else {
strideA[seqlen_transpose_dim_idx] = h * d;
strideA[hidden_transpose_dim_idx] = 1;
strideA[head_dim_idx] = d;
strideA[batch_dim_idx] = s_kv * h * d;
}
break;
case MHA_Matrix::V_Matrix:
if (layout == MHA_Layout::QKV_INTERLEAVED) {
strideA[seqlen_dim_idx] = 3 * h * d;
strideA[hidden_dim_idx] = 1;
strideA[head_dim_idx] = d;
strideA[batch_dim_idx] = s_kv * 3 * h * d;
} else if (layout == MHA_Layout::KV_INTERLEAVED) {
strideA[seqlen_dim_idx] = 2 * h * d;
strideA[hidden_dim_idx] = 1;
strideA[head_dim_idx] = d;
strideA[batch_dim_idx] = s_kv * 2 * h * d;
} else if (layout == MHA_Layout::SBH_INTERLEAVED) {
strideA[seqlen_dim_idx] = 3 * h * d * b;
strideA[hidden_dim_idx] = 1;
strideA[head_dim_idx] = 3 * d;
strideA[batch_dim_idx] = 3 * h * d;
} else {
strideA[seqlen_dim_idx] = h * d;
strideA[hidden_dim_idx] = 1;
strideA[head_dim_idx] = d;
strideA[batch_dim_idx] = s_kv * h * d;
}
break;
case MHA_Matrix::V_Matrix_Transpose:
if (layout == MHA_Layout::QKV_INTERLEAVED) {
strideA[seqlen_transpose_dim_idx] = 3 * h * d;
strideA[hidden_transpose_dim_idx] = 1;
strideA[head_dim_idx] = d;
strideA[batch_dim_idx] = s_kv * 3 * h * d;
} else if (layout == MHA_Layout::KV_INTERLEAVED) {
strideA[seqlen_transpose_dim_idx] = 2 * h * d;
strideA[hidden_transpose_dim_idx] = 1;
strideA[head_dim_idx] = d;
strideA[batch_dim_idx] = s_kv * 2 * h * d;
} else if (layout == MHA_Layout::SBH_INTERLEAVED) {
strideA[seqlen_transpose_dim_idx] = 3 * h * d * b;
strideA[hidden_transpose_dim_idx] = 1;
strideA[head_dim_idx] = 3 * d;
strideA[batch_dim_idx] = 3 * h * d;
} else {
strideA[seqlen_transpose_dim_idx] = h * d;
strideA[hidden_transpose_dim_idx] = 1;
strideA[head_dim_idx] = d;
strideA[batch_dim_idx] = s_kv * h * d;
}
break;
case MHA_Matrix::S_Matrix:
strideA[seqlen_kv_dim_idx] = 1;
strideA[seqlen_q_dim_idx] = s_kv;
strideA[head_dim_idx] = s_q * s_kv;
strideA[batch_dim_idx] = h * s_q * s_kv;
break;
case MHA_Matrix::O_Matrix:
strideA[seqlen_kv_dim_idx] = 1;
strideA[seqlen_q_dim_idx] = h * d;
strideA[head_dim_idx] = d;
strideA[batch_dim_idx] = s_q * h * d;
break;
}
}
// Used for CHWN
void
generate4dTransposeStrides(const int64_t* dimA, int64_t* strideA, int64_t nbDims, cudnnTensorFormat_t filterFormat) {
// For INT8x4 and INT8x32 we still compute standard strides here to input
// into the cuDNN functions. We will manually scale by resizeFactor in the cpu ref.
try {
if (filterFormat == CUDNN_TENSOR_NCHW) {
throw std::runtime_error("[ERROR] NCHW tranpose not supported");
} else if (nbDims != 4) {
throw std::runtime_error("[ERROR] Only 4 dims supported");
} else {
// Here we assume that the format is NWHC getting tranposed to CHWN
strideA[0] = 1; // N has stride 1
strideA[3] = strideA[0] * dimA[0]; // W has stride strideN * dimN
strideA[2] = strideA[3] * dimA[3]; // H has stride strideW * dimW
strideA[1] = strideA[2] * dimA[2]; // C has stride strideH * dimH
}
} catch (std::exception& e) {
std::cout << "Exception: " << e.what() << std::endl;
}
}
// Convert a linear index
// i = d_1 s_1 ... s_n + d_2 s_2 ... s_n + d_n-1 s_n + d_n
// into a multidimensional index
// (d_1, d_2, ..., d_n)
void
lin2dim(int64_t id, int64_t* ids, const int64_t* dims, int64_t length) {
int64_t idrem = id;
int64_t prod = 1; // accumulates the product of the dimensions
for (int64_t i = length - 1; i >= 0; i--) {
ids[i] = (idrem / prod) % dims[i];
idrem = id - ids[i] * prod;
prod *= dims[i];
}
}
// Convert a multidimensional index
// (d_1, d_2, ..., d_n)
// into a linear index
// i = d_1 s_1 + ... + d_n s_n
int64_t
dim2lin(const int64_t* ids, const int64_t* strides, int64_t length) {
int64_t res = 0;
for (int64_t i = 0; i < length; i++) {
res += ids[i] * strides[i];
}
return static_cast<int>(res);
}
void
doEpilog(float* out, int64_t idx, float alphaAcc, float beta) {
if (beta == 0.f) {
out[idx] = alphaAcc;
} else {
out[idx] = alphaAcc + out[idx] * beta;
}
}
void
doEpilog(half1* out, int64_t idx, float alphaAcc, float beta) {
if (beta == 0.f) {
out[idx] = cpu_float2half_rn(alphaAcc);
} else {
out[idx] = cpu_float2half_rn(alphaAcc + cpu_half2float(out[idx]) * beta);
}
}
void
doEpilog(int8_t* out, int64_t idx, int32_t alphaAcc, float beta) {
int32_t val;
if (beta == 0.f) {
val = alphaAcc;
} else {
val = alphaAcc + int(float(out[idx]) * beta);
}
// Properly handle overflow errors in the same way cuDNN does
if (val > 127) {
val = 127;
} else if (val < -128) {
val = -128;
}
out[idx] = static_cast<int8_t>(val);
}
float
getError(float dev, float ref) {
if (ref > 1.0 || ref < -1.0)
return (dev - ref) / ref;
else
return dev - ref;
}
float
getError(half1 dev, half1 ref) {
if (cpu_half2float(ref) > 1.0 || cpu_half2float(ref) < -1.0)
return (cpu_half2float(dev) - cpu_half2float(ref)) / cpu_half2float(ref);
else
return cpu_half2float(dev) - cpu_half2float(ref);
}
int8_t
getError(int8_t dev, int8_t ref) {
return dev - ref;
}
int64_t
getFwdConvDilatedFilterDim(int64_t filterDim, int64_t dilation) {
return ((filterDim - 1) * dilation) + 1;
}
int64_t
getFwdConvPaddedImageDim(int64_t tensorDim, int64_t pad) {
return tensorDim + (2 * pad);
}
int64_t
getFwdConvOutputDim(int64_t tensorDim, int64_t pad, int64_t filterDim, int64_t stride, int64_t dilation) {
int64_t p =
(getFwdConvPaddedImageDim(tensorDim, pad) - getFwdConvDilatedFilterDim(filterDim, dilation)) / stride + 1;
return (p);
}