interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-opencl / kernels / gemv_moe_q5_1_f32_ns.cl

128 lines · 4641 bytes Raw Blame History
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_Q5_1 32
6#define N_SIMDGROUP 4
7#define SIMDGROUP_WIDTH 64
8
9static inline float8 q5_1_to_fp32_packed8(ushort2 qs5x8, uchar qh5x8, half s, half m) {
10 float8 fp32x8;
11 fp32x8.s0 = (float)((( qs5x8.s0 & 0x000F) | (( qh5x8 & 0x01) << 4)) * s + m);
12 fp32x8.s1 = (float)((((qs5x8.s0 & 0x00F0) >> 4 ) | (((qh5x8 >> 1) & 0x01) << 4)) * s + m);
13 fp32x8.s2 = (float)((((qs5x8.s0 & 0x0F00) >> 8 ) | (((qh5x8 >> 2) & 0x01) << 4)) * s + m);
14 fp32x8.s3 = (float)((((qs5x8.s0 & 0xF000) >> 12) | (((qh5x8 >> 3) & 0x01) << 4)) * s + m);
15 fp32x8.s4 = (float)((( qs5x8.s1 & 0x000F) | (((qh5x8 >> 4) & 0x01) << 4)) * s + m);
16 fp32x8.s5 = (float)((((qs5x8.s1 & 0x00F0) >> 4 ) | (((qh5x8 >> 5) & 0x01) << 4)) * s + m);
17 fp32x8.s6 = (float)((((qs5x8.s1 & 0x0F00) >> 8 ) | (((qh5x8 >> 6) & 0x01) << 4)) * s + m);
18 fp32x8.s7 = (float)((((qs5x8.s1 & 0xF000) >> 12) | (((qh5x8 >> 7) & 0x01) << 4)) * s + m);
19 return fp32x8;
20}
21
22
23__attribute__((qcom_reqd_sub_group_size("half")))
24__kernel void kernel_gemv_moe_q5_1_f32_ns(
25 __global uint * src0_qs,
26 __global uint * src0_qh,
27 __global half * src0_d,
28 __global half * src0_m,
29 __read_only image1d_buffer_t src1,
30 __global uint * src2,
31 __global float * dst,
32 ulong offsetd,
33 uint ne00,
34 uint ne01,
35 uint ne11
36) {
37 uint i01 = get_global_id(0);
38 uint i20 = get_global_id(2);
39 uint sgid = get_local_id(1);
40 uint slid = get_sub_group_local_id();
41
42 if (i01 >= ne01) {
43 return;
44 }
45
46 uint i11 = i20 % ne11;
47
48 uint expert_id = src2[i20];
49 if ((int)expert_id == -1) { // skipped slot, the dst row is zeroed by kernel_moe_zero_dst
50 return;
51 }
52 uint expert_offset = expert_id * ne00 * ne01 / 32;
53
54 __private float sum = 0.0f; // each thread calculate partial sum of one output
55
56 // loop along ne00 in block granularity, skip 4 blocks every iter
57 for (uint ib00 = sgid; ib00 < (ne00 / QK_Q5_1); ib00 += N_SIMDGROUP) {
58
59 // load one block of q
60 uint4 regQ;
61 uint block_offset = expert_offset * 4 + ib00 * ne01 * 4 + i01;
62
63 regQ.s0 = src0_qs[block_offset];
64 regQ.s1 = src0_qs[block_offset + ne01];
65 regQ.s2 = src0_qs[block_offset + ne01 * 2];
66 regQ.s3 = src0_qs[block_offset + ne01 * 3];
67
68 uint offset = i11 * ne00 / 4 + ib00 * 8;
69
70 uchar4 regQh = as_uchar4(src0_qh[ib00 * ne01 + i01 + expert_offset]);
71 half regM = src0_m[ib00 * ne01 + i01 + expert_offset];
72 half regS = src0_d[ib00 * ne01 + i01 + expert_offset];
73
74 float8 fp32x8 = q5_1_to_fp32_packed8(as_ushort2(regQ.s0), regQh.s0, regS, regM);
75
76 float4 shared_y4;
77 shared_y4 = read_imagef(src1, (offset + 0));
78 float4 acc = shared_y4 * fp32x8.lo;
79
80 shared_y4 = read_imagef(src1, (offset + 1));
81 acc += shared_y4 * fp32x8.hi;
82
83 fp32x8 = q5_1_to_fp32_packed8(as_ushort2(regQ.s1), regQh.s1, regS, regM);
84
85 shared_y4 = read_imagef(src1, (offset + 2));
86 acc += shared_y4 * fp32x8.lo;
87
88 shared_y4 = read_imagef(src1, (offset + 3));
89 acc += shared_y4 * fp32x8.hi;
90
91
92 fp32x8 = q5_1_to_fp32_packed8(as_ushort2(regQ.s2), regQh.s2, regS, regM);
93
94 shared_y4 = read_imagef(src1, (offset + 4));
95 acc += shared_y4 * fp32x8.lo;
96
97 shared_y4 = read_imagef(src1, (offset + 5));
98 acc += shared_y4 * fp32x8.hi;
99
100
101 fp32x8 = q5_1_to_fp32_packed8(as_ushort2(regQ.s3), regQh.s3, regS, regM);
102
103 shared_y4 = read_imagef(src1, (offset + 6));
104 acc += shared_y4 * fp32x8.lo;
105
106 shared_y4 = read_imagef(src1, (offset + 7));
107 acc += shared_y4 * fp32x8.hi;
108
109 sum += ((acc.s0 + acc.s1) + (acc.s2 + acc.s3));
110 }
111
112 // reduction in local memory, assumes #subgroups=4
113 __local float reduceLM[SIMDGROUP_WIDTH * (N_SIMDGROUP - 1)];
114 if (sgid == 1) reduceLM[SIMDGROUP_WIDTH * 0 + slid] = sum;
115 if (sgid == 2) reduceLM[SIMDGROUP_WIDTH * 1 + slid] = sum;
116 if (sgid == 3) reduceLM[SIMDGROUP_WIDTH * 2 + slid] = sum;
117 barrier(CLK_LOCAL_MEM_FENCE);
118 if (sgid == 0) sum += reduceLM[SIMDGROUP_WIDTH * 0 + slid];
119 if (sgid == 0) sum += reduceLM[SIMDGROUP_WIDTH * 1 + slid];
120 if (sgid == 0) sum += reduceLM[SIMDGROUP_WIDTH * 2 + slid];
121
122 // 1 outputs per thread in subgroup 0
123 if (sgid == 0) {
124 dst = dst + (offsetd >> 2);
125 dst[i01 + i20 * ne01] = sum;
126 }
127
128}