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-cpu / tiled / tiled.cpp

1245 lines · 58049 bytes Raw Blame History
1#include "tiled.h"
2#include "tiled-kernel.h"
3
4#include "ggml-cpu-impl.h"
5#include "ggml-cpu.h"
6#include "ggml.h"
7
8// kvalues table (impl section) for the iq4_xs unpack
9#define GGML_COMMON_IMPL_CPP
10#include "ggml-common.h"
11
12#include <cassert>
13#include <cstdlib>
14#include <cstring>
15
16#include <mutex>
17
18#define UNUSED GGML_UNUSED
19
20// unpack routines for various quant types src0
21static void tiled_unpack_src0(const block_q4_K * rows, int64_t row_stride, int n_rows, tiled_tile_src0 * tile, int num_k) {
22 GGML_ASSERT(n_rows <= TILED_TILE_ROWS);
23 constexpr int NB = 8; // 32-wide subblocks
24 // 12-byte packed scale/min decode, same extraction as the reference kernels
25 static const uint32_t kmask1 = 0x3f3f3f3f;
26 static const uint32_t kmask2 = 0x0f0f0f0f;
27 static const uint32_t kmask3 = 0x03030303;
28
29 const int qk_stride = num_k * TILED_TILE_K;
30 const int nb_stride = NB * num_k;
31 for (int slab = 0; slab < num_k; slab++) {
32 for (int r = 0; r < n_rows; r++) {
33 const int d_off = slab * TILED_MICRO + r;
34 const int q_off = r * qk_stride + slab * TILED_TILE_K;
35 const int s_off = r * nb_stride + slab * NB;
36 const block_q4_K & x = rows[r * row_stride + slab];
37
38 tile->d[d_off] = ggml_fp16_to_fp32(x.data.data.d);
39 tile->dmin[d_off] = ggml_fp16_to_fp32(x.data.data.dmin);
40
41 uint32_t utmp[4];
42 memcpy(utmp, x.scales, 12);
43 utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
44 const uint32_t uaux = utmp[1] & kmask1;
45 utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
46 utmp[2] = uaux;
47 utmp[0] &= kmask1;
48
49 const uint8_t * scales = (const uint8_t *) &utmp[0];
50 const uint8_t * mins = (const uint8_t *) &utmp[2];
51 for (int s = 0; s < NB; s++) {
52 tile->scales[s_off + s] = (int32_t) scales[s];
53 tile->mins[s_off + s] = (int32_t) mins[s];
54 }
55
56 // extract the 4-bit codes (low 4 + high 4), same extraction as the reference kernels
57 uint8_t * q = &tile->q[q_off];
58 tiled_unpk_nib4(x.qs + 0, q + 0, q + 32);
59 tiled_unpk_nib4(x.qs + 32, q + 64, q + 96);
60 tiled_unpk_nib4(x.qs + 64, q + 128, q + 160);
61 tiled_unpk_nib4(x.qs + 96, q + 192, q + 224);
62 }
63 }
64}
65
66static void tiled_unpack_src0(const block_q5_K * rows, int64_t row_stride, int n_rows, tiled_tile_src0 * tile, int num_k) {
67 GGML_ASSERT(n_rows <= TILED_TILE_ROWS);
68 constexpr int NB = 8; // 32-wide subblocks
69 // 12-byte packed scale/min decode, same extraction as the reference kernels
70 static const uint32_t kmask1 = 0x3f3f3f3f;
71 static const uint32_t kmask2 = 0x0f0f0f0f;
72 static const uint32_t kmask3 = 0x03030303;
73
74 const int qk_stride = num_k * TILED_TILE_K;
75 const int nb_stride = NB * num_k;
76 for (int slab = 0; slab < num_k; slab++) {
77 for (int r = 0; r < n_rows; r++) {
78 const int d_off = slab * TILED_MICRO + r;
79 const int q_off = r * qk_stride + slab * TILED_TILE_K;
80 const int s_off = r * nb_stride + slab * NB;
81 const block_q5_K & x = rows[r * row_stride + slab];
82
83 tile->d[d_off] = ggml_fp16_to_fp32(x.data.data.d);
84 tile->dmin[d_off] = ggml_fp16_to_fp32(x.data.data.dmin);
85
86 uint32_t utmp[4];
87 memcpy(utmp, x.scales, 12);
88 utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
89 const uint32_t uaux = utmp[1] & kmask1;
90 utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
91 utmp[2] = uaux;
92 utmp[0] &= kmask1;
93
94 const uint8_t * scales = (const uint8_t *) &utmp[0];
95 const uint8_t * mins = (const uint8_t *) &utmp[2];
96 for (int s = 0; s < NB; s++) {
97 tile->scales[s_off + s] = (int32_t) scales[s];
98 tile->mins[s_off + s] = (int32_t) mins[s];
99 }
100
101 // extract the 5-bit codes (4 low bits + 1 high bit), same as the generic kernels:
102 // 64-element chunk j uses qh bits 2j (low 32) and 2j+1 (high 32); OR adds the 5th bit
103 // (no overlap with the 4-bit codes, identical to the reference ADD)
104 uint8_t * q = &tile->q[q_off];
105 tiled_unpk_nib4(x.qs + 0, q + 0, q + 32);
106 tiled_unpk_nib4(x.qs + 32, q + 64, q + 96);
107 tiled_unpk_nib4(x.qs + 64, q + 128, q + 160);
108 tiled_unpk_nib4(x.qs + 96, q + 192, q + 224);
109 tiled_unpk_or<0, 4, 1>(q + 0, x.qh);
110 tiled_unpk_or<1, 4, 1>(q + 32, x.qh);
111 tiled_unpk_or<2, 4, 1>(q + 64, x.qh);
112 tiled_unpk_or<3, 4, 1>(q + 96, x.qh);
113 tiled_unpk_or<4, 4, 1>(q + 128, x.qh);
114 tiled_unpk_or<5, 4, 1>(q + 160, x.qh);
115 tiled_unpk_or<6, 4, 1>(q + 192, x.qh);
116 tiled_unpk_or<7, 4, 1>(q + 224, x.qh);
117 }
118 }
119}
120
121static void tiled_unpack_src0(const block_q6_K * rows, int64_t row_stride, int n_rows, tiled_tile_src0 * tile, int num_k) {
122 GGML_ASSERT(n_rows <= TILED_TILE_ROWS);
123 constexpr int NB = 16; // 16-wide subblocks
124 const int qk_stride = num_k * TILED_TILE_K;
125 const int nb_stride = NB * num_k;
126 for (int slab = 0; slab < num_k; slab++) {
127 for (int r = 0; r < n_rows; r++) {
128 const int d_off = slab * TILED_MICRO + r;
129 const int q_off = r * qk_stride + slab * TILED_TILE_K;
130 const int s_off = r * nb_stride + slab * NB;
131 const block_q6_K & x = rows[r * row_stride + slab];
132 tile->d[d_off] = ggml_fp16_to_fp32(x.d);
133
134 // 6-bit code = 4 low bits (ql) | 2 high bits (qh); see ggml_vec_dot_q6_K_q8_K_generic
135 // per half the lanes are [ql lo(0:32)] [ql lo(32:64)] [ql hi(0:32)] [ql hi(32:64)]
136 uint8_t * q = &tile->q[q_off];
137 for (int half = 0; half < 2; half++) {
138 uint8_t * out = q + 128 * half;
139 tiled_unpk_nib4(x.ql + 64 * half + 0, out + 0, out + 64);
140 tiled_unpk_nib4(x.ql + 64 * half + 32, out + 32, out + 96);
141 tiled_unpk_or<0, 4, 3>(out + 0, x.qh + 32 * half);
142 tiled_unpk_or<2, 4, 3>(out + 32, x.qh + 32 * half);
143 tiled_unpk_or<4, 4, 3>(out + 64, x.qh + 32 * half);
144 tiled_unpk_or<6, 4, 3>(out + 96, x.qh + 32 * half);
145 }
146 // scale is a plain int8 per 16-element subblock (16 per 256-K)
147 for (int s = 0; s < NB; s++) { tile->scales[s_off + s] = (int32_t) (int8_t) x.scales[s]; }
148 }
149 }
150}
151
152static void tiled_unpack_src0(const block_q3_K * rows, int64_t row_stride, int n_rows, tiled_tile_src0 * tile, int num_k) {
153 GGML_ASSERT(n_rows <= TILED_TILE_ROWS);
154 constexpr int NB = 16; // 16-wide subblocks
155 const int qk_stride = num_k * TILED_TILE_K;
156 const int nb_stride = NB * num_k;
157 for (int slab = 0; slab < num_k; slab++) {
158 for (int r = 0; r < n_rows; r++) {
159 const int d_off = slab * TILED_MICRO + r;
160 const int q_off = r * qk_stride + slab * TILED_TILE_K;
161 const int s_off = r * nb_stride + slab * NB;
162 const block_q3_K & x = rows[r * row_stride + slab];
163 tile->d[d_off] = ggml_fp16_to_fp32(x.d);
164
165 // 3-bit code = 2 low bits (qs) | (1 high bit from hmask << 2)
166 // element e (0..255): half=e>>7, el=e&127, group=el>>5, l=el&31
167 // low2 = (qs[half*32 + l] >> 2*group) & 3
168 // high = (hmask[l] >> (half*4 + group)) & 1
169 // see ggml_vec_dot_q3_K_q8_K_generic
170 uint8_t * q = &tile->q[q_off];
171 const uint8_t * s0 = x.qs;
172 const uint8_t * s1 = x.qs + 32;
173 uint8_t * o0 = q;
174 uint8_t * o1 = q + 128;
175 tiled_unpk_2bit<0>(s0, o0); tiled_unpk_or<0, 2, 1>(o0, x.hmask);
176 tiled_unpk_2bit<2>(s0, o0 + 32); tiled_unpk_or<1, 2, 1>(o0 + 32, x.hmask);
177 tiled_unpk_2bit<4>(s0, o0 + 64); tiled_unpk_or<2, 2, 1>(o0 + 64, x.hmask);
178 tiled_unpk_2bit<6>(s0, o0 + 96); tiled_unpk_or<3, 2, 1>(o0 + 96, x.hmask);
179 tiled_unpk_2bit<0>(s1, o1); tiled_unpk_or<4, 2, 1>(o1, x.hmask);
180 tiled_unpk_2bit<2>(s1, o1 + 32); tiled_unpk_or<5, 2, 1>(o1 + 32, x.hmask);
181 tiled_unpk_2bit<4>(s1, o1 + 64); tiled_unpk_or<6, 2, 1>(o1 + 64, x.hmask);
182 tiled_unpk_2bit<6>(s1, o1 + 96); tiled_unpk_or<7, 2, 1>(o1 + 96, x.hmask);
183 // 6-bit scale decode (same kmask trick as the reference), stored as (scales - 32)
184 static const uint32_t kmask1 = 0x03030303;
185 static const uint32_t kmask2 = 0x0f0f0f0f;
186 uint32_t auxs[4];
187 memcpy(auxs, x.scales, 12);
188 const uint32_t tmp = auxs[2];
189 auxs[2] = ((auxs[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
190 auxs[3] = ((auxs[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
191 auxs[0] = (auxs[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
192 auxs[1] = (auxs[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
193 const int8_t * scales = (const int8_t *) &auxs[0];
194 for (int s = 0; s < NB; s++) { tile->scales[s_off + s] = (int32_t) scales[s] - 32; }
195 }
196 }
197}
198
199static void tiled_unpack_src0(const block_q2_K * rows, int64_t row_stride, int n_rows, tiled_tile_src0 * tile, int num_k) {
200 GGML_ASSERT(n_rows <= TILED_TILE_ROWS);
201 constexpr int NB = 16; // 16-wide subblocks
202 const int qk_stride = num_k * TILED_TILE_K;
203 const int nb_stride = NB * num_k;
204 for (int slab = 0; slab < num_k; slab++) {
205 for (int r = 0; r < n_rows; r++) {
206 const int d_off = slab * TILED_MICRO + r;
207 const int q_off = r * qk_stride + slab * TILED_TILE_K;
208 const int s_off = r * nb_stride + slab * NB;
209 const block_q2_K & x = rows[r * row_stride + slab];
210 tile->d[d_off] = ggml_fp16_to_fp32(x.data.data.d);
211 tile->dmin[d_off] = ggml_fp16_to_fp32(x.data.data.dmin);
212
213 // 2-bit code: element e -> half=e>>7, el=e&127
214 // byte = half*32 + (el & 31), shift = 2*(el >> 5)
215 // see ggml_vec_dot_q2_K_q8_K_generic
216 uint8_t * q = &tile->q[q_off];
217 for (int half = 0; half < 2; half++) {
218 const uint8_t * s = x.qs + 32 * half;
219 uint8_t * out = q + 128 * half;
220 tiled_unpk_2bit<0>(s, out + 0);
221 tiled_unpk_2bit<2>(s, out + 32);
222 tiled_unpk_2bit<4>(s, out + 64);
223 tiled_unpk_2bit<6>(s, out + 96);
224 }
225 // scale/min packed in one byte per 16-element subblock: low 4 bits = scale, high 4 = min
226 for (int s = 0; s < NB; s++) {
227 tile->scales[s_off + s] = (int32_t) (x.scales[s] & 0xF);
228 tile->mins[s_off + s] = (int32_t) (x.scales[s] >> 4);
229 }
230 }
231 }
232}
233
234// iq4_xs: 4-bit codes through the kvalues_iq4nl LUT, 6-bit scales per 32, no min.
235// LUT values are stored as kvalues + 128 so the +128 shift the kernel applies to
236// the activations cancels against the bsums correction (BIAS = 128); the +128 is
237// folded into the table up front so the expansion is a plain lookup
238static void tiled_unpack_src0(const block_iq4_xs * rows, int64_t row_stride, int n_rows, tiled_tile_src0 * tile, int num_k) {
239 GGML_ASSERT(n_rows <= TILED_TILE_ROWS);
240 constexpr int NB = 8; // 32-wide subblocks
241
242 static const uint8_t lut[16] = {
243 (uint8_t) (kvalues_iq4nl[0] + 128), (uint8_t) (kvalues_iq4nl[1] + 128),
244 (uint8_t) (kvalues_iq4nl[2] + 128), (uint8_t) (kvalues_iq4nl[3] + 128),
245 (uint8_t) (kvalues_iq4nl[4] + 128), (uint8_t) (kvalues_iq4nl[5] + 128),
246 (uint8_t) (kvalues_iq4nl[6] + 128), (uint8_t) (kvalues_iq4nl[7] + 128),
247 (uint8_t) (kvalues_iq4nl[8] + 128), (uint8_t) (kvalues_iq4nl[9] + 128),
248 (uint8_t) (kvalues_iq4nl[10] + 128), (uint8_t) (kvalues_iq4nl[11] + 128),
249 (uint8_t) (kvalues_iq4nl[12] + 128), (uint8_t) (kvalues_iq4nl[13] + 128),
250 (uint8_t) (kvalues_iq4nl[14] + 128), (uint8_t) (kvalues_iq4nl[15] + 128),
251 };
252
253 const int qk_stride = num_k * TILED_TILE_K;
254 const int nb_stride = NB * num_k;
255 for (int slab = 0; slab < num_k; slab++) {
256 for (int r = 0; r < n_rows; r++) {
257 const int d_off = slab * TILED_MICRO + r;
258 const int q_off = r * qk_stride + slab * TILED_TILE_K;
259 const int s_off = r * nb_stride + slab * NB;
260 const block_iq4_xs & x = rows[r * row_stride + slab];
261 tile->d[d_off] = ggml_fp16_to_fp32(x.d);
262
263 // 6-bit scale per 32, stored as (ls - 32); same extraction as dequantize_row_iq4_xs
264 for (int s = 0; s < NB; s++) {
265 const int ls = ((x.scales_l[s / 2] >> 4 * (s % 2)) & 0xf) | (((x.scales_h >> 2 * s) & 3) << 4);
266 tile->scales[s_off + s] = (int32_t) ls - 32;
267 }
268
269 // 4-bit codes through the LUT: low nibbles of a byte pair come first
270 uint8_t * q = &tile->q[q_off];
271 uint8_t lo[32], hi[32];
272 for (int u = 0; u < 4; u++) {
273 tiled_unpk_nib4(x.qs + 32 * u, lo, hi);
274 tiled_lut8(lut, lo + 0, q + 64 * u + 0);
275 tiled_lut8(lut, hi + 0, q + 64 * u + 16);
276 tiled_lut8(lut, lo + 16, q + 64 * u + 32);
277 tiled_lut8(lut, hi + 16, q + 64 * u + 48);
278 }
279 }
280 }
281}
282
283// iq2_xxs: 2-bit grids through the iq2xxs_grid LUT, 4-bit scale per 32, no min
284// codes stored as (value + 128), matching BIAS = 128
285static void tiled_unpack_src0(const block_iq2_xxs * rows, int64_t row_stride, int n_rows, tiled_tile_src0 * tile, int num_k) {
286 GGML_ASSERT(n_rows <= TILED_TILE_ROWS);
287 constexpr int NB = 8; // 32-wide subblocks
288
289 const int qk_stride = num_k * TILED_TILE_K;
290 const int nb_stride = NB * num_k;
291 for (int slab = 0; slab < num_k; slab++) {
292 for (int r = 0; r < n_rows; r++) {
293 const int d_off = slab * TILED_MICRO + r;
294 const int q_off = r * qk_stride + slab * TILED_TILE_K;
295 const int s_off = r * nb_stride + slab * NB;
296 const block_iq2_xxs & x = rows[r * row_stride + slab];
297 tile->d[d_off] = ggml_fp16_to_fp32(x.d) * 0.125f;
298
299 uint32_t aux32[2];
300 const uint8_t * aux8 = (const uint8_t *) aux32;
301 uint64_t g[4];
302 uint8_t signs4[4];
303 for (int ib32 = 0; ib32 < NB; ib32++) {
304 memcpy(aux32, x.qs + 4 * ib32, 2 * sizeof(uint32_t));
305 tile->scales[s_off + ib32] = (int32_t) (2 * (aux32[1] >> 28) + 1);
306 for (int l = 0; l < 4; l++) {
307 g[l] = iq2xxs_grid[aux8[l]];
308 signs4[l] = ksigns_iq2xs[(aux32[1] >> (7 * l)) & 127];
309 }
310 tiled_unpk_sign32(g[0], g[1], g[2], g[3], signs4, &tile->q[q_off + 32 * ib32]);
311 }
312 }
313 }
314}
315
316// iq2_xs: 2-bit grids through the iq2xs_grid LUT, 4-bit scale per 16 (two per 32), no min
317// codes stored as (value + 128), matching BIAS = 128
318static void tiled_unpack_src0(const block_iq2_xs * rows, int64_t row_stride, int n_rows, tiled_tile_src0 * tile, int num_k) {
319 GGML_ASSERT(n_rows <= TILED_TILE_ROWS);
320 constexpr int NB = 16; // 16-wide subblocks
321
322 const int qk_stride = num_k * TILED_TILE_K;
323 const int nb_stride = NB * num_k;
324 for (int slab = 0; slab < num_k; slab++) {
325 for (int r = 0; r < n_rows; r++) {
326 const int d_off = slab * TILED_MICRO + r;
327 const int q_off = r * qk_stride + slab * TILED_TILE_K;
328 const int s_off = r * nb_stride + slab * NB;
329 const block_iq2_xs & x = rows[r * row_stride + slab];
330 tile->d[d_off] = ggml_fp16_to_fp32(x.d) * 0.125f;
331
332 uint64_t g[4];
333 uint8_t signs4[4];
334 for (int ib32 = 0; ib32 < QK_K / 32; ib32++) {
335 tile->scales[s_off + 2 * ib32 + 0] = (int32_t) (2 * (x.scales[ib32] & 0xf) + 1);
336 tile->scales[s_off + 2 * ib32 + 1] = (int32_t) (2 * (x.scales[ib32] >> 4) + 1);
337 const uint16_t * q = x.qs + 4 * ib32;
338 for (int l = 0; l < 4; l++) {
339 g[l] = iq2xs_grid[q[l] & 511];
340 signs4[l] = ksigns_iq2xs[q[l] >> 9];
341 }
342 tiled_unpk_sign32(g[0], g[1], g[2], g[3], signs4, &tile->q[q_off + 32 * ib32]);
343 }
344 }
345 }
346}
347
348// iq2_s: 2-bit grids through the iq2s_grid LUT, 4-bit scale per 16 (two per 32), no min
349// codes stored as (value + 128), matching BIAS = 128
350static void tiled_unpack_src0(const block_iq2_s * rows, int64_t row_stride, int n_rows, tiled_tile_src0 * tile, int num_k) {
351 GGML_ASSERT(n_rows <= TILED_TILE_ROWS);
352 constexpr int NB = 16; // 16-wide subblocks
353
354 const int qk_stride = num_k * TILED_TILE_K;
355 const int nb_stride = NB * num_k;
356 for (int slab = 0; slab < num_k; slab++) {
357 for (int r = 0; r < n_rows; r++) {
358 const int d_off = slab * TILED_MICRO + r;
359 const int q_off = r * qk_stride + slab * TILED_TILE_K;
360 const int s_off = r * nb_stride + slab * NB;
361 const block_iq2_s & x = rows[r * row_stride + slab];
362 tile->d[d_off] = ggml_fp16_to_fp32(x.d) * 0.125f;
363
364 const uint8_t * qs = x.qs;
365 const uint8_t * signs = x.qs + QK_K / 8; // packed sign bytes share the qs array, same as the reference
366 uint64_t g[4];
367 for (int ib32 = 0; ib32 < QK_K / 32; ib32++) {
368 tile->scales[s_off + 2 * ib32 + 0] = (int32_t) (2 * (x.scales[ib32] & 0xf) + 1);
369 tile->scales[s_off + 2 * ib32 + 1] = (int32_t) (2 * (x.scales[ib32] >> 4) + 1);
370 for (int l = 0; l < 4; l++) {
371 g[l] = iq2s_grid[qs[l] | (x.qh[ib32] << (8 - 2 * l) & 0x300)];
372 }
373 tiled_unpk_sign32(g[0], g[1], g[2], g[3], signs, &tile->q[q_off + 32 * ib32]);
374 qs += 4;
375 signs += 4;
376 }
377 }
378 }
379}
380
381// iq3_xxs: 3-bit grids through the iq3xxs_grid LUT, 4-bit scale per 32, no min
382// codes stored as (value + 128), matching BIAS = 128
383static void tiled_unpack_src0(const block_iq3_xxs * rows, int64_t row_stride, int n_rows, tiled_tile_src0 * tile, int num_k) {
384 GGML_ASSERT(n_rows <= TILED_TILE_ROWS);
385 constexpr int NB = 8; // 32-wide subblocks
386
387 const int qk_stride = num_k * TILED_TILE_K;
388 const int nb_stride = NB * num_k;
389 for (int slab = 0; slab < num_k; slab++) {
390 for (int r = 0; r < n_rows; r++) {
391 const int d_off = slab * TILED_MICRO + r;
392 const int q_off = r * qk_stride + slab * TILED_TILE_K;
393 const int s_off = r * nb_stride + slab * NB;
394 const block_iq3_xxs & x = rows[r * row_stride + slab];
395 tile->d[d_off] = ggml_fp16_to_fp32(x.d) * 0.25f;
396
397 const uint8_t * qs = x.qs;
398 const uint8_t * scales_and_signs = x.qs + QK_K / 4; // 4 bytes per 32: code bits in the top nibble, signs in 7-bit chunks
399 uint64_t g[4];
400 uint8_t signs4[4];
401 for (int ib32 = 0; ib32 < QK_K / 32; ib32++) {
402 const uint32_t aux32 = *(const uint32_t *) (scales_and_signs + 4 * ib32);
403 tile->scales[s_off + ib32] = (int32_t) (2 * (aux32 >> 28) + 1);
404 for (int l = 0; l < 4; l++) {
405 // bytes 8*l .. 8*l+7: low entry e1 (4 values) then high entry e2 (4 values)
406 g[l] = (uint64_t) iq3xxs_grid[qs[2 * l + 1]] << 32 | iq3xxs_grid[qs[2 * l + 0]];
407 signs4[l] = ksigns_iq2xs[(aux32 >> (7 * l)) & 127];
408 }
409 tiled_unpk_sign32(g[0], g[1], g[2], g[3], signs4, &tile->q[q_off + 32 * ib32]);
410 qs += 8;
411 }
412 }
413 }
414}
415
416// iq3_s: 3-bit grids through the iq3s_grid LUT, 4-bit scale per 32 (two per scale byte), no min
417// codes stored as (value + 128), matching BIAS = 128
418static void tiled_unpack_src0(const block_iq3_s * rows, int64_t row_stride, int n_rows, tiled_tile_src0 * tile, int num_k) {
419 GGML_ASSERT(n_rows <= TILED_TILE_ROWS);
420 constexpr int NB = 8; // 32-wide subblocks
421
422 const int qk_stride = num_k * TILED_TILE_K;
423 const int nb_stride = NB * num_k;
424 for (int slab = 0; slab < num_k; slab++) {
425 for (int r = 0; r < n_rows; r++) {
426 const int d_off = slab * TILED_MICRO + r;
427 const int q_off = r * qk_stride + slab * TILED_TILE_K;
428 const int s_off = r * nb_stride + slab * NB;
429 const block_iq3_s & x = rows[r * row_stride + slab];
430 tile->d[d_off] = ggml_fp16_to_fp32(x.d);
431
432 const uint8_t * qs = x.qs;
433 const uint8_t * qh = x.qh;
434 const uint8_t * signs = x.signs;
435 for (int ib32 = 0; ib32 < QK_K / 32; ib32 += 2) {
436 tile->scales[s_off + ib32 + 0] = (int32_t) (1 + 2 * (x.scales[ib32 / 2] & 0xf));
437 tile->scales[s_off + ib32 + 1] = (int32_t) (1 + 2 * (x.scales[ib32 / 2] >> 4));
438 for (int h = 0; h < 2; h++) {
439 uint64_t g[4];
440 for (int l = 0; l < 4; l++) {
441 // bytes 8*l .. 8*l+7: low entry e1 (4 values) then high entry e2 (4 values)
442 g[l] = (uint64_t) iq3s_grid[qs[2 * l + 1] | ((qh[h] << (7 - 2 * l)) & 256)] << 32
443 | iq3s_grid[qs[2 * l + 0] | ((qh[h] << (8 - 2 * l)) & 256)];
444 }
445 tiled_unpk_sign32(g[0], g[1], g[2], g[3], signs, &tile->q[q_off + (ib32 + h) * 32]);
446 qs += 8;
447 signs += 4;
448 }
449 qh += 2;
450 }
451 }
452 }
453}
454
455// iq1_s: ternary grid (values +-1/0) scaled by 8 to leave room for the +-1 delta offset,
456// the /8 folds into d; 3-bit scale per 32, no min
457// codes stored as (8 * grid + delta + 128), matching BIAS = 128
458static void tiled_unpack_src0(const block_iq1_s * rows, int64_t row_stride, int n_rows, tiled_tile_src0 * tile, int num_k) {
459 GGML_ASSERT(n_rows <= TILED_TILE_ROWS);
460 constexpr int NB = 8; // 32-wide subblocks
461
462 const int qk_stride = num_k * TILED_TILE_K;
463 const int nb_stride = NB * num_k;
464 for (int slab = 0; slab < num_k; slab++) {
465 for (int r = 0; r < n_rows; r++) {
466 const int d_off = slab * TILED_MICRO + r;
467 const int q_off = r * qk_stride + slab * TILED_TILE_K;
468 const int s_off = r * nb_stride + slab * NB;
469 const block_iq1_s & x = rows[r * row_stride + slab];
470 tile->d[d_off] = ggml_fp16_to_fp32(x.d) * 0.125f;
471
472 const uint8_t * qs = x.qs;
473 for (int ib = 0; ib < QK_K / 32; ib++) {
474 const uint16_t hw = x.qh[ib];
475 tile->scales[s_off + ib] = (int32_t) (2 * ((hw >> 12) & 7) + 1);
476 const int8_t delta = (hw & 0x8000) ? -1 : 1;
477 for (int l = 0; l < 4; l++) {
478 const uint64_t entry = iq1s_grid[qs[l] | (((hw >> (3 * l)) & 7) << 8)];
479 tiled_unpk_tern8((const uint8_t *) &entry, delta, &tile->q[q_off + 32 * ib + 8 * l]);
480 }
481 qs += 4;
482 }
483 }
484 }
485}
486
487// iq1_m: like iq1_s but the fp16 scale is packed across the 4 scale bytes (no d field) and the
488// delta offset is per 8, giving one scale per 16; 3-bit scale per 16, no min
489// codes stored as (8 * grid + delta + 128), matching BIAS = 128
490static void tiled_unpack_src0(const block_iq1_m * rows, int64_t row_stride, int n_rows, tiled_tile_src0 * tile, int num_k) {
491 GGML_ASSERT(n_rows <= TILED_TILE_ROWS);
492 constexpr int NB = 16; // 16-wide subblocks
493
494 const int qk_stride = num_k * TILED_TILE_K;
495 const int nb_stride = NB * num_k;
496 for (int slab = 0; slab < num_k; slab++) {
497 for (int r = 0; r < n_rows; r++) {
498 const int d_off = slab * TILED_MICRO + r;
499 const int q_off = r * qk_stride + slab * TILED_TILE_K;
500 const int s_off = r * nb_stride + slab * NB;
501 const block_iq1_m & x = rows[r * row_stride + slab];
502
503 const uint16_t * sc = (const uint16_t *) x.scales;
504 iq1m_scale_t scale;
505 scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
506 tile->d[d_off] = ggml_fp16_to_fp32(scale.f16) * 0.125f;
507
508 const uint8_t * qs = x.qs;
509 const uint8_t * qh = x.qh;
510 for (int ib = 0; ib < QK_K / 32; ib++) {
511 const uint16_t hw = sc[ib / 2];
512 const int sh = 6 * (ib % 2);
513 tile->scales[s_off + 2 * ib + 0] = (int32_t) (2 * ((hw >> sh) & 7) + 1);
514 tile->scales[s_off + 2 * ib + 1] = (int32_t) (2 * ((hw >> (sh + 3)) & 7) + 1);
515
516 const uint16_t idx[4] = {
517 (uint16_t) (qs[0] | ((qh[0] << 8) & 0x700)),
518 (uint16_t) (qs[1] | ((qh[0] << 4) & 0x700)),
519 (uint16_t) (qs[2] | ((qh[1] << 8) & 0x700)),
520 (uint16_t) (qs[3] | ((qh[1] << 4) & 0x700)),
521 };
522 const int8_t delta[4] = {
523 (int8_t) ((qh[0] & 0x08) ? -1 : 1), (int8_t) ((qh[0] & 0x80) ? -1 : 1),
524 (int8_t) ((qh[1] & 0x08) ? -1 : 1), (int8_t) ((qh[1] & 0x80) ? -1 : 1),
525 };
526 for (int l = 0; l < 4; l++) {
527 const uint64_t entry = iq1s_grid[idx[l]];
528 tiled_unpk_tern8((const uint8_t *) &entry, delta[l], &tile->q[q_off + 32 * ib + 8 * l]);
529 }
530 qs += 4;
531 qh += 2;
532 }
533 }
534 }
535}
536
537// unpack src1 tile from q8_K rows. num_k = K-blocks per row: each row decodes num_k consecutive
538// source blocks (rows[r][kblk + slab]) into the tile at row stride num_k*256, so each activation
539// row is one long stream. num_k=1 is the standard single-slab path (byte-identical to before).
540static void tiled_unpack_src1_q8_K(const block_q8_K * const * rows, int n_rows, tiled_tile_src1 * tile,
541 int kblk, int num_k) {
542 GGML_ASSERT(n_rows <= TILED_TILE_ROWS);
543 const int n_padded = (n_rows + TILED_MICRO - 1) & ~(TILED_MICRO - 1);
544 const int bs_stride = (num_k == 1) ? TILED_TILE_ROWS : TILED_MICRO;
545 const int n16 = TILED_TILE_K / TILED_MICRO;
546 // natural [row][k] fill, zero-pad ragged tail; codes stay natural here, the driver
547 // repacks them just-in-time via tiled_repack_src1 (per 16-row band on the standard path,
548 // the whole chunk on the narrow path) so each repacked region is L1-hot for its uses
549 for (int slab = 0; slab < num_k; slab++) {
550 const int q_off = slab * TILED_TILE_K;
551 for (int r = 0; r < n_padded; r++) {
552 if (r < n_rows) {
553 memcpy(&tile->q[r * (num_k * TILED_TILE_K) + q_off], rows[r][kblk + slab].qs, TILED_TILE_K);
554 } else {
555 memset(&tile->q[r * (num_k * TILED_TILE_K) + q_off], 0, TILED_TILE_K);
556 }
557 }
558 // d and bsums (ISA-independent)
559 for (int r = 0; r < n_padded; r++) {
560 if (r < n_rows) {
561 const block_q8_K & x = rows[r][kblk + slab];
562 for (int s = 0; s < n16; s++) { tile->bsums[(slab * n16 + s) * bs_stride + r] = (int32_t) x.bsums[s]; }
563 tile->d[slab * TILED_MICRO + r] = x.d;
564 } else {
565 for (int s = 0; s < n16; s++) { tile->bsums[(slab * n16 + s) * bs_stride + r] = 0; }
566 tile->d[slab * TILED_MICRO + r] = 0.0f;
567 }
568 }
569 }
570}
571
572// GGML_CPU_TILED_MM: master switch, on by default. If off, we fast return false and normal vec_dot mul_mat resumes
573static bool ggml_tiled_matmul_enabled(void) {
574 static bool enabled = true;
575 static std::once_flag flag;
576 std::call_once(flag, []() {
577 const char * env = getenv("GGML_CPU_TILED_MM");
578 enabled = env == NULL || atoi(env) != 0;
579 });
580 return enabled;
581}
582
583// GGML_CPU_TILED_MM_FORCE: test/bench only, take the tiled path even when unprofitable
584static bool ggml_tiled_matmul_forced(void) {
585 static bool forced = false;
586 static std::once_flag flag;
587 std::call_once(flag, []() {
588 const char * env = getenv("GGML_CPU_TILED_MM_FORCE");
589 forced = env != NULL && atoi(env) == 1;
590 });
591 return forced;
592}
593
594// hard constraints shared by the MUL_MAT and MUL_MAT_ID entries; the src0 type gate is the
595// entries' type switch
596
597#if !defined(__AVX512VNNI__) && !defined(__AVX2__) && !defined(__AVX__)
598static bool ggml_tiled_supported(const struct ggml_tensor * src0,
599 const struct ggml_tensor * src1) {
600 UNUSED(src0);
601 UNUSED(src1);
602 return false;
603}
604#else
605static bool ggml_tiled_supported(const struct ggml_tensor * src0,
606 const struct ggml_tensor * src1) {
607 if (!ggml_tiled_matmul_enabled()) {
608 return false;
609 }
610
611 // repack-buffer weights hold a repacked layout, let that kernel handle
612 if (src0->extra != NULL) {
613 return false;
614 }
615 // Supported quant types for src0
616 switch (src0->type) {
617 case GGML_TYPE_Q6_K:
618 case GGML_TYPE_Q5_K:
619 case GGML_TYPE_Q4_K:
620 case GGML_TYPE_Q3_K:
621 case GGML_TYPE_Q2_K:
622 case GGML_TYPE_IQ4_XS:
623 case GGML_TYPE_IQ2_XXS:
624 case GGML_TYPE_IQ2_XS:
625 case GGML_TYPE_IQ2_S:
626 case GGML_TYPE_IQ3_XXS:
627 case GGML_TYPE_IQ3_S:
628 case GGML_TYPE_IQ1_S:
629 case GGML_TYPE_IQ1_M:
630 return true;
631 default:
632 return false;
633 }
634 if (src1->type != GGML_TYPE_F32 && src1->type != GGML_TYPE_Q8_K) {
635 return false;
636 }
637
638 if (src1->type == GGML_TYPE_Q8_K && !ggml_is_contiguous(src1)) {
639 // We can handle noncontiguous floats because we're repacking to q8_k anyways
640 return false;
641 }
642 return true;
643}
644#endif
645
646// per-thread workspace slot size (0 when tiled is disabled or unsupported on this arch)
647static size_t ggml_tiled_ws_size(void) {
648 if (!ggml_tiled_matmul_enabled()) {
649 return 0;
650 }
651 return TILED_WS_SLOT; // clean 512KB slot, rounded up from sizeof(tiled_ws)
652}
653
654size_t ggml_tiled_wdata_size(int n_tasks, struct ggml_tensor * dst) {
655 if (! ggml_tiled_supported(dst->src[0], dst->src[1])) {
656 return 0; // unsupported, don't allocate
657 }
658 return 64 + n_tasks * ggml_tiled_ws_size(); // 64 for alignment plus one 512KB slot per thread
659}
660
661
662// narrow-path K chunk: how many K's worth of weights to read contiguously into long/skinny tiles
663// Assumes 32kb L1 cache budget
664static int ggml_tiled_narrow_k_extent(int64_t n_rows, int64_t ne00) {
665 int l1 = 31 * 1024; // fit weights in 31k to leave room for scales
666
667 int rows = (int) n_rows;
668 if (rows < TILED_MICRO) rows = TILED_MICRO;
669 int ke = l1 / (TILED_MICRO + rows);
670 ke &= ~(TILED_TILE_K - 1); // floor to a multiple of 256
671 const int ke_max = TILED_TILE_ROWS * TILED_TILE_K / TILED_MICRO; // buffer ceiling
672 if (ke > ke_max) ke = ke_max;
673 while (ke >= TILED_TILE_K && ne00 % ke != 0) {
674 ke -= TILED_TILE_K;
675 }
676 return ke < TILED_TILE_K ? 0 : ke;
677}
678
679// Writeback of the 256x256 window: buf is j-major (row stride buf_stride), dst is i-major (column stride dst_stride).
680static void tiled_store_window(const float * buf, int n_src0, int n_src1, int buf_stride,
681 float * dst, size_t dst_stride) {
682 int ri = 0;
683 for (; ri + 16 <= n_src0; ri += 16) {
684 int rj = 0;
685 for (; rj + 8 <= n_src1; rj += 8) {
686 float r[16][8];
687 for (int t = 0; t < 16; t++) {
688 for (int u = 0; u < 8; u++) {
689 r[t][u] = buf[(ri + t) * buf_stride + rj + u];
690 }
691 }
692 for (int u = 0; u < 8; u++) {
693 for (int t = 0; t < 16; t++) {
694 dst[(ri + t) + (size_t) (rj + u) * dst_stride] = r[t][u];
695 }
696 }
697 }
698 // ragged j tail
699 for (; rj < n_src1; rj++) {
700 for (int t = 0; t < 16; t++) {
701 dst[(ri + t) + (size_t) rj * dst_stride] = buf[(ri + t) * buf_stride + rj];
702 }
703 }
704 }
705 // ragged i tail
706 for (; ri < n_src0; ri++) {
707 for (int j = 0; j < n_src1; j++) {
708 dst[ri + (size_t) j * dst_stride] = buf[ri * buf_stride + j];
709 }
710 }
711}
712
713// MUL_MAT_ID (MoE): src1 rows are gathered per output row (expert dispatch) and dst rows are
714// scattered back. The expert's cne1 gathered q8_K rows are staged once per expert into
715// thread-local scratch (contiguous rows, plus the [k/4][row][4] interleave on VNNI) so the
716// unpack and microtile are reused unchanged from mul_mat.
717
718// src0 rows per g group; finer than the TILED_TILE_K kernel tile so all threads stay busy at small ne01
719#define TILED_MMID_GROUP 64
720
721// like tiled_store_window, but the dst columns are not contiguous: col_ptrs[j] points at the
722// start of dst column j, whose rows are contiguous (dim 0, nb0 == 4 bytes).
723static void tiled_store_window_scatter(const float * buf, int n_src0, int n_src1, int buf_stride,
724 float * const * col_ptrs) {
725 int ri = 0;
726 for (; ri + 16 <= n_src0; ri += 16) {
727 int rj = 0;
728 for (; rj + 8 <= n_src1; rj += 8) {
729 float r[16][8];
730 for (int t = 0; t < 16; t++) {
731 for (int u = 0; u < 8; u++) {
732 r[t][u] = buf[(ri + t) * buf_stride + rj + u];
733 }
734 }
735 for (int u = 0; u < 8; u++) {
736 for (int t = 0; t < 16; t++) {
737 col_ptrs[rj + u][ri + t] = r[t][u];
738 }
739 }
740 }
741 // ragged j tail
742 for (; rj < n_src1; rj++) {
743 for (int t = 0; t < 16; t++) {
744 col_ptrs[rj][ri + t] = buf[(ri + t) * buf_stride + rj];
745 }
746 }
747 }
748 // ragged i tail
749 for (; ri < n_src0; ri++) {
750 for (int j = 0; j < n_src1; j++) {
751 col_ptrs[j][ri] = buf[ri * buf_stride + j];
752 }
753 }
754}
755
756
757// one (g, k) macrotile: zero the acc window, sweep K in 256 element slabs, scatter the result rows
758// rows points at this k window's routed src1 rows, row r at its base block
759template <typename B, int SUBBLK, bool HAS_MIN, int BIAS, bool ACTBIAS>
760static void tiled_mmid_gemm_window(struct ggml_tensor * dst, const struct ggml_tensor * src0,
761 const char * src0_cur, int64_t r, int64_t k, int64_t nrows,
762 const int32_t * expert_rows,
763 const block_q8_K * const * rows,
764 tiled_ws * ws) {
765 const int64_t ne00 = src0->ne[0];
766 const int64_t ne01 = src0->ne[1];
767
768 const int64_t r_end = MIN(r + TILED_MMID_GROUP, ne01);
769 const int n_src0 = (int) (r_end - r);
770
771 const size_t src0_bs = ggml_type_size(src0->type);
772 const int64_t src0_stride = src0->nb[1] / src0_bs;
773
774 // the window is at most TILED_MMID_GROUP x TILED_TILE_K, so zero only that region of acc
775 for (int64_t i = 0; i < n_src0; i++) {
776 memset(&ws->acc[i * TILED_TILE_ROWS], 0, nrows * sizeof(float));
777 }
778
779 // scattered writeback: column m goes to its routed dst row; r * nb[0] is the window row offset
780 float * col_ptrs[TILED_TILE_ROWS];
781 for (int64_t m = 0; m < nrows; m++) {
782 col_ptrs[m] = (float *) ((char *) dst->data + r * dst->nb[0] + expert_rows[2 * (k + m) + 0] * dst->nb[1] +
783 expert_rows[2 * (k + m) + 1] * dst->nb[2]);
784 }
785
786 // narrow path (small nrows): we're memory bound in this case so do longer, skinny tiled in order
787 // to have contiguous reads and improve memory bandwidth
788 if (nrows <= TILED_MICRO) {
789 const int k_extent = ggml_tiled_narrow_k_extent(nrows, ne00);
790 const int num_k = k_extent / TILED_TILE_K;
791 for (int64_t k0 = 0; k0 < ne00; k0 += k_extent) {
792 const int kstart = (int) (k0 / TILED_TILE_K);
793 // activation: one 16-row band (nrows <= 16). Unpack the whole k_extent chunk (all
794 // slabs) so each activation row is a long stream, then repack each slab in place
795 tiled_unpack_src1_q8_K(rows, nrows, &ws->src1, kstart, num_k);
796 // repack the whole k_extent chunk in place: num_k slabs x 4 tiles, 16 rows, row stride num_k*256
797 tiled_repack_src1(&ws->src1, 0, num_k, ACTBIAS);
798 // weight groups (16 at a time): unpack the k_extent chunk of the group (the long per-row
799 // read), then one standard MAC per slab accumulating into the same acc rows
800 for (int64_t ir0 = r; ir0 < r_end; ir0 += TILED_MICRO) {
801 const int n0 = (int) MIN(TILED_MICRO, r_end - ir0);
802 const B * wbase = (const B *) (src0_cur + ir0 * src0->nb[1] + kstart * src0_bs);
803 tiled_unpack_src0(wbase, src0_stride, n0, &ws->src0, num_k);
804 tiled_repack_src0<SUBBLK>(&ws->src0, n0, num_k, BIAS, ACTBIAS);
805 float * buf = ws->acc + (ir0 - r) * TILED_TILE_ROWS;
806 for (int slab = 0; slab < num_k; slab++) {
807 tiled_run_microtile<SUBBLK, HAS_MIN, BIAS, ACTBIAS>(ws->src0, ws->src1, 0, 0, num_k, slab, buf, TILED_TILE_ROWS);
808 }
809 }
810 }
811 } else {
812 // standard per-slab path: K is stepped in 256-element slabs
813 for (int64_t ib = 0; ib < ne00; ib += TILED_TILE_K) {
814 const int kblk = (int) (ib / TILED_TILE_K);
815 tiled_unpack_src0((const B *) (src0_cur + r * src0->nb[1] + kblk * src0_bs), src0_stride, n_src0, &ws->src0, 1);
816 tiled_repack_src0<SUBBLK>(&ws->src0, n_src0, 1, BIAS, ACTBIAS);
817 tiled_unpack_src1_q8_K(rows, nrows, &ws->src1, kblk, 1);
818 // 16x16 microtiles sweeping the window; repack each src1 band just-in-time
819 // (j0-outer) so only the bands actually used are repacked and each is L1-hot
820 for (int64_t ir1 = 0; ir1 < nrows; ir1 += TILED_MICRO) {
821 tiled_repack_src1(&ws->src1, (int) ir1, 1, ACTBIAS);
822 for (int64_t ir0 = r; ir0 < r_end; ir0 += TILED_MICRO) {
823 tiled_run_microtile<SUBBLK, HAS_MIN, BIAS, ACTBIAS>(ws->src0, ws->src1,
824 (int) (ir0 - r), (int) ir1, 1, 0,
825 ws->acc, TILED_TILE_ROWS);
826 }
827 }
828 }
829 }
830
831 tiled_store_window_scatter(ws->acc, n_src0, nrows, TILED_TILE_ROWS, col_ptrs);
832}
833
834// one expert of MUL_MAT_ID: each k window's dispatched rows are pointed at by a per-window
835// row pointer list and swept over the thread's row windows, the dst rows are scattered back
836// per window
837template <typename B, int SUBBLK, bool HAS_MIN, int BIAS, bool ACTBIAS>
838static void ggml_compute_forward_mul_mat_id_tiled_one_expert(
839 const struct ggml_compute_params * params,
840 struct ggml_tensor * dst,
841 int64_t cur_a,
842 int64_t cne1,
843 const int32_t * expert_rows,
844 char * scratch) {
845
846 const struct ggml_tensor * src0 = dst->src[0];
847 const struct ggml_tensor * src1 = dst->src[1];
848
849 GGML_TENSOR_BINARY_OP_LOCALS
850
851 const int ith = params->ith;
852 const int nth = params->nth;
853
854 const enum ggml_type vec_dot_type = ggml_get_type_traits_cpu(src0->type)->vec_dot_type;
855
856 const size_t nbw1 = (src1->type == vec_dot_type) ? nb11 : ggml_row_size(vec_dot_type, ne10);
857 const void * wdata = (src1->type == vec_dot_type) ? src1->data : params->wdata;
858
859 const char * src0_cur = (const char *) src0->data + cur_a * nb02;
860
861 // this thread's workspace
862 tiled_ws * ws = (tiled_ws *) (scratch + (size_t) ith * ggml_tiled_ws_size());
863
864 // groups of TILED_MMID_GROUP rows; rounded up, the window tail is clamped in the gemm
865 const int64_t ngroups = (ne01 + TILED_MMID_GROUP - 1) / TILED_MMID_GROUP;
866
867 const int64_t g0 = (ngroups * ith) / nth;
868 const int64_t g1 = (ngroups * (ith + 1)) / nth;
869
870 // no rows for this thread; nothing to do
871 if (g0 >= g1) {
872 return;
873 }
874
875 // MUL_MAT_ID scatters the src1 rows per routed row: point each k window's rows
876 // at the routed src1 rows, then sweep the row windows
877 for (int64_t k = 0; k < cne1; k += TILED_TILE_K) {
878 const int64_t nrows = MIN(TILED_TILE_K, cne1 - k);
879
880 const block_q8_K * rows[TILED_TILE_ROWS];
881 for (int64_t i = 0; i < nrows; i++) {
882 const int64_t i11 = expert_rows[2 * (k + i) + 0] % ne11;
883 const int64_t i12 = expert_rows[2 * (k + i) + 1];
884 rows[i] = (const block_q8_K *) ((const char *) wdata + (i11 + i12 * ne11) * nbw1);
885 }
886
887 for (int64_t g = g0; g < g1; g++) {
888 const int64_t r = g * TILED_MMID_GROUP;
889
890 tiled_mmid_gemm_window<B, SUBBLK, HAS_MIN, BIAS, ACTBIAS>(dst, src0, src0_cur, r, k, nrows, expert_rows, rows, ws);
891 }
892 }
893}
894
895template <typename B, int SUBBLK, bool HAS_MIN, int BIAS, bool ACTBIAS>
896static void ggml_compute_forward_mul_mat_tiled_one_chunk(
897 const struct ggml_compute_params * params,
898 struct ggml_tensor * dst,
899 const int64_t ir0_start,
900 const int64_t ir0_end,
901 const int64_t ir1_start,
902 const int64_t ir1_end,
903 tiled_ws * ws) {
904
905 const struct ggml_tensor * src0 = dst->src[0];
906 const struct ggml_tensor * src1 = dst->src[1];
907
908 GGML_TENSOR_BINARY_OP_LOCALS
909
910 const enum ggml_type vec_dot_type = ggml_get_type_traits_cpu(src0->type)->vec_dot_type;
911
912 // broadcast factors
913 const int64_t r2 = ne12 / ne02;
914 const int64_t r3 = ne13 / ne03;
915
916 if (ir0_start >= ir0_end || ir1_start >= ir1_end) {
917 return;
918 }
919
920 const void * wdata = (src1->type == vec_dot_type) ? src1->data : params->wdata;
921 const size_t row_size = ggml_row_size(vec_dot_type, ne10);
922 const size_t src0_bs = ggml_type_size(src0->type);
923 const size_t src1_bs = ggml_type_size(vec_dot_type);
924
925 GGML_ASSERT(ne00 % 256 == 0);
926 assert(ne12 % ne02 == 0);
927 assert(ne13 % ne03 == 0);
928
929 const int64_t src0_stride = nb01 / src0_bs; // blocks between src0 rows
930 const int64_t src1_stride = (src1->type == vec_dot_type ? src1->nb[1] : row_size) / src1_bs;
931
932 const int64_t TILE = 256;
933 const int64_t MICRO = 16;
934
935 // 256-wide windows over the chunk. The iir1 window is additionally clamped at the
936 // src1 batch (ne11) boundary: the tiles require a constant batch index (i12/i13)
937 // within a window, so advance by the clamped end, not a fixed 256.
938 for (int64_t iir1 = ir1_start; iir1 < ir1_end; ) {
939 int64_t iir1_end = MIN(iir1 + TILE, ir1_end);
940 const int64_t bnd = (iir1 / ne11 + 1) * ne11;
941 if (bnd < iir1_end) {
942 iir1_end = bnd;
943 }
944
945 const int n_src1 = (int) (iir1_end - iir1);
946
947 // batch coords, constant within the clamped window
948 const int64_t i13 = iir1 / (ne12 * ne11);
949 const int64_t i12 = (iir1 - i13 * ne12 * ne11) / ne11;
950 // within-batch row; dst_col below holds the i12/i13 batch offset, so the store
951 // applies i11 * nb1 (not the flattened iir1, which spans all batch dims)
952 const int64_t i11 = iir1 - i13 * ne12 * ne11 - i12 * ne11;
953
954 // dst batches == src1 batches (ggml_mul_mat), so the loop batch coords are in
955 // src1 space; src0 batches are broadcast over them, map down into src0's batch
956 const int64_t i02 = i12 / r2;
957 const int64_t i03 = i13 / r3;
958
959 const char * src0_row = (const char *) src0->data + i02 * src0->nb[2] + i03 * src0->nb[3];
960 char * dst_col = (char *) dst->data + i12 * nb2 + i13 * nb3;
961
962 // rows[r] is the window's row r base block; the k-slab offset is applied in the unpack
963 const block_q8_K * rows[TILED_TILE_ROWS];
964 for (int r = 0; r < n_src1; r++) {
965 rows[r] = (const block_q8_K *) ((const char *) wdata + (iir1 + r) * src1_stride * src1_bs);
966 }
967
968 for (int64_t iir0 = ir0_start; iir0 < ir0_end; iir0 += TILE) {
969 int64_t iir0_end = MIN(iir0 + TILE, ir0_end);
970 const int n_src0 = (int) (iir0_end - iir0);
971
972 // result buffer zeroed once per macrotile
973 memset(ws->acc, 0, (size_t)TILED_TILE_ROWS * TILED_TILE_ROWS * sizeof(float));
974
975 if (n_src1 <= TILED_MICRO) {
976 // narrow path: if we have few enough rows, we're memory bound
977 // Do longer, skinner tiles so we have longer contiguous reads and improve bandwidth
978 const int k_extent = ggml_tiled_narrow_k_extent(n_src1, ne00);
979 const int num_k = k_extent / TILED_TILE_K;
980 for (int64_t k0 = 0; k0 < ne00; k0 += k_extent) {
981 const int kstart = (int) (k0 / TILED_TILE_K);
982 // activation: one 16-row band (n_src1 <= 16). Unpack the whole k_extent chunk
983 // (all slabs) so each activation row is a long stream, then repack each slab in place
984 tiled_unpack_src1_q8_K(rows, n_src1, &ws->src1, kstart, num_k);
985 // repack the whole k_extent chunk in place, if kernel wants to: num_k slabs x 4 tiles, 16 rows, row stride num_k*256
986 tiled_repack_src1(&ws->src1, 0, num_k, ACTBIAS);
987 // weight groups: unpack the k_extent chunk of each 16-row group (the long per-row
988 // read), then one standard MAC per slab accumulating into the same acc rows
989 for (int64_t ir0 = iir0; ir0 < iir0_end; ir0 += MICRO) {
990 const int n0 = (int) MIN(MICRO, iir0_end - ir0);
991 const B * wbase = (const B *) (src0_row + ir0 * nb01 + kstart * src0_bs);
992 tiled_unpack_src0(wbase, src0_stride, n0, &ws->src0, num_k);
993 tiled_repack_src0<SUBBLK>(&ws->src0, n0, num_k, BIAS, ACTBIAS);
994 float * buf = ws->acc + (ir0 - iir0) * TILED_TILE_ROWS;
995 for (int slab = 0; slab < num_k; slab++) {
996 tiled_run_microtile<SUBBLK, HAS_MIN, BIAS, ACTBIAS>(ws->src0, ws->src1, 0, 0, num_k, slab, buf, TILED_TILE_ROWS);
997 }
998 }
999 }
1000 } else {
1001 // standard per-slab path: K is stepped in 256-element chunks
1002 for (int64_t ib = 0; ib < ne00; ib += TILE) {
1003 const int kblk = (int) (ib / TILE);
1004 tiled_unpack_src0((const B *) (src0_row + iir0 * nb01 + kblk * src0_bs), src0_stride, n_src0, &ws->src0, 1);
1005 tiled_repack_src0<SUBBLK>(&ws->src0, n_src0, 1, BIAS, ACTBIAS);
1006 tiled_unpack_src1_q8_K(rows, n_src1, &ws->src1, kblk, 1);
1007
1008 // 16x16 microtiles sweeping the window; repack each src1 band before first use
1009 for (int64_t ir1 = iir1; ir1 < iir1_end; ir1 += MICRO) {
1010 tiled_repack_src1(&ws->src1, (int) (ir1 - iir1), 1, ACTBIAS);
1011 for (int64_t ir0 = iir0; ir0 < iir0_end; ir0 += MICRO) {
1012 tiled_run_microtile<SUBBLK, HAS_MIN, BIAS, ACTBIAS>(ws->src0, ws->src1,
1013 (int) (ir0 - iir0), (int) (ir1 - iir1), 1, 0,
1014 ws->acc, TILED_TILE_ROWS);
1015 }
1016 }
1017 }
1018 }
1019 // write acc back out from L2 to main memory
1020 tiled_store_window(ws->acc, n_src0, n_src1, TILED_TILE_ROWS,
1021 (float *) (dst_col + iir0 * nb0 + i11 * nb1), nb1 / nb0);
1022 }
1023 iir1 = iir1_end;
1024 }
1025}
1026
1027template <typename B, int SUBBLK, bool HAS_MIN, int BIAS, bool ACTBIAS>
1028static void ggml_compute_forward_mul_mat_tiled_driver(
1029 const struct ggml_compute_params * params,
1030 struct ggml_tensor * dst) {
1031
1032 const struct ggml_tensor * src0 = dst->src[0];
1033 const struct ggml_tensor * src1 = dst->src[1];
1034
1035 GGML_TENSOR_BINARY_OP_LOCALS
1036
1037 const int ith = params->ith;
1038 const int nth = params->nth;
1039
1040 enum ggml_type const vec_dot_type = ggml_get_type_traits_cpu(src0->type)->vec_dot_type;
1041 ggml_from_float_t const from_float = ggml_get_type_traits_cpu(vec_dot_type)->from_float;
1042
1043 GGML_ASSERT(ne0 == ne01);
1044 GGML_ASSERT(ne1 == ne11);
1045 GGML_ASSERT(ne2 == ne12);
1046 GGML_ASSERT(ne3 == ne13);
1047
1048 // we don't support permuted src0 or src1
1049 GGML_ASSERT(nb10 == ggml_type_size(src1->type));
1050
1051 // dst cannot be transposed or permuted
1052 GGML_ASSERT(nb0 == sizeof(float));
1053 GGML_ASSERT(nb0 <= nb1);
1054 GGML_ASSERT(nb1 <= nb2);
1055 GGML_ASSERT(nb2 <= nb3);
1056
1057 if (src1->type != vec_dot_type) {
1058 char * wdata = (char *) params->wdata;
1059
1060 const size_t nbw0 = ggml_type_size(vec_dot_type);
1061 const size_t nbw1 = ggml_row_size(vec_dot_type, ne10);
1062 const size_t nbw2 = nbw1*ne11;
1063 const size_t nbw3 = nbw2*ne12;
1064
1065 assert(params->wsize >= ne13*nbw3);
1066 GGML_ASSERT(src1->type == GGML_TYPE_F32);
1067
1068 for (int64_t i13 = 0; i13 < ne13; ++i13) {
1069 for (int64_t i12 = 0; i12 < ne12; ++i12) {
1070 for (int64_t i11 = 0; i11 < ne11; ++i11) {
1071 size_t bs = ggml_blck_size(vec_dot_type);
1072 int64_t ne10_block_start = (ith * ne10/bs) / nth;
1073 int64_t ne10_block_end = ((ith + 1) * ne10/bs) / nth;
1074 from_float((float *)((char *) src1->data + i13*src1->nb[3] + i12*src1->nb[2] + i11*src1->nb[1] + ne10_block_start*bs*src1->nb[0]),
1075 (void *) (wdata + i13*nbw3 + i12*nbw2 + i11*nbw1 + ne10_block_start*nbw0),
1076 (ne10_block_end - ne10_block_start) * bs);
1077 }
1078 }
1079 }
1080 }
1081
1082 if (ith == 0) {
1083 // Every thread starts at ith, so the first unprocessed chunk is nth. This saves a bit of coordination right at the start.
1084 ggml_threadpool_chunk_set(params->threadpool, nth);
1085 }
1086
1087 ggml_barrier(params->threadpool);
1088
1089 // This is the size of the first dimension of the result, so we can iterate that way. (see the ASSERT above, these are the same numbers)
1090 const int64_t nr0 = ne0;
1091
1092 // This is the size of the rest of the dimensions of the result
1093 const int64_t nr1 = ne1 * ne2 * ne3;
1094
1095 // Now select a reasonable chunk size.
1096 int chunk_size = 256; //TILED_TILE_ROWS;
1097
1098 // distribute the work across the inner or outer loop based on which one is larger
1099 // The number of chunks in the 0/1 dim. CEIL(nr/chunk_size)
1100 int64_t nchunk0 = (nr0 + chunk_size - 1) / chunk_size;
1101 int64_t nchunk1 = (nr1 + chunk_size - 1) / chunk_size;
1102
1103 // Step down chunk size if too few chunks to saturate cores, minimum is microtile size
1104 while (nchunk0 * nchunk1 < nth * 4 && chunk_size > 16) {
1105 chunk_size = chunk_size / 2;
1106 nchunk0 = (nr0 + chunk_size - 1) / chunk_size;
1107 nchunk1 = (nr1 + chunk_size - 1) / chunk_size;
1108 }
1109
1110 // The number of elements in each chunk
1111 const int64_t dr0 = (nr0 + nchunk0 - 1) / nchunk0;
1112 const int64_t dr1 = (nr1 + nchunk1 - 1) / nchunk1;
1113
1114 // The first chunk comes from our thread_id, the rest will get auto-assigned.
1115 int current_chunk = ith;
1116
1117 // per-thread workspace: after any converted src1 data in wdata, aligned to 64
1118 char * ws_base = (char *) params->wdata;
1119 if (src1->type != vec_dot_type) {
1120 ws_base += GGML_PAD(ggml_row_size(vec_dot_type, ggml_nelements(src1)), 64);
1121 }
1122 ws_base = (char *) (((uintptr_t) ws_base + 63) & ~(uintptr_t) 63);
1123 tiled_ws * ws = (tiled_ws *) (ws_base + (size_t) ith * ggml_tiled_ws_size());
1124
1125 // TODO: if we KNOW we're on a machine where all cores are equal, we could skip the coordination/work-stealing and just assign chunks deterministically
1126 while (current_chunk < nchunk0 * nchunk1) {
1127 const int64_t ith0 = current_chunk % nchunk0;
1128 const int64_t ith1 = current_chunk / nchunk0;
1129
1130 const int64_t ir0_start = dr0 * ith0;
1131 const int64_t ir0_end = MIN(ir0_start + dr0, nr0);
1132
1133 const int64_t ir1_start = dr1 * ith1;
1134 const int64_t ir1_end = MIN(ir1_start + dr1, nr1);
1135
1136 ggml_compute_forward_mul_mat_tiled_one_chunk<B, SUBBLK, HAS_MIN, BIAS, ACTBIAS>(params, dst, ir0_start, ir0_end, ir1_start, ir1_end, ws);
1137
1138 if (nth >= nchunk0 * nchunk1) {
1139 break;
1140 }
1141
1142 current_chunk = ggml_threadpool_chunk_add(params->threadpool, 1);
1143 }
1144}
1145
1146// src0 type dispatch, shared by the MUL_MAT and MUL_MAT_ID entries: one expert for
1147// MUL_MAT_ID (expert_rows != NULL), the full op for MUL_MAT
1148template <typename B, int SUBBLK, bool HAS_MIN, int BIAS, bool ACTBIAS>
1149static bool tiled_matmul_dispatch(const struct ggml_compute_params * params,
1150 struct ggml_tensor * dst,
1151 const int32_t * expert_rows,
1152 int64_t cur_a,
1153 int64_t cne1,
1154 char * scratch) {
1155 if (expert_rows == NULL) {
1156 ggml_compute_forward_mul_mat_tiled_driver<B, SUBBLK, HAS_MIN, BIAS, ACTBIAS>(params, dst);
1157 } else {
1158 ggml_compute_forward_mul_mat_id_tiled_one_expert<B, SUBBLK, HAS_MIN, BIAS, ACTBIAS>(params, dst, cur_a, cne1, expert_rows, scratch);
1159 }
1160 return true;
1161}
1162
1163// the supported src0 types, one list for both ops; false to fall through to vec_dot
1164static bool ggml_tiled_matmul_type_dispatch(const struct ggml_compute_params * params,
1165 struct ggml_tensor * dst,
1166 const int32_t * expert_rows = NULL,
1167 int64_t cur_a = 0,
1168 int64_t cne1 = 0,
1169 char * scratch = NULL) {
1170 switch (dst->src[0]->type) {
1171 case GGML_TYPE_Q6_K:
1172 return tiled_matmul_dispatch<block_q6_K, 16, false, 32, true>(params, dst, expert_rows, cur_a, cne1, scratch);
1173 case GGML_TYPE_Q5_K:
1174 return tiled_matmul_dispatch<block_q5_K, 32, true, 0, false>(params, dst, expert_rows, cur_a, cne1, scratch);
1175 case GGML_TYPE_Q4_K:
1176 return tiled_matmul_dispatch<block_q4_K, 32, true, 0, false>(params, dst, expert_rows, cur_a, cne1, scratch);
1177 case GGML_TYPE_Q3_K:
1178 return tiled_matmul_dispatch<block_q3_K, 16, false, 4, true>(params, dst, expert_rows, cur_a, cne1, scratch);
1179 case GGML_TYPE_Q2_K:
1180 return tiled_matmul_dispatch<block_q2_K, 16, true, 0, false>(params, dst, expert_rows, cur_a, cne1, scratch);
1181 case GGML_TYPE_IQ4_XS:
1182 return tiled_matmul_dispatch<block_iq4_xs, 32, false, 128, false>(params, dst, expert_rows, cur_a, cne1, scratch);
1183 case GGML_TYPE_IQ2_XXS:
1184 return tiled_matmul_dispatch<block_iq2_xxs, 32, false, 128, true>(params, dst, expert_rows, cur_a, cne1, scratch);
1185 case GGML_TYPE_IQ2_XS:
1186 return tiled_matmul_dispatch<block_iq2_xs, 16, false, 128, true>(params, dst, expert_rows, cur_a, cne1, scratch);
1187 case GGML_TYPE_IQ2_S:
1188 return tiled_matmul_dispatch<block_iq2_s, 16, false, 128, true>(params, dst, expert_rows, cur_a, cne1, scratch);
1189 case GGML_TYPE_IQ3_XXS:
1190 return tiled_matmul_dispatch<block_iq3_xxs, 32, false, 128, true>(params, dst, expert_rows, cur_a, cne1, scratch);
1191 case GGML_TYPE_IQ3_S:
1192 return tiled_matmul_dispatch<block_iq3_s, 32, false, 128, true>(params, dst, expert_rows, cur_a, cne1, scratch);
1193 case GGML_TYPE_IQ1_S:
1194 return tiled_matmul_dispatch<block_iq1_s, 32, false, 128, true>(params, dst, expert_rows, cur_a, cne1, scratch);
1195 case GGML_TYPE_IQ1_M:
1196 return tiled_matmul_dispatch<block_iq1_m, 16, false, 128, true>(params, dst, expert_rows, cur_a, cne1, scratch);
1197 default:
1198 return false;
1199 }
1200}
1201
1202static bool ggml_tiled_min_batch(int64_t rows) {
1203 // Profitable at rows >= 8, take even when unprofitable if we're forced
1204 return rows >= 8 || ggml_tiled_matmul_forced();
1205}
1206
1207// tiled K-quant matmul; returns true if the op was computed here,
1208// false to fall through to the stock path
1209bool ggml_compute_forward_mul_mat_tiled(
1210 const struct ggml_compute_params * params,
1211 struct ggml_tensor * dst) {
1212 // --use-ref means bail out and go back to vec_dot reference impl
1213 if (params->use_ref) {
1214 return false;
1215 }
1216 if (!ggml_tiled_supported(dst->src[0], dst->src[1])) {
1217 return false;
1218 }
1219 if (!ggml_tiled_min_batch(dst->src[1]->ne[1])) {
1220 return false;
1221 }
1222 return ggml_tiled_matmul_type_dispatch(params, dst);
1223}
1224
1225// MUL_MAT_ID (MoE), one expert; returns true if the expert was computed here,
1226// per expert eligibility (type gate, batch floor) is decided here
1227bool ggml_compute_forward_mul_mat_id_tiled(
1228 const struct ggml_compute_params * params,
1229 struct ggml_tensor * dst,
1230 int64_t cur_a,
1231 int64_t cne1,
1232 const int32_t * expert_rows,
1233 char * scratch) {
1234 if (params->use_ref) {
1235 return false;
1236 }
1237 if (!ggml_tiled_supported(dst->src[0], dst->src[1])) {
1238 return false;
1239 }
1240 // profitability is per expert: the rows routed to this expert
1241 if (!ggml_tiled_min_batch(cne1)) {
1242 return false;
1243 }
1244 return ggml_tiled_matmul_type_dispatch(params, dst, expert_rows, cur_a, cne1, scratch);
1245}