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

253 lines · 9601 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#define TILESIZE_N 32
8
9// Expand the 4 nibbles in the low 16 bits of u into 4 bytes (value 0..15),
10// packed for the int8 dp4a. The -8 zero-point is applied via the sum term.
11#define EXP4(u) ( ((uint)((u) & 0x000Fu)) | \
12 (((uint)((u) & 0x00F0u)) << 4) | \
13 (((uint)((u) & 0x0F00u)) << 8) | \
14 (((uint)((u) & 0xF000u)) << 12) )
15
16inline int dot8_q8a(uint8 qw, __local const uint * a) {
17 int r = 0;
18 r = dot_acc_sat_4x8packed_ss_int(qw.s0, a[0], r);
19 r = dot_acc_sat_4x8packed_ss_int(qw.s1, a[1], r);
20 r = dot_acc_sat_4x8packed_ss_int(qw.s2, a[2], r);
21 r = dot_acc_sat_4x8packed_ss_int(qw.s3, a[3], r);
22 r = dot_acc_sat_4x8packed_ss_int(qw.s4, a[4], r);
23 r = dot_acc_sat_4x8packed_ss_int(qw.s5, a[5], r);
24 r = dot_acc_sat_4x8packed_ss_int(qw.s6, a[6], r);
25 r = dot_acc_sat_4x8packed_ss_int(qw.s7, a[7], r);
26 return r;
27}
28
29__attribute__((qcom_wave_pair_mode(1)))
30kernel void kernel_gemm_noshuffle_q4_0_q8_1_dp4a(
31 __global const ushort * src0_q, // q4_0 nibbles (4/ushort, feature-major)
32 __global const half * src0_d, // per-32-block scale, feature-major
33 __global const uint * src1_qa, // q8_1 activations int8 (as uint, 4/elem) [N, K]
34 __global const half * src1_da, // q8_1 per-block scale [N, K/32]
35 __global const half * src1_sa, // q8_1 per-block sum*d [N, K/32]
36 __global float * dst,
37 ulong offsetd,
38 int m, // output features (rows)
39 int n_no_padding, // tokens (cols)
40 int k // K (== ne00)
41) {
42 dst = (global float *)((global char *)dst + offsetd);
43
44 const uint lid = get_local_id(0); // 0..63 -> row within the M-tile
45 const uint block_id_m = get_global_id(1);
46 const uint block_id_n = get_global_id(2);
47
48 const uint row = block_id_m * 64 + lid;
49 const uint col_base = block_id_n * TILESIZE_N;
50 const bool row_valid = row < (uint)m;
51 const uint rrow = row_valid ? row : 0; // clamp OOB rows; their writes are masked
52
53 const uint k_u = (uint)k >> 2; // K in uint (int8x4) units
54 const uint k_b = (uint)k >> 5; // blocks-of-32 along K
55
56 __local uint sh_qa[TILESIZE_N][8];
57 __local half sh_d[TILESIZE_N];
58 __local half sh_s[TILESIZE_N];
59
60#define NGROUPS (TILESIZE_N / 4)
61 float4 acc[NGROUPS];
62 #pragma unroll
63 for (int g = 0; g < NGROUPS; ++g) acc[g] = (float4)(0.0f);
64
65 for (uint step = 0; step < (uint)k; step += 32) {
66 const uint sub = step >> 5;
67
68 const float d_w = (float)src0_d[rrow + sub * (uint)m];
69
70 // 8 weight uints (32 nibbles) for this row, this 32-block. Feature-major:
71 // src0_q[row + (k/4 + u)*m], k/4 = step/4 (= step>>2). EXP4 -> dp4a int8.
72 const uint qsbase = rrow + (step >> 2) * (uint)m;
73 uint8 qw;
74 qw.s0 = EXP4(src0_q[qsbase + 0 * m]);
75 qw.s1 = EXP4(src0_q[qsbase + 1 * m]);
76 qw.s2 = EXP4(src0_q[qsbase + 2 * m]);
77 qw.s3 = EXP4(src0_q[qsbase + 3 * m]);
78 qw.s4 = EXP4(src0_q[qsbase + 4 * m]);
79 qw.s5 = EXP4(src0_q[qsbase + 5 * m]);
80 qw.s6 = EXP4(src0_q[qsbase + 6 * m]);
81 qw.s7 = EXP4(src0_q[qsbase + 7 * m]);
82
83 // cooperatively stage the 32-token x 32-K int8 activations to LDS
84 for (uint idx = lid; idx < TILESIZE_N * 8; idx += 64) {
85 const uint t = idx >> 3;
86 const uint u = idx & 7;
87 const uint c = col_base + t;
88 sh_qa[t][u] = (c < (uint)n_no_padding) ? src1_qa[c * k_u + (step >> 2) + u] : 0u;
89 }
90 if (lid < TILESIZE_N) {
91 const uint c = col_base + lid;
92 sh_d[lid] = (c < (uint)n_no_padding) ? src1_da[c * k_b + sub] : (half)0;
93 sh_s[lid] = (c < (uint)n_no_padding) ? src1_sa[c * k_b + sub] : (half)0;
94 }
95 barrier(CLK_LOCAL_MEM_FENCE);
96
97#define LD4(arr, b) ((float4)((float)arr[(b)+0], (float)arr[(b)+1], (float)arr[(b)+2], (float)arr[(b)+3]))
98 #pragma unroll
99 for (int g = 0; g < NGROUPS; ++g) {
100 const int b = g * 4;
101 float4 rf;
102 rf.s0 = (float)dot8_q8a(qw, sh_qa[b+0]); rf.s1 = (float)dot8_q8a(qw, sh_qa[b+1]);
103 rf.s2 = (float)dot8_q8a(qw, sh_qa[b+2]); rf.s3 = (float)dot8_q8a(qw, sh_qa[b+3]);
104 // q4_0: w = d*(q-8) -> d_w * (a_d * dp4a(q,qa) - 8 * a_s)
105 acc[g] += d_w * (LD4(sh_d, b) * rf - 8.0f * LD4(sh_s, b));
106 }
107#undef LD4
108 barrier(CLK_LOCAL_MEM_FENCE);
109 }
110
111 if (!row_valid) {
112 return;
113 }
114
115 // dst is [token, feature] row-major (stride m): dst[col*m + row].
116 #pragma unroll
117 for (int g = 0; g < NGROUPS; ++g) {
118 const uint b = (uint)(g * 4);
119 const float4 a = acc[g];
120 const uint c0 = col_base + b;
121 if (c0 + 0 < (uint)n_no_padding) dst[(c0 + 0) * (uint)m + row] = a.s0;
122 if (c0 + 1 < (uint)n_no_padding) dst[(c0 + 1) * (uint)m + row] = a.s1;
123 if (c0 + 2 < (uint)n_no_padding) dst[(c0 + 2) * (uint)m + row] = a.s2;
124 if (c0 + 3 < (uint)n_no_padding) dst[(c0 + 3) * (uint)m + row] = a.s3;
125 }
126#undef NGROUPS
127}
128
129// Stage each 32-K activation block as two uint4 values.
130inline int dot8_q8a_v(uint8 qw, uint4 a0, uint4 a1) {
131 int r = 0;
132 r = dot_acc_sat_4x8packed_ss_int(qw.s0, a0.x, r);
133 r = dot_acc_sat_4x8packed_ss_int(qw.s1, a0.y, r);
134 r = dot_acc_sat_4x8packed_ss_int(qw.s2, a0.z, r);
135 r = dot_acc_sat_4x8packed_ss_int(qw.s3, a0.w, r);
136 r = dot_acc_sat_4x8packed_ss_int(qw.s4, a1.x, r);
137 r = dot_acc_sat_4x8packed_ss_int(qw.s5, a1.y, r);
138 r = dot_acc_sat_4x8packed_ss_int(qw.s6, a1.z, r);
139 r = dot_acc_sat_4x8packed_ss_int(qw.s7, a1.w, r);
140 return r;
141}
142
143__attribute__((qcom_wave_pair_mode(1)))
144kernel void kernel_gemm_noshuffle_q4_0_q8_1_dp4a_act_vload4(
145 __global const ushort * src0_q,
146 __global const half * src0_d,
147 __global const uint * src1_qa,
148 __global const half * src1_da,
149 __global const half * src1_sa,
150 __global float * dst,
151 ulong offsetd,
152 int m,
153 int n_no_padding,
154 int k
155) {
156 dst = (global float *)((global char *)dst + offsetd);
157
158 const uint lid = get_local_id(0);
159 const uint block_id_m = get_global_id(1);
160 const uint block_id_n = get_global_id(2);
161
162 const uint row = block_id_m * 64 + lid;
163 const uint col_base = block_id_n * TILESIZE_N;
164 const bool row_valid = row < (uint)m;
165 const uint rrow = row_valid ? row : 0;
166
167 const uint k_u = (uint)k >> 2;
168 const uint k_b = (uint)k >> 5;
169
170 __local uint4 sh_qa4[TILESIZE_N][2];
171 __local half sh_d[TILESIZE_N];
172 __local half sh_s[TILESIZE_N];
173
174#define NGROUPS (TILESIZE_N / 4)
175 float4 acc[NGROUPS];
176 #pragma unroll
177 for (int g = 0; g < NGROUPS; ++g) {
178 acc[g] = (float4)(0.0f);
179 }
180
181 for (uint step = 0; step < (uint)k; step += 32) {
182 const uint sub = step >> 5;
183
184 const float d_w = (float)src0_d[rrow + sub * (uint)m];
185
186 const uint qsbase = rrow + (step >> 2) * (uint)m;
187 uint8 qw;
188 qw.s0 = EXP4(src0_q[qsbase + 0 * m]);
189 qw.s1 = EXP4(src0_q[qsbase + 1 * m]);
190 qw.s2 = EXP4(src0_q[qsbase + 2 * m]);
191 qw.s3 = EXP4(src0_q[qsbase + 3 * m]);
192 qw.s4 = EXP4(src0_q[qsbase + 4 * m]);
193 qw.s5 = EXP4(src0_q[qsbase + 5 * m]);
194 qw.s6 = EXP4(src0_q[qsbase + 6 * m]);
195 qw.s7 = EXP4(src0_q[qsbase + 7 * m]);
196
197 for (uint idx = lid; idx < TILESIZE_N * 2; idx += 64) {
198 const uint t = idx >> 1;
199 const uint v = idx & 1;
200 const uint c = col_base + t;
201 sh_qa4[t][v] = (c < (uint)n_no_padding)
202 ? vload4(0, src1_qa + c * k_u + (step >> 2) + (v << 2))
203 : (uint4)(0u);
204 }
205 if (lid < TILESIZE_N) {
206 const uint c = col_base + lid;
207 sh_d[lid] = (c < (uint)n_no_padding) ? src1_da[c * k_b + sub] : (half)0;
208 sh_s[lid] = (c < (uint)n_no_padding) ? src1_sa[c * k_b + sub] : (half)0;
209 }
210 barrier(CLK_LOCAL_MEM_FENCE);
211
212#define LD4(arr, b) ((float4)((float)arr[(b)+0], (float)arr[(b)+1], (float)arr[(b)+2], (float)arr[(b)+3]))
213 #pragma unroll
214 for (int g = 0; g < NGROUPS; ++g) {
215 const int b = g * 4;
216 float4 rf;
217#define DOTV(T) dot8_q8a_v(qw, sh_qa4[T][0], sh_qa4[T][1])
218 rf.s0 = (float)DOTV(b+0);
219 rf.s1 = (float)DOTV(b+1);
220 rf.s2 = (float)DOTV(b+2);
221 rf.s3 = (float)DOTV(b+3);
222#undef DOTV
223
224 acc[g] += d_w * (LD4(sh_d, b) * rf - 8.0f * LD4(sh_s, b));
225 }
226#undef LD4
227 barrier(CLK_LOCAL_MEM_FENCE);
228 }
229
230 if (!row_valid) {
231 return;
232 }
233
234 #pragma unroll
235 for (int g = 0; g < NGROUPS; ++g) {
236 const uint b = (uint)(g * 4);
237 const float4 a = acc[g];
238 const uint c0 = col_base + b;
239 if (c0 + 0 < (uint)n_no_padding) {
240 dst[(c0 + 0) * (uint)m + row] = a.s0;
241 }
242 if (c0 + 1 < (uint)n_no_padding) {
243 dst[(c0 + 1) * (uint)m + row] = a.s1;
244 }
245 if (c0 + 2 < (uint)n_no_padding) {
246 dst[(c0 + 2) * (uint)m + row] = a.s2;
247 }
248 if (c0 + 3 < (uint)n_no_padding) {
249 dst[(c0 + 3) * (uint)m + row] = a.s3;
250 }
251 }
252#undef NGROUPS
253}