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_q6_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 | |
| 10 | // 4 nibbles in the low 16 bits of `u` -> 4 bytes (value 0..15, in bits 0-3). |
| 11 | #define EXP4(u) ( ((uint)((u) & 0x000Fu)) | \ |
| 12 | (((uint)((u) & 0x00F0u)) << 4) | \ |
| 13 | (((uint)((u) & 0x0F00u)) << 8) | \ |
| 14 | (((uint)((u) & 0xF000u)) << 12) ) |
| 15 | |
| 16 | // 4 2-bit highs in byte `b` -> 4 bytes, value 0..3 in bits 4-5 (pre-multiplied |
| 17 | // by 16 so it ORs with the EXP4 nibble to form q6 in 0..63). |
| 18 | #define EXP2(b) ( (((uint)((b) & 0x03u)) << 4) | \ |
| 19 | (((uint)((b) & 0x0Cu)) << 10) | \ |
| 20 | (((uint)((b) & 0x30u)) << 16) | \ |
| 21 | (((uint)((b) & 0xC0u)) << 22) ) |
| 22 | |
| 23 | // q6 (0..63, bits 0-5 of each byte) -> (q6-32) as a signed int8 per byte. |
| 24 | inline uint SIGN6(uint q6p) { |
| 25 | uint x = q6p ^ 0x20202020u; |
| 26 | uint s = x & 0x20202020u; |
| 27 | return x | (s << 1) | (s << 2); |
| 28 | } |
| 29 | |
| 30 | // 16-K dp4a dot: 4 packed weight uints against 4 packed int8 activation uints. |
| 31 | inline int dot4_q8a(uint w0, uint w1, uint w2, uint w3, |
| 32 | uint a0, uint a1, uint a2, uint a3) { |
| 33 | int r = 0; |
| 34 | r = dot_acc_sat_4x8packed_ss_int(w0, a0, r); |
| 35 | r = dot_acc_sat_4x8packed_ss_int(w1, a1, r); |
| 36 | r = dot_acc_sat_4x8packed_ss_int(w2, a2, r); |
| 37 | r = dot_acc_sat_4x8packed_ss_int(w3, a3, r); |
| 38 | return r; |
| 39 | } |
| 40 | |
| 41 | __attribute__((qcom_wave_pair_mode(1))) |
| 42 | kernel void kernel_gemm_noshuffle_q6_k_q8_1_dp4a( |
| 43 | __global const ushort * src0_ql, // q6_K low nibbles (noshuffle) |
| 44 | __global const uchar * src0_qh, // q6_K high 2-bit (uchar, 4 highs/elem) |
| 45 | __global const ushort * src0_s, // int8 scale codes (2 chars/ushort, per 16) |
| 46 | __global const half * src0_d, // per-superblock scale |
| 47 | __global const uint * src1_qa, // q8_1 activations int8 (as uint, 4/elem) [N, K] |
| 48 | __global const half * src1_da, // q8_1 per-block scale [N, K/32] |
| 49 | __global float * dst, |
| 50 | ulong offsetd, |
| 51 | int m, // output features (rows) |
| 52 | int n_no_padding, // tokens (cols) |
| 53 | int k // K (== ne00) |
| 54 | ) { |
| 55 | dst = (global float *)((global char *)dst + offsetd); |
| 56 | |
| 57 | const uint lid = get_local_id(0); // 0..63 -> row within the M-tile |
| 58 | const uint block_id_m = get_global_id(1); |
| 59 | const uint block_id_n = get_global_id(2); |
| 60 | |
| 61 | const uint row = block_id_m * 64 + lid; |
| 62 | const uint col_base = block_id_n * TILESIZE_N; |
| 63 | const bool row_valid = row < (uint)m; |
| 64 | const uint rrow = row_valid ? row : 0; // clamp OOB rows; their writes are masked |
| 65 | |
| 66 | const uint k_u = (uint)k >> 2; // K in uint (int8x4) units |
| 67 | const uint k_b = (uint)k >> 5; // blocks-of-32 along K |
| 68 | |
| 69 | __local uint sh_qa[TILESIZE_N][8]; |
| 70 | __local half sh_d[TILESIZE_N]; |
| 71 | |
| 72 | #define NGROUPS (TILESIZE_N / 4) |
| 73 | float4 acc[NGROUPS]; |
| 74 | #pragma unroll |
| 75 | for (int g = 0; g < NGROUPS; ++g) acc[g] = (float4)(0.0f); |
| 76 | |
| 77 | for (uint step = 0; step < (uint)k; step += 32) { |
| 78 | const uint sub = step >> 5; // 32-block index along K |
| 79 | const uint sb_idx = step / QK_K; // superblock index |
| 80 | |
| 81 | // q6_K superblock scale + the two int8 sub-scales spanning this 32-block |
| 82 | const float dd = (float)src0_d[rrow + sb_idx * m]; |
| 83 | const char2 sc = as_char2(src0_s[rrow + sub * m]); |
| 84 | const float scale0 = dd * (float)sc.s0; // K step..step+15 |
| 85 | const float scale1 = dd * (float)sc.s1; // K step+16..step+31 |
| 86 | |
| 87 | // repack this row's 32 weights into 8 dp4a uints (4 K each). ql ushort + |
| 88 | // qh uchar are co-located at src0_*[row + (step/4 + u)*m]. |
| 89 | const uint wbase = rrow + (step >> 2) * (uint)m; |
| 90 | uint qw[8]; |
| 91 | #pragma unroll |
| 92 | for (int u = 0; u < 8; ++u) { |
| 93 | const uint o = wbase + (uint)u * (uint)m; |
| 94 | qw[u] = SIGN6(EXP4((uint)src0_ql[o]) | EXP2((uint)src0_qh[o])); |
| 95 | } |
| 96 | |
| 97 | // cooperatively stage the 32-token x 32-K int8 activations + scale |
| 98 | for (uint idx = lid; idx < TILESIZE_N * 8; idx += 64) { |
| 99 | const uint t = idx >> 3; |
| 100 | const uint u = idx & 7; |
| 101 | const uint c = col_base + t; |
| 102 | sh_qa[t][u] = (c < (uint)n_no_padding) ? src1_qa[c * k_u + (step >> 2) + u] : 0u; |
| 103 | } |
| 104 | if (lid < TILESIZE_N) { |
| 105 | const uint c = col_base + lid; |
| 106 | sh_d[lid] = (c < (uint)n_no_padding) ? src1_da[c * k_b + sub] : (half)0; |
| 107 | } |
| 108 | barrier(CLK_LOCAL_MEM_FENCE); |
| 109 | |
| 110 | #pragma unroll |
| 111 | for (int g = 0; g < NGROUPS; ++g) { |
| 112 | const int b = g * 4; |
| 113 | float4 rf; |
| 114 | #define DOT_TOK(j) { \ |
| 115 | __local const uint * a = sh_qa[b + (j)]; \ |
| 116 | const int raw1 = dot4_q8a(qw[0], qw[1], qw[2], qw[3], a[0], a[1], a[2], a[3]); \ |
| 117 | const int raw2 = dot4_q8a(qw[4], qw[5], qw[6], qw[7], a[4], a[5], a[6], a[7]); \ |
| 118 | rf.s##j = scale0 * (float)raw1 + scale1 * (float)raw2; \ |
| 119 | } |
| 120 | DOT_TOK(0); DOT_TOK(1); DOT_TOK(2); DOT_TOK(3); |
| 121 | #undef DOT_TOK |
| 122 | const float4 ad = (float4)((float)sh_d[b+0], (float)sh_d[b+1], (float)sh_d[b+2], (float)sh_d[b+3]); |
| 123 | acc[g] += ad * rf; |
| 124 | } |
| 125 | barrier(CLK_LOCAL_MEM_FENCE); |
| 126 | } |
| 127 | |
| 128 | if (!row_valid) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | // dst is [token, feature] row-major (stride m): dst[col*m + row]. |
| 133 | #pragma unroll |
| 134 | for (int g = 0; g < NGROUPS; ++g) { |
| 135 | const uint b = (uint)(g * 4); |
| 136 | const float4 a = acc[g]; |
| 137 | const uint c0 = col_base + b; |
| 138 | if (c0 + 0 < (uint)n_no_padding) dst[(c0 + 0) * (uint)m + row] = a.s0; |
| 139 | if (c0 + 1 < (uint)n_no_padding) dst[(c0 + 1) * (uint)m + row] = a.s1; |
| 140 | if (c0 + 2 < (uint)n_no_padding) dst[(c0 + 2) * (uint)m + row] = a.s2; |
| 141 | if (c0 + 3 < (uint)n_no_padding) dst[(c0 + 3) * (uint)m + row] = a.s3; |
| 142 | } |
| 143 | #undef NGROUPS |
| 144 | } |
| 145 | |
| 146 | // Stage each 32-K activation block as two uint4 values. |
| 147 | __attribute__((qcom_wave_pair_mode(1))) |
| 148 | kernel void kernel_gemm_noshuffle_q6_k_q8_1_dp4a_act_vload4( |
| 149 | __global const ushort * src0_ql, |
| 150 | __global const uchar * src0_qh, |
| 151 | __global const ushort * src0_s, |
| 152 | __global const half * src0_d, |
| 153 | __global const uint * src1_qa, |
| 154 | __global const half * src1_da, |
| 155 | __global float * dst, |
| 156 | ulong offsetd, |
| 157 | int m, |
| 158 | int n_no_padding, |
| 159 | int k |
| 160 | ) { |
| 161 | dst = (global float *)((global char *)dst + offsetd); |
| 162 | |
| 163 | const uint lid = get_local_id(0); |
| 164 | const uint block_id_m = get_global_id(1); |
| 165 | const uint block_id_n = get_global_id(2); |
| 166 | |
| 167 | const uint row = block_id_m * 64 + lid; |
| 168 | const uint col_base = block_id_n * TILESIZE_N; |
| 169 | const bool row_valid = row < (uint)m; |
| 170 | const uint rrow = row_valid ? row : 0; |
| 171 | |
| 172 | const uint k_u = (uint)k >> 2; |
| 173 | const uint k_b = (uint)k >> 5; |
| 174 | |
| 175 | __local uint4 sh_qa4[TILESIZE_N][2]; |
| 176 | __local half sh_d[TILESIZE_N]; |
| 177 | |
| 178 | #define NGROUPS (TILESIZE_N / 4) |
| 179 | float4 acc[NGROUPS]; |
| 180 | #pragma unroll |
| 181 | for (int g = 0; g < NGROUPS; ++g) { |
| 182 | acc[g] = (float4)(0.0f); |
| 183 | } |
| 184 | |
| 185 | for (uint step = 0; step < (uint)k; step += 32) { |
| 186 | const uint sub = step >> 5; |
| 187 | const uint sb_idx = step / QK_K; |
| 188 | |
| 189 | const float dd = (float)src0_d[rrow + sb_idx * m]; |
| 190 | const char2 sc = as_char2(src0_s[rrow + sub * m]); |
| 191 | const float scale0 = dd * (float)sc.s0; |
| 192 | const float scale1 = dd * (float)sc.s1; |
| 193 | |
| 194 | const uint wbase = rrow + (step >> 2) * (uint)m; |
| 195 | uint qw[8]; |
| 196 | #pragma unroll |
| 197 | for (int u = 0; u < 8; ++u) { |
| 198 | const uint o = wbase + (uint)u * (uint)m; |
| 199 | qw[u] = SIGN6(EXP4((uint)src0_ql[o]) | EXP2((uint)src0_qh[o])); |
| 200 | } |
| 201 | |
| 202 | for (uint idx = lid; idx < TILESIZE_N * 2; idx += 64) { |
| 203 | const uint t = idx >> 1; |
| 204 | const uint v = idx & 1; |
| 205 | const uint c = col_base + t; |
| 206 | sh_qa4[t][v] = (c < (uint)n_no_padding) |
| 207 | ? vload4(0, src1_qa + c * k_u + (step >> 2) + (v << 2)) |
| 208 | : (uint4)(0u); |
| 209 | } |
| 210 | if (lid < TILESIZE_N) { |
| 211 | const uint c = col_base + lid; |
| 212 | sh_d[lid] = (c < (uint)n_no_padding) ? src1_da[c * k_b + sub] : (half)0; |
| 213 | } |
| 214 | barrier(CLK_LOCAL_MEM_FENCE); |
| 215 | |
| 216 | #pragma unroll |
| 217 | for (int g = 0; g < NGROUPS; ++g) { |
| 218 | const int b = g * 4; |
| 219 | float4 rf; |
| 220 | #define DOT_TOK(j) { \ |
| 221 | const uint4 a0 = sh_qa4[b + (j)][0]; \ |
| 222 | const uint4 a1 = sh_qa4[b + (j)][1]; \ |
| 223 | const int raw1 = dot4_q8a(qw[0], qw[1], qw[2], qw[3], a0.x, a0.y, a0.z, a0.w); \ |
| 224 | const int raw2 = dot4_q8a(qw[4], qw[5], qw[6], qw[7], a1.x, a1.y, a1.z, a1.w); \ |
| 225 | rf.s##j = scale0 * (float)raw1 + scale1 * (float)raw2; \ |
| 226 | } |
| 227 | DOT_TOK(0); DOT_TOK(1); DOT_TOK(2); DOT_TOK(3); |
| 228 | #undef DOT_TOK |
| 229 | const float4 ad = (float4)((float)sh_d[b+0], (float)sh_d[b+1], (float)sh_d[b+2], (float)sh_d[b+3]); |
| 230 | acc[g] += ad * rf; |
| 231 | } |
| 232 | barrier(CLK_LOCAL_MEM_FENCE); |
| 233 | } |
| 234 | |
| 235 | if (!row_valid) { |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | #pragma unroll |
| 240 | for (int g = 0; g < NGROUPS; ++g) { |
| 241 | const uint b = (uint)(g * 4); |
| 242 | const float4 a = acc[g]; |
| 243 | const uint c0 = col_base + b; |
| 244 | if (c0 + 0 < (uint)n_no_padding) { |
| 245 | dst[(c0 + 0) * (uint)m + row] = a.s0; |
| 246 | } |
| 247 | if (c0 + 1 < (uint)n_no_padding) { |
| 248 | dst[(c0 + 1) * (uint)m + row] = a.s1; |
| 249 | } |
| 250 | if (c0 + 2 < (uint)n_no_padding) { |
| 251 | dst[(c0 + 2) * (uint)m + row] = a.s2; |
| 252 | } |
| 253 | if (c0 + 3 < (uint)n_no_padding) { |
| 254 | dst[(c0 + 3) * (uint)m + row] = a.s3; |
| 255 | } |
| 256 | } |
| 257 | #undef NGROUPS |
| 258 | } |