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

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