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

169 lines · 6948 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// Expand the 4 nibbles held in the low 16 bits of `u` into 4 bytes (one nibble
11// per byte, value 0..15), packed for the int8 dp4a. The -8 zero-point is applied
12// in the epilogue via the activation sum term (cheaper than biasing every byte).
13#define EXP4(u) ( ((uint)((u) & 0x000Fu)) | \
14 (((uint)((u) & 0x00F0u)) << 4) | \
15 (((uint)((u) & 0x0F00u)) << 8) | \
16 (((uint)((u) & 0xF000u)) << 12) )
17
18// One token's dp4a dot (8 uints = 32 K elems) + q4_0 scale/zero-point epilogue.
19#define MOE_Q40_DP4A_T(t) do { \
20 uint4 a0 = vload4(0, &sh_qa[t][0]); \
21 uint4 a1 = vload4(0, &sh_qa[t][4]); \
22 int raw = 0; \
23 raw = dot_acc_sat_4x8packed_ss_int(qw[0], a0.s0, raw); \
24 raw = dot_acc_sat_4x8packed_ss_int(qw[1], a0.s1, raw); \
25 raw = dot_acc_sat_4x8packed_ss_int(qw[2], a0.s2, raw); \
26 raw = dot_acc_sat_4x8packed_ss_int(qw[3], a0.s3, raw); \
27 raw = dot_acc_sat_4x8packed_ss_int(qw[4], a1.s0, raw); \
28 raw = dot_acc_sat_4x8packed_ss_int(qw[5], a1.s1, raw); \
29 raw = dot_acc_sat_4x8packed_ss_int(qw[6], a1.s2, raw); \
30 raw = dot_acc_sat_4x8packed_ss_int(qw[7], a1.s3, raw); \
31 acc[t] += d_val * ((float)sh_d[t] * (float)raw - 8.0f * (float)sh_s[t]); \
32 } while (0)
33
34__attribute__((qcom_wave_pair_mode(1)))
35kernel void kernel_gemm_moe_q4_0_q8_1_dp4a(
36 __read_only image1d_buffer_t src0_q, // q4_0 weights (transposed, packed nibbles)
37 __global half * src0_d, // per-32-block scale
38 __global uint * src1_qa, // q8_1 activations: int8 quants (as uint, 4/elem)
39 __global half * src1_da, // q8_1 per-block scale [tok_slot * ne00/32]
40 __global half * src1_sa, // q8_1 per-block sum*d [tok_slot * ne00/32]
41 __global uint * src2, // post-router (orig out positions)
42 __global ushort * src2_emap,// tile -> expert id
43 __write_only image1d_buffer_t dst,
44 __global int * total_tiles,
45 uint ne00,
46 uint ne01,
47 int is_ragged // 1: compute only real tokens per tile
48) {
49 const uint block_id_m = get_global_id(1); // m_tile
50 const uint block_id_n = get_global_id(2); // n_tile
51
52 if (block_id_n >= total_tiles[0]) {
53 return;
54 }
55
56 const uint lid = get_local_id(0); // 0..63, == this WI's output row in the M-tile
57
58 const ushort expert_id = src2_emap[block_id_n];
59 const uint row = block_id_m * TILESIZE_M;
60 const uint col = block_id_n * TILESIZE_N;
61
62 const uint num_blocks = ne00 >> 5; // blocks-of-32 per token
63 const uint row_idx = row + lid;
64
65 const uint ne00_u = ne00 >> 2; // ne00 in uint (int8x4) units
66
67 __local uint sh_qa[TILESIZE_N][8]; // 32 tokens x 8 uints (32 int8) = 1 KiB
68 __local half sh_d[TILESIZE_N];
69 __local half sh_s[TILESIZE_N];
70
71 // Real-token count for this tile
72 __local uint sh_src2[TILESIZE_N];
73 __local int sh_nreal;
74 if (lid < TILESIZE_N) {
75 sh_src2[lid] = src2[col + lid];
76 }
77 barrier(CLK_LOCAL_MEM_FENCE);
78 if (lid == 0) {
79 int nr = TILESIZE_N;
80 if (is_ragged) {
81 nr = 0;
82 #pragma unroll
83 for (int t = 0; t < TILESIZE_N; ++t) {
84 if (sh_src2[t] != 0xFFFFFFFFu) ++nr;
85 }
86 }
87 sh_nreal = nr;
88 }
89 barrier(CLK_LOCAL_MEM_FENCE);
90 const int n_real = sh_nreal;
91
92 float acc[TILESIZE_N];
93 #pragma unroll
94 for (int t = 0; t < TILESIZE_N; ++t) acc[t] = 0.0f;
95
96 for (uint step = 0; step < ne00; step += 32) {
97 const uint sub = step >> 5; // 32-block index along K
98
99 // per-32-block scale for this WI's row
100 const uint d_offset = row_idx + sub * ne01 + expert_id * num_blocks * ne01;
101 const float d_val = (float)src0_d[d_offset];
102
103 // repack this WI's 32 weight nibbles into 8 dp4a uints
104 const uint qoff0 = row + ((ne01 * step) >> 3) + ((expert_id * ne00 * ne01) >> 3);
105 const uint qoff1 = row + ((ne01 * (step + 16)) >> 3) + ((expert_id * ne00 * ne01) >> 3);
106 const uint r0 = read_imageui(src0_q, qoff0 + lid).x;
107 const uint r1 = read_imageui(src0_q, qoff0 + lid + ne01).x;
108 const uint r2 = read_imageui(src0_q, qoff1 + lid).x;
109 const uint r3 = read_imageui(src0_q, qoff1 + lid + ne01).x;
110 uint qw[8];
111 qw[0] = EXP4(r0); qw[1] = EXP4(r0 >> 16);
112 qw[2] = EXP4(r1); qw[3] = EXP4(r1 >> 16);
113 qw[4] = EXP4(r2); qw[5] = EXP4(r2 >> 16);
114 qw[6] = EXP4(r3); qw[7] = EXP4(r3 >> 16);
115
116 // cooperatively stage the n_real-token x 32-K int8 activations
117 // Stage each token's 8 activation uints as two 128-bit uint4 loads/stores.
118 const uint vlim = (uint)n_real * 2;
119 for (uint idx = lid; idx < vlim; idx += 64) {
120 const uint t = idx >> 1;
121 const uint h = (idx & 1) << 2; // 0 or 4
122 uint4 v = vload4(0, &src1_qa[(col + t) * ne00_u + (step >> 2) + h]);
123 vstore4(v, 0, &sh_qa[t][h]);
124 }
125 if (lid < (uint)n_real) {
126 sh_d[lid] = src1_da[(col + lid) * num_blocks + sub];
127 sh_s[lid] = src1_sa[(col + lid) * num_blocks + sub];
128 }
129 barrier(CLK_LOCAL_MEM_FENCE);
130
131 if (n_real == TILESIZE_N) {
132 #pragma unroll
133 for (int t = 0; t < TILESIZE_N; ++t) { MOE_Q40_DP4A_T(t); }
134 } else {
135 #pragma unroll 4
136 for (int t = 0; t < n_real; ++t) { MOE_Q40_DP4A_T(t); }
137 }
138 barrier(CLK_LOCAL_MEM_FENCE);
139 }
140
141 if (row_idx >= ne01) {
142 return;
143 }
144
145 // scatter results to original output rows (reuse sh_src2 from the top)
146 __local uint out_idx[TILESIZE_N];
147 if (lid < TILESIZE_N) {
148 uint idx = sh_src2[lid];
149 if (idx == 0xFFFFFFFF) {
150 idx = sh_src2[0];
151 }
152 out_idx[lid] = idx * ne01;
153 }
154 barrier(CLK_LOCAL_MEM_FENCE);
155
156 const uint m_offset = row + lid;
157 if (n_real == TILESIZE_N) {
158 #pragma unroll
159 for (int t = 1; t < TILESIZE_N; ++t) {
160 write_imagef(dst, out_idx[t] + m_offset, acc[t]);
161 }
162 barrier(CLK_GLOBAL_MEM_FENCE);
163 write_imagef(dst, out_idx[0] + m_offset, acc[0]);
164 } else {
165 for (int t = 0; t < n_real; ++t) {
166 write_imagef(dst, out_idx[t] + m_offset, acc[t]);
167 }
168 }
169}