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_moe_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 | // Generic int8 dp4a MoE GEMM, specialized versions also exist |
| 8 | // MOE_QT: |
| 9 | // 4 (q4_K)/41(q4_1)/40(q4_0) NIBBLE image low nibbles -> EXP4 |
| 10 | // 5 (q5_K)/51(q5_1)/50(q5_0) NIBBLE+HI image nibbles + qh high-bit plane |
| 11 | // 6 (q6_K) Q6 image nibbles + qh 2-bit -> SIGN6((nibble|hi2)) |
| 12 | // 80(q8_0)/82(mxfp4) INT8 global int8 codes (mxfp4: convert applies kvalues LUT) |
| 13 | |
| 14 | #define TILESIZE_M 64 |
| 15 | #define TILESIZE_N 32 |
| 16 | #define QK_K 256 |
| 17 | |
| 18 | #ifndef MOE_QT |
| 19 | #define MOE_QT 4 |
| 20 | #endif |
| 21 | |
| 22 | // 4 nibbles in low 16 bits of u -> 4 bytes (value 0..15) |
| 23 | #define EXP4(u) ( ((uint)((u) & 0x000Fu)) | \ |
| 24 | (((uint)((u) & 0x00F0u)) << 4) | \ |
| 25 | (((uint)((u) & 0x0F00u)) << 8) | \ |
| 26 | (((uint)((u) & 0xF000u)) << 12) ) |
| 27 | // 4 2-bit highs in byte b -> 4 bytes, bits 4-5 (q6_K) |
| 28 | #define EXP2(b) ( (((uint)((b) & 0x03u)) << 4) | \ |
| 29 | (((uint)((b) & 0x0Cu)) << 10) | \ |
| 30 | (((uint)((b) & 0x30u)) << 16) | \ |
| 31 | (((uint)((b) & 0xC0u)) << 22) ) |
| 32 | |
| 33 | // q6 (0..63) -> (q6-32) signed int8/byte (no inter-byte carry) |
| 34 | inline uint SIGN6(uint q6p){ uint x=q6p^0x20202020u; uint s=x&0x20202020u; return x|(s<<1)|(s<<2); } |
| 35 | |
| 36 | // 4 high bits (one per element, in bits 0..3 of h) -> bit4 of each of 4 bytes (5-bit hi) |
| 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 | // per-type weight params + per-32-step unpack into qw[8] (8 int8 uints) |
| 43 | #if MOE_QT == 4 || MOE_QT == 41 || MOE_QT == 40 |
| 44 | #define WEIGHT_PARAMS __read_only image1d_buffer_t src0_q, |
| 45 | #define LOAD_QW(step, sub) \ |
| 46 | uint qw[8]; { \ |
| 47 | const uint qoff0 = row + ((ne01*(step))>>3) + ((expert_id*ne00*ne01)>>3); \ |
| 48 | const uint qoff1 = row + ((ne01*((step)+16))>>3) + ((expert_id*ne00*ne01)>>3); \ |
| 49 | const uint r0=read_imageui(src0_q,qoff0+lid).x, r1=read_imageui(src0_q,qoff0+lid+ne01).x; \ |
| 50 | const uint r2=read_imageui(src0_q,qoff1+lid).x, r3=read_imageui(src0_q,qoff1+lid+ne01).x; \ |
| 51 | qw[0]=EXP4(r0); qw[1]=EXP4(r0>>16); qw[2]=EXP4(r1); qw[3]=EXP4(r1>>16); \ |
| 52 | qw[4]=EXP4(r2); qw[5]=EXP4(r2>>16); qw[6]=EXP4(r3); qw[7]=EXP4(r3>>16); } |
| 53 | |
| 54 | #elif MOE_QT == 5 || MOE_QT == 51 || MOE_QT == 50 |
| 55 | // low nibbles via image (q4_K layout) + high-bit plane src0_qh: 1 uint per 32-block |
| 56 | // (bit i = high bit of element i). qh laid out [expert][block][row] to match the |
| 57 | // existing q5_0 trans4 convert |
| 58 | #define WEIGHT_PARAMS __read_only image1d_buffer_t src0_q, __global uint * src0_qh, |
| 59 | #define LOAD_QW(step, sub) \ |
| 60 | uint qw[8]; { \ |
| 61 | const uint qoff0 = row + ((ne01*(step))>>3) + ((expert_id*ne00*ne01)>>3); \ |
| 62 | const uint qoff1 = row + ((ne01*((step)+16))>>3) + ((expert_id*ne00*ne01)>>3); \ |
| 63 | const uint r0=read_imageui(src0_q,qoff0+lid).x, r1=read_imageui(src0_q,qoff0+lid+ne01).x; \ |
| 64 | const uint r2=read_imageui(src0_q,qoff1+lid).x, r3=read_imageui(src0_q,qoff1+lid+ne01).x; \ |
| 65 | const uint h = src0_qh[row_idx + (sub)*ne01 + expert_id*(ne00>>5)*ne01]; \ |
| 66 | qw[0]=EXP4(r0)|EXP1(h); qw[1]=EXP4(r0>>16)|EXP1(h>>4); \ |
| 67 | qw[2]=EXP4(r1)|EXP1(h>>8); qw[3]=EXP4(r1>>16)|EXP1(h>>12); \ |
| 68 | qw[4]=EXP4(r2)|EXP1(h>>16); qw[5]=EXP4(r2>>16)|EXP1(h>>20); \ |
| 69 | qw[6]=EXP4(r3)|EXP1(h>>24); qw[7]=EXP4(r3>>16)|EXP1(h>>28); } |
| 70 | |
| 71 | #elif MOE_QT == 6 |
| 72 | #define WEIGHT_PARAMS __read_only image1d_buffer_t src0_ql, __global uint * src0_qh, |
| 73 | #define LOAD_QW(step, sub) \ |
| 74 | uint qw[8]; { \ |
| 75 | const uint qoff0 = row + ((ne01*(step))>>3) + ((expert_id*ne00*ne01)>>3); \ |
| 76 | const uint qoff1 = row + ((ne01*((step)+16))>>3) + ((expert_id*ne00*ne01)>>3); \ |
| 77 | const uint r0=read_imageui(src0_ql,qoff0+lid).x, r1=read_imageui(src0_ql,qoff0+lid+ne01).x; \ |
| 78 | const uint r2=read_imageui(src0_ql,qoff1+lid).x, r3=read_imageui(src0_ql,qoff1+lid+ne01).x; \ |
| 79 | const uint qhb = row + ((sub)*2)*ne01 + expert_id*((ne00>>5)*2)*ne01 + lid; \ |
| 80 | const uint qh1=src0_qh[qhb], qh2=src0_qh[qhb+ne01]; \ |
| 81 | qw[0]=SIGN6(EXP4(r0)|EXP2(qh1&0xFFu)); qw[1]=SIGN6(EXP4(r0>>16)|EXP2((qh1>>8)&0xFFu)); \ |
| 82 | qw[2]=SIGN6(EXP4(r1)|EXP2((qh1>>16)&0xFFu)); qw[3]=SIGN6(EXP4(r1>>16)|EXP2((qh1>>24)&0xFFu)); \ |
| 83 | qw[4]=SIGN6(EXP4(r2)|EXP2(qh2&0xFFu)); qw[5]=SIGN6(EXP4(r2>>16)|EXP2((qh2>>8)&0xFFu)); \ |
| 84 | qw[6]=SIGN6(EXP4(r3)|EXP2((qh2>>16)&0xFFu)); qw[7]=SIGN6(EXP4(r3>>16)|EXP2((qh2>>24)&0xFFu)); } |
| 85 | |
| 86 | #elif MOE_QT == 80 || MOE_QT == 82 |
| 87 | // 8-bit direct: int8 codes 8 uints / 32-block, [expert][row][8*sub]. mxfp4: the |
| 88 | // convert resolves kvalues_mxfp4[nibble] -> int8 and stores the e8m0_half scale. |
| 89 | #define WEIGHT_PARAMS __global uint * src0_q8, |
| 90 | #define LOAD_QW(step, sub) \ |
| 91 | uint qw[8]; { \ |
| 92 | const uint qb = (expert_id*ne01 + row_idx)*(ne00>>2) + (sub)*8; \ |
| 93 | qw[0]=src0_q8[qb+0]; qw[1]=src0_q8[qb+1]; qw[2]=src0_q8[qb+2]; qw[3]=src0_q8[qb+3]; \ |
| 94 | qw[4]=src0_q8[qb+4]; qw[5]=src0_q8[qb+5]; qw[6]=src0_q8[qb+6]; qw[7]=src0_q8[qb+7]; } |
| 95 | #else |
| 96 | #error "unknown MOE_QT" |
| 97 | #endif |
| 98 | |
| 99 | inline int dp4a4(uint w0,uint w1,uint w2,uint w3,uint a0,uint a1,uint a2,uint a3){ |
| 100 | int r=0; r=dot_acc_sat_4x8packed_ss_int(w0,a0,r); r=dot_acc_sat_4x8packed_ss_int(w1,a1,r); |
| 101 | r=dot_acc_sat_4x8packed_ss_int(w2,a2,r); r=dot_acc_sat_4x8packed_ss_int(w3,a3,r); return r; } |
| 102 | |
| 103 | // Q8_0 and MXFP4 have no min term. Do not multiply zero by an overflowing half activation sum. |
| 104 | #if MOE_QT == 80 || MOE_QT == 82 |
| 105 | #define MOE_MIN_TERM(t) 0.0f |
| 106 | #else |
| 107 | #define MOE_MIN_TERM(t) (mn*(float)sh_s[t]) |
| 108 | #endif |
| 109 | |
| 110 | // One token's two-half dp4a + uniform scale/min epilogue into acc[t]. |
| 111 | #define MOE_DP4A_T(t) do { \ |
| 112 | uint4 a0 = vload4(0, &sh_qa[t][0]); \ |
| 113 | uint4 a1 = vload4(0, &sh_qa[t][4]); \ |
| 114 | const int raw1 = dp4a4(qw[0],qw[1],qw[2],qw[3], a0.s0,a0.s1,a0.s2,a0.s3); \ |
| 115 | const int raw2 = dp4a4(qw[4],qw[5],qw[6],qw[7], a1.s0,a1.s1,a1.s2,a1.s3); \ |
| 116 | const float a_d = (float)sh_d[t]; \ |
| 117 | acc[t] += sc0*a_d*(float)raw1 + sc1*a_d*(float)raw2 - MOE_MIN_TERM(t); \ |
| 118 | } while (0) |
| 119 | |
| 120 | __attribute__((qcom_wave_pair_mode(1))) |
| 121 | kernel void kernel_gemm_moe_q8_1_dp4a( |
| 122 | WEIGHT_PARAMS // per-type native weight buffer(s) |
| 123 | __global half * src0_scale,// uniform f16 16/superblock (per-16), [expert,row] |
| 124 | __global half * src0_min, // uniform f16 8/superblock (per-32), [expert,row] |
| 125 | __global uint * src1_qa, // q8_1 activations int8 (as uint, 4/elem) |
| 126 | __global half * src1_da, // q8_1 per-block scale [tok_slot * ne00/32] |
| 127 | __global half * src1_sa, // q8_1 per-block sum*d [tok_slot * ne00/32] |
| 128 | __global uint * src2, // post-router (orig out positions) |
| 129 | __global ushort * src2_emap, // tile -> expert id |
| 130 | __write_only image1d_buffer_t dst, |
| 131 | __global int * total_tiles, |
| 132 | uint ne00, |
| 133 | uint ne01, |
| 134 | int is_ragged, |
| 135 | int has_min // 0 for symmetric types (q8_0/q6_K/q4_0/...): skip min read |
| 136 | ) { |
| 137 | const uint block_id_m = get_global_id(1); |
| 138 | const uint block_id_n = get_global_id(2); |
| 139 | if (block_id_n >= total_tiles[0]) return; |
| 140 | |
| 141 | const uint lid = get_local_id(0); // 0..63 -> output row within M-tile |
| 142 | const ushort expert_id = src2_emap[block_id_n]; |
| 143 | const uint row = block_id_m * TILESIZE_M; |
| 144 | const uint col = block_id_n * TILESIZE_N; |
| 145 | const uint row_idx = row + lid; |
| 146 | |
| 147 | // Scale/min are laid out FLAT per-32-block (2 per-16-segment scales + 1 min per |
| 148 | // 32-block), so K only needs to be a multiple of 32 — works for the 32-block |
| 149 | // types (q8_0/q5_0/q4_0/...) as well as the K-quants (K%256==0, same bytes). |
| 150 | const uint nblk32 = ne00 / 32; |
| 151 | const uint sc_per_row = nblk32 * 2; |
| 152 | const uint mn_per_row = nblk32; |
| 153 | const uint ne00_u = ne00 >> 2; |
| 154 | const uint ne00_b = ne00 >> 5; |
| 155 | |
| 156 | __local uint sh_qa[TILESIZE_N][8]; |
| 157 | __local half sh_d[TILESIZE_N]; |
| 158 | __local half sh_s[TILESIZE_N]; |
| 159 | |
| 160 | __local uint sh_src2[TILESIZE_N]; |
| 161 | __local int sh_nreal; |
| 162 | if (lid < TILESIZE_N) sh_src2[lid] = src2[col + lid]; |
| 163 | barrier(CLK_LOCAL_MEM_FENCE); |
| 164 | if (lid == 0) { |
| 165 | int nr = TILESIZE_N; |
| 166 | if (is_ragged) { nr = 0; |
| 167 | #pragma unroll |
| 168 | for (int t = 0; t < TILESIZE_N; ++t) if (sh_src2[t] != 0xFFFFFFFFu) ++nr; } |
| 169 | sh_nreal = nr; |
| 170 | } |
| 171 | barrier(CLK_LOCAL_MEM_FENCE); |
| 172 | const int n_real = sh_nreal; |
| 173 | |
| 174 | float acc[TILESIZE_N]; |
| 175 | #pragma unroll |
| 176 | for (int t = 0; t < TILESIZE_N; ++t) acc[t] = 0.0f; |
| 177 | |
| 178 | for (uint step = 0; step < ne00; step += 32) { |
| 179 | const uint sub = step >> 5; // 32-block index along K |
| 180 | |
| 181 | // uniform pre-decoded scale (2 per-16-seg) + min (1) for this row, this 32-block |
| 182 | __global half * scl = src0_scale + (expert_id*ne01 + row_idx)*sc_per_row + sub*2; |
| 183 | const float sc0 = (float)scl[0]; |
| 184 | const float sc1 = (float)scl[1]; |
| 185 | float mn = 0.0f; |
| 186 | if (has_min) mn = (float)src0_min[(expert_id*ne01 + row_idx)*mn_per_row + sub]; |
| 187 | |
| 188 | LOAD_QW(step, sub) |
| 189 | |
| 190 | // Stage each token's 8 activation uints as two 128-bit uint4 loads/stores. |
| 191 | const uint vlim = (uint)n_real * 2; |
| 192 | for (uint idx = lid; idx < vlim; idx += 64) { |
| 193 | const uint t = idx >> 1; |
| 194 | const uint h = (idx & 1) << 2; // 0 or 4 |
| 195 | uint4 v = vload4(0, &src1_qa[(col + t) * ne00_u + (step >> 2) + h]); |
| 196 | vstore4(v, 0, &sh_qa[t][h]); |
| 197 | } |
| 198 | if (lid < (uint)n_real) { |
| 199 | sh_d[lid] = src1_da[(col + lid) * ne00_b + sub]; |
| 200 | sh_s[lid] = src1_sa[(col + lid) * ne00_b + sub]; |
| 201 | } |
| 202 | barrier(CLK_LOCAL_MEM_FENCE); |
| 203 | |
| 204 | if (n_real == TILESIZE_N) { |
| 205 | #pragma unroll |
| 206 | for (int t = 0; t < TILESIZE_N; ++t) { MOE_DP4A_T(t); } |
| 207 | } else { |
| 208 | #pragma unroll 4 |
| 209 | for (int t = 0; t < n_real; ++t) { MOE_DP4A_T(t); } |
| 210 | } |
| 211 | barrier(CLK_LOCAL_MEM_FENCE); |
| 212 | } |
| 213 | |
| 214 | if (row_idx >= ne01) return; |
| 215 | |
| 216 | __local uint out_idx[TILESIZE_N]; |
| 217 | if (lid < TILESIZE_N) { |
| 218 | uint idx = sh_src2[lid]; |
| 219 | if (idx == 0xFFFFFFFF) idx = sh_src2[0]; |
| 220 | out_idx[lid] = idx * ne01; |
| 221 | } |
| 222 | barrier(CLK_LOCAL_MEM_FENCE); |
| 223 | |
| 224 | const uint m_offset = row + lid; |
| 225 | if (n_real == TILESIZE_N) { |
| 226 | #pragma unroll |
| 227 | for (int t = 1; t < TILESIZE_N; ++t) write_imagef(dst, out_idx[t] + m_offset, acc[t]); |
| 228 | barrier(CLK_GLOBAL_MEM_FENCE); |
| 229 | write_imagef(dst, out_idx[0] + m_offset, acc[0]); |
| 230 | } else { |
| 231 | for (int t = 0; t < n_real; ++t) write_imagef(dst, out_idx[t] + m_offset, acc[t]); |
| 232 | } |
| 233 | } |