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

117 lines · 3764 bytes Raw Blame History
1#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2
3#define QK_K 256
4
5#ifndef IQ2_XXS_MV_NSG
6#define IQ2_XXS_MV_NSG 4
7#endif
8
9inline float4 iq2_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_iq2_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 IQ2_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 += IQ2_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 * 4u;
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 qsv = (uint)qsu[qsb + u * mh];
70 const uint sg0 = iq2_xxs_signs((aux.s0 >> (7u * u)) & 127u);
71 const uint sg1 = iq2_xxs_signs((aux.s1 >> (7u * u)) & 127u);
72
73 const uint g0 = ( qsv & 0xFFu) << 1;
74 const uint g1 = ((qsv >> 8) & 0xFFu) << 1;
75
76 const float4 yl = vload4(sub * 8u + u * 2u + 0u, y);
77 const float4 yh = vload4(sub * 8u + u * 2u + 1u, y);
78
79 a0 += dot(yl, iq2_xxs_vals(IQ2_XXS_GRID(g0 ), sg0, 0u))
80 + dot(yh, iq2_xxs_vals(IQ2_XXS_GRID(g0 + 1), sg0, 4u));
81 a1 += dot(yl, iq2_xxs_vals(IQ2_XXS_GRID(g1 ), sg1, 0u))
82 + dot(yh, iq2_xxs_vals(IQ2_XXS_GRID(g1 + 1), sg1, 4u));
83 }
84 acc0 += (0.5f + (float)(aux.s0 >> 28)) * a0;
85 acc1 += (0.5f + (float)(aux.s1 >> 28)) * a1;
86 }
87 sumf += d0 * 0.25f * acc0;
88 sumf1 += d1 * 0.25f * acc1;
89 }
90 }
91
92#if IQ2_XXS_MV_NSG > 1
93 __local float2 part[IQ2_XXS_MV_NSG][64];
94 part[sgi][lid] = (float2)(sumf, sumf1);
95 barrier(CLK_LOCAL_MEM_FENCE);
96 if (sgi != 0) {
97 return;
98 }
99 for (uint s = 1; s < IQ2_XXS_MV_NSG; ++s) {
100 const float2 p = part[s][lid];
101 sumf += p.s0;
102 sumf1 += p.s1;
103 }
104#endif
105
106 if (j < mh) {
107 vstore2((float2)(sumf, sumf1), 0, dst + (ulong)col * (uint)ne0 + row);
108 }
109#undef IQ2_XXS_GRID
110}
111
112kernel void kernel_iq2_xxs_grid_export(global uint * out) {
113 const uint i = get_global_id(0);
114 if (i < 512u) {
115 out[i] = iq2_xxs_grid[i];
116 }
117}