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

235 lines · 9723 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// Weight layout
8// src0_qs[row + (k/4)*m] ushort = 4 low nibbles (K = 4*grp .. +3)
9// src0_qh[row + (k/8)*m] uchar = 8 high bits (one per element)
10// src0_d [row + (k/32)*m] half = per-32-block scale
11
12#define TILESIZE_N 32
13
14// 4 nibbles in low 16 bits of u -> 4 bytes (value 0..15)
15#define EXP4(u) ( ((uint)((u) & 0x000Fu)) | \
16 (((uint)((u) & 0x00F0u)) << 4) | \
17 (((uint)((u) & 0x0F00u)) << 8) | \
18 (((uint)((u) & 0xF000u)) << 12) )
19// 4 high bits (one per element, in bits 0..3 of h) -> bit4 of each of 4 bytes
20#define EXP1(h) ( (((uint)((h) & 0x1u)) << 4) | \
21 (((uint)((h) & 0x2u)) << 11) | \
22 (((uint)((h) & 0x4u)) << 18) | \
23 (((uint)((h) & 0x8u)) << 25) )
24
25inline int dot8_q8a(uint8 qw, __local const uint * a) {
26 int r = 0;
27 r = dot_acc_sat_4x8packed_ss_int(qw.s0, a[0], r);
28 r = dot_acc_sat_4x8packed_ss_int(qw.s1, a[1], r);
29 r = dot_acc_sat_4x8packed_ss_int(qw.s2, a[2], r);
30 r = dot_acc_sat_4x8packed_ss_int(qw.s3, a[3], r);
31 r = dot_acc_sat_4x8packed_ss_int(qw.s4, a[4], r);
32 r = dot_acc_sat_4x8packed_ss_int(qw.s5, a[5], r);
33 r = dot_acc_sat_4x8packed_ss_int(qw.s6, a[6], r);
34 r = dot_acc_sat_4x8packed_ss_int(qw.s7, a[7], r);
35 return r;
36}
37
38__attribute__((qcom_wave_pair_mode(1)))
39kernel void kernel_gemm_noshuffle_q5_0_q8_1_dp4a(
40 __global const ushort * src0_qs, // q5_0 low nibbles (4/ushort, feature-major)
41 __global const uchar * src0_qh, // q5_0 high-bit plane (8/uchar, feature-major)
42 __global const half * src0_d, // per-32-block scale, feature-major
43 __global const uint * src1_qa, // q8_1 activations int8 (as uint, 4/elem) [N, K]
44 __global const half * src1_da, // q8_1 per-block scale [N, K/32]
45 __global const half * src1_sa, // q8_1 per-block sum*d [N, K/32]
46 __global float * dst,
47 ulong offsetd,
48 int m, // output features (rows)
49 int n_no_padding, // tokens (cols)
50 int k // K (== ne00)
51) {
52 dst = (global float *)((global char *)dst + offsetd);
53
54 const uint lid = get_local_id(0); // 0..63 -> row within the M-tile
55 const uint block_id_m = get_global_id(1);
56 const uint block_id_n = get_global_id(2);
57
58 const uint row = block_id_m * 64 + lid;
59 const uint col_base = block_id_n * TILESIZE_N;
60 const bool row_valid = row < (uint)m;
61 const uint rrow = row_valid ? row : 0; // clamp OOB rows; their writes are masked
62
63 const uint k_u = (uint)k >> 2; // K in uint (int8x4) units
64 const uint k_b = (uint)k >> 5; // blocks-of-32 along K
65
66 __local uint sh_qa[TILESIZE_N][8];
67 __local half sh_d[TILESIZE_N];
68 __local half sh_s[TILESIZE_N];
69
70#define NGROUPS (TILESIZE_N / 4)
71 float4 acc[NGROUPS];
72 #pragma unroll
73 for (int g = 0; g < NGROUPS; ++g) acc[g] = (float4)(0.0f);
74
75 for (uint step = 0; step < (uint)k; step += 32) {
76 const uint sub = step >> 5;
77
78 const float d_w = (float)src0_d[rrow + sub * (uint)m];
79 const float minv = d_w * 16.0f; // -16 centering -> subtract via q8_1 sum
80
81 // 8 weight uints (32 elements) for this row, this 32-block.
82 // nibbles: src0_qs[row + (step/4 + u)*m]; high bits: src0_qh[row + (step/8 + u/2)*m],
83 // 4-bit group selected by (u&1)*4.
84 const uint qsbase = rrow + (step >> 2) * (uint)m;
85 const uint qhbase = rrow + (step >> 3) * (uint)m;
86 uint8 qw;
87 #define QW(u) (EXP4(src0_qs[qsbase + (u) * m]) | \
88 EXP1((uint)(src0_qh[qhbase + ((u) >> 1) * m] >> (((u) & 1u) * 4u)) & 0xFu))
89 qw.s0 = QW(0); qw.s1 = QW(1); qw.s2 = QW(2); qw.s3 = QW(3);
90 qw.s4 = QW(4); qw.s5 = QW(5); qw.s6 = QW(6); qw.s7 = QW(7);
91 #undef QW
92
93 // cooperatively stage the 32-token x 32-K int8 activations to lm
94 for (uint idx = lid; idx < TILESIZE_N * 8; idx += 64) {
95 const uint t = idx >> 3;
96 const uint u = idx & 7;
97 const uint c = col_base + t;
98 sh_qa[t][u] = (c < (uint)n_no_padding) ? src1_qa[c * k_u + (step >> 2) + u] : 0u;
99 }
100 if (lid < TILESIZE_N) {
101 const uint c = col_base + lid;
102 sh_d[lid] = (c < (uint)n_no_padding) ? src1_da[c * k_b + sub] : (half)0;
103 sh_s[lid] = (c < (uint)n_no_padding) ? src1_sa[c * k_b + sub] : (half)0;
104 }
105 barrier(CLK_LOCAL_MEM_FENCE);
106
107#define LD4(arr, b) ((float4)((float)arr[(b)+0], (float)arr[(b)+1], (float)arr[(b)+2], (float)arr[(b)+3]))
108 #pragma unroll
109 for (int g = 0; g < NGROUPS; ++g) {
110 const int b = g * 4;
111 float4 rf;
112 rf.s0 = (float)dot8_q8a(qw, sh_qa[b+0]); rf.s1 = (float)dot8_q8a(qw, sh_qa[b+1]);
113 rf.s2 = (float)dot8_q8a(qw, sh_qa[b+2]); rf.s3 = (float)dot8_q8a(qw, sh_qa[b+3]);
114 acc[g] += d_w * LD4(sh_d, b) * rf - minv * LD4(sh_s, b);
115 }
116#undef LD4
117 barrier(CLK_LOCAL_MEM_FENCE);
118 }
119
120 if (!row_valid) {
121 return;
122 }
123
124 #pragma unroll
125 for (int g = 0; g < NGROUPS; ++g) {
126 const uint b = (uint)(g * 4);
127 const float4 a = acc[g];
128 const uint c0 = col_base + b;
129 if (c0 + 0 < (uint)n_no_padding) dst[(c0 + 0) * (uint)m + row] = a.s0;
130 if (c0 + 1 < (uint)n_no_padding) dst[(c0 + 1) * (uint)m + row] = a.s1;
131 if (c0 + 2 < (uint)n_no_padding) dst[(c0 + 2) * (uint)m + row] = a.s2;
132 if (c0 + 3 < (uint)n_no_padding) dst[(c0 + 3) * (uint)m + row] = a.s3;
133 }
134#undef NGROUPS
135}
136
137__attribute__((qcom_wave_pair_mode(1)))
138kernel void kernel_gemm_noshuffle_q5_0_q8_1_dp4a_wimg(
139 __read_only image1d_buffer_t src0_qs_img, // q5_0 low nibbles as uint32 texels (2 ushorts/texel)
140 __global const uchar * src0_qh,
141 __global const half * src0_d,
142 __global const uint * src1_qa,
143 __global const half * src1_da,
144 __global const half * src1_sa,
145 __global float * dst,
146 ulong offsetd,
147 int m,
148 int n_no_padding,
149 int k
150) {
151 dst = (global float *)((global char *)dst + offsetd);
152
153 const uint lid = get_local_id(0);
154 const uint block_id_m = get_global_id(1);
155 const uint block_id_n = get_global_id(2);
156
157 const uint row = block_id_m * 64 + lid;
158 const uint col_base = block_id_n * TILESIZE_N;
159 const bool row_valid = row < (uint)m;
160 const uint rrow = row_valid ? row : 0;
161
162 const uint sel = (rrow & 1u) * 16u; // constant per WI: qs ushort half in its uint32 texel
163
164 const uint k_u = (uint)k >> 2;
165 const uint k_b = (uint)k >> 5;
166
167 __local uint sh_qa[TILESIZE_N][8];
168 __local half sh_d[TILESIZE_N];
169 __local half sh_s[TILESIZE_N];
170
171#define NGROUPS (TILESIZE_N / 4)
172 float4 acc[NGROUPS];
173 #pragma unroll
174 for (int g = 0; g < NGROUPS; ++g) acc[g] = (float4)(0.0f);
175
176 for (uint step = 0; step < (uint)k; step += 32) {
177 const uint sub = step >> 5;
178
179 const float d_w = (float)src0_d[rrow + sub * (uint)m];
180 const float minv = d_w * 16.0f;
181
182 const uint qsbase = rrow + (step >> 2) * (uint)m; // ushort index
183 const uint qhbase = rrow + (step >> 3) * (uint)m;
184 uint8 qw;
185 // qs ushort via texture: uint32 texel = ushort_index>>1, half = sel.
186 #define QSU(u) ((read_imageui(src0_qs_img, (int)((qsbase + (u) * m) >> 1)).x >> sel) & 0xFFFFu)
187 #define QW(u) (EXP4(QSU(u)) | \
188 EXP1((uint)(src0_qh[qhbase + ((u) >> 1) * m] >> (((u) & 1u) * 4u)) & 0xFu))
189 qw.s0 = QW(0); qw.s1 = QW(1); qw.s2 = QW(2); qw.s3 = QW(3);
190 qw.s4 = QW(4); qw.s5 = QW(5); qw.s6 = QW(6); qw.s7 = QW(7);
191 #undef QW
192 #undef QSU
193
194 for (uint idx = lid; idx < TILESIZE_N * 8; idx += 64) {
195 const uint t = idx >> 3;
196 const uint u = idx & 7;
197 const uint c = col_base + t;
198 sh_qa[t][u] = (c < (uint)n_no_padding) ? src1_qa[c * k_u + (step >> 2) + u] : 0u;
199 }
200 if (lid < TILESIZE_N) {
201 const uint c = col_base + lid;
202 sh_d[lid] = (c < (uint)n_no_padding) ? src1_da[c * k_b + sub] : (half)0;
203 sh_s[lid] = (c < (uint)n_no_padding) ? src1_sa[c * k_b + sub] : (half)0;
204 }
205 barrier(CLK_LOCAL_MEM_FENCE);
206
207#define LD4(arr, b) ((float4)((float)arr[(b)+0], (float)arr[(b)+1], (float)arr[(b)+2], (float)arr[(b)+3]))
208 #pragma unroll
209 for (int g = 0; g < NGROUPS; ++g) {
210 const int b = g * 4;
211 float4 rf;
212 rf.s0 = (float)dot8_q8a(qw, sh_qa[b+0]); rf.s1 = (float)dot8_q8a(qw, sh_qa[b+1]);
213 rf.s2 = (float)dot8_q8a(qw, sh_qa[b+2]); rf.s3 = (float)dot8_q8a(qw, sh_qa[b+3]);
214 acc[g] += d_w * LD4(sh_d, b) * rf - minv * LD4(sh_s, b);
215 }
216#undef LD4
217 barrier(CLK_LOCAL_MEM_FENCE);
218 }
219
220 if (!row_valid) {
221 return;
222 }
223
224 #pragma unroll
225 for (int g = 0; g < NGROUPS; ++g) {
226 const uint b = (uint)(g * 4);
227 const float4 a = acc[g];
228 const uint c0 = col_base + b;
229 if (c0 + 0 < (uint)n_no_padding) dst[(c0 + 0) * (uint)m + row] = a.s0;
230 if (c0 + 1 < (uint)n_no_padding) dst[(c0 + 1) * (uint)m + row] = a.s1;
231 if (c0 + 2 < (uint)n_no_padding) dst[(c0 + 2) * (uint)m + row] = a.s2;
232 if (c0 + 3 < (uint)n_no_padding) dst[(c0 + 3) * (uint)m + row] = a.s3;
233 }
234#undef NGROUPS
235}