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

264 lines · 10520 bytes Raw Blame History
1// Cooperative-K DP4A GEMM for 2 to 4 columns with Q4_K weights and Q8_1 activations.
2// Named vector components avoid dynamic private-array indexing, which can cause register spills.
3
4#pragma OPENCL EXTENSION cl_khr_fp16 : enable
5#ifdef cl_khr_integer_dot_product
6#pragma OPENCL EXTENSION cl_khr_integer_dot_product : enable
7#endif
8
9#define QK_K 256
10#define K_SCALE_SIZE 12
11
12#ifndef COOPK_NSG
13#define COOPK_NSG 4
14#endif
15#define COOPK_SG 64
16
17#ifndef COOPK_ROWS
18#define COOPK_ROWS 4
19#endif
20#ifndef COOPK_COLS
21#define COOPK_COLS 4
22#endif
23
24#if COOPK_COLS == 4
25typedef float4 coopk_accv;
26typedef int4 coopk_dotv;
27#define COOPK_CONVF convert_float4
28#else
29typedef float2 coopk_accv;
30typedef int2 coopk_dotv;
31#define COOPK_CONVF convert_float2
32#endif
33
34// Expand four Q4_K nibbles into bytes for 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#if COOPK_ROWS == 4
41#define COOPK_DOT(ci, t) \
42 s0.s##ci = dot_acc_sat_4x8packed_ss_int(w0, A##ci.s##t, s0.s##ci); \
43 s1.s##ci = dot_acc_sat_4x8packed_ss_int(w1, A##ci.s##t, s1.s##ci); \
44 s2.s##ci = dot_acc_sat_4x8packed_ss_int(w2, A##ci.s##t, s2.s##ci); \
45 s3.s##ci = dot_acc_sat_4x8packed_ss_int(w3, A##ci.s##t, s3.s##ci);
46#else
47#define COOPK_DOT(ci, t) \
48 s0.s##ci = dot_acc_sat_4x8packed_ss_int(w0, A##ci.s##t, s0.s##ci); \
49 s1.s##ci = dot_acc_sat_4x8packed_ss_int(w1, A##ci.s##t, s1.s##ci);
50#endif
51
52#if COOPK_COLS == 4
53#define COOPK_DOTS_AT(t) COOPK_DOT(0,t) COOPK_DOT(1,t) COOPK_DOT(2,t) COOPK_DOT(3,t)
54#define COOPK_FOR_COLS(F) F(0) F(1) F(2) F(3)
55#else
56#define COOPK_DOTS_AT(t) COOPK_DOT(0,t) COOPK_DOT(1,t)
57#define COOPK_FOR_COLS(F) F(0) F(1)
58#endif
59
60// The bin layout packs eight K values in each uint, with four nibbles in each 16-bit half.
61// Since ku0 is a multiple of four, t / 2 selects the uint and t & 1 selects its half.
62#ifdef COOPK_Q_BIN
63#define COOPK_QBITS4(t) convert_ushort4(((t) & 1) ? (vload4(0, src0_q + row0 + ((ku0 >> 1) + ((t) >> 1)) * m) >> 16) \
64 : (vload4(0, src0_q + row0 + ((ku0 >> 1) + ((t) >> 1)) * m) & 0xFFFFu))
65#define COOPK_QBITS2(t) convert_ushort2(((t) & 1) ? (vload2(0, src0_q + row0 + ((ku0 >> 1) + ((t) >> 1)) * m) >> 16) \
66 : (vload2(0, src0_q + row0 + ((ku0 >> 1) + ((t) >> 1)) * m) & 0xFFFFu))
67#else
68#define COOPK_QBITS4(t) vload4(0, src0_q + row0 + (ku0 + t) * m)
69#define COOPK_QBITS2(t) vload2(0, src0_q + row0 + (ku0 + t) * m)
70#endif
71
72#if COOPK_ROWS == 4
73#define COOPK_KSTEP(t) \
74 { \
75 ushort4 bits = COOPK_QBITS4(t); \
76 const uint w0 = EXP4(bits.s0); \
77 const uint w1 = EXP4(bits.s1); \
78 const uint w2 = EXP4(bits.s2); \
79 const uint w3 = EXP4(bits.s3); \
80 COOPK_DOTS_AT(t) \
81 }
82#else
83#define COOPK_KSTEP(t) \
84 { \
85 ushort2 bits = COOPK_QBITS2(t); \
86 const uint w0 = EXP4(bits.s0); \
87 const uint w1 = EXP4(bits.s1); \
88 COOPK_DOTS_AT(t) \
89 }
90#endif
91
92inline void get_scale_min_k4_c(int j, global const uchar * q, int stride,
93 uchar * d, uchar * m,
94 uchar mask_d6, uchar mask_d4, uchar mask_hi2) {
95 if (j < 4) {
96 *d = q[j*stride] & mask_d6;
97 *m = q[(j + 4)*stride] & mask_d6;
98 } else {
99 *d = (q[(j + 4)*stride] & mask_d4) | ((q[(j - 4)*stride] >> 6) << 4);
100 *m = (q[(j + 4)*stride] >> 4) | ((q[(j )*stride] >> 6) << 4);
101 }
102}
103
104// Weights use the untiled Adreno noshuffle SoA layout; COOPK_Q_BIN selects uint-transposed q instead of ushort-transposed q.
105// Q8_1 activation planes are K-major (K contiguous); no activation transpose is used.
106kernel void kernel_gemm_coopk_q4_k_q8_1_dp4a(
107#ifdef COOPK_Q_BIN
108 global const uint * src0_q, // eight consecutive K nibbles per uint, [k/8][m]
109#else
110 global const ushort * src0_q, // four consecutive K nibbles per ushort, [k/4][m]
111#endif
112 global const uchar * src0_s, // packed 6-bit scales/mins, [k/256][12][m]
113 global const half * src0_d, // super-block scales, [k/256][m]
114 global const half * src0_dm, // super-block mins, [k/256][m]
115 global const uint * src1_qa, // four signed activation bytes per uint, [n][k/4]
116 global const half * src1_da, // activation scales, [n][k/32]
117 global const half * src1_sa, // scaled activation sums, [n][k/32]
118 global float * dst,
119 ulong offsetd,
120 int m,
121 int n,
122 int k,
123 int n_no_padding,
124 uchar mask_d6,
125 uchar mask_d4,
126 uchar mask_hi2
127) {
128 dst = (global float *)((global char *)dst + offsetd);
129
130 const int gx = get_global_id(0); // row group
131 const int sg = get_local_id(1); // K-split subgroup
132 const int lane = get_local_id(0);
133
134 const int row0 = gx * COOPK_ROWS;
135 const int num_32blk = k / 32;
136 const int k_u = k >> 2; // K in uint (int8x4) units
137 const int k_b = k >> 5; // 32-blocks along K
138
139 // The host pads activations to COOPK_COLS; stores mask columns beyond n_no_padding.
140 const int nl = n - 1;
141 const int c0 = 0;
142 const int c1 = (1 < n) ? 1 : nl;
143#if COOPK_COLS >= 4
144 const int c2 = (2 < n) ? 2 : nl;
145 const int c3 = (3 < n) ? 3 : nl;
146#endif
147
148 coopk_accv acc0 = (coopk_accv)(0.0f), acc1 = (coopk_accv)(0.0f);
149#if COOPK_ROWS == 4
150 coopk_accv acc2 = (coopk_accv)(0.0f), acc3 = (coopk_accv)(0.0f);
151#endif
152
153 for (int blk = sg; blk < num_32blk; blk += COOPK_NSG) {
154 const int i = blk << 5;
155 const int sb_idx = blk >> 3;
156 const int sub_idx = blk & 7;
157
158 global const uchar * sc = src0_s + sb_idx * K_SCALE_SIZE * m + row0;
159 uchar sv0, mn0, sv1, mn1;
160 get_scale_min_k4_c(sub_idx, sc + 0, m, &sv0, &mn0, mask_d6, mask_d4, mask_hi2);
161 get_scale_min_k4_c(sub_idx, sc + 1, m, &sv1, &mn1, mask_d6, mask_d4, mask_hi2);
162#if COOPK_ROWS == 4
163 half4 dd = vload4(0, src0_d + row0 + sb_idx * m);
164 half4 dmm = vload4(0, src0_dm + row0 + sb_idx * m);
165 uchar sv2, mn2, sv3, mn3;
166 get_scale_min_k4_c(sub_idx, sc + 2, m, &sv2, &mn2, mask_d6, mask_d4, mask_hi2);
167 get_scale_min_k4_c(sub_idx, sc + 3, m, &sv3, &mn3, mask_d6, mask_d4, mask_hi2);
168 const float sc2 = (float)dd.s2 * (float)sv2, mv2 = (float)dmm.s2 * (float)mn2;
169 const float sc3 = (float)dd.s3 * (float)sv3, mv3 = (float)dmm.s3 * (float)mn3;
170#else
171 half2 dd = vload2(0, src0_d + row0 + sb_idx * m);
172 half2 dmm = vload2(0, src0_dm + row0 + sb_idx * m);
173#endif
174 const float sc0 = (float)dd.s0 * (float)sv0, mv0 = (float)dmm.s0 * (float)mn0;
175 const float sc1 = (float)dd.s1 * (float)sv1, mv1 = (float)dmm.s1 * (float)mn1;
176
177 // Q4_K scale and min values change every 32 K values.
178 coopk_dotv s0 = (coopk_dotv)(0), s1 = (coopk_dotv)(0);
179#if COOPK_ROWS == 4
180 coopk_dotv s2 = (coopk_dotv)(0), s3 = (coopk_dotv)(0);
181#endif
182
183 for (int uq = 0; uq < 2; ++uq) {
184 const int ku0 = (i >> 2) + uq * 4;
185
186 uint4 A0 = vload4(0, src1_qa + (uint)c0 * k_u + ku0);
187 uint4 A1 = vload4(0, src1_qa + (uint)c1 * k_u + ku0);
188#if COOPK_COLS >= 4
189 uint4 A2 = vload4(0, src1_qa + (uint)c2 * k_u + ku0);
190 uint4 A3 = vload4(0, src1_qa + (uint)c3 * k_u + ku0);
191#endif
192 COOPK_KSTEP(0)
193 COOPK_KSTEP(1)
194 COOPK_KSTEP(2)
195 COOPK_KSTEP(3)
196 }
197
198 // Q4_K stores q * scale - min for each 32-value block.
199 // src1_sa already includes the activation scale, so the min term needs no extra factor of da.
200 coopk_accv da, sa;
201 da.s0 = (float)src1_da[c0*k_b + blk]; sa.s0 = (float)src1_sa[c0*k_b + blk];
202 da.s1 = (float)src1_da[c1*k_b + blk]; sa.s1 = (float)src1_sa[c1*k_b + blk];
203#if COOPK_COLS >= 4
204 da.s2 = (float)src1_da[c2*k_b + blk]; sa.s2 = (float)src1_sa[c2*k_b + blk];
205 da.s3 = (float)src1_da[c3*k_b + blk]; sa.s3 = (float)src1_sa[c3*k_b + blk];
206#endif
207
208 acc0 += sc0 * da * COOPK_CONVF(s0) - mv0 * sa;
209 acc1 += sc1 * da * COOPK_CONVF(s1) - mv1 * sa;
210#if COOPK_ROWS == 4
211 acc2 += sc2 * da * COOPK_CONVF(s2) - mv2 * sa;
212 acc3 += sc3 * da * COOPK_CONVF(s3) - mv3 * sa;
213#endif
214 }
215
216 // Reuse one local buffer for all rows; the first barrier protects the previous row's reads.
217 local coopk_accv reduceLM[COOPK_SG * (COOPK_NSG - 1)];
218 coopk_accv out0 = (coopk_accv)(0.0f), out1 = (coopk_accv)(0.0f);
219#if COOPK_ROWS == 4
220 coopk_accv out2 = (coopk_accv)(0.0f), out3 = (coopk_accv)(0.0f);
221#endif
222
223#define COOPK_REDUCE(accv, outv) \
224 barrier(CLK_LOCAL_MEM_FENCE); \
225 if (sg > 0) { reduceLM[(sg - 1) * COOPK_SG + lane] = (accv); } \
226 barrier(CLK_LOCAL_MEM_FENCE); \
227 if (sg == 0) { \
228 coopk_accv sum = (accv); \
229 for (int s = 0; s < COOPK_NSG - 1; s++) { \
230 sum += reduceLM[s * COOPK_SG + lane]; \
231 } \
232 (outv) = sum; \
233 }
234
235 COOPK_REDUCE(acc0, out0)
236 COOPK_REDUCE(acc1, out1)
237#if COOPK_ROWS == 4
238 COOPK_REDUCE(acc2, out2)
239 COOPK_REDUCE(acc3, out3)
240#endif
241
242#undef COOPK_REDUCE
243
244#if COOPK_ROWS == 4
245#define COOPK_STORE_COL(ci) \
246 if (idx < m*n_no_padding) { \
247 vstore4((float4)(out0.s##ci, out1.s##ci, out2.s##ci, out3.s##ci), 0, dst + idx); \
248 idx += m; \
249 }
250#else
251#define COOPK_STORE_COL(ci) \
252 if (idx < m*n_no_padding) { \
253 vstore2((float2)(out0.s##ci, out1.s##ci), 0, dst + idx); \
254 idx += m; \
255 }
256#endif
257
258 if (sg == 0) {
259 int idx = row0;
260 COOPK_FOR_COLS(COOPK_STORE_COL)
261 }
262
263#undef COOPK_STORE_COL
264}