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

120 lines · 3931 bytes Raw Blame History
1#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2
3#define QK_K 256
4
5#ifndef IQ3_XXS_MV_NSG
6#define IQ3_XXS_MV_NSG 4
7#endif
8
9inline float4 iq3_xxs_vals(uint gv, uint sgv, uint base) {
10 const uint s = sgv >> base;
11 const uint4 sgn = (uint4)(s << 31, s << 30, s << 29, s << 28) & 0x80000000u;
12 return as_float4(as_uint4(convert_float4(as_uchar4(gv))) ^ sgn);
13}
14
15kernel void kernel_gemv_noshuffle_iq3_xxs_f32(
16 __read_only image1d_buffer_t grid_img,
17 global const uchar * src0_qs,
18 global const uint * src0_sas,
19 global const half * src0_d,
20 global const float * src1,
21 ulong offset1,
22 global float * dst,
23 ulong offsetd,
24 int ne00,
25 int ne01,
26 int ne10,
27 int ne0
28) {
29 src1 = (global const float *)((global const char *)src1 + offset1);
30 dst = (global float *)((global char *)dst + offsetd);
31
32 const uint m = (uint)ne01;
33 const uint K = (uint)ne00;
34 const uint nsb = K / QK_K;
35
36 const uint lid = get_local_id(0);
37 const uint sgi = get_local_id(1);
38 const uint col = get_group_id(1);
39
40 global const float * y = src1 + (ulong)col * (uint)ne10;
41
42#define IQ3_XXS_GRID(i) (read_imageui(grid_img, (int)(i)).x)
43
44 const uint mh = m >> 1;
45 const uint j = get_group_id(0) * 64u + lid;
46 const uint row = j << 1;
47
48 float sumf = 0.f;
49 float sumf1 = 0.f;
50
51 if (j < mh) {
52 global const ushort * qsu = (global const ushort *)src0_qs;
53
54 for (uint ib = sgi; ib < nsb; ib += IQ3_XXS_MV_NSG) {
55 const half2 dh = vload2(j + ib * mh, src0_d);
56 const float d0 = (float)dh.s0;
57 const float d1 = (float)dh.s1;
58
59 float acc0 = 0.f, acc1 = 0.f;
60 for (uint sb = 0; sb < 8u; ++sb) {
61 const uint sub = ib * 8u + sb;
62 const uint2 aux = vload2(j + sub * mh, src0_sas);
63
64 const uint grp = sub * 8u;
65 const uint qsb = j + grp * mh;
66
67 float a0 = 0.f, a1 = 0.f;
68 for (uint u = 0; u < 4u; ++u) {
69 const uint qsv0 = (uint)qsu[qsb + (2u*u)*mh];
70 const uint qsv1 = (uint)qsu[qsb + (2u*u + 1u)*mh];
71 const uint sg0 = iq3_xxs_signs((aux.s0 >> (7u * u)) & 127u);
72 const uint sg1 = iq3_xxs_signs((aux.s1 >> (7u * u)) & 127u);
73
74 const uint g00 = qsv0 & 0xFFu;
75 const uint g01 = qsv1 & 0xFFu;
76 const uint g10 = (qsv0 >> 8) & 0xFFu;
77 const uint g11 = (qsv1 >> 8) & 0xFFu;
78
79 const float4 yl = vload4(sub * 8u + u * 2u + 0u, y);
80 const float4 yh = vload4(sub * 8u + u * 2u + 1u, y);
81
82 a0 += dot(yl, iq3_xxs_vals(IQ3_XXS_GRID(g00), sg0, 0u))
83 + dot(yh, iq3_xxs_vals(IQ3_XXS_GRID(g01), sg0, 4u));
84 a1 += dot(yl, iq3_xxs_vals(IQ3_XXS_GRID(g10), sg1, 0u))
85 + dot(yh, iq3_xxs_vals(IQ3_XXS_GRID(g11), sg1, 4u));
86 }
87 acc0 += (0.5f + (float)(aux.s0 >> 28)) * a0;
88 acc1 += (0.5f + (float)(aux.s1 >> 28)) * a1;
89 }
90 sumf += d0 * 0.5f * acc0;
91 sumf1 += d1 * 0.5f * acc1;
92 }
93 }
94
95#if IQ3_XXS_MV_NSG > 1
96 __local float2 part[IQ3_XXS_MV_NSG][64];
97 part[sgi][lid] = (float2)(sumf, sumf1);
98 barrier(CLK_LOCAL_MEM_FENCE);
99 if (sgi != 0) {
100 return;
101 }
102 for (uint s = 1; s < IQ3_XXS_MV_NSG; ++s) {
103 const float2 p = part[s][lid];
104 sumf += p.s0;
105 sumf1 += p.s1;
106 }
107#endif
108
109 if (j < mh) {
110 vstore2((float2)(sumf, sumf1), 0, dst + (ulong)col * (uint)ne0 + row);
111 }
112#undef IQ3_XXS_GRID
113}
114
115kernel void kernel_iq3_xxs_grid_export(global uint * out) {
116 const uint i = get_global_id(0);
117 if (i < 256u) {
118 out[i] = iq3_xxs_grid[i];
119 }
120}