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 / gemv_moe_q4_k_f32_ns.cl
| 1 | #pragma OPENCL EXTENSION cl_khr_fp16 : enable |
| 2 | #pragma OPENCL EXTENSION cl_khr_subgroups : enable |
| 3 | #pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable |
| 4 | |
| 5 | #define QK_K 256 |
| 6 | #define K_SCALE_SIZE 12 |
| 7 | #define N_SIMDGROUP 4 |
| 8 | #define SIMDGROUP_WIDTH 64 |
| 9 | |
| 10 | inline void get_scale_min_k4( |
| 11 | int j, |
| 12 | global const uchar * q, |
| 13 | uchar * d, |
| 14 | uchar * m |
| 15 | ) { |
| 16 | if (j < 4) { |
| 17 | *d = q[j] & 63; |
| 18 | *m = q[j+4] & 63; |
| 19 | } else { |
| 20 | *d = (q[j+4] & 0x0F) | ((q[j-4] & 0xC0) >> 2); |
| 21 | *m = ((q[j+4] >> 4) & 0x0F) | ((q[j] & 0xC0) >> 2); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | static inline float8 q4_k_to_fp32_packed8(ushort2 q4x8, float scale, float minv) { |
| 26 | float8 fp32x8; |
| 27 | fp32x8.s0 = (q4x8.s0 & 0x000F) * scale - minv; |
| 28 | fp32x8.s1 = ((q4x8.s0 & 0x00F0) >> 4) * scale - minv; |
| 29 | fp32x8.s2 = ((q4x8.s0 & 0x0F00) >> 8) * scale - minv; |
| 30 | fp32x8.s3 = ((q4x8.s0 & 0xF000) >> 12) * scale - minv; |
| 31 | fp32x8.s4 = (q4x8.s1 & 0x000F) * scale - minv; |
| 32 | fp32x8.s5 = ((q4x8.s1 & 0x00F0) >> 4) * scale - minv; |
| 33 | fp32x8.s6 = ((q4x8.s1 & 0x0F00) >> 8) * scale - minv; |
| 34 | fp32x8.s7 = ((q4x8.s1 & 0xF000) >> 12) * scale - minv; |
| 35 | return fp32x8; |
| 36 | } |
| 37 | |
| 38 | __attribute__((qcom_reqd_sub_group_size("half"))) |
| 39 | __kernel void kernel_gemv_moe_q4_k_f32_ns( |
| 40 | __global uint * src0_q, |
| 41 | __global half * src0_d, |
| 42 | __global half * src0_dm, |
| 43 | __global uchar * src0_s, |
| 44 | __read_only image1d_buffer_t src1, |
| 45 | __global uint * src2, |
| 46 | __global float * dst, |
| 47 | ulong offsetd, |
| 48 | int ne00, |
| 49 | int ne01, |
| 50 | int ne11 |
| 51 | ) { |
| 52 | uint i01 = get_global_id(0); |
| 53 | uint i20 = get_global_id(2); |
| 54 | uint sgid = get_local_id(1); |
| 55 | uint slid = get_sub_group_local_id(); |
| 56 | |
| 57 | if (i01 >= ne01) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | uint i11 = i20 % ne11; |
| 62 | |
| 63 | uint expert_id = src2[i20]; |
| 64 | if ((int)expert_id == -1) { // skipped slot, the dst row is zeroed by kernel_moe_zero_dst |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | int num_superblocks = ne00 / QK_K; |
| 69 | int num_subblocks = ne00 / 32; |
| 70 | int scales_per_row = num_superblocks * K_SCALE_SIZE; |
| 71 | |
| 72 | // Expert offsets in the transposed noshuffle layout |
| 73 | uint expert_q_offset = expert_id * (ne00 / 8) * ne01; |
| 74 | uint expert_d_offset = expert_id * num_superblocks * ne01; |
| 75 | |
| 76 | __private float sum = 0.0f; |
| 77 | |
| 78 | // Loop over sub-blocks of 32 elements, N_SIMDGROUP sub-blocks per iter |
| 79 | for (uint ib = sgid; ib < num_subblocks; ib += N_SIMDGROUP) { |
| 80 | uint sb = ib / 8; |
| 81 | uint j = ib % 8; |
| 82 | |
| 83 | // Load d and dmin for this super-block |
| 84 | half d_val = src0_d[expert_d_offset + sb * ne01 + i01]; |
| 85 | half dm_val = src0_dm[expert_d_offset + sb * ne01 + i01]; |
| 86 | |
| 87 | // Load sub-block scale and min |
| 88 | global const uchar * sc = src0_s + (expert_id * ne01 + i01) * scales_per_row + sb * K_SCALE_SIZE; |
| 89 | uchar sv, mn; |
| 90 | get_scale_min_k4(j, sc, &sv, &mn); |
| 91 | |
| 92 | float scale = (float)d_val * (float)sv; |
| 93 | float minv = (float)dm_val * (float)mn; |
| 94 | |
| 95 | // Load 4 uints of quants (32 nibbles = 32 elements) |
| 96 | uint q_base = expert_q_offset + ib * ne01 * 4 + i01; |
| 97 | |
| 98 | uint4 regQ; |
| 99 | regQ.s0 = src0_q[q_base]; |
| 100 | regQ.s1 = src0_q[q_base + ne01]; |
| 101 | regQ.s2 = src0_q[q_base + ne01 * 2]; |
| 102 | regQ.s3 = src0_q[q_base + ne01 * 3]; |
| 103 | |
| 104 | // Load activations: 32 floats = 8 float4s |
| 105 | uint y_offset = i11 * ne00 / 4 + ib * 8; |
| 106 | |
| 107 | float8 fp32x8 = q4_k_to_fp32_packed8(as_ushort2(regQ.s0), scale, minv); |
| 108 | |
| 109 | float4 shared_y4; |
| 110 | shared_y4 = read_imagef(src1, (y_offset + 0)); |
| 111 | float4 acc = shared_y4 * fp32x8.lo; |
| 112 | |
| 113 | shared_y4 = read_imagef(src1, (y_offset + 1)); |
| 114 | acc += shared_y4 * fp32x8.hi; |
| 115 | |
| 116 | fp32x8 = q4_k_to_fp32_packed8(as_ushort2(regQ.s1), scale, minv); |
| 117 | |
| 118 | shared_y4 = read_imagef(src1, (y_offset + 2)); |
| 119 | acc += shared_y4 * fp32x8.lo; |
| 120 | |
| 121 | shared_y4 = read_imagef(src1, (y_offset + 3)); |
| 122 | acc += shared_y4 * fp32x8.hi; |
| 123 | |
| 124 | fp32x8 = q4_k_to_fp32_packed8(as_ushort2(regQ.s2), scale, minv); |
| 125 | |
| 126 | shared_y4 = read_imagef(src1, (y_offset + 4)); |
| 127 | acc += shared_y4 * fp32x8.lo; |
| 128 | |
| 129 | shared_y4 = read_imagef(src1, (y_offset + 5)); |
| 130 | acc += shared_y4 * fp32x8.hi; |
| 131 | |
| 132 | fp32x8 = q4_k_to_fp32_packed8(as_ushort2(regQ.s3), scale, minv); |
| 133 | |
| 134 | shared_y4 = read_imagef(src1, (y_offset + 6)); |
| 135 | acc += shared_y4 * fp32x8.lo; |
| 136 | |
| 137 | shared_y4 = read_imagef(src1, (y_offset + 7)); |
| 138 | acc += shared_y4 * fp32x8.hi; |
| 139 | |
| 140 | sum += ((acc.s0 + acc.s1) + (acc.s2 + acc.s3)); |
| 141 | } |
| 142 | |
| 143 | // reduction in local memory, assumes #subgroups=4 |
| 144 | __local float reduceLM[SIMDGROUP_WIDTH * (N_SIMDGROUP - 1)]; |
| 145 | if (sgid == 1) reduceLM[SIMDGROUP_WIDTH * 0 + slid] = sum; |
| 146 | if (sgid == 2) reduceLM[SIMDGROUP_WIDTH * 1 + slid] = sum; |
| 147 | if (sgid == 3) reduceLM[SIMDGROUP_WIDTH * 2 + slid] = sum; |
| 148 | barrier(CLK_LOCAL_MEM_FENCE); |
| 149 | if (sgid == 0) sum += reduceLM[SIMDGROUP_WIDTH * 0 + slid]; |
| 150 | if (sgid == 0) sum += reduceLM[SIMDGROUP_WIDTH * 1 + slid]; |
| 151 | if (sgid == 0) sum += reduceLM[SIMDGROUP_WIDTH * 2 + slid]; |
| 152 | |
| 153 | // 1 output per thread in subgroup 0 |
| 154 | if (sgid == 0) { |
| 155 | dst = dst + (offsetd >> 2); |
| 156 | dst[i01 + i20 * ne01] = sum; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | __attribute__((qcom_reqd_sub_group_size("half"))) |
| 161 | __kernel void kernel_gemv_moe_q4_k_f32_ns_wimg( |
| 162 | __read_only image1d_buffer_t src0_q, |
| 163 | __global half * src0_d, |
| 164 | __global half * src0_dm, |
| 165 | __global uchar * src0_s, |
| 166 | __read_only image1d_buffer_t src1, |
| 167 | __global uint * src2, |
| 168 | __global float * dst, |
| 169 | ulong offsetd, |
| 170 | int ne00, |
| 171 | int ne01, |
| 172 | int ne11 |
| 173 | ) { |
| 174 | uint i01 = get_global_id(0); |
| 175 | uint i20 = get_global_id(2); |
| 176 | uint sgid = get_local_id(1); |
| 177 | uint slid = get_sub_group_local_id(); |
| 178 | |
| 179 | if (i01 >= ne01) { |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | uint i11 = i20 % ne11; |
| 184 | |
| 185 | uint expert_id = src2[i20]; |
| 186 | if ((int)expert_id == -1) { // skipped slot, the dst row is zeroed by kernel_moe_zero_dst |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | int num_superblocks = ne00 / QK_K; |
| 191 | int num_subblocks = ne00 / 32; |
| 192 | int scales_per_row = num_superblocks * K_SCALE_SIZE; |
| 193 | |
| 194 | uint expert_q_offset = expert_id * (ne00 / 8) * ne01; |
| 195 | uint expert_d_offset = expert_id * num_superblocks * ne01; |
| 196 | |
| 197 | __private float sum = 0.0f; |
| 198 | |
| 199 | for (uint ib = sgid; ib < num_subblocks; ib += N_SIMDGROUP) { |
| 200 | uint sb = ib / 8; |
| 201 | uint j = ib % 8; |
| 202 | |
| 203 | half d_val = src0_d[expert_d_offset + sb * ne01 + i01]; |
| 204 | half dm_val = src0_dm[expert_d_offset + sb * ne01 + i01]; |
| 205 | |
| 206 | global const uchar * sc = src0_s + (expert_id * ne01 + i01) * scales_per_row + sb * K_SCALE_SIZE; |
| 207 | uchar sv, mn; |
| 208 | get_scale_min_k4(j, sc, &sv, &mn); |
| 209 | |
| 210 | float scale = (float)d_val * (float)sv; |
| 211 | float minv = (float)dm_val * (float)mn; |
| 212 | |
| 213 | uint q_base = expert_q_offset + ib * ne01 * 4 + i01; |
| 214 | |
| 215 | uint4 regQ; |
| 216 | regQ.s0 = read_imageui(src0_q, (int)(q_base)).x; |
| 217 | regQ.s1 = read_imageui(src0_q, (int)(q_base + ne01)).x; |
| 218 | regQ.s2 = read_imageui(src0_q, (int)(q_base + ne01 * 2)).x; |
| 219 | regQ.s3 = read_imageui(src0_q, (int)(q_base + ne01 * 3)).x; |
| 220 | |
| 221 | uint y_offset = i11 * ne00 / 4 + ib * 8; |
| 222 | |
| 223 | float8 fp32x8 = q4_k_to_fp32_packed8(as_ushort2(regQ.s0), scale, minv); |
| 224 | |
| 225 | float4 shared_y4; |
| 226 | shared_y4 = read_imagef(src1, (y_offset + 0)); |
| 227 | float4 acc = shared_y4 * fp32x8.lo; |
| 228 | |
| 229 | shared_y4 = read_imagef(src1, (y_offset + 1)); |
| 230 | acc += shared_y4 * fp32x8.hi; |
| 231 | |
| 232 | fp32x8 = q4_k_to_fp32_packed8(as_ushort2(regQ.s1), scale, minv); |
| 233 | |
| 234 | shared_y4 = read_imagef(src1, (y_offset + 2)); |
| 235 | acc += shared_y4 * fp32x8.lo; |
| 236 | |
| 237 | shared_y4 = read_imagef(src1, (y_offset + 3)); |
| 238 | acc += shared_y4 * fp32x8.hi; |
| 239 | |
| 240 | fp32x8 = q4_k_to_fp32_packed8(as_ushort2(regQ.s2), scale, minv); |
| 241 | |
| 242 | shared_y4 = read_imagef(src1, (y_offset + 4)); |
| 243 | acc += shared_y4 * fp32x8.lo; |
| 244 | |
| 245 | shared_y4 = read_imagef(src1, (y_offset + 5)); |
| 246 | acc += shared_y4 * fp32x8.hi; |
| 247 | |
| 248 | fp32x8 = q4_k_to_fp32_packed8(as_ushort2(regQ.s3), scale, minv); |
| 249 | |
| 250 | shared_y4 = read_imagef(src1, (y_offset + 6)); |
| 251 | acc += shared_y4 * fp32x8.lo; |
| 252 | |
| 253 | shared_y4 = read_imagef(src1, (y_offset + 7)); |
| 254 | acc += shared_y4 * fp32x8.hi; |
| 255 | |
| 256 | sum += ((acc.s0 + acc.s1) + (acc.s2 + acc.s3)); |
| 257 | } |
| 258 | |
| 259 | __local float reduceLM[SIMDGROUP_WIDTH * (N_SIMDGROUP - 1)]; |
| 260 | if (sgid == 1) reduceLM[SIMDGROUP_WIDTH * 0 + slid] = sum; |
| 261 | if (sgid == 2) reduceLM[SIMDGROUP_WIDTH * 1 + slid] = sum; |
| 262 | if (sgid == 3) reduceLM[SIMDGROUP_WIDTH * 2 + slid] = sum; |
| 263 | barrier(CLK_LOCAL_MEM_FENCE); |
| 264 | if (sgid == 0) sum += reduceLM[SIMDGROUP_WIDTH * 0 + slid]; |
| 265 | if (sgid == 0) sum += reduceLM[SIMDGROUP_WIDTH * 1 + slid]; |
| 266 | if (sgid == 0) sum += reduceLM[SIMDGROUP_WIDTH * 2 + slid]; |
| 267 | |
| 268 | if (sgid == 0) { |
| 269 | dst = dst + (offsetd >> 2); |
| 270 | dst[i01 + i20 * ne01] = sum; |
| 271 | } |
| 272 | } |