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