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 / mul_mm_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 | #extension GL_KHR_memory_scope_semantics : enable |
| 12 | #extension GL_KHR_cooperative_matrix : enable |
| 13 | #extension GL_NV_cooperative_matrix2 : enable |
| 14 | #ifdef GGML_VULKAN_COOPMAT2_DECODE_VECTOR |
| 15 | #extension GL_NV_cooperative_matrix_decode_vector : enable |
| 16 | #endif |
| 17 | #extension GL_EXT_buffer_reference : enable |
| 18 | #extension GL_KHR_shader_subgroup_ballot : enable |
| 19 | #extension GL_KHR_shader_subgroup_vote : enable |
| 20 | #ifdef DATA_A_BF16 |
| 21 | #extension GL_EXT_bfloat16 : enable |
| 22 | #endif |
| 23 | |
| 24 | #include "ggml_type_ids.glsl" |
| 25 | |
| 26 | #ifdef MULMAT_QUANT |
| 27 | layout (constant_id = 7) const uint MmTypeA = 0; |
| 28 | layout (constant_id = 8) const uint MmABlockBytes = 2; |
| 29 | #endif |
| 30 | |
| 31 | #include "types.glsl" |
| 32 | #include "utils.glsl" |
| 33 | |
| 34 | layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in; |
| 35 | |
| 36 | #define IS_MUL_MM2 1 |
| 37 | |
| 38 | layout (constant_id = 0) const uint BLOCK_SIZE = 256; |
| 39 | layout (constant_id = 1) const uint BM = 64; |
| 40 | layout (constant_id = 2) const uint BN = 64; |
| 41 | layout (constant_id = 3) const uint BK = 16; // Assumed to be 32 if working with a quant |
| 42 | |
| 43 | layout (constant_id = 4) const bool enable_smaller_matrices = false; |
| 44 | const uint BNover2 = BN / 2; |
| 45 | const uint BNover4 = enable_smaller_matrices ? (BN / 4) : (BN / 2); |
| 46 | layout (constant_id = 5) const uint ALIGNED = 0; |
| 47 | layout (constant_id = 6) const uint subgroup_size = 32; |
| 48 | #define GROUP_A_BUDGET (8u * 1024u * 1024u) |
| 49 | |
| 50 | #ifdef MULMAT_QUANT |
| 51 | |
| 52 | uint mm_quant_k() { |
| 53 | // Use ternary expressions instead of a switch to keep qk as a specialization-constant expression. |
| 54 | return (MmTypeA == GGML_TYPE_Q1_0) ? 128u : |
| 55 | (MmTypeA == GGML_TYPE_Q2_0) ? 64u : |
| 56 | ((MmTypeA == GGML_TYPE_Q4_0 || MmTypeA == GGML_TYPE_Q4_1 || |
| 57 | MmTypeA == GGML_TYPE_Q5_0 || MmTypeA == GGML_TYPE_Q5_1 || |
| 58 | MmTypeA == GGML_TYPE_Q8_0) ? 32u : 256u); |
| 59 | } |
| 60 | #endif |
| 61 | |
| 62 | layout (push_constant) uniform parameter |
| 63 | { |
| 64 | uint M; |
| 65 | uint N; |
| 66 | uint K; |
| 67 | uint stride_a; |
| 68 | uint stride_b; |
| 69 | uint stride_d; |
| 70 | |
| 71 | uint batch_stride_a; |
| 72 | uint batch_stride_b; |
| 73 | uint batch_stride_d; |
| 74 | |
| 75 | #ifdef MUL_MAT_ID |
| 76 | uint nei0; |
| 77 | uint nei1; |
| 78 | uint nbi1; |
| 79 | uint ne11; |
| 80 | uint n_experts; |
| 81 | uint row_ids_offset; |
| 82 | #else |
| 83 | uint base_work_group_z; |
| 84 | uint num_batches; |
| 85 | uint k_split; |
| 86 | uint ne02; |
| 87 | uint ne12; |
| 88 | uint broadcast2; |
| 89 | uint broadcast3; |
| 90 | // N dimension for the B matrix can be >= p.N |
| 91 | uint padded_N; |
| 92 | #endif |
| 93 | } p; |
| 94 | |
| 95 | |
| 96 | #ifndef MULMAT_QUANT |
| 97 | layout (binding = 0) readonly buffer A {A_TYPE data_a[];}; |
| 98 | #else |
| 99 | layout (binding = 0) readonly buffer A {uint8_t data_a[];}; |
| 100 | #endif |
| 101 | layout (binding = 1) readonly buffer B {B_TYPE data_b[];}; |
| 102 | layout (binding = 2) writeonly buffer D {D_TYPE data_d[];}; |
| 103 | #if defined(MUL_MAT_ID) && defined(GGML_VULKAN_COOPMAT2_DECODE_VECTOR) |
| 104 | layout (binding = 1) readonly buffer B4 {B_TYPEV4 data_b_v4[];}; |
| 105 | #endif |
| 106 | |
| 107 | #if defined(MULMAT_QUANT) || QUANT_K > 1 |
| 108 | #include "dequant_funcs_cm2.glsl" |
| 109 | #ifndef MULMAT_QUANT |
| 110 | // Per-type path: use the alias set by dequant_funcs_cm2.glsl |
| 111 | #if defined(dequantFuncA_v) && defined(GGML_VULKAN_COOPMAT2_DECODE_VECTOR) |
| 112 | #define DECODEFUNCA , dequantFuncA, dequantFuncA_v |
| 113 | #else |
| 114 | #define DECODEFUNCA , dequantFuncA |
| 115 | #endif |
| 116 | #else |
| 117 | layout(buffer_reference, std430, buffer_reference_align = 1) buffer decodeBufA { |
| 118 | uint8_t raw[MmABlockBytes]; |
| 119 | }; |
| 120 | |
| 121 | float16_t mmDecodeA(const in decodeBufA bl_in, const in uint blockCoords[2], const in uint coordInBlock[2]) { |
| 122 | switch (MmTypeA) { |
| 123 | case GGML_TYPE_Q1_0: return dequantFuncQ1_0 (decodeBufQ1_0 (bl_in), blockCoords, coordInBlock); |
| 124 | case GGML_TYPE_Q2_0: return dequantFuncQ2_0 (decodeBufQ2_0 (bl_in), blockCoords, coordInBlock); |
| 125 | case GGML_TYPE_Q4_0: return dequantFuncQ4_0 (decodeBufQ4_0 (bl_in), blockCoords, coordInBlock); |
| 126 | case GGML_TYPE_Q4_1: return dequantFuncQ4_1 (decodeBufQ4_1 (bl_in), blockCoords, coordInBlock); |
| 127 | case GGML_TYPE_Q5_0: return dequantFuncQ5_0 (decodeBufQ5_0 (bl_in), blockCoords, coordInBlock); |
| 128 | case GGML_TYPE_Q5_1: return dequantFuncQ5_1 (decodeBufQ5_1 (bl_in), blockCoords, coordInBlock); |
| 129 | case GGML_TYPE_Q8_0: return dequantFuncQ8_0 (decodeBufQ8_0 (bl_in), blockCoords, coordInBlock); |
| 130 | case GGML_TYPE_Q2_K: return dequantFuncQ2_K (decodeBufQ2_K (bl_in), blockCoords, coordInBlock); |
| 131 | case GGML_TYPE_Q3_K: return dequantFuncQ3_K (decodeBufQ3_K (bl_in), blockCoords, coordInBlock); |
| 132 | case GGML_TYPE_Q6_K: return dequantFuncQ6_K (decodeBufQ6_K (bl_in), blockCoords, coordInBlock); |
| 133 | case GGML_TYPE_TQ1_0: return dequantFuncTQ1_0(decodeBufTQ1_0(bl_in), blockCoords, coordInBlock); |
| 134 | case GGML_TYPE_TQ2_0: return dequantFuncTQ2_0(decodeBufTQ2_0(bl_in), blockCoords, coordInBlock); |
| 135 | default: return float16_t(0); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | #ifdef GGML_VULKAN_COOPMAT2_DECODE_VECTOR |
| 140 | f16vec4 mmDecodeA_v(const in decodeBufA bl_in, const in uint blockCoords[2], const in uint coordInBlock[2]) { |
| 141 | switch (MmTypeA) { |
| 142 | case GGML_TYPE_Q1_0: return dequantFuncQ1_0_v (decodeBufQ1_0 (bl_in), blockCoords, coordInBlock); |
| 143 | case GGML_TYPE_Q2_0: return dequantFuncQ2_0_v (decodeBufQ2_0 (bl_in), blockCoords, coordInBlock); |
| 144 | case GGML_TYPE_Q4_0: return dequantFuncQ4_0_v (decodeBufQ4_0 (bl_in), blockCoords, coordInBlock); |
| 145 | case GGML_TYPE_Q4_1: return dequantFuncQ4_1_v (decodeBufQ4_1 (bl_in), blockCoords, coordInBlock); |
| 146 | case GGML_TYPE_Q5_0: return dequantFuncQ5_0_v (decodeBufQ5_0 (bl_in), blockCoords, coordInBlock); |
| 147 | case GGML_TYPE_Q5_1: return dequantFuncQ5_1_v (decodeBufQ5_1 (bl_in), blockCoords, coordInBlock); |
| 148 | case GGML_TYPE_Q8_0: return dequantFuncQ8_0_v (decodeBufQ8_0 (bl_in), blockCoords, coordInBlock); |
| 149 | case GGML_TYPE_Q2_K: return dequantFuncQ2_K_v (decodeBufQ2_K (bl_in), blockCoords, coordInBlock); |
| 150 | case GGML_TYPE_Q3_K: return dequantFuncQ3_K_v (decodeBufQ3_K (bl_in), blockCoords, coordInBlock); |
| 151 | case GGML_TYPE_Q6_K: return dequantFuncQ6_K_v (decodeBufQ6_K (bl_in), blockCoords, coordInBlock); |
| 152 | case GGML_TYPE_TQ1_0: return dequantFuncTQ1_0_v(decodeBufTQ1_0(bl_in), blockCoords, coordInBlock); |
| 153 | case GGML_TYPE_TQ2_0: return dequantFuncTQ2_0_v(decodeBufTQ2_0(bl_in), blockCoords, coordInBlock); |
| 154 | default: return f16vec4(0); |
| 155 | } |
| 156 | } |
| 157 | #define DECODEFUNCA , mmDecodeA, mmDecodeA_v |
| 158 | #else |
| 159 | #define DECODEFUNCA , mmDecodeA |
| 160 | #endif |
| 161 | #endif |
| 162 | #else |
| 163 | #define DECODEFUNCA |
| 164 | #endif |
| 165 | |
| 166 | #if !defined(fetch_scales) |
| 167 | #define fetch_scales(a, b, c, d, e, f) |
| 168 | #endif |
| 169 | #if !defined(store_scales) |
| 170 | #define store_scales(a) |
| 171 | #endif |
| 172 | |
| 173 | #if defined(DATA_A_BF16) |
| 174 | #define MAT_TYPE bfloat16_t |
| 175 | #else |
| 176 | #define MAT_TYPE FLOAT_TYPE |
| 177 | #endif |
| 178 | |
| 179 | #ifdef MUL_MAT_ID |
| 180 | layout (binding = 3) readonly buffer IDS {int data_ids[];}; |
| 181 | layout (binding = 4) readonly buffer RowMap {uint data_row_map[];}; |
| 182 | |
| 183 | shared u16vec4 row_ids[BN]; |
| 184 | |
| 185 | layout(buffer_reference, std430, buffer_reference_align = 2) buffer decodeBufB { |
| 186 | B_TYPE b[]; |
| 187 | }; |
| 188 | |
| 189 | uint _ne1; |
| 190 | |
| 191 | B_TYPE decodeFuncB(const in decodeBufB bl, const in uint blockCoords[2], const in uint coordInBlock[2]) |
| 192 | { |
| 193 | const uint row_i = blockCoords[0]; |
| 194 | |
| 195 | const u16vec4 row_idx = row_ids[row_i]; |
| 196 | #if defined(GGML_VULKAN_COOPMAT2_DECODE_VECTOR) |
| 197 | // The decode-vector path gives B a K-dimension tensor-layout block size of BK. |
| 198 | const uint k = blockCoords[1] * BK + coordInBlock[1]; |
| 199 | #else |
| 200 | const uint k = blockCoords[1]; |
| 201 | #endif |
| 202 | B_TYPE ret = data_b[row_idx.y * p.batch_stride_b + row_idx.x * p.stride_b + k]; |
| 203 | |
| 204 | return ret; |
| 205 | } |
| 206 | |
| 207 | #if defined(GGML_VULKAN_COOPMAT2_DECODE_VECTOR) |
| 208 | B_TYPEV4 decodeFuncB_v(const in decodeBufB bl, const in uint blockCoords[2], const in uint coordInBlock[2]) |
| 209 | { |
| 210 | const uint row_i = blockCoords[0]; |
| 211 | |
| 212 | const u16vec4 row_idx = row_ids[row_i]; |
| 213 | const uint k = blockCoords[1] * BK + coordInBlock[1]; |
| 214 | const uint base = row_idx.y * p.batch_stride_b + row_idx.x * p.stride_b + k; |
| 215 | |
| 216 | return data_b_v4[base >> 2]; |
| 217 | } |
| 218 | #define DECODEFUNCB , decodeFuncB, decodeFuncB_v |
| 219 | #else |
| 220 | #define DECODEFUNCB , decodeFuncB |
| 221 | #endif |
| 222 | |
| 223 | D_TYPE perElemOpD(const in uint32_t r, const in uint32_t c, const in D_TYPE elem, const in uint32_t ir, const in uint32_t ic) |
| 224 | { |
| 225 | uint dr = ir * BM + r; |
| 226 | uint dc = ic * BN + c; |
| 227 | |
| 228 | if (dr < p.M && dc < _ne1) { |
| 229 | uint row_i = c; |
| 230 | const u16vec4 row_idx = row_ids[row_i]; |
| 231 | data_d[row_idx.y * p.batch_stride_d + row_idx.z * p.stride_d + dr] = elem; |
| 232 | } |
| 233 | return elem; |
| 234 | } |
| 235 | |
| 236 | void load_row_ids(uint row_begin) { |
| 237 | for (uint i = gl_LocalInvocationIndex; i < _ne1; i += BLOCK_SIZE) { |
| 238 | const uint packed_row_id = data_row_map[p.row_ids_offset + row_begin + i]; |
| 239 | const uint ii0 = packed_row_id & 0xffffu; |
| 240 | const uint ii1 = packed_row_id >> 16; |
| 241 | row_ids[i] = u16vec4(fastmod(ii0, p.ne11), ii1, ii0, 0); |
| 242 | } |
| 243 | barrier(); |
| 244 | } |
| 245 | #endif |
| 246 | |
| 247 | #ifdef MULMAT_QUANT |
| 248 | #include "iq_shmem_init.glsl" |
| 249 | #endif |
| 250 | |
| 251 | void main() { |
| 252 | const uint tid = gl_LocalInvocationIndex; |
| 253 | #ifdef MUL_MAT_ID |
| 254 | const uint tile_idx = gl_WorkGroupID.y + gl_WorkGroupID.z * gl_NumWorkGroups.y; |
| 255 | if (tile_idx >= data_row_map[0]) { |
| 256 | return; |
| 257 | } |
| 258 | const uint expert_idx = data_row_map[1 + 2 * tile_idx]; |
| 259 | const uint row_begin = tile_idx * BN; |
| 260 | _ne1 = data_row_map[2 + 2 * tile_idx]; |
| 261 | const uint ic = 0; |
| 262 | #endif |
| 263 | |
| 264 | #ifdef MUL_MAT_ID |
| 265 | // initialize to row 0 so we don't need to bounds check |
| 266 | if (tid < BN) { |
| 267 | row_ids[tid] = u16vec4(0); |
| 268 | } |
| 269 | #if !defined(NEEDS_INIT_IQ_SHMEM) |
| 270 | barrier(); |
| 271 | #endif |
| 272 | #endif |
| 273 | |
| 274 | #if defined(NEEDS_INIT_IQ_SHMEM) || defined(MULMAT_QUANT) |
| 275 | init_iq_shmem(gl_WorkGroupSize); |
| 276 | #endif |
| 277 | |
| 278 | #ifndef MUL_MAT_ID |
| 279 | const uint batch_idx = gl_WorkGroupID.z + p.base_work_group_z; |
| 280 | |
| 281 | const uint i13 = batch_idx / p.ne12; |
| 282 | const uint i12 = batch_idx % p.ne12; |
| 283 | |
| 284 | const uint i03 = i13 / p.broadcast3; |
| 285 | const uint i02 = i12 / p.broadcast2; |
| 286 | |
| 287 | const uint batch_idx_a = i03 * p.ne02 + i02; |
| 288 | #endif |
| 289 | |
| 290 | const uint blocks_m = (p.M + BM - 1) / BM; |
| 291 | const uint ik = gl_WorkGroupID.x / blocks_m; |
| 292 | |
| 293 | #ifdef MUL_MAT_ID |
| 294 | const uint ir = gl_WorkGroupID.x % blocks_m; |
| 295 | #else |
| 296 | // L2-friendly workgroup scheduling |
| 297 | const uint blocks_n = (p.N + BN - 1) / BN; |
| 298 | #if defined(MULMAT_QUANT) |
| 299 | const uint a_panel_bytes = (BM * p.K / mm_quant_k()) * MmABlockBytes; |
| 300 | #elif defined(DATA_A_F32) |
| 301 | const uint a_panel_bytes = BM * p.K * 4; |
| 302 | #elif QUANT_K == 1 |
| 303 | const uint a_panel_bytes = BM * p.K * 2; |
| 304 | #else |
| 305 | const uint a_panel_bytes = (BM * p.K) / 2 + (BM * p.K) / 16; |
| 306 | #endif |
| 307 | const uint group_m = clamp(GROUP_A_BUDGET / max(a_panel_bytes, 1u), 1u, min(blocks_m, 32u)); |
| 308 | const uint tiles_per_group = group_m * blocks_n; |
| 309 | const uint lin = gl_WorkGroupID.y * blocks_m + (gl_WorkGroupID.x % blocks_m); |
| 310 | const uint group_id = lin / tiles_per_group; |
| 311 | const uint first_m = group_id * group_m; |
| 312 | const uint gsize = min(blocks_m - first_m, group_m); |
| 313 | const uint in_group = lin - group_id * tiles_per_group; |
| 314 | const uint ir = first_m + in_group % gsize; |
| 315 | const uint ic = in_group / gsize; |
| 316 | #endif |
| 317 | |
| 318 | #ifdef MUL_MAT_ID |
| 319 | load_row_ids(row_begin); |
| 320 | #endif |
| 321 | |
| 322 | #ifdef MUL_MAT_ID |
| 323 | uint start_k = 0; |
| 324 | const uint end_k = p.K; |
| 325 | #else |
| 326 | uint start_k = ik * p.k_split; |
| 327 | const uint end_k = min(p.K, (ik + 1) * p.k_split); |
| 328 | #endif |
| 329 | |
| 330 | #ifdef MULMAT_QUANT |
| 331 | const uint qk = mm_quant_k(); |
| 332 | #else |
| 333 | const uint qk = QUANT_K; |
| 334 | #endif |
| 335 | |
| 336 | #ifdef MUL_MAT_ID |
| 337 | uint pos_a = expert_idx * (p.batch_stride_a / qk); |
| 338 | uint pos_b = 0; |
| 339 | #else |
| 340 | uint pos_a = batch_idx_a * (p.batch_stride_a / qk); |
| 341 | uint pos_b = batch_idx * p.batch_stride_b; |
| 342 | uint pos_d = batch_idx * p.batch_stride_d + ik * p.batch_stride_d * p.num_batches; |
| 343 | #endif |
| 344 | |
| 345 | #ifdef MULMAT_QUANT |
| 346 | // pos_a is a byte offset into the raw buffer; strides stay in block units |
| 347 | pos_a *= MmABlockBytes; |
| 348 | #endif |
| 349 | |
| 350 | uint stride_a = p.stride_a / qk; |
| 351 | uint stride_b = p.stride_b; |
| 352 | |
| 353 | // Hint to the compiler that values are aligned (want 16B alignment). |
| 354 | // Quants are always block-aligned, no alignment needed. |
| 355 | if (ALIGNED != 0) { |
| 356 | #if !defined(MULMAT_QUANT) && QUANT_K == 1 |
| 357 | stride_a &= ~7; |
| 358 | #endif |
| 359 | stride_b &= ~7; |
| 360 | } |
| 361 | |
| 362 | // Create layouts for both clamped and unclamped accesses |
| 363 | tensorLayoutNV<2> tensorLayoutA = createTensorLayoutNV(2); |
| 364 | tensorLayoutNV<2, gl_CooperativeMatrixClampModeConstantNV> tensorLayoutAClamp = createTensorLayoutNV(2, gl_CooperativeMatrixClampModeConstantNV); |
| 365 | tensorLayoutNV<2> tensorLayoutB = createTensorLayoutNV(2); |
| 366 | #ifndef MUL_MAT_ID |
| 367 | tensorLayoutNV<2, gl_CooperativeMatrixClampModeConstantNV> tensorLayoutBClamp = createTensorLayoutNV(2, gl_CooperativeMatrixClampModeConstantNV); |
| 368 | #endif |
| 369 | tensorLayoutNV<2, gl_CooperativeMatrixClampModeConstantNV> tensorLayoutD = createTensorLayoutNV(2, gl_CooperativeMatrixClampModeConstantNV); |
| 370 | |
| 371 | tensorLayoutA = setTensorLayoutBlockSizeNV(tensorLayoutA, 1, qk); |
| 372 | tensorLayoutAClamp = setTensorLayoutBlockSizeNV(tensorLayoutAClamp, 1, qk); |
| 373 | #if defined(MUL_MAT_ID) && defined(GGML_VULKAN_COOPMAT2_DECODE_VECTOR) |
| 374 | tensorLayoutB = setTensorLayoutBlockSizeNV(tensorLayoutB, 1, BK); |
| 375 | #endif |
| 376 | |
| 377 | // Use end_k rather than p.K as the dimension because that's what |
| 378 | // we need to bound check against when using split_k. |
| 379 | tensorLayoutA = setTensorLayoutDimensionNV(tensorLayoutA, p.M, end_k); |
| 380 | #ifdef MUL_MAT_ID |
| 381 | // MUL_MAT_ID pads each B row to stride_b so partial K tiles read zeros without clamping. |
| 382 | tensorLayoutB = setTensorLayoutDimensionNV(tensorLayoutB, BN, p.stride_b); |
| 383 | #else |
| 384 | // Bounds check B against padded_N, but bounds check D against N. |
| 385 | tensorLayoutB = setTensorLayoutDimensionNV(tensorLayoutB, p.padded_N, end_k); |
| 386 | #endif |
| 387 | tensorLayoutD = setTensorLayoutDimensionNV(tensorLayoutD, p.N, p.M); |
| 388 | tensorLayoutAClamp = setTensorLayoutDimensionNV(tensorLayoutAClamp, p.M, end_k); |
| 389 | #ifndef MUL_MAT_ID |
| 390 | tensorLayoutBClamp = setTensorLayoutDimensionNV(tensorLayoutBClamp, p.padded_N, end_k); |
| 391 | #endif |
| 392 | |
| 393 | tensorLayoutD = setTensorLayoutStrideNV(tensorLayoutD, p.stride_d, 1); |
| 394 | |
| 395 | tensorViewNV<2, false, 1, 0> tensorViewTranspose = createTensorViewNV(2, false, 1, 0); |
| 396 | |
| 397 | #if !defined(MUL_MAT_ID) |
| 398 | |
| 399 | const uint START_ALIGN_K = 256; |
| 400 | // For Qi_K (block size 256), unroll whole 256 element tiles. |
| 401 | // For legacy quants (block size 32), unroll 8x. |
| 402 | const uint UNROLL_K = (qk == 256) ? 256 : (BK * 8); |
| 403 | const uint unroll_count = UNROLL_K / BK; |
| 404 | |
| 405 | // Detect a fast path where all loads are entirely in bounds and no clamping is required |
| 406 | if ((ir + 1) * BM <= p.M && (ic + 1) * BN <= p.padded_N && (start_k % START_ALIGN_K) == 0 && (end_k % BK) == 0 && |
| 407 | #if !defined(MULMAT_QUANT) && QUANT_K == 1 |
| 408 | (stride_a % 8) == 0 && |
| 409 | #endif |
| 410 | (stride_b % 8) == 0) { |
| 411 | // Hint to the compiler that values are aligned (want 16B alignment) |
| 412 | start_k &= ~(START_ALIGN_K-1); |
| 413 | stride_b &= ~7; |
| 414 | #if !defined(MULMAT_QUANT) && QUANT_K == 1 |
| 415 | stride_a &= ~7; |
| 416 | #endif |
| 417 | |
| 418 | tensorLayoutA = setTensorLayoutStrideNV(tensorLayoutA, stride_a, 1); |
| 419 | tensorLayoutB = setTensorLayoutStrideNV(tensorLayoutB, stride_b, 1); |
| 420 | |
| 421 | uint k_iters = (end_k - start_k) / UNROLL_K; |
| 422 | uint block_k = start_k; |
| 423 | |
| 424 | // fetch scale values for a tile of quants. These will be copied into shared memory. |
| 425 | // The fetches and stores are pipelined to hide the latency. |
| 426 | fetch_scales(ir * BM, pos_a, stride_a, start_k, tid, true); |
| 427 | |
| 428 | if (enable_smaller_matrices && ic * BN + BNover4 >= p.N) { |
| 429 | coopmat<ACC_TYPE, gl_ScopeWorkgroup, BM, BNover4, gl_MatrixUseAccumulator> sum = coopmat<ACC_TYPE, gl_ScopeWorkgroup, BM, BNover4, gl_MatrixUseAccumulator>(0.0); |
| 430 | for (uint i = 0; i < k_iters; ++i) { |
| 431 | |
| 432 | store_scales(tid); |
| 433 | if (block_k + UNROLL_K < end_k) { |
| 434 | fetch_scales(ir * BM, pos_a, stride_a, block_k + UNROLL_K, tid, true); |
| 435 | } |
| 436 | |
| 437 | // Manually partial unroll |
| 438 | [[unroll]] for (uint j = 0; j < unroll_count; ++j) { |
| 439 | coopmat<MAT_TYPE, gl_ScopeWorkgroup, BM, BK, gl_MatrixUseA> mat_a; |
| 440 | coopmat<MAT_TYPE, gl_ScopeWorkgroup, BK, BNover4, gl_MatrixUseB> mat_b; |
| 441 | |
| 442 | coopMatLoadTensorNV(mat_a, data_a, pos_a, sliceTensorLayoutNV(tensorLayoutA, ir * BM, BM, block_k, BK) DECODEFUNCA); |
| 443 | coopMatLoadTensorNV(mat_b, data_b, pos_b, sliceTensorLayoutNV(tensorLayoutB, ic * BN, BNover4, block_k, BK), tensorViewTranspose); |
| 444 | |
| 445 | sum = coopMatMulAdd(mat_a, mat_b, sum); |
| 446 | block_k += BK; |
| 447 | } |
| 448 | } |
| 449 | // Do any remaining iterations that were not unrolled |
| 450 | if (block_k < end_k) { |
| 451 | store_scales(tid); |
| 452 | } |
| 453 | while (block_k < end_k) { |
| 454 | coopmat<MAT_TYPE, gl_ScopeWorkgroup, BM, BK, gl_MatrixUseA> mat_a; |
| 455 | coopmat<MAT_TYPE, gl_ScopeWorkgroup, BK, BNover4, gl_MatrixUseB> mat_b; |
| 456 | |
| 457 | coopMatLoadTensorNV(mat_a, data_a, pos_a, sliceTensorLayoutNV(tensorLayoutA, ir * BM, BM, block_k, BK) DECODEFUNCA); |
| 458 | coopMatLoadTensorNV(mat_b, data_b, pos_b, sliceTensorLayoutNV(tensorLayoutB, ic * BN, BNover4, block_k, BK), tensorViewTranspose); |
| 459 | |
| 460 | sum = coopMatMulAdd(mat_a, mat_b, sum); |
| 461 | block_k += BK; |
| 462 | } |
| 463 | #if defined(ACC_TYPE_MAX) |
| 464 | [[unroll]] for (uint i = 0; i < sum.length(); ++i) { sum[i] = clamp(sum[i], -ACC_TYPE_MAX, ACC_TYPE_MAX); } |
| 465 | #endif |
| 466 | |
| 467 | coopmat<D_TYPE, gl_ScopeWorkgroup, BM, BNover4, gl_MatrixUseAccumulator> mat_d = coopmat<D_TYPE, gl_ScopeWorkgroup, BM, BNover4, gl_MatrixUseAccumulator>(sum); |
| 468 | |
| 469 | coopMatStoreTensorNV(mat_d, data_d, pos_d, sliceTensorLayoutNV(tensorLayoutD, ic * BN, BNover4, ir * BM, BM), tensorViewTranspose); |
| 470 | return; |
| 471 | } else if (enable_smaller_matrices && ic * BN + BNover2 >= p.N) { |
| 472 | coopmat<ACC_TYPE, gl_ScopeWorkgroup, BM, BNover2, gl_MatrixUseAccumulator> sum = coopmat<ACC_TYPE, gl_ScopeWorkgroup, BM, BNover2, gl_MatrixUseAccumulator>(0.0); |
| 473 | for (uint i = 0; i < k_iters; ++i) { |
| 474 | |
| 475 | store_scales(tid); |
| 476 | if (block_k + UNROLL_K < end_k) { |
| 477 | fetch_scales(ir * BM, pos_a, stride_a, block_k + UNROLL_K, tid, true); |
| 478 | } |
| 479 | |
| 480 | // Manually partial unroll |
| 481 | [[unroll]] for (uint j = 0; j < unroll_count; ++j) { |
| 482 | coopmat<MAT_TYPE, gl_ScopeWorkgroup, BM, BK, gl_MatrixUseA> mat_a; |
| 483 | coopmat<MAT_TYPE, gl_ScopeWorkgroup, BK, BNover2, gl_MatrixUseB> mat_b; |
| 484 | |
| 485 | coopMatLoadTensorNV(mat_a, data_a, pos_a, sliceTensorLayoutNV(tensorLayoutA, ir * BM, BM, block_k, BK) DECODEFUNCA); |
| 486 | coopMatLoadTensorNV(mat_b, data_b, pos_b, sliceTensorLayoutNV(tensorLayoutB, ic * BN, BNover2, block_k, BK), tensorViewTranspose); |
| 487 | |
| 488 | sum = coopMatMulAdd(mat_a, mat_b, sum); |
| 489 | block_k += BK; |
| 490 | } |
| 491 | } |
| 492 | // Do any remaining iterations that were not unrolled |
| 493 | if (block_k < end_k) { |
| 494 | store_scales(tid); |
| 495 | } |
| 496 | while (block_k < end_k) { |
| 497 | coopmat<MAT_TYPE, gl_ScopeWorkgroup, BM, BK, gl_MatrixUseA> mat_a; |
| 498 | coopmat<MAT_TYPE, gl_ScopeWorkgroup, BK, BNover2, gl_MatrixUseB> mat_b; |
| 499 | |
| 500 | coopMatLoadTensorNV(mat_a, data_a, pos_a, sliceTensorLayoutNV(tensorLayoutA, ir * BM, BM, block_k, BK) DECODEFUNCA); |
| 501 | coopMatLoadTensorNV(mat_b, data_b, pos_b, sliceTensorLayoutNV(tensorLayoutB, ic * BN, BNover2, block_k, BK), tensorViewTranspose); |
| 502 | |
| 503 | sum = coopMatMulAdd(mat_a, mat_b, sum); |
| 504 | block_k += BK; |
| 505 | } |
| 506 | #if defined(ACC_TYPE_MAX) |
| 507 | [[unroll]] for (uint i = 0; i < sum.length(); ++i) { sum[i] = clamp(sum[i], -ACC_TYPE_MAX, ACC_TYPE_MAX); } |
| 508 | #endif |
| 509 | |
| 510 | coopmat<D_TYPE, gl_ScopeWorkgroup, BM, BNover2, gl_MatrixUseAccumulator> mat_d = coopmat<D_TYPE, gl_ScopeWorkgroup, BM, BNover2, gl_MatrixUseAccumulator>(sum); |
| 511 | |
| 512 | coopMatStoreTensorNV(mat_d, data_d, pos_d, sliceTensorLayoutNV(tensorLayoutD, ic * BN, BNover2, ir * BM, BM), tensorViewTranspose); |
| 513 | return; |
| 514 | } else { |
| 515 | coopmat<ACC_TYPE, gl_ScopeWorkgroup, BM, BN, gl_MatrixUseAccumulator> sum = coopmat<ACC_TYPE, gl_ScopeWorkgroup, BM, BN, gl_MatrixUseAccumulator>(0.0); |
| 516 | |
| 517 | for (uint i = 0; i < k_iters; ++i) { |
| 518 | |
| 519 | store_scales(tid); |
| 520 | if (block_k + UNROLL_K < end_k) { |
| 521 | fetch_scales(ir * BM, pos_a, stride_a, block_k + UNROLL_K, tid, true); |
| 522 | } |
| 523 | |
| 524 | // Manually partial unroll |
| 525 | [[unroll]] for (uint j = 0; j < unroll_count; ++j) { |
| 526 | coopmat<MAT_TYPE, gl_ScopeWorkgroup, BM, BK, gl_MatrixUseA> mat_a; |
| 527 | coopmat<MAT_TYPE, gl_ScopeWorkgroup, BK, BN, gl_MatrixUseB> mat_b; |
| 528 | |
| 529 | coopMatLoadTensorNV(mat_a, data_a, pos_a, sliceTensorLayoutNV(tensorLayoutA, ir * BM, BM, block_k, BK) DECODEFUNCA); |
| 530 | coopMatLoadTensorNV(mat_b, data_b, pos_b, sliceTensorLayoutNV(tensorLayoutB, ic * BN, BN, block_k, BK), tensorViewTranspose); |
| 531 | |
| 532 | sum = coopMatMulAdd(mat_a, mat_b, sum); |
| 533 | block_k += BK; |
| 534 | } |
| 535 | } |
| 536 | // Do any remaining iterations that were not unrolled |
| 537 | if (block_k < end_k) { |
| 538 | store_scales(tid); |
| 539 | } |
| 540 | while (block_k < end_k) { |
| 541 | coopmat<MAT_TYPE, gl_ScopeWorkgroup, BM, BK, gl_MatrixUseA> mat_a; |
| 542 | coopmat<MAT_TYPE, gl_ScopeWorkgroup, BK, BN, gl_MatrixUseB> mat_b; |
| 543 | |
| 544 | coopMatLoadTensorNV(mat_a, data_a, pos_a, sliceTensorLayoutNV(tensorLayoutA, ir * BM, BM, block_k, BK) DECODEFUNCA); |
| 545 | coopMatLoadTensorNV(mat_b, data_b, pos_b, sliceTensorLayoutNV(tensorLayoutB, ic * BN, BN, block_k, BK), tensorViewTranspose); |
| 546 | |
| 547 | sum = coopMatMulAdd(mat_a, mat_b, sum); |
| 548 | block_k += BK; |
| 549 | } |
| 550 | #if defined(ACC_TYPE_MAX) |
| 551 | [[unroll]] for (uint i = 0; i < sum.length(); ++i) { sum[i] = clamp(sum[i], -ACC_TYPE_MAX, ACC_TYPE_MAX); } |
| 552 | #endif |
| 553 | |
| 554 | coopmat<D_TYPE, gl_ScopeWorkgroup, BM, BN, gl_MatrixUseAccumulator> mat_d = coopmat<D_TYPE, gl_ScopeWorkgroup, BM, BN, gl_MatrixUseAccumulator>(sum); |
| 555 | |
| 556 | coopMatStoreTensorNV(mat_d, data_d, pos_d, sliceTensorLayoutNV(tensorLayoutD, ic * BN, BN, ir * BM, BM), tensorViewTranspose); |
| 557 | return; |
| 558 | } |
| 559 | } else |
| 560 | #endif // !defined(MUL_MAT_ID) |
| 561 | { |
| 562 | tensorLayoutA = setTensorLayoutStrideNV(tensorLayoutA, stride_a, 1); |
| 563 | |
| 564 | tensorLayoutAClamp = setTensorLayoutStrideNV(tensorLayoutAClamp, stride_a, 1); |
| 565 | |
| 566 | tensorLayoutB = setTensorLayoutStrideNV(tensorLayoutB, stride_b, 1); |
| 567 | |
| 568 | #ifndef MUL_MAT_ID |
| 569 | tensorLayoutBClamp = setTensorLayoutStrideNV(tensorLayoutBClamp, stride_b, 1); |
| 570 | #endif |
| 571 | |
| 572 | uint k_iters = (end_k - start_k + BK - 1) / BK; |
| 573 | |
| 574 | fetch_scales(ir * BM, pos_a, stride_a, start_k, tid, false); |
| 575 | store_scales(tid); |
| 576 | |
| 577 | #ifdef MUL_MAT_ID |
| 578 | if (enable_smaller_matrices && ic * BN + BNover4 >= _ne1) { |
| 579 | coopmat<ACC_TYPE, gl_ScopeWorkgroup, BM, BNover4, gl_MatrixUseAccumulator> sum; |
| 580 | sum = coopmat<ACC_TYPE, gl_ScopeWorkgroup, BM, BNover4, gl_MatrixUseAccumulator>(0.0); |
| 581 | |
| 582 | [[dont_unroll]] |
| 583 | for (uint block_k = start_k, i = 0; i < k_iters; block_k += BK, ++i) { |
| 584 | |
| 585 | if ((block_k % qk) == 0) { |
| 586 | store_scales(tid); |
| 587 | } |
| 588 | if (block_k + BK < end_k && ((block_k + BK) % qk) == 0) { |
| 589 | fetch_scales(ir * BM, pos_a, stride_a, block_k + BK, tid, false); |
| 590 | } |
| 591 | |
| 592 | if ((ir + 1) * BM <= p.M && block_k + BK <= end_k) { |
| 593 | coopmat<MAT_TYPE, gl_ScopeWorkgroup, BM, BK, gl_MatrixUseA> mat_a; |
| 594 | coopmat<MAT_TYPE, gl_ScopeWorkgroup, BK, BNover4, gl_MatrixUseB> mat_b; |
| 595 | |
| 596 | coopMatLoadTensorNV(mat_a, data_a, pos_a, sliceTensorLayoutNV(tensorLayoutA, ir * BM, BM, block_k, BK) DECODEFUNCA); |
| 597 | coopMatLoadTensorNV(mat_b, data_b, pos_b, sliceTensorLayoutNV(tensorLayoutB, 0, BNover4, block_k, BK), tensorViewTranspose DECODEFUNCB); |
| 598 | |
| 599 | sum = coopMatMulAdd(mat_a, mat_b, sum); |
| 600 | } else { |
| 601 | coopmat<MAT_TYPE, gl_ScopeWorkgroup, BM, BK, gl_MatrixUseA> mat_a; |
| 602 | coopmat<MAT_TYPE, gl_ScopeWorkgroup, BK, BNover4, gl_MatrixUseB> mat_b; |
| 603 | |
| 604 | coopMatLoadTensorNV(mat_a, data_a, pos_a, sliceTensorLayoutNV(tensorLayoutAClamp, ir * BM, BM, block_k, BK) DECODEFUNCA); |
| 605 | coopMatLoadTensorNV(mat_b, data_b, pos_b, sliceTensorLayoutNV(tensorLayoutB, 0, BNover4, block_k, BK), tensorViewTranspose DECODEFUNCB); |
| 606 | |
| 607 | sum = coopMatMulAdd(mat_a, mat_b, sum); |
| 608 | } |
| 609 | } |
| 610 | #if defined(ACC_TYPE_MAX) |
| 611 | [[unroll]] for (uint i = 0; i < sum.length(); ++i) { sum[i] = clamp(sum[i], -ACC_TYPE_MAX, ACC_TYPE_MAX); } |
| 612 | #endif |
| 613 | |
| 614 | // Convert from ACC_TYPE to D_TYPE |
| 615 | coopmat<D_TYPE, gl_ScopeWorkgroup, BM, BNover4, gl_MatrixUseAccumulator> mat_d; |
| 616 | mat_d = coopmat<D_TYPE, gl_ScopeWorkgroup, BM, BNover4, gl_MatrixUseAccumulator>(sum); |
| 617 | |
| 618 | // Call callback to store each element, remapping row through shared memory |
| 619 | coopMatPerElementNV(mat_d, mat_d, perElemOpD, ir, ic); |
| 620 | return; |
| 621 | } |
| 622 | if (ic * BN + BNover2 >= _ne1) { |
| 623 | coopmat<ACC_TYPE, gl_ScopeWorkgroup, BM, BNover2, gl_MatrixUseAccumulator> sum; |
| 624 | sum = coopmat<ACC_TYPE, gl_ScopeWorkgroup, BM, BNover2, gl_MatrixUseAccumulator>(0.0); |
| 625 | |
| 626 | [[dont_unroll]] |
| 627 | for (uint block_k = start_k, i = 0; i < k_iters; block_k += BK, ++i) { |
| 628 | |
| 629 | if ((block_k % qk) == 0) { |
| 630 | store_scales(tid); |
| 631 | } |
| 632 | if (block_k + BK < end_k && ((block_k + BK) % qk) == 0) { |
| 633 | fetch_scales(ir * BM, pos_a, stride_a, block_k + BK, tid, false); |
| 634 | } |
| 635 | |
| 636 | if ((ir + 1) * BM <= p.M && block_k + BK <= end_k) { |
| 637 | coopmat<MAT_TYPE, gl_ScopeWorkgroup, BM, BK, gl_MatrixUseA> mat_a; |
| 638 | coopmat<MAT_TYPE, gl_ScopeWorkgroup, BK, BNover2, gl_MatrixUseB> mat_b; |
| 639 | |
| 640 | coopMatLoadTensorNV(mat_a, data_a, pos_a, sliceTensorLayoutNV(tensorLayoutA, ir * BM, BM, block_k, BK) DECODEFUNCA); |
| 641 | coopMatLoadTensorNV(mat_b, data_b, pos_b, sliceTensorLayoutNV(tensorLayoutB, 0, BNover2, block_k, BK), tensorViewTranspose DECODEFUNCB); |
| 642 | |
| 643 | sum = coopMatMulAdd(mat_a, mat_b, sum); |
| 644 | } else { |
| 645 | coopmat<MAT_TYPE, gl_ScopeWorkgroup, BM, BK, gl_MatrixUseA> mat_a; |
| 646 | coopmat<MAT_TYPE, gl_ScopeWorkgroup, BK, BNover2, gl_MatrixUseB> mat_b; |
| 647 | |
| 648 | coopMatLoadTensorNV(mat_a, data_a, pos_a, sliceTensorLayoutNV(tensorLayoutAClamp, ir * BM, BM, block_k, BK) DECODEFUNCA); |
| 649 | coopMatLoadTensorNV(mat_b, data_b, pos_b, sliceTensorLayoutNV(tensorLayoutB, 0, BNover2, block_k, BK), tensorViewTranspose DECODEFUNCB); |
| 650 | |
| 651 | sum = coopMatMulAdd(mat_a, mat_b, sum); |
| 652 | } |
| 653 | } |
| 654 | #if defined(ACC_TYPE_MAX) |
| 655 | [[unroll]] for (uint i = 0; i < sum.length(); ++i) { sum[i] = clamp(sum[i], -ACC_TYPE_MAX, ACC_TYPE_MAX); } |
| 656 | #endif |
| 657 | |
| 658 | // Convert from ACC_TYPE to D_TYPE |
| 659 | coopmat<D_TYPE, gl_ScopeWorkgroup, BM, BNover2, gl_MatrixUseAccumulator> mat_d; |
| 660 | mat_d = coopmat<D_TYPE, gl_ScopeWorkgroup, BM, BNover2, gl_MatrixUseAccumulator>(sum); |
| 661 | |
| 662 | // Call callback to store each element, remapping row through shared memory |
| 663 | coopMatPerElementNV(mat_d, mat_d, perElemOpD, ir, ic); |
| 664 | return; |
| 665 | } |
| 666 | #endif |
| 667 | coopmat<ACC_TYPE, gl_ScopeWorkgroup, BM, BN, gl_MatrixUseAccumulator> sum; |
| 668 | sum = coopmat<ACC_TYPE, gl_ScopeWorkgroup, BM, BN, gl_MatrixUseAccumulator>(0.0); |
| 669 | |
| 670 | [[dont_unroll]] |
| 671 | for (uint block_k = start_k, i = 0; i < k_iters; block_k += BK, ++i) { |
| 672 | |
| 673 | if ((block_k % qk) == 0) { |
| 674 | store_scales(tid); |
| 675 | } |
| 676 | if (block_k + BK < end_k && ((block_k + BK) % qk) == 0) { |
| 677 | fetch_scales(ir * BM, pos_a, stride_a, block_k + BK, tid, false); |
| 678 | } |
| 679 | |
| 680 | if ((ir + 1) * BM <= p.M && block_k + BK <= end_k) { |
| 681 | coopmat<MAT_TYPE, gl_ScopeWorkgroup, BM, BK, gl_MatrixUseA> mat_a; |
| 682 | coopmat<MAT_TYPE, gl_ScopeWorkgroup, BK, BN, gl_MatrixUseB> mat_b; |
| 683 | |
| 684 | coopMatLoadTensorNV(mat_a, data_a, pos_a, sliceTensorLayoutNV(tensorLayoutA, ir * BM, BM, block_k, BK) DECODEFUNCA); |
| 685 | #ifdef MUL_MAT_ID |
| 686 | coopMatLoadTensorNV(mat_b, data_b, pos_b, sliceTensorLayoutNV(tensorLayoutB, 0, BN, block_k, BK), tensorViewTranspose DECODEFUNCB); |
| 687 | #else |
| 688 | coopMatLoadTensorNV(mat_b, data_b, pos_b, sliceTensorLayoutNV(tensorLayoutBClamp, ic * BN, BN, block_k, BK), tensorViewTranspose); |
| 689 | #endif |
| 690 | |
| 691 | sum = coopMatMulAdd(mat_a, mat_b, sum); |
| 692 | } else { |
| 693 | coopmat<MAT_TYPE, gl_ScopeWorkgroup, BM, BK, gl_MatrixUseA> mat_a; |
| 694 | coopmat<MAT_TYPE, gl_ScopeWorkgroup, BK, BN, gl_MatrixUseB> mat_b; |
| 695 | |
| 696 | coopMatLoadTensorNV(mat_a, data_a, pos_a, sliceTensorLayoutNV(tensorLayoutAClamp, ir * BM, BM, block_k, BK) DECODEFUNCA); |
| 697 | #ifdef MUL_MAT_ID |
| 698 | coopMatLoadTensorNV(mat_b, data_b, pos_b, sliceTensorLayoutNV(tensorLayoutB, 0, BN, block_k, BK), tensorViewTranspose DECODEFUNCB); |
| 699 | #else |
| 700 | coopMatLoadTensorNV(mat_b, data_b, pos_b, sliceTensorLayoutNV(tensorLayoutBClamp, ic * BN, BN, block_k, BK), tensorViewTranspose); |
| 701 | #endif |
| 702 | |
| 703 | sum = coopMatMulAdd(mat_a, mat_b, sum); |
| 704 | } |
| 705 | } |
| 706 | #if defined(ACC_TYPE_MAX) |
| 707 | [[unroll]] for (uint i = 0; i < sum.length(); ++i) { sum[i] = clamp(sum[i], -ACC_TYPE_MAX, ACC_TYPE_MAX); } |
| 708 | #endif |
| 709 | |
| 710 | // Convert from ACC_TYPE to D_TYPE |
| 711 | coopmat<D_TYPE, gl_ScopeWorkgroup, BM, BN, gl_MatrixUseAccumulator> mat_d; |
| 712 | mat_d = coopmat<D_TYPE, gl_ScopeWorkgroup, BM, BN, gl_MatrixUseAccumulator>(sum); |
| 713 | |
| 714 | #ifdef MUL_MAT_ID |
| 715 | // Call callback to store each element, remapping row through shared memory |
| 716 | coopMatPerElementNV(mat_d, mat_d, perElemOpD, ir, ic); |
| 717 | #else |
| 718 | coopMatStoreTensorNV(mat_d, data_d, pos_d, sliceTensorLayoutNV(tensorLayoutD, ic * BN, BN, ir * BM, BM), tensorViewTranspose); |
| 719 | #endif |
| 720 | } |
| 721 | } |