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

128 lines · 4401 bytes Raw Blame History
1#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2
3#define QK_K 256
4
5#ifndef IQ3_S_MV_NSG
6#define IQ3_S_MV_NSG 4
7#endif
8
9inline float4 iq3_s_vals(uint gv, uint sg, uint base) {
10
11 const uint s = sg >> base;
12 const uint4 sgn = (uint4)(s << 31, s << 30, s << 29, s << 28) & 0x80000000u;
13 return as_float4(as_uint4(convert_float4(as_uchar4(gv))) ^ sgn);
14}
15
16kernel void kernel_gemv_noshuffle_iq3_s_f32(
17 __read_only image1d_buffer_t grid_img,
18 global const uchar * src0_qs,
19 global const uchar * src0_sg,
20 global const uchar * src0_qh,
21 global const uchar * src0_sc,
22 global const half * src0_d,
23 global const float * src1,
24 ulong offset1,
25 global float * dst,
26 ulong offsetd,
27 int ne00,
28 int ne01,
29 int ne10,
30 int ne0
31) {
32 src1 = (global const float *)((global const char *)src1 + offset1);
33 dst = (global float *)((global char *)dst + offsetd);
34
35 const uint m = (uint)ne01;
36 const uint K = (uint)ne00;
37 const uint nsb = K / QK_K;
38
39 const uint lid = get_local_id(0);
40 const uint sgi = get_local_id(1);
41 const uint col = get_group_id(1);
42
43 global const float * y = src1 + (ulong)col * (uint)ne10;
44
45#define IQ3_S_GRID(i) (read_imageui(grid_img, (int)(i)).x)
46
47 const uint mh = m >> 1;
48 const uint j = get_group_id(0) * 64u + lid;
49 const uint row = j << 1;
50
51 float sumf = 0.f;
52 float sumf1 = 0.f;
53
54 if (j < mh) {
55 global const ushort * qsu = (global const ushort *)src0_qs;
56 global const ushort * qhu = (global const ushort *)src0_qh;
57 global const ushort * sgu = (global const ushort *)src0_sg;
58 global const ushort * scu = (global const ushort *)src0_sc;
59
60 for (uint ib = sgi; ib < nsb; ib += IQ3_S_MV_NSG) {
61 const half2 dh = vload2(j + ib * mh, src0_d);
62 const float d0 = (float)dh.s0;
63 const float d1 = (float)dh.s1;
64
65 const uint scbase = j + ib * 4u * mh;
66 ushort sc4[4];
67 sc4[0] = scu[scbase + 0u * mh];
68 sc4[1] = scu[scbase + 1u * mh];
69 sc4[2] = scu[scbase + 2u * mh];
70 sc4[3] = scu[scbase + 3u * mh];
71
72 float acc0 = 0.f, acc1 = 0.f;
73 for (uint sb = 0; sb < 8u; ++sb) {
74 const uint scv = (uint)sc4[sb >> 1];
75 const uint nib0 = (sb & 1u) ? ((scv >> 4) & 0xFu) : ( scv & 0xFu);
76 const uint nib1 = (sb & 1u) ? ((scv >> 12) & 0xFu) : ((scv >> 8) & 0xFu);
77
78 const uint qhv = (uint)qhu[j + (ib * 8u + sb) * mh];
79 const uint qh0 = qhv & 0xFFu;
80 const uint qh1 = qhv >> 8;
81
82 const uint grp = ib * 64u + sb * 8u;
83 const uint qsb = j + grp * mh;
84 const uint sgb = j + (ib * 32u + sb * 4u) * mh;
85
86 float a0 = 0.f, a1 = 0.f;
87 for (uint u = 0; u < 8u; ++u) {
88 const uint qsv = (uint)qsu[qsb + u * mh];
89 const uint sgv = (uint)sgu[sgb + (u >> 1) * mh];
90 const uint g0 = ( qsv & 0xFFu) | (((qh0 >> u) & 1u) << 8);
91 const uint g1 = ((qsv >> 8) & 0xFFu) | (((qh1 >> u) & 1u) << 8);
92 const uint base = (u & 1u) * 4u;
93 const float4 yv = vload4(grp + u, y);
94 const uint gw0 = IQ3_S_GRID(g0);
95 const uint gw1 = IQ3_S_GRID(g1);
96 a0 += dot(yv, iq3_s_vals(gw0, sgv & 0xFFu, base));
97 a1 += dot(yv, iq3_s_vals(gw1, (sgv >> 8) & 0xFFu, base));
98 }
99 acc0 += (float)(1u + 2u * nib0) * a0;
100 acc1 += (float)(1u + 2u * nib1) * a1;
101 }
102 sumf += d0 * acc0;
103 sumf1 += d1 * acc1;
104 }
105 }
106
107#if IQ3_S_MV_NSG > 1
108 __local float2 part[IQ3_S_MV_NSG][64];
109 part[sgi][lid] = (float2)(sumf, sumf1);
110 barrier(CLK_LOCAL_MEM_FENCE);
111 if (sgi != 0) {
112 return;
113 }
114 for (uint s = 1; s < IQ3_S_MV_NSG; ++s) {
115 const float2 p = part[s][lid];
116 sumf += p.s0;
117 sumf1 += p.s1;
118 }
119#endif
120
121 if (j < mh) {
122 vstore2((float2)(sumf, sumf1), 0, dst + (ulong)col * (uint)ne0 + row);
123 }
124}
125
126kernel void kernel_iq3_s_grid_export(global uint * out) {
127 out[get_global_id(0)] = iq3_s_grid[get_global_id(0)];
128}