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

163 lines · 5853 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_K 256
6#define K_SCALE_SIZE 12
7#define N_SIMDGROUP 4
8#define SIMDGROUP_WIDTH 64
9
10inline void get_scale_min_k4(
11 int j,
12 global const uchar * q,
13 uchar * d,
14 uchar * m
15) {
16 if (j < 4) {
17 *d = q[j] & 63;
18 *m = q[j+4] & 63;
19 } else {
20 *d = (q[j+4] & 0x0F) | ((q[j-4] & 0xC0) >> 2);
21 *m = ((q[j+4] >> 4) & 0x0F) | ((q[j] & 0xC0) >> 2);
22 }
23}
24
25static inline float8 q5_k_to_fp32_packed8(ushort2 qs5x8, uchar qh5x8, half s, half m) {
26 float8 fp32x8;
27 fp32x8.s0 = (float)((( qs5x8.s0 & 0x000F) | (( qh5x8 & 0x01) << 4)) * s + m);
28 fp32x8.s1 = (float)((((qs5x8.s0 & 0x00F0) >> 4 ) | (((qh5x8 >> 1) & 0x01) << 4)) * s + m);
29 fp32x8.s2 = (float)((((qs5x8.s0 & 0x0F00) >> 8 ) | (((qh5x8 >> 2) & 0x01) << 4)) * s + m);
30 fp32x8.s3 = (float)((((qs5x8.s0 & 0xF000) >> 12) | (((qh5x8 >> 3) & 0x01) << 4)) * s + m);
31 fp32x8.s4 = (float)((( qs5x8.s1 & 0x000F) | (((qh5x8 >> 4) & 0x01) << 4)) * s + m);
32 fp32x8.s5 = (float)((((qs5x8.s1 & 0x00F0) >> 4 ) | (((qh5x8 >> 5) & 0x01) << 4)) * s + m);
33 fp32x8.s6 = (float)((((qs5x8.s1 & 0x0F00) >> 8 ) | (((qh5x8 >> 6) & 0x01) << 4)) * s + m);
34 fp32x8.s7 = (float)((((qs5x8.s1 & 0xF000) >> 12) | (((qh5x8 >> 7) & 0x01) << 4)) * s + m);
35 return fp32x8;
36}
37
38__attribute__((qcom_reqd_sub_group_size("half")))
39__kernel void kernel_gemv_moe_q5_k_f32_ns(
40 __global uint * src0_q,
41 __global uint * src0_qh,
42 __global half * src0_d,
43 __global half * src0_dm,
44 __global uchar * src0_s,
45 __read_only image1d_buffer_t src1,
46 __global uint * src2,
47 __global float * dst,
48 ulong offsetd,
49 int ne00,
50 int ne01,
51 int ne11
52) {
53 uint i01 = get_global_id(0);
54 uint i20 = get_global_id(2);
55 uint sgid = get_local_id(1);
56 uint slid = get_sub_group_local_id();
57
58 if (i01 >= ne01) {
59 return;
60 }
61
62 uint i11 = i20 % ne11;
63
64 uint expert_id = src2[i20];
65 if ((int)expert_id == -1) { // skipped slot, the dst row is zeroed by kernel_moe_zero_dst
66 return;
67 }
68
69 int num_superblocks = ne00 / QK_K;
70 int num_subblocks = ne00 / 32;
71 int scales_per_row = num_superblocks * K_SCALE_SIZE;
72
73 // Expert offsets in the transposed noshuffle layout
74 uint expert_q_offset = expert_id * (ne00 / 8) * ne01;
75 uint expert_d_offset = expert_id * num_superblocks * ne01;
76
77 __private float sum = 0.0f;
78
79 // Loop over sub-blocks of 32 elements, N_SIMDGROUP sub-blocks per iter
80 for (uint ib = sgid; ib < num_subblocks; ib += N_SIMDGROUP) {
81 uint sb = ib / 8;
82 uint j = ib % 8;
83
84 // Load d and dmin for this super-block
85 half d_val = src0_d[expert_d_offset + sb * ne01 + i01];
86 half dm_val = src0_dm[expert_d_offset + sb * ne01 + i01];
87
88 // sub_block index = sb * 8 + j
89 uint expert_qh_offset = expert_id * num_superblocks * 8 * ne01;
90 uchar4 regQh = as_uchar4(src0_qh[expert_qh_offset + (sb * 8 + j) * ne01 + i01]);
91
92 // Load sub-block scale and min
93 global const uchar * sc = src0_s + (expert_id * ne01 + i01) * scales_per_row + sb * K_SCALE_SIZE;
94 uchar sv, mn;
95 get_scale_min_k4(j, sc, &sv, &mn);
96
97 float scale = (float)d_val * (float)sv;
98 float minv = -(float)dm_val * (float)mn;
99
100 // Load 4 uints of quants (32 nibbles = 32 elements)
101 uint q_base = expert_q_offset + ib * ne01 * 4 + i01;
102
103 uint4 regQ;
104 regQ.s0 = src0_q[q_base];
105 regQ.s1 = src0_q[q_base + ne01];
106 regQ.s2 = src0_q[q_base + ne01 * 2];
107 regQ.s3 = src0_q[q_base + ne01 * 3];
108
109 // Load activations: 32 floats = 8 float4s
110 uint y_offset = i11 * ne00 / 4 + ib * 8;
111
112 float8 fp32x8 = q5_k_to_fp32_packed8(as_ushort2(regQ.s0), regQh.s0, scale, minv);
113
114 float4 shared_y4;
115 shared_y4 = read_imagef(src1, (y_offset + 0));
116 float4 acc = shared_y4 * fp32x8.lo;
117
118 shared_y4 = read_imagef(src1, (y_offset + 1));
119 acc += shared_y4 * fp32x8.hi;
120
121 fp32x8 = q5_k_to_fp32_packed8(as_ushort2(regQ.s1), regQh.s1, scale, minv);
122
123 shared_y4 = read_imagef(src1, (y_offset + 2));
124 acc += shared_y4 * fp32x8.lo;
125
126 shared_y4 = read_imagef(src1, (y_offset + 3));
127 acc += shared_y4 * fp32x8.hi;
128
129 fp32x8 = q5_k_to_fp32_packed8(as_ushort2(regQ.s2), regQh.s2, scale, minv);
130
131 shared_y4 = read_imagef(src1, (y_offset + 4));
132 acc += shared_y4 * fp32x8.lo;
133
134 shared_y4 = read_imagef(src1, (y_offset + 5));
135 acc += shared_y4 * fp32x8.hi;
136
137 fp32x8 = q5_k_to_fp32_packed8(as_ushort2(regQ.s3), regQh.s3, scale, minv);
138
139 shared_y4 = read_imagef(src1, (y_offset + 6));
140 acc += shared_y4 * fp32x8.lo;
141
142 shared_y4 = read_imagef(src1, (y_offset + 7));
143 acc += shared_y4 * fp32x8.hi;
144
145 sum += ((acc.s0 + acc.s1) + (acc.s2 + acc.s3));
146 }
147
148 // reduction in local memory, assumes #subgroups=4
149 __local float reduceLM[SIMDGROUP_WIDTH * (N_SIMDGROUP - 1)];
150 if (sgid == 1) reduceLM[SIMDGROUP_WIDTH * 0 + slid] = sum;
151 if (sgid == 2) reduceLM[SIMDGROUP_WIDTH * 1 + slid] = sum;
152 if (sgid == 3) reduceLM[SIMDGROUP_WIDTH * 2 + slid] = sum;
153 barrier(CLK_LOCAL_MEM_FENCE);
154 if (sgid == 0) sum += reduceLM[SIMDGROUP_WIDTH * 0 + slid];
155 if (sgid == 0) sum += reduceLM[SIMDGROUP_WIDTH * 1 + slid];
156 if (sgid == 0) sum += reduceLM[SIMDGROUP_WIDTH * 2 + slid];
157
158 // 1 output per thread in subgroup 0
159 if (sgid == 0) {
160 dst = dst + (offsetd >> 2);
161 dst[i01 + i20 * ne01] = sum;
162 }
163}