static_switch.h 21.4 KB
Newer Older
zhangshao's avatar
zhangshao committed
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
// Inspired by
// https://github.com/NVIDIA/DALI/blob/main/include/dali/core/static_switch.h
// and https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/Dispatch.h

#pragma once
#include "numeric_types.h"
#include "splitkv.h"

/// @param COND       - a boolean expression to switch by
/// @param CONST_NAME - a name given for the constexpr bool variable.
/// @param ...       - code to execute for true and false
///
/// Usage:
/// ```
/// BOOL_SWITCH(flag, BoolConst, [&] {
///     some_function<BoolConst>(...);
/// });
/// ```
#define BOOL_SWITCH(COND, CONST_NAME, ...)      \
  [&] {                                         \
    if (COND) {                                 \
      constexpr static bool CONST_NAME = true;  \
      return __VA_ARGS__();                     \
    } else {                                    \
      constexpr static bool CONST_NAME = false; \
      return __VA_ARGS__();                     \
    }                                           \
  }()

#define FP16_SWITCH(COND, ...)               \
  [&] {                                      \
    if (COND) {                              \
      using elem_type = Float16;     \
      return __VA_ARGS__();                  \
    } else {                                 \
      using elem_type = BFloat16; \
      return __VA_ARGS__();                  \
    }                                        \
  }()

#define LAYOUT_SWITCH(layout, ...)         \
  [&] {                                    \
    if (layout == 0) {                     \
      constexpr static int Layout = 0;     \
      return __VA_ARGS__();                \
    } else if (layout == 1) {              \
      constexpr static int Layout = 1;     \
      return __VA_ARGS__();                \
    }                                      \
  }()

// #define ElementType_SWITCH(is_bf16, is_e4m3, ...)   \
//   [&] {                                      \
//     if (is_bf16) {                           \
//       using elem_type = BFloat16;            \
//       return __VA_ARGS__();                  \
//     } else if (is_e4m3) {                    \
//       using elem_type = Float8_e4m3_t;       \
//       return __VA_ARGS__();                  \
//     } else {                                 \
//       using elem_type = Float16;             \
//       return __VA_ARGS__();                  \
//     }                                        \
//   }()

#define ElementType_SWITCH(is_bf16, is_e4m3, ...)   \
[&] {                                      \
  if (is_bf16) {                           \
    using elem_type = BFloat16;            \
    return __VA_ARGS__();                  \
  } else if (is_e4m3) {                    \
    printf("fa bwd does not support fp8 yet");\
  } else {                                 \
    using elem_type = Float16;             \
    return __VA_ARGS__();                  \
  }                                        \
}()

#define HEADDIM_SWITCH(HEADDIMQ, HEADDIMV, ...)   \
  [&] {                                       \
    if (HEADDIMQ <= 32) {                     \
      constexpr static int kHeadDimQ = 32;    \
      constexpr static int kHeadDimV = 32;    \
      return __VA_ARGS__();                   \
    } else if (HEADDIMQ <= 64) {              \
      constexpr static int kHeadDimQ = 64;    \
      constexpr static int kHeadDimV = 64;    \
      return __VA_ARGS__();                   \
    } else if (HEADDIMQ <= 96) {              \
      constexpr static int kHeadDimQ = 96;    \
      constexpr static int kHeadDimV = 96;    \
      return __VA_ARGS__();                   \
    } else if (HEADDIMQ <= 128) {             \
      constexpr static int kHeadDimQ = 128;   \
      constexpr static int kHeadDimV = 128;   \
      return __VA_ARGS__();                   \
    } else if (HEADDIMQ <= 160) {             \
      constexpr static int kHeadDimQ = 160;   \
      constexpr static int kHeadDimV = 160;   \
      return __VA_ARGS__();                   \
    } else if (HEADDIMQ <= 192) {             \
      constexpr static int kHeadDimQ = 192;   \
      if (HEADDIMV <= 128) {                  \
        constexpr static int kHeadDimV = 128; \
        return __VA_ARGS__();                 \
      } else if (HEADDIMV <= 192) {           \
        constexpr static int kHeadDimV = 192; \
        return __VA_ARGS__();                 \
      }                                       \
    } else if (HEADDIMQ <= 224) {             \
      constexpr static int kHeadDimQ = 224;   \
      constexpr static int kHeadDimV = 224;   \
      return __VA_ARGS__();                   \
    } else if (HEADDIMQ <= 256) {             \
      constexpr static int kHeadDimQ = 256;   \
      constexpr static int kHeadDimV = 256;   \
      return __VA_ARGS__();                   \
    }                                         \
  }()


