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 / mul_mmq.comp

293 lines · 9056 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#extension GL_EXT_shader_explicit_arithmetic_types_int8 : require
6
7#extension GL_EXT_integer_dot_product : require
8
9#define KVALUES_IQ4NL_I8
10
11#ifdef FLOAT16
12#extension GL_EXT_shader_explicit_arithmetic_types_float16 : require
13#endif
14
15#if defined(MUL_MAT_ID_USE_SUBGROUPS)
16#extension GL_KHR_shader_subgroup_basic : enable
17#extension GL_KHR_shader_subgroup_ballot : enable
18#endif
19
20#ifdef MUL_MAT_ID
21#extension GL_EXT_shader_explicit_arithmetic_types_int16 : require
22#endif
23
24#include "types.glsl"
25
26layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
27
28layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
29#if defined(A_TYPE_PACKED16)
30layout (binding = 0) readonly buffer A_PACKED16 {A_TYPE_PACKED16 data_a_packed16[];};
31#endif
32#if defined(A_TYPE_PACKED32)
33layout (binding = 0) readonly buffer A_PACKED32 {A_TYPE_PACKED32 data_a_packed32[];};
34#endif
35layout (binding = 1) readonly buffer B {block_q8_1_x4_packed128 data_b[];};
36layout (binding = 2) writeonly buffer D {D_TYPE data_d[];};
37
38#ifdef MUL_MAT_ID
39layout (binding = 3) readonly buffer IDS {int data_ids[];};
40layout (binding = 4) readonly buffer RowMap {uint data_row_map[];};
41#endif
42
43layout (push_constant) uniform parameter
44{
45 uint M;
46 uint N;
47 uint K;
48 uint stride_a;
49 uint stride_b;
50 uint stride_d;
51
52 uint batch_stride_a;
53 uint batch_stride_b;
54 uint batch_stride_d;
55
56#ifdef MUL_MAT_ID
57 uint nei0;
58 uint nei1;
59 uint nbi1;
60 uint ne11;
61 uint n_experts;
62 uint row_ids_offset;
63#else
64 uint base_work_group_z;
65 uint num_batches;
66 uint k_split;
67 uint ne02;
68 uint ne12;
69 uint broadcast2;
70 uint broadcast3;
71#endif
72} p;
73
74layout (constant_id = 0) const uint BLOCK_SIZE = 64;
75layout (constant_id = 1) const uint BM = 64;
76layout (constant_id = 2) const uint BN = 64;
77// layout (constant_id = 3) const uint BK = 32;
78layout (constant_id = 4) const uint WM = 32;
79layout (constant_id = 5) const uint WN = 32;
80layout (constant_id = 6) const uint WMITER = 2;
81layout (constant_id = 7) const uint TM = 4;
82layout (constant_id = 8) const uint TN = 2;
83layout (constant_id = 9) const uint TK = 1; // Only needed for coopmat
84layout (constant_id = 10) const uint WARP = 32;
85
86#define BK 32
87
88#include "mul_mmq_shmem_types.glsl"
89
90#ifdef MUL_MAT_ID
91#define BK_STEP 1
92#else
93#ifndef BK_STEP
94#define BK_STEP 4
95#endif
96#endif
97
98// Shared memory cache
99shared block_a_cache buf_a[BM * BK_STEP];
100shared block_b_cache buf_b[BN * BK_STEP];
101// Register cache
102block_a_cache cache_a[WMITER * TM];
103block_b_cache cache_b;
104
105#define LOAD_VEC_A (4 * QUANT_R_MMQ)
106#define LOAD_VEC_B 16
107
108
109#include "mul_mm_id_funcs.glsl"
110#include "mul_mmq_funcs.glsl"
111
112void main() {
113#ifdef MUL_MAT_ID
114 const uint tile_idx = gl_WorkGroupID.y + gl_WorkGroupID.z * gl_NumWorkGroups.y;
115 if (tile_idx >= data_row_map[0]) {
116 return;
117 }
118 const uint expert_idx = data_row_map[1 + 2 * tile_idx];
119 const uint row_begin = tile_idx * BN;
120 _ne1 = data_row_map[2 + 2 * tile_idx];
121 const uint ic = 0;
122#else
123 const uint ic = gl_WorkGroupID.y;
124#endif
125#ifdef NEEDS_INIT_IQ_SHMEM
126 init_iq_shmem(gl_WorkGroupSize);
127#endif
128
129#ifndef MUL_MAT_ID
130 const uint batch_idx = gl_WorkGroupID.z + p.base_work_group_z;
131
132 const uint i13 = batch_idx / p.ne12;
133 const uint i12 = batch_idx % p.ne12;
134
135 const uint i03 = i13 / p.broadcast3;
136 const uint i02 = i12 / p.broadcast2;
137
138 const uint batch_idx_a = i03 * p.ne02 + i02;
139#endif
140
141 const uint blocks_m = (p.M + BM - 1) / BM;
142 const uint ir = gl_WorkGroupID.x % blocks_m;
143 const uint ik = gl_WorkGroupID.x / blocks_m;
144
145 const uint WNITER = (WM * WN) / (WARP * TM * TN * WMITER);
146 const uint WSUBM = WM / WMITER;
147 const uint WSUBN = WN / WNITER;
148 const uint warp_i = gl_LocalInvocationID.x / WARP;
149
150 const uint tiw = gl_LocalInvocationID.x % WARP;
151
152 const uint tiwr = tiw % (WSUBM / TM);
153 const uint tiwc = tiw / (WSUBM / TM);
154
155 const uint warp_r = warp_i % (BM / WM);
156 const uint warp_c = warp_i / (BM / WM);
157
158 const uint loadr_a = gl_LocalInvocationID.x % (BK / LOAD_VEC_A);
159 const uint loadc_a = gl_LocalInvocationID.x / (BK / LOAD_VEC_A);
160 const uint loadr_b = gl_LocalInvocationID.x % (BK / LOAD_VEC_B);
161 const uint loadc_b = gl_LocalInvocationID.x / (BK / LOAD_VEC_B);
162
163 const uint loadstride_a = BLOCK_SIZE * LOAD_VEC_A / BK;
164 const uint loadstride_b = BLOCK_SIZE * LOAD_VEC_B / BK;
165
166#ifdef MUL_MAT_ID
167 load_row_ids(row_begin);
168#endif
169
170#ifdef MUL_MAT_ID
171 const uint start_k = 0;
172 const uint end_k = p.K;
173#else
174 const uint start_k = ik * p.k_split;
175 const uint end_k = min(p.K, (ik + 1) * p.k_split);
176#endif
177
178 uint pos_a_ib =
179#ifdef MUL_MAT_ID
180 expert_idx * (p.batch_stride_a / BK) +
181#else
182 batch_idx_a * (p.batch_stride_a / BK) +
183#endif
184 (ir * BM * p.stride_a + start_k) / BK;
185#ifdef MUL_MAT_ID
186 uint pos_b_ib = 0;
187#else
188 uint pos_b_ib = (batch_idx * p.batch_stride_b + ic * BN * p.stride_b + start_k) / BK;
189#endif
190
191 ACC_TYPE sums[WMITER * TM * WNITER * TN];
192
193 [[unroll]] for (uint i = 0; i < WMITER*TM*WNITER*TN; i++) {
194 sums[i] = ACC_TYPE(0.0f);
195 }
196
197 for (uint block = start_k; block < end_k; block += BK * BK_STEP) {
198 [[unroll]] for (uint l = 0; loadc_a + l < BM; l += loadstride_a) {
199 const uint buf_ib = loadc_a + l;
200 const uint ib = pos_a_ib + buf_ib * p.stride_a / BK;
201 const uint iqs = loadr_a;
202
203 [[unroll]] for (uint k_step = 0; k_step < BK_STEP; k_step++) {
204 block_a_to_shmem(k_step * BM + buf_ib, ib + k_step, iqs, block + k_step * BK < end_k);
205 }
206 }
207 [[unroll]] for (uint l = 0; loadc_b + l < BN; l += loadstride_b) {
208 const uint buf_ib = loadc_b + l;
209
210#ifdef MUL_MAT_ID
211 const u16vec2 row_idx = row_ids[buf_ib];
212 const uint ib = pos_b_ib + row_idx.y * p.batch_stride_b / BK + (row_idx.x % p.ne11) * p.stride_b / BK;
213#else
214 const uint ib = pos_b_ib + buf_ib * p.stride_b / BK;
215#endif
216 const uint iqs = loadr_b;
217
218 [[unroll]] for (uint k_step = 0; k_step < BK_STEP; k_step++) {
219 block_b_to_shmem(k_step * BN + buf_ib, ib + k_step, iqs, block + k_step * BK < end_k);
220 }
221 }
222
223 barrier();
224
225 pos_a_ib += BK_STEP;
226 pos_b_ib += BK_STEP;
227
228 for (uint k_step = 0; k_step < BK_STEP; k_step++) {
229 // Load from shared into cache
230 [[unroll]] for (uint wsir = 0; wsir < WMITER; wsir++) {
231 [[unroll]] for (uint cr = 0; cr < TM; cr++) {
232 const uint reg_ib = wsir * TM + cr;
233 const uint buf_ib = warp_r * WM + wsir * WSUBM + tiwr * TM + cr;
234
235 block_a_to_registers(reg_ib, k_step * BM + buf_ib);
236 }
237 }
238
239 [[unroll]] for (uint wsic = 0; wsic < WNITER; wsic++) {
240 [[unroll]] for (uint cc = 0; cc < TN; cc++) {
241 const uint ib = k_step * BN + warp_c * WN + wsic * WSUBN + tiwc * TN + cc;
242 block_b_to_registers(ib);
243
244 [[unroll]] for (uint wsir = 0; wsir < WMITER; wsir++) {
245 [[unroll]] for (uint cr = 0; cr < TM; cr++) {
246 const uint cache_a_idx = wsir * TM + cr;
247 const uint sums_idx = (wsic * TN + cc) * (WMITER * TM) + wsir * TM + cr;
248
249 sums[sums_idx] += mmq_dot_product(cache_a_idx);
250 }
251 }
252 }
253 }
254 }
255
256 barrier();
257 }
258
259 const uint dr = ir * BM + warp_r * WM;
260 const uint dc = ic * BN + warp_c * WN;
261
262#ifndef MUL_MAT_ID
263 const uint offsets = batch_idx * p.batch_stride_d + ik * p.batch_stride_d * p.num_batches;
264#endif
265
266 [[unroll]] for (uint wsic = 0; wsic < WNITER; wsic++) {
267 [[unroll]] for (uint wsir = 0; wsir < WMITER; wsir++) {
268
269 const uint dr_warp = dr + wsir * WSUBM + tiwr * TM;
270 const uint dc_warp = dc + wsic * WSUBN + tiwc * TN;
271 [[unroll]] for (uint cc = 0; cc < TN; cc++) {
272#ifdef MUL_MAT_ID
273 const uint row_i = dc_warp + cc;
274 if (row_i >= _ne1) break;
275
276 const u16vec2 row_idx = row_ids[row_i - ic * BN];
277#endif // MUL_MAT_ID
278 [[unroll]] for (uint cr = 0; cr < TM; cr++) {
279 const uint sums_idx = (wsic * TN + cc) * WMITER * TM + wsir * TM + cr;
280#ifdef MUL_MAT_ID
281 if (dr_warp + cr < p.M) {
282 data_d[row_idx.y * p.batch_stride_d + row_idx.x * p.stride_d + dr_warp + cr] = D_TYPE(sums[sums_idx].x);
283 }
284#else
285 if (dr_warp + cr < p.M && dc_warp + cc < p.N) {
286 data_d[offsets + (dc_warp + cc) * p.stride_d + dr_warp + cr] = D_TYPE(sums[sums_idx].x);
287 }
288#endif // MUL_MAT_ID
289 }
290 }
291 }
292 }
293}