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 / gemm_noshuffle_q5_k_q8_1_dp4a.cl
| 1 | #pragma OPENCL EXTENSION cl_khr_fp16 : enable |
| 2 | #pragma OPENCL EXTENSION cl_khr_subgroups : enable |
| 3 | #ifdef cl_khr_integer_dot_product |
| 4 | #pragma OPENCL EXTENSION cl_khr_integer_dot_product : enable |
| 5 | #endif |
| 6 | |
| 7 | #define TILESIZE_N 32 |
| 8 | #define QK_K 256 |
| 9 | #define K_SCALE_SIZE 12 |
| 10 | |
| 11 | inline void get_scale_min_k4( |
| 12 | int j, |
| 13 | global const uchar * q, |
| 14 | uchar * d, |
| 15 | uchar * m, |
| 16 | uchar mask_d6, |
| 17 | uchar mask_d4, |
| 18 | uchar mask_hi2 |
| 19 | ) { |
| 20 | if (j < 4) { |
| 21 | *d = q[j] & mask_d6; |
| 22 | *m = q[j+4] & mask_d6; |
| 23 | } else { |
| 24 | *d = (q[j+4] & mask_d4) | ((q[j-4] & mask_hi2) >> 2); |
| 25 | *m = ((q[j+4] >> 4) & mask_d4) | ((q[j] & mask_hi2) >> 2); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | // 4 nibbles in the low 16 bits of `u` -> 4 bytes (value 0..15, bits 0-3). |
| 30 | #define EXP4(u) ( ((uint)((u) & 0x000Fu)) | \ |
| 31 | (((uint)((u) & 0x00F0u)) << 4) | \ |
| 32 | (((uint)((u) & 0x0F00u)) << 8) | \ |
| 33 | (((uint)((u) & 0xF000u)) << 12) ) |
| 34 | |
| 35 | // 4 high bits (one per element, in bits 0-3 of h) -> bit 4 of each of 4 bytes, |
| 36 | // so OR with EXP4 forms the 5-bit q5_K code 0..31. |
| 37 | #define EXP1(h) ( (((uint)((h) & 0x1u)) << 4) | \ |
| 38 | (((uint)((h) & 0x2u)) << 11) | \ |
| 39 | (((uint)((h) & 0x4u)) << 18) | \ |
| 40 | (((uint)((h) & 0x8u)) << 25) ) |
| 41 | |
| 42 | inline int dot8_q8a(uint8 qw, __local const uint * a) { |
| 43 | int r = 0; |
| 44 | r = dot_acc_sat_4x8packed_ss_int(qw.s0, a[0], r); |
| 45 | r = dot_acc_sat_4x8packed_ss_int(qw.s1, a[1], r); |
| 46 | r = dot_acc_sat_4x8packed_ss_int(qw.s2, a[2], r); |
| 47 | r = dot_acc_sat_4x8packed_ss_int(qw.s3, a[3], r); |
| 48 | r = dot_acc_sat_4x8packed_ss_int(qw.s4, a[4], r); |
| 49 | r = dot_acc_sat_4x8packed_ss_int(qw.s5, a[5], r); |
| 50 | r = dot_acc_sat_4x8packed_ss_int(qw.s6, a[6], r); |
| 51 | r = dot_acc_sat_4x8packed_ss_int(qw.s7, a[7], r); |
| 52 | return r; |
| 53 | } |
| 54 | |
| 55 | __attribute__((qcom_wave_pair_mode(1))) |
| 56 | kernel void kernel_gemm_noshuffle_q5_k_q8_1_dp4a( |
| 57 | __global const ushort * src0_q, // q5_K low nibbles (transposed, ushort = 4 nibbles) |
| 58 | __global const uchar * src0_qh, // q5_K high bits (transposed, uchar = 8 elems/byte) |
| 59 | __global const uchar * src0_s, // 6-bit scale/min codes [row][superblock][12] |
| 60 | __global const half * src0_d, // per-superblock scale (transposed) |
| 61 | __global const half * src0_dm, // per-superblock min (transposed) |
| 62 | __global const uint * src1_qa, // q8_1 activations int8 (as uint, 4/elem) [N, K] |
| 63 | __global const half * src1_da, // q8_1 per-block scale [N, K/32] |
| 64 | __global const half * src1_sa, // q8_1 per-block sum*d [N, K/32] |
| 65 | __global float * dst, |
| 66 | ulong offsetd, |
| 67 | int m, // output features (rows) |
| 68 | int n_no_padding, // tokens (cols) |
| 69 | int k, // K (== ne00) |
| 70 | uchar mask_d6, |
| 71 | uchar mask_d4, |
| 72 | uchar mask_hi2 |
| 73 | ) { |
| 74 | dst = (global float *)((global char *)dst + offsetd); |
| 75 | |
| 76 | const uint lid = get_local_id(0); // 0..63 -> row within the M-tile |
| 77 | const uint block_id_m = get_global_id(1); |
| 78 | const uint block_id_n = get_global_id(2); |
| 79 | |
| 80 | const uint row = block_id_m * 64 + lid; |
| 81 | const uint col_base = block_id_n * TILESIZE_N; |
| 82 | const bool row_valid = row < (uint)m; |
| 83 | const uint rrow = row_valid ? row : 0; |
| 84 | |
| 85 | const uint num_superblocks = (uint)k / QK_K; |
| 86 | const uint k_u = (uint)k >> 2; |
| 87 | const uint k_b = (uint)k >> 5; |
| 88 | |
| 89 | __local uint sh_qa[TILESIZE_N][8]; |
| 90 | __local half sh_d[TILESIZE_N]; |
| 91 | __local half sh_s[TILESIZE_N]; |
| 92 | |
| 93 | #define NGROUPS (TILESIZE_N / 4) |
| 94 | float4 acc[NGROUPS]; |
| 95 | #pragma unroll |
| 96 | for (int g = 0; g < NGROUPS; ++g) acc[g] = (float4)(0.0f); |
| 97 | |
| 98 | for (uint step = 0; step < (uint)k; step += 32) { |
| 99 | const uint sub = step >> 5; |
| 100 | const uint sb_idx = step / QK_K; |
| 101 | const uint sub_idx = sub & 7; |
| 102 | |
| 103 | const float dd = (float)src0_d [rrow + sb_idx * m]; |
| 104 | const float dmm = (float)src0_dm[rrow + sb_idx * m]; |
| 105 | global const uchar * sc = src0_s + rrow * num_superblocks * K_SCALE_SIZE + sb_idx * K_SCALE_SIZE; |
| 106 | uchar sv, mn; |
| 107 | get_scale_min_k4(sub_idx, sc, &sv, &mn, mask_d6, mask_d4, mask_hi2); |
| 108 | const float scale = dd * (float)sv; |
| 109 | const float minv = dmm * (float)mn; |
| 110 | |
| 111 | // repack this row's 32 weights (nibble | high-bit) into 8 dp4a uints. |
| 112 | // ushort u -> 4 elements at K = step + u*4; its 4 high bits are nibble |
| 113 | // (u&1) of qh byte (step/8 + u/2). |
| 114 | const uint wbase = rrow + (step >> 2) * (uint)m; |
| 115 | const uint qhbase = rrow + (step >> 3) * (uint)m; |
| 116 | uint8 qw; |
| 117 | #define QWU(u) ( EXP4((uint)src0_q[wbase + (uint)(u) * m]) \ |
| 118 | | EXP1( (uint)((src0_qh[qhbase + (uint)((u) >> 1) * m] >> (((u) & 1) * 4)) & 0x0Fu) ) ) |
| 119 | qw.s0 = QWU(0); qw.s1 = QWU(1); qw.s2 = QWU(2); qw.s3 = QWU(3); |
| 120 | qw.s4 = QWU(4); qw.s5 = QWU(5); qw.s6 = QWU(6); qw.s7 = QWU(7); |
| 121 | #undef QWU |
| 122 | |
| 123 | for (uint idx = lid; idx < TILESIZE_N * 8; idx += 64) { |
| 124 | const uint t = idx >> 3; |
| 125 | const uint u = idx & 7; |
| 126 | const uint c = col_base + t; |
| 127 | sh_qa[t][u] = (c < (uint)n_no_padding) ? src1_qa[c * k_u + (step >> 2) + u] : 0u; |
| 128 | } |
| 129 | if (lid < TILESIZE_N) { |
| 130 | const uint c = col_base + lid; |
| 131 | sh_d[lid] = (c < (uint)n_no_padding) ? src1_da[c * k_b + sub] : (half)0; |
| 132 | sh_s[lid] = (c < (uint)n_no_padding) ? src1_sa[c * k_b + sub] : (half)0; |
| 133 | } |
| 134 | barrier(CLK_LOCAL_MEM_FENCE); |
| 135 | |
| 136 | #define LD4(arr, b) ((float4)((float)arr[(b)+0], (float)arr[(b)+1], (float)arr[(b)+2], (float)arr[(b)+3])) |
| 137 | #pragma unroll |
| 138 | for (int g = 0; g < NGROUPS; ++g) { |
| 139 | const int b = g * 4; |
| 140 | float4 rf; |
| 141 | rf.s0 = (float)dot8_q8a(qw, sh_qa[b+0]); rf.s1 = (float)dot8_q8a(qw, sh_qa[b+1]); |
| 142 | rf.s2 = (float)dot8_q8a(qw, sh_qa[b+2]); rf.s3 = (float)dot8_q8a(qw, sh_qa[b+3]); |
| 143 | acc[g] += scale * LD4(sh_d, b) * rf - minv * LD4(sh_s, b); |
| 144 | } |
| 145 | #undef LD4 |
| 146 | barrier(CLK_LOCAL_MEM_FENCE); |
| 147 | } |
| 148 | |
| 149 | if (!row_valid) { |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | #pragma unroll |
| 154 | for (int g = 0; g < NGROUPS; ++g) { |
| 155 | const uint b = (uint)(g * 4); |
| 156 | const float4 a = acc[g]; |
| 157 | const uint c0 = col_base + b; |
| 158 | if (c0 + 0 < (uint)n_no_padding) dst[(c0 + 0) * (uint)m + row] = a.s0; |
| 159 | if (c0 + 1 < (uint)n_no_padding) dst[(c0 + 1) * (uint)m + row] = a.s1; |
| 160 | if (c0 + 2 < (uint)n_no_padding) dst[(c0 + 2) * (uint)m + row] = a.s2; |
| 161 | if (c0 + 3 < (uint)n_no_padding) dst[(c0 + 3) * (uint)m + row] = a.s3; |
| 162 | } |
| 163 | #undef NGROUPS |
| 164 | } |
| 165 | |
| 166 | // Stage each 32-K activation block as two uint4 values. |
| 167 | inline int dot8_q8a_v(uint8 qw, uint4 a0, uint4 a1) { |
| 168 | int r = 0; |
| 169 | r = dot_acc_sat_4x8packed_ss_int(qw.s0, a0.x, r); |
| 170 | r = dot_acc_sat_4x8packed_ss_int(qw.s1, a0.y, r); |
| 171 | r = dot_acc_sat_4x8packed_ss_int(qw.s2, a0.z, r); |
| 172 | r = dot_acc_sat_4x8packed_ss_int(qw.s3, a0.w, r); |
| 173 | r = dot_acc_sat_4x8packed_ss_int(qw.s4, a1.x, r); |
| 174 | r = dot_acc_sat_4x8packed_ss_int(qw.s5, a1.y, r); |
| 175 | r = dot_acc_sat_4x8packed_ss_int(qw.s6, a1.z, r); |
| 176 | r = dot_acc_sat_4x8packed_ss_int(qw.s7, a1.w, r); |
| 177 | return r; |
| 178 | } |
| 179 | |
| 180 | __attribute__((qcom_wave_pair_mode(1))) |
| 181 | kernel void kernel_gemm_noshuffle_q5_k_q8_1_dp4a_act_vload4( |
| 182 | __global const ushort * src0_q, |
| 183 | __global const uchar * src0_qh, |
| 184 | __global const uchar * src0_s, |
| 185 | __global const half * src0_d, |
| 186 | __global const half * src0_dm, |
| 187 | __global const uint * src1_qa, |
| 188 | __global const half * src1_da, |
| 189 | __global const half * src1_sa, |
| 190 | __global float * dst, |
| 191 | ulong offsetd, |
| 192 | int m, |
| 193 | int n_no_padding, |
| 194 | int k, |
| 195 | uchar mask_d6, |
| 196 | uchar mask_d4, |
| 197 | uchar mask_hi2 |
| 198 | ) { |
| 199 | dst = (global float *)((global char *)dst + offsetd); |
| 200 | |
| 201 | const uint lid = get_local_id(0); |
| 202 | const uint block_id_m = get_global_id(1); |
| 203 | const uint block_id_n = get_global_id(2); |
| 204 | |
| 205 | const uint row = block_id_m * 64 + lid; |
| 206 | const uint col_base = block_id_n * TILESIZE_N; |
| 207 | const bool row_valid = row < (uint)m; |
| 208 | const uint rrow = row_valid ? row : 0; |
| 209 | |
| 210 | const uint num_superblocks = (uint)k / QK_K; |
| 211 | const uint k_u = (uint)k >> 2; |
| 212 | const uint k_b = (uint)k >> 5; |
| 213 | |
| 214 | __local uint4 sh_qa4[TILESIZE_N][2]; |
| 215 | __local half sh_d[TILESIZE_N]; |
| 216 | __local half sh_s[TILESIZE_N]; |
| 217 | |
| 218 | #define NGROUPS (TILESIZE_N / 4) |
| 219 | float4 acc[NGROUPS]; |
| 220 | #pragma unroll |
| 221 | for (int g = 0; g < NGROUPS; ++g) { |
| 222 | acc[g] = (float4)(0.0f); |
| 223 | } |
| 224 | |
| 225 | for (uint step = 0; step < (uint)k; step += 32) { |
| 226 | const uint sub = step >> 5; |
| 227 | const uint sb_idx = step / QK_K; |
| 228 | const uint sub_idx = sub & 7; |
| 229 | |
| 230 | const float dd = (float)src0_d [rrow + sb_idx * m]; |
| 231 | const float dmm = (float)src0_dm[rrow + sb_idx * m]; |
| 232 | global const uchar * sc = src0_s + rrow * num_superblocks * K_SCALE_SIZE + sb_idx * K_SCALE_SIZE; |
| 233 | uchar sv, mn; |
| 234 | get_scale_min_k4(sub_idx, sc, &sv, &mn, mask_d6, mask_d4, mask_hi2); |
| 235 | const float scale = dd * (float)sv; |
| 236 | const float minv = dmm * (float)mn; |
| 237 | |
| 238 | const uint wbase = rrow + (step >> 2) * (uint)m; |
| 239 | const uint qhbase = rrow + (step >> 3) * (uint)m; |
| 240 | uint8 qw; |
| 241 | #define QWU(u) ( EXP4((uint)src0_q[wbase + (uint)(u) * m]) \ |
| 242 | | EXP1( (uint)((src0_qh[qhbase + (uint)((u) >> 1) * m] >> (((u) & 1) * 4)) & 0x0Fu) ) ) |
| 243 | qw.s0 = QWU(0); qw.s1 = QWU(1); qw.s2 = QWU(2); qw.s3 = QWU(3); |
| 244 | qw.s4 = QWU(4); qw.s5 = QWU(5); qw.s6 = QWU(6); qw.s7 = QWU(7); |
| 245 | #undef QWU |
| 246 | |
| 247 | for (uint idx = lid; idx < TILESIZE_N * 2; idx += 64) { |
| 248 | const uint t = idx >> 1; |
| 249 | const uint v = idx & 1; |
| 250 | const uint c = col_base + t; |
| 251 | sh_qa4[t][v] = (c < (uint)n_no_padding) |
| 252 | ? vload4(0, src1_qa + c * k_u + (step >> 2) + (v << 2)) |
| 253 | : (uint4)(0u); |
| 254 | } |
| 255 | if (lid < TILESIZE_N) { |
| 256 | const uint c = col_base + lid; |
| 257 | sh_d[lid] = (c < (uint)n_no_padding) ? src1_da[c * k_b + sub] : (half)0; |
| 258 | sh_s[lid] = (c < (uint)n_no_padding) ? src1_sa[c * k_b + sub] : (half)0; |
| 259 | } |
| 260 | barrier(CLK_LOCAL_MEM_FENCE); |
| 261 | |
| 262 | #define LD4(arr, b) ((float4)((float)arr[(b)+0], (float)arr[(b)+1], (float)arr[(b)+2], (float)arr[(b)+3])) |
| 263 | #pragma unroll |
| 264 | for (int g = 0; g < NGROUPS; ++g) { |
| 265 | const int b = g * 4; |
| 266 | float4 rf; |
| 267 | #define DOTV(T) dot8_q8a_v(qw, sh_qa4[T][0], sh_qa4[T][1]) |
| 268 | rf.s0 = (float)DOTV(b+0); |
| 269 | rf.s1 = (float)DOTV(b+1); |
| 270 | rf.s2 = (float)DOTV(b+2); |
| 271 | rf.s3 = (float)DOTV(b+3); |
| 272 | #undef DOTV |
| 273 | acc[g] += scale * LD4(sh_d, b) * rf - minv * LD4(sh_s, b); |
| 274 | } |
| 275 | #undef LD4 |
| 276 | barrier(CLK_LOCAL_MEM_FENCE); |
| 277 | } |
| 278 | |
| 279 | if (!row_valid) { |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | #pragma unroll |
| 284 | for (int g = 0; g < NGROUPS; ++g) { |
| 285 | const uint b = (uint)(g * 4); |
| 286 | const float4 a = acc[g]; |
| 287 | const uint c0 = col_base + b; |
| 288 | if (c0 + 0 < (uint)n_no_padding) { |
| 289 | dst[(c0 + 0) * (uint)m + row] = a.s0; |
| 290 | } |
| 291 | if (c0 + 1 < (uint)n_no_padding) { |
| 292 | dst[(c0 + 1) * (uint)m + row] = a.s1; |
| 293 | } |
| 294 | if (c0 + 2 < (uint)n_no_padding) { |
| 295 | dst[(c0 + 2) * (uint)m + row] = a.s2; |
| 296 | } |
| 297 | if (c0 + 3 < (uint)n_no_padding) { |
| 298 | dst[(c0 + 3) * (uint)m + row] = a.s3; |
| 299 | } |
| 300 | } |
| 301 | #undef NGROUPS |
| 302 | } |