#define ALL_HEADDIM_SWITCH(HEADDIMQ, HEADDIMV, ...)   \
  [&] {                                       \
    if (HEADDIMQ <= 32) {                     \
      constexpr static int kHeadDimQ = 32;    \
      constexpr static int kHeadDimV = 32;    \
      return __VA_ARGS__();                   \
    } else if (HEADDIMQ <= 64) {              \
      constexpr static int kHeadDimQ = 64;    \
      constexpr static int kHeadDimV = 64;    \
      return __VA_ARGS__();                   \
    } else if (HEADDIMQ <= 96) {              \
      constexpr static int kHeadDimQ = 96;    \
      constexpr static int kHeadDimV = 96;    \
      return __VA_ARGS__();                   \
    } else if (HEADDIMQ <= 128) {             \
      constexpr static int kHeadDimQ = 128;   \
      constexpr static int kHeadDimV = 128;   \
      return __VA_ARGS__();                   \
    } else if (HEADDIMQ <= 160) {             \
      constexpr static int kHeadDimQ = 160;   \
      constexpr static int kHeadDimV = 160;   \
      return __VA_ARGS__();                   \
    } else if (HEADDIMQ <= 192) {             \
      constexpr static int kHeadDimQ = 192;   \
      if (HEADDIMV <= 128) {                  \
        constexpr static int kHeadDimV = 128; \
        return __VA_ARGS__();                 \
      } else if (HEADDIMV <= 192) {           \
        constexpr static int kHeadDimV = 192; \
        return __VA_ARGS__();                 \
      }                                       \
    } else if (HEADDIMQ <= 224) {             \
      constexpr static int kHeadDimQ = 224;   \
      constexpr static int kHeadDimV = 224;   \
      return __VA_ARGS__();                   \
    }  else if (HEADDIMQ <= 256) {            \
      constexpr static int kHeadDimQ = 256;   \
      constexpr static int kHeadDimV = 256;   \
      return __VA_ARGS__();                   \
    } else if (HEADDIMQ <= 512) {             \
      constexpr static int kHeadDimQ = 512;   \
      constexpr static int kHeadDimV = 512;   \
      return __VA_ARGS__();                   \
    }                                         \
  }()


#define WARP_ID_SWITCH(warp_id, ...)   \
  [&] {                                    \
    if (warp_id == 0) {                    \
      constexpr  int WARP_ID = 0;          \
      return __VA_ARGS__();                \
    } else if (warp_id == 1) {             \
      constexpr  int WARP_ID = 1;          \
      return __VA_ARGS__();                \
    } else if (warp_id == 2) {             \
      constexpr  int WARP_ID = 2;          \
      return __VA_ARGS__();                \
    } else if (warp_id == 3) {             \
      constexpr  int WARP_ID = 3;          \
      return __VA_ARGS__();                \
    }                                      \
  }()


#define CU_SWITCH(cu_count, ...)           \
  [&] {                                    \
    if (cu_count == 120) {                 \
      constexpr  int CU_COUNT = 120;       \
      return __VA_ARGS__();                \
    } else if (cu_count == 128) {          \
      constexpr  int CU_COUNT = 128;       \
      return __VA_ARGS__();                \
    } else if (cu_count == 88) {           \
      constexpr  int CU_COUNT = 88;        \
      return __VA_ARGS__();                \
    } else if (cu_count == 80) {           \
      constexpr  int CU_COUNT = 80;        \
      return __VA_ARGS__();                \
    }                                      \
  }()

