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 / gemm_moe_q6_k_q8_1_dp4a.cl

200 lines · 8307 bytes Raw Blame History
1#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2#pragma OPENCL EXTENSION cl_khr_subgroups : enable
3#ifdef cl_khr_integer_dot_product
4#pragma OPENCL EXTENSION cl_khr_integer_dot_product : enable
5#endif
6
7#define TILESIZE_N 32
8#define QK_K 256
9
10// 4 nibbles in the low 16 bits of `u` -> 4 bytes (value 0..15, in bits 0-3).
11#define EXP4(u) ( ((uint)((u) & 0x000Fu)) | \
12 (((uint)((u) & 0x00F0u)) << 4) | \
13 (((uint)((u) & 0x0F00u)) << 8) | \
14 (((uint)((u) & 0xF000u)) << 12) )
15
16// 4 2-bit highs in byte `b` (8 bits) -> 4 bytes, value 0..3 in bits 4-5
17// (pre-multiplied by 16 so it ORs with the EXP4 nibble to form q6 in 0..63).
18#define EXP2(b) ( (((uint)((b) & 0x03u)) << 4) | \
19 (((uint)((b) & 0x0Cu)) << 10) | \
20 (((uint)((b) & 0x30u)) << 16) | \
21 (((uint)((b) & 0xC0u)) << 22) )
22
23// q6 (0..63, bits 0-5 of each byte) -> (q6-32) as a signed int8 per byte.
24// Flipping bit5 subtracts 32 in 6-bit two's complement; then replicate bit5
25// into bits 6-7 to sign-extend to int8. Per-byte, no inter-byte carry.
26inline uint SIGN6(uint q6p) {
27 uint x = q6p ^ 0x20202020u;
28 uint s = x & 0x20202020u;
29 return x | (s << 1) | (s << 2);
30}
31
32inline int dp4a_q6(uint qw0, uint qw1, uint qw2, uint qw3,
33 uint a0, uint a1, uint a2, uint a3) {
34 int raw = 0;
35 raw = dot_acc_sat_4x8packed_ss_int(qw0, a0, raw);
36 raw = dot_acc_sat_4x8packed_ss_int(qw1, a1, raw);
37 raw = dot_acc_sat_4x8packed_ss_int(qw2, a2, raw);
38 raw = dot_acc_sat_4x8packed_ss_int(qw3, a3, raw);
39 return raw;
40}
41
42// One token's q6_K dp4a dot (two halves, per-16 scales) + epilogue into acc[t].
43#define MOE_Q6K_DP4A_T(t) do { \
44 uint4 a0 = vload4(0, &sh_qa[t][0]); \
45 uint4 a1 = vload4(0, &sh_qa[t][4]); \
46 const int raw1 = dp4a_q6(qw[0], qw[1], qw[2], qw[3], a0.s0, a0.s1, a0.s2, a0.s3); \
47 const int raw2 = dp4a_q6(qw[4], qw[5], qw[6], qw[7], a1.s0, a1.s1, a1.s2, a1.s3); \
48 const float a_d = (float)sh_d[t]; \
49 acc[t] += scale0 * a_d * (float)raw1 + scale1 * a_d * (float)raw2; \
50 } while (0)
51
52__attribute__((qcom_wave_pair_mode(1)))
53kernel void kernel_gemm_moe_q6_k_q8_1_dp4a(
54 __read_only image1d_buffer_t src0_ql, // q6_K low nibbles (image, q4_K-style layout)
55 __global uint * src0_qh, // q6_K high 2-bit (16 elems/uint)
56 __global char * src0_s, // int8 scales (one per 16 elems)
57 __global half * src0_d, // per-superblock scale
58 __global uint * src1_qa, // q8_1 activations int8 (as uint, 4/elem)
59 __global half * src1_da, // q8_1 per-block scale [tok_slot * ne00/32]
60 __global uint * src2, // post-router (orig out positions)
61 __global ushort * src2_emap, // tile -> expert id
62 __write_only image1d_buffer_t dst,
63 __global int * total_tiles,
64 uint ne00,
65 uint ne01,
66 int is_ragged // 1: compute only real tokens per tile
67) {
68 const uint block_id_m = get_global_id(1);
69 const uint block_id_n = get_global_id(2);
70
71 if (block_id_n >= total_tiles[0]) {
72 return;
73 }
74
75 const uint lid = get_local_id(0); // 0..63 -> row within M-tile
76
77 const ushort expert_id = src2_emap[block_id_n];
78 const uint row = block_id_m * 64;
79 const uint col = block_id_n * TILESIZE_N;
80
81 const uint num_superblocks = ne00 / QK_K;
82 const uint scales_per_row = num_superblocks * 16;
83 const uint row_idx = row + lid;
84
85 const uint ne00_u = ne00 >> 2;
86 const uint ne00_b = ne00 >> 5;
87
88 __local uint sh_qa[TILESIZE_N][8];
89 __local half sh_d[TILESIZE_N];
90
91 // Real token count for this tile
92 __local uint sh_src2[TILESIZE_N];
93 __local int sh_nreal;
94 if (lid < TILESIZE_N) {
95 sh_src2[lid] = src2[col + lid];
96 }
97 barrier(CLK_LOCAL_MEM_FENCE);
98 if (lid == 0) {
99 int nr = TILESIZE_N;
100 if (is_ragged) {
101 nr = 0;
102 #pragma unroll
103 for (int t = 0; t < TILESIZE_N; ++t) {
104 if (sh_src2[t] != 0xFFFFFFFFu) ++nr;
105 }
106 }
107 sh_nreal = nr;
108 }
109 barrier(CLK_LOCAL_MEM_FENCE);
110 const int n_real = sh_nreal;
111
112 float acc[TILESIZE_N];
113 #pragma unroll
114 for (int t = 0; t < TILESIZE_N; ++t) acc[t] = 0.0f;
115
116 for (uint step = 0; step < ne00; step += 32) {
117 const uint sub = step >> 5;
118 const uint sb = sub >> 3;
119 const uint j = sub & 7;
120
121 const float d_val = (float)src0_d[row + sb * ne01 + expert_id * num_superblocks * ne01 + lid];
122 global const char * sc = src0_s + (expert_id * ne01 + row_idx) * scales_per_row + sb * 16;
123 const float scale0 = d_val * (float)sc[j * 2];
124 const float scale1 = d_val * (float)sc[j * 2 + 1];
125
126 // high bits: one uint covers 16 elems; first/second 16 of this 32-block
127 const uint qh_base = row + (sub * 2) * ne01 + expert_id * (num_superblocks * 16) * ne01 + lid;
128 const uint qh1 = src0_qh[qh_base];
129 const uint qh2 = src0_qh[qh_base + ne01];
130
131 // low nibbles: same image layout as q4_K (8 ushorts over the 32 K)
132 const uint qoff0 = row + ((ne01 * step) >> 3) + ((expert_id * ne00 * ne01) >> 3);
133 const uint qoff1 = row + ((ne01 * (step + 16)) >> 3) + ((expert_id * ne00 * ne01) >> 3);
134 const uint r0 = read_imageui(src0_ql, qoff0 + lid).x;
135 const uint r1 = read_imageui(src0_ql, qoff0 + lid + ne01).x;
136 const uint r2 = read_imageui(src0_ql, qoff1 + lid).x;
137 const uint r3 = read_imageui(src0_ql, qoff1 + lid + ne01).x;
138
139 uint qw[8];
140 qw[0] = SIGN6(EXP4(r0) | EXP2((qh1) & 0xFFu));
141 qw[1] = SIGN6(EXP4(r0 >> 16) | EXP2((qh1 >> 8) & 0xFFu));
142 qw[2] = SIGN6(EXP4(r1) | EXP2((qh1 >> 16) & 0xFFu));
143 qw[3] = SIGN6(EXP4(r1 >> 16) | EXP2((qh1 >> 24) & 0xFFu));
144 qw[4] = SIGN6(EXP4(r2) | EXP2((qh2) & 0xFFu));
145 qw[5] = SIGN6(EXP4(r2 >> 16) | EXP2((qh2 >> 8) & 0xFFu));
146 qw[6] = SIGN6(EXP4(r3) | EXP2((qh2 >> 16) & 0xFFu));
147 qw[7] = SIGN6(EXP4(r3 >> 16) | EXP2((qh2 >> 24) & 0xFFu));
148
149 // Stage each token's 8 activation uints as two 128-bit uint4 loads/stores.
150 const uint vlim = (uint)n_real * 2;
151 for (uint idx = lid; idx < vlim; idx += 64) {
152 const uint t = idx >> 1;
153 const uint h = (idx & 1) << 2; // 0 or 4
154 uint4 v = vload4(0, &src1_qa[(col + t) * ne00_u + (step >> 2) + h]);
155 vstore4(v, 0, &sh_qa[t][h]);
156 }
157 if (lid < (uint)n_real) {
158 sh_d[lid] = src1_da[(col + lid) * ne00_b + sub];
159 }
160 barrier(CLK_LOCAL_MEM_FENCE);
161
162 // Full tiles keep the fully-unrolled 32-wide loop; partial tiles run n_real.
163 if (n_real == TILESIZE_N) {
164 #pragma unroll
165 for (int t = 0; t < TILESIZE_N; ++t) { MOE_Q6K_DP4A_T(t); }
166 } else {
167 #pragma unroll 4
168 for (int t = 0; t < n_real; ++t) { MOE_Q6K_DP4A_T(t); }
169 }
170 barrier(CLK_LOCAL_MEM_FENCE);
171 }
172
173 if (row_idx >= ne01) {
174 return;
175 }
176
177 __local uint out_idx[TILESIZE_N];
178 if (lid < TILESIZE_N) {
179 uint idx = sh_src2[lid];
180 if (idx == 0xFFFFFFFF) {
181 idx = sh_src2[0];
182 }
183 out_idx[lid] = idx * ne01;
184 }
185 barrier(CLK_LOCAL_MEM_FENCE);
186
187 const uint m_offset = row + lid;
188 if (n_real == TILESIZE_N) {
189 #pragma unroll
190 for (int t = 1; t < TILESIZE_N; ++t) {
191 write_imagef(dst, out_idx[t] + m_offset, acc[t]);
192 }
193 barrier(CLK_GLOBAL_MEM_FENCE);
194 write_imagef(dst, out_idx[0] + m_offset, acc[0]);
195 } else {
196 for (int t = 0; t < n_real; ++t) {
197 write_imagef(dst, out_idx[t] + m_offset, acc[t]);
198 }
199 }
200}