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

216 lines · 8736 bytes Raw Blame History
1// Cooperative-K DP4A GEMM for 2 to 4 columns with Q6_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#ifndef COOPK_NSG
10#define COOPK_NSG 4
11#endif
12#define COOPK_SG 64
13
14#ifndef COOPK_ROWS
15#define COOPK_ROWS 4
16#endif
17#ifndef COOPK_COLS
18#define COOPK_COLS 4
19#endif
20
21#if COOPK_COLS == 4
22typedef float4 coopk_accv;
23typedef int4 coopk_dotv;
24#define COOPK_CONVF convert_float4
25#else
26typedef float2 coopk_accv;
27typedef int2 coopk_dotv;
28#define COOPK_CONVF convert_float2
29#endif
30
31// Combine low nibbles and high bits, subtract 32, and pack four signed bytes for DP4A.
32#define EXP6(l, h, mf, mc) \
33 ( (((uint)((int)((( (l) & 0x000Fu) | (((h) & 0x03u) << 4)) - 32) & 0xFF))) \
34 | (((uint)((int)(((((l) & 0x00F0u) >> 4) | (((h) & 0x0Cu) << 2)) - 32) & 0xFF)) << 8) \
35 | (((uint)((int)(((((l) & 0x0F00u) >> 8) | ((h) & 0x30u)) - 32) & 0xFF)) << 16) \
36 | (((uint)((int)(((((l) & (mf)) >> 12) | (((h) & (mc)) >> 2)) - 32) & 0xFF)) << 24) )
37
38#if COOPK_ROWS == 4
39#define COOPK_DOT(ci, t) \
40 d0.s##ci = dot_acc_sat_4x8packed_ss_int(w0, A##ci.s##t, d0.s##ci); \
41 d1.s##ci = dot_acc_sat_4x8packed_ss_int(w1, A##ci.s##t, d1.s##ci); \
42 d2.s##ci = dot_acc_sat_4x8packed_ss_int(w2, A##ci.s##t, d2.s##ci); \
43 d3.s##ci = dot_acc_sat_4x8packed_ss_int(w3, A##ci.s##t, d3.s##ci);
44#else
45#define COOPK_DOT(ci, t) \
46 d0.s##ci = dot_acc_sat_4x8packed_ss_int(w0, A##ci.s##t, d0.s##ci); \
47 d1.s##ci = dot_acc_sat_4x8packed_ss_int(w1, A##ci.s##t, d1.s##ci);
48#endif
49
50#if COOPK_COLS == 4
51#define COOPK_DOTS_AT(t) COOPK_DOT(0,t) COOPK_DOT(1,t) COOPK_DOT(2,t) COOPK_DOT(3,t)
52#define COOPK_FOR_COLS(F) F(0) F(1) F(2) F(3)
53#else
54#define COOPK_DOTS_AT(t) COOPK_DOT(0,t) COOPK_DOT(1,t)
55#define COOPK_FOR_COLS(F) F(0) F(1)
56#endif
57
58#if COOPK_ROWS == 4
59#define COOPK_KSTEP(t) \
60 { \
61 ushort4 bl = vload4(0, src0_ql + row0 + (ku0 + t) * m); \
62 uchar4 bh = vload4(0, src0_qh + row0 + (ku0 + t) * m); \
63 const uint w0 = EXP6(bl.s0, bh.s0, mask_f000, mask_c0); \
64 const uint w1 = EXP6(bl.s1, bh.s1, mask_f000, mask_c0); \
65 const uint w2 = EXP6(bl.s2, bh.s2, mask_f000, mask_c0); \
66 const uint w3 = EXP6(bl.s3, bh.s3, mask_f000, mask_c0); \
67 COOPK_DOTS_AT(t) \
68 }
69#else
70#define COOPK_KSTEP(t) \
71 { \
72 ushort2 bl = vload2(0, src0_ql + row0 + (ku0 + t) * m); \
73 uchar2 bh = vload2(0, src0_qh + row0 + (ku0 + t) * m); \
74 const uint w0 = EXP6(bl.s0, bh.s0, mask_f000, mask_c0); \
75 const uint w1 = EXP6(bl.s1, bh.s1, mask_f000, mask_c0); \
76 COOPK_DOTS_AT(t) \
77 }
78#endif
79
80// Weights use the untiled Adreno noshuffle SoA layout, with the output row contiguous in each plane.
81// Q8_1 activation planes are K-major (K contiguous); no activation transpose is used.
82kernel void kernel_gemm_coopk_q6_k_q8_1_dp4a(
83 global const ushort * src0_ql, // four consecutive low nibbles per ushort, [k/4][m]
84 global const uchar * src0_qh, // four consecutive high 2-bit codes per uchar, [k/4][m]
85 global const ushort * src0_s, // two signed int8 scales per ushort, [k/32][m]
86 global const half * src0_d, // super-block scales, [k/256][m]
87 global const uint * src1_qa, // four signed activation bytes per uint, [n][k/4]
88 global const half * src1_da, // activation scales, [n][k/32]
89 global float * dst,
90 ulong offsetd,
91 int m,
92 int n,
93 int k,
94 int n_no_padding,
95 ushort mask_f000,
96 uchar mask_c0
97) {
98 dst = (global float *)((global char *)dst + offsetd);
99
100 const int gx = get_global_id(0);
101 const int sg = get_local_id(1);
102 const int lane = get_local_id(0);
103
104 const int row0 = gx * COOPK_ROWS;
105 const int num_32blk = k / 32;
106 const int k_u = k >> 2;
107 const int k_b = k >> 5;
108
109 // The host pads activations to COOPK_COLS; stores mask columns beyond n_no_padding.
110 const int c0 = 0, c1 = 1;
111#if COOPK_COLS == 4
112 const int c2 = 2, c3 = 3;
113#endif
114
115 coopk_accv acc0 = (coopk_accv)(0.0f), acc1 = (coopk_accv)(0.0f);
116#if COOPK_ROWS == 4
117 coopk_accv acc2 = (coopk_accv)(0.0f), acc3 = (coopk_accv)(0.0f);
118#endif
119
120 for (int blk = sg; blk < num_32blk; blk += COOPK_NSG) {
121 const int i = blk << 5;
122
123 // Each ushort packs two signed scale bytes, one for each 16-value half of this 32-value block.
124 // The half-precision super-block scale is shared by 256 K values.
125 ushort4 spk = vload4(0, src0_s + row0 + blk * m);
126 half4 scd = vload4(0, src0_d + row0 + (i >> 8) * m);
127
128 coopk_accv da;
129 da.s0 = (float)src1_da[c0*k_b + blk];
130 da.s1 = (float)src1_da[c1*k_b + blk];
131#if COOPK_COLS == 4
132 da.s2 = (float)src1_da[c2*k_b + blk];
133 da.s3 = (float)src1_da[c3*k_b + blk];
134#endif
135
136 // Apply each signed weight scale to its 16-value dot product.
137 for (int half_idx = 0; half_idx < 2; ++half_idx) {
138 const int ku0 = (i >> 2) + half_idx * 4;
139
140 coopk_dotv d0 = (coopk_dotv)(0), d1 = (coopk_dotv)(0);
141#if COOPK_ROWS == 4
142 coopk_dotv d2 = (coopk_dotv)(0), d3 = (coopk_dotv)(0);
143#endif
144 uint4 A0 = vload4(0, src1_qa + (uint)c0 * k_u + ku0);
145 uint4 A1 = vload4(0, src1_qa + (uint)c1 * k_u + ku0);
146#if COOPK_COLS == 4
147 uint4 A2 = vload4(0, src1_qa + (uint)c2 * k_u + ku0);
148 uint4 A3 = vload4(0, src1_qa + (uint)c3 * k_u + ku0);
149#endif
150 COOPK_KSTEP(0)
151 COOPK_KSTEP(1)
152 COOPK_KSTEP(2)
153 COOPK_KSTEP(3)
154
155 const float s0f = (float)(half_idx ? as_char2(spk.s0).s1 : as_char2(spk.s0).s0) * (float)scd.s0;
156 const float s1f = (float)(half_idx ? as_char2(spk.s1).s1 : as_char2(spk.s1).s0) * (float)scd.s1;
157 acc0 += s0f * da * COOPK_CONVF(d0);
158 acc1 += s1f * da * COOPK_CONVF(d1);
159#if COOPK_ROWS == 4
160 const float s2f = (float)(half_idx ? as_char2(spk.s2).s1 : as_char2(spk.s2).s0) * (float)scd.s2;
161 const float s3f = (float)(half_idx ? as_char2(spk.s3).s1 : as_char2(spk.s3).s0) * (float)scd.s3;
162 acc2 += s2f * da * COOPK_CONVF(d2);
163 acc3 += s3f * da * COOPK_CONVF(d3);
164#endif
165 }
166 }
167
168 // Reuse one local buffer for all rows; the first barrier protects the previous row's reads.
169 local coopk_accv reduceLM[COOPK_SG * (COOPK_NSG - 1)];
170 coopk_accv out0 = (coopk_accv)(0.0f), out1 = (coopk_accv)(0.0f);
171#if COOPK_ROWS == 4
172 coopk_accv out2 = (coopk_accv)(0.0f), out3 = (coopk_accv)(0.0f);
173#endif
174
175#define COOPK_REDUCE(accv, outv) \
176 barrier(CLK_LOCAL_MEM_FENCE); \
177 if (sg > 0) { reduceLM[(sg - 1) * COOPK_SG + lane] = (accv); } \
178 barrier(CLK_LOCAL_MEM_FENCE); \
179 if (sg == 0) { \
180 coopk_accv sum = (accv); \
181 for (int s = 0; s < COOPK_NSG - 1; s++) { \
182 sum += reduceLM[s * COOPK_SG + lane]; \
183 } \
184 (outv) = sum; \
185 }
186
187 COOPK_REDUCE(acc0, out0)
188 COOPK_REDUCE(acc1, out1)
189#if COOPK_ROWS == 4
190 COOPK_REDUCE(acc2, out2)
191 COOPK_REDUCE(acc3, out3)
192#endif
193
194#undef COOPK_REDUCE
195
196#if COOPK_ROWS == 4
197#define COOPK_STORE_COL(ci) \
198 if (idx < m*n_no_padding) { \
199 vstore4((float4)(out0.s##ci, out1.s##ci, out2.s##ci, out3.s##ci), 0, dst + idx); \
200 idx += m; \
201 }
202#else
203#define COOPK_STORE_COL(ci) \
204 if (idx < m*n_no_padding) { \
205 vstore2((float2)(out0.s##ci, out1.s##ci), 0, dst + idx); \
206 idx += m; \
207 }
208#endif
209
210 if (sg == 0) {
211 int idx = row0;
212 COOPK_FOR_COLS(COOPK_STORE_COL)
213 }
214
215#undef COOPK_STORE_COL
216}