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

134 lines · 4984 bytes Raw Blame History
1#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2
3#define QK_K 256
4#define IQ1_M_DELTA 0.125f
5
6#ifndef IQ1_M_MV_NSG
7#define IQ1_M_MV_NSG 4
8#endif
9
10inline float4 iq1_m_vals(uint g) {
11 return (float4)((float)( g & 0xFu), (float)((g >> 8) & 0xFu),
12 (float)((g >> 16) & 0xFu), (float)((g >> 24) & 0xFu));
13}
14
15inline float iq1_m_super(uint w0, uint w1, uint w2, uint w3) {
16 const ushort u16 = (ushort)(((w0 >> 12)) | ((w1 >> 8) & 0x00f0u)
17 | ((w2 >> 4) & 0x0f00u) | (w3 & 0xf000u));
18 return (float)as_half(u16);
19}
20
21kernel void kernel_gemv_noshuffle_iq1_m_f32(
22 __read_only image1d_buffer_t grid_img,
23 global const uchar * src0_qs,
24 global const uchar * src0_qh,
25 global const ushort * src0_sc,
26 global const float * src1,
27 ulong offset1,
28 global float * dst,
29 ulong offsetd,
30 int ne00,
31 int ne01,
32 int ne10,
33 int ne0
34) {
35 src1 = (global const float *)((global const char *)src1 + offset1);
36 dst = (global float *)((global char *)dst + offsetd);
37
38 const uint m = (uint)ne01;
39 const uint K = (uint)ne00;
40 const uint nsb = K / QK_K;
41
42 const uint lid = get_local_id(0);
43 const uint sgi = get_local_id(1);
44 const uint col = get_group_id(1);
45
46 global const float * y = src1 + (ulong)col * (uint)ne10;
47
48#define IQ1_M_GRID(i) (read_imageui(grid_img, (int)(i)).x)
49
50 const uint mh = m >> 1;
51 const uint j = get_group_id(0) * 64u + lid;
52 const uint row = j << 1;
53
54 float sumf = 0.f, sumf1 = 0.f;
55
56 if (j < mh) {
57 global const ushort * qsu = (global const ushort *)src0_qs;
58 global const ushort * qhu = (global const ushort *)src0_qh;
59 global const uint * scu = (global const uint *)src0_sc;
60
61 for (uint ib = sgi; ib < nsb; ib += IQ1_M_MV_NSG) {
62 const uint sb = j + (ib * 4u) * mh;
63 const uint w0 = scu[sb + 0u * mh];
64 const uint w1 = scu[sb + 1u * mh];
65 const uint w2 = scu[sb + 2u * mh];
66 const uint w3 = scu[sb + 3u * mh];
67 const float d0 = iq1_m_super( w0 & 0xFFFFu, w1 & 0xFFFFu,
68 w2 & 0xFFFFu, w3 & 0xFFFFu);
69 const float d1 = iq1_m_super((w0 >> 16) & 0xFFFFu, (w1 >> 16) & 0xFFFFu,
70 (w2 >> 16) & 0xFFFFu, (w3 >> 16) & 0xFFFFu);
71
72 float acc0 = 0.f, acc1 = 0.f;
73
74 for (uint sub_l = 0; sub_l < 8u; ++sub_l) {
75 const uint sub = ib * 8u + sub_l;
76 const uint scw = scu[j + (ib * 4u + (sub_l >> 1)) * mh];
77 const uint grp = sub * 8u;
78 const uint shb = 3u * (2u * (sub_l & 1u));
79
80 for (uint l = 0; l < 4u; ++l) {
81 const uint sh = shb + 3u * (l >> 1);
82 const float s0 = (float)(2u * (((scw ) >> sh) & 7u) + 1u);
83 const float s1 = (float)(2u * (((scw >> 16) >> sh) & 7u) + 1u);
84
85 const uint qhv = (uint)qhu[j + (sub * 2u + (l >> 1)) * mh];
86 const uint h0 = qhv & 0xFFu;
87 const uint h1 = (qhv >> 8) & 0xFFu;
88 const uint qsv = (uint)qsu[j + (sub * 4u + l) * mh];
89
90 const uint msk = 0x08u << (4u * (l & 1u));
91 const float t0 = (h0 & msk) ? (-1.f - IQ1_M_DELTA) : (-1.f + IQ1_M_DELTA);
92 const float t1 = (h1 & msk) ? (-1.f - IQ1_M_DELTA) : (-1.f + IQ1_M_DELTA);
93
94 const uint g0 = IQ1_M_GRID(( qsv & 0xFFu)
95 | ((((h0 >> (4u*(l & 1u))) & 7u) << 8)));
96 const uint g1 = IQ1_M_GRID(((qsv >> 8) & 0xFFu)
97 | ((((h1 >> (4u*(l & 1u))) & 7u) << 8)));
98
99 const float4 y0 = vload4(grp + 2u*l + 0u, y);
100 const float4 y1 = vload4(grp + 2u*l + 1u, y);
101
102 const float4 ys = y0 + y1;
103 const float as = ys.s0 + ys.s1 + ys.s2 + ys.s3;
104
105 const float a0 = dot(y0, iq1_m_vals(g0)) + dot(y1, iq1_m_vals(g0 >> 4));
106 const float a1 = dot(y0, iq1_m_vals(g1)) + dot(y1, iq1_m_vals(g1 >> 4));
107 acc0 += s0 * (a0 + t0 * as);
108 acc1 += s1 * (a1 + t1 * as);
109 }
110 }
111 sumf += d0 * acc0;
112 sumf1 += d1 * acc1;
113 }
114 }
115
116#if IQ1_M_MV_NSG > 1
117 __local float2 part[IQ1_M_MV_NSG][64];
118 part[sgi][lid] = (float2)(sumf, sumf1);
119 barrier(CLK_LOCAL_MEM_FENCE);
120 if (sgi != 0) {
121 return;
122 }
123 for (uint s = 1; s < IQ1_M_MV_NSG; ++s) {
124 const float2 p = part[s][lid];
125 sumf += p.s0;
126 sumf1 += p.s1;
127 }
128#endif
129
130 if (j < mh) {
131 vstore2((float2)(sumf, sumf1), 0, dst + (ulong)col * (uint)ne0 + row);
132 }
133#undef IQ1_M_GRID
134}