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_1_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_Q4_1 32 |
| 6 | #define N_SIMDGROUP 4 |
| 7 | #define SIMDGROUP_WIDTH 64 |
| 8 | |
| 9 | static inline float8 q4_1_to_fp32_packed8(ushort2 q4x8, half s, half m) { |
| 10 | float8 fp32x8; |
| 11 | fp32x8.s0 = (float)((q4x8.s0 & 0x000F) * s + m); |
| 12 | fp32x8.s1 = (float)(((q4x8.s0 & 0x00F0) >> 4) * s + m); |
| 13 | fp32x8.s2 = (float)(((q4x8.s0 & 0x0F00) >> 8) * s + m); |
| 14 | fp32x8.s3 = (float)(((q4x8.s0 & 0xF000) >> 12) * s + m); |
| 15 | fp32x8.s4 = (float)((q4x8.s1 & 0x000F) * s + m); |
| 16 | fp32x8.s5 = (float)(((q4x8.s1 & 0x00F0) >> 4) * s + m); |
| 17 | fp32x8.s6 = (float)(((q4x8.s1 & 0x0F00) >> 8) * s + m); |
| 18 | fp32x8.s7 = (float)(((q4x8.s1 & 0xF000) >> 12) * s + m); |
| 19 | return fp32x8; |
| 20 | } |
| 21 | |
| 22 | |
| 23 | __attribute__((qcom_reqd_sub_group_size("half"))) |
| 24 | __kernel void kernel_gemv_moe_q4_1_f32_ns( |
| 25 | __global uint * src0_q, |
| 26 | __global half * src0_d, |
| 27 | __global half * src0_m, |
| 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 | uint expert_offset = expert_id * ne00 * ne01 / 32; |
| 52 | |
| 53 | __private float sum = 0.0f; // each thread calculate partial sum of one output |
| 54 | |
| 55 | // loop along ne00 in block granularity, skip 4 blocks every iter |
| 56 | for (uint ib00 = sgid; ib00 < (ne00 / QK_Q4_1); ib00 += N_SIMDGROUP) { |
| 57 | |
| 58 | // load one block of q |
| 59 | uint4 regQ; |
| 60 | uint block_offset = expert_offset * 4 + ib00 * ne01 * 4 + i01; |
| 61 | |
| 62 | regQ.s0 = src0_q[block_offset]; |
| 63 | regQ.s1 = src0_q[block_offset + ne01]; |
| 64 | regQ.s2 = src0_q[block_offset + ne01 * 2]; |
| 65 | regQ.s3 = src0_q[block_offset + ne01 * 3]; |
| 66 | |
| 67 | uint offset = i11 * ne00 / 4 + ib00 * 8; |
| 68 | |
| 69 | half regM = src0_m[ib00 * ne01 + i01 + expert_offset]; |
| 70 | half regS = src0_d[ib00 * ne01 + i01 + expert_offset]; |
| 71 | |
| 72 | float8 fp32x8 = q4_1_to_fp32_packed8(as_ushort2(regQ.s0), regS, regM); |
| 73 | |
| 74 | float4 shared_y4; |
| 75 | shared_y4 = read_imagef(src1, (offset + 0)); |
| 76 | float4 acc = shared_y4 * fp32x8.lo; |
| 77 | |
| 78 | shared_y4 = read_imagef(src1, (offset + 1)); |
| 79 | acc += shared_y4 * fp32x8.hi; |
| 80 | |
| 81 | fp32x8 = q4_1_to_fp32_packed8(as_ushort2(regQ.s1), regS, regM); |
| 82 | |
| 83 | shared_y4 = read_imagef(src1, (offset + 2)); |
| 84 | acc += shared_y4 * fp32x8.lo; |
| 85 | |
| 86 | shared_y4 = read_imagef(src1, (offset + 3)); |
| 87 | acc += shared_y4 * fp32x8.hi; |
| 88 | |
| 89 | |
| 90 | fp32x8 = q4_1_to_fp32_packed8(as_ushort2(regQ.s2), regS, regM); |
| 91 | |
| 92 | shared_y4 = read_imagef(src1, (offset + 4)); |
| 93 | acc += shared_y4 * fp32x8.lo; |
| 94 | |
| 95 | shared_y4 = read_imagef(src1, (offset + 5)); |
| 96 | acc += shared_y4 * fp32x8.hi; |
| 97 | |
| 98 | |
| 99 | fp32x8 = q4_1_to_fp32_packed8(as_ushort2(regQ.s3), regS, regM); |
| 100 | |
| 101 | shared_y4 = read_imagef(src1, (offset + 6)); |
| 102 | acc += shared_y4 * fp32x8.lo; |
| 103 | |
| 104 | shared_y4 = read_imagef(src1, (offset + 7)); |
| 105 | acc += shared_y4 * fp32x8.hi; |
| 106 | |
| 107 | sum += ((acc.s0 + acc.s1) + (acc.s2 + acc.s3)); |
| 108 | } |
| 109 | |
| 110 | // reduction in local memory, assumes #subgroups=4 |
| 111 | __local float reduceLM[SIMDGROUP_WIDTH * (N_SIMDGROUP - 1)]; |
| 112 | if (sgid == 1) reduceLM[SIMDGROUP_WIDTH * 0 + slid] = sum; |
| 113 | if (sgid == 2) reduceLM[SIMDGROUP_WIDTH * 1 + slid] = sum; |
| 114 | if (sgid == 3) reduceLM[SIMDGROUP_WIDTH * 2 + slid] = sum; |
| 115 | barrier(CLK_LOCAL_MEM_FENCE); |
| 116 | if (sgid == 0) sum += reduceLM[SIMDGROUP_WIDTH * 0 + slid]; |
| 117 | if (sgid == 0) sum += reduceLM[SIMDGROUP_WIDTH * 1 + slid]; |
| 118 | if (sgid == 0) sum += reduceLM[SIMDGROUP_WIDTH * 2 + slid]; |
| 119 | |
| 120 | // 1 outputs per thread in subgroup 0 |
| 121 | if (sgid == 0) { |
| 122 | dst = dst + (offsetd >> 2); |
| 123 | dst[i01 + i20 * ne01] = sum; |
| 124 | } |
| 125 | |
| 126 | } |