interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

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
21layout(binding = 0) readonly buffer A {
22 A_TYPE knl_data[];
23}; // src0 - kernel: [KW, KH, KD, IC*OC]
24
25layout(binding = 1) readonly buffer B {
26 B_TYPE src_data[];
27}; // src1 - input: [IW, IH, ID, IC*N] -- channel_first format
28
29layout(binding = 2) writeonly buffer D {
30 D_TYPE dst_data[];
31}; // dst - result: [OW, OH, OD, OC*N]
32
33layout(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
71p;
72
73layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
74// Blocktile sizes
75layout(constant_id = 1) const uint BS_K = 128;
76layout(constant_id = 2) const uint BS_CRS = 16;
77layout(constant_id = 3) const uint BS_NPQ = 128;
78// Thread-tile sizes
79layout(constant_id = 4) const uint TS_K = 8;
80layout(constant_id = 5) const uint SHMEM_PAD = 4;
81// Stride, padding, dilation
82layout(constant_id = 6) const uint s0 = 1;
83layout(constant_id = 7) const uint s1 = 1;
84layout(constant_id = 8) const uint s2 = 1;
85layout(constant_id = 9) const uint p0 = 0;
86layout(constant_id = 10) const uint p1 = 0;
87layout(constant_id = 11) const uint p2 = 0;
88layout(constant_id = 12) const uint d0 = 1;
89layout(constant_id = 13) const uint d1 = 1;
90layout(constant_id = 14) const uint d2 = 1;
91// Kernel spatial sizes
92layout(constant_id = 15) const uint KW = 1;
93layout(constant_id = 16) const uint KH = 1;
94layout(constant_id = 17) const uint KD = 1;
95// when set, skip bounds checks and address clamps (K/CRS/NPQ are tile-aligned)
96layout(constant_id = 18) const uint aligned = 0;
97// stage cm2 result through shmem (Csh) for coalesced stores. cm1 always does this.
98layout(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.
104layout(constant_id = 20) const uint WM = 32;
105layout(constant_id = 21) const uint WN = 32;
106const uint TM = 16;
107const uint TN = 16;
108const uint TK = 16;
109const uint cms_per_row = WM / TM;
110const uint cms_per_col = WN / TN;
111const uint warps_M = BS_K / WM;
112const uint warps_N = BS_NPQ / WN;
113#endif
114
115// without padding, ID_idx/IH_idx/IW_idx are in bounds by construction
116const bool dhw_in_bounds = (p0 == 0) && (p1 == 0) && (p2 == 0);
117
118uint32_t tid = gl_LocalInvocationID.x;
119const uint32_t WG_SIZE = gl_WorkGroupSize.x;
120
121uint splitWork(uint work_size, uint block_size) {
122 return (block_size + work_size - 1) / block_size;
123}
124
125uint32_t K = p.OC;
126uint32_t CRS = p.IC * KD * KH * KW;
127uint32_t NPQ = p.N * p.OD * p.OH * p.OW;
128
129// Number of blocktiles per input
130uint32_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
138const uint32_t Ash_stride = BS_CRS + SHMEM_PAD;
139const uint32_t Bsh_stride = BS_NPQ + SHMEM_PAD;
140
141const uint32_t Ash_len = BS_K * Ash_stride;
142const uint32_t Bsh_len = BS_CRS * Bsh_stride;
143
144shared SHMEM_TYPE Ash[Ash_len]; // K x CRS
145shared 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)
149const uint32_t Csh_stride = BS_NPQ;
150#ifdef COOPMAT
151const uint32_t Csh_len = BS_K * Csh_stride;
152#else
153const uint32_t Csh_len = csh_store != 0 ? BS_K * Csh_stride : 8; // 8 to workaround compiler bug
154#endif
155shared SHMEM_TYPE Csh[Csh_len]; // K x NPQ
156#endif
157
158// Threadtile sizes
159const uint32_t TS_NPQ = BS_K * BS_NPQ / WG_SIZE / TS_K;
160
161// Number of threadtiles per blocktile
162const uint32_t NT_NPQ = BS_NPQ / TS_NPQ;
163
164/*
165Compute
166KxCRS @ CRSxNPQ = K x NPQ
167K=OC
168C=IC
169D,R,S=KD,KH,KW
170Z,P,Q=OD,OH,OW
171*/
172
173uint32_t B_idx_K = gl_WorkGroupID.x;
174uint32_t B_idx_NPQ = gl_WorkGroupID.y + gl_WorkGroupID.z * 512;
175
176uint32_t T_y = tid / NT_NPQ;
177uint32_t T_x = tid % NT_NPQ;
178
179uint32_t Ar = tid / BS_CRS;
180uint32_t Ac = tid % BS_CRS;
181const uint32_t ArpWg = WG_SIZE / BS_CRS;
182
183uint32_t Br = tid / BS_NPQ;
184uint32_t Bc = tid % BS_NPQ;
185const uint32_t BrpWg = WG_SIZE / BS_NPQ;
186
187void 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
198void 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
211ACC_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
228void main() {
229 if (B_idx_NPQ * BS_NPQ >= NPQ) {
230 return;
231 }
232
233#ifdef COOPMAT2
234 coopmat<ACC_TYPE, gl_ScopeWorkgroup, BS_K, BS_NPQ, gl_MatrixUseAccumulator> matC;
235 matC = coopmat<ACC_TYPE, gl_ScopeWorkgroup, BS_K, BS_NPQ, gl_MatrixUseAccumulator>(0.0);
236#elif defined(COOPMAT)
237 coopmat<float16_t, gl_ScopeSubgroup, TM, TN, gl_MatrixUseAccumulator> 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<float16_t, gl_ScopeSubgroup, TM, TN, gl_MatrixUseAccumulator>(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<float16_t, gl_ScopeWorkgroup, BS_K, BS_CRS, gl_MatrixUseA> matA;
319 coopmat<float16_t, gl_ScopeWorkgroup, BS_CRS, BS_NPQ, gl_MatrixUseB> 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<float16_t, gl_ScopeSubgroup, TM, TK, gl_MatrixUseA> 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<float16_t, gl_ScopeSubgroup, TK, TN, gl_MatrixUseB> 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}