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_0_f32_ns.cl

126 lines · 4465 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_0 32
6#define N_SIMDGROUP 4
7#define SIMDGROUP_WIDTH 64
8
9static inline float8 q5_0_to_fp32_packed8(ushort2 qs5x8, uchar qh5x8) {
10 float8 fp32x8;
11 fp32x8.s0 = (float)((( qs5x8.s0 & 0x000F) | (( qh5x8 & 0x01) << 4)) - 16);
12 fp32x8.s1 = (float)((((qs5x8.s0 & 0x00F0) >> 4 ) | (((qh5x8 >> 1) & 0x01) << 4)) - 16);
13 fp32x8.s2 = (float)((((qs5x8.s0 & 0x0F00) >> 8 ) | (((qh5x8 >> 2) & 0x01) << 4)) - 16);
14 fp32x8.s3 = (float)((((qs5x8.s0 & 0xF000) >> 12) | (((qh5x8 >> 3) & 0x01) << 4)) - 16);
15 fp32x8.s4 = (float)((( qs5x8.s1 & 0x000F) | (((qh5x8 >> 4) & 0x01) << 4)) - 16);
16 fp32x8.s5 = (float)((((qs5x8.s1 & 0x00F0) >> 4 ) | (((qh5x8 >> 5) & 0x01) << 4)) - 16);
17 fp32x8.s6 = (float)((((qs5x8.s1 & 0x0F00) >> 8 ) | (((qh5x8 >> 6) & 0x01) << 4)) - 16);
18 fp32x8.s7 = (float)((((qs5x8.s1 & 0xF000) >> 12) | (((qh5x8 >> 7) & 0x01) << 4)) - 16);
19 return fp32x8;
20}
21
22
23__attribute__((qcom_reqd_sub_group_size("half")))
24__kernel void kernel_gemv_moe_q5_0_f32_ns(
25 __global uint * src0_qs,
26 __global uint * src0_qh,
27 __global half * src0_d,
28 __read_only image1d_buffer_t src1,
29 __global uint * src2,
30 __global float * dst,
31 ulong offsetd,
32 uint ne00,
33 uint ne01,
34 uint 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_Q5_0); 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_qs[block_offset];
63 regQ.s1 = src0_qs[block_offset + ne01];
64 regQ.s2 = src0_qs[block_offset + ne01 * 2];
65 regQ.s3 = src0_qs[block_offset + ne01 * 3];
66
67 uint offset = i11 * ne00 / 4 + ib00 * 8;
68
69 uchar4 regQh = as_uchar4(src0_qh[ib00 * ne01 + i01 + expert_offset]);
70 half regS = src0_d[ib00 * ne01 + i01 + expert_offset];
71
72 float8 fp32x8 = q5_0_to_fp32_packed8(as_ushort2(regQ.s0), regQh.s0);
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 = q5_0_to_fp32_packed8(as_ushort2(regQ.s1), regQh.s1);
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 = q5_0_to_fp32_packed8(as_ushort2(regQ.s2), regQh.s2);
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 = q5_0_to_fp32_packed8(as_ushort2(regQ.s3), regQh.s3);
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 += (float)(regS) * ((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}