#define M_MMAC_COUNT_SWITCH(COND, M_MMAC_COUNT, ...)    \
  [&] {                                         \
    if (COND) {                                 \
      constexpr static int M_MMAC_COUNT = 2;    \
      return __VA_ARGS__();                     \
    } else {                                    \
      constexpr static int M_MMAC_COUNT = 1;    \
      return __VA_ARGS__();                     \
    }                                           \
  }()



#define REUSEKV_SWITCH(seqlen_q , ...)                  \
[&] {                                                   \
    if (seqlen_q == 16) {                               \
        constexpr static int REUSE_KV_TIMES = 16;       \
        return __VA_ARGS__();                           \
    } else if (seqlen_q == 13) {                        \
        constexpr static int REUSE_KV_TIMES = 13;       \
        return __VA_ARGS__();                           \
    } else if (seqlen_q == 8) {                         \
        constexpr static int REUSE_KV_TIMES = 8;        \
        return __VA_ARGS__();                           \
    } else if (seqlen_q == 7) {                         \
        constexpr static int REUSE_KV_TIMES = 7;        \
        return __VA_ARGS__();                           \
    } else if (seqlen_q == 4) {                         \
        constexpr static int REUSE_KV_TIMES = 4;        \
        return __VA_ARGS__();                           \
    } else if (seqlen_q == 3) {                         \
        constexpr static int REUSE_KV_TIMES = 3;        \
        return __VA_ARGS__();                           \
    } else if (seqlen_q == 2) {                         \
        constexpr static int REUSE_KV_TIMES = 2;        \
        return __VA_ARGS__();                           \
    } else if (seqlen_q == 1) {                         \
        constexpr static int REUSE_KV_TIMES = 1;        \
        return __VA_ARGS__();                           \
    } else if (seqlen_q == 29) {                        \
        constexpr static int REUSE_KV_TIMES = 29;       \
        return __VA_ARGS__();                           \
    } else {                                            \
        constexpr static int REUSE_KV_TIMES = 0;        \
        return __VA_ARGS__();                           \
    }                                                   \
}()


#define PA_GQA_REGROUP_SWITCH(ngroups, ...) \
[&] {                               \
    if (ngroups == 8) {             \
        int GQA_REGROUP = 8;        \
        return __VA_ARGS__();       \
    } else if (ngroups == 4) {      \
        int GQA_REGROUP = 4;        \
        return __VA_ARGS__();       \
    } else if (ngroups == 2) {      \
        int GQA_REGROUP = 2;        \
        return __VA_ARGS__();       \
    } else if (ngroups == 9) {      \
        int GQA_REGROUP = 9;        \
        return __VA_ARGS__();       \
    } else if (ngroups == 7) {      \
        int GQA_REGROUP = 7;        \
        return __VA_ARGS__();       \
    } else if (ngroups == 5) {      \
        int GQA_REGROUP = 5;        \
        return __VA_ARGS__();       \
    } else if (ngroups == 3) {      \
        int GQA_REGROUP = 3;        \
        return __VA_ARGS__();       \
    } else if (ngroups == 29) {     \
        int GQA_REGROUP = 29;       \
        return __VA_ARGS__();       \
    } else {                        \
        int GQA_REGROUP = ngroups;  \
        return __VA_ARGS__();       \
    }                               \
}()

#define PA_MTP_REUSEKV_SWITCH(pa_mtp , ...)             \
[&] {                                                   \
    if (pa_mtp == 8) {                                  \
        constexpr static int REUSE_KV_TIMES = 8;        \
        return __VA_ARGS__();                           \
    } else if (pa_mtp == 16) {                          \
        constexpr static int REUSE_KV_TIMES = 16;       \
        return __VA_ARGS__();                           \
    } else if (pa_mtp == 20) {                          \
        constexpr static int REUSE_KV_TIMES = 20;       \
        return __VA_ARGS__();                           \
    } else if (pa_mtp == 24) {                          \
        constexpr static int REUSE_KV_TIMES = 24;       \
        return __VA_ARGS__();                           \
    } else if (pa_mtp == 28) {                          \
        constexpr static int REUSE_KV_TIMES = 28;       \
        return __VA_ARGS__();                           \
    } else if (pa_mtp == 32) {                          \
        constexpr static int REUSE_KV_TIMES = 32;       \
        return __VA_ARGS__();                           \
    } else {                                            \
        constexpr static int REUSE_KV_TIMES = 0;        \
        return __VA_ARGS__();                           \
    }                                                   \
}()


