Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-opencl/kernels/gemv_noshuffle_iq2_s_f32.cl # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-opencl / kernels / gemv_noshuffle_iq2_s_f32.cl` 122 lines ยท 4401 bytes Raw Blame History 1 #pragma OPENCL EXTENSION cl_khr_fp16 : enable 2 3 #define QK_K 256 4 5 #ifndef IQ2_S_MV_NSG 6 #define IQ2_S_MV_NSG 8 7 #endif 8 9 inline float4 iq2_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 16 kernel void kernel_iq2_s_grid_export(global uint * out) { 17 out[get_global_id(0)] = iq2_s_grid[get_global_id(0)]; 18 } 19 20 kernel void kernel_gemv_noshuffle_iq2_s_f32( 21 read_only image1d_buffer_t grid_img, 22 global const uchar * src0_qs, 23 global const uchar * src0_sg, 24 global const uchar * src0_qh, 25 global const uchar * src0_sc, 26 global const half * src0_d, 27 global const float * src1, 28 ulong offset1, 29 global float * dst, 30 ulong offsetd, 31 int ne00, 32 int ne01, 33 int ne10, 34 int ne0 35 ) { 36 src1 = (global const float *)((global const char *)src1 + offset1); 37 dst = (global float *)((global char *)dst + offsetd); 38 39 const uint m = (uint)ne01; 40 const uint K = (uint)ne00; 41 const uint nsb = K / QK_K; 42 43 const uint lid = get_local_id(0); 44 const uint sgi = get_local_id(1); 45 const uint col = get_group_id(1); 46 47 global const float * y = src1 + (ulong)col * (uint)ne10; 48 49 #define IQ2_S_GRID(i) read_imageui(grid_img, (int)(i)).x 50 51 const uint mh = m >> 1; 52 const uint j = get_group_id(0) * 64u + lid; 53 const uint row = j << 1; 54 55 float sumf = 0.f, sumf1 = 0.f; 56 57 if (j < mh) { 58 global const ushort * qsu = (global const ushort *)src0_qs; 59 global const ushort * sgu = (global const ushort *)src0_sg; 60 global const ushort * qhu = (global const ushort *)src0_qh; 61 global const ushort * scu = (global const ushort *)src0_sc; 62 63 for (uint ib = sgi; ib < nsb; ib += IQ2_S_MV_NSG) { 64 const half2 dh = vload2(j + ib * mh, src0_d); 65 66 float acc0 = 0.f, acc1 = 0.f; 67 for (uint sb = 0; sb < 8u; ++sb) { 68 const uint sub = ib * 8u + sb; 69 const uint qhv = (uint)qhu[j + sub * mh]; 70 const uint scv = (uint)scu[j + sub * mh]; 71 const uint gb = j + (sub * 4u) * mh; 72 73 for (uint h = 0; h < 2u; ++h) { 74 const uint n0 = (h == 0u) ? ( scv & 0xFu) : (( scv >> 4) & 0xFu); 75 const uint n1 = (h == 0u) ? ((scv >> 8) & 0xFu) : (((scv >> 8) >> 4) & 0xFu); 76 77 float a0 = 0.f, a1 = 0.f; 78 for (uint t = 0; t < 2u; ++t) { 79 const uint l = 2u*h + t; 80 const uint qsv = (uint)qsu[gb + l * mh]; 81 const uint sgv = (uint)sgu[gb + l * mh]; 82 const uint gi0 = ( qsv & 0xFFu) | ((((qhv ) >> (2u*l)) & 3u) << 8); 83 const uint gi1 = ((qsv >> 8) & 0xFFu) | ((((qhv >> 8) >> (2u*l)) & 3u) << 8); 84 const uint s0 = sgv & 0xFFu; 85 const uint s1 = (sgv >> 8) & 0xFFu; 86 87 const uint grp = (ib * 64u + sb * 8u) + l * 2u; 88 const float4 y0 = vload4(grp + 0u, y); 89 const float4 y1 = vload4(grp + 1u, y); 90 a0 += dot(y0, iq2_s_vals(IQ2_S_GRID(2u*gi0 + 0u), s0, 0u)); 91 a0 += dot(y1, iq2_s_vals(IQ2_S_GRID(2u*gi0 + 1u), s0, 4u)); 92 a1 += dot(y0, iq2_s_vals(IQ2_S_GRID(2u*gi1 + 0u), s1, 0u)); 93 a1 += dot(y1, iq2_s_vals(IQ2_S_GRID(2u*gi1 + 1u), s1, 4u)); 94 } 95 acc0 += (0.5f + (float)n0) * a0; 96 acc1 += (0.5f + (float)n1) * a1; 97 } 98 } 99 sumf += (float)dh.s0 * 0.25f * acc0; 100 sumf1 += (float)dh.s1 * 0.25f * acc1; 101 } 102 } 103 104 #if IQ2_S_MV_NSG > 1 105 __local float2 part[IQ2_S_MV_NSG][64]; 106 part[sgi][lid] = (float2)(sumf, sumf1); 107 barrier(CLK_LOCAL_MEM_FENCE); 108 if (sgi != 0) { 109 return; 110 } 111 for (uint s = 1; s < IQ2_S_MV_NSG; ++s) { 112 const float2 p = part[s][lid]; 113 sumf += p.s0; 114 sumf1 += p.s1; 115 } 116 #endif 117 118 if (j < mh) { 119 vstore2((float2)(sumf, sumf1), 0, dst + (ulong)col * (uint)ne0 + row); 120 } 121 #undef IQ2_S_GRID 122 }