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_q6_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 N_SIMDGROUP 4 |
| 7 | #define SIMDGROUP_WIDTH 64 |
| 8 | |
| 9 | static inline float8 q6_k_to_fp32_packed8(ushort2 ql8, ushort qh8, float d_scale) { |
| 10 | float8 fp32x8; |
| 11 | fp32x8.s0 = ((float)(( ql8.s0 & 0x000F) | ((uint)((qh8 ) & 0x3) << 4)) - 32.f) * d_scale; |
| 12 | fp32x8.s1 = ((float)((( ql8.s0 >> 4) & 0x000F) | ((uint)((qh8 >> 2) & 0x3) << 4)) - 32.f) * d_scale; |
| 13 | fp32x8.s2 = ((float)((( ql8.s0 >> 8) & 0x000F) | ((uint)((qh8 >> 4) & 0x3) << 4)) - 32.f) * d_scale; |
| 14 | fp32x8.s3 = ((float)((( ql8.s0 >> 12)& 0x000F) | ((uint)((qh8 >> 6) & 0x3) << 4)) - 32.f) * d_scale; |
| 15 | fp32x8.s4 = ((float)(( ql8.s1 & 0x000F) | ((uint)((qh8 >> 8) & 0x3) << 4)) - 32.f) * d_scale; |
| 16 | fp32x8.s5 = ((float)((( ql8.s1 >> 4) & 0x000F) | ((uint)((qh8 >>10) & 0x3) << 4)) - 32.f) * d_scale; |
| 17 | fp32x8.s6 = ((float)((( ql8.s1 >> 8) & 0x000F) | ((uint)((qh8 >>12) & 0x3) << 4)) - 32.f) * d_scale; |
| 18 | fp32x8.s7 = ((float)((( ql8.s1 >> 12)& 0x000F) | ((uint)((qh8 >>14) & 0x3) << 4)) - 32.f) * d_scale; |
| 19 | return fp32x8; |
| 20 | } |
| 21 | |
| 22 | __attribute__((qcom_reqd_sub_group_size("half"))) |
| 23 | __kernel void kernel_gemv_moe_q6_k_f32_ns( |
| 24 | __global uint * src0_ql, |
| 25 | __global uint * src0_qh, |
| 26 | __global char * src0_s, |
| 27 | __global half * src0_d, |
| 28 | __read_only image1d_buffer_t src1, |
| 29 | __global uint * src2, |
| 30 | __global float * dst, |
| 31 | ulong offsetd, |
| 32 | int ne00, |
| 33 | int ne01, |
| 34 | int ne11 |
| 35 | ) { |
| 36 | uint i01 = get_global_id(0); |
| 37 | uint i20 = get_global_id(2); |
| 38 | uint sgid = get_local_id(1); |
| 39 | uint slid = get_sub_group_local_id(); |
| 40 | |
| 41 | if (i01 >= ne01) { |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | uint i11 = i20 % ne11; |
| 46 | |
| 47 | uint expert_id = src2[i20]; |
| 48 | if ((int)expert_id == -1) { // skipped slot, the dst row is zeroed by kernel_moe_zero_dst |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | int num_superblocks = ne00 / QK_K; |
| 53 | int num_subblocks = ne00 / 32; // 8 sub-blocks of 32 per super-block |
| 54 | int scales_per_row = num_superblocks * 16; |
| 55 | |
| 56 | // Expert offsets in the transposed noshuffle layout |
| 57 | uint expert_ql_offset = expert_id * (ne00 / 8) * ne01; // 32 uints per super-block |
| 58 | uint expert_qh_offset = expert_id * (ne00 / 16) * ne01; // 16 uints per super-block |
| 59 | uint expert_d_offset = expert_id * num_superblocks * ne01; |
| 60 | |
| 61 | __private float sum = 0.0f; |
| 62 | |
| 63 | // Loop over sub-blocks of 32 elements, N_SIMDGROUP sub-blocks per iter |
| 64 | for (uint ib = sgid; ib < num_subblocks; ib += N_SIMDGROUP) { |
| 65 | uint sb = ib / 8; // super-block index |
| 66 | uint j = ib % 8; // 32-element group within super-block |
| 67 | |
| 68 | // Load d for this super-block |
| 69 | half d_val = src0_d[expert_d_offset + sb * ne01 + i01]; |
| 70 | |
| 71 | // Load 2 sub-block scales |
| 72 | global const char * sc = src0_s + (expert_id * ne01 + i01) * scales_per_row + sb * 16; |
| 73 | float scale0 = (float)d_val * (float)sc[j * 2]; |
| 74 | float scale1 = (float)d_val * (float)sc[j * 2 + 1]; |
| 75 | |
| 76 | // Load 4 uints of ql |
| 77 | uint ql_base = expert_ql_offset + (ib * 4) * ne01 + i01; |
| 78 | uint4 regQL; |
| 79 | regQL.s0 = src0_ql[ql_base]; |
| 80 | regQL.s1 = src0_ql[ql_base + ne01]; |
| 81 | regQL.s2 = src0_ql[ql_base + ne01 * 2]; |
| 82 | regQL.s3 = src0_ql[ql_base + ne01 * 3]; |
| 83 | |
| 84 | // Load 2 uints of qh |
| 85 | uint qh_base = expert_qh_offset + (ib * 2) * ne01 + i01; |
| 86 | uint2 regQH; |
| 87 | regQH.s0 = src0_qh[qh_base]; |
| 88 | regQH.s1 = src0_qh[qh_base + ne01]; |
| 89 | |
| 90 | // Load activations: 32 floats = 8 float4s |
| 91 | uint y_offset = i11 * ne00 / 4 + ib * 8; |
| 92 | |
| 93 | float8 fp32x8 = q6_k_to_fp32_packed8(as_ushort2(regQL.s0), (ushort)(regQH.s0 & 0xFFFF), scale0); |
| 94 | |
| 95 | float4 shared_y4; |
| 96 | shared_y4 = read_imagef(src1, (y_offset + 0)); |
| 97 | float4 acc = shared_y4 * fp32x8.lo; |
| 98 | |
| 99 | shared_y4 = read_imagef(src1, (y_offset + 1)); |
| 100 | acc += shared_y4 * fp32x8.hi; |
| 101 | |
| 102 | fp32x8 = q6_k_to_fp32_packed8(as_ushort2(regQL.s1), (ushort)(regQH.s0 >> 16), scale0); |
| 103 | |
| 104 | shared_y4 = read_imagef(src1, (y_offset + 2)); |
| 105 | acc += shared_y4 * fp32x8.lo; |
| 106 | |
| 107 | shared_y4 = read_imagef(src1, (y_offset + 3)); |
| 108 | acc += shared_y4 * fp32x8.hi; |
| 109 | |
| 110 | fp32x8 = q6_k_to_fp32_packed8(as_ushort2(regQL.s2), (ushort)(regQH.s1 & 0xFFFF), scale1); |
| 111 | |
| 112 | shared_y4 = read_imagef(src1, (y_offset + 4)); |
| 113 | acc += shared_y4 * fp32x8.lo; |
| 114 | |
| 115 | shared_y4 = read_imagef(src1, (y_offset + 5)); |
| 116 | acc += shared_y4 * fp32x8.hi; |
| 117 | |
| 118 | fp32x8 = q6_k_to_fp32_packed8(as_ushort2(regQL.s3), (ushort)(regQH.s1 >> 16), scale1); |
| 119 | |
| 120 | shared_y4 = read_imagef(src1, (y_offset + 6)); |
| 121 | acc += shared_y4 * fp32x8.lo; |
| 122 | |
| 123 | shared_y4 = read_imagef(src1, (y_offset + 7)); |
| 124 | acc += shared_y4 * fp32x8.hi; |
| 125 | |
| 126 | sum += ((acc.s0 + acc.s1) + (acc.s2 + acc.s3)); |
| 127 | } |
| 128 | |
| 129 | // reduction in local memory, assumes #subgroups=4 |
| 130 | __local float reduceLM[SIMDGROUP_WIDTH * (N_SIMDGROUP - 1)]; |
| 131 | if (sgid == 1) reduceLM[SIMDGROUP_WIDTH * 0 + slid] = sum; |
| 132 | if (sgid == 2) reduceLM[SIMDGROUP_WIDTH * 1 + slid] = sum; |
| 133 | if (sgid == 3) reduceLM[SIMDGROUP_WIDTH * 2 + slid] = sum; |
| 134 | barrier(CLK_LOCAL_MEM_FENCE); |
| 135 | if (sgid == 0) sum += reduceLM[SIMDGROUP_WIDTH * 0 + slid]; |
| 136 | if (sgid == 0) sum += reduceLM[SIMDGROUP_WIDTH * 1 + slid]; |
| 137 | if (sgid == 0) sum += reduceLM[SIMDGROUP_WIDTH * 2 + slid]; |
| 138 | |
| 139 | // 1 output per thread in subgroup 0 |
| 140 | if (sgid == 0) { |
| 141 | dst = dst + (offsetd >> 2); |
| 142 | dst[i01 + i20 * ne01] = sum; |
| 143 | } |
| 144 | } |