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 / conv2d_mm.comp
| 1 | #version 450 |
| 2 | |
| 3 | #extension GL_EXT_control_flow_attributes : enable |
| 4 | #ifdef COOPMAT2 |
| 5 | #extension GL_NV_cooperative_matrix2 : enable |
| 6 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : require |
| 7 | #extension GL_KHR_memory_scope_semantics : enable |
| 8 | #endif |
| 9 | |
| 10 | #ifdef COOPMAT |
| 11 | #extension GL_KHR_cooperative_matrix : enable |
| 12 | #extension GL_KHR_shader_subgroup_basic : enable |
| 13 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : require |
| 14 | #extension GL_KHR_memory_scope_semantics : enable |
| 15 | #endif |
| 16 | |
| 17 | #ifdef USE_COLLECTIVES |
| 18 | # extension GL_KHR_shader_subgroup_shuffle : enable |
| 19 | #endif |
| 20 | |
| 21 | #include "types.glsl" |
| 22 | #include "utils.glsl" |
| 23 | |
| 24 | // shape notation: [dim(N), ..., dim(0)] -- stride(dim(j)) >= stride(dim(i)) if i > j |
| 25 | layout(binding = 0) readonly buffer A { |
| 26 | A_TYPE knl_data[]; |
| 27 | }; // src0 - kernel: [KW, KH, Cin, Cout] for conv_2d, [KW, KH, Cout, Cin] for conv_transposed_2d |
| 28 | |
| 29 | layout(binding = 1) readonly buffer B { |
| 30 | B_TYPE src_data[]; |
| 31 | }; // src1 - input: [W, H, Cin, N] -- channel_first format |
| 32 | |
| 33 | layout(binding = 2) writeonly buffer D { |
| 34 | D_TYPE dst_data[]; |
| 35 | }; // dst - result: [OW, OH, Cout, N] |
| 36 | |
| 37 | layout(push_constant) uniform parameter { |
| 38 | // I/O channels, batch size |
| 39 | uint32_t Cout; |
| 40 | uint32_t Cin; |
| 41 | uint32_t N; |
| 42 | |
| 43 | // Tensor spatial sizes: input, output |
| 44 | uint32_t W; |
| 45 | uint32_t H; |
| 46 | uint32_t OW; |
| 47 | uint32_t OH; |
| 48 | |
| 49 | // Strides in elements |
| 50 | uint32_t nb01; |
| 51 | uint32_t nb02; |
| 52 | uint32_t nb03; |
| 53 | |
| 54 | uint32_t nb11; |
| 55 | uint32_t nb12; |
| 56 | uint32_t nb13; |
| 57 | |
| 58 | uint32_t nb1; |
| 59 | uint32_t nb2; |
| 60 | uint32_t nb3; |
| 61 | |
| 62 | // fastdiv helper values |
| 63 | uint32_t OWmp; uint32_t OWL; |
| 64 | uint32_t OWOHmp; uint32_t OWOHL; |
| 65 | |
| 66 | // element offsets for misaligned buffer bindings |
| 67 | uint32_t knl_offset; |
| 68 | uint32_t src_offset; |
| 69 | uint32_t dst_offset; |
| 70 | } |
| 71 | |
| 72 | p; |
| 73 | |
| 74 | layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in; |
| 75 | // Blocktile sizes |
| 76 | layout(constant_id = 1) const uint BS_K = 128; |
| 77 | layout(constant_id = 2) const uint BS_CRS = 16; |
| 78 | layout(constant_id = 3) const uint BS_NPQ = 128; |
| 79 | // Thread-tile sizes |
| 80 | layout(constant_id = 4) const uint TS_K = 8; |
| 81 | layout(constant_id = 5) const uint use_collectives = 1; |
| 82 | layout(constant_id = 6) const uint SHMEM_PAD = 4; |
| 83 | // Stride, padding, dilation |
| 84 | layout(constant_id = 7) const uint s0 = 1; |
| 85 | layout(constant_id = 8) const uint s1 = 1; |
| 86 | layout(constant_id = 9) const uint p0 = 0; |
| 87 | layout(constant_id = 10) const uint p1 = 0; |
| 88 | layout(constant_id = 11) const uint d0 = 1; |
| 89 | layout(constant_id = 12) const uint d1 = 1; |
| 90 | // Kernel spatial sizes |
| 91 | layout(constant_id = 13) const uint KW = 1; |
| 92 | layout(constant_id = 14) const uint KH = 1; |
| 93 | // when set, skip bounds checks and address clamps (K/CRS/NPQ are tile-aligned) |
| 94 | layout(constant_id = 15) const uint aligned = 0; |
| 95 | // stage cm2 result through shmem (Csh) for coalesced stores. cm1 always does this. |
| 96 | layout(constant_id = 16) const uint csh_store = 0; |
| 97 | |
| 98 | #ifdef COOPMAT |
| 99 | // cm1 subgroup tile: each subgroup computes a WM x WN region as a grid of |
| 100 | // TM x TN x TK fragments. Requires WM%TM == WN%TN == BS_K%WM == BS_NPQ%WN == |
| 101 | // BS_CRS%TK == 0, and WG_SIZE == (BS_K/WM) * (BS_NPQ/WN) * subgroup_size. |
| 102 | layout(constant_id = 17) const uint WM = 32; |
| 103 | layout(constant_id = 18) const uint WN = 32; |
| 104 | const uint TM = 16; |
| 105 | const uint TN = 16; |
| 106 | const uint TK = 16; |
| 107 | const uint cms_per_row = WM / TM; |
| 108 | const uint cms_per_col = WN / TN; |
| 109 | const uint warps_M = BS_K / WM; |
| 110 | const uint warps_N = BS_NPQ / WN; |
| 111 | #endif |
| 112 | |
| 113 | // without padding, H_idx/W_idx are in bounds by construction (non-TRANSPOSE only) |
| 114 | #ifdef TRANSPOSE |
| 115 | const bool hw_in_bounds = false; |
| 116 | #else |
| 117 | const bool hw_in_bounds = (p0 == 0) && (p1 == 0); |
| 118 | #endif |
| 119 | |
| 120 | // TRANSPOSE stride alignment is trivially satisfied for stride 1 |
| 121 | #ifdef TRANSPOSE |
| 122 | const bool stride_in_bounds = (s0 == 1) && (s1 == 1); |
| 123 | #else |
| 124 | const bool stride_in_bounds = true; |
| 125 | #endif |
| 126 | |
| 127 | uint32_t tid = gl_LocalInvocationID.x; |
| 128 | const uint32_t WG_SIZE = gl_WorkGroupSize.x; |
| 129 | |
| 130 | uint splitWork(uint work_size, uint block_size) { |
| 131 | return (block_size + work_size - 1) / block_size; |
| 132 | } |
| 133 | |
| 134 | uint32_t K = p.Cout; |
| 135 | uint32_t CRS = p.Cin * KH * KW; |
| 136 | uint32_t NPQ = p.N * p.OH * p.OW; |
| 137 | |
| 138 | uint32_t n_elems_out = K * NPQ; |
| 139 | |
| 140 | // Number of blocktiles per input |
| 141 | uint32_t NB_CRS = splitWork(CRS, BS_CRS); |
| 142 | |
| 143 | #if defined(COOPMAT2) || defined(COOPMAT) |
| 144 | #define SHMEM_TYPE float16_t |
| 145 | #else |
| 146 | #define SHMEM_TYPE float |
| 147 | #endif |
| 148 | |
| 149 | const uint32_t Ash_stride = BS_CRS + SHMEM_PAD; |
| 150 | const uint32_t Bsh_stride = BS_NPQ + SHMEM_PAD; |
| 151 | |
| 152 | const uint32_t Ash_numel = BS_K * BS_CRS; |
| 153 | const uint32_t Bsh_numel = BS_CRS * BS_NPQ; |
| 154 | |
| 155 | const uint32_t Ash_len = BS_K * Ash_stride; |
| 156 | const uint32_t Bsh_len = BS_CRS * Bsh_stride; |
| 157 | |
| 158 | shared SHMEM_TYPE Ash[Ash_len]; // K x CRS |
| 159 | shared SHMEM_TYPE Bsh[Bsh_len]; // CRS x NPQ |
| 160 | |
| 161 | #if defined(COOPMAT2) || defined(COOPMAT) |
| 162 | // stage matC through shmem so global stores are row-major (NPQ-contiguous) |
| 163 | const uint32_t Csh_stride = BS_NPQ; |
| 164 | #ifdef COOPMAT |
| 165 | const uint32_t Csh_len = BS_K * Csh_stride; |
| 166 | #else |
| 167 | const uint32_t Csh_len = csh_store != 0 ? BS_K * Csh_stride : 8; // 8 to workaround compiler bug |
| 168 | #endif |
| 169 | shared SHMEM_TYPE Csh[Csh_len]; // K x NPQ |
| 170 | #endif |
| 171 | |
| 172 | // Threadtile sizes |
| 173 | const uint32_t TS_NPQ = BS_K * BS_NPQ / WG_SIZE / TS_K; |
| 174 | |
| 175 | // Number of threadtiles per blocktile |
| 176 | const uint32_t NT_K = BS_K / TS_K; |
| 177 | const uint32_t NT_NPQ = BS_NPQ / TS_NPQ; |
| 178 | |
| 179 | /* |
| 180 | Compute |
| 181 | KxCRS @ CRSxNPQ = K x NPQ |
| 182 | K=Cout |
| 183 | C=Cin |
| 184 | R,S=KH,KW |
| 185 | P,Q=OH,OW |
| 186 | */ |
| 187 | |
| 188 | uint32_t B_idx_K = gl_WorkGroupID.x; |
| 189 | uint32_t B_idx_NPQ = gl_WorkGroupID.y + gl_WorkGroupID.z * 512; |
| 190 | |
| 191 | uint32_t T_y = tid / NT_NPQ; |
| 192 | uint32_t T_x = tid % NT_NPQ; |
| 193 | |
| 194 | uint32_t Ar = tid / BS_CRS; |
| 195 | uint32_t Ac = tid % BS_CRS; |
| 196 | const uint32_t ArpWg = WG_SIZE / BS_CRS; |
| 197 | |
| 198 | uint32_t Br = tid / BS_NPQ; |
| 199 | uint32_t Bc = tid % BS_NPQ; |
| 200 | const uint32_t BrpWg = WG_SIZE / BS_NPQ; |
| 201 | |
| 202 | #ifdef COOPMAT2 |
| 203 | #define ACC_TYPE float16_t |
| 204 | |
| 205 | ACC_TYPE perElemOpStore(const in uint32_t r, const in uint32_t c, const in ACC_TYPE elem) |
| 206 | { |
| 207 | uint32_t K_idx = B_idx_K * BS_K + r; |
| 208 | uint32_t NPQ_idx = B_idx_NPQ * BS_NPQ + c; |
| 209 | uint32_t N_idx = fastdiv(NPQ_idx, p.OWOHmp, p.OWOHL); // divide by p.OH * p.OW; |
| 210 | uint32_t OH_idx = fastdiv(NPQ_idx - N_idx * p.OH * p.OW, p.OWmp, p.OWL); // divide by p.OW; |
| 211 | uint32_t OW_idx = NPQ_idx - N_idx * p.OH * p.OW - OH_idx * p.OW; |
| 212 | uint32_t dst_idx = OW_idx + OH_idx * p.nb1 + K_idx * p.nb2 + N_idx * p.nb3; |
| 213 | if (aligned != 0 || (K_idx < K && NPQ_idx < NPQ)) { |
| 214 | dst_data[dst_idx + p.dst_offset] = D_TYPE(elem); |
| 215 | } |
| 216 | return elem; |
| 217 | } |
| 218 | #endif |
| 219 | |
| 220 | void main() { |
| 221 | if (B_idx_NPQ * BS_NPQ >= NPQ) { |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | #ifdef COOPMAT2 |
| 226 | coopmat<ACC_TYPE, gl_ScopeWorkgroup, BS_K, BS_NPQ, gl_MatrixUseAccumulator> matC; |
| 227 | matC = coopmat<ACC_TYPE, gl_ScopeWorkgroup, BS_K, BS_NPQ, gl_MatrixUseAccumulator>(0.0); |
| 228 | #elif defined(COOPMAT) |
| 229 | coopmat<float16_t, gl_ScopeSubgroup, TM, TN, gl_MatrixUseAccumulator> sums[cms_per_row * cms_per_col]; |
| 230 | [[unroll]] for (uint i = 0; i < cms_per_row * cms_per_col; i++) { |
| 231 | sums[i] = coopmat<float16_t, gl_ScopeSubgroup, TM, TN, gl_MatrixUseAccumulator>(0.0); |
| 232 | } |
| 233 | const uint warp_r = gl_SubgroupID / warps_N; |
| 234 | const uint warp_c = gl_SubgroupID % warps_N; |
| 235 | #else |
| 236 | float regC[TS_K][TS_NPQ]; |
| 237 | for (uint32_t T_ly = 0; T_ly < TS_K; T_ly++) { |
| 238 | for (uint32_t T_lx = 0; T_lx < TS_NPQ; T_lx++) { |
| 239 | regC[T_ly][T_lx] = 0.0; |
| 240 | } |
| 241 | } |
| 242 | #endif |
| 243 | /* Advance block in CRS dim */ |
| 244 | [[dont_unroll]] for (uint32_t B_idx_CRS = 0; B_idx_CRS < NB_CRS; B_idx_CRS++) { |
| 245 | uint32_t CRS_idx_a; |
| 246 | uint32_t Cin_idx_a; |
| 247 | uint32_t KH_idx_a; |
| 248 | uint32_t KW_idx_a; |
| 249 | |
| 250 | #ifdef USE_COLLECTIVES |
| 251 | uint32_t cached_CRS_idx; |
| 252 | uint32_t cached_Cin_idx; |
| 253 | uint32_t cached_KH_idx; |
| 254 | uint32_t cached_KW_idx; |
| 255 | if (use_collectives == 1) { |
| 256 | cached_CRS_idx = B_idx_CRS * BS_CRS + gl_SubgroupInvocationID; |
| 257 | cached_Cin_idx = cached_CRS_idx / (KW * KH); |
| 258 | uint32_t cached_CRS_remainder = cached_CRS_idx % (KW * KH); |
| 259 | cached_KH_idx = cached_CRS_remainder / KW; |
| 260 | cached_KW_idx = cached_CRS_remainder % KW; |
| 261 | |
| 262 | CRS_idx_a = subgroupShuffle(cached_CRS_idx, Ac); |
| 263 | Cin_idx_a = subgroupShuffle(cached_Cin_idx, Ac); |
| 264 | KH_idx_a = subgroupShuffle(cached_KH_idx, Ac); |
| 265 | KW_idx_a = subgroupShuffle(cached_KW_idx, Ac); |
| 266 | } else { |
| 267 | CRS_idx_a = B_idx_CRS * BS_CRS + Ac; // Global CRS_idx_a (column index of A) |
| 268 | Cin_idx_a = CRS_idx_a / (KW * KH); |
| 269 | uint32_t CRS_remainder = CRS_idx_a % (KW * KH); |
| 270 | KH_idx_a = CRS_remainder / KW; |
| 271 | KW_idx_a = CRS_remainder % KW; |
| 272 | } |
| 273 | #else |
| 274 | CRS_idx_a = B_idx_CRS * BS_CRS + Ac; // Global CRS_idx_a (column index of A) |
| 275 | Cin_idx_a = CRS_idx_a / (KW * KH); |
| 276 | CRS_remainder = CRS_idx_a % (KW * KH); |
| 277 | KH_idx_a = CRS_remainder / KW; |
| 278 | KW_idx_a = CRS_remainder % KW; |
| 279 | #endif |
| 280 | |
| 281 | /* Load kernel to A_block: (BS_K x BS_CRS)*/ |
| 282 | UNROLL for (uint32_t r_offset = 0; r_offset < BS_K; r_offset += ArpWg) { |
| 283 | uint32_t B_ly = r_offset + Ar; |
| 284 | uint32_t B_lx = Ac; |
| 285 | uint32_t K_idx = B_idx_K * BS_K + B_ly; /* Global K_idx (row index of A)*/ |
| 286 | #ifdef TRANSPOSE |
| 287 | uint32_t knl_idx = KW_idx_a + KH_idx_a * p.nb01 + K_idx * p.nb02 + Cin_idx_a * p.nb03; |
| 288 | #else |
| 289 | uint32_t knl_idx = KW_idx_a + KH_idx_a * p.nb01 + Cin_idx_a * p.nb02 + K_idx * p.nb03; |
| 290 | #endif |
| 291 | if (aligned == 0) { |
| 292 | knl_idx = min(knl_idx, K * CRS - 1); |
| 293 | } |
| 294 | float val = knl_data[knl_idx + p.knl_offset]; |
| 295 | if (aligned == 0 && (K_idx >= K || CRS_idx_a >= CRS)) { |
| 296 | val = 0.0; |
| 297 | } |
| 298 | Ash[B_ly * Ash_stride + B_lx] = SHMEM_TYPE(val); |
| 299 | } |
| 300 | /* Load input to B_block: (BS_CRS x BS_NPQ) */ |
| 301 | UNROLL for (uint32_t r_offset = 0; r_offset < BS_CRS; r_offset += BrpWg) { |
| 302 | uint32_t B_ly = r_offset + Br; /* Row index of B block */ |
| 303 | uint32_t B_lx = Bc; |
| 304 | uint32_t NPQ_idx = B_idx_NPQ * BS_NPQ + B_lx; /* Global NPQ index (column index of B) */ |
| 305 | uint32_t N_idx = fastdiv(NPQ_idx, p.OWOHmp, p.OWOHL); // divide by p.OH * p.OW; |
| 306 | uint32_t NPQ_remainder = NPQ_idx - N_idx * p.OH * p.OW; |
| 307 | uint32_t OH_idx = fastdiv(NPQ_remainder, p.OWmp, p.OWL); // divide by p.OW; |
| 308 | uint32_t OW_idx = NPQ_remainder - OH_idx * p.OW; |
| 309 | |
| 310 | uint32_t CRS_idx_b; |
| 311 | uint32_t Cin_idx_b; |
| 312 | uint32_t KH_idx_b; |
| 313 | uint32_t KW_idx_b; |
| 314 | #ifdef USE_COLLECTIVES |
| 315 | if (use_collectives == 1) { |
| 316 | CRS_idx_b = subgroupShuffle(cached_CRS_idx, r_offset + Br); |
| 317 | Cin_idx_b = subgroupShuffle(cached_Cin_idx, r_offset + Br); |
| 318 | KH_idx_b = subgroupShuffle(cached_KH_idx, r_offset + Br); |
| 319 | KW_idx_b = subgroupShuffle(cached_KW_idx, r_offset + Br); |
| 320 | } else { |
| 321 | CRS_idx_b = B_idx_CRS * BS_CRS + B_ly; /* Global CRS index (row index of B) */ |
| 322 | Cin_idx_b = CRS_idx_b / (KW * KH); |
| 323 | uint32_t CRS_remainder = CRS_idx_b % (KW * KH); |
| 324 | KH_idx_b = CRS_remainder / KW; |
| 325 | KW_idx_b = CRS_remainder % KW; |
| 326 | } |
| 327 | #else |
| 328 | CRS_idx_b = B_idx_CRS * BS_CRS + B_ly; /* Global CRS index (row index of B) */ |
| 329 | Cin_idx_b = CRS_idx_b / (KW * KH); |
| 330 | uint32_t CRS_remainder = CRS_idx_b % (KW * KH); |
| 331 | KH_idx_b = CRS_remainder / KW; |
| 332 | KW_idx_b = CRS_remainder % KW; |
| 333 | #endif |
| 334 | |
| 335 | #ifdef TRANSPOSE |
| 336 | uint32_t H_idx_x_s1 = OH_idx - KH_idx_b * d1 + p1; |
| 337 | uint32_t W_idx_x_s0 = OW_idx - KW_idx_b * d0 + p0; |
| 338 | uint32_t H_idx = H_idx_x_s1 / s1; |
| 339 | uint32_t W_idx = W_idx_x_s0 / s0; |
| 340 | #else |
| 341 | uint32_t H_idx = OH_idx * s1 + KH_idx_b * d1 - p1; |
| 342 | uint32_t W_idx = OW_idx * s0 + KW_idx_b * d0 - p0; |
| 343 | #endif |
| 344 | uint32_t src_idx = W_idx + H_idx * p.nb11 + Cin_idx_b * p.nb12 + N_idx * p.nb13; |
| 345 | // skip clamp when address can't go OOB |
| 346 | if (aligned == 0 || !hw_in_bounds || !stride_in_bounds) { |
| 347 | src_idx = min(max(src_idx, 0), p.Cin * p.N * p.W * p.H - 1); |
| 348 | } |
| 349 | float val = src_data[src_idx + p.src_offset]; |
| 350 | bool oob = false; |
| 351 | if (aligned == 0 && (CRS_idx_b >= CRS || NPQ_idx >= NPQ)) { |
| 352 | oob = true; |
| 353 | } |
| 354 | // also catches lower-bound underflow (idx wraps to 0x80000000+) |
| 355 | if (!hw_in_bounds && (H_idx >= p.H || W_idx >= p.W)) { |
| 356 | oob = true; |
| 357 | } |
| 358 | #ifdef TRANSPOSE |
| 359 | if (!stride_in_bounds && |
| 360 | ((H_idx_x_s1 - H_idx * s1 != 0) || (W_idx_x_s0 - W_idx * s0 != 0))) { |
| 361 | oob = true; |
| 362 | } |
| 363 | #endif |
| 364 | if (oob) { |
| 365 | val = 0.0; |
| 366 | } |
| 367 | Bsh[B_ly * Bsh_stride + B_lx] = SHMEM_TYPE(val); |
| 368 | } |
| 369 | barrier(); |
| 370 | #ifdef COOPMAT2 |
| 371 | coopmat<float16_t, gl_ScopeWorkgroup, BS_K, BS_CRS, gl_MatrixUseA> matA; |
| 372 | coopmat<float16_t, gl_ScopeWorkgroup, BS_CRS, BS_NPQ, gl_MatrixUseB> matB; |
| 373 | |
| 374 | coopMatLoad(matA, Ash, 0, Ash_stride, gl_CooperativeMatrixLayoutRowMajor); |
| 375 | coopMatLoad(matB, Bsh, 0, Bsh_stride, gl_CooperativeMatrixLayoutRowMajor); |
| 376 | matC = coopMatMulAdd(matA, matB, matC); |
| 377 | #elif defined(COOPMAT) |
| 378 | // each subgroup multiplies its grid of fragments per TK-sized CRS chunk |
| 379 | [[unroll]] for (uint k_step = 0; k_step < BS_CRS / TK; k_step++) { |
| 380 | coopmat<float16_t, gl_ScopeSubgroup, TM, TK, gl_MatrixUseA> cache_a[cms_per_row]; |
| 381 | [[unroll]] for (uint cm_row = 0; cm_row < cms_per_row; cm_row++) { |
| 382 | const uint a_off = (warp_r * WM + cm_row * TM) * Ash_stride + k_step * TK; |
| 383 | coopMatLoad(cache_a[cm_row], Ash, a_off, Ash_stride, gl_CooperativeMatrixLayoutRowMajor); |
| 384 | } |
| 385 | [[unroll]] for (uint cm_col = 0; cm_col < cms_per_col; cm_col++) { |
| 386 | coopmat<float16_t, gl_ScopeSubgroup, TK, TN, gl_MatrixUseB> cache_b; |
| 387 | const uint b_off = k_step * TK * Bsh_stride + warp_c * WN + cm_col * TN; |
| 388 | coopMatLoad(cache_b, Bsh, b_off, Bsh_stride, gl_CooperativeMatrixLayoutRowMajor); |
| 389 | [[unroll]] for (uint cm_row = 0; cm_row < cms_per_row; cm_row++) { |
| 390 | sums[cm_col * cms_per_row + cm_row] = coopMatMulAdd(cache_a[cm_row], cache_b, sums[cm_col * cms_per_row + cm_row]); |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | #else |
| 395 | if (T_y * TS_K < K) { |
| 396 | UNROLL for (uint32_t CRS_lidx = 0; CRS_lidx < BS_CRS; CRS_lidx++) { |
| 397 | float regA[TS_K]; |
| 398 | float regB[TS_NPQ]; |
| 399 | for (uint32_t T_ly = 0; T_ly < TS_K; T_ly++) { |
| 400 | regA[T_ly] = Ash[(T_y * TS_K + T_ly) * Ash_stride + CRS_lidx]; |
| 401 | } |
| 402 | for (uint32_t T_lx = 0; T_lx < TS_NPQ; T_lx++) { |
| 403 | regB[T_lx] = Bsh[CRS_lidx * Bsh_stride + T_x * TS_NPQ + T_lx]; |
| 404 | } |
| 405 | for (uint32_t T_ly = 0; T_ly < TS_K; T_ly++) { |
| 406 | for (uint32_t T_lx = 0; T_lx < TS_NPQ; T_lx++) { |
| 407 | regC[T_ly][T_lx] = fma(regA[T_ly], regB[T_lx], regC[T_ly][T_lx]); |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | #endif |
| 413 | barrier(); |
| 414 | } |
| 415 | /* Save C* */ |
| 416 | #if defined(COOPMAT2) || defined(COOPMAT) |
| 417 | // stage matC into Csh, then write to dst with coalesced NPQ-contiguous stores |
| 418 | #ifdef COOPMAT |
| 419 | const bool use_staged_store = true; |
| 420 | #else |
| 421 | const bool use_staged_store = (csh_store != 0); |
| 422 | #endif |
| 423 | if (use_staged_store) { |
| 424 | #ifdef COOPMAT |
| 425 | // cm1: each subgroup stores its fragment grid into its Csh slot |
| 426 | [[unroll]] for (uint cm_row = 0; cm_row < cms_per_row; cm_row++) { |
| 427 | [[unroll]] for (uint cm_col = 0; cm_col < cms_per_col; cm_col++) { |
| 428 | const uint csh_off = (warp_r * WM + cm_row * TM) * Csh_stride + warp_c * WN + cm_col * TN; |
| 429 | coopMatStore(sums[cm_col * cms_per_row + cm_row], Csh, csh_off, Csh_stride, gl_CooperativeMatrixLayoutRowMajor); |
| 430 | } |
| 431 | } |
| 432 | #else |
| 433 | coopMatStore(matC, Csh, 0, Csh_stride, gl_CooperativeMatrixLayoutRowMajor); |
| 434 | #endif |
| 435 | barrier(); |
| 436 | |
| 437 | // cooperative shmem->global: WG threads spread across BS_NPQ (the |
| 438 | // contiguous direction of dst), each iter covers store_rows_per_iter K-rows |
| 439 | const uint32_t store_rows_per_iter = WG_SIZE / BS_NPQ; |
| 440 | const uint32_t store_iters = BS_K / store_rows_per_iter; |
| 441 | const uint32_t k_thread_offset = tid / BS_NPQ; |
| 442 | const uint32_t npq_thread = tid % BS_NPQ; |
| 443 | [[unroll]] for (uint32_t i = 0; i < store_iters; i++) { |
| 444 | uint32_t k_local = i * store_rows_per_iter + k_thread_offset; |
| 445 | uint32_t K_idx = B_idx_K * BS_K + k_local; |
| 446 | uint32_t NPQ_idx = B_idx_NPQ * BS_NPQ + npq_thread; |
| 447 | uint32_t N_idx = fastdiv(NPQ_idx, p.OWOHmp, p.OWOHL); |
| 448 | uint32_t OH_idx = fastdiv(NPQ_idx - N_idx * p.OH * p.OW, p.OWmp, p.OWL); |
| 449 | uint32_t OW_idx = NPQ_idx - N_idx * p.OH * p.OW - OH_idx * p.OW; |
| 450 | uint32_t dst_idx = OW_idx + OH_idx * p.nb1 + K_idx * p.nb2 + N_idx * p.nb3; |
| 451 | if (aligned != 0 || (K_idx < K && NPQ_idx < NPQ)) { |
| 452 | dst_data[dst_idx + p.dst_offset] = D_TYPE(Csh[k_local * Csh_stride + npq_thread]); |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | #ifdef COOPMAT2 |
| 457 | else { |
| 458 | coopMatPerElementNV(matC, matC, perElemOpStore); |
| 459 | } |
| 460 | #endif |
| 461 | #else |
| 462 | if (T_y * TS_K < K) { |
| 463 | for (uint32_t T_ly = 0; T_ly < TS_K; T_ly++) { |
| 464 | for (uint32_t T_lx = 0; T_lx < TS_NPQ; T_lx++) { |
| 465 | uint32_t K_idx = B_idx_K * BS_K + T_y * TS_K + T_ly; |
| 466 | uint32_t NPQ_idx = B_idx_NPQ * BS_NPQ + T_x * TS_NPQ + T_lx; |
| 467 | uint32_t N_idx = fastdiv(NPQ_idx, p.OWOHmp, p.OWOHL); // divide by p.OH * p.OW; |
| 468 | uint32_t OH_idx = fastdiv(NPQ_idx - N_idx * p.OH * p.OW, p.OWmp, p.OWL); // divide by p.OW; |
| 469 | uint32_t OW_idx = NPQ_idx - N_idx * p.OH * p.OW - OH_idx * p.OW; |
| 470 | uint32_t dst_idx = OW_idx + OH_idx * p.nb1 + K_idx * p.nb2 + N_idx * p.nb3; |
| 471 | if (aligned != 0 || (K_idx < K && NPQ_idx < NPQ)) { |
| 472 | dst_data[dst_idx + p.dst_offset] = regC[T_ly][T_lx]; |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | #endif |
| 478 | } |