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 / gemv_noshuffle_q8_0_f32.cl

284 lines · 13379 bytes Raw Blame History
1#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2#pragma OPENCL EXTENSION cl_khr_subgroups : enable
3
4#ifdef cl_qcom_reqd_sub_group_size
5#pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable
6#define ADRENO_GPU 1
7#define REQD_SUBGROUP_SIZE_64 __attribute__((qcom_reqd_sub_group_size("half")))
8#endif
9
10#define QK8_0 32
11#define N_SIMDGROUP 4
12
13#define dequantizeBlockAccum_ns_sgbroadcast_1(total_sums, bits8, scale, y) \
14 float shared_y; \
15 char elem; \
16 \
17 shared_y = sub_group_broadcast(y.s0, 0); \
18 elem = (char)(bits8.s0 & 0x000000FF); \
19 total_sums += convert_int(elem) * scale * shared_y; \
20 shared_y = sub_group_broadcast(y.s1, 0); \
21 elem = (char)((bits8.s0 & 0x0000FF00) >> 8); \
22 total_sums += convert_int(elem) * scale * shared_y; \
23 shared_y = sub_group_broadcast(y.s2, 0); \
24 elem = (char)((bits8.s0 & 0x00FF0000) >> 16); \
25 total_sums += convert_int(elem) * scale * shared_y; \
26 shared_y = sub_group_broadcast(y.s3, 0); \
27 elem = (char)((bits8.s0 & 0xFF000000) >> 24); \
28 total_sums += convert_int(elem) * scale * shared_y; \
29 \
30 shared_y = sub_group_broadcast(y.s4, 0); \
31 elem = (char)(bits8.s1 & 0x000000FF); \
32 total_sums += convert_int(elem) * scale * shared_y; \
33 shared_y = sub_group_broadcast(y.s5, 0); \
34 elem = (char)((bits8.s1 & 0x0000FF00) >> 8); \
35 total_sums += convert_int(elem) * scale * shared_y; \
36 shared_y = sub_group_broadcast(y.s6, 0); \
37 elem = (char)((bits8.s1 & 0x00FF0000) >> 16); \
38 total_sums += convert_int(elem) * scale * shared_y; \
39 shared_y = sub_group_broadcast(y.s7, 0); \
40 elem = (char)((bits8.s1 & 0xFF000000) >> 24); \
41 total_sums += convert_int(elem) * scale * shared_y; \
42 \
43 shared_y = sub_group_broadcast(y.s0, 1); \
44 elem = (char)(bits8.s2 & 0x000000FF); \
45 total_sums += convert_int(elem) * scale * shared_y; \
46 shared_y = sub_group_broadcast(y.s1, 1); \
47 elem = (char)((bits8.s2 & 0x0000FF00) >> 8); \
48 total_sums += convert_int(elem) * scale * shared_y; \
49 shared_y = sub_group_broadcast(y.s2, 1); \
50 elem = (char)((bits8.s2 & 0x00FF0000) >> 16); \
51 total_sums += convert_int(elem) * scale * shared_y; \
52 shared_y = sub_group_broadcast(y.s3, 1); \
53 elem = (char)((bits8.s2 & 0xFF000000) >> 24); \
54 total_sums += convert_int(elem) * scale * shared_y; \
55 \
56 shared_y = sub_group_broadcast(y.s4, 1); \
57 elem = (char)(bits8.s3 & 0x000000FF); \
58 total_sums += convert_int(elem) * scale * shared_y; \
59 shared_y = sub_group_broadcast(y.s5, 1); \
60 elem = (char)((bits8.s3 & 0x0000FF00) >> 8); \
61 total_sums += convert_int(elem) * scale * shared_y; \
62 shared_y = sub_group_broadcast(y.s6, 1); \
63 elem = (char)((bits8.s3 & 0x00FF0000) >> 16); \
64 total_sums += convert_int(elem) * scale * shared_y; \
65 shared_y = sub_group_broadcast(y.s7, 1); \
66 elem = (char)((bits8.s3 & 0xFF000000) >> 24); \
67 total_sums += convert_int(elem) * scale * shared_y; \
68 \
69 shared_y = sub_group_broadcast(y.s0, 2); \
70 elem = (char)(bits8.s4 & 0x000000FF); \
71 total_sums += convert_int(elem) * scale * shared_y; \
72 shared_y = sub_group_broadcast(y.s1, 2); \
73 elem = (char)((bits8.s4 & 0x0000FF00) >> 8); \
74 total_sums += convert_int(elem) * scale * shared_y; \
75 shared_y = sub_group_broadcast(y.s2, 2); \
76 elem = (char)((bits8.s4 & 0x00FF0000) >> 16); \
77 total_sums += convert_int(elem) * scale * shared_y; \
78 shared_y = sub_group_broadcast(y.s3, 2); \
79 elem = (char)((bits8.s4 & 0xFF000000) >> 24); \
80 total_sums += convert_int(elem) * scale * shared_y; \
81 \
82 shared_y = sub_group_broadcast(y.s4, 2); \
83 elem = (char)(bits8.s5 & 0x000000FF); \
84 total_sums += convert_int(elem) * scale * shared_y; \
85 shared_y = sub_group_broadcast(y.s5, 2); \
86 elem = (char)((bits8.s5 & 0x0000FF00) >> 8); \
87 total_sums += convert_int(elem) * scale * shared_y; \
88 shared_y = sub_group_broadcast(y.s6, 2); \
89 elem = (char)((bits8.s5 & 0x00FF0000) >> 16); \
90 total_sums += convert_int(elem) * scale * shared_y; \
91 shared_y = sub_group_broadcast(y.s7, 2); \
92 elem = (char)((bits8.s5 & 0xFF000000) >> 24); \
93 total_sums += convert_int(elem) * scale * shared_y; \
94 \
95 shared_y = sub_group_broadcast(y.s0, 3); \
96 elem = (char)(bits8.s6 & 0x000000FF); \
97 total_sums += convert_int(elem) * scale * shared_y; \
98 shared_y = sub_group_broadcast(y.s1, 3); \
99 elem = (char)((bits8.s6 & 0x0000FF00) >> 8); \
100 total_sums += convert_int(elem) * scale * shared_y; \
101 shared_y = sub_group_broadcast(y.s2, 3); \
102 elem = (char)((bits8.s6 & 0x00FF0000) >> 16); \
103 total_sums += convert_int(elem) * scale * shared_y; \
104 shared_y = sub_group_broadcast(y.s3, 3); \
105 elem = (char)((bits8.s6 & 0xFF000000) >> 24); \
106 total_sums += convert_int(elem) * scale * shared_y; \
107 \
108 shared_y = sub_group_broadcast(y.s4, 3); \
109 elem = (char)(bits8.s7 & 0x000000FF); \
110 total_sums += convert_int(elem) * scale * shared_y; \
111 shared_y = sub_group_broadcast(y.s5, 3); \
112 elem = (char)((bits8.s7 & 0x0000FF00) >> 8); \
113 total_sums += convert_int(elem) * scale * shared_y; \
114 shared_y = sub_group_broadcast(y.s6, 3); \
115 elem = (char)((bits8.s7 & 0x00FF0000) >> 16); \
116 total_sums += convert_int(elem) * scale * shared_y; \
117 shared_y = sub_group_broadcast(y.s7, 3); \
118 elem = (char)((bits8.s7 & 0xFF000000) >> 24); \
119 total_sums += convert_int(elem) * scale * shared_y; \
120
121// ============================================================================
122// Split-K variant for small-M decode GEMVs.
123// ----------------------------------------------------------------------------
124// The base kernel below puts one output row per lane and splits K only across
125// the N_SIMDGROUP subgroups of a single workgroup, so M=512 yields M/64 = 8
126// workgroups -- half the compute units on a 16-CU X2 sit idle, and the kernel
127// measures ~48 GB/s against the ~122 GB/s the larger projections reach in the
128// same graph. Here each (kslice, subgroup) pair reduces a disjoint set of
129// K-blocks into partial[kslice * M + row]; kernel_gemv_splitk_reduce_f32 (in
130// gemv_noshuffle_q4_k_f32.cl) sums the slices. Same operand order within a
131// slice as the base kernel; only the cross-slice grouping differs.
132//
133// Placed BEFORE the base kernel deliberately: on A6X no kernel may be defined
134// after one that uses a subgroup builtin, or it silently miscompiles.
135// ============================================================================
136#ifdef ADRENO_GPU
137REQD_SUBGROUP_SIZE_64
138#endif
139__kernel void kernel_gemv_noshuffle_q8_0_f32_splitk(
140 __read_only image1d_buffer_t src0_q, // quantized A (weights)
141 global half * src0_d, // A scales
142 __read_only image1d_buffer_t src1, // B (activations)
143 global float * partial, // [ksplit * M], slice-major
144 int ne00, // K
145 int ne01) // M
146{
147 uint groupId = get_local_id(1);
148 uint gid = get_global_id(0);
149 ushort slid = get_sub_group_local_id();
150 uint nsg = get_local_size(1);
151 uint ksplit = get_num_groups(1);
152 uint kslice = get_group_id(1);
153
154 uint K = ne00;
155 uint M = ne01;
156
157 uint LINE_STRIDE_A = M;
158 // Keep tail lanes active for subgroup ops, but read a valid weight row.
159 uint gid_s = min(gid, LINE_STRIDE_A - 1);
160 uint BLOCK_STRIDE_A = 8 * M; // physical, independent of the K-split
161
162 __private uint8 regA;
163 __private half regS;
164 __private float8 regB;
165 __private float totalSum = (float)(0.0f);
166
167 #pragma unroll 1
168 for (uint k = kslice * nsg + groupId; k < (K / QK8_0); k += ksplit * nsg) {
169 regS = src0_d[gid_s + k * LINE_STRIDE_A];
170 if (slid < 4) {
171 regB.s0123 = read_imagef(src1, (slid * 2 + k * 8));
172 regB.s4567 = read_imagef(src1, (1 + slid * 2 + k * 8));
173 }
174 regA.s0 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 0)).x;
175 regA.s1 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 1)).x;
176 regA.s2 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 2)).x;
177 regA.s3 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 3)).x;
178 regA.s4 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 4)).x;
179 regA.s5 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 5)).x;
180 regA.s6 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 6)).x;
181 regA.s7 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 7)).x;
182
183 dequantizeBlockAccum_ns_sgbroadcast_1(totalSum, regA, convert_float(regS), regB);
184 }
185
186 // Intra-workgroup reduce across this K-slice's subgroups. Sized for
187 // nsg <= 8; the host never dispatches more.
188 __local float reduceLM[SIMDGROUP_WIDTH * 7];
189 if (groupId > 0) {
190 reduceLM[SIMDGROUP_WIDTH * (groupId - 1) + slid] = totalSum;
191 }
192 barrier(CLK_LOCAL_MEM_FENCE);
193 if (groupId == 0) {
194 for (uint i = 0; i < nsg - 1; ++i) {
195 totalSum += reduceLM[SIMDGROUP_WIDTH * i + slid];
196 }
197 // x-grid is padded to CEIL_DIV(M,wave)*wave; guard the tail rows.
198 if (gid < M) {
199 partial[kslice * M + gid] = totalSum;
200 }
201 }
202}
203
204#ifdef ADRENO_GPU
205REQD_SUBGROUP_SIZE_64
206#endif
207__kernel void kernel_gemv_noshuffle_q8_0_f32(
208 __read_only image1d_buffer_t src0_q, // quantized A
209 global half * src0_d, // A scales
210 __read_only image1d_buffer_t src1, // B
211 ulong offset1, // offset to B (0)
212 global float * dst, // C
213 ulong offsetd, // offset to C
214 int ne00, // K
215 int ne01, // M
216 int ne02, // 1
217 int ne10, // K
218 int ne12, // 1
219 int ne0, // M
220 int ne1, // N
221 int r2, // 1
222 int r3)
223{
224 uint groupId = get_local_id(1);
225 uint gid = get_global_id(0);
226 ushort slid = get_sub_group_local_id();
227
228 uint K = ne00;
229 uint M = ne01;
230
231 uint LINE_STRIDE_A = M;
232 // Keep tail lanes active for subgroup ops, but read a valid weight row.
233 uint gid_s = min(gid, LINE_STRIDE_A - 1);
234 uint BLOCK_STRIDE_A = 8 * M; // 32 / 4 = 8
235
236 __private uint8 regA;
237 __private half regS;
238 __private float8 regB;
239
240 __private float totalSum = (float)(0.0f);
241
242 // loop along K in block granularity, skip 4 blocks every iter
243 #pragma unroll 1 /* tell compiler not to unroll */
244 for (uint k = groupId; k < (K / QK8_0); k += N_SIMDGROUP) {
245 regS = src0_d[gid_s + k * LINE_STRIDE_A]; // each fiber loads the scale of one row
246 // first 4 fibers in each wave load 8 B values to its private scope
247 if (slid < 4) {
248 regB.s0123 = read_imagef(src1, (slid * 2 + k * 8));
249 regB.s4567 = read_imagef(src1, (1 + slid * 2 + k * 8));
250 }
251
252 // load weights for one block in consecutive rows
253 regA.s0 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 0)).x;
254 regA.s1 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 1)).x;
255 regA.s2 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 2)).x;
256 regA.s3 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 3)).x;
257 regA.s4 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 4)).x;
258 regA.s5 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 5)).x;
259 regA.s6 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 6)).x;
260 regA.s7 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 7)).x;
261
262 dequantizeBlockAccum_ns_sgbroadcast_1(totalSum, regA, convert_float(regS), regB);
263 }
264
265 // reduction in local memory, assumes #wave=4
266 __local float reduceLM[SIMDGROUP_WIDTH * 3];
267 if (groupId == 1) reduceLM[SIMDGROUP_WIDTH * 0 + slid] = totalSum;
268 if (groupId == 2) reduceLM[SIMDGROUP_WIDTH * 1 + slid] = totalSum;
269 if (groupId == 3) reduceLM[SIMDGROUP_WIDTH * 2 + slid] = totalSum;
270 barrier(CLK_LOCAL_MEM_FENCE);
271 if (groupId == 0) totalSum += reduceLM[SIMDGROUP_WIDTH * 0 + slid];
272 if (groupId == 0) totalSum += reduceLM[SIMDGROUP_WIDTH * 1 + slid];
273 if (groupId == 0) totalSum += reduceLM[SIMDGROUP_WIDTH * 2 + slid];
274
275 // 1 outputs per fiber in wave 0
276 if (groupId == 0) {
277 dst = (global float*)((global char*)dst + offsetd);
278 // Guard the output row. The x-grid is padded to CEIL_DIV(M,wavesize)*wavesize,
279 // so when ne01 is not a multiple of the wave size the tail work-items run past
280 // row ne01 and would overrun dst into the adjacent tensor. No-op / byte-identical
281 // when ne01 is wave-aligned (no padding).
282 if (gid < M) dst[gid] = totalSum;
283 }
284}