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

190 lines · 7981 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_M 64
8#define TILESIZE_N 32
9
10// 2*mxfp4_value as signed int8, packed 4 codes per uint. Divergent nibble
11// lookups read a __constant *uint* array + shift, never a byte array
12// (byte-indexed __constant loads serialize on Adreno and are far slower).
13// idx 0-3: 0, 1, 2, 3 = 0x03020100
14// idx 4-7: 4, 6, 8, 12 = 0x0C080604
15// idx 8-11: 0, -1, -2, -3 = 0xFDFEFF00 (-1=0xFF,-2=0xFE,-3=0xFD)
16// idx 12-15:-4, -6, -8,-12 = 0xF4F8FAFC (-4=0xFC,-6=0xFA,-8=0xF8,-12=0xF4)
17__constant uint mxfp4_i8x4[4] = {
18 0x03020100u, 0x0C080604u, 0xFDFEFF00u, 0xF4F8FAFCu
19};
20inline uint mxfp4_code(uint n) {
21 return (mxfp4_i8x4[n >> 2] >> ((n & 3u) * 8u)) & 0xFFu;
22}
23// 4 nibbles in the low 16 bits of u -> 4 codebook int8, packed for dp4a.
24inline uint mxfp4_pack(ushort u) {
25 return mxfp4_code((uint)( u & 0xF))
26 | (mxfp4_code((uint)((u >> 4) & 0xF)) << 8)
27 | (mxfp4_code((uint)((u >> 8) & 0xF)) << 16)
28 | (mxfp4_code((uint)((u >> 12) & 0xF)) << 24);
29}
30
31static inline float e8m0_to_fp32(uchar x) {
32 int bits;
33 bits = (x == 0) ? 0x00400000 : ((uint) x << 23);
34 return as_float(bits);
35}
36
37// One token's dp4a dot (8 uints = 32 K elems) + mxfp4 block-scale epilogue.
38// blk_scale already carries the 0.5 factor (== 0.5 * 2^e).
39#define MOE_MXFP4_DP4A_T(t) do { \
40 uint4 a0 = vload4(0, &sh_qa[t][0]); \
41 uint4 a1 = vload4(0, &sh_qa[t][4]); \
42 int raw = 0; \
43 raw = dot_acc_sat_4x8packed_ss_int(qw[0], a0.s0, raw); \
44 raw = dot_acc_sat_4x8packed_ss_int(qw[1], a0.s1, raw); \
45 raw = dot_acc_sat_4x8packed_ss_int(qw[2], a0.s2, raw); \
46 raw = dot_acc_sat_4x8packed_ss_int(qw[3], a0.s3, raw); \
47 raw = dot_acc_sat_4x8packed_ss_int(qw[4], a1.s0, raw); \
48 raw = dot_acc_sat_4x8packed_ss_int(qw[5], a1.s1, raw); \
49 raw = dot_acc_sat_4x8packed_ss_int(qw[6], a1.s2, raw); \
50 raw = dot_acc_sat_4x8packed_ss_int(qw[7], a1.s3, raw); \
51 acc[t] += blk_scale * (float)sh_d[t] * (float)raw; \
52 } while (0)
53
54__attribute__((qcom_wave_pair_mode(1)))
55kernel void kernel_gemm_moe_mxfp4_q8_1_dp4a(
56 __read_only image1d_buffer_t src0_q, // mxfp4 codes (transposed, packed nibbles)
57 __global uchar * src0_e, // e8m0 per-32-block scale
58 __global uint * src1_qa, // q8_1 activations: int8 quants (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); // m_tile
69 const uint block_id_n = get_global_id(2); // n_tile
70
71 if (block_id_n >= total_tiles[0]) {
72 return;
73 }
74
75 const uint lid = get_local_id(0); // 0..63, == this WI's output row in the M-tile
76
77 const ushort expert_id = src2_emap[block_id_n];
78 const uint row = block_id_m * TILESIZE_M;
79 const uint col = block_id_n * TILESIZE_N;
80
81 const uint num_blocks = ne00 >> 5; // blocks-of-32 per token
82 const uint row_idx = row + lid;
83
84 const uint ne00_u = ne00 >> 2; // ne00 in uint (int8x4) units
85
86 __local uint sh_qa[TILESIZE_N][8]; // 32 tokens x 8 uints (32 int8) = 1 KiB
87 __local half sh_d[TILESIZE_N];
88
89 // Real token count for this tile.
90 // Real tokens are packed contiguously at the tile start; padded slots hold
91 // 0xFFFFFFFF (only the last tile of each expert is partial). is_ragged skips
92 // the dp4a/staging/scatter for padded slots; is_ragged==0 forces n_real=32.
93 __local uint sh_src2[TILESIZE_N];
94 __local int sh_nreal;
95 if (lid < TILESIZE_N) {
96 sh_src2[lid] = src2[col + lid];
97 }
98 barrier(CLK_LOCAL_MEM_FENCE);
99 if (lid == 0) {
100 int nr = TILESIZE_N;
101 if (is_ragged) {
102 nr = 0;
103 #pragma unroll
104 for (int t = 0; t < TILESIZE_N; ++t) {
105 if (sh_src2[t] != 0xFFFFFFFFu) ++nr;
106 }
107 }
108 sh_nreal = nr;
109 }
110 barrier(CLK_LOCAL_MEM_FENCE);
111 const int n_real = sh_nreal;
112
113 float acc[TILESIZE_N];
114 #pragma unroll
115 for (int t = 0; t < TILESIZE_N; ++t) acc[t] = 0.0f;
116
117 for (uint step = 0; step < ne00; step += 32) {
118 const uint sub = step >> 5; // 32-block index along K
119
120 // e8m0 block scale for this WI's row, this 32-block (folded x0.5)
121 const uint e_offset = row_idx + sub * ne01 + expert_id * num_blocks * ne01;
122 const float blk_scale = 0.5f * e8m0_to_fp32(src0_e[e_offset]);
123
124 // repack this WI's 32 weight nibbles into 8 dp4a uints
125 const uint qoff0 = row + ((ne01 * step) >> 3) + ((expert_id * ne00 * ne01) >> 3);
126 const uint qoff1 = row + ((ne01 * (step + 16)) >> 3) + ((expert_id * ne00 * ne01) >> 3);
127 const uint r0 = read_imageui(src0_q, qoff0 + lid).x;
128 const uint r1 = read_imageui(src0_q, qoff0 + lid + ne01).x;
129 const uint r2 = read_imageui(src0_q, qoff1 + lid).x;
130 const uint r3 = read_imageui(src0_q, qoff1 + lid + ne01).x;
131 uint qw[8];
132 qw[0] = mxfp4_pack((ushort)(r0)); qw[1] = mxfp4_pack((ushort)(r0 >> 16));
133 qw[2] = mxfp4_pack((ushort)(r1)); qw[3] = mxfp4_pack((ushort)(r1 >> 16));
134 qw[4] = mxfp4_pack((ushort)(r2)); qw[5] = mxfp4_pack((ushort)(r2 >> 16));
135 qw[6] = mxfp4_pack((ushort)(r3)); qw[7] = mxfp4_pack((ushort)(r3 >> 16));
136
137 // cooperatively stage the n_real-token x 32-K int8 activations
138 // Stage each token's 8 activation uints as two 128-bit uint4 loads/stores.
139 const uint vlim = (uint)n_real * 2;
140 for (uint idx = lid; idx < vlim; idx += 64) {
141 const uint t = idx >> 1;
142 const uint h = (idx & 1) << 2; // 0 or 4
143 uint4 v = vload4(0, &src1_qa[(col + t) * ne00_u + (step >> 2) + h]);
144 vstore4(v, 0, &sh_qa[t][h]);
145 }
146 if (lid < (uint)n_real) {
147 sh_d[lid] = src1_da[(col + lid) * num_blocks + sub];
148 }
149 barrier(CLK_LOCAL_MEM_FENCE);
150
151 // Full tiles keep the fully-unrolled 32-wide loop; partial tiles run only n_real
152 if (n_real == TILESIZE_N) {
153 #pragma unroll
154 for (int t = 0; t < TILESIZE_N; ++t) { MOE_MXFP4_DP4A_T(t); }
155 } else {
156 #pragma unroll 4
157 for (int t = 0; t < n_real; ++t) { MOE_MXFP4_DP4A_T(t); }
158 }
159 barrier(CLK_LOCAL_MEM_FENCE);
160 }
161
162 if (row_idx >= ne01) {
163 return;
164 }
165
166 // scatter results to original output rows (reuse sh_src2 from the top)
167 __local uint out_idx[TILESIZE_N];
168 if (lid < TILESIZE_N) {
169 uint idx = sh_src2[lid];
170 if (idx == 0xFFFFFFFF) {
171 idx = sh_src2[0];
172 }
173 out_idx[lid] = idx * ne01;
174 }
175 barrier(CLK_LOCAL_MEM_FENCE);
176
177 const uint m_offset = row + lid;
178 if (n_real == TILESIZE_N) {
179 #pragma unroll
180 for (int t = 1; t < TILESIZE_N; ++t) {
181 write_imagef(dst, out_idx[t] + m_offset, acc[t]);
182 }
183 barrier(CLK_GLOBAL_MEM_FENCE);
184 write_imagef(dst, out_idx[0] + m_offset, acc[0]);
185 } else {
186 for (int t = 0; t < n_real; ++t) {
187 write_imagef(dst, out_idx[t] + m_offset, acc[t]);
188 }
189 }
190}