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

221 lines · 10092 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_subgroup_uniform_load: enable
4#pragma OPENCL EXTENSION cl_qcom_subgroup_constant_load: enable
5#pragma OPENCL EXTENSION cl_qcom_extra_vector_types : enable
6
7#define TILESIZE_K 16
8#define TILESIZE_M 64
9#define TILESIZE_N 32
10
11// q8_0: 16 signed int8 weights (one uint4 = 16 chars) -> half16, scaled.
12#define dequantize_q8_0(q4, a_f16, scale) \
13 a_f16 = convert_half16(as_char16(q4)) * scale;
14
15#define dotx16_reduce8(a_reg, b_lm, c_reg, lm_offset) \
16 acc.s0 = dot(a_reg.s0123, b_lm[lm_offset + 0]); \
17 acc.s1 = dot(a_reg.s0123, b_lm[lm_offset + 1]); \
18 acc.s2 = dot(a_reg.s0123, b_lm[lm_offset + 2]); \
19 acc.s3 = dot(a_reg.s0123, b_lm[lm_offset + 3]); \
20 acc.s4 = dot(a_reg.s0123, b_lm[lm_offset + 4]); \
21 acc.s5 = dot(a_reg.s0123, b_lm[lm_offset + 5]); \
22 acc.s6 = dot(a_reg.s0123, b_lm[lm_offset + 6]); \
23 acc.s7 = dot(a_reg.s0123, b_lm[lm_offset + 7]); \
24 acc.s8 = dot(a_reg.s0123, b_lm[lm_offset + 8]); \
25 acc.s9 = dot(a_reg.s0123, b_lm[lm_offset + 9]); \
26 acc.sa = dot(a_reg.s0123, b_lm[lm_offset + 10]); \
27 acc.sb = dot(a_reg.s0123, b_lm[lm_offset + 11]); \
28 acc.sc = dot(a_reg.s0123, b_lm[lm_offset + 12]); \
29 acc.sd = dot(a_reg.s0123, b_lm[lm_offset + 13]); \
30 acc.se = dot(a_reg.s0123, b_lm[lm_offset + 14]); \
31 acc.sf = dot(a_reg.s0123, b_lm[lm_offset + 15]); \
32 acc.s0 += dot(a_reg.s4567, b_lm[lm_offset + 32]); \
33 acc.s1 += dot(a_reg.s4567, b_lm[lm_offset + 33]); \
34 acc.s2 += dot(a_reg.s4567, b_lm[lm_offset + 34]); \
35 acc.s3 += dot(a_reg.s4567, b_lm[lm_offset + 35]); \
36 acc.s4 += dot(a_reg.s4567, b_lm[lm_offset + 36]); \
37 acc.s5 += dot(a_reg.s4567, b_lm[lm_offset + 37]); \
38 acc.s6 += dot(a_reg.s4567, b_lm[lm_offset + 38]); \
39 acc.s7 += dot(a_reg.s4567, b_lm[lm_offset + 39]); \
40 acc.s8 += dot(a_reg.s4567, b_lm[lm_offset + 40]); \
41 acc.s9 += dot(a_reg.s4567, b_lm[lm_offset + 41]); \
42 acc.sa += dot(a_reg.s4567, b_lm[lm_offset + 42]); \
43 acc.sb += dot(a_reg.s4567, b_lm[lm_offset + 43]); \
44 acc.sc += dot(a_reg.s4567, b_lm[lm_offset + 44]); \
45 acc.sd += dot(a_reg.s4567, b_lm[lm_offset + 45]); \
46 acc.se += dot(a_reg.s4567, b_lm[lm_offset + 46]); \
47 acc.sf += dot(a_reg.s4567, b_lm[lm_offset + 47]); \
48 c_reg.lo += convert_float8(acc.lo); \
49 c_reg.hi += convert_float8(acc.hi); \
50 acc.s0 = dot(a_reg.s89ab, b_lm[lm_offset + 64]); \
51 acc.s1 = dot(a_reg.s89ab, b_lm[lm_offset + 65]); \
52 acc.s2 = dot(a_reg.s89ab, b_lm[lm_offset + 66]); \
53 acc.s3 = dot(a_reg.s89ab, b_lm[lm_offset + 67]); \
54 acc.s4 = dot(a_reg.s89ab, b_lm[lm_offset + 68]); \
55 acc.s5 = dot(a_reg.s89ab, b_lm[lm_offset + 69]); \
56 acc.s6 = dot(a_reg.s89ab, b_lm[lm_offset + 70]); \
57 acc.s7 = dot(a_reg.s89ab, b_lm[lm_offset + 71]); \
58 acc.s8 = dot(a_reg.s89ab, b_lm[lm_offset + 72]); \
59 acc.s9 = dot(a_reg.s89ab, b_lm[lm_offset + 73]); \
60 acc.sa = dot(a_reg.s89ab, b_lm[lm_offset + 74]); \
61 acc.sb = dot(a_reg.s89ab, b_lm[lm_offset + 75]); \
62 acc.sc = dot(a_reg.s89ab, b_lm[lm_offset + 76]); \
63 acc.sd = dot(a_reg.s89ab, b_lm[lm_offset + 77]); \
64 acc.se = dot(a_reg.s89ab, b_lm[lm_offset + 78]); \
65 acc.sf = dot(a_reg.s89ab, b_lm[lm_offset + 79]); \
66 acc.s0 += dot(a_reg.scdef, b_lm[lm_offset + 96]); \
67 acc.s1 += dot(a_reg.scdef, b_lm[lm_offset + 97]); \
68 acc.s2 += dot(a_reg.scdef, b_lm[lm_offset + 98]); \
69 acc.s3 += dot(a_reg.scdef, b_lm[lm_offset + 99]); \
70 acc.s4 += dot(a_reg.scdef, b_lm[lm_offset + 100]); \
71 acc.s5 += dot(a_reg.scdef, b_lm[lm_offset + 101]); \
72 acc.s6 += dot(a_reg.scdef, b_lm[lm_offset + 102]); \
73 acc.s7 += dot(a_reg.scdef, b_lm[lm_offset + 103]); \
74 acc.s8 += dot(a_reg.scdef, b_lm[lm_offset + 104]); \
75 acc.s9 += dot(a_reg.scdef, b_lm[lm_offset + 105]); \
76 acc.sa += dot(a_reg.scdef, b_lm[lm_offset + 106]); \
77 acc.sb += dot(a_reg.scdef, b_lm[lm_offset + 107]); \
78 acc.sc += dot(a_reg.scdef, b_lm[lm_offset + 108]); \
79 acc.sd += dot(a_reg.scdef, b_lm[lm_offset + 109]); \
80 acc.se += dot(a_reg.scdef, b_lm[lm_offset + 110]); \
81 acc.sf += dot(a_reg.scdef, b_lm[lm_offset + 111]); \
82 c_reg.lo += convert_float8(acc.lo); \
83 c_reg.hi += convert_float8(acc.hi); \
84
85
86__attribute__((qcom_wave_pair_mode(1)))
87kernel void kernel_gemm_moe_q8_0_f32_ns(
88 __global char * src0_q, // flat q8_0 quants [n_expert*ne01*ne00]
89 __global half * src0_d, // flat q8_0 scales [n_expert*ne01*nb]
90 __read_only image1d_buffer_t src1, // reordered activations (f32)
91 __global uint * src2, // post-router out indices
92 __global ushort * src2_emap,// expert per tile
93 __write_only image1d_buffer_t dst,
94 __global int * total_tiles,
95 uint ne00,
96 uint ne01
97) {
98 uint block_id_m = get_global_id(1); // m_tile
99 uint block_id_n = get_global_id(2); // n_tile
100
101 if (block_id_n >= total_tiles[0]) {
102 return;
103 }
104
105 __private half16 reg_a;
106 __private float32 reg_c = (float32)(0);
107 __local half4 shared_b[128];
108
109 const ushort expert_id = src2_emap[block_id_n];
110
111 const uint row = block_id_m * TILESIZE_M;
112 const uint col = block_id_n * TILESIZE_N;
113
114 const uint nb = ne00 >> 5; // blocks per row (ne00/32)
115 const uint w_row = expert_id * ne01 + row + get_local_id(0); // this lane's output row
116 __global char * w_q = src0_q + (ulong)w_row * ne00; // char base for the row
117 __global half * w_d = src0_d + (ulong)w_row * nb; // scale base for the row
118
119 uint sub_block_id_m = get_local_id(0);
120 uint2 b_global_offset;
121 b_global_offset.x = ((sub_block_id_m & 3) << 2) + (sub_block_id_m >> 2) * ne00;
122 b_global_offset.y = b_global_offset.x + (16 * ne00);
123 uint2 b_local_offset;
124 b_local_offset.x = (sub_block_id_m & 3) * 32 + (sub_block_id_m >> 2);
125 b_local_offset.y = b_local_offset.x + 16;
126
127 // Loop along K axis, 32 elements per iteration, split into 2 sub-blocks.
128 for (uint step = 0; step < ne00; step += TILESIZE_K * 2) {
129 half s = w_d[step >> 5]; // one q8_0 scale per 32-element block
130
131 // First sub-block: 16 weights (16 chars = one uint4) at K=step
132 uint4 q8x16 = *((__global uint4 *)(w_q + step));
133
134 uint b_sub_offset = col * ne00 + step;
135 float8 bx8_f32;
136 bx8_f32.lo = read_imagef(src1, (b_sub_offset + b_global_offset.x) / 4);
137 bx8_f32.hi = read_imagef(src1, (b_sub_offset + b_global_offset.y) / 4);
138 half8 bx8_f16 = convert_half8(bx8_f32);
139 shared_b[b_local_offset.x] = bx8_f16.lo;
140 shared_b[b_local_offset.y] = bx8_f16.hi;
141
142 dequantize_q8_0(q8x16, reg_a, s);
143
144 sub_group_barrier(CLK_LOCAL_MEM_FENCE);
145
146 half16 acc;
147 dotx16_reduce8(reg_a, shared_b, reg_c.lo, 0);
148 dotx16_reduce8(reg_a, shared_b, reg_c.hi, 16);
149
150 // Second sub-block: next 16 weights at K=step+16
151 uint half_step = step + TILESIZE_K;
152 q8x16 = *((__global uint4 *)(w_q + half_step));
153 b_sub_offset = col * ne00 + half_step;
154
155 bx8_f32.lo = read_imagef(src1, (b_sub_offset + b_global_offset.x) / 4);
156 bx8_f32.hi = read_imagef(src1, (b_sub_offset + b_global_offset.y) / 4);
157 bx8_f16 = convert_half8(bx8_f32);
158 shared_b[b_local_offset.x] = bx8_f16.lo;
159 shared_b[b_local_offset.y] = bx8_f16.hi;
160
161 dequantize_q8_0(q8x16, reg_a, s);
162
163 sub_group_barrier(CLK_LOCAL_MEM_FENCE);
164
165 dotx16_reduce8(reg_a, shared_b, reg_c.lo, 0);
166 dotx16_reduce8(reg_a, shared_b, reg_c.hi, 16);
167 }
168
169 if ((get_global_id(0) + block_id_m * TILESIZE_M) >= ne01) {
170 return;
171 }
172
173 __local uint out_idx[TILESIZE_N];
174
175 if (get_local_id(0) < TILESIZE_N) {
176 uint idx = src2[block_id_n * TILESIZE_N + get_local_id(0)];
177 if (idx == 0xFFFFFFFF) {
178 idx = src2[block_id_n * TILESIZE_N + 0];
179 }
180 out_idx[get_local_id(0)] = idx * ne01;
181 }
182
183 barrier(CLK_LOCAL_MEM_FENCE);
184
185 uint m_offset = row + get_local_id(0);
186
187 write_imagef(dst, out_idx[1] + m_offset, (reg_c.s1));
188 write_imagef(dst, out_idx[2] + m_offset, (reg_c.s2));
189 write_imagef(dst, out_idx[3] + m_offset, (reg_c.s3));
190 write_imagef(dst, out_idx[4] + m_offset, (reg_c.s4));
191 write_imagef(dst, out_idx[5] + m_offset, (reg_c.s5));
192 write_imagef(dst, out_idx[6] + m_offset, (reg_c.s6));
193 write_imagef(dst, out_idx[7] + m_offset, (reg_c.s7));
194 write_imagef(dst, out_idx[8] + m_offset, (reg_c.s8));
195 write_imagef(dst, out_idx[9] + m_offset, (reg_c.s9));
196 write_imagef(dst, out_idx[10] + m_offset, (reg_c.sa));
197 write_imagef(dst, out_idx[11] + m_offset, (reg_c.sb));
198 write_imagef(dst, out_idx[12] + m_offset, (reg_c.sc));
199 write_imagef(dst, out_idx[13] + m_offset, (reg_c.sd));
200 write_imagef(dst, out_idx[14] + m_offset, (reg_c.se));
201 write_imagef(dst, out_idx[15] + m_offset, (reg_c.sf));
202 write_imagef(dst, out_idx[16] + m_offset, (reg_c.sg));
203 write_imagef(dst, out_idx[17] + m_offset, (reg_c.sh));
204 write_imagef(dst, out_idx[18] + m_offset, (reg_c.si));
205 write_imagef(dst, out_idx[19] + m_offset, (reg_c.sj));
206 write_imagef(dst, out_idx[20] + m_offset, (reg_c.sk));
207 write_imagef(dst, out_idx[21] + m_offset, (reg_c.sl));
208 write_imagef(dst, out_idx[22] + m_offset, (reg_c.sm));
209 write_imagef(dst, out_idx[23] + m_offset, (reg_c.sn));
210 write_imagef(dst, out_idx[24] + m_offset, (reg_c.so));
211 write_imagef(dst, out_idx[25] + m_offset, (reg_c.sp));
212 write_imagef(dst, out_idx[26] + m_offset, (reg_c.sq));
213 write_imagef(dst, out_idx[27] + m_offset, (reg_c.sr));
214 write_imagef(dst, out_idx[28] + m_offset, (reg_c.ss));
215 write_imagef(dst, out_idx[29] + m_offset, (reg_c.st));
216 write_imagef(dst, out_idx[30] + m_offset, (reg_c.su));
217 write_imagef(dst, out_idx[31] + m_offset, (reg_c.sv));
218
219 barrier(CLK_GLOBAL_MEM_FENCE);
220 write_imagef(dst, out_idx[0] + m_offset, (reg_c.s0));
221}