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 / mul_mv_q6_k_f32_flat.cl
| 1 | #pragma OPENCL EXTENSION cl_khr_fp16 : enable |
| 2 | |
| 3 | #ifdef cl_intel_subgroups |
| 4 | #pragma OPENCL EXTENSION cl_intel_subgroups : enable |
| 5 | #else |
| 6 | #pragma OPENCL EXTENSION cl_khr_subgroups : enable |
| 7 | #endif |
| 8 | |
| 9 | #ifdef cl_intel_required_subgroup_size |
| 10 | #pragma OPENCL EXTENSION cl_intel_required_subgroup_size : enable |
| 11 | #define INTEL_GPU 1 |
| 12 | #define REQD_SUBGROUP_SIZE_16 __attribute__((intel_reqd_sub_group_size(16))) |
| 13 | #define REQD_SUBGROUP_SIZE_32 __attribute__((intel_reqd_sub_group_size(32))) |
| 14 | #elif defined(cl_qcom_reqd_sub_group_size) |
| 15 | #pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable |
| 16 | #define ADRENO_GPU 1 |
| 17 | #define REQD_SUBGROUP_SIZE_64 __attribute__((qcom_reqd_sub_group_size("half"))) |
| 18 | #define REQD_SUBGROUP_SIZE_128 __attribute__((qcom_reqd_sub_group_size("full"))) |
| 19 | #endif |
| 20 | |
| 21 | //------------------------------------------------------------------------------ |
| 22 | // kernel_mul_mv_q6_K_f32_flat |
| 23 | //------------------------------------------------------------------------------ |
| 24 | #define Q6_K_MASK1 0x03 |
| 25 | #define Q6_K_MASK2 0x0C |
| 26 | #define Q6_K_MASK3 0x30 |
| 27 | #define Q6_K_MASK4 0xC0 |
| 28 | |
| 29 | #define QK_K 256 |
| 30 | |
| 31 | // ADRENO_OLD_COMPILER is defined by the host (-D) only for the Adreno E031 |
| 32 | // compilers older than E031.45, which miscompile several constructs this kernel |
| 33 | // used (confirmed on E031.38 and E031.41; E031.45 is clean). Every other |
| 34 | // compiler -- newer E031, E17, DX, Intel, and every non-Adreno device that |
| 35 | // builds this program -- takes the #else branches, which are the original |
| 36 | // source: the workarounds below cost ~13% on the q6_K flat n=1 GEMV where they |
| 37 | // are not needed. |
| 38 | inline float block_q_6_K_dot_y_flat( |
| 39 | global uchar * blk_ql, |
| 40 | global uchar * blk_qh, |
| 41 | global char * blk_scales, |
| 42 | global half * blk_d, |
| 43 | int ib, |
| 44 | int ip, |
| 45 | int is, |
| 46 | int l0, |
| 47 | #if defined(ADRENO_OLD_COMPILER) |
| 48 | int dbg, |
| 49 | #endif |
| 50 | float4 y0, |
| 51 | float4 y1, |
| 52 | float4 y2, |
| 53 | float4 y3 |
| 54 | ) { |
| 55 | int q_offset_l = 64*ip + l0; |
| 56 | int q_offset_h = 32*ip + l0; |
| 57 | |
| 58 | global uchar * q1 = blk_ql + ib*128 + q_offset_l; |
| 59 | global uchar * q2 = q1 + QK_K/8; |
| 60 | global uchar * qh = blk_qh + ib*64 + q_offset_h; |
| 61 | |
| 62 | float dall = blk_d[ib]; |
| 63 | |
| 64 | #if defined(ADRENO_OLD_COMPILER) |
| 65 | // The vectorized dequant (int4/float4 bit-ops, convert_*4, dot()) and vload4 |
| 66 | // are miscompiled here -> garbage weights. Reconstruct the 6-bit weights and |
| 67 | // take the dot product scalar. q4_K/q5_K flat already use scalar paths, which |
| 68 | // is why q6_K was the only flat GEMV that failed. |
| 69 | // Scales are SIGNED int8; read as uchar and sign-extend arithmetically so the |
| 70 | // result does not depend on whether the compiler treats `char` as signed. |
| 71 | global uchar * sc = (global uchar *)(blk_scales + ib*16 + is); |
| 72 | |
| 73 | int s0 = (int)sc[0] - 256*(sc[0] >> 7); |
| 74 | int s2 = (int)sc[2] - 256*(sc[2] >> 7); |
| 75 | int s4 = (int)sc[4] - 256*(sc[4] >> 7); |
| 76 | int s6 = (int)sc[6] - 256*(sc[6] >> 7); |
| 77 | |
| 78 | // one 6-bit weight: low/high nibble of a ql byte OR'd with a 2-bit qh plane |
| 79 | // (plane p in {0,1,2,3} selects qh bits 2p..2p+1) placed at bits 4-5, minus 32. |
| 80 | #define Q6W(qb, sh, hb, p) ((float)((((int)(qb) >> (sh)) & 15) | ((((int)(hb) >> (2*(p))) & 3) << 4)) - 32.f) |
| 81 | |
| 82 | float d0 = y0.s0*Q6W(q1[0],0,qh[0],0) + y0.s1*Q6W(q1[1],0,qh[1],0) + y0.s2*Q6W(q1[2],0,qh[2],0) + y0.s3*Q6W(q1[3],0,qh[3],0); |
| 83 | float d1 = y1.s0*Q6W(q2[0],0,qh[0],1) + y1.s1*Q6W(q2[1],0,qh[1],1) + y1.s2*Q6W(q2[2],0,qh[2],1) + y1.s3*Q6W(q2[3],0,qh[3],1); |
| 84 | float d2 = y2.s0*Q6W(q1[0],4,qh[0],2) + y2.s1*Q6W(q1[1],4,qh[1],2) + y2.s2*Q6W(q1[2],4,qh[2],2) + y2.s3*Q6W(q1[3],4,qh[3],2); |
| 85 | float d3 = y3.s0*Q6W(q2[0],4,qh[0],3) + y3.s1*Q6W(q2[1],4,qh[1],3) + y3.s2*Q6W(q2[2],4,qh[2],3) + y3.s3*Q6W(q2[3],4,qh[3],3); |
| 86 | #undef Q6W |
| 87 | |
| 88 | if (dbg) printf("HELPER dall=%f s=[%d %d %d %d] d=[%f %f %f %f] ql0=%d qh0=%d y00=%f\n", |
| 89 | dall, s0, s2, s4, s6, d0, d1, d2, d3, (int)q1[0], (int)qh[0], y0.s0); |
| 90 | |
| 91 | return dall * (d0 * s0 + d1 * s2 + d2 * s4 + d3 * s6); |
| 92 | #else |
| 93 | global char * sc = blk_scales + ib*16 + is; |
| 94 | |
| 95 | // Vectorized loads: 3 uchar4 weight loads instead of 12 scalar byte reads. |
| 96 | // q_offset_l/h are 4-aligned, so these are aligned vector loads. |
| 97 | uchar4 q1v = vload4(0, q1); |
| 98 | uchar4 q2v = vload4(0, q2); |
| 99 | uchar4 qhv = vload4(0, qh); |
| 100 | |
| 101 | int4 q1i = convert_int4(q1v); |
| 102 | int4 q2i = convert_int4(q2v); |
| 103 | int4 qhi = convert_int4(qhv); |
| 104 | |
| 105 | // Reconstruct the four 6-bit weight groups (low/high nibble of ql OR'd with the |
| 106 | // matching 2-bit plane of qh), same arithmetic as the scalar version, then dot() |
| 107 | // against the cached activation lanes. |
| 108 | float4 w0 = convert_float4((q1i & 0xF) | ((qhi & Q6_K_MASK1) << 4)) - 32.f; |
| 109 | float4 w1 = convert_float4((q2i & 0xF) | ((qhi & Q6_K_MASK2) << 2)) - 32.f; |
| 110 | float4 w2 = convert_float4((q1i >> 4) | ((qhi & Q6_K_MASK3) )) - 32.f; |
| 111 | float4 w3 = convert_float4((q2i >> 4) | ((qhi & Q6_K_MASK4) >> 2)) - 32.f; |
| 112 | |
| 113 | return dall * (dot(y0, w0) * sc[0] + dot(y1, w1) * sc[2] + |
| 114 | dot(y2, w2) * sc[4] + dot(y3, w3) * sc[6]); |
| 115 | #endif |
| 116 | } |
| 117 | |
| 118 | #undef N_DST |
| 119 | #undef N_SIMDGROUP |
| 120 | #undef N_SIMDWIDTH |
| 121 | |
| 122 | #ifdef INTEL_GPU |
| 123 | #define N_DST 4 |
| 124 | #define N_SIMDGROUP 2 |
| 125 | #define N_SIMDWIDTH 16 |
| 126 | #elif defined (ADRENO_GPU) |
| 127 | #define N_DST 16 |
| 128 | #define N_SIMDGROUP 2 |
| 129 | #define N_SIMDWIDTH 64 |
| 130 | #endif |
| 131 | |
| 132 | #define BLOCK_STRIDE (N_SIMDWIDTH/16) // number of blocks each subgroup processes |
| 133 | |
| 134 | #ifdef INTEL_GPU |
| 135 | REQD_SUBGROUP_SIZE_16 |
| 136 | #elif defined (ADRENO_GPU) |
| 137 | REQD_SUBGROUP_SIZE_64 |
| 138 | #endif |
| 139 | kernel void kernel_mul_mv_q6_K_f32_flat( |
| 140 | global uchar * src0_ql, |
| 141 | global uchar * src0_qh, |
| 142 | global char * src0_s, |
| 143 | global half * src0_d, |
| 144 | global float * src1, |
| 145 | ulong offset1, |
| 146 | global float * dst, |
| 147 | ulong offsetd, |
| 148 | int ne00, |
| 149 | int ne01, |
| 150 | int ne02, |
| 151 | int ne10, |
| 152 | int ne12, |
| 153 | int ne0, |
| 154 | int ne1, |
| 155 | int r2, |
| 156 | int r3 |
| 157 | #if defined(ADRENO_OLD_COMPILER) |
| 158 | , |
| 159 | uchar q6k_mask // runtime 0xFF; the host passes it so the compiler cannot |
| 160 | // constant-fold the printf guards below into nothing |
| 161 | #endif |
| 162 | ) { |
| 163 | src1 = (global float*)((global char*)src1 + offset1); |
| 164 | dst = (global float*)((global char*)dst + offsetd); |
| 165 | |
| 166 | int nb = ne00/QK_K; |
| 167 | |
| 168 | int r0 = get_group_id(0); |
| 169 | int r1 = get_group_id(1); |
| 170 | int im = get_group_id(2); |
| 171 | |
| 172 | int i12 = im%ne12; |
| 173 | int i13 = im/ne12; |
| 174 | |
| 175 | int first_row = (N_SIMDGROUP * r0 + get_sub_group_id()) * N_DST; |
| 176 | |
| 177 | #if defined(ADRENO_OLD_COMPILER) |
| 178 | // 64-bit `ulong` integer arithmetic is miscompiled here -> the base-pointer byte |
| 179 | // offsets came out wrong, so EVERY weight/scale read hit the wrong address. This |
| 180 | // was the primary cause of the q6_K flat failure (q5_K uses int offsets and is |
| 181 | // unaffected). Compute the block index in `int` and widen to `ulong` only inside |
| 182 | // the pointer expression: the byte offset stays 64-bit, but there is no ulong |
| 183 | // arithmetic chain to miscompile. The int index would overflow past ~2^31 blocks, |
| 184 | // which no realistic weight reaches -- but that is a narrowing, so keep it off the |
| 185 | // conformant path, which retains full ulong arithmetic. |
| 186 | int offset_src0 = first_row*nb + (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02); |
| 187 | |
| 188 | global uchar * blk_ql = (global uchar *) src0_ql + (ulong)offset_src0 * 128; |
| 189 | global uchar * blk_qh = (global uchar *) src0_qh + (ulong)offset_src0 * 64; |
| 190 | global char * blk_scales = (global char *) src0_s + (ulong)offset_src0 * 16; |
| 191 | global half * blk_d = (global half *) src0_d + offset_src0; |
| 192 | #else |
| 193 | ulong offset_src0 = first_row*nb + (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02); |
| 194 | ulong offset_src0_ql = offset_src0 * 128; |
| 195 | ulong offset_src0_qh = offset_src0 * 64; |
| 196 | ulong offset_src0_s = offset_src0 * 16; |
| 197 | ulong offset_src0_d = offset_src0; |
| 198 | |
| 199 | global uchar * blk_ql = (global uchar *) src0_ql + offset_src0_ql; |
| 200 | global uchar * blk_qh = (global uchar *) src0_qh + offset_src0_qh; |
| 201 | global char * blk_scales = (global char *) src0_s + offset_src0_s; |
| 202 | global half * blk_d = (global half *) src0_d + offset_src0_d; |
| 203 | #endif |
| 204 | global float * yy = (global float *) src1 + r1*ne10 + im*ne00*ne1; |
| 205 | |
| 206 | int tid = get_sub_group_local_id()%(N_SIMDWIDTH/BLOCK_STRIDE); // within-super-block part, 0..15 |
| 207 | int ix = get_sub_group_local_id()/(N_SIMDWIDTH/BLOCK_STRIDE); // super-block selector, 0..BLOCK_STRIDE-1 |
| 208 | int ip = tid/8; // first or second half of (super) block (0 or 1) |
| 209 | int il = tid%8; // each half has 8 parts, one per scale |
| 210 | int n = 4; // 4 scales at a time (and 4 sums) |
| 211 | int l0 = n*il; // offset into half-block, 0..28 |
| 212 | int is = 8*ip + l0/16; // 0, 1, 8, 9 |
| 213 | |
| 214 | float sumf[N_DST]; |
| 215 | for (int row = 0; row < N_DST; row++) { |
| 216 | sumf[row] = 0.f; |
| 217 | } |
| 218 | |
| 219 | for (int ib = ix; ib < nb; ib += BLOCK_STRIDE) { |
| 220 | global float * y = yy + ib * QK_K + 128*ip + l0; |
| 221 | #if defined(ADRENO_OLD_COMPILER) |
| 222 | // vload4 of f32 is miscompiled here; index the lanes scalar instead. |
| 223 | float4 y0 = (float4)(y[ 0], y[ 1], y[ 2], y[ 3]); |
| 224 | float4 y1 = (float4)(y[32], y[33], y[34], y[35]); |
| 225 | float4 y2 = (float4)(y[64], y[65], y[66], y[67]); |
| 226 | float4 y3 = (float4)(y[96], y[97], y[98], y[99]); |
| 227 | #else |
| 228 | float4 y0 = vload4(0, y + 0); |
| 229 | float4 y1 = vload4(0, y + 32); |
| 230 | float4 y2 = vload4(0, y + 64); |
| 231 | float4 y3 = vload4(0, y + 96); |
| 232 | #endif |
| 233 | |
| 234 | for (int row = 0; row < N_DST; row++) { |
| 235 | if (first_row + row < ne01) { |
| 236 | #if defined(ADRENO_OLD_COMPILER) |
| 237 | int dbg = (q6k_mask==0xFE && r0==0 && r1==0 && im==0 && row==0 && ib==0 && |
| 238 | ne00==256 && ne01==16 && get_sub_group_local_id()==0) ? 1 : 0; |
| 239 | sumf[row] += block_q_6_K_dot_y_flat( |
| 240 | blk_ql + row*nb*128, blk_qh + row*nb*64, blk_scales + row*nb*16, blk_d + row*nb, |
| 241 | ib, ip, is, l0, dbg, y0, y1, y2, y3); |
| 242 | #else |
| 243 | sumf[row] += block_q_6_K_dot_y_flat( |
| 244 | blk_ql + row*nb*128, blk_qh + row*nb*64, blk_scales + row*nb*16, blk_d + row*nb, |
| 245 | ib, ip, is, l0, y0, y1, y2, y3); |
| 246 | #endif |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | #if defined(ADRENO_OLD_COMPILER) |
| 252 | // Optimizer barrier. This compiler drops the sumf partials unless a side effect |
| 253 | // forces them to materialize. q6k_mask is a kernel arg the compiler cannot prove |
| 254 | // is never 0xFE (the host always passes 0xFF), so the printf survives compilation |
| 255 | // but never executes. FRAGILE: the exact set and placement of these guarded |
| 256 | // printfs is load-bearing on E031.41 -- removing any one re-breaks q6_K. |
| 257 | if (q6k_mask==0xFE && r0==0 && r1==0 && im==0 && ne00==256 && ne01==16 && get_sub_group_local_id()<16) { |
| 258 | printf("Q6KLANE lane=%d ip=%d il=%d is=%d l0=%d sumf0=%f\n", |
| 259 | get_sub_group_local_id(), ip, il, is, l0, sumf[0]); |
| 260 | } |
| 261 | #endif |
| 262 | for (int row = 0; row < N_DST; row++) { |
| 263 | float tot = sub_group_reduce_add(sumf[row]); |
| 264 | if (get_sub_group_local_id() == 0 && first_row + row < ne01) { |
| 265 | dst[r1*ne0 + im*ne0*ne1 + first_row + row] = tot; |
| 266 | #if defined(ADRENO_OLD_COMPILER) |
| 267 | if (q6k_mask==0xFE && r0==0 && r1==0 && im==0 && row==0 && ne00==256 && ne01==16) |
| 268 | printf("Q6KTOT tot=%f\n", tot); |
| 269 | #endif |
| 270 | } |
| 271 | } |
| 272 | } |