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

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