2023-07-19 16:04:55 +08:00
|
|
|
// Inspired by
|
|
|
|
|
// https://github.com/NVIDIA/DALI/blob/main/include/dali/core/static_switch.h
|
2022-07-10 09:39:02 +08:00
|
|
|
// and https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/Dispatch.h
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
/// @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:
|
|
|
|
|
/// ```
|
2023-07-17 20:26:11 +08:00
|
|
|
/// BOOL_SWITCH(flag, BoolConst, [&] {
|
2022-07-10 09:39:02 +08:00
|
|
|
/// some_function<BoolConst>(...);
|
2023-07-17 20:26:11 +08:00
|
|
|
/// });
|
2022-07-10 09:39:02 +08:00
|
|
|
/// ```
|
2023-07-19 16:04:55 +08:00
|
|
|
#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__(); \
|
|
|
|
|
} \
|
|
|
|
|
}()
|
2022-12-07 06:16:04 +08:00
|
|
|
|
2023-07-19 16:04:55 +08:00
|
|
|
#define FP16_SWITCH(COND, ...) \
|
|
|
|
|
[&] { \
|
|
|
|
|
if (COND) { \
|
|
|
|
|
using elem_type = cutlass::half_t; \
|
|
|
|
|
return __VA_ARGS__(); \
|
|
|
|
|
} else { \
|
|
|
|
|
using elem_type = cutlass::bfloat16_t; \
|
|
|
|
|
return __VA_ARGS__(); \
|
|
|
|
|
} \
|
|
|
|
|
}()
|
2023-07-17 20:26:11 +08:00
|
|
|
|
2024-01-22 06:42:55 +08:00
|
|
|
#define HEADDIM_SWITCH(HEADDIM, ...) \
|
2023-07-19 16:04:55 +08:00
|
|
|
[&] { \
|
|
|
|
|
if (HEADDIM <= 32) { \
|
|
|
|
|
constexpr static int kHeadDim = 32; \
|
|
|
|
|
return __VA_ARGS__(); \
|
|
|
|
|
} else if (HEADDIM <= 64) { \
|
|
|
|
|
constexpr static int kHeadDim = 64; \
|
|
|
|
|
return __VA_ARGS__(); \
|
|
|
|
|
} else if (HEADDIM <= 96) { \
|
|
|
|
|
constexpr static int kHeadDim = 96; \
|
|
|
|
|
return __VA_ARGS__(); \
|
|
|
|
|
} else if (HEADDIM <= 128) { \
|
|
|
|
|
constexpr static int kHeadDim = 128; \
|
|
|
|
|
return __VA_ARGS__(); \
|
|
|
|
|
} else if (HEADDIM <= 160) { \
|
|
|
|
|
constexpr static int kHeadDim = 160; \
|
|
|
|
|
return __VA_ARGS__(); \
|
|
|
|
|
} else if (HEADDIM <= 192) { \
|
|
|
|
|
constexpr static int kHeadDim = 192; \
|
|
|
|
|
return __VA_ARGS__(); \
|
|
|
|
|
} else if (HEADDIM <= 224) { \
|
|
|
|
|
constexpr static int kHeadDim = 224; \
|
|
|
|
|
return __VA_ARGS__(); \
|
|
|
|
|
} else if (HEADDIM <= 256) { \
|
|
|
|
|
constexpr static int kHeadDim = 256; \
|
|
|
|
|
return __VA_ARGS__(); \
|
|
|
|
|
} \
|
|
|
|
|
}()
|