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

272 lines · 8312 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#define QK4_0 32
22#define QR4_0 2
23#define QK4_1 32
24#define QR4_1 2
25#define QK5_0 32
26#define QR5_0 2
27#define QK5_1 32
28#define QR5_1 2
29#define QK8_0 32
30#define QR8_0 1
31#define QK_K 256
32#define K_QUANTS_PER_ITERATION 2
33
34typedef char int8_t;
35typedef uchar uint8_t;
36typedef short int16_t;
37typedef ushort uint16_t;
38typedef int int32_t;
39typedef uint uint32_t;
40
41//------------------------------------------------------------------------------
42// block_q4_0
43//------------------------------------------------------------------------------
44struct block_q4_0
45{
46 half d;
47 uint8_t qs[QK4_0 / 2];
48};
49
50// This function requires the original shuffled weights.
51// As a reminder, the original weights are shuffled so that (q[0], q[16]) are
52// packed together in a byte, so are (q[1], q[17]) and so on.
53inline float block_q_4_0_dot_y_flat(
54 global uchar * x,
55 global half * dh,
56 float sumy,
57 float16 yl,
58 int il
59) {
60 float d = *dh;
61 global ushort * qs = ((global ushort *)x + il/2);
62 float acc = 0.f;
63
64 acc += yl.s0 * (qs[0] & 0x000F);
65 acc += yl.s1 * (qs[0] & 0x0F00);
66 acc += yl.s8 * (qs[0] & 0x00F0);
67 acc += yl.s9 * (qs[0] & 0xF000);
68
69 acc += yl.s2 * (qs[1] & 0x000F);
70 acc += yl.s3 * (qs[1] & 0x0F00);
71 acc += yl.sa * (qs[1] & 0x00F0);
72 acc += yl.sb * (qs[1] & 0xF000);
73
74 acc += yl.s4 * (qs[2] & 0x000F);
75 acc += yl.s5 * (qs[2] & 0x0F00);
76 acc += yl.sc * (qs[2] & 0x00F0);
77 acc += yl.sd * (qs[2] & 0xF000);
78
79 acc += yl.s6 * (qs[3] & 0x000F);
80 acc += yl.s7 * (qs[3] & 0x0F00);
81 acc += yl.se * (qs[3] & 0x00F0);
82 acc += yl.sf * (qs[3] & 0xF000);
83
84 return d * (sumy * -8.f + acc);
85}
86
87//
88// This variant outputs 8 values.
89//
90#undef N_DST
91#undef N_SIMDGROUP
92#undef N_SIMDWIDTH
93
94#ifdef INTEL_GPU
95#define N_DST 8 // each SIMD group works on 8 rows
96#define N_SIMDGROUP 1 // number of SIMD groups in a thread group
97#define N_SIMDWIDTH 16 // assuming SIMD group size is 32
98#elif defined (ADRENO_GPU)
99#define N_DST 8
100#define N_SIMDGROUP 1
101#define N_SIMDWIDTH 64
102#endif
103
104inline void mul_vec_q_n_f32_8x_flat(
105 global uchar * src0_q,
106 global half * src0_d,
107 global float * src1,
108 global float * dst,
109 int ne00,
110 int ne01,
111 int ne02,
112 int ne10,
113 int ne12,
114 int ne0,
115 int ne1,
116 int r2,
117 int r3
118) {
119 const ulong nb = ne00/QK4_0;
120
121 int r0 = get_group_id(0);
122 int r1 = get_group_id(1);
123 int im = get_group_id(2);
124
125 // (r0 * N_SIMDGROUP + get_sub_group_id()) is the linear global id of
126 // a SIMD group in the grid. Each SIMD group produces N_DST values in the
127 // result, hence uses nb blocks, i.e., the offset becomes first_row*nb.
128 // Currently with llama2 7B, im is always 0.
129 // TODO: how to handle im/gqa*(nb*ne0)?
130 int first_row = (r0 * N_SIMDGROUP + get_sub_group_id()) * N_DST;
131
132 int i12 = im%ne12;
133 int i13 = im/ne12;
134
135 // The number of scales is the same as the number of blocks.
136 ulong offset0_d = first_row * nb + (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
137 // Each block contains QK4_0/2 uchars, hence offset for qs is as follows.
138 ulong offset0_q = (first_row * nb + (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02)) * QK4_0/2;
139
140 global uchar * x = (global uchar *) src0_q + offset0_q;
141 global half * d = (global half *) src0_d + offset0_d;
142 global float * y = (global float *) src1 + r1*ne10 + im*ne00*ne1;
143
144 float16 yl;
145 float8 sumf = 0.f;
146
147 int ix = get_sub_group_local_id()/2;
148 int il = 8*(get_sub_group_local_id()%2);
149
150 global float * yb = y + ix*QK4_0 + il;
151
152 for (int ib = ix; ib < nb; ib += N_SIMDWIDTH/2) {
153 float sumy = 0.f;
154
155 sumy += yb[0];
156 sumy += yb[1];
157 sumy += yb[2];
158 sumy += yb[3];
159 sumy += yb[4];
160 sumy += yb[5];
161 sumy += yb[6];
162 sumy += yb[7];
163
164 sumy += yb[16];
165 sumy += yb[17];
166 sumy += yb[18];
167 sumy += yb[19];
168 sumy += yb[20];
169 sumy += yb[21];
170 sumy += yb[22];
171 sumy += yb[23];
172
173 yl.s0 = yb[0];
174 yl.s1 = yb[1]/256.f;
175
176 yl.s2 = yb[2];
177 yl.s3 = yb[3]/256.f;
178
179 yl.s4 = yb[4];
180 yl.s5 = yb[5]/256.f;
181
182 yl.s6 = yb[6];
183 yl.s7 = yb[7]/256.f;
184
185 yl.s8 = yb[16]/16.f;
186 yl.s9 = yb[17]/4096.f;
187
188 yl.sa = yb[18]/16.f;
189 yl.sb = yb[19]/4096.f;
190
191 yl.sc = yb[20]/16.f;
192 yl.sd = yb[21]/4096.f;
193
194 yl.se = yb[22]/16.f;
195 yl.sf = yb[23]/4096.f;
196
197 sumf.s0 += block_q_4_0_dot_y_flat(x + ib*QK4_0/2 + 0*nb*QK4_0/2, d + ib + 0*nb, sumy, yl, il);
198 sumf.s1 += block_q_4_0_dot_y_flat(x + ib*QK4_0/2 + 1*nb*QK4_0/2, d + ib + 1*nb, sumy, yl, il);
199 sumf.s2 += block_q_4_0_dot_y_flat(x + ib*QK4_0/2 + 2*nb*QK4_0/2, d + ib + 2*nb, sumy, yl, il);
200 sumf.s3 += block_q_4_0_dot_y_flat(x + ib*QK4_0/2 + 3*nb*QK4_0/2, d + ib + 3*nb, sumy, yl, il);
201
202 sumf.s4 += block_q_4_0_dot_y_flat(x + ib*QK4_0/2 + 4*nb*QK4_0/2, d + ib + 4*nb, sumy, yl, il);
203 sumf.s5 += block_q_4_0_dot_y_flat(x + ib*QK4_0/2 + 5*nb*QK4_0/2, d + ib + 5*nb, sumy, yl, il);
204 sumf.s6 += block_q_4_0_dot_y_flat(x + ib*QK4_0/2 + 6*nb*QK4_0/2, d + ib + 6*nb, sumy, yl, il);
205 sumf.s7 += block_q_4_0_dot_y_flat(x + ib*QK4_0/2 + 7*nb*QK4_0/2, d + ib + 7*nb, sumy, yl, il);
206
207 yb += QK4_0 * (N_SIMDWIDTH/2);
208 }
209
210 float8 tot = (float8)(
211 sub_group_reduce_add(sumf.s0), sub_group_reduce_add(sumf.s1),
212 sub_group_reduce_add(sumf.s2), sub_group_reduce_add(sumf.s3),
213 sub_group_reduce_add(sumf.s4), sub_group_reduce_add(sumf.s5),
214 sub_group_reduce_add(sumf.s6), sub_group_reduce_add(sumf.s7)
215 );
216
217 if (get_sub_group_local_id() == 0) {
218 if (first_row + 0 < ne01) {
219 dst[r1*ne0 + im*ne0*ne1 + first_row + 0] = tot.s0;
220 }
221 if (first_row + 1 < ne01) {
222 dst[r1*ne0 + im*ne0*ne1 + first_row + 1] = tot.s1;
223 }
224 if (first_row + 2 < ne01) {
225 dst[r1*ne0 + im*ne0*ne1 + first_row + 2] = tot.s2;
226 }
227 if (first_row + 3 < ne01) {
228 dst[r1*ne0 + im*ne0*ne1 + first_row + 3] = tot.s3;
229 }
230
231 if (first_row + 4 < ne01) {
232 dst[r1*ne0 + im*ne0*ne1 + first_row + 4] = tot.s4;
233 }
234 if (first_row + 5 < ne01) {
235 dst[r1*ne0 + im*ne0*ne1 + first_row + 5] = tot.s5;
236 }
237 if (first_row + 6 < ne01) {
238 dst[r1*ne0 + im*ne0*ne1 + first_row + 6] = tot.s6;
239 }
240 if (first_row + 7 < ne01) {
241 dst[r1*ne0 + im*ne0*ne1 + first_row + 7] = tot.s7;
242 }
243 }
244}
245
246#ifdef INTEL_GPU
247REQD_SUBGROUP_SIZE_16
248#elif defined (ADRENO_GPU)
249REQD_SUBGROUP_SIZE_64
250#endif
251kernel void kernel_mul_mat_q4_0_f32_8x_flat(
252 global uchar * src0_q,
253 global half * src0_d,
254 global float * src1,
255 ulong offset1,
256 global float * dst,
257 ulong offsetd,
258 int ne00,
259 int ne01,
260 int ne02,
261 int ne10,
262 int ne12,
263 int ne0,
264 int ne1,
265 int r2,
266 int r3
267) {
268 src1 = (global float*)((global char*)src1 + offset1);
269 dst = (global float*)((global char*)dst + offsetd);
270
271 mul_vec_q_n_f32_8x_flat(src0_q, src0_d, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3);
272}