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

85 lines · 2895 bytes Raw Blame History
1#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2
3#define QK_K 256
4
5#ifndef IQ4_XS_MV_NSG
6#define IQ4_XS_MV_NSG 8
7#endif
8
9inline float4 iq4_xs_vals(ushort q) {
10 return (float4)((char)iq4_xs_code(q & 15), (char)iq4_xs_code((q >> 4) & 15),
11 (char)iq4_xs_code((q >> 8) & 15), (char)iq4_xs_code(q >> 12));
12}
13
14kernel void kernel_gemv_noshuffle_iq4_xs_f32(
15 global const ushort * src0_qs,
16 global const uint * src0_sl,
17 global const ushort * src0_sh,
18 global const half * src0_d,
19 global const float * src1,
20 ulong offset1,
21 global float * dst,
22 ulong offsetd,
23 int ne00,
24 int ne01,
25 int ne10,
26 int ne0
27) {
28 src1 = (global const float *)((global const char *)src1 + offset1);
29 dst = (global float *)((global char *)dst + offsetd);
30
31 const uint m = (uint)ne01;
32 const uint mh = m >> 1;
33 const uint lid = get_local_id(0);
34 const uint sgi = get_local_id(1);
35 const uint col = get_group_id(1);
36 const uint j = get_group_id(0)*64u + lid;
37 const uint row = j << 1;
38 global const float * y = src1 + (ulong)col * (uint)ne10;
39
40 float sum0 = 0.0f, sum1 = 0.0f;
41 if (j < mh) {
42 global const uint * qs = (global const uint *)src0_qs;
43 global const uint * sh = (global const uint *)src0_sh;
44 for (uint ib = sgi; ib < (uint)ne00/QK_K; ib += IQ4_XS_MV_NSG) {
45 const uint base = j + ib*mh;
46 const uint2 sl = vload2(base, src0_sl);
47 const uint shp = sh[base];
48 const half2 d = vload2(base, src0_d);
49 float acc0 = 0.0f, acc1 = 0.0f;
50 for (uint sb = 0; sb < 8; ++sb) {
51 const int ls0 = (int)(((sl.s0 >> (4*sb)) & 15u) | (((shp >> (2*sb)) & 3u) << 4)) - 32;
52 const int ls1 = (int)(((sl.s1 >> (4*sb)) & 15u) | (((shp >> (16 + 2*sb)) & 3u) << 4)) - 32;
53 float a0 = 0.0f, a1 = 0.0f;
54 for (uint u = 0; u < 8; ++u) {
55 const uint group = ib*64 + sb*8 + u;
56 const uint q = qs[j + group*mh];
57 const float4 yv = vload4(group, y);
58 a0 += dot(yv, iq4_xs_vals((ushort)q));
59 a1 += dot(yv, iq4_xs_vals((ushort)(q >> 16)));
60 }
61 acc0 += (float)ls0*a0;
62 acc1 += (float)ls1*a1;
63 }
64 sum0 += (float)d.s0*acc0;
65 sum1 += (float)d.s1*acc1;
66 }
67 }
68
69#if IQ4_XS_MV_NSG > 1
70 local float2 part[IQ4_XS_MV_NSG][64];
71 part[sgi][lid] = (float2)(sum0, sum1);
72 barrier(CLK_LOCAL_MEM_FENCE);
73 if (sgi != 0) {
74 return;
75 }
76 for (uint s = 1; s < IQ4_XS_MV_NSG; ++s) {
77 const float2 v = part[s][lid];
78 sum0 += v.s0;
79 sum1 += v.s1;
80 }
81#endif
82 if (j < mh) {
83 vstore2((float2)(sum0, sum1), 0, dst + (ulong)col*(uint)ne0 + row);
84 }
85}