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

306 lines · 16468 bytes Raw Blame History
1#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2
3#ifdef cl_intel_subgroups
4#pragma OPENCL EXTENSION cl_intel_subgroups : enable
5#else
6#pragma OPENCL EXTENSION cl_khr_subgroups : enable
7#endif
8
9#ifdef cl_intel_required_subgroup_size
10#pragma OPENCL EXTENSION cl_intel_required_subgroup_size : enable
11#define INTEL_GPU 1
12#define REQD_SUBGROUP_SIZE_16 __attribute__((intel_reqd_sub_group_size(16)))
13#define REQD_SUBGROUP_SIZE_32 __attribute__((intel_reqd_sub_group_size(32)))
14#elif defined(cl_qcom_reqd_sub_group_size)
15#pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable
16#define ADRENO_GPU 1
17#define REQD_SUBGROUP_SIZE_64 __attribute__((qcom_reqd_sub_group_size("half")))
18#define REQD_SUBGROUP_SIZE_128 __attribute__((qcom_reqd_sub_group_size("full")))
19#endif
20
21// Multi-row f16xf32 GEMV for the DECODE path (single token, ne11*ne12 small).
22// The legacy kernel_mul_mat_f16_f32_1row runs ONE 64-lane subgroup per workgroup =
23// one output row per WG, which caps memory-level parallelism at roughly half of
24// LPDDR5x peak. This variant packs MROW subgroups per workgroup, each
25// computing a distinct output row, so a WG keeps 64*MROW loads in flight. The
26// activation column y (shared by every output row) is staged into __local ONCE per
27// WG and reused across the MROW rows, cutting redundant activation reads. Used for
28// the f16 attention projections (Q/K/V/O) and lm_head, which dominate decode.
29// Numerically equivalent to _1row (same f16->f32 widening, same float4 partial sums,
30// same subgroup-reduce order), so byte-identical to the per-op path.
31
32#define MROW 16
33
34#ifdef ADRENO_GPU
35REQD_SUBGROUP_SIZE_64
36#endif
37kernel void kernel_mul_mat_f16_f32_mrow(
38 global char * src0,
39 ulong offset0,
40 global char * src1,
41 ulong offset1,
42 global float * dst,
43 ulong offsetd,
44 int ne00,
45 int ne01,
46 int ne02,
47 ulong nb00,
48 ulong nb01,
49 ulong nb02,
50 ulong nb03,
51 int ne10,
52 int ne11,
53 int ne12,
54 ulong nb10,
55 ulong nb11,
56 ulong nb12,
57 ulong nb13,
58 int ne0,
59 int ne1,
60 int r2,
61 int r3,
62 __local float * ysh
63) {
64 src0 = (global char*)((global char*)src0 + offset0);
65 src1 = (global char*)((global char*)src1 + offset1);
66 dst = (global float*)((global char*)dst + offsetd);
67
68 int r0 = get_group_id(0) * MROW + get_local_id(1); // output row
69 int r1 = get_group_id(1); // token (ne11)
70 int im = get_group_id(2);
71 int lid = get_sub_group_local_id(); // 0..63
72 int nsg = get_local_size(1); // == MROW
73
74 int i12 = im % ne12;
75 int i13 = im / ne12;
76
77 ulong offset_src1 = r1*nb11 + (i12)*nb12 + (i13)*nb13;
78 global float * y = (global float *) (src1 + offset_src1);
79
80 // Cooperatively stage the activation column (ne00 floats) into __local once per
81 // WG and reuse across the MROW rows. Staging is the actual win here: dropping it
82 // (each subgroup re-reading y from global) regresses below the 1-row kernel.
83 for (int i = get_local_id(1)*get_sub_group_size() + lid; i < ne00; i += nsg*get_sub_group_size()) {
84 ysh[i] = y[i];
85 }
86 barrier(CLK_LOCAL_MEM_FENCE);
87
88 if (r0 >= ne01) {
89 return;
90 }
91
92 ulong offset_src0 = r0*nb01 + (i12/r2)*nb02 + (i13/r3)*nb03;
93 global half * x = (global half *) (src0 + offset_src0);
94
95 // The vector path below casts the row pointer to half4, which must be 8-byte aligned.
96 // A row address is r0*nb01 + ..., and a permuted or strided src0 leaves nb01/nb02/nb03
97 // unconstrained -- ne00 % 4 == 0 bounds the element count per row, not the byte stride
98 // between rows. Take the vector path only when this work-item's row is actually
99 // aligned; the scalar loop below has no such requirement.
100 const bool row_aligned = (((ulong) x) & 7) == 0;
101
102 float sumf = 0.0f;
103 if (ne00 < 128 || !row_aligned) {
104 for (int i = lid; i < ne00; i += get_sub_group_size()) {
105 sumf += (float) x[i] * ysh[i];
106 }
107 float all_sum = sub_group_reduce_add(sumf);
108 if (lid == 0) {
109 dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
110 }
111 } else {
112 global half4 * x4 = (global half4 *) x;
113 __local float4 * ysh4 = (__local float4 *) ysh;
114 for (int i = lid; i < ne00/4; i += get_sub_group_size()) {
115 float4 yv = ysh4[i];
116 sumf += (float) x4[i].s0 * yv.s0;
117 sumf += (float) x4[i].s1 * yv.s1;
118 sumf += (float) x4[i].s2 * yv.s2;
119 sumf += (float) x4[i].s3 * yv.s3;
120 }
121 float all_sum = sub_group_reduce_add(sumf);
122 if (lid == 0) {
123 for (int i = 4*(ne00/4); i < ne00; ++i) {
124 all_sum += (float) x[i] * ysh[i];
125 }
126 dst[im*ne1*ne0 + r1*ne0 + r0] = all_sum;
127 }
128 }
129}
130
131// Register-blocked variant: each 64-lane subgroup accumulates RPT consecutive
132// output rows instead of one. The staged activation is reused across all RPT rows,
133// and each lane keeps RPT independent weight loads in flight per column step ->
134// more memory-level parallelism on the streaming f16 weight read (the BW limiter),
135// plus RPT fewer staging barriers per output row. Per-row reduction order is
136// identical to _mrow, so byte-identical to the per-op path. Dispatch guarantees
137// ne00 >= 128 and ne00 % 4 == 0, so only the half4 path is needed (no tail).
138#define MROW_RB_BODY(RPT) \
139 src0 = (global char*)((global char*)src0 + offset0); \
140 src1 = (global char*)((global char*)src1 + offset1); \
141 dst = (global float*)((global char*)dst + offsetd); \
142 int r0b = (get_group_id(0) * get_local_size(1) + get_local_id(1)) * (RPT); \
143 int r1 = get_group_id(1); \
144 int im = get_group_id(2); \
145 int lid = get_sub_group_local_id(); \
146 int nsg = get_local_size(1); \
147 int i12 = im % ne12; \
148 int i13 = im / ne12; \
149 ulong off_y = r1*nb11 + i12*nb12 + i13*nb13; \
150 global float * y = (global float *) (src1 + off_y); \
151 for (int i = get_local_id(1)*get_sub_group_size() + lid; i < ne00; \
152 i += nsg*get_sub_group_size()) { \
153 ysh[i] = y[i]; \
154 } \
155 barrier(CLK_LOCAL_MEM_FENCE); \
156 __local float4 * ysh4 = (__local float4 *) ysh; \
157 global half4 * xr[RPT]; \
158 _Pragma("unroll") \
159 for (int rr = 0; rr < (RPT); ++rr) { \
160 int row = r0b + rr; \
161 if (row > ne01 - 1) row = ne01 - 1; \
162 xr[rr] = (global half4 *) (src0 + (ulong)row*nb01 + (i12/r2)*nb02 + (i13/r3)*nb03); \
163 } \
164 float sumf[RPT]; \
165 _Pragma("unroll") \
166 for (int rr = 0; rr < (RPT); ++rr) sumf[rr] = 0.0f; \
167 for (int i = lid; i < ne00/4; i += get_sub_group_size()) { \
168 float4 yv = ysh4[i]; \
169 _Pragma("unroll") \
170 for (int rr = 0; rr < (RPT); ++rr) { \
171 half4 xv = xr[rr][i]; \
172 sumf[rr] += (float) xv.s0 * yv.s0 + (float) xv.s1 * yv.s1 \
173 + (float) xv.s2 * yv.s2 + (float) xv.s3 * yv.s3; \
174 } \
175 } \
176 _Pragma("unroll") \
177 for (int rr = 0; rr < (RPT); ++rr) { \
178 float s = sub_group_reduce_add(sumf[rr]); \
179 int row = r0b + rr; \
180 if (lid == 0 && row < ne01) { \
181 dst[im*ne1*ne0 + r1*ne0 + row] = s; \
182 } \
183 }
184
185// half8 (128-bit) load variant: Adreno's load/store unit issues 128-bit
186// transactions, so half4 (64-bit) loads may leave the load path half-idle. This
187// processes 8 weight elements per lane per step via half8. Accumulation groups
188// elements in 8s rather than 4s, so it is NOT bit-identical to _1row (float add is
189// non-associative) -- experimental BW probe, gate on ne00 % 8 == 0.
190#define MROW_H8_BODY(RPT) \
191 src0 = (global char*)((global char*)src0 + offset0); \
192 src1 = (global char*)((global char*)src1 + offset1); \
193 dst = (global float*)((global char*)dst + offsetd); \
194 int r0b = (get_group_id(0) * get_local_size(1) + get_local_id(1)) * (RPT); \
195 int r1 = get_group_id(1); \
196 int im = get_group_id(2); \
197 int lid = get_sub_group_local_id(); \
198 int nsg = get_local_size(1); \
199 int i12 = im % ne12; \
200 int i13 = im / ne12; \
201 ulong off_y = r1*nb11 + i12*nb12 + i13*nb13; \
202 global float * y = (global float *) (src1 + off_y); \
203 for (int i = get_local_id(1)*get_sub_group_size() + lid; i < ne00; \
204 i += nsg*get_sub_group_size()) { \
205 ysh[i] = y[i]; \
206 } \
207 barrier(CLK_LOCAL_MEM_FENCE); \
208 __local float4 * ysh4 = (__local float4 *) ysh; \
209 global half8 * xr[RPT]; \
210 _Pragma("unroll") \
211 for (int rr = 0; rr < (RPT); ++rr) { \
212 int row = r0b + rr; \
213 if (row > ne01 - 1) row = ne01 - 1; \
214 xr[rr] = (global half8 *) (src0 + (ulong)row*nb01 + (i12/r2)*nb02 + (i13/r3)*nb03); \
215 } \
216 float sumf[RPT]; \
217 _Pragma("unroll") \
218 for (int rr = 0; rr < (RPT); ++rr) sumf[rr] = 0.0f; \
219 for (int i = lid; i < ne00/8; i += get_sub_group_size()) { \
220 float4 y0 = ysh4[2*i]; \
221 float4 y1 = ysh4[2*i + 1]; \
222 _Pragma("unroll") \
223 for (int rr = 0; rr < (RPT); ++rr) { \
224 half8 xv = xr[rr][i]; \
225 sumf[rr] += (float) xv.s0 * y0.s0 + (float) xv.s1 * y0.s1 \
226 + (float) xv.s2 * y0.s2 + (float) xv.s3 * y0.s3 \
227 + (float) xv.s4 * y1.s0 + (float) xv.s5 * y1.s1 \
228 + (float) xv.s6 * y1.s2 + (float) xv.s7 * y1.s3; \
229 } \
230 } \
231 _Pragma("unroll") \
232 for (int rr = 0; rr < (RPT); ++rr) { \
233 float s = sub_group_reduce_add(sumf[rr]); \
234 int row = r0b + rr; \
235 if (lid == 0 && row < ne01) { \
236 dst[im*ne1*ne0 + r1*ne0 + row] = s; \
237 } \
238 }
239
240#ifdef ADRENO_GPU
241REQD_SUBGROUP_SIZE_64
242#endif
243kernel void kernel_mul_mat_f16_f32_mrow_h8(
244 global char * src0, ulong offset0,
245 global char * src1, ulong offset1,
246 global float * dst, ulong offsetd,
247 int ne00, int ne01, int ne02,
248 ulong nb00, ulong nb01, ulong nb02, ulong nb03,
249 int ne10, int ne11, int ne12,
250 ulong nb10, ulong nb11, ulong nb12, ulong nb13,
251 int ne0, int ne1, int r2, int r3,
252 __local float * ysh
253) {
254 MROW_H8_BODY(1)
255}
256
257#ifdef ADRENO_GPU
258REQD_SUBGROUP_SIZE_64
259#endif
260kernel void kernel_mul_mat_f16_f32_mrow_h8r2(
261 global char * src0, ulong offset0,
262 global char * src1, ulong offset1,
263 global float * dst, ulong offsetd,
264 int ne00, int ne01, int ne02,
265 ulong nb00, ulong nb01, ulong nb02, ulong nb03,
266 int ne10, int ne11, int ne12,
267 ulong nb10, ulong nb11, ulong nb12, ulong nb13,
268 int ne0, int ne1, int r2, int r3,
269 __local float * ysh
270) {
271 MROW_H8_BODY(2)
272}
273
274#ifdef ADRENO_GPU
275REQD_SUBGROUP_SIZE_64
276#endif
277kernel void kernel_mul_mat_f16_f32_mrow_r2(
278 global char * src0, ulong offset0,
279 global char * src1, ulong offset1,
280 global float * dst, ulong offsetd,
281 int ne00, int ne01, int ne02,
282 ulong nb00, ulong nb01, ulong nb02, ulong nb03,
283 int ne10, int ne11, int ne12,
284 ulong nb10, ulong nb11, ulong nb12, ulong nb13,
285 int ne0, int ne1, int r2, int r3,
286 __local float * ysh
287) {
288 MROW_RB_BODY(2)
289}
290
291#ifdef ADRENO_GPU
292REQD_SUBGROUP_SIZE_64
293#endif
294kernel void kernel_mul_mat_f16_f32_mrow_r4(
295 global char * src0, ulong offset0,
296 global char * src1, ulong offset1,
297 global float * dst, ulong offsetd,
298 int ne00, int ne01, int ne02,
299 ulong nb00, ulong nb01, ulong nb02, ulong nb03,
300 int ne10, int ne11, int ne12,
301 ulong nb10, ulong nb11, ulong nb12, ulong nb13,
302 int ne0, int ne1, int r2, int r3,
303 __local float * ysh
304) {
305 MROW_RB_BODY(4)
306}