ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-opencl / kernels / gemv_noshuffle_iq2_xs_f32.cl
| 1 | #pragma OPENCL EXTENSION cl_khr_fp16 : enable |
| 2 | |
| 3 | #define QK_K 256 |
| 4 | |
| 5 | #ifndef IQ2_XS_MV_NSG |
| 6 | #define IQ2_XS_MV_NSG 4 |
| 7 | #endif |
| 8 | |
| 9 | inline float4 iq2_xs_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 | |
| 15 | kernel void kernel_gemv_noshuffle_iq2_xs_f32( |
| 16 | __read_only image1d_buffer_t grid_img, |
| 17 | global const ushort * src0_qs, |
| 18 | global const uchar * src0_sc, |
| 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_XS_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 uint * qsu = (global const uint *)src0_qs; |
| 53 | global const ushort * scu = (global const ushort *)src0_sc; |
| 54 | |
| 55 | for (uint ib = sgi; ib < nsb; ib += IQ2_XS_MV_NSG) { |
| 56 | const half2 dh = vload2(j + ib * mh, src0_d); |
| 57 | const float d0 = (float)dh.s0; |
| 58 | const float d1 = (float)dh.s1; |
| 59 | |
| 60 | float acc0 = 0.f, acc1 = 0.f; |
| 61 | for (uint sb = 0; sb < 8u; ++sb) { |
| 62 | const uint sub = ib * 8u + sb; |
| 63 | const uint scv = (uint)scu[j + sub * mh]; |
| 64 | |
| 65 | const float lo0 = 0.5f + (float)( scv & 0xFu); |
| 66 | const float hi0 = 0.5f + (float)((scv >> 4) & 0xFu); |
| 67 | const float lo1 = 0.5f + (float)((scv >> 8) & 0xFu); |
| 68 | const float hi1 = 0.5f + (float)((scv >> 12) & 0xFu); |
| 69 | |
| 70 | const uint grp = sub * 4u; |
| 71 | const uint qsb = j + grp * mh; |
| 72 | |
| 73 | float alo0 = 0.f, ahi0 = 0.f, alo1 = 0.f, ahi1 = 0.f; |
| 74 | for (uint u = 0; u < 4u; ++u) { |
| 75 | const uint qsv = qsu[qsb + u * mh]; |
| 76 | const uint q0 = qsv & 0xFFFFu; |
| 77 | const uint q1 = qsv >> 16; |
| 78 | |
| 79 | const uint g0 = (q0 & 511u) << 1; |
| 80 | const uint g1 = (q1 & 511u) << 1; |
| 81 | const uint sg0 = iq2_xs_signs(q0 >> 9); |
| 82 | const uint sg1 = iq2_xs_signs(q1 >> 9); |
| 83 | |
| 84 | const float4 yl = vload4(sub * 8u + u * 2u + 0u, y); |
| 85 | const float4 yh = vload4(sub * 8u + u * 2u + 1u, y); |
| 86 | |
| 87 | const float a0 = dot(yl, iq2_xs_vals(IQ2_XS_GRID(g0 ), sg0, 0u)) |
| 88 | + dot(yh, iq2_xs_vals(IQ2_XS_GRID(g0 + 1), sg0, 4u)); |
| 89 | const float a1 = dot(yl, iq2_xs_vals(IQ2_XS_GRID(g1 ), sg1, 0u)) |
| 90 | + dot(yh, iq2_xs_vals(IQ2_XS_GRID(g1 + 1), sg1, 4u)); |
| 91 | |
| 92 | if (u < 2u) { alo0 += a0; alo1 += a1; } |
| 93 | else { ahi0 += a0; ahi1 += a1; } |
| 94 | } |
| 95 | acc0 += lo0 * alo0 + hi0 * ahi0; |
| 96 | acc1 += lo1 * alo1 + hi1 * ahi1; |
| 97 | } |
| 98 | sumf += d0 * 0.25f * acc0; |
| 99 | sumf1 += d1 * 0.25f * acc1; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | #if IQ2_XS_MV_NSG > 1 |
| 104 | __local float2 part[IQ2_XS_MV_NSG][64]; |
| 105 | part[sgi][lid] = (float2)(sumf, sumf1); |
| 106 | barrier(CLK_LOCAL_MEM_FENCE); |
| 107 | if (sgi != 0) { |
| 108 | return; |
| 109 | } |
| 110 | for (uint s = 1; s < IQ2_XS_MV_NSG; ++s) { |
| 111 | const float2 p = part[s][lid]; |
| 112 | sumf += p.s0; |
| 113 | sumf1 += p.s1; |
| 114 | } |
| 115 | #endif |
| 116 | |
| 117 | if (j < mh) { |
| 118 | vstore2((float2)(sumf, sumf1), 0, dst + (ulong)col * (uint)ne0 + row); |
| 119 | } |
| 120 | #undef IQ2_XS_GRID |
| 121 | } |
| 122 | |
| 123 | kernel void kernel_iq2_xs_grid_export(global uint * out) { |
| 124 | const uint i = get_global_id(0); |
| 125 | if (i < 1024u) { |
| 126 | out[i] = iq2_xs_grid[i]; |
| 127 | } |
| 128 | } |