#define MLA_REUSEKV_SWITCH(seqlen_q , ...)              \
[&] {                                                   \
    if (seqlen_q == 128) {                              \
        constexpr static int REUSE_KV_TIMES = 128;      \
        return __VA_ARGS__();                           \
    } else if (seqlen_q == 64) {                        \
        constexpr static int REUSE_KV_TIMES = 64;       \
        return __VA_ARGS__();                           \
    } else if (seqlen_q == 32) {                        \
        constexpr static int REUSE_KV_TIMES = 32;       \
        return __VA_ARGS__();                           \
    } else if (seqlen_q == 16) {                        \
        constexpr static int REUSE_KV_TIMES = 16;       \
        return __VA_ARGS__();                           \
    } else if (seqlen_q == 12) {                        \
        constexpr static int REUSE_KV_TIMES = 12;       \
        return __VA_ARGS__();                           \
    } else if (seqlen_q == 8) {                         \
        constexpr static int REUSE_KV_TIMES = 8;        \
        return __VA_ARGS__();                           \
    } else if (seqlen_q == 4) {                         \
        constexpr static int REUSE_KV_TIMES = 4;        \
        return __VA_ARGS__();                           \
    } else if (seqlen_q == 2) {                         \
        constexpr static int REUSE_KV_TIMES = 2;        \
        return __VA_ARGS__();                           \
    } else if (seqlen_q == 1) {                         \
        constexpr static int REUSE_KV_TIMES = 1;        \
        return __VA_ARGS__();                           \
    }                                                   \
}()


#define MLA_MTP_REUSEKV_SWITCH(seqlen_q , ...)          \
[&] {                                                   \
    if (seqlen_q == 32) {                               \
        constexpr static int REUSE_KV_TIMES = 32;       \
        return __VA_ARGS__();                           \
    } else if (seqlen_q == 24) {                        \
        constexpr static int REUSE_KV_TIMES = 24;       \
        return __VA_ARGS__();                           \
    } else if (seqlen_q == 16) {                        \
        constexpr static int REUSE_KV_TIMES = 16;       \
        return __VA_ARGS__();                           \
    } else {                                            \
        constexpr static int REUSE_KV_TIMES = 8;        \
        return __VA_ARGS__();                           \
    }                                                   \
}()


#define PERMUTE_DWORD_SWITCH(COND, DWORD_PER_TX, ...)    \
  [&] {                                         \
    if (COND) {                                 \
      constexpr static int DWORD_PER_TX = 4;    \
      return __VA_ARGS__();                     \
    } else {                                    \
      constexpr static int DWORD_PER_TX = 1;    \
      return __VA_ARGS__();                     \
    }                                           \
  }()

#define PERMUTE_HEADDIM_SWITCH(HEADDIM, ...)   \
  [&] {                                    \
    if (HEADDIM == 64) {                   \
      constexpr static int kHeadDim = 64;  \
      return __VA_ARGS__();                \
    } else if (HEADDIM == 128) {           \
      constexpr static int kHeadDim = 128; \
      return __VA_ARGS__();                \
    } else if (HEADDIM == 192) {           \
      constexpr static int kHeadDim = 192; \
      return __VA_ARGS__();                \
    } else if (HEADDIM < 256) {            \
      constexpr static int kHeadDim = 0;   \
      return __VA_ARGS__();                \
    }  else if (HEADDIM == 256) {          \
      constexpr static int kHeadDim = 256; \
      return __VA_ARGS__();                \
    }                                      \
  }()


