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_q4_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 | // q4_K subblock (32 elems): w_i = scale*q_i - minv, q_i in [0,15], scale = |
| 8 | // d_super*sv6, minv = dmin_super*mn6. With activation block (a_d, a_s, qa[32]): |
| 9 | // Sum_i w_i * a_i = scale * a_d * dp4a(q, qa) - minv * a_s |
| 10 | // where a_s = a_d * Sum(qa) (the q8_1 "s" field) |
| 11 | |
| 12 | #define TILESIZE_M 64 |
| 13 | #define TILESIZE_N 32 |
| 14 | #define QK_K 256 |
| 15 | #define K_SCALE_SIZE 12 |
| 16 | |
| 17 | inline void get_scale_min_k4( |
| 18 | int j, |
| 19 | global const uchar * q, |
| 20 | uchar * d, |
| 21 | uchar * m |
| 22 | ) { |
| 23 | if (j < 4) { |
| 24 | *d = q[j] & 63; |
| 25 | *m = q[j+4] & 63; |
| 26 | } else { |
| 27 | *d = (q[j+4] & 0x0F) | ((q[j-4] & 0xC0) >> 2); |
| 28 | *m = ((q[j+4] >> 4) & 0x0F) | ((q[j] & 0xC0) >> 2); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // Expand the 4 nibbles held in the low 16 bits of `u` into 4 bytes (one nibble |
| 33 | // per byte, value 0..15), packed for the int8 dp4a. |
| 34 | #define EXP4(u) ( ((uint)((u) & 0x000Fu)) | \ |
| 35 | (((uint)((u) & 0x00F0u)) << 4) | \ |
| 36 | (((uint)((u) & 0x0F00u)) << 8) | \ |
| 37 | (((uint)((u) & 0xF000u)) << 12) ) |
| 38 | |
| 39 | // One token's dp4a dot (8 uints = 32 K elems) + q4_K scale/min epilogue into acc[t]. |
| 40 | // The 8 activation uints are read as two 128-bit uint4 loads staged to private (Adreno |
| 41 | // wants 128-bit local reads, and a __local operand fed straight to the dp4a builtin is |
| 42 | // slower and can miscompile). |
| 43 | #define MOE_Q4K_DP4A_T(t) do { \ |
| 44 | uint4 a0 = vload4(0, &sh_qa[t][0]); \ |
| 45 | uint4 a1 = vload4(0, &sh_qa[t][4]); \ |
| 46 | int raw = 0; \ |
| 47 | raw = dot_acc_sat_4x8packed_ss_int(qw[0], a0.s0, raw); \ |
| 48 | raw = dot_acc_sat_4x8packed_ss_int(qw[1], a0.s1, raw); \ |
| 49 | raw = dot_acc_sat_4x8packed_ss_int(qw[2], a0.s2, raw); \ |
| 50 | raw = dot_acc_sat_4x8packed_ss_int(qw[3], a0.s3, raw); \ |
| 51 | raw = dot_acc_sat_4x8packed_ss_int(qw[4], a1.s0, raw); \ |
| 52 | raw = dot_acc_sat_4x8packed_ss_int(qw[5], a1.s1, raw); \ |
| 53 | raw = dot_acc_sat_4x8packed_ss_int(qw[6], a1.s2, raw); \ |
| 54 | raw = dot_acc_sat_4x8packed_ss_int(qw[7], a1.s3, raw); \ |
| 55 | acc[t] += scale * (float)sh_d[t] * (float)raw - minv * (float)sh_s[t]; \ |
| 56 | } while (0) |
| 57 | |
| 58 | __attribute__((qcom_wave_pair_mode(1))) |
| 59 | kernel void kernel_gemm_moe_q4_k_q8_1_dp4a( |
| 60 | __read_only image1d_buffer_t src0_q, // q4_K weights (transposed, packed nibbles) |
| 61 | __global half * src0_d, // per-superblock scale |
| 62 | __global half * src0_dm, // per-superblock min |
| 63 | __global uchar * src0_s, // 6-bit scale/min codes |
| 64 | __global uint * src1_qa, // q8_1 activations: int8 quants (as uint, 4/elem) |
| 65 | __global half * src1_da, // q8_1 per-block scale [tok_slot * ne00/32] |
| 66 | __global half * src1_sa, // q8_1 per-block sum*d [tok_slot * ne00/32] |
| 67 | __global uint * src2, // post-router (orig out positions) |
| 68 | __global ushort * src2_emap,// tile -> expert id |
| 69 | __write_only image1d_buffer_t dst, |
| 70 | __global int * total_tiles, |
| 71 | uint ne00, |
| 72 | uint ne01, |
| 73 | int is_ragged // 1: compute only real tokens per tile |
| 74 | ) { |
| 75 | const uint block_id_m = get_global_id(1); // m_tile |
| 76 | const uint block_id_n = get_global_id(2); // n_tile |
| 77 | |
| 78 | if (block_id_n >= total_tiles[0]) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | const uint lid = get_local_id(0); // 0..63, == this WI's output row in the M-tile |
| 83 | |
| 84 | const ushort expert_id = src2_emap[block_id_n]; |
| 85 | const uint row = block_id_m * TILESIZE_M; |
| 86 | const uint col = block_id_n * TILESIZE_N; |
| 87 | |
| 88 | const uint num_superblocks = ne00 / QK_K; |
| 89 | const uint scales_per_row = num_superblocks * K_SCALE_SIZE; |
| 90 | const uint row_idx = row + lid; |
| 91 | |
| 92 | const uint ne00_u = ne00 >> 2; // ne00 in uint (int8x4) units |
| 93 | const uint ne00_b = ne00 >> 5; // blocks-of-32 per token |
| 94 | |
| 95 | __local uint sh_qa[TILESIZE_N][8]; // 32 tokens x 8 uints (32 int8) = 1 KiB |
| 96 | __local half sh_d[TILESIZE_N]; |
| 97 | __local half sh_s[TILESIZE_N]; |
| 98 | |
| 99 | // Real token count for this tile |
| 100 | __local uint sh_src2[TILESIZE_N]; |
| 101 | __local int sh_nreal; |
| 102 | if (lid < TILESIZE_N) { |
| 103 | sh_src2[lid] = src2[col + lid]; |
| 104 | } |
| 105 | barrier(CLK_LOCAL_MEM_FENCE); |
| 106 | if (lid == 0) { |
| 107 | int nr = TILESIZE_N; |
| 108 | if (is_ragged) { |
| 109 | nr = 0; |
| 110 | #pragma unroll |
| 111 | for (int t = 0; t < TILESIZE_N; ++t) { |
| 112 | if (sh_src2[t] != 0xFFFFFFFFu) ++nr; |
| 113 | } |
| 114 | } |
| 115 | sh_nreal = nr; |
| 116 | } |
| 117 | barrier(CLK_LOCAL_MEM_FENCE); |
| 118 | const int n_real = sh_nreal; |
| 119 | |
| 120 | float acc[TILESIZE_N]; |
| 121 | #pragma unroll |
| 122 | for (int t = 0; t < TILESIZE_N; ++t) acc[t] = 0.0f; |
| 123 | |
| 124 | for (uint step = 0; step < ne00; step += 32) { |
| 125 | const uint sub = step >> 5; // subblock index along K |
| 126 | const uint sb = sub >> 3; // superblock index |
| 127 | const uint j = sub & 7; // subblock within superblock |
| 128 | |
| 129 | // --- weight scale / min for this WI's row, this subblock --- |
| 130 | const uint d_offset = row + sb * ne01 + expert_id * num_superblocks * ne01 + lid; |
| 131 | const float d_val = (float)src0_d[d_offset]; |
| 132 | const float dm_val = (float)src0_dm[d_offset]; |
| 133 | |
| 134 | global const uchar * sc = src0_s + (expert_id * ne01 + row_idx) * scales_per_row + sb * K_SCALE_SIZE; |
| 135 | uchar sv, mn; |
| 136 | get_scale_min_k4(j, sc, &sv, &mn); |
| 137 | const float scale = d_val * (float)sv; |
| 138 | const float minv = dm_val * (float)mn; |
| 139 | |
| 140 | // --- repack this WI's 32 weight nibbles into 8 dp4a uints --- |
| 141 | const uint qoff0 = row + ((ne01 * step) >> 3) + ((expert_id * ne00 * ne01) >> 3); |
| 142 | const uint qoff1 = row + ((ne01 * (step + 16)) >> 3) + ((expert_id * ne00 * ne01) >> 3); |
| 143 | const uint r0 = read_imageui(src0_q, qoff0 + lid).x; |
| 144 | const uint r1 = read_imageui(src0_q, qoff0 + lid + ne01).x; |
| 145 | const uint r2 = read_imageui(src0_q, qoff1 + lid).x; |
| 146 | const uint r3 = read_imageui(src0_q, qoff1 + lid + ne01).x; |
| 147 | uint qw[8]; |
| 148 | qw[0] = EXP4(r0); qw[1] = EXP4(r0 >> 16); |
| 149 | qw[2] = EXP4(r1); qw[3] = EXP4(r1 >> 16); |
| 150 | qw[4] = EXP4(r2); qw[5] = EXP4(r2 >> 16); |
| 151 | qw[6] = EXP4(r3); qw[7] = EXP4(r3 >> 16); |
| 152 | |
| 153 | // cooperatively stage the n_real-token x 32-K int8 activations to lm |
| 154 | // Stage each token's 8 activation uints as two 128-bit uint4 loads/stores. |
| 155 | const uint vlim = (uint)n_real * 2; |
| 156 | for (uint idx = lid; idx < vlim; idx += 64) { |
| 157 | const uint t = idx >> 1; |
| 158 | const uint h = (idx & 1) << 2; // 0 or 4 |
| 159 | uint4 v = vload4(0, &src1_qa[(col + t) * ne00_u + (step >> 2) + h]); |
| 160 | vstore4(v, 0, &sh_qa[t][h]); |
| 161 | } |
| 162 | if (lid < (uint)n_real) { |
| 163 | sh_d[lid] = src1_da[(col + lid) * ne00_b + sub]; |
| 164 | sh_s[lid] = src1_sa[(col + lid) * ne00_b + sub]; |
| 165 | } |
| 166 | barrier(CLK_LOCAL_MEM_FENCE); |
| 167 | |
| 168 | // dp4a - each real token sum over 8 uints (32 K), then scale/min |
| 169 | // Full tiles keep the fully-unrolled 32-wide loop; |
| 170 | // partial tiles run only n_real (saves the padded-slot dp4a + staging). |
| 171 | if (n_real == TILESIZE_N) { |
| 172 | #pragma unroll |
| 173 | for (int t = 0; t < TILESIZE_N; ++t) { MOE_Q4K_DP4A_T(t); } |
| 174 | } else { |
| 175 | #pragma unroll 4 |
| 176 | for (int t = 0; t < n_real; ++t) { MOE_Q4K_DP4A_T(t); } |
| 177 | } |
| 178 | barrier(CLK_LOCAL_MEM_FENCE); |
| 179 | } |
| 180 | |
| 181 | if (row_idx >= ne01) { |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | // scatter results to original output rows |
| 186 | __local uint out_idx[TILESIZE_N]; |
| 187 | if (lid < TILESIZE_N) { |
| 188 | uint idx = sh_src2[lid]; |
| 189 | if (idx == 0xFFFFFFFF) { |
| 190 | idx = sh_src2[0]; |
| 191 | } |
| 192 | out_idx[lid] = idx * ne01; |
| 193 | } |
| 194 | barrier(CLK_LOCAL_MEM_FENCE); |
| 195 | |
| 196 | const uint m_offset = row + lid; |
| 197 | if (n_real == TILESIZE_N) { |
| 198 | #pragma unroll |
| 199 | for (int t = 1; t < TILESIZE_N; ++t) { |
| 200 | write_imagef(dst, out_idx[t] + m_offset, acc[t]); |
| 201 | } |
| 202 | barrier(CLK_GLOBAL_MEM_FENCE); |
| 203 | write_imagef(dst, out_idx[0] + m_offset, acc[0]); |
| 204 | } else { |
| 205 | for (int t = 0; t < n_real; ++t) { |
| 206 | write_imagef(dst, out_idx[t] + m_offset, acc[t]); |
| 207 | } |
| 208 | } |
| 209 | } |