static_switch.h 4.01 KB
Newer Older
zhangshao's avatar
zhangshao committed
1
2
3
4
5
6
7
8
9
10
11
#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__();                     \
    }                                           \
  }()

12
13
14
15
16
17
18
19
20
21
22
#define OPT_SWITCH(COND, ...)      \
  [&] {                                         \
    if (COND) {                                 \
      constexpr static int opt = 1;  \
      return __VA_ARGS__();                     \
    } else {                                    \
      constexpr static int opt = 2; \
      return __VA_ARGS__();                     \
    }                                           \
  }()

zhangshao's avatar
zhangshao committed
23
24
25
26
27
#define NUM_THREADS_SWITCH(NUM_THREAD, ...)    \
  [&] {                                         \
    if (NUM_THREAD == 256) {                   \
      constexpr static int NUM_THREADS = 256;  \
      return __VA_ARGS__();                     \
zhuwenwen's avatar
zhuwenwen committed
28
    } else {                                    \
29
      constexpr static int NUM_THREADS = 128;  \
zhuwenwen's avatar
zhuwenwen committed
30
      return __VA_ARGS__();                     \
zhangshao's avatar
zhangshao committed
31
32
33
34
35
    }                                           \
  }()

  #define HEADSIZE_SWITCH(HEADDIM, ...)   \
  [&] {                                    \
zhuwenwen's avatar
zhuwenwen committed
36
37
38
39
    if (HEADDIM == 32) {                   \
      constexpr static int HEAD_SIZE = 32;  \
      return __VA_ARGS__();                \
    } else if (HEADDIM == 64) {            \
zhangshao's avatar
zhangshao committed
40
41
42
43
44
45
46
47
48
49
50
51
      constexpr static int HEAD_SIZE = 64;  \
      return __VA_ARGS__();                \
    } else if (HEADDIM == 80) {            \
      constexpr static int HEAD_SIZE = 80;  \
      return __VA_ARGS__();                \
    } else if (HEADDIM == 96) {            \
      constexpr static int HEAD_SIZE = 96;  \
      return __VA_ARGS__();                \
    } else if (HEADDIM == 112) {           \
      constexpr static int HEAD_SIZE = 112; \
      return __VA_ARGS__();                \
    } else if (HEADDIM == 128) {           \
zhangshao's avatar
zhangshao committed
52
53
      constexpr static int HEAD_SIZE = 128; \
      return __VA_ARGS__();                \
zhuwenwen's avatar
zhuwenwen committed
54
55
56
    } else if (HEADDIM == 160) {           \
      constexpr static int HEAD_SIZE = 160; \
      return __VA_ARGS__();                \
57
58
59
    } else if (HEADDIM == 192) {           \
      constexpr static int HEAD_SIZE = 192; \
      return __VA_ARGS__();                \
zhangshao's avatar
zhangshao committed
60
61
62
63
64
    } else if (HEADDIM == 256) {           \
      constexpr static int HEAD_SIZE = 256; \
      return __VA_ARGS__();                \
    }                                      \
    else {                                 \
zhangshao's avatar
zhangshao committed
65
66
67
68
      TORCH_CHECK(false, "Unsupported head size: ", HEADDIM);\
    }                                      \
  }()

69
#define REUSEKV_SWITCH(num_blocks , ...)      \
zhangshao's avatar
zhangshao committed
70
[&] {                                                   \
71
    if (num_heads % 2 == 0 && num_heads / num_kv_heads >= 4 && num_blocks >= 1200){      \
zhuwenwen's avatar
zhuwenwen committed
72
73
        constexpr static int REUSE_KV_TIMES = 4;        \
        return __VA_ARGS__();                           \
74
    } else if (num_heads / num_kv_heads >= 2 && num_blocks >= 1200){\
zhangshao's avatar
zhangshao committed
75
76
        constexpr static int REUSE_KV_TIMES = 2;        \
        return __VA_ARGS__();                           \
77
    } else {                                            \
zhangshao's avatar
zhangshao committed
78
79
80
81
82
        constexpr static int REUSE_KV_TIMES = 1;        \
        return __VA_ARGS__();                           \
    }                                                   \
}()

83
#define REUSEKV_SWITCH_V1(num_blocks , ...)      \
zhuwenwen's avatar
zhuwenwen committed
84
[&] {                                                   \
85
86
    if (num_heads > num_kv_heads && num_blocks >= 1200){      \
        constexpr static int REUSE_KV_TIMES = 2;        \
zhuwenwen's avatar
zhuwenwen committed
87
88
        return __VA_ARGS__();                           \
    }  else {                                           \
89
        constexpr static int REUSE_KV_TIMES = 1;        \
zhuwenwen's avatar
zhuwenwen committed
90
91
        return __VA_ARGS__();                           \
    }                                                   \
92
93
}()