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-opencl / kernels / gemm_noshuffle_q4_k_q8_1_dp4a.cl

419 lines · 16795 bytes Raw Blame History
1#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2#pragma OPENCL EXTENSION cl_khr_subgroups : enable
3#ifdef cl_khr_integer_dot_product
4#pragma OPENCL EXTENSION cl_khr_integer_dot_product : enable
5#endif
6
7#ifndef TILESIZE_N
8#define TILESIZE_N 32
9#endif
10#define QK_K 256
11#define K_SCALE_SIZE 12
12
13// scales are transposed: consecutive codes of a row are `stride` apart
14inline void get_scale_min_k4(
15 int j,
16 global const uchar * q,
17 uint stride,
18 uchar * d,
19 uchar * m,
20 uchar mask_d6,
21 uchar mask_d4,
22 uchar mask_hi2
23) {
24 if (j < 4) {
25 *d = q[j*stride] & mask_d6;
26 *m = q[(j+4)*stride] & mask_d6;
27 } else {
28 *d = (q[(j+4)*stride] & mask_d4) | ((q[(j-4)*stride] & mask_hi2) >> 2);
29 *m = ((q[(j+4)*stride] >> 4) & mask_d4) | ((q[j*stride] & mask_hi2) >> 2);
30 }
31}
32
33// Expand the 4 nibbles in the low 16 bits of `u` into 4 bytes (one nibble per
34// byte, value 0..15), packed for the int8 dp4a.
35#define EXP4(u) ( ((uint)((u) & 0x000Fu)) | \
36 (((uint)((u) & 0x00F0u)) << 4) | \
37 (((uint)((u) & 0x0F00u)) << 8) | \
38 (((uint)((u) & 0xF000u)) << 12) )
39
40// 32-K dp4a dot of one token's int8 activations (8 packed uints in lm) against the
41// row's 8 packed weight uints. qw passed by value as a uint8 (register), not an array.
42inline int dot8_q8a(uint8 qw, __local const uint * a) {
43 int r = 0;
44 r = dot_acc_sat_4x8packed_ss_int(qw.s0, a[0], r);
45 r = dot_acc_sat_4x8packed_ss_int(qw.s1, a[1], r);
46 r = dot_acc_sat_4x8packed_ss_int(qw.s2, a[2], r);
47 r = dot_acc_sat_4x8packed_ss_int(qw.s3, a[3], r);
48 r = dot_acc_sat_4x8packed_ss_int(qw.s4, a[4], r);
49 r = dot_acc_sat_4x8packed_ss_int(qw.s5, a[5], r);
50 r = dot_acc_sat_4x8packed_ss_int(qw.s6, a[6], r);
51 r = dot_acc_sat_4x8packed_ss_int(qw.s7, a[7], r);
52 return r;
53}
54
55__attribute__((qcom_wave_pair_mode(1)))
56kernel void kernel_gemm_noshuffle_q4_k_q8_1_dp4a(
57 __global const ushort * src0_q, // q4_K weights (noshuffle, packed nibbles)
58 __global const uchar * src0_s, // 6-bit scale/min codes
59 __global const half * src0_d, // per-superblock scale
60 __global const half * src0_dm, // per-superblock min
61 __global const uint * src1_qa, // q8_1 activations int8 (as uint, 4/elem) [N, K]
62 __global const half * src1_da, // q8_1 per-block scale [N, K/32]
63 __global const half * src1_sa, // q8_1 per-block sum*d [N, K/32]
64 __global float * dst,
65 ulong offsetd,
66 int m, // output features (rows)
67 int n_no_padding, // tokens (cols)
68 int k, // K (== ne00)
69 uchar mask_d6,
70 uchar mask_d4,
71 uchar mask_hi2
72) {
73 dst = (global float *)((global char *)dst + offsetd);
74
75 const uint lid = get_local_id(0); // 0..63 -> row within the M-tile
76 const uint block_id_m = get_global_id(1);
77 const uint block_id_n = get_global_id(2);
78
79 const uint row = block_id_m * 64 + lid;
80 const uint col_base = block_id_n * TILESIZE_N;
81 const bool row_valid = row < (uint)m;
82 const uint rrow = row_valid ? row : 0; // clamp OOB rows; their writes are masked
83
84 const uint k_u = (uint)k >> 2; // K in uint (int8x4) units
85 const uint k_b = (uint)k >> 5; // blocks-of-32 along K
86
87 __local uint sh_qa[TILESIZE_N][8];
88 __local half sh_d[TILESIZE_N];
89 __local half sh_s[TILESIZE_N];
90
91 // One float4 vector-register accumulator per group of 4 tokens (NGROUPS = TILESIZE_N/4).
92#define NGROUPS (TILESIZE_N / 4)
93 float4 acc[NGROUPS];
94 #pragma unroll
95 for (int g = 0; g < NGROUPS; ++g) { acc[g] = (float4)(0.0f); }
96
97 for (uint step = 0; step < (uint)k; step += 32) {
98 const uint sub = step >> 5;
99 const uint sb_idx = step / QK_K;
100 const uint sub_idx = sub & 7;
101
102 // weight scale/min for this WI's row, this subblock
103 const float dd = (float)src0_d [rrow + sb_idx * m];
104 const float dmm = (float)src0_dm[rrow + sb_idx * m];
105 global const uchar * sc = src0_s + sb_idx * K_SCALE_SIZE * (uint)m + rrow;
106 uchar sv, mn;
107 get_scale_min_k4(sub_idx, sc, (uint)m, &sv, &mn, mask_d6, mask_d4, mask_hi2);
108 const float scale = dd * (float)sv;
109 const float minv = dmm * (float)mn;
110
111 // repack this row's 32 weight nibbles into 8 dp4a uints. The packed q4_K
112 // layout stores one ushort = 4 consecutive-K nibbles for a row at
113 // src0_q[row + (K_group)*m], K_group = step/4 + u.
114 const uint wbase = rrow + (step >> 2) * (uint)m;
115 uint8 qw;
116 qw.s0 = EXP4(src0_q[wbase + 0 * m]);
117 qw.s1 = EXP4(src0_q[wbase + 1 * m]);
118 qw.s2 = EXP4(src0_q[wbase + 2 * m]);
119 qw.s3 = EXP4(src0_q[wbase + 3 * m]);
120 qw.s4 = EXP4(src0_q[wbase + 4 * m]);
121 qw.s5 = EXP4(src0_q[wbase + 5 * m]);
122 qw.s6 = EXP4(src0_q[wbase + 6 * m]);
123 qw.s7 = EXP4(src0_q[wbase + 7 * m]);
124
125 // cooperatively stage the 32-token x 32-K int8 activations to lm
126 for (uint idx = lid; idx < TILESIZE_N * 8; idx += 64) {
127 const uint t = idx >> 3;
128 const uint u = idx & 7;
129 const uint c = col_base + t;
130 sh_qa[t][u] = (c < (uint)n_no_padding) ? src1_qa[c * k_u + (step >> 2) + u] : 0u;
131 }
132 if (lid < TILESIZE_N) {
133 const uint c = col_base + lid;
134 sh_d[lid] = (c < (uint)n_no_padding) ? src1_da[c * k_b + sub] : (half)0;
135 sh_s[lid] = (c < (uint)n_no_padding) ? src1_sa[c * k_b + sub] : (half)0;
136 }
137 barrier(CLK_LOCAL_MEM_FENCE);
138
139#define LD4(arr, b) ((float4)((float)arr[(b)+0], (float)arr[(b)+1], (float)arr[(b)+2], (float)arr[(b)+3]))
140 #pragma unroll
141 for (int g = 0; g < NGROUPS; ++g) {
142 const int b = g * 4;
143 float4 rf;
144 rf.s0 = (float)dot8_q8a(qw, sh_qa[b+0]); rf.s1 = (float)dot8_q8a(qw, sh_qa[b+1]);
145 rf.s2 = (float)dot8_q8a(qw, sh_qa[b+2]); rf.s3 = (float)dot8_q8a(qw, sh_qa[b+3]);
146 acc[g] += scale * LD4(sh_d, b) * rf - minv * LD4(sh_s, b);
147 }
148#undef LD4
149 barrier(CLK_LOCAL_MEM_FENCE);
150 }
151
152 if (!row_valid) {
153 return;
154 }
155
156 // dst is [token, feature] row-major (stride m): dst[col*m + row]. Scatter each
157 // lane with a per-token padding guard (dst is non-contiguous in token).
158 #pragma unroll
159 for (int g = 0; g < NGROUPS; ++g) {
160 const uint b = (uint)(g * 4);
161 const float4 a = acc[g];
162 const uint c0 = col_base + b;
163 if (c0 + 0 < (uint)n_no_padding) dst[(c0 + 0) * (uint)m + row] = a.s0;
164 if (c0 + 1 < (uint)n_no_padding) dst[(c0 + 1) * (uint)m + row] = a.s1;
165 if (c0 + 2 < (uint)n_no_padding) dst[(c0 + 2) * (uint)m + row] = a.s2;
166 if (c0 + 3 < (uint)n_no_padding) dst[(c0 + 3) * (uint)m + row] = a.s3;
167 }
168#undef NGROUPS
169}
170
171__attribute__((qcom_wave_pair_mode(1)))
172kernel void kernel_gemm_noshuffle_q4_k_q8_1_dp4a_wimg(
173 __read_only image1d_buffer_t src0_q_img, // q4_K weights as uint32 texels (2 ushorts/texel)
174 __global const uchar * src0_s, // 6-bit scale/min codes
175 __global const half * src0_d, // per-superblock scale
176 __global const half * src0_dm, // per-superblock min
177 __global const uint * src1_qa, // q8_1 activations int8 (as uint, 4/elem) [N, K]
178 __global const half * src1_da, // q8_1 per-block scale [N, K/32]
179 __global const half * src1_sa, // q8_1 per-block sum*d [N, K/32]
180 __global float * dst,
181 ulong offsetd,
182 int m, // output features (rows)
183 int n_no_padding, // tokens (cols)
184 int k, // K (== ne00)
185 uchar mask_d6,
186 uchar mask_d4,
187 uchar mask_hi2
188) {
189 dst = (global float *)((global char *)dst + offsetd);
190
191 const uint lid = get_local_id(0); // 0..63 -> row within the M-tile
192 const uint block_id_m = get_global_id(1);
193 const uint block_id_n = get_global_id(2);
194
195 const uint row = block_id_m * 64 + lid;
196 const uint col_base = block_id_n * TILESIZE_N;
197 const bool row_valid = row < (uint)m;
198 const uint rrow = row_valid ? row : 0; // clamp OOB rows; their writes are masked
199
200 // Constant per WI: the ushort the row needs always sits in the same half of
201 // its uint32 texel (m even => index parity == rrow parity). Hoist the shift.
202 const uint sel = (rrow & 1u) * 16u;
203
204 const uint k_u = (uint)k >> 2; // K in uint (int8x4) units
205 const uint k_b = (uint)k >> 5; // blocks-of-32 along K
206
207 __local uint sh_qa[TILESIZE_N][8];
208 __local half sh_d[TILESIZE_N];
209 __local half sh_s[TILESIZE_N];
210
211#define NGROUPS (TILESIZE_N / 4)
212 float4 acc[NGROUPS];
213 #pragma unroll
214 for (int g = 0; g < NGROUPS; ++g) acc[g] = (float4)(0.0f);
215
216 for (uint step = 0; step < (uint)k; step += 32) {
217 const uint sub = step >> 5;
218 const uint sb_idx = step / QK_K;
219 const uint sub_idx = sub & 7;
220
221 const float dd = (float)src0_d [rrow + sb_idx * m];
222 const float dmm = (float)src0_dm[rrow + sb_idx * m];
223 global const uchar * sc = src0_s + sb_idx * K_SCALE_SIZE * (uint)m + rrow;
224 uchar sv, mn;
225 get_scale_min_k4(sub_idx, sc, (uint)m, &sv, &mn, mask_d6, mask_d4, mask_hi2);
226 const float scale = dd * (float)sv;
227 const float minv = dmm * (float)mn;
228
229 const uint wbase = rrow + (step >> 2) * (uint)m;
230 uint8 qw;
231 qw.s0 = EXP4(read_imageui(src0_q_img, (int)((wbase + 0 * m) >> 1)).x >> sel);
232 qw.s1 = EXP4(read_imageui(src0_q_img, (int)((wbase + 1 * m) >> 1)).x >> sel);
233 qw.s2 = EXP4(read_imageui(src0_q_img, (int)((wbase + 2 * m) >> 1)).x >> sel);
234 qw.s3 = EXP4(read_imageui(src0_q_img, (int)((wbase + 3 * m) >> 1)).x >> sel);
235 qw.s4 = EXP4(read_imageui(src0_q_img, (int)((wbase + 4 * m) >> 1)).x >> sel);
236 qw.s5 = EXP4(read_imageui(src0_q_img, (int)((wbase + 5 * m) >> 1)).x >> sel);
237 qw.s6 = EXP4(read_imageui(src0_q_img, (int)((wbase + 6 * m) >> 1)).x >> sel);
238 qw.s7 = EXP4(read_imageui(src0_q_img, (int)((wbase + 7 * m) >> 1)).x >> sel);
239
240 for (uint idx = lid; idx < TILESIZE_N * 8; idx += 64) {
241 const uint t = idx >> 3;
242 const uint u = idx & 7;
243 const uint c = col_base + t;
244 sh_qa[t][u] = (c < (uint)n_no_padding) ? src1_qa[c * k_u + (step >> 2) + u] : 0u;
245 }
246 if (lid < TILESIZE_N) {
247 const uint c = col_base + lid;
248 sh_d[lid] = (c < (uint)n_no_padding) ? src1_da[c * k_b + sub] : (half)0;
249 sh_s[lid] = (c < (uint)n_no_padding) ? src1_sa[c * k_b + sub] : (half)0;
250 }
251 barrier(CLK_LOCAL_MEM_FENCE);
252
253#define LD4(arr, b) ((float4)((float)arr[(b)+0], (float)arr[(b)+1], (float)arr[(b)+2], (float)arr[(b)+3]))
254 #pragma unroll
255 for (int g = 0; g < NGROUPS; ++g) {
256 const int b = g * 4;
257 float4 rf;
258 rf.s0 = (float)dot8_q8a(qw, sh_qa[b+0]); rf.s1 = (float)dot8_q8a(qw, sh_qa[b+1]);
259 rf.s2 = (float)dot8_q8a(qw, sh_qa[b+2]); rf.s3 = (float)dot8_q8a(qw, sh_qa[b+3]);
260 acc[g] += scale * LD4(sh_d, b) * rf - minv * LD4(sh_s, b);
261 }
262#undef LD4
263 barrier(CLK_LOCAL_MEM_FENCE);
264 }
265
266 if (!row_valid) {
267 return;
268 }
269
270 #pragma unroll
271 for (int g = 0; g < NGROUPS; ++g) {
272 const uint b = (uint)(g * 4);
273 const float4 a = acc[g];
274 const uint c0 = col_base + b;
275 if (c0 + 0 < (uint)n_no_padding) dst[(c0 + 0) * (uint)m + row] = a.s0;
276 if (c0 + 1 < (uint)n_no_padding) dst[(c0 + 1) * (uint)m + row] = a.s1;
277 if (c0 + 2 < (uint)n_no_padding) dst[(c0 + 2) * (uint)m + row] = a.s2;
278 if (c0 + 3 < (uint)n_no_padding) dst[(c0 + 3) * (uint)m + row] = a.s3;
279 }
280#undef NGROUPS
281}
282
283// Stage each 32-K activation block as two uint4 values.
284inline int dot8_q8a_v(uint8 qw, uint4 a0, uint4 a1) {
285 int r = 0;
286 r = dot_acc_sat_4x8packed_ss_int(qw.s0, a0.x, r);
287 r = dot_acc_sat_4x8packed_ss_int(qw.s1, a0.y, r);
288 r = dot_acc_sat_4x8packed_ss_int(qw.s2, a0.z, r);
289 r = dot_acc_sat_4x8packed_ss_int(qw.s3, a0.w, r);
290 r = dot_acc_sat_4x8packed_ss_int(qw.s4, a1.x, r);
291 r = dot_acc_sat_4x8packed_ss_int(qw.s5, a1.y, r);
292 r = dot_acc_sat_4x8packed_ss_int(qw.s6, a1.z, r);
293 r = dot_acc_sat_4x8packed_ss_int(qw.s7, a1.w, r);
294 return r;
295}
296
297__attribute__((qcom_wave_pair_mode(1)))
298kernel void kernel_gemm_noshuffle_q4_k_q8_1_dp4a_act_vload4(
299 __global const ushort * src0_q,
300 __global const uchar * src0_s,
301 __global const half * src0_d,
302 __global const half * src0_dm,
303 __global const uint * src1_qa,
304 __global const half * src1_da,
305 __global const half * src1_sa,
306 __global float * dst,
307 ulong offsetd,
308 int m,
309 int n_no_padding,
310 int k,
311 uchar mask_d6,
312 uchar mask_d4,
313 uchar mask_hi2
314) {
315 dst = (global float *)((global char *)dst + offsetd);
316
317 const uint lid = get_local_id(0);
318 const uint block_id_m = get_global_id(1);
319 const uint block_id_n = get_global_id(2);
320
321 const uint row = block_id_m * 64 + lid;
322 const uint col_base = block_id_n * TILESIZE_N;
323 const bool row_valid = row < (uint)m;
324 const uint rrow = row_valid ? row : 0;
325
326 const uint k_u = (uint)k >> 2;
327 const uint k_b = (uint)k >> 5;
328
329 __local uint4 sh_qa4[TILESIZE_N][2];
330 __local half sh_d[TILESIZE_N];
331 __local half sh_s[TILESIZE_N];
332
333#define NGROUPS (TILESIZE_N / 4)
334 float4 acc[NGROUPS];
335 #pragma unroll
336 for (int g = 0; g < NGROUPS; ++g) {
337 acc[g] = (float4)(0.0f);
338 }
339
340 for (uint step = 0; step < (uint)k; step += 32) {
341 const uint sub = step >> 5;
342 const uint sb_idx = step / QK_K;
343 const uint sub_idx = sub & 7;
344
345 const float dd = (float)src0_d [rrow + sb_idx * m];
346 const float dmm = (float)src0_dm[rrow + sb_idx * m];
347 global const uchar * sc = src0_s + sb_idx * K_SCALE_SIZE * (uint)m + rrow;
348 uchar sv, mn;
349 get_scale_min_k4(sub_idx, sc, (uint)m, &sv, &mn, mask_d6, mask_d4, mask_hi2);
350 const float scale = dd * (float)sv;
351 const float minv = dmm * (float)mn;
352
353 const uint wbase = rrow + (step >> 2) * (uint)m;
354 uint8 qw;
355 qw.s0 = EXP4(src0_q[wbase + 0 * m]);
356 qw.s1 = EXP4(src0_q[wbase + 1 * m]);
357 qw.s2 = EXP4(src0_q[wbase + 2 * m]);
358 qw.s3 = EXP4(src0_q[wbase + 3 * m]);
359 qw.s4 = EXP4(src0_q[wbase + 4 * m]);
360 qw.s5 = EXP4(src0_q[wbase + 5 * m]);
361 qw.s6 = EXP4(src0_q[wbase + 6 * m]);
362 qw.s7 = EXP4(src0_q[wbase + 7 * m]);
363
364 for (uint idx = lid; idx < TILESIZE_N * 2; idx += 64) {
365 const uint t = idx >> 1;
366 const uint v = idx & 1;
367 const uint c = col_base + t;
368 sh_qa4[t][v] = (c < (uint)n_no_padding)
369 ? vload4(0, src1_qa + c * k_u + (step >> 2) + (v << 2))
370 : (uint4)(0u);
371 }
372 if (lid < TILESIZE_N) {
373 const uint c = col_base + lid;
374 sh_d[lid] = (c < (uint)n_no_padding) ? src1_da[c * k_b + sub] : (half)0;
375 sh_s[lid] = (c < (uint)n_no_padding) ? src1_sa[c * k_b + sub] : (half)0;
376 }
377 barrier(CLK_LOCAL_MEM_FENCE);
378
379#define LD4(arr, b) ((float4)((float)arr[(b)+0], (float)arr[(b)+1], (float)arr[(b)+2], (float)arr[(b)+3]))
380 #pragma unroll
381 for (int g = 0; g < NGROUPS; ++g) {
382 const int b = g * 4;
383 float4 rf;
384#define DOTV(T) dot8_q8a_v(qw, sh_qa4[T][0], sh_qa4[T][1])
385 rf.s0 = (float)DOTV(b+0);
386 rf.s1 = (float)DOTV(b+1);
387 rf.s2 = (float)DOTV(b+2);
388 rf.s3 = (float)DOTV(b+3);
389#undef DOTV
390 acc[g] += scale * LD4(sh_d, b) * rf - minv * LD4(sh_s, b);
391 }
392#undef LD4
393 barrier(CLK_LOCAL_MEM_FENCE);
394 }
395
396 if (!row_valid) {
397 return;
398 }
399
400 #pragma unroll
401 for (int g = 0; g < NGROUPS; ++g) {
402 const uint b = (uint)(g * 4);
403 const float4 a = acc[g];
404 const uint c0 = col_base + b;
405 if (c0 + 0 < (uint)n_no_padding) {
406 dst[(c0 + 0) * (uint)m + row] = a.s0;
407 }
408 if (c0 + 1 < (uint)n_no_padding) {
409 dst[(c0 + 1) * (uint)m + row] = a.s1;
410 }
411 if (c0 + 2 < (uint)n_no_padding) {
412 dst[(c0 + 2) * (uint)m + row] = a.s2;
413 }
414 if (c0 + 3 < (uint)n_no_padding) {
415 dst[(c0 + 3) * (uint)m + row] = a.s3;
416 }
417 }
418#undef NGROUPS
419}