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

244 lines · 9176 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//------------------------------------------------------------------------------
22// block_q5_K
23//------------------------------------------------------------------------------
24#define QK_K 256
25#define BLOCK_Q5K_SIZE 176
26#define K_SCALE_SIZE 12
27
28typedef struct {
29 half d; // super-block scale for quantized scales
30 half dmin; // super-block scale for quantized mins
31 uchar scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
32 uchar qh[QK_K/8]; // quants, high bit (1 bit per value, packed 8 per byte)
33 uchar qs[QK_K/2]; // quants, low 4 bits (2 values per byte)
34} block_q5_K;
35
36#undef N_DST
37#undef N_SIMDGROUP
38#undef N_SIMDWIDTH
39
40#ifdef INTEL_GPU
41#define N_DST 8 // Intel: 4->8 for 2x activation reuse (see mul_mv_q4_k_f32_flat.cl)
42#define N_SIMDGROUP 1
43#define N_SIMDWIDTH 16
44#elif defined(ADRENO_GPU)
45#define N_DST 16
46#define N_SIMDGROUP 2
47#define N_SIMDWIDTH 64
48#endif
49
50#undef BLOCK_STRIDE
51// number of (super) blocks each subgroup processes
52// each thread in a subgroup processes a block (32 weights)
53#define BLOCK_STRIDE (N_SIMDWIDTH/8)
54
55#ifdef INTEL_GPU
56REQD_SUBGROUP_SIZE_16
57#elif defined (ADRENO_GPU)
58REQD_SUBGROUP_SIZE_64
59#endif
60kernel void kernel_mul_mv_q5_K_f32_flat(
61 global uchar * src0_q,
62 global uchar * src0_qh,
63 global uchar * src0_s,
64 global half * src0_d,
65 global half * src0_dm,
66 global char * src1,
67 int offset1,
68 global char * dst,
69 int offsetd,
70 int ne00,
71 int ne01,
72 ulong nb01,
73 ulong nb02,
74 ulong nb03,
75 int ne12,
76 ulong nb11,
77 ulong nb12,
78 ulong nb13,
79 int ne0,
80 int ne1,
81 int r2,
82 int r3
83) {
84 src1 = src1 + offset1;
85 dst = dst + offsetd;
86
87 ushort kmask1 = 0x3f3f;
88 ushort kmask2 = 0x0f0f;
89 ushort kmask3 = 0xc0c0;
90
91 int ix = get_sub_group_local_id()/8;
92 int it = get_sub_group_local_id()%8;
93 int iq = it/4;
94 int ir = it%4;
95
96 int nb = ne00/QK_K;
97
98 int r0 = get_group_id(0);
99 int r1 = get_group_id(1);
100 int im = get_group_id(2);
101 int first_row = (r0 * N_SIMDGROUP + get_sub_group_id()) * N_DST;
102
103 int i12 = im%ne12;
104 int i13 = im/ne12;
105
106 int offset_src0 = (first_row*nb01 + (i12/r2)*nb02 + (i13/r3)*nb03)/BLOCK_Q5K_SIZE;
107 uint blk = nb01 / BLOCK_Q5K_SIZE;
108 global uchar * blk_q = (global uchar *)src0_q + offset_src0*(QK_K/2);
109 global uchar * blk_qh = (global uchar *)src0_qh + offset_src0*(QK_K/8);
110 global uchar * blk_s = (global uchar *)src0_s + offset_src0*K_SCALE_SIZE;
111 global half * blk_d = (global half *)src0_d + offset_src0;
112 global half * blk_dm = (global half *)src0_dm + offset_src0;
113
114 int offset_src1 = r1*nb11 + (i12)*nb12 + (i13)*nb13;
115 global float * y = (global float *)(src1 + offset_src1);
116
117 float yl[16];
118 float yh[16];
119 float sumf[N_DST] = {0.f};
120 float all_sum;
121
122 global float * y4 = y + ix * QK_K + 64 * iq + 8 * ir;
123
124 uchar u1_lo = (uchar)(1 << (2*iq));
125 uchar u2_lo = (uchar)(2 << (2*iq));
126 uchar u1_hi = (uchar)(1 << (2*iq + 4));
127 uchar u2_hi = (uchar)(2 << (2*iq + 4));
128
129 ushort sc16[4];
130 uchar * sc8 = (uchar *)sc16;
131
132 for (int ib = ix; ib < nb; ib += BLOCK_STRIDE) {
133 float4 sumy = {0.f, 0.f, 0.f, 0.f};
134 for (int i = 0; i < 8; ++i) {
135 yl[i+0] = y4[i+0];
136 sumy.s0 += yl[i+0];
137
138 yl[i+8] = y4[i+32];
139 sumy.s1 += yl[i+8];
140
141 yh[i+0] = y4[i+128];
142 sumy.s2 += yh[i+0];
143
144 yh[i+8] = y4[i+160];
145 sumy.s3 += yh[i+8];
146 }
147
148 global ushort * q1 = (global ushort *)(blk_q + ib * (QK_K/2)) + (16 * iq + 4 * ir);
149 global uchar * qh = (global uchar *)(blk_qh + ib * (QK_K/8)) + 8 * ir;
150 global ushort * sc = (global ushort *)(blk_s + ib * K_SCALE_SIZE) + iq;
151 global half * d = blk_d + ib;
152 global half * dm = blk_dm + ib;
153
154 for (int row = 0; row < N_DST; row++) {
155 sc16[0] = sc[0] & kmask1;
156 sc16[1] = sc[2] & kmask1;
157 sc16[2] = ((sc[4] >> 0) & kmask2) | ((sc[0] & kmask3) >> 2);
158 sc16[3] = ((sc[4] >> 4) & kmask2) | ((sc[2] & kmask3) >> 2);
159
160 global ushort * q2 = q1 + 32;
161
162 // Load the 4 q1 / 4 q2 quant ushorts as 2 uints each. 16-bit integer ops are
163 // disproportionately slow on the A7X (E031.41) compiler; keeping the dequant
164 // operands in 32-bit registers avoids the ushort path (same fix as q4_K flat).
165 // q1/q2 are 4-byte aligned; w & 0x0F00 on the low/high half of a uint equals the
166 // original ushort mask value, so the math is unchanged. The qh high-bit term is
167 // byte-indexed (qh[0..7]) and left as-is.
168 global uint * q1u = (global uint *)q1;
169 global uint * q2u = (global uint *)q2;
170 uint a0 = q1u[0], a1 = q1u[1], b0 = q2u[0], b1 = q2u[1];
171 uint w0 = a0 & 0xFFFF, w1 = a0 >> 16, w2 = a1 & 0xFFFF, w3 = a1 >> 16;
172 uint v0 = b0 & 0xFFFF, v1 = b0 >> 16, v2 = b1 & 0xFFFF, v3 = b1 >> 16;
173
174 float4 acc1, acc2;
175 acc1.s0 =
176 yl[0]*((w0&0x000F)+(qh[0]&u1_lo?16.f:0.f)) +
177 yl[2]*((w1&0x000F)+(qh[2]&u1_lo?16.f:0.f)) +
178 yl[4]*((w2&0x000F)+(qh[4]&u1_lo?16.f:0.f)) +
179 yl[6]*((w3&0x000F)+(qh[6]&u1_lo?16.f:0.f));
180 acc1.s1 =
181 yl[1]*((w0&0x0F00)+(qh[1]&u1_lo?16.f*256.f:0.f)) +
182 yl[3]*((w1&0x0F00)+(qh[3]&u1_lo?16.f*256.f:0.f)) +
183 yl[5]*((w2&0x0F00)+(qh[5]&u1_lo?16.f*256.f:0.f)) +
184 yl[7]*((w3&0x0F00)+(qh[7]&u1_lo?16.f*256.f:0.f));
185 acc1.s2 =
186 yl[ 8]*((w0&0x00F0)+(qh[0]&u2_lo?16.f*16.f:0.f)) +
187 yl[10]*((w1&0x00F0)+(qh[2]&u2_lo?16.f*16.f:0.f)) +
188 yl[12]*((w2&0x00F0)+(qh[4]&u2_lo?16.f*16.f:0.f)) +
189 yl[14]*((w3&0x00F0)+(qh[6]&u2_lo?16.f*16.f:0.f));
190 acc1.s3 =
191 yl[ 9]*((w0&0xF000)+(qh[1]&u2_lo?16.f*4096.f:0.f)) +
192 yl[11]*((w1&0xF000)+(qh[3]&u2_lo?16.f*4096.f:0.f)) +
193 yl[13]*((w2&0xF000)+(qh[5]&u2_lo?16.f*4096.f:0.f)) +
194 yl[15]*((w3&0xF000)+(qh[7]&u2_lo?16.f*4096.f:0.f));
195 acc2.s0 =
196 yh[0]*((v0&0x000F)+(qh[0]&u1_hi?16.f:0.f)) +
197 yh[2]*((v1&0x000F)+(qh[2]&u1_hi?16.f:0.f)) +
198 yh[4]*((v2&0x000F)+(qh[4]&u1_hi?16.f:0.f)) +
199 yh[6]*((v3&0x000F)+(qh[6]&u1_hi?16.f:0.f));
200 acc2.s1 =
201 yh[1]*((v0&0x0F00)+(qh[1]&u1_hi?16.f*256.f:0.f)) +
202 yh[3]*((v1&0x0F00)+(qh[3]&u1_hi?16.f*256.f:0.f)) +
203 yh[5]*((v2&0x0F00)+(qh[5]&u1_hi?16.f*256.f:0.f)) +
204 yh[7]*((v3&0x0F00)+(qh[7]&u1_hi?16.f*256.f:0.f));
205 acc2.s2 =
206 yh[ 8]*((v0&0x00F0)+(qh[0]&u2_hi?16.f*16.f:0.f)) +
207 yh[10]*((v1&0x00F0)+(qh[2]&u2_hi?16.f*16.f:0.f)) +
208 yh[12]*((v2&0x00F0)+(qh[4]&u2_hi?16.f*16.f:0.f)) +
209 yh[14]*((v3&0x00F0)+(qh[6]&u2_hi?16.f*16.f:0.f));
210 acc2.s3 =
211 yh[ 9]*((v0&0xF000)+(qh[1]&u2_hi?16.f*4096.f:0.f)) +
212 yh[11]*((v1&0xF000)+(qh[3]&u2_hi?16.f*4096.f:0.f)) +
213 yh[13]*((v2&0xF000)+(qh[5]&u2_hi?16.f*4096.f:0.f)) +
214 yh[15]*((v3&0xF000)+(qh[7]&u2_hi?16.f*4096.f:0.f));
215
216 float dall = *d;
217 float dmin = *dm;
218 sumf[row] += dall * ((acc1.s0 + 1.f/256.f * acc1.s1) * sc8[0] +
219 (acc1.s2 + 1.f/256.f * acc1.s3) * sc8[1] * 1.f/16.f +
220 (acc2.s0 + 1.f/256.f * acc2.s1) * sc8[4] +
221 (acc2.s2 + 1.f/256.f * acc2.s3) * sc8[5] * 1.f/16.f) -
222 dmin * (sumy.s0 * sc8[2] + sumy.s1 * sc8[3] + sumy.s2 * sc8[6] + sumy.s3 * sc8[7]);
223
224 q1 += blk*64;
225 qh += blk*32;
226 sc += blk*6;
227 d += blk;
228 dm += blk;
229 }
230
231 y4 += BLOCK_STRIDE * QK_K;
232 }
233
234 global float * dst_f32 = (global float *) dst + im*ne0*ne1 + r1*ne0;
235
236 for (int row = 0; row < N_DST; ++row) {
237 all_sum = sub_group_reduce_add(sumf[row]);
238 if (first_row + row < ne01) {
239 if (get_sub_group_local_id() == 0) {
240 dst_f32[first_row + row] = all_sum;
241 }
242 }
243 }
244}