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_mmq_cm1.comp
| 1 | #version 450 |
| 2 | |
| 3 | #extension GL_EXT_control_flow_attributes : enable |
| 4 | #extension GL_EXT_shader_16bit_storage : require |
| 5 | #extension GL_EXT_shader_explicit_arithmetic_types_int8 : require |
| 6 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : require |
| 7 | |
| 8 | #extension GL_KHR_shader_subgroup_basic : require |
| 9 | #extension GL_KHR_cooperative_matrix : require |
| 10 | #extension GL_KHR_memory_scope_semantics : enable |
| 11 | |
| 12 | #if defined(MUL_MAT_ID_USE_SUBGROUPS) |
| 13 | #extension GL_KHR_shader_subgroup_ballot : enable |
| 14 | #endif |
| 15 | |
| 16 | #ifdef MUL_MAT_ID |
| 17 | #extension GL_EXT_shader_explicit_arithmetic_types_int16 : require |
| 18 | #endif |
| 19 | |
| 20 | #include "types.glsl" |
| 21 | |
| 22 | #if defined(DATA_A_Q3_K) || defined(DATA_A_Q6_K) || defined(DATA_A_NVFP4) |
| 23 | #define KSCALES 2 |
| 24 | #else |
| 25 | #define KSCALES 1 |
| 26 | #endif |
| 27 | |
| 28 | layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in; |
| 29 | |
| 30 | layout (binding = 0) readonly buffer A {A_TYPE data_a[];}; |
| 31 | #if defined(A_TYPE_PACKED16) |
| 32 | layout (binding = 0) readonly buffer A_PACKED16 {A_TYPE_PACKED16 data_a_packed16[];}; |
| 33 | #endif |
| 34 | #if defined(A_TYPE_PACKED32) |
| 35 | layout (binding = 0) readonly buffer A_PACKED32 {A_TYPE_PACKED32 data_a_packed32[];}; |
| 36 | #endif |
| 37 | layout (binding = 1) readonly buffer B {block_q8_1_x4_packed128 data_b[];}; |
| 38 | layout (binding = 2) writeonly buffer D {D_TYPE data_d[];}; |
| 39 | |
| 40 | #ifdef MUL_MAT_ID |
| 41 | layout (binding = 3) readonly buffer IDS {int data_ids[];}; |
| 42 | layout (binding = 4) readonly buffer RowMap {uint data_row_map[];}; |
| 43 | #endif |
| 44 | |
| 45 | layout (push_constant) uniform parameter |
| 46 | { |
| 47 | uint M; |
| 48 | uint N; |
| 49 | uint K; |
| 50 | uint stride_a; |
| 51 | uint stride_b; |
| 52 | uint stride_d; |
| 53 | |
| 54 | uint batch_stride_a; |
| 55 | uint batch_stride_b; |
| 56 | uint batch_stride_d; |
| 57 | |
| 58 | #ifdef MUL_MAT_ID |
| 59 | uint nei0; |
| 60 | uint nei1; |
| 61 | uint nbi1; |
| 62 | uint ne11; |
| 63 | uint n_experts; |
| 64 | uint row_ids_offset; |
| 65 | #else |
| 66 | uint base_work_group_z; |
| 67 | uint num_batches; |
| 68 | uint k_split; |
| 69 | uint ne02; |
| 70 | uint ne12; |
| 71 | uint broadcast2; |
| 72 | uint broadcast3; |
| 73 | #endif |
| 74 | } p; |
| 75 | |
| 76 | layout (constant_id = 0) const uint BLOCK_SIZE = 256; |
| 77 | layout (constant_id = 1) const uint BM = 128; |
| 78 | layout (constant_id = 2) const uint BN = 128; |
| 79 | // layout (constant_id = 3) const uint BK = 32; |
| 80 | layout (constant_id = 4) const uint WM = 64; |
| 81 | layout (constant_id = 5) const uint WN = 32; |
| 82 | layout (constant_id = 7) const uint TM = 16; |
| 83 | layout (constant_id = 8) const uint TN = 16; |
| 84 | layout (constant_id = 9) const uint TK = 16; |
| 85 | layout (constant_id = 10) const uint WARP = 32; |
| 86 | layout (constant_id = 11) const uint DEVICE_ARCH = 0; // vk_device_architecture (ggml-vulkan.cpp) |
| 87 | #define VK_ARCH_AMD_RDNA4 5u |
| 88 | |
| 89 | #define BK 32 |
| 90 | #ifdef MUL_MAT_ID |
| 91 | #define BK_STEP 2 |
| 92 | #else |
| 93 | #define BK_STEP 4 |
| 94 | #endif |
| 95 | #define GROUP_A_BUDGET (16u * 1024u * 1024u) |
| 96 | |
| 97 | const uint QPITCH = BK_STEP * (BK / 4) + 4; |
| 98 | |
| 99 | shared uint32_t buf_a_qs[BM * QPITCH]; |
| 100 | #if defined(DATA_A_Q4_1) || defined(DATA_A_Q5_1) || defined(DATA_A_Q4_K) || defined(DATA_A_Q5_K) |
| 101 | shared vec2 buf_a_dm[BM * BK_STEP]; // .x = d, .y = m |
| 102 | #else |
| 103 | shared float buf_a_d[BM * BK_STEP * KSCALES]; |
| 104 | #endif |
| 105 | |
| 106 | shared uint32_t buf_b_qs[BN * QPITCH]; |
| 107 | shared float buf_b_d[BN * BK_STEP]; |
| 108 | |
| 109 | #if defined(DATA_A_Q4_1) || defined(DATA_A_Q5_1) || defined(DATA_A_Q4_K) || defined(DATA_A_Q5_K) |
| 110 | shared float buf_b_s[BN * BK_STEP]; |
| 111 | #endif |
| 112 | |
| 113 | #if defined(DATA_A_IQ4_NL) || defined(DATA_A_IQ4_XS) || defined(DATA_A_MXFP4) || defined(DATA_A_NVFP4) |
| 114 | shared int8_t cm1_kvalues[16]; |
| 115 | #endif |
| 116 | |
| 117 | #if defined(DATA_A_QUANT_K) || defined(DATA_A_IQ4_XS) || defined(DATA_A_NVFP4) |
| 118 | #define LOAD_VEC_A 8 |
| 119 | #else |
| 120 | #define LOAD_VEC_A (4 * QUANT_R) |
| 121 | #endif |
| 122 | #define LOAD_VEC_B 16 |
| 123 | |
| 124 | const uint CM_ELEMS = (TM * TN) / WARP; |
| 125 | #define ACC_BIAS_BITS 0x4B400000 |
| 126 | #define ACC_BIAS_F 12582912.0f |
| 127 | const bool USE_MAGIC_BIAS = WARP != 32; |
| 128 | |
| 129 | // Accumulator row for element e: RDNA4 blocked, RDNA3/3.5 interleaved. |
| 130 | uint cm_elem_row(uint e) { |
| 131 | const uint row_half = gl_SubgroupInvocationID / TN; |
| 132 | return (DEVICE_ARCH == VK_ARCH_AMD_RDNA4) ? (e + row_half * CM_ELEMS) : (row_half + 2u * e); |
| 133 | } |
| 134 | |
| 135 | // min_term = asymmetric-quant min*b_sum correction (0 for symmetric types). |
| 136 | ACC_TYPE cm1_accumulate(ACC_TYPE prev, int acc_e, float scale_a, float nbias_a, float scale_b, float min_term) { |
| 137 | if (USE_MAGIC_BIAS) { |
| 138 | const float t = fma(intBitsToFloat(acc_e), scale_a, nbias_a); |
| 139 | return ACC_TYPE(fma(t, scale_b, float(prev) + min_term)); |
| 140 | } |
| 141 | return prev + ACC_TYPE(fma(float(acc_e) * scale_a, scale_b, min_term)); |
| 142 | } |
| 143 | |
| 144 | #ifdef MUL_MAT_ID |
| 145 | #include "mul_mm_id_funcs.glsl" |
| 146 | #endif |
| 147 | |
| 148 | #include "mul_mmq_cm1_funcs.glsl" |
| 149 | |
| 150 | void main() { |
| 151 | #ifdef MUL_MAT_ID |
| 152 | const uint tile_idx = gl_WorkGroupID.y + gl_WorkGroupID.z * gl_NumWorkGroups.y; |
| 153 | if (tile_idx >= data_row_map[0]) { |
| 154 | return; |
| 155 | } |
| 156 | const uint expert_idx = data_row_map[1 + 2 * tile_idx]; |
| 157 | const uint row_begin = tile_idx * BN; |
| 158 | _ne1 = data_row_map[2 + 2 * tile_idx]; |
| 159 | const uint ic = 0; |
| 160 | #endif |
| 161 | #if defined(DATA_A_IQ4_NL) || defined(DATA_A_IQ4_XS) |
| 162 | if (gl_LocalInvocationIndex < 16u) { |
| 163 | cm1_kvalues[gl_LocalInvocationIndex] = kvalues_iq4nl_const[gl_LocalInvocationIndex]; |
| 164 | } |
| 165 | barrier(); |
| 166 | #elif defined(DATA_A_MXFP4) |
| 167 | if (gl_LocalInvocationIndex < 16u) { |
| 168 | cm1_kvalues[gl_LocalInvocationIndex] = kvalues_mxfp4_const[gl_LocalInvocationIndex]; |
| 169 | } |
| 170 | barrier(); |
| 171 | #elif defined(DATA_A_NVFP4) |
| 172 | if (gl_LocalInvocationIndex < 16u) { |
| 173 | cm1_kvalues[gl_LocalInvocationIndex] = kvalues_mxfp4_const[gl_LocalInvocationIndex]; |
| 174 | } |
| 175 | #if !defined(USE_OCP_FP4) |
| 176 | for (uint i = gl_LocalInvocationIndex; i < 256u; i += BLOCK_SIZE) { |
| 177 | ue4m3_fp32_lut[i] = ue4m3_to_fp32_build(i); |
| 178 | } |
| 179 | #endif |
| 180 | barrier(); |
| 181 | #endif |
| 182 | |
| 183 | const uint blocks_m = (p.M + BM - 1) / BM; |
| 184 | const uint ik = gl_WorkGroupID.x / blocks_m; |
| 185 | |
| 186 | #ifdef MUL_MAT_ID |
| 187 | const uint ir = gl_WorkGroupID.x % blocks_m; |
| 188 | #else |
| 189 | // L2-friendly workgroup scheduling |
| 190 | const uint blocks_n = (p.N + BN - 1) / BN; |
| 191 | #if defined(DATA_A_IQ4_XS) |
| 192 | const uint a_panel_bytes = (BM * p.K) / 2 + (BM * p.K) / 32; |
| 193 | #else |
| 194 | const uint a_panel_bytes = BM * p.K + (BM * p.K) / 16; |
| 195 | #endif |
| 196 | const uint group_m = clamp(GROUP_A_BUDGET / max(a_panel_bytes, 1u), 1u, min(blocks_m, 32u)); |
| 197 | const uint tiles_per_group = group_m * blocks_n; |
| 198 | const uint lin = gl_WorkGroupID.y * blocks_m + (gl_WorkGroupID.x % blocks_m); |
| 199 | const uint group_id = lin / tiles_per_group; |
| 200 | const uint first_m = group_id * group_m; |
| 201 | const uint gsize = min(blocks_m - first_m, group_m); |
| 202 | const uint in_group = lin - group_id * tiles_per_group; |
| 203 | const uint ir = first_m + in_group % gsize; |
| 204 | const uint ic = in_group / gsize; |
| 205 | #endif |
| 206 | |
| 207 | #ifndef MUL_MAT_ID |
| 208 | const uint batch_idx = gl_WorkGroupID.z + p.base_work_group_z; |
| 209 | |
| 210 | const uint i13 = batch_idx / p.ne12; |
| 211 | const uint i12 = batch_idx % p.ne12; |
| 212 | |
| 213 | const uint i03 = i13 / p.broadcast3; |
| 214 | const uint i02 = i12 / p.broadcast2; |
| 215 | |
| 216 | const uint batch_idx_a = i03 * p.ne02 + i02; |
| 217 | #endif |
| 218 | |
| 219 | const uint warp_i = gl_SubgroupID; |
| 220 | |
| 221 | const uint cms_per_row = WM / TM; |
| 222 | const uint cms_per_col = WN / TN; |
| 223 | |
| 224 | const uint warp_r = warp_i % (BM / WM); |
| 225 | const uint warp_c = warp_i / (BM / WM); |
| 226 | |
| 227 | const uint elem_col0 = gl_SubgroupInvocationID % TN; |
| 228 | |
| 229 | const uint loadr_a = gl_LocalInvocationID.x % (BK / LOAD_VEC_A); |
| 230 | const uint loadc_a = gl_LocalInvocationID.x / (BK / LOAD_VEC_A); |
| 231 | const uint loadr_b = gl_LocalInvocationID.x % (BK / LOAD_VEC_B); |
| 232 | const uint loadc_b = gl_LocalInvocationID.x / (BK / LOAD_VEC_B); |
| 233 | |
| 234 | const uint loadstride_a = BLOCK_SIZE * LOAD_VEC_A / BK; |
| 235 | const uint loadstride_b = BLOCK_SIZE * LOAD_VEC_B / BK; |
| 236 | |
| 237 | #ifdef MUL_MAT_ID |
| 238 | load_row_ids(row_begin); |
| 239 | #endif |
| 240 | |
| 241 | #ifdef MUL_MAT_ID |
| 242 | const uint start_k = 0; |
| 243 | const uint end_k = p.K; |
| 244 | #else |
| 245 | const uint start_k = ik * p.k_split; |
| 246 | const uint end_k = min(p.K, (ik + 1) * p.k_split); |
| 247 | #endif |
| 248 | |
| 249 | uint pos_a_ib = |
| 250 | #ifdef MUL_MAT_ID |
| 251 | expert_idx * (p.batch_stride_a / BK) + |
| 252 | #else |
| 253 | batch_idx_a * (p.batch_stride_a / BK) + |
| 254 | #endif |
| 255 | (ir * BM * p.stride_a + start_k) / BK; |
| 256 | #ifdef MUL_MAT_ID |
| 257 | uint pos_b_ib = 0; |
| 258 | #else |
| 259 | uint pos_b_ib = (batch_idx * p.batch_stride_b + ic * BN * p.stride_b + start_k) / BK; |
| 260 | #endif |
| 261 | |
| 262 | ACC_TYPE sums[cms_per_row * cms_per_col * CM_ELEMS]; |
| 263 | [[unroll]] for (uint i = 0; i < cms_per_row * cms_per_col * CM_ELEMS; i++) { |
| 264 | sums[i] = ACC_TYPE(0.0); |
| 265 | } |
| 266 | |
| 267 | // Double-buffering: prefetch registers |
| 268 | const uint A_LOADS = (BM + loadstride_a - 1) / loadstride_a; |
| 269 | const uint B_LOADS = (BN + loadstride_b - 1) / loadstride_b; |
| 270 | |
| 271 | block_a_prefetch pre_a[A_LOADS * BK_STEP]; |
| 272 | block_b_prefetch pre_b[B_LOADS * BK_STEP]; |
| 273 | |
| 274 | if (start_k < end_k) { |
| 275 | PREFETCH_BLOCK(start_k) |
| 276 | } |
| 277 | |
| 278 | const uint a_row0 = warp_r * WM; |
| 279 | const uint b_col0 = warp_c * WN; |
| 280 | #ifdef MUL_MAT_ID |
| 281 | const bool active_col_tile = ic * BN + b_col0 < _ne1; |
| 282 | #else |
| 283 | const bool active_col_tile = ic * BN + b_col0 < p.N; |
| 284 | #endif |
| 285 | |
| 286 | barrier(); |
| 287 | |
| 288 | for (uint block = start_k; block < end_k; block += BK * BK_STEP) { |
| 289 | STORE_BLOCK_TO_LDS(block) |
| 290 | |
| 291 | barrier(); |
| 292 | |
| 293 | pos_a_ib += BK_STEP; |
| 294 | pos_b_ib += BK_STEP; |
| 295 | |
| 296 | const uint next_block = block + BK * BK_STEP; |
| 297 | if (next_block < end_k) { |
| 298 | PREFETCH_BLOCK(next_block) |
| 299 | } |
| 300 | |
| 301 | if (active_col_tile) { |
| 302 | [[unroll]] for (uint ks = 0; ks < BK_STEP; ks++) { |
| 303 | const uint K_SUB = BK / TK; |
| 304 | |
| 305 | #if KSCALES == 2 |
| 306 | [[unroll]] for (uint h = 0; h < K_SUB; h++) { |
| 307 | [[unroll]] for (uint r = 0; r < cms_per_row; r++) { |
| 308 | coopmat<int8_t, gl_ScopeSubgroup, TM, TK, gl_MatrixUseA> cache_a; |
| 309 | coopMatLoad(cache_a, buf_a_qs, (a_row0 + r * TM) * QPITCH + ks * (BK / 4) + h * (TK / 4), QPITCH, gl_CooperativeMatrixLayoutRowMajor); |
| 310 | |
| 311 | float scale_a[CM_ELEMS]; |
| 312 | float nbias_a[CM_ELEMS]; |
| 313 | [[unroll]] for (uint e = 0; e < CM_ELEMS; e++) { |
| 314 | scale_a[e] = buf_a_d[(ks * KSCALES + h) * BM + a_row0 + r * TM + cm_elem_row(e)]; |
| 315 | if (USE_MAGIC_BIAS) { |
| 316 | nbias_a[e] = -ACC_BIAS_F * scale_a[e]; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | [[unroll]] for (uint c = 0; c < cms_per_col; c++) { |
| 321 | coopmat<int8_t, gl_ScopeSubgroup, TK, TN, gl_MatrixUseB> cache_b; |
| 322 | coopMatLoad(cache_b, buf_b_qs, (b_col0 + c * TN) * QPITCH + ks * (BK / 4) + h * (TK / 4), QPITCH, gl_CooperativeMatrixLayoutColumnMajor); |
| 323 | |
| 324 | const float scale_b_v = buf_b_d[ks * BN + b_col0 + c * TN + elem_col0]; |
| 325 | |
| 326 | coopmat<int32_t, gl_ScopeSubgroup, TM, TN, gl_MatrixUseAccumulator> acc = |
| 327 | coopmat<int32_t, gl_ScopeSubgroup, TM, TN, gl_MatrixUseAccumulator>( |
| 328 | USE_MAGIC_BIAS ? ACC_BIAS_BITS : 0); |
| 329 | acc = coopMatMulAdd(cache_a, cache_b, acc); |
| 330 | |
| 331 | const uint tile_idx = r * cms_per_col + c; |
| 332 | [[unroll]] for (uint e = 0; e < CM_ELEMS; e++) { |
| 333 | sums[tile_idx * CM_ELEMS + e] = cm1_accumulate( |
| 334 | sums[tile_idx * CM_ELEMS + e], int(acc[e]), |
| 335 | scale_a[e], nbias_a[e], scale_b_v, 0.0); |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | #elif defined(DATA_A_Q4_1) || defined(DATA_A_Q5_1) || defined(DATA_A_Q4_K) || defined(DATA_A_Q5_K) |
| 341 | // Preload all A/B fragments up front (ILP). |
| 342 | coopmat<int8_t, gl_ScopeSubgroup, TM, TK, gl_MatrixUseA> cache_a[cms_per_row * K_SUB]; |
| 343 | coopmat<int8_t, gl_ScopeSubgroup, TK, TN, gl_MatrixUseB> cache_b[cms_per_col * K_SUB]; |
| 344 | |
| 345 | [[unroll]] for (uint r = 0; r < cms_per_row; r++) { |
| 346 | [[unroll]] for (uint h = 0; h < K_SUB; h++) { |
| 347 | coopMatLoad(cache_a[r * K_SUB + h], buf_a_qs, (a_row0 + r * TM) * QPITCH + ks * (BK / 4) + h * (TK / 4), QPITCH, gl_CooperativeMatrixLayoutRowMajor); |
| 348 | } |
| 349 | } |
| 350 | [[unroll]] for (uint c = 0; c < cms_per_col; c++) { |
| 351 | [[unroll]] for (uint h = 0; h < K_SUB; h++) { |
| 352 | coopMatLoad(cache_b[c * K_SUB + h], buf_b_qs, (b_col0 + c * TN) * QPITCH + ks * (BK / 4) + h * (TK / 4), QPITCH, gl_CooperativeMatrixLayoutColumnMajor); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | float scale_b[cms_per_col]; |
| 357 | float bs[cms_per_col]; |
| 358 | [[unroll]] for (uint c = 0; c < cms_per_col; c++) { |
| 359 | scale_b[c] = buf_b_d[ks * BN + b_col0 + c * TN + elem_col0]; |
| 360 | bs[c] = float(buf_b_s[ks * BN + b_col0 + c * TN + elem_col0]); |
| 361 | } |
| 362 | |
| 363 | [[unroll]] for (uint r = 0; r < cms_per_row; r++) { |
| 364 | float scale_a[CM_ELEMS]; |
| 365 | float nbias_a[CM_ELEMS]; |
| 366 | float ma[CM_ELEMS]; |
| 367 | [[unroll]] for (uint e = 0; e < CM_ELEMS; e++) { |
| 368 | vec2 dm = buf_a_dm[ks * BM + a_row0 + r * TM + cm_elem_row(e)]; |
| 369 | scale_a[e] = dm.x; |
| 370 | if (USE_MAGIC_BIAS) { |
| 371 | nbias_a[e] = -ACC_BIAS_F * scale_a[e]; |
| 372 | } |
| 373 | ma[e] = dm.y; |
| 374 | } |
| 375 | |
| 376 | [[unroll]] for (uint c = 0; c < cms_per_col; c++) { |
| 377 | coopmat<int32_t, gl_ScopeSubgroup, TM, TN, gl_MatrixUseAccumulator> acc = |
| 378 | coopmat<int32_t, gl_ScopeSubgroup, TM, TN, gl_MatrixUseAccumulator>( |
| 379 | USE_MAGIC_BIAS ? ACC_BIAS_BITS : 0); |
| 380 | |
| 381 | [[unroll]] for (uint h = 0; h < K_SUB; h++) { |
| 382 | acc = coopMatMulAdd(cache_a[r * K_SUB + h], cache_b[c * K_SUB + h], acc); |
| 383 | } |
| 384 | |
| 385 | const uint tile_idx = r * cms_per_col + c; |
| 386 | [[unroll]] for (uint e = 0; e < CM_ELEMS; e++) { |
| 387 | sums[tile_idx * CM_ELEMS + e] = cm1_accumulate( |
| 388 | sums[tile_idx * CM_ELEMS + e], int(acc[e]), |
| 389 | scale_a[e], nbias_a[e], scale_b[c], ma[e] * bs[c]); |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | #else |
| 394 | // Preload all A/B fragments up front (ILP). |
| 395 | coopmat<int8_t, gl_ScopeSubgroup, TM, TK, gl_MatrixUseA> cache_a[cms_per_row * K_SUB]; |
| 396 | coopmat<int8_t, gl_ScopeSubgroup, TK, TN, gl_MatrixUseB> cache_b[cms_per_col * K_SUB]; |
| 397 | |
| 398 | [[unroll]] for (uint r = 0; r < cms_per_row; r++) { |
| 399 | [[unroll]] for (uint h = 0; h < K_SUB; h++) { |
| 400 | coopMatLoad(cache_a[r * K_SUB + h], buf_a_qs, (a_row0 + r * TM) * QPITCH + ks * (BK / 4) + h * (TK / 4), QPITCH, gl_CooperativeMatrixLayoutRowMajor); |
| 401 | } |
| 402 | } |
| 403 | [[unroll]] for (uint c = 0; c < cms_per_col; c++) { |
| 404 | [[unroll]] for (uint h = 0; h < K_SUB; h++) { |
| 405 | coopMatLoad(cache_b[c * K_SUB + h], buf_b_qs, (b_col0 + c * TN) * QPITCH + ks * (BK / 4) + h * (TK / 4), QPITCH, gl_CooperativeMatrixLayoutColumnMajor); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | float scale_b[cms_per_col]; |
| 410 | [[unroll]] for (uint c = 0; c < cms_per_col; c++) { |
| 411 | scale_b[c] = buf_b_d[ks * BN + b_col0 + c * TN + elem_col0]; |
| 412 | } |
| 413 | |
| 414 | [[unroll]] for (uint r = 0; r < cms_per_row; r++) { |
| 415 | float scale_a[CM_ELEMS]; |
| 416 | float nbias_a[CM_ELEMS]; |
| 417 | [[unroll]] for (uint e = 0; e < CM_ELEMS; e++) { |
| 418 | scale_a[e] = buf_a_d[ks * BM + a_row0 + r * TM + cm_elem_row(e)]; |
| 419 | if (USE_MAGIC_BIAS) { |
| 420 | nbias_a[e] = -ACC_BIAS_F * scale_a[e]; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | [[unroll]] for (uint c = 0; c < cms_per_col; c++) { |
| 425 | coopmat<int32_t, gl_ScopeSubgroup, TM, TN, gl_MatrixUseAccumulator> acc = |
| 426 | coopmat<int32_t, gl_ScopeSubgroup, TM, TN, gl_MatrixUseAccumulator>( |
| 427 | USE_MAGIC_BIAS ? ACC_BIAS_BITS : 0); |
| 428 | |
| 429 | [[unroll]] for (uint h = 0; h < K_SUB; h++) { |
| 430 | acc = coopMatMulAdd(cache_a[r * K_SUB + h], cache_b[c * K_SUB + h], acc); |
| 431 | } |
| 432 | |
| 433 | const uint tile_idx = r * cms_per_col + c; |
| 434 | [[unroll]] for (uint e = 0; e < CM_ELEMS; e++) { |
| 435 | sums[tile_idx * CM_ELEMS + e] = cm1_accumulate( |
| 436 | sums[tile_idx * CM_ELEMS + e], int(acc[e]), |
| 437 | scale_a[e], nbias_a[e], scale_b[c], 0.0); |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | #endif // KSCALES |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | barrier(); |
| 446 | } |
| 447 | |
| 448 | #undef PREFETCH_BLOCK |
| 449 | #undef STORE_BLOCK_TO_LDS |
| 450 | #undef B_IB_CALC |
| 451 | |
| 452 | const uint dr = ir * BM + a_row0; |
| 453 | const uint dc = ic * BN + b_col0; |
| 454 | |
| 455 | #ifdef MUL_MAT_ID |
| 456 | [[unroll]] for (uint r = 0; r < cms_per_row; r++) { |
| 457 | [[unroll]] for (uint c = 0; c < cms_per_col; c++) { |
| 458 | const uint tile_idx = r * cms_per_col + c; |
| 459 | [[unroll]] for (uint e = 0; e < CM_ELEMS; e++) { |
| 460 | const uint col_i = dc + c * TN + elem_col0; |
| 461 | if (col_i >= _ne1) continue; |
| 462 | |
| 463 | const uint row_g = dr + r * TM + cm_elem_row(e); |
| 464 | if (row_g >= p.M) continue; |
| 465 | |
| 466 | const u16vec2 row_idx = row_ids[col_i - ic * BN]; |
| 467 | const uint store_offset = row_idx.y * p.batch_stride_d + row_idx.x * p.stride_d + row_g; |
| 468 | data_d[store_offset] = D_TYPE(sums[tile_idx * CM_ELEMS + e]); |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | #else |
| 473 | const uint offsets = batch_idx * p.batch_stride_d + ik * p.batch_stride_d * p.num_batches; |
| 474 | |
| 475 | [[unroll]] for (uint r = 0; r < cms_per_row; r++) { |
| 476 | [[unroll]] for (uint c = 0; c < cms_per_col; c++) { |
| 477 | const uint tile_idx = r * cms_per_col + c; |
| 478 | [[unroll]] for (uint e = 0; e < CM_ELEMS; e++) { |
| 479 | const uint row_g = dr + r * TM + cm_elem_row(e); |
| 480 | const uint col_g = dc + c * TN + elem_col0; |
| 481 | if (row_g < p.M && col_g < p.N) { |
| 482 | data_d[offsets + col_g * p.stride_d + row_g] = D_TYPE(sums[tile_idx * CM_ELEMS + e]); |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | #endif // MUL_MAT_ID |
| 488 | } |