#define PA_HEADDIM_SWITCH(HEADDIMQ, HEADDIMV, ...)   \
  [&] {                                       \
    if (HEADDIMQ <= 32) {                     \
      constexpr static int kHeadDimQ = 32;    \
      constexpr static int kHeadDimV = 32;    \
      return __VA_ARGS__();                   \
    } else if (HEADDIMQ <= 64) {              \
      constexpr static int kHeadDimQ = 64;    \
      constexpr static int kHeadDimV = 64;    \
      return __VA_ARGS__();                   \
    } else if (HEADDIMQ <= 96) {              \
      constexpr static int kHeadDimQ = 96;    \
      constexpr static int kHeadDimV = 96;    \
      return __VA_ARGS__();                   \
    } else if (HEADDIMQ <= 128) {             \
      constexpr static int kHeadDimQ = 128;   \
      constexpr static int kHeadDimV = 128;   \
      return __VA_ARGS__();                   \
    } else if (HEADDIMQ <= 160) {             \
      constexpr static int kHeadDimQ = 160;   \
      constexpr static int kHeadDimV = 160;   \
      return __VA_ARGS__();                   \
    } else if (HEADDIMQ <= 192) {             \
      constexpr static int kHeadDimQ = 192;   \
      if (HEADDIMV <= 128) {                  \
        constexpr static int kHeadDimV = 128; \
        return __VA_ARGS__();                 \
      } else if (HEADDIMV <= 192) {           \
        constexpr static int kHeadDimV = 192; \
        return __VA_ARGS__();                 \
      }                                       \
    } else if (HEADDIMQ <= 224) {             \
      constexpr static int kHeadDimQ = 224;   \
      constexpr static int kHeadDimV = 224;   \
      return __VA_ARGS__();                   \
    } else if (HEADDIMQ <= 256) {             \
      constexpr static int kHeadDimQ = 256;   \
      constexpr static int kHeadDimV = 256;   \
      return __VA_ARGS__();                   \
    } else if (HEADDIMQ == 576) {             \
      constexpr static int kHeadDimQ = 576;   \
      constexpr static int kHeadDimV = 512;   \
      return __VA_ARGS__();                   \
    }                                         \
  }()


#define PA_PAGEBLOCKSIZE_SWITCH(page_block_size, ...)                      \
  [&] {                                                                    \
    if (page_block_size % 32 == 0 and page_block_size % 64 != 0) {         \
      constexpr static int kBlockN = 32;                                   \
      return __VA_ARGS__();                                                \
    } else if (page_block_size % 64 == 0 and page_block_size % 128 != 0) { \
      constexpr static int kBlockN = 64;                                   \
      return __VA_ARGS__();                                                \
    } else if (page_block_size % 128 == 0) {                               \
      constexpr static int kBlockN = 128;                                  \
      return __VA_ARGS__();                                                \
    }                                                                      \
  }()


#define MLA_PARTITION_SIZE_SWITCH(partition_size, num_splits, ...)         \
  [&] {                                                                    \
    if (partition_size == 128 and num_splits > 1) {                        \
      constexpr static int Partition_Size = 1;                             \
      return __VA_ARGS__();                                                \
    } else if (partition_size == 256 and num_splits > 1) {                 \
      constexpr static int Partition_Size = 2;                             \
      return __VA_ARGS__();                                                \
    } else if (partition_size == 512 and num_splits > 1) {                 \
      constexpr static int Partition_Size = 4;                             \
      return __VA_ARGS__();                                                \
    } else if (partition_size == 1024 and num_splits > 1) {                \
      constexpr static int Partition_Size = 8;                             \
      return __VA_ARGS__();                                                \
    } else if (partition_size == MLA_FIX_PARTITION and num_splits > 1) {   \
      constexpr static int Partition_Size = MLA_FIX_PARTITION;             \
      return __VA_ARGS__();                                                \
    } else {                                                               \
      constexpr static int Partition_Size = 0;                             \
      return __VA_ARGS__();                                                \
    }                                                                      \
  }()