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

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