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_q8_0_q8_1_dp4a.cl

334 lines · 12288 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// ne1<=8 keeps the f16 / bin small-batch path.
8
9#define TILESIZE_N 32
10
11// 32-K dp4a dot of one token's int8 activations (8 packed uints in lm) against
12// 8 packed weight uints. q8_0 weights are already dp4a-format signed int8.
13inline int dot8_q8a(uint8 qw, __local const uint * a) {
14 int r = 0;
15 r = dot_acc_sat_4x8packed_ss_int(qw.s0, a[0], r);
16 r = dot_acc_sat_4x8packed_ss_int(qw.s1, a[1], r);
17 r = dot_acc_sat_4x8packed_ss_int(qw.s2, a[2], r);
18 r = dot_acc_sat_4x8packed_ss_int(qw.s3, a[3], r);
19 r = dot_acc_sat_4x8packed_ss_int(qw.s4, a[4], r);
20 r = dot_acc_sat_4x8packed_ss_int(qw.s5, a[5], r);
21 r = dot_acc_sat_4x8packed_ss_int(qw.s6, a[6], r);
22 r = dot_acc_sat_4x8packed_ss_int(qw.s7, a[7], r);
23 return r;
24}
25
26__attribute__((qcom_wave_pair_mode(1)))
27kernel void kernel_gemm_noshuffle_q8_0_q8_1_dp4a(
28 __global const uint * src0_q, // q8_0 weights: signed int8, 4/uint, feature-major
29 __global const half * src0_d, // per-32-block scale, feature-major [row + (k/32)*m]
30 __global const uint * src1_qa, // q8_1 activations int8 (as uint, 4/elem) [N, K]
31 __global const half * src1_da, // q8_1 per-block scale [N, K/32]
32 __global float * dst,
33 ulong offsetd,
34 int m, // output features (rows)
35 int n_no_padding, // tokens (cols)
36 int k // K (== ne00)
37) {
38 dst = (global float *)((global char *)dst + offsetd);
39
40 const uint lid = get_local_id(0); // 0..63 -> row within the M-tile
41 const uint block_id_m = get_global_id(1);
42 const uint block_id_n = get_global_id(2);
43
44 const uint row = block_id_m * 64 + lid;
45 const uint col_base = block_id_n * TILESIZE_N;
46 const bool row_valid = row < (uint)m;
47 const uint rrow = row_valid ? row : 0; // clamp OOB rows; their writes are masked
48
49 const uint k_u = (uint)k >> 2; // K in uint (int8x4) units
50 const uint k_b = (uint)k >> 5; // blocks-of-32 along K
51
52 __local uint sh_qa[TILESIZE_N][8];
53 __local half sh_d[TILESIZE_N];
54
55#define NGROUPS (TILESIZE_N / 4)
56 float4 acc[NGROUPS];
57 #pragma unroll
58 for (int g = 0; g < NGROUPS; ++g) acc[g] = (float4)(0.0f);
59
60 for (uint step = 0; step < (uint)k; step += 32) {
61 const uint sub = step >> 5;
62
63 const float d_w = (float)src0_d[rrow + sub * (uint)m];
64
65 // 8 weight uints (32 int8) for this row, this 32-block. Feature-major:
66 // src0_q[row + (k/4 + u)*m], k/4 = step/4 (= step>>2).
67 const uint wbase = rrow + (step >> 2) * (uint)m;
68 uint8 qw;
69 qw.s0 = src0_q[wbase + 0 * m];
70 qw.s1 = src0_q[wbase + 1 * m];
71 qw.s2 = src0_q[wbase + 2 * m];
72 qw.s3 = src0_q[wbase + 3 * m];
73 qw.s4 = src0_q[wbase + 4 * m];
74 qw.s5 = src0_q[wbase + 5 * m];
75 qw.s6 = src0_q[wbase + 6 * m];
76 qw.s7 = src0_q[wbase + 7 * m];
77
78 // cooperatively stage the 32-token x 32-K int8 activations to LDS
79 for (uint idx = lid; idx < TILESIZE_N * 8; idx += 64) {
80 const uint t = idx >> 3;
81 const uint u = idx & 7;
82 const uint c = col_base + t;
83 sh_qa[t][u] = (c < (uint)n_no_padding) ? src1_qa[c * k_u + (step >> 2) + u] : 0u;
84 }
85 if (lid < TILESIZE_N) {
86 const uint c = col_base + lid;
87 sh_d[lid] = (c < (uint)n_no_padding) ? src1_da[c * k_b + sub] : (half)0;
88 }
89 barrier(CLK_LOCAL_MEM_FENCE);
90
91#define LD4(arr, b) ((float4)((float)arr[(b)+0], (float)arr[(b)+1], (float)arr[(b)+2], (float)arr[(b)+3]))
92 #pragma unroll
93 for (int g = 0; g < NGROUPS; ++g) {
94 const int b = g * 4;
95 float4 rf;
96 rf.s0 = (float)dot8_q8a(qw, sh_qa[b+0]); rf.s1 = (float)dot8_q8a(qw, sh_qa[b+1]);
97 rf.s2 = (float)dot8_q8a(qw, sh_qa[b+2]); rf.s3 = (float)dot8_q8a(qw, sh_qa[b+3]);
98 acc[g] += d_w * LD4(sh_d, b) * rf;
99 }
100#undef LD4
101 barrier(CLK_LOCAL_MEM_FENCE);
102 }
103
104 if (!row_valid) {
105 return;
106 }
107
108 // dst is [token, feature] row-major (stride m): dst[col*m + row].
109 #pragma unroll
110 for (int g = 0; g < NGROUPS; ++g) {
111 const uint b = (uint)(g * 4);
112 const float4 a = acc[g];
113 const uint c0 = col_base + b;
114 if (c0 + 0 < (uint)n_no_padding) dst[(c0 + 0) * (uint)m + row] = a.s0;
115 if (c0 + 1 < (uint)n_no_padding) dst[(c0 + 1) * (uint)m + row] = a.s1;
116 if (c0 + 2 < (uint)n_no_padding) dst[(c0 + 2) * (uint)m + row] = a.s2;
117 if (c0 + 3 < (uint)n_no_padding) dst[(c0 + 3) * (uint)m + row] = a.s3;
118 }
119#undef NGROUPS
120}
121
122__attribute__((qcom_wave_pair_mode(1)))
123kernel void kernel_gemm_noshuffle_q8_0_q8_1_dp4a_wimg(
124 __read_only image1d_buffer_t src0_q_img, // q8_0 weights as uint32 texels (4 int8/texel)
125 __global const half * src0_d,
126 __global const uint * src1_qa,
127 __global const half * src1_da,
128 __global float * dst,
129 ulong offsetd,
130 int m,
131 int n_no_padding,
132 int k
133) {
134 dst = (global float *)((global char *)dst + offsetd);
135
136 const uint lid = get_local_id(0);
137 const uint block_id_m = get_global_id(1);
138 const uint block_id_n = get_global_id(2);
139
140 const uint row = block_id_m * 64 + lid;
141 const uint col_base = block_id_n * TILESIZE_N;
142 const bool row_valid = row < (uint)m;
143 const uint rrow = row_valid ? row : 0;
144
145 const uint k_u = (uint)k >> 2;
146 const uint k_b = (uint)k >> 5;
147
148 __local uint sh_qa[TILESIZE_N][8];
149 __local half sh_d[TILESIZE_N];
150
151#define NGROUPS (TILESIZE_N / 4)
152 float4 acc[NGROUPS];
153 #pragma unroll
154 for (int g = 0; g < NGROUPS; ++g) acc[g] = (float4)(0.0f);
155
156 for (uint step = 0; step < (uint)k; step += 32) {
157 const uint sub = step >> 5;
158
159 const float d_w = (float)src0_d[rrow + sub * (uint)m];
160
161 const uint wbase = rrow + (step >> 2) * (uint)m;
162 uint8 qw;
163 qw.s0 = read_imageui(src0_q_img, (int)(wbase + 0 * m)).x;
164 qw.s1 = read_imageui(src0_q_img, (int)(wbase + 1 * m)).x;
165 qw.s2 = read_imageui(src0_q_img, (int)(wbase + 2 * m)).x;
166 qw.s3 = read_imageui(src0_q_img, (int)(wbase + 3 * m)).x;
167 qw.s4 = read_imageui(src0_q_img, (int)(wbase + 4 * m)).x;
168 qw.s5 = read_imageui(src0_q_img, (int)(wbase + 5 * m)).x;
169 qw.s6 = read_imageui(src0_q_img, (int)(wbase + 6 * m)).x;
170 qw.s7 = read_imageui(src0_q_img, (int)(wbase + 7 * m)).x;
171
172 for (uint idx = lid; idx < TILESIZE_N * 8; idx += 64) {
173 const uint t = idx >> 3;
174 const uint u = idx & 7;
175 const uint c = col_base + t;
176 sh_qa[t][u] = (c < (uint)n_no_padding) ? src1_qa[c * k_u + (step >> 2) + u] : 0u;
177 }
178 if (lid < TILESIZE_N) {
179 const uint c = col_base + lid;
180 sh_d[lid] = (c < (uint)n_no_padding) ? src1_da[c * k_b + sub] : (half)0;
181 }
182 barrier(CLK_LOCAL_MEM_FENCE);
183
184#define LD4(arr, b) ((float4)((float)arr[(b)+0], (float)arr[(b)+1], (float)arr[(b)+2], (float)arr[(b)+3]))
185 #pragma unroll
186 for (int g = 0; g < NGROUPS; ++g) {
187 const int b = g * 4;
188 float4 rf;
189 rf.s0 = (float)dot8_q8a(qw, sh_qa[b+0]); rf.s1 = (float)dot8_q8a(qw, sh_qa[b+1]);
190 rf.s2 = (float)dot8_q8a(qw, sh_qa[b+2]); rf.s3 = (float)dot8_q8a(qw, sh_qa[b+3]);
191 acc[g] += d_w * LD4(sh_d, b) * rf;
192 }
193#undef LD4
194 barrier(CLK_LOCAL_MEM_FENCE);
195 }
196
197 if (!row_valid) {
198 return;
199 }
200
201 #pragma unroll
202 for (int g = 0; g < NGROUPS; ++g) {
203 const uint b = (uint)(g * 4);
204 const float4 a = acc[g];
205 const uint c0 = col_base + b;
206 if (c0 + 0 < (uint)n_no_padding) dst[(c0 + 0) * (uint)m + row] = a.s0;
207 if (c0 + 1 < (uint)n_no_padding) dst[(c0 + 1) * (uint)m + row] = a.s1;
208 if (c0 + 2 < (uint)n_no_padding) dst[(c0 + 2) * (uint)m + row] = a.s2;
209 if (c0 + 3 < (uint)n_no_padding) dst[(c0 + 3) * (uint)m + row] = a.s3;
210 }
211#undef NGROUPS
212}
213
214// Stage each 32-K activation block as two uint4 values.
215inline int dot8_q8a_v(uint8 qw, uint4 a0, uint4 a1) {
216 int r = 0;
217 r = dot_acc_sat_4x8packed_ss_int(qw.s0, a0.x, r);
218 r = dot_acc_sat_4x8packed_ss_int(qw.s1, a0.y, r);
219 r = dot_acc_sat_4x8packed_ss_int(qw.s2, a0.z, r);
220 r = dot_acc_sat_4x8packed_ss_int(qw.s3, a0.w, r);
221 r = dot_acc_sat_4x8packed_ss_int(qw.s4, a1.x, r);
222 r = dot_acc_sat_4x8packed_ss_int(qw.s5, a1.y, r);
223 r = dot_acc_sat_4x8packed_ss_int(qw.s6, a1.z, r);
224 r = dot_acc_sat_4x8packed_ss_int(qw.s7, a1.w, r);
225 return r;
226}
227
228__attribute__((qcom_wave_pair_mode(1)))
229kernel void kernel_gemm_noshuffle_q8_0_q8_1_dp4a_act_vload4(
230 __global const uint * src0_q,
231 __global const half * src0_d,
232 __global const uint * src1_qa,
233 __global const half * src1_da,
234 __global float * dst,
235 ulong offsetd,
236 int m,
237 int n_no_padding,
238 int k
239) {
240 dst = (global float *)((global char *)dst + offsetd);
241
242 const uint lid = get_local_id(0);
243 const uint block_id_m = get_global_id(1);
244 const uint block_id_n = get_global_id(2);
245
246 const uint row = block_id_m * 64 + lid;
247 const uint col_base = block_id_n * TILESIZE_N;
248 const bool row_valid = row < (uint)m;
249 const uint rrow = row_valid ? row : 0;
250
251 const uint k_u = (uint)k >> 2;
252 const uint k_b = (uint)k >> 5;
253
254 __local uint4 sh_qa4[TILESIZE_N][2];
255 __local half sh_d[TILESIZE_N];
256
257#define NGROUPS (TILESIZE_N / 4)
258 float4 acc[NGROUPS];
259 #pragma unroll
260 for (int g = 0; g < NGROUPS; ++g) {
261 acc[g] = (float4)(0.0f);
262 }
263
264 for (uint step = 0; step < (uint)k; step += 32) {
265 const uint sub = step >> 5;
266
267 const float d_w = (float)src0_d[rrow + sub * (uint)m];
268
269 const uint wbase = rrow + (step >> 2) * (uint)m;
270 uint8 qw;
271 qw.s0 = src0_q[wbase + 0 * m];
272 qw.s1 = src0_q[wbase + 1 * m];
273 qw.s2 = src0_q[wbase + 2 * m];
274 qw.s3 = src0_q[wbase + 3 * m];
275 qw.s4 = src0_q[wbase + 4 * m];
276 qw.s5 = src0_q[wbase + 5 * m];
277 qw.s6 = src0_q[wbase + 6 * m];
278 qw.s7 = src0_q[wbase + 7 * m];
279
280 for (uint idx = lid; idx < TILESIZE_N * 2; idx += 64) {
281 const uint t = idx >> 1;
282 const uint v = idx & 1;
283 const uint c = col_base + t;
284 sh_qa4[t][v] = (c < (uint)n_no_padding)
285 ? vload4(0, src1_qa + c * k_u + (step >> 2) + (v << 2))
286 : (uint4)(0u);
287 }
288 if (lid < TILESIZE_N) {
289 const uint c = col_base + lid;
290 sh_d[lid] = (c < (uint)n_no_padding) ? src1_da[c * k_b + sub] : (half)0;
291 }
292 barrier(CLK_LOCAL_MEM_FENCE);
293
294#define LD4(arr, b) ((float4)((float)arr[(b)+0], (float)arr[(b)+1], (float)arr[(b)+2], (float)arr[(b)+3]))
295 #pragma unroll
296 for (int g = 0; g < NGROUPS; ++g) {
297 const int b = g * 4;
298 float4 rf;
299#define DOTV(T) dot8_q8a_v(qw, sh_qa4[T][0], sh_qa4[T][1])
300 rf.s0 = (float)DOTV(b+0);
301 rf.s1 = (float)DOTV(b+1);
302 rf.s2 = (float)DOTV(b+2);
303 rf.s3 = (float)DOTV(b+3);
304#undef DOTV
305 acc[g] += d_w * LD4(sh_d, b) * rf;
306 }
307#undef LD4
308 barrier(CLK_LOCAL_MEM_FENCE);
309 }
310
311 if (!row_valid) {
312 return;
313 }
314
315 #pragma unroll
316 for (int g = 0; g < NGROUPS; ++g) {
317 const uint b = (uint)(g * 4);
318 const float4 a = acc[g];
319 const uint c0 = col_base + b;
320 if (c0 + 0 < (uint)n_no_padding) {
321 dst[(c0 + 0) * (uint)m + row] = a.s0;
322 }
323 if (c0 + 1 < (uint)n_no_padding) {
324 dst[(c0 + 1) * (uint)m + row] = a.s1;
325 }
326 if (c0 + 2 < (uint)n_no_padding) {
327 dst[(c0 + 2) * (uint)m + row] = a.s2;
328 }
329 if (c0 + 3 < (uint)n_no_padding) {
330 dst[(c0 + 3) * (uint)m + row] = a.s3;
331 }
332 }
333#undef NGROUPS
334}