ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-vulkan / vulkan-shaders / flash_attn_base.glsl
| 1 | |
| 2 | layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in; |
| 3 | |
| 4 | layout (constant_id = 0) const uint32_t WorkGroupSize = 128; |
| 5 | layout (constant_id = 1) const uint32_t Br = 1; |
| 6 | layout (constant_id = 2) const uint32_t Bc = 32; |
| 7 | layout (constant_id = 3) const uint32_t HSK = 32; |
| 8 | layout (constant_id = 4) const uint32_t HSV = 32; |
| 9 | layout (constant_id = 5) const uint32_t Clamp = 0; |
| 10 | layout (constant_id = 6) const uint32_t D_split = 16; |
| 11 | layout (constant_id = 7) const uint32_t row_split = 1; |
| 12 | layout (constant_id = 8) const uint32_t SubGroupSize = 32; |
| 13 | layout (constant_id = 9) const uint32_t SHMEM_STAGING = 0; |
| 14 | layout (constant_id = 10) const uint32_t Flags = 0; |
| 15 | layout (constant_id = 11) const uint32_t LIMIT_OCCUPANCY_SHMEM = 0; |
| 16 | // ggml_type enumerant for K/V |
| 17 | layout (constant_id = 12) const uint32_t FaTypeK = 0; |
| 18 | layout (constant_id = 13) const uint32_t FaTypeV = 0; |
| 19 | // sizeof(decode buffer): quants -> ggml block size; F32 -> 16 (decodeBufF32 vec4). |
| 20 | layout (constant_id = 14) const uint32_t FaBlockBytesK = 2; |
| 21 | layout (constant_id = 15) const uint32_t FaBlockBytesV = 2; |
| 22 | |
| 23 | const bool USE_MASK_OPT = (Flags & 1) != 0; |
| 24 | const bool MASK_ENABLE = (Flags & 2) != 0; |
| 25 | const bool LOGIT_SOFTCAP = (Flags & 4) != 0; |
| 26 | const bool OLD_AMD_WINDOWS = (Flags & 8) != 0; |
| 27 | // Sparse: gather binding-7 indices instead of scanning [0,KV); p.split_kv = n_kv_max. |
| 28 | const bool USE_SPARSE = (Flags & 16) != 0; |
| 29 | |
| 30 | // Round up head sizes to a multiple of 16, for coopmat1/coopmat2 paths |
| 31 | const uint32_t HSK_pad = (HSK + 15) & ~15; |
| 32 | const uint32_t HSV_pad = (HSV + 15) & ~15; |
| 33 | |
| 34 | const bool KV_bounds_check = Clamp != 0; |
| 35 | |
| 36 | layout (push_constant) uniform parameter { |
| 37 | uint32_t N; |
| 38 | uint32_t KV; |
| 39 | |
| 40 | uint32_t ne1; |
| 41 | uint32_t ne2; |
| 42 | uint32_t ne3; |
| 43 | |
| 44 | uint32_t neq2; |
| 45 | uint32_t neq3; |
| 46 | uint32_t nek2; |
| 47 | uint32_t nek3; |
| 48 | uint32_t nev2; |
| 49 | uint32_t nev3; |
| 50 | uint32_t nem1; |
| 51 | uint32_t nem2; |
| 52 | uint32_t nem3; |
| 53 | |
| 54 | uint32_t nb01; |
| 55 | uint32_t nb02; |
| 56 | uint32_t nb03; |
| 57 | uint32_t nb11; |
| 58 | uint32_t nb12; |
| 59 | uint32_t nb13; |
| 60 | uint32_t nb21; |
| 61 | uint32_t nb22; |
| 62 | uint32_t nb23; |
| 63 | |
| 64 | float scale; |
| 65 | float max_bias; |
| 66 | float logit_softcap; |
| 67 | |
| 68 | uint32_t mask_n_head_log2; |
| 69 | float m0; |
| 70 | float m1; |
| 71 | |
| 72 | uint32_t gqa_ratio; |
| 73 | uint32_t split_kv; |
| 74 | uint32_t k_num; |
| 75 | } p; |
| 76 | |
| 77 | #define SINK_ENABLE_BIT (1<<24) |
| 78 | #define N_LOG2_MASK 0xFFFF |
| 79 | |
| 80 | layout (binding = 4) readonly buffer S {float data_s[];}; |
| 81 | |
| 82 | layout (binding = 5) writeonly buffer O {D_TYPE data_o[];}; |
| 83 | layout (binding = 5) writeonly buffer OV4 {D_TYPEV4 data_ov4[];}; |
| 84 | |
| 85 | layout (binding = 6) readonly buffer MO {uint32_t data_mask_opt[];}; |
| 86 | |
| 87 | layout (binding = 7) readonly buffer SP {int32_t data_sparse[];}; |
| 88 | |
| 89 | #define MASK_OPT_ALL_NEG_INF 1 |
| 90 | #define MASK_OPT_ALL_ZERO 2 |
| 91 | |
| 92 | #define BINDING_IDX_K 0 |
| 93 | #define BINDING_IDX_V 1 |
| 94 | |
| 95 | #include "fa_types.glsl" |
| 96 | |
| 97 | #if defined(BFLOAT16) |
| 98 | #define O_TYPE float |
| 99 | #define O_TYPEV4 vec4 |
| 100 | #else |
| 101 | #define O_TYPE FLOAT_TYPE |
| 102 | #define O_TYPEV4 FLOAT_TYPEV4 |
| 103 | #endif |
| 104 | |
| 105 | // These can't be `const` globals because GLSL forbids function calls in global |
| 106 | // const initializers, even when the spec constants would let the driver fold |
| 107 | // them. Macros expand at the use site and fold after specialization. |
| 108 | #define BLOCK_SIZE_K fa_block_elems(FaTypeK) |
| 109 | #define BLOCK_SIZE_V fa_block_elems(FaTypeV) |
| 110 | // F16 reads f16 elements directly from the binding; everything else routes |
| 111 | // through dequantize4 / the MMQ helpers to unpack from the packed block layout. |
| 112 | #define USE_DECODE_K (FaTypeK != GGML_TYPE_F16) |
| 113 | #define USE_DECODE_V (FaTypeV != GGML_TYPE_F16) |
| 114 | |
| 115 | #define CEIL_DIV(a, b) (((a) + (b) - 1) / (b)) |
| 116 | |
| 117 | |
| 118 | // Store column zero. This is used to save per-row m and L values for split_k. |
| 119 | ACC_TYPE perElemOpStoreCol0(const in uint32_t r, const in uint32_t c, const in ACC_TYPE elem, const in uint32_t o_offset, const in uint32_t iq2, const in uint32_t N) |
| 120 | { |
| 121 | if (r < N && c == 0) { |
| 122 | uint32_t offset = iq2 + r; |
| 123 | data_o[o_offset + offset] = D_TYPE(elem); |
| 124 | } |
| 125 | return elem; |
| 126 | } |
| 127 | |
| 128 | // Load the slope matrix, indexed by Q's dimension 2. |
| 129 | ACC_TYPE perElemOpComputeSlope(const in uint32_t r, const in uint32_t c, const in ACC_TYPE elem, const in uint32_t iq2) |
| 130 | { |
| 131 | const uint32_t h = iq2 + (r % p.gqa_ratio); |
| 132 | |
| 133 | uint32_t n_head_log2 = p.mask_n_head_log2 & N_LOG2_MASK; |
| 134 | |
| 135 | const ACC_TYPE base = ACC_TYPE(h < n_head_log2 ? p.m0 : p.m1); |
| 136 | const int exph = int(h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1); |
| 137 | |
| 138 | return ACC_TYPE(pow(base, ACC_TYPE(exph))); |
| 139 | } |
| 140 | |
| 141 | // Load the sink value, indexed by Q's dimension 2. |
| 142 | ACC_TYPE perElemOpGetSink(const in uint32_t r, const in uint32_t c, const in ACC_TYPE elem, const in uint32_t iq2) |
| 143 | { |
| 144 | const uint32_t h = iq2 + (r % p.gqa_ratio); |
| 145 | |
| 146 | return ACC_TYPE(data_s[h]); |
| 147 | } |
| 148 | |
| 149 | uint32_t i, N, KV, split_k_index, Tr, start_j, end_j, |
| 150 | gqa_iq1, iq2, iq3, rk2, rk3, rv2, rv3, ik2, ik3, iv2, iv3, |
| 151 | q_stride, k_stride, v_stride, m_stride, sparse_base; |
| 152 | |
| 153 | void init_indices() |
| 154 | { |
| 155 | N = p.N; |
| 156 | KV = p.KV; |
| 157 | |
| 158 | if (p.k_num > 1) { |
| 159 | if (p.gqa_ratio > 1) { |
| 160 | i = 0; |
| 161 | // batch and split_k share gl_WorkGroupID.x |
| 162 | gqa_iq1 = gl_WorkGroupID.x / p.k_num; |
| 163 | split_k_index = gl_WorkGroupID.x % p.k_num; |
| 164 | } else { |
| 165 | gqa_iq1 = 0; |
| 166 | split_k_index = gl_WorkGroupID.x % p.k_num; |
| 167 | i = gl_WorkGroupID.x / p.k_num; |
| 168 | } |
| 169 | } else if (p.gqa_ratio > 1) { |
| 170 | i = 0; |
| 171 | gqa_iq1 = gl_WorkGroupID.x; |
| 172 | split_k_index = 0; |
| 173 | } else { |
| 174 | i = gl_WorkGroupID.x; |
| 175 | gqa_iq1 = 0; |
| 176 | split_k_index = 0; |
| 177 | } |
| 178 | |
| 179 | Tr = CEIL_DIV(N, Br); |
| 180 | |
| 181 | start_j = split_k_index * p.split_kv / Bc; |
| 182 | end_j = CEIL_DIV(min(KV, (split_k_index + 1) * p.split_kv), Bc); |
| 183 | |
| 184 | // When not using grouped query attention, all rows share the same iq2, equal to gl_WorkGroupID.y. |
| 185 | // When using grouped query attention, each workgroup does gqa_ratio consecutive values of iq2. |
| 186 | iq2 = gl_WorkGroupID.y * p.gqa_ratio; |
| 187 | iq3 = gl_WorkGroupID.z; |
| 188 | |
| 189 | // broadcast factors |
| 190 | rk2 = p.neq2/p.nek2; |
| 191 | rk3 = p.neq3/p.nek3; |
| 192 | |
| 193 | rv2 = p.neq2/p.nev2; |
| 194 | rv3 = p.neq3/p.nev3; |
| 195 | |
| 196 | // k indices |
| 197 | ik3 = iq3 / rk3; |
| 198 | ik2 = iq2 / rk2; |
| 199 | |
| 200 | // v indices |
| 201 | iv3 = iq3 / rv3; |
| 202 | iv2 = iq2 / rv2; |
| 203 | |
| 204 | // nb?1 are already divided by the type size and are in units of elements. |
| 205 | // When using grouped query attention, Q is indexed by iq2, so the stride |
| 206 | // should be nb02 (which is in bytes). |
| 207 | q_stride = p.gqa_ratio > 1 ? (p.nb02 / 4) : p.nb01; |
| 208 | k_stride = p.nb11; |
| 209 | v_stride = p.nb21; |
| 210 | // When using grouped query attention, all rows use the same mask (stride 0). |
| 211 | // "p.gqa_ratio >> 16" is just a roundabout way of writing zero |
| 212 | // that prevents the compiler from folding the "&" through the select |
| 213 | // and breaking the alignment detection. |
| 214 | m_stride = (p.gqa_ratio > 1) ? (p.gqa_ratio >> 16) : KV; |
| 215 | |
| 216 | // Sparse: the tile shares one mask row (gqa heads, or Br==1). split_k |
| 217 | // partitions the n_kv_max blocks. |
| 218 | if (USE_SPARSE) { |
| 219 | uint32_t qrow = (p.gqa_ratio > 1) ? gqa_iq1 : (i * Br); |
| 220 | sparse_base = (((iq3 % p.nem3) * p.nem2 + (iq2 % p.nem2)) * p.nem1 + qrow) * p.split_kv; |
| 221 | |
| 222 | uint32_t total_blocks = CEIL_DIV(p.split_kv, Bc); |
| 223 | uint32_t per_blocks = CEIL_DIV(total_blocks, p.k_num); |
| 224 | start_j = min(split_k_index * per_blocks, total_blocks); |
| 225 | end_j = min((split_k_index + 1) * per_blocks, total_blocks); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // Resolve a linear KV slot to a real column; false for inactive (sparse padding/-1, or dense OOB). |
| 230 | bool fa_kv_index(uint lin, out uint kv_col) { |
| 231 | if (USE_SPARSE) { |
| 232 | if (lin >= p.split_kv) { |
| 233 | kv_col = 0; |
| 234 | return false; |
| 235 | } |
| 236 | int idx = data_sparse[sparse_base + lin]; |
| 237 | kv_col = idx >= 0 ? uint(idx) : 0; |
| 238 | return idx >= 0; |
| 239 | } |
| 240 | kv_col = lin; |
| 241 | return !KV_bounds_check || lin < KV; |
| 242 | } |
| 243 | |
| 244 | // Bias applied to softmax to stay in fp16 range. |
| 245 | // Based on ggml-cuda issue https://github.com/ggml-org/llama.cpp/issues/18606 |
| 246 | const float FATTN_KQ_MAX_OFFSET = 3.0f*0.6931f; |
| 247 | |
| 248 | // Store the output when doing grouped query attention. |
| 249 | // Rows index by Q's dimension 2, and the first N rows are valid. |
| 250 | void gqaStore(const in uint32_t r, const in uint32_t c, const in O_TYPEV4 elems, const in uint32_t o_offset, const in uint32_t iq2, const in uint32_t N) |
| 251 | { |
| 252 | uint32_t offset = (iq2 + r) * HSV / 4 + c; |
| 253 | data_ov4[o_offset + offset] = D_TYPEV4(elems); |
| 254 | } |