Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-vulkan/vulkan-shaders/mul_mm.comp # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-vulkan / vulkan-shaders / mul_mm.comp` 534 lines ยท 20283 bytes Raw Blame History 1 #version 450 2 3 #extension GL_EXT_control_flow_attributes : enable 4 #extension GL_EXT_shader_16bit_storage : require 5 6 #ifdef FLOAT16 7 #extension GL_EXT_shader_explicit_arithmetic_types_float16 : require 8 #endif 9 #if defined(DATA_A_IQ1_M) 10 #extension GL_EXT_shader_explicit_arithmetic_types_int16 : require 11 #endif 12 #if !defined(DATA_A_F32) && !defined(DATA_A_F16) && !defined(DATA_A_BF16) 13 #extension GL_EXT_shader_explicit_arithmetic_types_int16 : require 14 #endif 15 16 #if defined(DATA_A_BF16) && defined(COOPMAT) 17 #extension GL_EXT_bfloat16 : enable 18 #endif 19 20 #ifdef COOPMAT 21 #extension GL_KHR_cooperative_matrix : enable 22 #extension GL_KHR_memory_scope_semantics : enable 23 #endif 24 25 #if defined(COOPMAT) || defined(MUL_MAT_ID_USE_SUBGROUPS) 26 #extension GL_KHR_shader_subgroup_basic : enable 27 #extension GL_KHR_shader_subgroup_ballot : enable 28 #endif 29 30 #ifdef MUL_MAT_ID 31 #extension GL_EXT_shader_explicit_arithmetic_types_int16 : require 32 #endif 33 34 #ifdef MULMAT_QUANT 35 #include "ggml_type_ids.glsl" 36 layout (constant_id = 12) const uint MmTypeA = 0; 37 #endif 38 39 #include "types.glsl" 40 #include "dot_product_funcs.glsl" 41 42 #ifndef MULMAT_QUANT 43 #ifndef LOAD_VEC_A 44 #define LOAD_VEC_A 1 45 #endif 46 #endif 47 #ifndef LOAD_VEC_B 48 #define LOAD_VEC_B 1 49 #endif 50 51 layout (constant_id = 11) const uint ALIGNED = 0; 52 53 #ifdef MULMAT_QUANT 54 55 uint mm_load_vec_a() { 56 switch (MmTypeA) { 57 case GGML_TYPE_Q1_0: 58 case GGML_TYPE_Q4_0: 59 case GGML_TYPE_Q4_1: 60 case GGML_TYPE_Q5_1: 61 return 8u; 62 case GGML_TYPE_Q2_0: 63 case GGML_TYPE_Q5_0: 64 case GGML_TYPE_Q8_0: 65 case GGML_TYPE_Q2_K: 66 case GGML_TYPE_Q4_K: 67 case GGML_TYPE_Q5_K: 68 return 4u; 69 default: 70 return 2u; 71 } 72 } 73 #endif 74 75 #if !defined(TO_FLOAT_TYPE) 76 #define TO_FLOAT_TYPE FLOAT_TYPE 77 #endif 78 79 layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in; 80 81 #ifndef MULMAT_QUANT 82 layout (binding = 0) readonly buffer A {A_TYPE data_a[];}; 83 #if defined(DATA_A_F32) 84 layout (binding = 0) readonly buffer A_SCALAR {float data_a_scalar[];}; 85 #elif defined(DATA_A_F16) 86 layout (binding = 0) readonly buffer A_SCALAR {float16_t data_a_scalar[];}; 87 #elif defined(DATA_A_BF16) 88 layout (binding = 0) readonly buffer A_SCALAR {uint16_t data_a_scalar[];}; 89 #endif 90 #if defined(A_TYPE_PACKED16) 91 layout (binding = 0) readonly buffer A_PACKED16 {A_TYPE_PACKED16 data_a_packed16[];}; 92 #endif 93 #if defined(A_TYPE_PACKED32) 94 layout (binding = 0) readonly buffer A_PACKED32 {A_TYPE_PACKED32 data_a_packed32[];}; 95 #endif 96 #else 97 // Unpacked struct aliases 98 layout (binding = 0) readonly buffer BUF_Q1_0 { block_q1_0 data[]; } a_q1_0; 99 layout (binding = 0) readonly buffer BUF_Q2_0 { block_q2_0 data[]; } a_q2_0; 100 layout (binding = 0) readonly buffer BUF_Q2_K { block_q2_K data[]; } a_q2_k; 101 layout (binding = 0) readonly buffer BUF_Q3_K { block_q3_K data[]; } a_q3_k; 102 layout (binding = 0) readonly buffer BUF_Q4_K { block_q4_K data[]; } a_q4_k; 103 layout (binding = 0) readonly buffer BUF_Q5_K { block_q5_K data[]; } a_q5_k; 104 layout (binding = 0) readonly buffer BUF_Q6_K { block_q6_K data[]; } a_q6_k; 105 layout (binding = 0) readonly buffer BUF_TQ1_0 { block_tq1_0 data[]; } a_tq1_0; 106 layout (binding = 0) readonly buffer BUF_TQ2_0 { block_tq2_0 data[]; } a_tq2_0; 107 // Packed16 aliases 108 layout (binding = 0) readonly buffer BUF_Q4_0_P16 { block_q4_0_packed16 data[]; } a_q4_0_p16; 109 layout (binding = 0) readonly buffer BUF_Q5_0_P16 { block_q5_0_packed16 data[]; } a_q5_0_p16; 110 layout (binding = 0) readonly buffer BUF_Q8_0_P16 { block_q8_0_packed16 data[]; } a_q8_0_p16; 111 layout (binding = 0) readonly buffer BUF_Q3_K_P16 { block_q3_K_packed16 data[]; } a_q3_k_p16; 112 layout (binding = 0) readonly buffer BUF_Q6_K_P16 { block_q6_K_packed16 data[]; } a_q6_k_p16; 113 // Packed32 aliases 114 layout (binding = 0) readonly buffer BUF_Q4_1_P32 { block_q4_1_packed32 data[]; } a_q4_1_p32; 115 layout (binding = 0) readonly buffer BUF_Q5_1_P32 { block_q5_1_packed32 data[]; } a_q5_1_p32; 116 layout (binding = 0) readonly buffer BUF_Q2_K_P32 { block_q2_K_packed32 data[]; } a_q2_k_p32; 117 layout (binding = 0) readonly buffer BUF_Q4_K_P32 { block_q4_K_packed32 data[]; } a_q4_k_p32; 118 layout (binding = 0) readonly buffer BUF_Q5_K_P32 { block_q5_K_packed32 data[]; } a_q5_k_p32; 119 #endif 120 121 layout (binding = 1) readonly buffer B {B_TYPE data_b[];}; 122 layout (binding = 1) readonly buffer B_SCALAR {B_TYPE_SCALAR data_b_scalar[];}; 123 layout (binding = 2) writeonly buffer D {D_TYPE data_d[];}; 124 125 #ifdef MUL_MAT_ID 126 layout (binding = 3) readonly buffer IDS {int data_ids[];}; 127 layout (binding = 4) readonly buffer RowMap {uint data_row_map[];}; 128 #endif 129 130 layout (push_constant) uniform parameter 131 { 132 uint M; 133 uint N; 134 uint K; 135 uint stride_a; 136 uint stride_b; 137 uint stride_d; 138 139 uint batch_stride_a; 140 uint batch_stride_b; 141 uint batch_stride_d; 142 143 #ifdef MUL_MAT_ID 144 uint nei0; 145 uint nei1; 146 uint nbi1; 147 uint ne11; 148 uint n_experts; 149 uint row_ids_offset; 150 #else 151 uint base_work_group_z; 152 uint num_batches; 153 uint k_split; 154 uint ne02; 155 uint ne12; 156 uint broadcast2; 157 uint broadcast3; 158 #endif 159 } p; 160 161 layout (constant_id = 0) const uint BLOCK_SIZE = 64; 162 layout (constant_id = 1) const uint BM = 64; 163 layout (constant_id = 2) const uint BN = 64; 164 layout (constant_id = 4) const uint WM = 32; 165 layout (constant_id = 5) const uint WN = 32; 166 layout (constant_id = 6) const uint WMITER = 2; 167 layout (constant_id = 7) const uint TM = 4; 168 layout (constant_id = 8) const uint TN = 2; 169 layout (constant_id = 9) const uint TK = 1; // Only needed for coopmat 170 layout (constant_id = 10) const uint WARP = 32; 171 172 #if defined(DATA_A_F32) || defined(DATA_A_F16) 173 #define BK 32 174 #define BK_STEP 4 175 #else 176 layout (constant_id = 3) const uint BK = 16; // Assumed to be 32 if working with a quant 177 #define BK_STEP 2 178 #endif 179 180 #ifdef COOPMAT 181 #ifdef MULMAT_QUANT 182 layout(constant_id = 13) const uint SHMEM_STRIDE_PAD = 4; 183 layout(constant_id = 14) const bool APPLY_SLM_A_RESHAPE = false; 184 #else 185 layout(constant_id = 12) const uint SHMEM_STRIDE_PAD = 4; 186 layout(constant_id = 13) const bool APPLY_SLM_A_RESHAPE = false; 187 #endif 188 #else 189 const uint SHMEM_STRIDE_PAD = 1; 190 const bool APPLY_SLM_A_RESHAPE = false; 191 #endif 192 #define SHMEM_STRIDE (BK / 2 + SHMEM_STRIDE_PAD) 193 194 shared FLOAT_TYPEV2 buf_a[BM * SHMEM_STRIDE]; 195 shared FLOAT_TYPEV2 buf_b[BN * SHMEM_STRIDE]; 196 197 #define NUM_WARPS (BLOCK_SIZE / WARP) 198 199 #ifdef COOPMAT 200 shared ACC_TYPE coopmat_stage[TM * TN * NUM_WARPS]; 201 #endif 202 203 #include "mul_mm_id_funcs.glsl" 204 #include "mul_mm_funcs.glsl" 205 206 #ifdef MULMAT_QUANT 207 #include "iq_shmem_init.glsl" 208 #endif 209 210 void main() { 211 #ifdef MUL_MAT_ID 212 const uint tile_idx = gl_WorkGroupID.y + gl_WorkGroupID.z * gl_NumWorkGroups.y; 213 if (tile_idx >= data_row_map[0]) { 214 return; 215 } 216 const uint expert_idx = data_row_map[1 + 2 * tile_idx]; 217 const uint row_begin = tile_idx * BN; 218 _ne1 = data_row_map[2 + 2 * tile_idx]; 219 const uint ic = 0; 220 #else 221 const uint ic = gl_WorkGroupID.y; 222 #endif 223 #if defined(NEEDS_INIT_IQ_SHMEM) || defined(MULMAT_QUANT) 224 init_iq_shmem(gl_WorkGroupSize); 225 #endif 226 227 #ifndef MUL_MAT_ID 228 const uint batch_idx = gl_WorkGroupID.z + p.base_work_group_z; 229 230 const uint i13 = batch_idx / p.ne12; 231 const uint i12 = batch_idx % p.ne12; 232 233 const uint i03 = i13 / p.broadcast3; 234 const uint i02 = i12 / p.broadcast2; 235 236 const uint batch_idx_a = i03 * p.ne02 + i02; 237 #endif 238 239 const uint blocks_m = (p.M + BM - 1) / BM; 240 const uint ir = gl_WorkGroupID.x % blocks_m; 241 const uint ik = gl_WorkGroupID.x / blocks_m; 242 243 const uint WNITER = (WM * WN) / (WARP * TM * TN * WMITER); 244 const uint WSUBM = WM / WMITER; 245 const uint WSUBN = WN / WNITER; 246 247 #ifdef COOPMAT 248 const uint warp_i = gl_SubgroupID; 249 250 const uint tiw = gl_SubgroupInvocationID; 251 252 const uint cms_per_row = WM / TM; 253 const uint cms_per_col = WN / TN; 254 255 const uint storestride = WARP / TM; 256 const uint store_r = tiw % TM; 257 const uint store_c = tiw / TM; 258 #else 259 const uint warp_i = gl_LocalInvocationID.x / WARP; 260 261 const uint tiw = gl_LocalInvocationID.x % WARP; 262 263 const uint tiwr = tiw % (WSUBM / TM); 264 const uint tiwc = tiw / (WSUBM / TM); 265 #endif 266 267 const uint warp_r = warp_i % (BM / WM); 268 const uint warp_c = warp_i / (BM / WM); 269 270 #if defined(DATA_A_F32) || defined(DATA_A_F16) || defined(DATA_A_BF16) 271 const uint LOAD_VEC_A_EFF = (ALIGNED != 0) ? LOAD_VEC_A : 1; 272 const uint LOAD_VEC_BATCH_A = (ALIGNED != 0) ? 1 : 2; 273 #elif !defined(MULMAT_QUANT) 274 const uint LOAD_VEC_A_EFF = LOAD_VEC_A; 275 const uint LOAD_VEC_BATCH_A = 1; 276 #else 277 const uint LOAD_VEC_A_EFF = mm_load_vec_a(); 278 const uint LOAD_VEC_BATCH_A = 1; 279 #endif 280 const uint LOAD_VEC_B_EFF = (ALIGNED != 0) ? LOAD_VEC_B : 1; 281 const uint LOAD_VEC_BATCH_B = (ALIGNED != 0) ? 1 : 2; 282 283 const uint loadr_a = gl_LocalInvocationID.x % (BK / LOAD_VEC_A_EFF / LOAD_VEC_BATCH_A); 284 const uint loadc_a = gl_LocalInvocationID.x / (BK / LOAD_VEC_A_EFF / LOAD_VEC_BATCH_A); 285 const uint loadr_b = gl_LocalInvocationID.x % (BK / LOAD_VEC_B_EFF / LOAD_VEC_BATCH_B); 286 const uint loadc_b = gl_LocalInvocationID.x / (BK / LOAD_VEC_B_EFF / LOAD_VEC_BATCH_B); 287 288 const uint loadstride_a = gl_WorkGroupSize.x * LOAD_VEC_A_EFF * LOAD_VEC_BATCH_A / BK; 289 const uint loadstride_b = gl_WorkGroupSize.x * LOAD_VEC_B_EFF * LOAD_VEC_BATCH_B / BK; 290 291 #ifdef MUL_MAT_ID 292 load_row_ids(row_begin); 293 uint required_work_items = _ne1 * BK / LOAD_VEC_B_EFF / LOAD_VEC_BATCH_B; 294 uint required_warp_c = (_ne1 + WN - 1) / WN; 295 #endif 296 297 #ifdef MUL_MAT_ID 298 const uint start_k = 0; 299 const uint end_k = p.K; 300 #else 301 const uint start_k = ik * p.k_split; 302 const uint end_k = min(p.K, (ik + 1) * p.k_split); 303 #endif 304 305 uint pos_a = 306 #ifdef MUL_MAT_ID 307 expert_idx * (p.batch_stride_a / LOAD_VEC_A_EFF) + 308 #else 309 batch_idx_a * (p.batch_stride_a / LOAD_VEC_A_EFF) + 310 #endif 311 (ir * BM * p.stride_a + start_k) / LOAD_VEC_A_EFF; 312 #ifdef MUL_MAT_ID 313 uint pos_b = 0; 314 #else 315 uint pos_b = (batch_idx * p.batch_stride_b + ic * BN * p.stride_b + start_k) / LOAD_VEC_B_EFF; 316 #endif 317 318 #ifdef COOPMAT 319 coopmat cache_a; 320 coopmat cache_b; 321 coopmat sums[cms_per_row * cms_per_col]; 322 323 [[unroll]] for (uint i = 0; i < cms_per_row * cms_per_col; i++) { 324 sums[i] = coopmat(0.0f); 325 } 326 #else 327 ACC_TYPEV2 sums[WMITER * TM * WNITER * TN/2]; 328 #if defined(DATA_A_F32) || defined(DATA_A_F16) 329 FLOAT_TYPEV4 cache_a[WMITER * TM]; 330 FLOAT_TYPEV4 cache_b; 331 #else 332 FLOAT_TYPEV2 cache_a[WMITER * TM]; 333 FLOAT_TYPEV2 cache_b; 334 #endif 335 336 [[unroll]] for (uint i = 0; i < WMITER*TM*WNITER*TN/2; i++) { 337 sums[i] = ACC_TYPEV2(0.0f, 0.0f); 338 } 339 #endif 340 341 for (uint block = start_k; block < end_k; block += BK) { 342 [[unroll]] for (uint l = 0; l < BM; l += loadstride_a) { 343 load_a_to_shmem(pos_a, loadr_a, loadc_a + l, ir * BM + loadc_a + l, block, end_k); 344 } 345 #ifdef MUL_MAT_ID 346 if (gl_LocalInvocationID.x < required_work_items) { 347 #endif 348 [[unroll]] for (uint l = 0; l < BN; l += loadstride_b) { 349 #if !defined(MUL_MAT_ID) 350 load_b_to_shmem(pos_b, loadr_b, loadc_b + l, ic * BN + loadc_b + l, block, end_k); 351 #else 352 load_b_to_shmem(pos_b, loadr_b, loadc_b + l, ic, _ne1, block, end_k); 353 #endif 354 } 355 #ifdef MUL_MAT_ID 356 } 357 #endif 358 359 barrier(); 360 361 pos_a += BK / LOAD_VEC_A_EFF; 362 pos_b += BK / LOAD_VEC_B_EFF; 363 364 #ifdef COOPMAT 365 #ifdef MUL_MAT_ID 366 if (warp_c < required_warp_c) { 367 #endif 368 [[unroll]] for (uint i = 0; i < BK; i += TK) { 369 [[unroll]] for (uint cm_row = 0; cm_row < cms_per_row; cm_row++) { 370 // Load from shared into cache 371 coopMatLoad(cache_a, buf_a, a_shmem_index(warp_r * WM + cm_row * TM, i / 2), a_shmem_stride(), gl_CooperativeMatrixLayoutRowMajor); 372 373 [[unroll]] for (uint cm_col = 0; cm_col < cms_per_col; cm_col++) { 374 coopMatLoad(cache_b, buf_b, (warp_c * WN + cm_col * TN) * SHMEM_STRIDE + i / 2, SHMEM_STRIDE, gl_CooperativeMatrixLayoutColumnMajor); 375 376 sums[cm_col * cms_per_row + cm_row] = coopMatMulAdd(cache_a, cache_b, sums[cm_col * cms_per_row + cm_row]); 377 } 378 } 379 } 380 #ifdef MUL_MAT_ID 381 } 382 #endif 383 #else 384 [[unroll]] for (uint i = 0; i < BK / BK_STEP; i++) { 385 // Load from shared into cache 386 [[unroll]] for (uint wsir = 0; wsir < WMITER; wsir++) { 387 [[unroll]] for (uint j = 0; j < TM; j++) { 388 #if defined(DATA_A_F32) || defined(DATA_A_F16) 389 cache_a[wsir * TM + j].xy = buf_a[(warp_r * WM + wsir * WSUBM + tiwr * TM + j) * SHMEM_STRIDE + 2 * i ]; 390 cache_a[wsir * TM + j].zw = buf_a[(warp_r * WM + wsir * WSUBM + tiwr * TM + j) * SHMEM_STRIDE + 2 * i + 1]; 391 #else 392 cache_a[wsir * TM + j] = buf_a[(warp_r * WM + wsir * WSUBM + tiwr * TM + j) * SHMEM_STRIDE + i]; 393 #endif 394 } 395 } 396 397 [[unroll]] for (uint wsic = 0; wsic < WNITER; wsic++) { 398 [[unroll]] for (uint cc = 0; cc < TN; cc++) { 399 #if defined(DATA_A_F32) || defined(DATA_A_F16) 400 cache_b.xy = buf_b[(warp_c * WN + wsic * WSUBN + tiwc * TN + cc) * SHMEM_STRIDE + 2 * i ]; 401 cache_b.zw = buf_b[(warp_c * WN + wsic * WSUBN + tiwc * TN + cc) * SHMEM_STRIDE + 2 * i + 1]; 402 #else 403 cache_b = buf_b[(warp_c * WN + wsic * WSUBN + tiwc * TN + cc) * SHMEM_STRIDE + i]; 404 #endif 405 406 [[unroll]] for (uint wsir = 0; wsir < WMITER; wsir++) { 407 [[unroll]] for (uint cr = 0; cr < TM / 2; cr++) { 408 // [WNITER][TN][WMITER][TM / 2] -> [wsic][cc][wsir][cr] 409 const uint sums_idx = (wsic * TN + cc) * WMITER * (TM / 2) + wsir * (TM / 2) + cr; 410 sums[sums_idx] = dot_product_v2(cache_a[wsir * TM + 2 * cr], cache_a[wsir * TM + 2 * cr + 1], cache_b, sums[sums_idx]); 411 } 412 } 413 } 414 } 415 416 } 417 #endif 418 419 barrier(); 420 } 421 422 #if defined(ACC_TYPE_MAX) 423 #ifdef COOPMAT 424 [[unroll]] for (uint j = 0; j < cms_per_row * cms_per_col; j++) { 425 [[unroll]] for (uint i = 0; i < sums[j].length(); ++i) { 426 sums[j][i] = clamp(sums[j][i], -ACC_TYPE_MAX, ACC_TYPE_MAX); 427 } 428 } 429 #else 430 [[unroll]] for (uint i = 0; i < WMITER*TM*WNITER*TN/2; i++) { 431 sums[i].x = clamp(sums[i].x, -ACC_TYPE_MAX, ACC_TYPE_MAX); 432 sums[i].y = clamp(sums[i].y, -ACC_TYPE_MAX, ACC_TYPE_MAX); 433 } 434 #endif 435 #endif 436 437 const uint dr = ir * BM + warp_r * WM; 438 const uint dc = ic * BN + warp_c * WN; 439 440 #ifndef MUL_MAT_ID 441 const uint offsets = batch_idx * p.batch_stride_d + ik * p.batch_stride_d * p.num_batches; 442 #endif 443 444 #ifdef COOPMAT 445 #ifdef MUL_MAT_ID 446 [[unroll]] for (uint cm_row = 0; cm_row < cms_per_row; cm_row++) { 447 [[unroll]] for (uint cm_col = 0; cm_col < cms_per_col; cm_col++) { 448 coopMatStore(sums[cm_col * cms_per_row + cm_row], coopmat_stage, warp_i * TM * TN, TM, gl_CooperativeMatrixLayoutColumnMajor); 449 450 barrier(); 451 [[unroll]] for (uint col = 0; col < TN; col += storestride) { 452 const uint row_i = dc + cm_col * TN + col + store_c; 453 if (row_i >= _ne1) break; 454 455 const u16vec2 row_idx = row_ids[row_i - ic * BN]; 456 457 if (dr + cm_row * TM + store_r < p.M) { 458 data_d[row_idx.y * p.batch_stride_d + row_idx.x * p.stride_d + dr + cm_row * TM + store_r] = D_TYPE(coopmat_stage[warp_i * TM * TN + (col + store_c) * TM + store_r]); 459 } 460 } 461 barrier(); 462 } 463 } 464 #else 465 const bool is_aligned = p.stride_d % 4 == 0; // Assumption: D_TYPE == float 466 467 [[unroll]] for (uint cm_row = 0; cm_row < cms_per_row; cm_row++) { 468 [[unroll]] for (uint cm_col = 0; cm_col < cms_per_col; cm_col++) { 469 const bool is_in_bounds = dr + (cm_row + 1) * TM <= p.M && dc + (cm_col + 1) * TN <= p.N; 470 471 if (is_aligned && is_in_bounds) { 472 // Full coopMat is within bounds and stride_d is aligned with 16B 473 coopmat cm_dtype = coopmat(sums[cm_col * cms_per_row + cm_row]); 474 coopMatStore(cm_dtype, data_d, offsets + (dc + cm_col * TN) * p.stride_d + dr + cm_row * TM, p.stride_d, gl_CooperativeMatrixLayoutColumnMajor); 475 } else if (is_in_bounds) { 476 // Full coopMat is within bounds, but stride_d is not aligned 477 coopMatStore(sums[cm_col * cms_per_row + cm_row], coopmat_stage, warp_i * TM * TN, TM, gl_CooperativeMatrixLayoutColumnMajor); 478 479 controlBarrier(gl_ScopeSubgroup, gl_ScopeSubgroup, gl_StorageSemanticsShared, gl_SemanticsAcquireRelease); 480 [[unroll]] for (uint col = 0; col < TN; col += storestride) { 481 data_d[offsets + (dc + cm_col * TN + col + store_c) * p.stride_d + dr + cm_row * TM + store_r] = D_TYPE(coopmat_stage[warp_i * TM * TN + (col + store_c) * TM + store_r]); 482 } 483 controlBarrier(gl_ScopeSubgroup, gl_ScopeSubgroup, gl_StorageSemanticsShared, gl_SemanticsAcquireRelease); 484 } else if (dr + cm_row * TM < p.M && dc + cm_col * TN < p.N) { 485 // Partial coopMat is within bounds 486 coopMatStore(sums[cm_col * cms_per_row + cm_row], coopmat_stage, warp_i * TM * TN, TM, gl_CooperativeMatrixLayoutColumnMajor); 487 488 controlBarrier(gl_ScopeSubgroup, gl_ScopeSubgroup, gl_StorageSemanticsShared, gl_SemanticsAcquireRelease); 489 [[unroll]] for (uint col = 0; col < TN; col += storestride) { 490 if (dr + cm_row * TM + store_r < p.M && dc + cm_col * TN + col + store_c < p.N) { 491 data_d[offsets + (dc + cm_col * TN + col + store_c) * p.stride_d + dr + cm_row * TM + store_r] = D_TYPE(coopmat_stage[warp_i * TM * TN + (col + store_c) * TM + store_r]); 492 } 493 } 494 controlBarrier(gl_ScopeSubgroup, gl_ScopeSubgroup, gl_StorageSemanticsShared, gl_SemanticsAcquireRelease); 495 } 496 } 497 } 498 #endif // MUL_MAT_ID 499 #else 500 [[unroll]] for (uint wsic = 0; wsic < WNITER; wsic++) { 501 [[unroll]] for (uint wsir = 0; wsir < WMITER; wsir++) { 502 503 const uint dr_warp = dr + wsir * WSUBM + tiwr * TM; 504 const uint dc_warp = dc + wsic * WSUBN + tiwc * TN; 505 [[unroll]] for (uint cc = 0; cc < TN; cc++) { 506 #ifdef MUL_MAT_ID 507 const uint row_i = dc_warp + cc; 508 if (row_i >= _ne1) break; 509 510 const u16vec2 row_idx = row_ids[row_i - ic * BN]; 511 #endif // MUL_MAT_ID 512 [[unroll]] for (uint cr = 0; cr < TM / 2; cr++) { 513 const uint sums_idx = (wsic * TN + cc) * WMITER * (TM / 2) + wsir * (TM / 2) + cr; 514 #ifdef MUL_MAT_ID 515 if (dr_warp + 2 * cr < p.M) { 516 data_d[row_idx.y * p.batch_stride_d + row_idx.x * p.stride_d + dr_warp + 2 * cr] = D_TYPE(sums[sums_idx].x); 517 } 518 if (dr_warp + 2 * cr + 1 < p.M) { 519 data_d[row_idx.y * p.batch_stride_d + row_idx.x * p.stride_d + dr_warp + 2 * cr + 1] = D_TYPE(sums[sums_idx].y); 520 } 521 #else 522 if (dr_warp + 2 * cr < p.M && dc_warp + cc < p.N) { 523 data_d[offsets + (dc_warp + cc) * p.stride_d + dr_warp + 2 * cr] = D_TYPE(sums[sums_idx].x); 524 } 525 if (dr_warp + 2 * cr + 1 < p.M && dc_warp + cc < p.N) { 526 data_d[offsets + (dc_warp + cc) * p.stride_d + dr_warp + 2 * cr + 1] = D_TYPE(sums[sums_idx].y); 527 } 528 #endif // MUL_MAT_ID 529 } 530 } 531 } 532 } 533 #endif // COOPMAT 534 }