2022-07-10 09:39:02 +08:00
|
|
|
// 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
|
2023-01-07 06:40:58 +08:00
|
|
|
// and https://github.com/facebookresearch/xformers/blob/main/xformers/csrc/attention/cuda/fmha/gemm_kernel_utils.h#L8
|
2022-07-10 09:39:02 +08:00
|
|
|
|
|
|
|
|
#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-01-07 06:40:58 +08:00
|
|
|
/// BOOL_SWITCH(flag, BoolConst, ([&] {
|
2022-07-10 09:39:02 +08:00
|
|
|
/// some_function<BoolConst>(...);
|
2022-12-07 06:16:04 +08:00
|
|
|
/// }));
|
2022-07-10 09:39:02 +08:00
|
|
|
/// ```
|
2022-12-07 06:16:04 +08:00
|
|
|
/// We need "({" and "})" to make sure that the code is a single argument being passed to the macro.
|
2023-01-07 06:40:58 +08:00
|
|
|
#define BOOL_SWITCH(COND, CONST_NAME, F) \
|
|
|
|
|
{ \
|
|
|
|
|
if (COND) { \
|
|
|
|
|
constexpr bool CONST_NAME = true; \
|
|
|
|
|
F(); \
|
|
|
|
|
} else { \
|
|
|
|
|
constexpr bool CONST_NAME = false; \
|
|
|
|
|
F(); \
|
|
|
|
|
} \
|
2022-12-07 06:16:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// modified from BOOL_SWITCH
|
|
|
|
|
// because MSVC cannot handle std::conditional with constexpr variable
|
2023-01-07 06:40:58 +08:00
|
|
|
#define FP16_SWITCH(COND, F) \
|
|
|
|
|
{ \
|
|
|
|
|
if (COND) { \
|
|
|
|
|
using elem_type = __nv_bfloat16; \
|
|
|
|
|
F(); \
|
|
|
|
|
} else { \
|
|
|
|
|
using elem_type = __half; \
|
|
|
|
|
F(); \
|
|
|
|
|
} \
|
|
|
|
|
}
|