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_cm2.comp
| 1 | #version 450 |
| 2 | |
| 3 | #extension GL_EXT_control_flow_attributes : enable |
| 4 | #extension GL_EXT_shader_16bit_storage : require |
| 5 | |
| 6 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : require |
| 7 | #extension GL_EXT_shader_explicit_arithmetic_types_int8 : require |
| 8 | #extension GL_EXT_shader_explicit_arithmetic_types_int32 : require |
| 9 | #extension GL_EXT_shader_explicit_arithmetic_types_int16 : require |
| 10 | |
| 11 | #if defined(BFLOAT16) |
| 12 | #extension GL_EXT_bfloat16 : enable |
| 13 | #endif |
| 14 | |
| 15 | #extension GL_KHR_memory_scope_semantics : enable |
| 16 | #extension GL_KHR_cooperative_matrix : enable |
| 17 | #extension GL_NV_cooperative_matrix2 : enable |
| 18 | #ifdef GL_NV_cooperative_matrix_decode_vector |
| 19 | #extension GL_NV_cooperative_matrix_decode_vector : enable |
| 20 | #endif |
| 21 | #extension GL_EXT_buffer_reference2 : enable |
| 22 | #extension GL_EXT_shader_explicit_arithmetic_types_int64 : enable |
| 23 | #extension GL_KHR_shader_subgroup_ballot : enable |
| 24 | #extension GL_KHR_shader_subgroup_vote : enable |
| 25 | #extension GL_EXT_null_initializer : enable |
| 26 | |
| 27 | #include "types.glsl" |
| 28 | #include "flash_attn_base.glsl" |
| 29 | #if !defined(BFLOAT16) |
| 30 | #include "dequant_funcs_cm2.glsl" |
| 31 | #endif |
| 32 | |
| 33 | #ifdef GL_NV_cooperative_matrix_decode_vector |
| 34 | #define FA_GATHER_BS 4u |
| 35 | #else |
| 36 | #define FA_GATHER_BS 1u |
| 37 | #endif |
| 38 | |
| 39 | layout(buffer_reference, std430, buffer_reference_align = 1) buffer decodeBufFA_Byte { |
| 40 | uint8_t raw; |
| 41 | }; |
| 42 | |
| 43 | // buffer_reference stride = sizeof(struct) = FaBlockBytesK/V. |
| 44 | layout(buffer_reference, std430, buffer_reference_align = 1) buffer decodeBufFA_K { |
| 45 | uint8_t raw[FaBlockBytesK]; |
| 46 | }; |
| 47 | layout(buffer_reference, std430, buffer_reference_align = 1) buffer decodeBufFA_V { |
| 48 | uint8_t raw[FaBlockBytesV]; |
| 49 | }; |
| 50 | |
| 51 | #if !defined(BFLOAT16) |
| 52 | float16_t faDecodeK(const decodeBufFA_K bl_in, const uint blockCoords[2], const uint coordInBlock[2]) { |
| 53 | switch (FaTypeK) { |
| 54 | case GGML_TYPE_F32: return dequantFuncF32 (decodeBufF32 (bl_in), blockCoords, coordInBlock); |
| 55 | case GGML_TYPE_Q4_0: return dequantFuncQ4_0(decodeBufQ4_0(bl_in), blockCoords, coordInBlock); |
| 56 | case GGML_TYPE_Q4_1: return dequantFuncQ4_1(decodeBufQ4_1(bl_in), blockCoords, coordInBlock); |
| 57 | case GGML_TYPE_Q5_0: return dequantFuncQ5_0(decodeBufQ5_0(bl_in), blockCoords, coordInBlock); |
| 58 | case GGML_TYPE_Q5_1: return dequantFuncQ5_1(decodeBufQ5_1(bl_in), blockCoords, coordInBlock); |
| 59 | case GGML_TYPE_Q8_0: return dequantFuncQ8_0(decodeBufQ8_0(bl_in), blockCoords, coordInBlock); |
| 60 | case GGML_TYPE_IQ4_NL: return dequantFuncIQ4_NL(decodeBufIQ4_NL(bl_in), blockCoords, coordInBlock); |
| 61 | case GGML_TYPE_Q1_0: return dequantFuncQ1_0(decodeBufQ1_0(bl_in), blockCoords, coordInBlock); |
| 62 | default: return float16_t(0); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | float16_t faDecodeV(const decodeBufFA_V bl_in, const uint blockCoords[2], const uint coordInBlock[2]) { |
| 67 | switch (FaTypeV) { |
| 68 | case GGML_TYPE_F32: return dequantFuncF32 (decodeBufF32 (bl_in), blockCoords, coordInBlock); |
| 69 | case GGML_TYPE_Q4_0: return dequantFuncQ4_0(decodeBufQ4_0(bl_in), blockCoords, coordInBlock); |
| 70 | case GGML_TYPE_Q4_1: return dequantFuncQ4_1(decodeBufQ4_1(bl_in), blockCoords, coordInBlock); |
| 71 | case GGML_TYPE_Q5_0: return dequantFuncQ5_0(decodeBufQ5_0(bl_in), blockCoords, coordInBlock); |
| 72 | case GGML_TYPE_Q5_1: return dequantFuncQ5_1(decodeBufQ5_1(bl_in), blockCoords, coordInBlock); |
| 73 | case GGML_TYPE_Q8_0: return dequantFuncQ8_0(decodeBufQ8_0(bl_in), blockCoords, coordInBlock); |
| 74 | case GGML_TYPE_IQ4_NL: return dequantFuncIQ4_NL(decodeBufIQ4_NL(bl_in), blockCoords, coordInBlock); |
| 75 | case GGML_TYPE_Q1_0: return dequantFuncQ1_0(decodeBufQ1_0(bl_in), blockCoords, coordInBlock); |
| 76 | default: return float16_t(0); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // V=4 vector decode for K/V; dispatches to per-format _v decoders. |
| 81 | f16vec4 faDecodeKVector(const decodeBufFA_K bl_in, const uint blockCoords[2], const uint coordInBlock[2]) { |
| 82 | switch (FaTypeK) { |
| 83 | case GGML_TYPE_F32: return f16vec4(decodeBufF32(bl_in).block); |
| 84 | case GGML_TYPE_Q4_0: return dequantFuncQ4_0_v(decodeBufQ4_0(bl_in), blockCoords, coordInBlock); |
| 85 | case GGML_TYPE_Q4_1: return dequantFuncQ4_1_v(decodeBufQ4_1(bl_in), blockCoords, coordInBlock); |
| 86 | case GGML_TYPE_Q5_0: return dequantFuncQ5_0_v(decodeBufQ5_0(bl_in), blockCoords, coordInBlock); |
| 87 | case GGML_TYPE_Q5_1: return dequantFuncQ5_1_v(decodeBufQ5_1(bl_in), blockCoords, coordInBlock); |
| 88 | case GGML_TYPE_Q8_0: return dequantFuncQ8_0_v(decodeBufQ8_0(bl_in), blockCoords, coordInBlock); |
| 89 | case GGML_TYPE_IQ4_NL: return dequantFuncIQ4_NL_v(decodeBufIQ4_NL(bl_in), blockCoords, coordInBlock); |
| 90 | default: return f16vec4(0); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | f16vec4 faDecodeVVector(const decodeBufFA_V bl_in, const uint blockCoords[2], const uint coordInBlock[2]) { |
| 95 | switch (FaTypeV) { |
| 96 | case GGML_TYPE_F32: return f16vec4(decodeBufF32(bl_in).block); |
| 97 | case GGML_TYPE_Q4_0: return dequantFuncQ4_0_v(decodeBufQ4_0(bl_in), blockCoords, coordInBlock); |
| 98 | case GGML_TYPE_Q4_1: return dequantFuncQ4_1_v(decodeBufQ4_1(bl_in), blockCoords, coordInBlock); |
| 99 | case GGML_TYPE_Q5_0: return dequantFuncQ5_0_v(decodeBufQ5_0(bl_in), blockCoords, coordInBlock); |
| 100 | case GGML_TYPE_Q5_1: return dequantFuncQ5_1_v(decodeBufQ5_1(bl_in), blockCoords, coordInBlock); |
| 101 | case GGML_TYPE_Q8_0: return dequantFuncQ8_0_v(decodeBufQ8_0(bl_in), blockCoords, coordInBlock); |
| 102 | case GGML_TYPE_IQ4_NL: return dequantFuncIQ4_NL_v(decodeBufIQ4_NL(bl_in), blockCoords, coordInBlock); |
| 103 | default: return f16vec4(0); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | #ifdef GL_NV_cooperative_matrix_decode_vector |
| 108 | #define FADECODEK , faDecodeK, faDecodeKVector |
| 109 | #define FADECODEV , faDecodeV, faDecodeVVector |
| 110 | #else |
| 111 | #define FADECODEK , faDecodeK |
| 112 | #define FADECODEV , faDecodeV |
| 113 | #endif |
| 114 | #endif |
| 115 | |
| 116 | layout (binding = 0) readonly buffer Q {uint8_t data_q[];}; |
| 117 | layout (binding = 1) readonly buffer K {uint8_t data_k[];}; |
| 118 | layout (binding = 2) readonly buffer V {uint8_t data_v[];}; |
| 119 | layout (binding = 3) readonly buffer M {uint8_t data_m[];}; |
| 120 | |
| 121 | // Native 16-bit aliases for the sparse gather callbacks. |
| 122 | layout (binding = 1) readonly buffer K16 {FLOAT_TYPE data_k16[];}; |
| 123 | layout (binding = 2) readonly buffer V16 {FLOAT_TYPE data_v16[];}; |
| 124 | layout (binding = 3) readonly buffer MF16 {float16_t data_mf16[];}; |
| 125 | #ifdef GL_NV_cooperative_matrix_decode_vector |
| 126 | layout (binding = 1) readonly buffer K16V4 {FLOAT_TYPEV4 data_k16v4[];}; |
| 127 | layout (binding = 2) readonly buffer V16V4 {FLOAT_TYPEV4 data_v16v4[];}; |
| 128 | #endif |
| 129 | |
| 130 | // K/V/mask offsets in 16-bit elements for the current head/batch, set in main(). |
| 131 | uint32_t g_k_off_elem, g_v_off_elem, g_m_off_elem; |
| 132 | |
| 133 | FLOAT_TYPE faGatherK(const decodeBufFA_K bl_in, const uint32_t blockCoords[2], const uint32_t coordInBlock[2]) { |
| 134 | if (blockCoords[0] >= p.split_kv) { return FLOAT_TYPE(0.0); } |
| 135 | const int r = data_sparse[sparse_base + blockCoords[0]]; |
| 136 | if (r < 0) { return FLOAT_TYPE(0.0); } |
| 137 | #if !defined(BFLOAT16) |
| 138 | if (USE_DECODE_K) { |
| 139 | decodeBufFA_K block = decodeBufFA_K(decodeBufFA_Byte(bl_in) + uint64_t(uint(r) - blockCoords[0]) * k_stride * FaBlockBytesK); |
| 140 | return faDecodeK(block, blockCoords, coordInBlock); |
| 141 | } |
| 142 | #endif |
| 143 | return data_k16[g_k_off_elem + uint(r) * k_stride + blockCoords[1] * FA_GATHER_BS + coordInBlock[1]]; |
| 144 | } |
| 145 | |
| 146 | FLOAT_TYPE faGatherV(const decodeBufFA_V bl_in, const uint32_t blockCoords[2], const uint32_t coordInBlock[2]) { |
| 147 | if (blockCoords[0] >= p.split_kv) { return FLOAT_TYPE(0.0); } |
| 148 | const int r = data_sparse[sparse_base + blockCoords[0]]; |
| 149 | if (r < 0) { return FLOAT_TYPE(0.0); } |
| 150 | #if !defined(BFLOAT16) |
| 151 | if (USE_DECODE_V) { |
| 152 | decodeBufFA_V block = decodeBufFA_V(decodeBufFA_Byte(bl_in) + uint64_t(uint(r) - blockCoords[0]) * v_stride * FaBlockBytesV); |
| 153 | return faDecodeV(block, blockCoords, coordInBlock); |
| 154 | } |
| 155 | #endif |
| 156 | return data_v16[g_v_off_elem + uint(r) * v_stride + blockCoords[1] * FA_GATHER_BS + coordInBlock[1]]; |
| 157 | } |
| 158 | |
| 159 | #ifdef GL_NV_cooperative_matrix_decode_vector |
| 160 | FLOAT_TYPEV4 faGatherKVector(const decodeBufFA_K bl_in, const uint32_t blockCoords[2], const uint32_t coordInBlock[2]) { |
| 161 | if (blockCoords[0] >= p.split_kv) { return FLOAT_TYPEV4(0.0); } |
| 162 | const int r = data_sparse[sparse_base + blockCoords[0]]; |
| 163 | if (r < 0) { return FLOAT_TYPEV4(0.0); } |
| 164 | #if !defined(BFLOAT16) |
| 165 | if (USE_DECODE_K) { |
| 166 | decodeBufFA_K block = decodeBufFA_K(decodeBufFA_Byte(bl_in) + uint64_t(uint(r) - blockCoords[0]) * k_stride * FaBlockBytesK); |
| 167 | return faDecodeKVector(block, blockCoords, coordInBlock); |
| 168 | } |
| 169 | #endif |
| 170 | const uint32_t o = g_k_off_elem + uint(r) * k_stride + blockCoords[1] * FA_GATHER_BS + coordInBlock[1]; |
| 171 | return data_k16v4[o / 4]; |
| 172 | } |
| 173 | |
| 174 | FLOAT_TYPEV4 faGatherVVector(const decodeBufFA_V bl_in, const uint32_t blockCoords[2], const uint32_t coordInBlock[2]) { |
| 175 | if (blockCoords[0] >= p.split_kv) { return FLOAT_TYPEV4(0.0); } |
| 176 | const int r = data_sparse[sparse_base + blockCoords[0]]; |
| 177 | if (r < 0) { return FLOAT_TYPEV4(0.0); } |
| 178 | #if !defined(BFLOAT16) |
| 179 | if (USE_DECODE_V) { |
| 180 | decodeBufFA_V block = decodeBufFA_V(decodeBufFA_Byte(bl_in) + uint64_t(uint(r) - blockCoords[0]) * v_stride * FaBlockBytesV); |
| 181 | return faDecodeVVector(block, blockCoords, coordInBlock); |
| 182 | } |
| 183 | #endif |
| 184 | const uint32_t o = g_v_off_elem + uint(r) * v_stride + blockCoords[1] * FA_GATHER_BS + coordInBlock[1]; |
| 185 | return data_v16v4[o / 4]; |
| 186 | } |
| 187 | |
| 188 | #define FAGATHERK , faGatherK, faGatherKVector |
| 189 | #define FAGATHERV , faGatherV, faGatherVVector |
| 190 | #else |
| 191 | #define FAGATHERK , faGatherK |
| 192 | #define FAGATHERV , faGatherV |
| 193 | #endif |
| 194 | |
| 195 | // Add gathered mask to S (slope==1 since sparse requires max_bias==0). col = slot in block jblk. |
| 196 | ACC_TYPE faAddSparseMask(const uint32_t row, const uint32_t col, const ACC_TYPE elem, const uint32_t jblk) { |
| 197 | const float NEG = uintBitsToFloat(0xFEFFFFFF); |
| 198 | const uint32_t kvslot = jblk * Bc + col; |
| 199 | if (kvslot >= p.split_kv) { return ACC_TYPE(NEG); } |
| 200 | const int r = data_sparse[sparse_base + kvslot]; |
| 201 | return r < 0 ? ACC_TYPE(NEG) : elem + ACC_TYPE(data_mf16[g_m_off_elem + row * m_stride + uint(r)]); |
| 202 | } |
| 203 | |
| 204 | ACC_TYPE maxReduce(const in ACC_TYPE x, const in ACC_TYPE y) { |
| 205 | return max(x, y); |
| 206 | } |
| 207 | |
| 208 | float16_t maxReduceFp16(const in float16_t x, const in float16_t y) { |
| 209 | return max(x, y); |
| 210 | } |
| 211 | |
| 212 | ACC_TYPE smearReduce(const in ACC_TYPE x, const in ACC_TYPE y) { |
| 213 | return x; |
| 214 | } |
| 215 | |
| 216 | // Replace matrix elements >= numRows or numCols with 'replace' |
| 217 | ACC_TYPE replacePadding(const in uint32_t row, const in uint32_t col, const in ACC_TYPE elem, const in ACC_TYPE replace, const in uint32_t numRows, const in uint32_t numCols) { |
| 218 | if (row >= numRows || col >= numCols) { |
| 219 | return replace; |
| 220 | } |
| 221 | return elem; |
| 222 | } |
| 223 | |
| 224 | ACC_TYPE Exp(const in uint32_t row, const in uint32_t col, const in ACC_TYPE elem) |
| 225 | { |
| 226 | return exp(elem); |
| 227 | } |
| 228 | |
| 229 | ACC_TYPE Max(const in uint32_t row, const in uint32_t col, const in ACC_TYPE elem0, const in ACC_TYPE elem1) |
| 230 | { |
| 231 | return max(elem0, elem1); |
| 232 | } |
| 233 | |
| 234 | // Store the output when doing grouped query attention. |
| 235 | // Rows index by Q's dimension 2, and the first N rows are valid. |
| 236 | D_TYPE perElemOpGqaStore(const in uint32_t r, const in uint32_t c, const in D_TYPE elem, const in uint32_t o_offset, const in uint32_t iq2, const in uint32_t N) |
| 237 | { |
| 238 | if (r < N && c < HSV) { |
| 239 | uint32_t offset = (iq2 + r) * HSV + c; |
| 240 | data_o[o_offset + offset] = D_TYPE(elem); |
| 241 | } |
| 242 | return elem; |
| 243 | } |
| 244 | |
| 245 | // Store O values for non-GQA split_k. Rows are tokens, not heads. |
| 246 | D_TYPE perElemOpNonGqaSplitKStore(const in uint32_t r, const in uint32_t c, const in D_TYPE elem, const in uint32_t unused, const in uint32_t iq2, const in uint32_t N) { |
| 247 | uint32_t global_row = i * Br + r; |
| 248 | if (global_row < N && c < HSV) { |
| 249 | uint32_t o_off = HSV * p.ne1 |
| 250 | * (split_k_index + p.k_num * (global_row + p.ne2 * iq3)); |
| 251 | data_o[o_off + iq2 * HSV + c] = D_TYPE(elem); |
| 252 | } |
| 253 | return elem; |
| 254 | } |
| 255 | |
| 256 | // Store L/M values for non-GQA split_k. |
| 257 | ACC_TYPE perElemOpNonGqaSplitKStoreCol0(const in uint32_t r, const in uint32_t c, const in ACC_TYPE elem, const in uint32_t lm_base, const in uint32_t iq2, const in uint32_t N) { |
| 258 | uint32_t global_row = i * Br + r; |
| 259 | if (global_row < N && c == 0) { |
| 260 | uint32_t lm_off = HSV * p.ne1 * p.k_num * p.ne2 * p.ne3 |
| 261 | + p.ne1 * 2 * (split_k_index + p.k_num * (global_row + p.ne2 * iq3)); |
| 262 | data_o[lm_off + lm_base + iq2] = D_TYPE(elem); |
| 263 | } |
| 264 | return elem; |
| 265 | } |
| 266 | |
| 267 | void main() { |
| 268 | #ifdef NEEDS_INIT_IQ_SHMEM |
| 269 | if (fa_type_needs_shmem(FaTypeK) || fa_type_needs_shmem(FaTypeV)) { |
| 270 | init_iq_shmem(gl_WorkGroupSize); |
| 271 | } |
| 272 | #endif |
| 273 | |
| 274 | init_indices(); |
| 275 | |
| 276 | tensorLayoutNV<2, gl_CooperativeMatrixClampModeConstantNV> tensorLayoutQ = createTensorLayoutNV(2, gl_CooperativeMatrixClampModeConstantNV); |
| 277 | tensorLayoutNV<2, Clamp> tensorLayoutK = createTensorLayoutNV(2, Clamp); |
| 278 | tensorLayoutNV<2, Clamp> tensorLayoutV = createTensorLayoutNV(2, Clamp); |
| 279 | |
| 280 | tensorViewNV<2, false, 1, 0> tensorViewTranspose = createTensorViewNV(2, false, 1, 0); |
| 281 | |
| 282 | const uint bs_k = USE_SPARSE ? max(FA_GATHER_BS, BLOCK_SIZE_K) : BLOCK_SIZE_K; |
| 283 | const uint bs_v = USE_SPARSE ? max(FA_GATHER_BS, BLOCK_SIZE_V) : BLOCK_SIZE_V; |
| 284 | tensorLayoutK = setTensorLayoutBlockSizeNV(tensorLayoutK, 1, bs_k); |
| 285 | tensorLayoutV = setTensorLayoutBlockSizeNV(tensorLayoutV, 1, bs_v); |
| 286 | |
| 287 | // Sparse iterates n_kv_max (in split_kv); the decode callbacks remap each slot. |
| 288 | const uint32_t KV_iter = USE_SPARSE ? p.split_kv : KV; |
| 289 | tensorLayoutQ = setTensorLayoutDimensionNV(tensorLayoutQ, N, HSK); |
| 290 | tensorLayoutK = setTensorLayoutDimensionNV(tensorLayoutK, KV_iter, HSK); |
| 291 | tensorLayoutV = setTensorLayoutDimensionNV(tensorLayoutV, KV_iter, HSV); |
| 292 | |
| 293 | // hint to the compiler that strides are aligned for the aligned variant of the shader |
| 294 | if (Clamp != gl_CooperativeMatrixClampModeConstantNV) |
| 295 | { |
| 296 | q_stride &= ~7; |
| 297 | if (bs_k == 1u) { |
| 298 | k_stride &= ~7; |
| 299 | } |
| 300 | if (bs_v == 1u) { |
| 301 | v_stride &= ~7; |
| 302 | } |
| 303 | m_stride &= ~7; |
| 304 | } |
| 305 | tensorLayoutQ = setTensorLayoutStrideNV(tensorLayoutQ, q_stride, 1); |
| 306 | tensorLayoutK = setTensorLayoutStrideNV(tensorLayoutK, k_stride, 1); |
| 307 | tensorLayoutV = setTensorLayoutStrideNV(tensorLayoutV, v_stride, 1); |
| 308 | |
| 309 | coopmat<Q_TYPE, gl_ScopeWorkgroup, Br, HSK_pad, gl_MatrixUseAccumulator> Q; |
| 310 | coopmat<FLOAT_TYPE, gl_ScopeWorkgroup, Br, HSK_pad, gl_MatrixUseA> Qf16; |
| 311 | |
| 312 | uint32_t q_offset = gqa_iq1*p.nb01*4/*sizeof(float)*/ + iq2*p.nb02+iq3*p.nb03; |
| 313 | coopMatLoadTensorNV(Q, data_q, q_offset, sliceTensorLayoutNV(tensorLayoutQ, i * Br, Br, 0, HSK_pad)); |
| 314 | |
| 315 | Q *= Q_TYPE(p.scale); |
| 316 | Qf16 = coopmat<FLOAT_TYPE, gl_ScopeWorkgroup, Br, HSK_pad, gl_MatrixUseA>(Q); |
| 317 | |
| 318 | coopmat<O_TYPE, gl_ScopeWorkgroup, Br, HSV_pad, gl_MatrixUseAccumulator> O = coopmat<O_TYPE, gl_ScopeWorkgroup, Br, HSV_pad, gl_MatrixUseAccumulator>(0); |
| 319 | |
| 320 | coopmat<ACC_TYPE, gl_ScopeWorkgroup, Br, Bc, gl_MatrixUseAccumulator> L, M; |
| 321 | |
| 322 | // Use -FLT_MAX/2 rather than -inf to reduce the possibility of NaNs, e.g. when computing Mold-M. |
| 323 | const float NEG_FLT_MAX_OVER_2 = uintBitsToFloat(0xFEFFFFFF); |
| 324 | |
| 325 | L = coopmat<ACC_TYPE, gl_ScopeWorkgroup, Br, Bc, gl_MatrixUseAccumulator>(0); |
| 326 | #if defined(ACC_TYPE_MAX) |
| 327 | M = coopmat<ACC_TYPE, gl_ScopeWorkgroup, Br, Bc, gl_MatrixUseAccumulator>(-ACC_TYPE_MAX / ACC_TYPE(2)); |
| 328 | #else |
| 329 | M = coopmat<ACC_TYPE, gl_ScopeWorkgroup, Br, Bc, gl_MatrixUseAccumulator>(NEG_FLT_MAX_OVER_2); |
| 330 | #endif |
| 331 | |
| 332 | coopmat<ACC_TYPE, gl_ScopeWorkgroup, Br, Bc, gl_MatrixUseAccumulator> slopeMat = coopmat<ACC_TYPE, gl_ScopeWorkgroup, Br, Bc, gl_MatrixUseAccumulator>(1.0); |
| 333 | |
| 334 | // ALiBi |
| 335 | if (p.max_bias > 0.0f) { |
| 336 | coopMatPerElementNV(slopeMat, slopeMat, perElemOpComputeSlope, iq2); |
| 337 | } |
| 338 | |
| 339 | const uint32_t mo_stride = CEIL_DIV(KV, 16 * Bc); |
| 340 | // mo_offset will point to the tile starting at row i*Br and col 0 |
| 341 | uint32_t mo_offset = mo_stride * i; |
| 342 | |
| 343 | uint32_t m_offset = gqa_iq1*KV * 2 /*sizeof(float16_t)*/; |
| 344 | if (p.nem2 != 1 || p.nem3 != 1) { |
| 345 | m_offset += ((iq3 % p.nem3) * p.nem2 + (iq2 % p.nem2)) * p.nem1 * KV * 2 /*sizeof(float16_t)*/; |
| 346 | mo_offset += ((iq3 % p.nem3) * p.nem2 + (iq2 % p.nem2)) * CEIL_DIV(p.nem1, Br) * mo_stride; |
| 347 | } |
| 348 | |
| 349 | g_k_off_elem = (ik2*p.nb12 + ik3*p.nb13) / 2; |
| 350 | g_v_off_elem = (iv2*p.nb22 + iv3*p.nb23) / 2; |
| 351 | g_m_off_elem = m_offset / 2; |
| 352 | |
| 353 | uint32_t mask_opt = 0; |
| 354 | uint32_t mask_opt_idx = ~0; |
| 355 | |
| 356 | [[dont_unroll]] |
| 357 | for (uint32_t j = start_j; j < end_j; ++j) { |
| 358 | |
| 359 | coopmat<float16_t, gl_ScopeWorkgroup, Br, Bc, gl_MatrixUseAccumulator> mv = coopmat<float16_t, gl_ScopeWorkgroup, Br, Bc, gl_MatrixUseAccumulator>(0); |
| 360 | if (MASK_ENABLE && !USE_SPARSE) { |
| 361 | |
| 362 | if (USE_MASK_OPT && mask_opt_idx != j / 16) { |
| 363 | mask_opt_idx = j / 16; |
| 364 | mask_opt = data_mask_opt[mo_offset + mask_opt_idx]; |
| 365 | } |
| 366 | uint32_t mask_opt_bits = (mask_opt >> ((j % 16) * 2)) & 0x3; |
| 367 | if (mask_opt_bits == MASK_OPT_ALL_NEG_INF) { |
| 368 | // skip this block |
| 369 | continue; |
| 370 | } |
| 371 | // Only load if the block is not all zeros |
| 372 | if (mask_opt_bits != MASK_OPT_ALL_ZERO) { |
| 373 | bool nem1_bounds_check = !(p.gqa_ratio > 1) && (p.nem1 % Br) != 0; |
| 374 | |
| 375 | if (nem1_bounds_check) { |
| 376 | tensorLayoutNV<2, gl_CooperativeMatrixClampModeConstantNV> tensorLayoutM = createTensorLayoutNV(2, gl_CooperativeMatrixClampModeConstantNV); |
| 377 | tensorLayoutM = setTensorLayoutDimensionNV(tensorLayoutM, p.nem1, KV); |
| 378 | tensorLayoutM = setTensorLayoutStrideNV(tensorLayoutM, m_stride, 1); |
| 379 | tensorLayoutM = setTensorLayoutClampValueNV(tensorLayoutM, 0xfc00); // -inf in float16_t |
| 380 | |
| 381 | coopmat<float16_t, gl_ScopeWorkgroup, Br, Bc, gl_MatrixUseAccumulator> mvmax; |
| 382 | |
| 383 | coopMatLoadTensorNV(mv, data_m, m_offset, sliceTensorLayoutNV(tensorLayoutM, i * Br, Br, j * Bc, Bc)); |
| 384 | // skip the block if the mask is entirely -inf |
| 385 | coopMatReduceNV(mvmax, mv, gl_CooperativeMatrixReduceRowAndColumnNV, maxReduceFp16); |
| 386 | if (mvmax[0] <= NEG_FLT_MAX_OVER_2) { |
| 387 | continue; |
| 388 | } |
| 389 | } else { |
| 390 | tensorLayoutNV<2, Clamp> tensorLayoutM = createTensorLayoutNV(2, Clamp); |
| 391 | // Don't clamp against nem1 when GQA is enabled |
| 392 | uint32_t m_height = p.gqa_ratio > 1 ? ~0 : p.nem1; |
| 393 | tensorLayoutM = setTensorLayoutDimensionNV(tensorLayoutM, m_height, KV); |
| 394 | tensorLayoutM = setTensorLayoutStrideNV(tensorLayoutM, m_stride, 1); |
| 395 | |
| 396 | coopmat<float16_t, gl_ScopeWorkgroup, Br, Bc, gl_MatrixUseAccumulator> mvmax; |
| 397 | |
| 398 | coopMatLoadTensorNV(mv, data_m, m_offset, sliceTensorLayoutNV(tensorLayoutM, i * Br, Br, j * Bc, Bc)); |
| 399 | // skip the block if the mask is entirely -inf |
| 400 | coopMatReduceNV(mvmax, mv, gl_CooperativeMatrixReduceRowAndColumnNV, maxReduceFp16); |
| 401 | if (mvmax[0] <= NEG_FLT_MAX_OVER_2) { |
| 402 | continue; |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | coopmat<ACC_TYPE, gl_ScopeWorkgroup, Br, Bc, gl_MatrixUseAccumulator> S = coopmat<ACC_TYPE, gl_ScopeWorkgroup, Br, Bc, gl_MatrixUseAccumulator>(0); |
| 409 | |
| 410 | coopmat<FLOAT_TYPE, gl_ScopeWorkgroup, HSK_pad, Bc, gl_MatrixUseB> K_T; |
| 411 | |
| 412 | uint32_t k_offset = ik2*p.nb12 + ik3*p.nb13; |
| 413 | // F16: bs_k==1 (direct load). F32: bs_k==4 (vec4 / dequantFuncF32). Quantized types: bs_k==32. |
| 414 | if (USE_SPARSE) { |
| 415 | coopMatLoadTensorNV(K_T, data_k, k_offset, sliceTensorLayoutNV(tensorLayoutK, j * Bc, Bc, 0, HSK_pad), tensorViewTranspose FAGATHERK); |
| 416 | #if !defined(BFLOAT16) |
| 417 | } else if (USE_DECODE_K) { |
| 418 | coopMatLoadTensorNV(K_T, data_k, k_offset, sliceTensorLayoutNV(tensorLayoutK, j * Bc, Bc, 0, HSK_pad), tensorViewTranspose FADECODEK); |
| 419 | #endif |
| 420 | } else { |
| 421 | coopMatLoadTensorNV(K_T, data_k, k_offset, sliceTensorLayoutNV(tensorLayoutK, j * Bc, Bc, 0, HSK_pad), tensorViewTranspose); |
| 422 | } |
| 423 | S = coopMatMulAdd(Qf16, K_T, S); |
| 424 | |
| 425 | if (LOGIT_SOFTCAP) { |
| 426 | [[unroll]] |
| 427 | for (int k = 0; k < S.length(); ++k) { |
| 428 | S[k] = ACC_TYPE(p.logit_softcap)*tanh(S[k]); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | if (MASK_ENABLE && USE_SPARSE) { |
| 433 | coopMatPerElementNV(S, S, faAddSparseMask, j); |
| 434 | } else if (MASK_ENABLE) { |
| 435 | S += slopeMat*coopmat<ACC_TYPE, gl_ScopeWorkgroup, Br, Bc, gl_MatrixUseAccumulator>(mv); |
| 436 | } |
| 437 | |
| 438 | // Clear padding elements to -inf, so they don't contribute to rowmax |
| 439 | if (Clamp != 0 && |
| 440 | ((j + 1) * Bc > KV || |
| 441 | (i + 1) * Br > N)) { |
| 442 | |
| 443 | uint R = ((i + 1) * Br > N) ? (N % Br) : Br; |
| 444 | uint C = ((j + 1) * Bc > KV) ? (KV % Bc) : Bc; |
| 445 | |
| 446 | coopMatPerElementNV(S, S, replacePadding, ACC_TYPE(NEG_FLT_MAX_OVER_2), R, C); |
| 447 | } |
| 448 | |
| 449 | coopmat<ACC_TYPE, gl_ScopeWorkgroup, Br, Bc, gl_MatrixUseAccumulator> rowmax, P, rowsum, eM; |
| 450 | |
| 451 | coopMatReduceNV(rowmax, S, gl_CooperativeMatrixReduceRowNV, maxReduce); |
| 452 | |
| 453 | rowmax += coopmat<ACC_TYPE, gl_ScopeWorkgroup, Br, Bc, gl_MatrixUseAccumulator>(FATTN_KQ_MAX_OFFSET); |
| 454 | |
| 455 | coopmat<ACC_TYPE, gl_ScopeWorkgroup, Br, Bc, gl_MatrixUseAccumulator> Mold = M; |
| 456 | |
| 457 | // M = max(rowmax, Mold) |
| 458 | // P = e^(S - M) |
| 459 | // eM = e^(Mold - M) |
| 460 | coopMatPerElementNV(M, rowmax, Max, Mold); |
| 461 | coopMatPerElementNV(P, S - M, Exp); |
| 462 | coopMatPerElementNV(eM, Mold - M, Exp); |
| 463 | |
| 464 | // Clear padding elements to 0, so they don't contribute to rowsum |
| 465 | if (Clamp != 0 && |
| 466 | ((j + 1) * Bc > KV || |
| 467 | (i + 1) * Br > N)) { |
| 468 | |
| 469 | uint R = ((i + 1) * Br > N) ? (N % Br) : Br; |
| 470 | uint C = ((j + 1) * Bc > KV) ? (KV % Bc) : Bc; |
| 471 | |
| 472 | coopMatPerElementNV(P, P, replacePadding, ACC_TYPE(0.0), R, C); |
| 473 | } |
| 474 | |
| 475 | coopmat<FLOAT_TYPE, gl_ScopeWorkgroup, Br, Bc, gl_MatrixUseA> P_A = coopmat<FLOAT_TYPE, gl_ScopeWorkgroup, Br, Bc, gl_MatrixUseA>(P); |
| 476 | |
| 477 | // compute rowsum by multiplying by matrix of all ones. |
| 478 | coopmat<FLOAT_TYPE, gl_ScopeWorkgroup, Bc, Bc, gl_MatrixUseB> One = coopmat<FLOAT_TYPE, gl_ScopeWorkgroup, Bc, Bc, gl_MatrixUseB>(1.0); |
| 479 | |
| 480 | rowsum = coopmat<ACC_TYPE, gl_ScopeWorkgroup, Br, Bc, gl_MatrixUseAccumulator>(0.0); |
| 481 | rowsum = coopMatMulAdd(P_A, One, rowsum); |
| 482 | |
| 483 | coopmat<FLOAT_TYPE, gl_ScopeWorkgroup, Bc, HSV_pad, gl_MatrixUseB> V; |
| 484 | uint32_t v_offset = iv2*p.nb22 + iv3*p.nb23; |
| 485 | if (USE_SPARSE) { |
| 486 | coopMatLoadTensorNV(V, data_v, v_offset, sliceTensorLayoutNV(tensorLayoutV, j * Bc, Bc, 0, HSV_pad) FAGATHERV); |
| 487 | #if !defined(BFLOAT16) |
| 488 | } else if (USE_DECODE_V) { |
| 489 | coopMatLoadTensorNV(V, data_v, v_offset, sliceTensorLayoutNV(tensorLayoutV, j * Bc, Bc, 0, HSV_pad) FADECODEV); |
| 490 | #endif |
| 491 | } else { |
| 492 | coopMatLoadTensorNV(V, data_v, v_offset, sliceTensorLayoutNV(tensorLayoutV, j * Bc, Bc, 0, HSV_pad)); |
| 493 | } |
| 494 | |
| 495 | L = eM*L + rowsum; |
| 496 | |
| 497 | // This is the "diagonal" matrix in the paper, but since we do componentwise |
| 498 | // multiply rather than matrix multiply it has the diagonal element smeared |
| 499 | // across the row |
| 500 | coopmat<ACC_TYPE, gl_ScopeWorkgroup, Br, HSV_pad, gl_MatrixUseAccumulator> eMdiag; |
| 501 | |
| 502 | // resize eM by using smear/reduce |
| 503 | coopMatReduceNV(eMdiag, eM, gl_CooperativeMatrixReduceRowNV, smearReduce); |
| 504 | |
| 505 | O *= coopmat<O_TYPE, gl_ScopeWorkgroup, Br, HSV_pad, gl_MatrixUseAccumulator>(eMdiag); |
| 506 | O = coopMatMulAdd(P_A, V, O); |
| 507 | } |
| 508 | |
| 509 | // If there is split_k, then the split_k resolve shader does the final |
| 510 | // division by L. Store the intermediate O value and per-row m and L values. |
| 511 | if (p.k_num > 1) { |
| 512 | coopmat<D_TYPE, gl_ScopeWorkgroup, Br, HSV_pad, gl_MatrixUseAccumulator> O_D = coopmat<D_TYPE, gl_ScopeWorkgroup, Br, HSV_pad, gl_MatrixUseAccumulator>(O); |
| 513 | |
| 514 | if (p.gqa_ratio > 1) { |
| 515 | // note: O and Q have swapped coord 1,2. |
| 516 | uint32_t o_offset = HSV * p.ne1 * (split_k_index + p.k_num * (gqa_iq1 + p.ne2 * iq3)); |
| 517 | coopMatPerElementNV(O_D, O_D, perElemOpGqaStore, o_offset, iq2, N); |
| 518 | |
| 519 | o_offset = HSV * p.ne1 * p.k_num * p.ne2 * p.ne3 + p.ne1 * 2 * (split_k_index + p.k_num * (gqa_iq1 + p.ne2 * iq3)); |
| 520 | coopMatPerElementNV(L, L, perElemOpStoreCol0, o_offset, iq2, N); |
| 521 | coopMatPerElementNV(M, M, perElemOpStoreCol0, o_offset + p.ne1, iq2, N); |
| 522 | } else { |
| 523 | coopMatPerElementNV(O_D, O_D, perElemOpNonGqaSplitKStore, 0u, iq2, N); |
| 524 | coopMatPerElementNV(L, L, perElemOpNonGqaSplitKStoreCol0, 0u, iq2, N); |
| 525 | coopMatPerElementNV(M, M, perElemOpNonGqaSplitKStoreCol0, p.ne1, iq2, N); |
| 526 | } |
| 527 | return; |
| 528 | } |
| 529 | |
| 530 | coopmat<ACC_TYPE, gl_ScopeWorkgroup, Br, HSV_pad, gl_MatrixUseAccumulator> Ldiag; |
| 531 | |
| 532 | // resize L by using smear/reduce |
| 533 | coopMatReduceNV(Ldiag, L, gl_CooperativeMatrixReduceRowNV, smearReduce); |
| 534 | |
| 535 | if ((p.mask_n_head_log2 & SINK_ENABLE_BIT) != 0) { |
| 536 | coopmat<ACC_TYPE, gl_ScopeWorkgroup, Br, HSV_pad, gl_MatrixUseAccumulator> S; |
| 537 | coopMatPerElementNV(S, S, perElemOpGetSink, iq2); |
| 538 | |
| 539 | coopmat<ACC_TYPE, gl_ScopeWorkgroup, Br, HSV_pad, gl_MatrixUseAccumulator> Mr; |
| 540 | |
| 541 | // resize M by using smear/reduce |
| 542 | coopMatReduceNV(Mr, M, gl_CooperativeMatrixReduceRowNV, smearReduce); |
| 543 | |
| 544 | // O, Ldiag, Mr all have the same type so all element locations match |
| 545 | [[unroll]] for (uint32_t i = 0; i < Ldiag.length(); ++i) { |
| 546 | ACC_TYPE sink = S[i]; |
| 547 | |
| 548 | ACC_TYPE ms = ACC_TYPE(1.0f); |
| 549 | ACC_TYPE vs = ACC_TYPE(1.0f); |
| 550 | |
| 551 | if (sink > Mr[i]) { |
| 552 | ms = exp(Mr[i] - sink); |
| 553 | |
| 554 | O[i] *= O_TYPE(ms); |
| 555 | } else { |
| 556 | vs = exp(sink - Mr[i]); |
| 557 | } |
| 558 | |
| 559 | Ldiag[i] = Ldiag[i]*ms + vs; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | [[unroll]] |
| 564 | for (int k = 0; k < Ldiag.length(); ++k) { |
| 565 | Ldiag[k] = (Ldiag[k] == 0.0) ? ACC_TYPE(0.0) : (ACC_TYPE(1.0) / Ldiag[k]); |
| 566 | } |
| 567 | |
| 568 | coopmat<D_TYPE, gl_ScopeWorkgroup, Br, HSV_pad, gl_MatrixUseAccumulator> O_D = coopmat<D_TYPE, gl_ScopeWorkgroup, Br, HSV_pad, gl_MatrixUseAccumulator>(O); |
| 569 | |
| 570 | O_D = coopmat<D_TYPE, gl_ScopeWorkgroup, Br, HSV_pad, gl_MatrixUseAccumulator>(Ldiag)*O_D; |
| 571 | |
| 572 | #if defined(ACC_TYPE_MAX) |
| 573 | [[unroll]] for (uint i = 0; i < O_D.length(); ++i) { O_D[i] = clamp(O_D[i], D_TYPE(-ACC_TYPE_MAX), D_TYPE(ACC_TYPE_MAX)); } |
| 574 | #endif |
| 575 | |
| 576 | uint32_t o_offset = gqa_iq1*p.ne1*HSV + iq3*p.ne2*p.ne1*HSV; |
| 577 | |
| 578 | if (p.gqa_ratio > 1) { |
| 579 | coopMatPerElementNV(O_D, O_D, perElemOpGqaStore, o_offset, iq2, N); |
| 580 | } else { |
| 581 | tensorLayoutNV<3, gl_CooperativeMatrixClampModeConstantNV> tensorLayoutD = createTensorLayoutNV(3, gl_CooperativeMatrixClampModeConstantNV); |
| 582 | tensorLayoutD = setTensorLayoutDimensionNV(tensorLayoutD, p.ne2, p.ne1, HSV); |
| 583 | |
| 584 | // permute dimensions |
| 585 | tensorViewNV<3, false, 1, 0, 2> tensorViewPermute = createTensorViewNV(3, false, 1, 0, 2); |
| 586 | |
| 587 | coopMatStoreTensorNV(O_D, data_o, o_offset, sliceTensorLayoutNV(tensorLayoutD, i * Br, Br, iq2, N, 0, HSV_pad), tensorViewPermute); |
| 588 | } |
| 589 | } |