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

243 lines · 7179 bytes Raw Blame History
1
2#pragma OPENCL EXTENSION cl_khr_fp16 : enable
3
4#ifdef cl_intel_subgroups
5#pragma OPENCL EXTENSION cl_intel_subgroups : enable
6#else
7#pragma OPENCL EXTENSION cl_khr_subgroups : enable
8#endif
9
10#ifdef cl_intel_required_subgroup_size
11#pragma OPENCL EXTENSION cl_intel_required_subgroup_size : enable
12#define INTEL_GPU 1
13#define REQD_SUBGROUP_SIZE_16 __attribute__((intel_reqd_sub_group_size(16)))
14#define REQD_SUBGROUP_SIZE_32 __attribute__((intel_reqd_sub_group_size(32)))
15#elif defined(cl_qcom_reqd_sub_group_size)
16#pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable
17#define ADRENO_GPU 1
18#define REQD_SUBGROUP_SIZE_64 __attribute__((qcom_reqd_sub_group_size("half")))
19#define REQD_SUBGROUP_SIZE_128 __attribute__((qcom_reqd_sub_group_size("full")))
20#endif
21
22#define QK5_0 32
23
24inline float block_q5_0_dot_y_flat(
25 global const uchar * x,
26 global const uint * qh_ptr,
27 global const half * dh,
28 float sumy,
29 float16 yl,
30 int il,
31 global const float * yb
32) {
33 float d = *dh;
34 global const ushort * qs = ((global const ushort *)(x + il));
35
36 float4 acc = (float4)(0.0f, 0.0f, 0.0f, 0.0f);
37
38 acc.s0 += yl.s0 * (qs[0] & 0x000F);
39 acc.s0 += yl.s1 * (qs[0] & 0x0F00);
40 acc.s0 += yl.s8 * (qs[0] & 0x00F0);
41 acc.s3 += yl.s9 * (qs[0] & 0xF000);
42
43 acc.s0 += yl.s2 * (qs[1] & 0x000F);
44 acc.s1 += yl.s3 * (qs[1] & 0x0F00);
45 acc.s2 += yl.sa * (qs[1] & 0x00F0);
46 acc.s3 += yl.sb * (qs[1] & 0xF000);
47
48 acc.s0 += yl.s4 * (qs[2] & 0x000F);
49 acc.s1 += yl.s5 * (qs[2] & 0x0F00);
50 acc.s2 += yl.sc * (qs[2] & 0x00F0);
51 acc.s3 += yl.sd * (qs[2] & 0xF000);
52
53 acc.s0 += yl.s6 * (qs[3] & 0x000F);
54 acc.s1 += yl.s7 * (qs[3] & 0x0F00);
55 acc.s2 += yl.se * (qs[3] & 0x00F0);
56 acc.s3 += yl.sf * (qs[3] & 0xF000);
57
58 uint qh_val = *qh_ptr;
59 uchar qh_lo = (uchar)((qh_val >> il) & 0xFF);
60 uchar qh_hi = (uchar)((qh_val >> (il + 16)) & 0xFF);
61
62 float qh_sum = 0.0f;
63 qh_sum += yb[0] * (float)((qh_lo >> 0) & 1);
64 qh_sum += yb[1] * (float)((qh_lo >> 1) & 1);
65 qh_sum += yb[2] * (float)((qh_lo >> 2) & 1);
66 qh_sum += yb[3] * (float)((qh_lo >> 3) & 1);
67 qh_sum += yb[4] * (float)((qh_lo >> 4) & 1);
68 qh_sum += yb[5] * (float)((qh_lo >> 5) & 1);
69 qh_sum += yb[6] * (float)((qh_lo >> 6) & 1);
70 qh_sum += yb[7] * (float)((qh_lo >> 7) & 1);
71 qh_sum += yb[16] * (float)((qh_hi >> 0) & 1);
72 qh_sum += yb[17] * (float)((qh_hi >> 1) & 1);
73 qh_sum += yb[18] * (float)((qh_hi >> 2) & 1);
74 qh_sum += yb[19] * (float)((qh_hi >> 3) & 1);
75 qh_sum += yb[20] * (float)((qh_hi >> 4) & 1);
76 qh_sum += yb[21] * (float)((qh_hi >> 5) & 1);
77 qh_sum += yb[22] * (float)((qh_hi >> 6) & 1);
78 qh_sum += yb[23] * (float)((qh_hi >> 7) & 1);
79
80 return d * (acc.s0 + acc.s1 + acc.s2 + acc.s3 + 16.0f * qh_sum - 16.0f * sumy);
81}
82
83#undef N_DST
84#undef N_SIMDGROUP
85#undef N_SIMDWIDTH
86
87#ifdef INTEL_GPU
88#define N_DST 4 // each subgroup works on 4 rows
89#define N_SIMDGROUP 1 // number of subgroups in a thread group
90#define N_SIMDWIDTH 16 // assuming subgroup size is 16
91#elif defined (ADRENO_GPU)
92#define N_DST 4
93#define N_SIMDGROUP 1
94#define N_SIMDWIDTH 64
95#endif
96
97inline void mul_vec_q_n_f32_flat(
98 global void * src0_qs,
99 global void * src0_qh,
100 global void * src0_d,
101 global float * src1,
102 global float * dst,
103 int ne00,
104 int ne01,
105 int ne02,
106 int ne10,
107 int ne12,
108 int ne0,
109 int ne1,
110 int r2,
111 int r3
112) {
113 const ulong nb = ne00/QK5_0;
114
115 int r0 = get_group_id(0);
116 int r1 = get_group_id(1);
117 int im = get_group_id(2);
118
119 int first_row = (r0 * N_SIMDGROUP + get_sub_group_id()) * N_DST;
120
121 int i12 = im%ne12;
122 int i13 = im/ne12;
123
124 ulong offset0 = first_row * nb + (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
125
126 ulong offset0_qs = offset0 * (QK5_0/2);
127
128 global uchar * x = (global uchar *) src0_qs + offset0_qs;
129 global uint * qh = (global uint *) src0_qh + offset0;
130 global half * d = (global half *) src0_d + offset0;
131 global float * y = (global float *) src1 + r1*ne10 + im*ne00*ne1;
132
133 float16 yl;
134 float4 sumf = (float4)(0.f, 0.f, 0.f, 0.f);
135
136 int ix = get_sub_group_local_id()/2;
137 int il = 8*(get_sub_group_local_id()%2);
138
139 global float * yb = y + ix * QK5_0 + il;
140
141 for (int ib = ix; ib < nb; ib += N_SIMDWIDTH/2) {
142 float sumy = 0;
143
144 sumy += yb[0];
145 sumy += yb[1];
146 sumy += yb[2];
147 sumy += yb[3];
148 sumy += yb[4];
149 sumy += yb[5];
150 sumy += yb[6];
151 sumy += yb[7];
152
153 sumy += yb[16];
154 sumy += yb[17];
155 sumy += yb[18];
156 sumy += yb[19];
157 sumy += yb[20];
158 sumy += yb[21];
159 sumy += yb[22];
160 sumy += yb[23];
161
162
163 yl.s0 = yb[0];
164 yl.s1 = yb[1]/256.f;
165
166 yl.s2 = yb[2];
167 yl.s3 = yb[3]/256.f;
168
169 yl.s4 = yb[4];
170 yl.s5 = yb[5]/256.f;
171
172 yl.s6 = yb[6];
173 yl.s7 = yb[7]/256.f;
174
175 yl.s8 = yb[16]/16.f;
176 yl.s9 = yb[17]/4096.f;
177
178 yl.sa = yb[18]/16.f;
179 yl.sb = yb[19]/4096.f;
180
181 yl.sc = yb[20]/16.f;
182 yl.sd = yb[21]/4096.f;
183
184 yl.se = yb[22]/16.f;
185 yl.sf = yb[23]/4096.f;
186
187 sumf.s0 += block_q5_0_dot_y_flat(x + ib*(QK5_0/2) + 0*nb*(QK5_0/2), qh + ib + 0*nb, d + ib + 0*nb, sumy, yl, il, yb);
188 sumf.s1 += block_q5_0_dot_y_flat(x + ib*(QK5_0/2) + 1*nb*(QK5_0/2), qh + ib + 1*nb, d + ib + 1*nb, sumy, yl, il, yb);
189 sumf.s2 += block_q5_0_dot_y_flat(x + ib*(QK5_0/2) + 2*nb*(QK5_0/2), qh + ib + 2*nb, d + ib + 2*nb, sumy, yl, il, yb);
190 sumf.s3 += block_q5_0_dot_y_flat(x + ib*(QK5_0/2) + 3*nb*(QK5_0/2), qh + ib + 3*nb, d + ib + 3*nb, sumy, yl, il, yb);
191
192 yb += QK5_0 * (N_SIMDWIDTH/2);
193 }
194
195 float4 tot = (float4)(
196 sub_group_reduce_add(sumf.s0), sub_group_reduce_add(sumf.s1),
197 sub_group_reduce_add(sumf.s2), sub_group_reduce_add(sumf.s3)
198 );
199
200 if (get_sub_group_local_id() == 0) {
201 if (first_row + 0 < ne01) {
202 dst[r1*ne0 + im*ne0*ne1 + first_row + 0] = tot.s0;
203 }
204 if (first_row + 1 < ne01) {
205 dst[r1*ne0 + im*ne0*ne1 + first_row + 1] = tot.s1;
206 }
207 if (first_row + 2 < ne01) {
208 dst[r1*ne0 + im*ne0*ne1 + first_row + 2] = tot.s2;
209 }
210 if (first_row + 3 < ne01) {
211 dst[r1*ne0 + im*ne0*ne1 + first_row + 3] = tot.s3;
212 }
213 }
214}
215
216#ifdef INTEL_GPU
217REQD_SUBGROUP_SIZE_16
218#elif defined (ADRENO_GPU)
219REQD_SUBGROUP_SIZE_64
220#endif
221kernel void kernel_mul_mv_q5_0_f32_flat(
222 global void * src0_qs,
223 global void * src0_qh,
224 global void * src0_d,
225 global float * src1,
226 ulong offset1,
227 global float * dst,
228 ulong offsetd,
229 int ne00,
230 int ne01,
231 int ne02,
232 int ne10,
233 int ne12,
234 int ne0,
235 int ne1,
236 int r2,
237 int r3
238) {
239 src1 = (global float*)((global char*)src1 + offset1);
240 dst = (global float*)((global char*)dst + offsetd);
241
242 mul_vec_q_n_f32_flat(src0_qs, src0_qh, src0_d, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3);
243}