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_q2_k_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_q2_k_f32.cl` 231 lines ยท 8159 bytes Raw Blame History 1 #pragma OPENCL EXTENSION cl_khr_fp16 : enable 2 3 #define QK_K 256 4 5 #ifndef Q2_K_MV_NSG 6 #define Q2_K_MV_NSG 8 7 #endif 8 9 #ifndef Q2_K_MV_R 10 #define Q2_K_MV_R 4 11 #endif 12 13 inline float4 q2_k_vals(uint pk) { 14 return (float4)((float)( pk & 3u), (float)((pk >> 2) & 3u), 15 (float)((pk >> 4) & 3u), (float)((pk >> 6) & 3u)); 16 } 17 18 kernel void kernel_gemv_noshuffle_q2_k_f32( 19 global const uchar * src0_qs, 20 global const uchar * src0_sc, 21 global const half * src0_dm, 22 global const float * src1, 23 ulong offset1, 24 global float * dst, 25 ulong offsetd, 26 int ne00, // K 27 int ne01, // M 28 int ne10, // activation row stride, == K 29 int ne0 // dst row stride 30 ) { 31 src1 = (global const float *)((global const char *)src1 + offset1); 32 dst = (global float *)((global char *)dst + offsetd); 33 34 const uint m = (uint)ne01; 35 const uint K = (uint)ne00; 36 const uint nsb = K / QK_K; 37 38 const uint lid = get_local_id(0); 39 const uint sgi = get_local_id(1); 40 const uint col = get_group_id(1); 41 42 global const float * y = src1 + (ulong)col * (uint)ne10; 43 44 const uint mr = m / Q2_K_MV_R; // row groups 45 const uint j = get_group_id(0) * 64u + lid; // row group this lane owns 46 const uint row = j * Q2_K_MV_R; 47 48 #if Q2_K_MV_R == 4 49 float sumf0 = 0.f; 50 float sumf1 = 0.f; 51 float sumf2 = 0.f; 52 float sumf3 = 0.f; 53 54 if (j < mr) { 55 global const uint * qsu = (global const uint *)src0_qs; 56 global const uint * scu = (global const uint *)src0_sc; 57 58 for (uint ib = sgi; ib < nsb; ib += Q2_K_MV_NSG) { 59 const half4 dma = vload4(2u*(j + ib*mr) + 0u, src0_dm); // d0, dmin0, d1, dmin1 60 const half4 dmb = vload4(2u*(j + ib*mr) + 1u, src0_dm); // d2, dmin2, d3, dmin3 61 62 float ad0 = 0.f, am0 = 0.f, ad1 = 0.f, am1 = 0.f; 63 float ad2 = 0.f, am2 = 0.f, ad3 = 0.f, am3 = 0.f; 64 65 for (uint sb = 0; sb < 8u; ++sb) { 66 const uint grp = ib * 64u + sb * 8u; 67 const uint qsb = j + grp * mr; 68 69 for (uint h = 0; h < 2u; ++h) { 70 const uint scv = scu[j + (2u * (ib * 8u + sb) + h) * mr]; 71 72 float a0 = 0.f, a1 = 0.f, a2 = 0.f, a3 = 0.f; 73 float4 as = (float4)(0.f); 74 for (uint u = 0; u < 4u; ++u) { 75 const uint gg = 4u*h + u; 76 const uint qsv = qsu[qsb + gg * mr]; // four rows, one load 77 const float4 yv = vload4(grp + gg, y); 78 as += yv; 79 a0 += dot(yv, q2_k_vals( qsv & 0xFFu)); 80 a1 += dot(yv, q2_k_vals((qsv >> 8) & 0xFFu)); 81 a2 += dot(yv, q2_k_vals((qsv >> 16) & 0xFFu)); 82 a3 += dot(yv, q2_k_vals((qsv >> 24) & 0xFFu)); 83 } 84 const float asum = as.s0 + as.s1 + as.s2 + as.s3; 85 86 const uint s0 = scv & 0xFFu; 87 const uint s1 = (scv >> 8) & 0xFFu; 88 const uint s2 = (scv >> 16) & 0xFFu; 89 const uint s3 = (scv >> 24) & 0xFFu; 90 ad0 += (float)(s0 & 0xFu) * a0; am0 += (float)(s0 >> 4) * asum; 91 ad1 += (float)(s1 & 0xFu) * a1; am1 += (float)(s1 >> 4) * asum; 92 ad2 += (float)(s2 & 0xFu) * a2; am2 += (float)(s2 >> 4) * asum; 93 ad3 += (float)(s3 & 0xFu) * a3; am3 += (float)(s3 >> 4) * asum; 94 } 95 } 96 sumf0 += (float)dma.s0 * ad0 - (float)dma.s1 * am0; 97 sumf1 += (float)dma.s2 * ad1 - (float)dma.s3 * am1; 98 sumf2 += (float)dmb.s0 * ad2 - (float)dmb.s1 * am2; 99 sumf3 += (float)dmb.s2 * ad3 - (float)dmb.s3 * am3; 100 } 101 } 102 103 #if Q2_K_MV_NSG > 1 104 __local float4 part[Q2_K_MV_NSG][64]; 105 part[sgi][lid] = (float4)(sumf0, sumf1, sumf2, sumf3); 106 barrier(CLK_LOCAL_MEM_FENCE); 107 if (sgi != 0) { 108 return; 109 } 110 for (uint s = 1; s < Q2_K_MV_NSG; ++s) { 111 const float4 p = part[s][lid]; 112 sumf0 += p.s0; 113 sumf1 += p.s1; 114 sumf2 += p.s2; 115 sumf3 += p.s3; 116 } 117 #endif 118 119 if (j < mr) { 120 global float * o = dst + (ulong)col * (uint)ne0 + row; 121 o[0] = sumf0; o[1] = sumf1; o[2] = sumf2; o[3] = sumf3; 122 } 123 124 #elif Q2_K_MV_R == 2 125 float sumf0 = 0.f; 126 float sumf1 = 0.f; 127 128 if (j < mr) { 129 global const ushort * qsu = (global const ushort *)src0_qs; 130 global const ushort * scu = (global const ushort *)src0_sc; 131 132 for (uint ib = sgi; ib < nsb; ib += Q2_K_MV_NSG) { 133 const half4 dm = vload4(j + ib * mr, src0_dm); // d0, dmin0, d1, dmin1 134 135 float ad0 = 0.f, am0 = 0.f, ad1 = 0.f, am1 = 0.f; 136 for (uint sb = 0; sb < 8u; ++sb) { 137 const uint grp = ib * 64u + sb * 8u; 138 const uint qsb = j + grp * mr; 139 140 for (uint h = 0; h < 2u; ++h) { 141 const uint scv = (uint)scu[j + (2u * (ib * 8u + sb) + h) * mr]; 142 const uint s0 = scv & 0xFFu; 143 const uint s1 = scv >> 8; 144 145 float a0 = 0.f, a1 = 0.f; 146 float4 as = (float4)(0.f); 147 for (uint u = 0; u < 4u; ++u) { 148 const uint gg = 4u*h + u; 149 const uint qsv = (uint)qsu[qsb + gg * mr]; 150 const float4 yv = vload4(grp + gg, y); 151 as += yv; 152 a0 += dot(yv, q2_k_vals( qsv & 0xFFu)); 153 a1 += dot(yv, q2_k_vals((qsv >> 8) & 0xFFu)); 154 } 155 const float asum = as.s0 + as.s1 + as.s2 + as.s3; 156 ad0 += (float)(s0 & 0xFu) * a0; am0 += (float)(s0 >> 4) * asum; 157 ad1 += (float)(s1 & 0xFu) * a1; am1 += (float)(s1 >> 4) * asum; 158 } 159 } 160 sumf0 += (float)dm.s0 * ad0 - (float)dm.s1 * am0; 161 sumf1 += (float)dm.s2 * ad1 - (float)dm.s3 * am1; 162 } 163 } 164 165 #if Q2_K_MV_NSG > 1 166 __local float2 part[Q2_K_MV_NSG][64]; 167 part[sgi][lid] = (float2)(sumf0, sumf1); 168 barrier(CLK_LOCAL_MEM_FENCE); 169 if (sgi != 0) { 170 return; 171 } 172 for (uint s = 1; s < Q2_K_MV_NSG; ++s) { 173 const float2 p = part[s][lid]; 174 sumf0 += p.s0; 175 sumf1 += p.s1; 176 } 177 #endif 178 179 if (j < mr) { 180 vstore2((float2)(sumf0, sumf1), 0, dst + (ulong)col * (uint)ne0 + row); 181 } 182 183 #else 184 float sumf0 = 0.f; 185 186 if (j < mr) { 187 for (uint ib = sgi; ib < nsb; ib += Q2_K_MV_NSG) { 188 const half2 dm = vload2(row + ib * m, src0_dm); 189 190 float ad = 0.f, am = 0.f; 191 for (uint sb = 0; sb < 8u; ++sb) { 192 const uint grp = ib * 64u + sb * 8u; 193 const uint qsb = row + grp * m; 194 195 for (uint h = 0; h < 2u; ++h) { 196 const uint sc = (uint)src0_sc[row + (2u * (ib * 8u + sb) + h) * m]; 197 198 float a = 0.f; 199 float4 as = (float4)(0.f); 200 for (uint u = 0; u < 4u; ++u) { 201 const uint gg = 4u*h + u; 202 const float4 yv = vload4(grp + gg, y); 203 as += yv; 204 const uint pkv = (uint)src0_qs[qsb + gg * m]; 205 a += dot(yv, q2_k_vals(pkv)); 206 } 207 const float asum = as.s0 + as.s1 + as.s2 + as.s3; 208 ad += (float)(sc & 0xFu) * a; am += (float)(sc >> 4) * asum; 209 } 210 } 211 sumf0 += (float)dm.s0 * ad - (float)dm.s1 * am; 212 } 213 } 214 215 #if Q2_K_MV_NSG > 1 216 __local float part[Q2_K_MV_NSG][64]; 217 part[sgi][lid] = sumf0; 218 barrier(CLK_LOCAL_MEM_FENCE); 219 if (sgi != 0) { 220 return; 221 } 222 for (uint s = 1; s < Q2_K_MV_NSG; ++s) { 223 sumf0 += part[s][lid]; 224 } 225 #endif 226 227 if (j < mr) { 228 dst[(ulong)col * (uint)ne0 + row] = sumf0; 229 } 230 #endif 231 }