← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
routed_softcapped_experts_checked.c
Go to the documentation of this file.
2#include "checked_parallel.h"
6#include "packed_iq2_xs_dot.h"
7#include "packed_iq2_s_dot.h"
9#include "packed_iq3_s_dot.h"
10#include "packed_q3_k_dot.h"
13
14#include <string.h>
15#include <stdatomic.h>
16
31
32#define IQ4_NL_BLOCK_BYTES 18
33
34/* GGML IQ4_NL codebook and split-half nibble layout. */
35static const int8_t iq4_nl_values[16] = {
36 -127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113
37};
38
39static size_t packed_block_bytes(enum expert_storage storage)
40{
41 switch (storage) {
42 case EXPERT_Q8_0: return sizeof(block_q8_0);
43 case EXPERT_Q4_K: return sizeof(block_q4_K);
44 case EXPERT_Q5_0: return sizeof(block_q5_0);
45 case EXPERT_Q5_1: return sizeof(block_q5_1);
47 case EXPERT_IQ2_XS: return sizeof(ck_block_iq2_xs);
48 case EXPERT_IQ2_S: return sizeof(ck_block_iq2_s);
49 case EXPERT_IQ3_XXS: return sizeof(ck_block_iq3_xxs);
50 case EXPERT_IQ3_S: return sizeof(ck_block_iq3_s);
51 case EXPERT_Q3_K: return sizeof(ck_block_q3_k);
52 case EXPERT_NVFP4: return sizeof(block_nvfp4);
53 default: return sizeof(float);
54 }
55}
56
57static size_t packed_block_elements(enum expert_storage storage)
58{
59 switch (storage) {
60 case EXPERT_F32: return 1;
61 case EXPERT_Q4_K:
62 case EXPERT_IQ2_XS:
63 case EXPERT_IQ2_S:
64 case EXPERT_IQ3_XXS:
65 case EXPERT_IQ3_S:
66 case EXPERT_Q3_K: return QK_K;
67 case EXPERT_NVFP4: return QK_NVFP4;
68 case EXPERT_Q5_0: return QK5_0;
69 case EXPERT_Q5_1: return QK5_1;
70 default: return QK8_0;
71 }
72}
73
74/* Packed blocks are read in GGUF byte order and expanded inside the dot.
75 * The FP32 activation and ordered FP64 accumulation policy is shared. */
76static float expert_dot(const float *input, const unsigned char *weight,
77 size_t width, enum expert_storage storage, const unsigned char *activation)
78{
79 if (activation) return ck_q8_activation_dot(weight, activation, width);
80 if (storage == EXPERT_Q8_0) return ck_packed_q8_dot_ordered(input, weight, width);
81 if (storage == EXPERT_Q4_K) return ck_packed_q4_k_dot_ordered(input, weight, width);
82 if (storage == EXPERT_Q5_0) return ck_packed_q5_0_dot_ordered(input, weight, width);
83 if (storage == EXPERT_Q3_K) return ck_packed_q3_k_dot_ordered(input, weight, width);
84 if (storage == EXPERT_Q5_1) return ck_packed_q5_1_dot_ordered(input, weight, width);
85 double sum = 0.0;
86 if (storage == EXPERT_F32) {
87 for (size_t i = 0; i < width; ++i) {
88 float value;
89 memcpy(&value, weight + i * sizeof(float), sizeof(float));
90 sum += (double)input[i] * (double)value;
91 }
92 } else {
93 const size_t block_bytes = packed_block_bytes(storage);
94 for (size_t i = 0; i < width; i += QK8_0, weight += block_bytes) {
95 const float scale = ck_fp16_to_fp32_soft(
96 (uint16_t)(weight[0] | ((uint16_t)weight[1] << 8)));
97 for (size_t j = 0; j < QK8_0; ++j) {
98 const unsigned pair = weight[2 + j % 16];
99 const float expanded = scale * (float)iq4_nl_values[j < 16 ? pair & 15 : pair >> 4];
100 sum += (double)input[i + j] * (double)expanded;
101 }
102 }
103 }
104 return (float)sum;
105}
106
107static int contains_overlap(const void *p, size_t pn, const void *q, size_t qn)
108{
109 return (uintptr_t)p < (uintptr_t)q + qn && (uintptr_t)q < (uintptr_t)p + pn;
110}
111
112static int region(const void *p, size_t bytes)
113{
114 return p && bytes && (uintptr_t)p <= UINTPTR_MAX - bytes;
115}
116
117#define EXPERT_ARGS \
118 const float *hidden, const int32_t *ids, const float *route_weights, \
119 const int32_t *expert_slots, const void *gate, const void *up, const void *down, \
120 float *expert_output, float *output, float *work, \
121 size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, \
122 size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, \
123 size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, \
124 size_t work_elements
125
126#define ROUTED_EXPERT_ARGS EXPERT_ARGS, float gate_cap, float up_cap
127
128struct packed_k_expert_ops {
129 int (*pack)(const float *, unsigned char *, size_t);
130 void (*matvec)(float *, const void *, const void *, int, int);
131 void (*down_q5)(float *, const void *, const void *, int, int);
132 void (*down_q5_pair)(float *, float *, const void *, const void *, const void *, int, int);
133 void (*down_mixed)(float *, const void *, const void *, int, int);
134 void (*matvec_pair)(float *, float *, const void *, const void *, const void *, int, int);
135 int group_rows;
136 int dispatch_pairs;
137 int hidden_q8_0;
138 int require_scales;
139};
140
141static const struct packed_k_expert_ops q4_hidden = {
142 .pack = ck_pack_q4_input_q8_k,
143 .matvec = gemv_q4_k_q8_k_avx2,
144};
145
146void gemv_q5_0_q8_0_checked_cpu(float *, const void *, const void *, int, int);
147void gemv_q5_0_q8_0_pair_checked_cpu(float *, float *, const void *, const void *, const void *, int, int);
148static const struct packed_k_expert_ops q4_hidden_and_down = {
149 .pack = ck_pack_q4_input_q8_k,
150 .matvec = gemv_q4_k_q8_k_avx2,
152};
153
154static const struct packed_k_expert_ops q4_grouped = {
155 .pack = ck_pack_q4_input_q8_k,
156 .matvec = gemv_q4_k_q8_k_avx2,
158 .group_rows = 1,
159};
160
161static const struct packed_k_expert_ops q4_grouped_pairs = {
162 .pack = ck_pack_q4_input_q8_k,
163 .matvec = gemv_q4_k_q8_k_avx2,
165 .down_q5_pair = gemv_q5_0_q8_0_pair_checked_cpu,
166 .matvec_pair = gemv_q4_k_q8_k_pair_checked_cpu,
167 .group_rows = 1,
168};
169
170static const struct packed_k_expert_ops q4_pair_jobs = {
171 .pack = ck_pack_q4_input_q8_k,
172 .matvec = gemv_q4_k_q8_k_avx2,
174 .down_q5_pair = gemv_q5_0_q8_0_pair_checked_cpu,
175 .matvec_pair = gemv_q4_k_q8_k_pair_checked_cpu,
176 .group_rows = 1,
177 .dispatch_pairs = 1,
178};
179
180struct routed_projection {
181 const unsigned char *gate, *up, *down;
182 const float *gate_up_scales, *down_scales;
183 size_t width, inner, gate_row, down_row, gate_expert_bytes, down_expert_bytes;
184 size_t packed_hidden_bytes;
185 float gate_cap, up_cap, gate_reciprocal, up_reciprocal;
186 enum expert_storage storage, down_storage;
187 int quantize, reference, uncapped;
188 const struct packed_k_expert_ops *packed_k;
189};
190
191static unsigned char *expert_packed_activation(const struct routed_projection *p, float *scratch)
192{
193 unsigned char *after_activation = (unsigned char *)(scratch + 3 * p->inner);
194 if (p->packed_k && (p->packed_k->down_q5 || p->packed_k->down_mixed))
195 return after_activation + p->packed_hidden_bytes;
196 return p->quantize ? after_activation + p->gate_row : NULL;
197}
198
199static int prepare_expert_activation(const struct routed_projection *p, float *scratch,
200 unsigned char *packed_activation)
201{
202 float *gate_values = scratch, *up_values = gate_values + p->inner, *activation = up_values + p->inner;
203 if (p->reference) {
204 for (size_t i = 0; i < p->inner; ++i) {
205 const float g = gate_values[i], u = up_values[i];
206 if (!isfinite(g) || !isfinite(u)) return -3;
207 const float capped_gate = p->gate_cap * tanhf(g * p->gate_reciprocal);
208 const float capped_up = p->up_cap * tanhf(u * p->up_reciprocal);
209 const float sigmoid = 1.0f / (1.0f + expf(-g));
210 activation[i] = (capped_gate * sigmoid) * capped_up;
211 if (!isfinite(activation[i])) return -3;
212 }
213 } else if (p->uncapped) {
214 if (silu_mul_rows_checked_f32(gate_values, p->inner, up_values, p->inner,
215 activation, p->inner, p->inner)) return -3;
216 } else if (softcapped_swiglu_rows_checked_f32(gate_values, p->inner, up_values, p->inner,
217 activation, p->inner, p->inner, p->gate_cap, p->up_cap)) return -3;
218 return packed_activation ? ck_pack_q8_activation_rne(activation, packed_activation, p->inner) : 0;
219}
220
221static int finish_expert(const struct routed_projection *p, size_t slot, float *scratch, float *output)
222{
223 const unsigned char *down = p->down + slot * p->down_expert_bytes;
224 const float *activation = scratch + 2 * p->inner;
225 unsigned char *packed_activation = expert_packed_activation(p, scratch);
226 if (prepare_expert_activation(p, scratch, packed_activation)) return -3;
227 if (p->packed_k && p->packed_k->down_mixed) {
228 p->packed_k->down_mixed(output, down, packed_activation, (int)p->width, (int)p->inner);
229 if (p->down_scales)
230 for (size_t i = 0; i < p->width; ++i) output[i] *= p->down_scales[slot];
231 if (!ck_checked_finite(output, p->width)) return -3;
232 } else if (p->packed_k && p->packed_k->down_q5 && p->down_storage == EXPERT_Q5_0) {
233 p->packed_k->down_q5(output, down, packed_activation, (int)p->width, (int)p->inner);
234 if (!ck_checked_finite(output, p->width)) return -3;
235 } else {
236 for (size_t i = 0; i < p->width; ++i) {
237 output[i] = expert_dot(activation, down + i * p->down_row, p->inner, p->down_storage, packed_activation);
238 if (!isfinite(output[i])) return -3;
239 }
240 }
241 return 0;
242}
243
244static int project_expert(const struct routed_projection *p, const float *input,
245 const unsigned char *packed_hidden, size_t slot, float *scratch,
246 float *output)
247{
248 const size_t offset = slot * p->gate_expert_bytes;
249 const unsigned char *gate = p->gate + offset, *up = p->up + offset;
250 float *gate_values = scratch, *up_values = scratch + p->inner;
251 if (p->packed_k) {
252 p->packed_k->matvec(gate_values, gate, packed_hidden, (int)p->inner, (int)p->width);
253 p->packed_k->matvec(up_values, up, packed_hidden, (int)p->inner, (int)p->width);
254 } else {
255 for (size_t i = 0; i < p->inner; ++i) {
256 if (packed_hidden) {
257 ck_q8_activation_dot_pair(gate + i * p->gate_row, up + i * p->gate_row, packed_hidden,
258 p->width, gate_values + i, up_values + i);
259 } else {
260 gate_values[i] = expert_dot(input, gate + i * p->gate_row, p->width, p->storage, NULL);
261 up_values[i] = expert_dot(input, up + i * p->gate_row, p->width, p->storage, NULL);
262 }
263 }
264 }
265 if (p->gate_up_scales) {
266 for (size_t i = 0; i < p->inner; ++i) {
267 gate_values[i] *= p->gate_up_scales[slot];
268 up_values[i] *= p->gate_up_scales[slot];
269 }
270 }
271 return finish_expert(p, slot, scratch, output);
272}
273
274static int project_expert_pair(const struct routed_projection *p, size_t slot,
275 const unsigned char *x0, const unsigned char *x1,
276 float *work0, float *work1, float *out0, float *out1)
277{
278 const size_t offset = slot * p->gate_expert_bytes;
279 p->packed_k->matvec_pair(work0, work1, p->gate + offset, x0, x1, (int)p->inner, (int)p->width);
280 p->packed_k->matvec_pair(work0 + p->inner, work1 + p->inner, p->up + offset,
281 x0, x1, (int)p->inner, (int)p->width);
282 if (p->packed_k->down_q5_pair && p->down_storage == EXPERT_Q5_0) {
283 unsigned char *packed0 = expert_packed_activation(p, work0);
284 unsigned char *packed1 = expert_packed_activation(p, work1);
285 if (prepare_expert_activation(p, work0, packed0) || prepare_expert_activation(p, work1, packed1))
286 return -3;
287 p->packed_k->down_q5_pair(out0, out1, p->down + slot * p->down_expert_bytes,
288 packed0, packed1, (int)p->width, (int)p->inner);
289 return ck_checked_finite(out0, p->width) && ck_checked_finite(out1, p->width) ? 0 : -3;
290 }
291 if (finish_expert(p, slot, work0, out0)) return -3;
292 return finish_expert(p, slot, work1, out1);
293}
294
295static inline int combine_expert(const float *expert, float *mixture, size_t width,
296 float weight, size_t index, int reference)
297{
298 for (size_t i = 0; i < width; ++i) {
299 if (reference) {
300 const volatile float weighted = expert[i] * weight;
301 mixture[i] = index == 0 ? weighted : mixture[i] + weighted;
302 } else {
303 mixture[i] += expert[i] * weight;
304 }
305 if (!isfinite(mixture[i])) return -3;
306 }
307 return 0;
308}
309
310static void order_slots(const int32_t *ids, size_t experts, size_t top_k, size_t *ordered_slots)
311{
312 size_t next = 0;
313 for (size_t id = 0; id < experts; ++id)
314 for (size_t slot = 0; slot < top_k; ++slot)
315 if ((size_t)ids[slot] == id) ordered_slots[next++] = slot;
316}
317
318struct expert_pair_job {
319 uint32_t first, second;
320};
321
322_Static_assert(sizeof(struct expert_pair_job) == 2 * sizeof(float) &&
323 _Alignof(struct expert_pair_job) <= _Alignof(float),
324 "expert job workspace layout changed");
325
326struct routed_work {
327 const struct routed_projection *projection;
328 const float *hidden, *route_weights;
329 const int32_t *ids, *slots;
330 float *row_work, *staged_experts, *staged_output;
331 unsigned char *packed_rows;
332 const struct expert_pair_job *jobs;
333 const ck_threadpool_t *pool;
334 size_t packed_row_bytes;
335 size_t row_scratch, scratch_rows, rows, experts, top_k;
336 atomic_int failed;
337};
338
339static unsigned char *hidden_workspace(const struct routed_projection *p, float *scratch)
340{
341 return p->quantize || p->packed_k ? (unsigned char *)(scratch + 3 * p->inner) : NULL;
342}
343
344static int pack_hidden(const struct routed_projection *p, const float *input, unsigned char *packed)
345{
346 if (!packed) return 0;
347 if (p->packed_k) return p->packed_k->pack(input, packed, p->width);
348 return ck_pack_q8_activation_rne(input, packed, p->width);
349}
350
351static void pack_grouped_rows(size_t begin, size_t end, void *data)
352{
353 struct routed_work *work = data;
354 const struct routed_projection *p = work->projection;
355 for (size_t row = begin; row < end; ++row) {
356 if (pack_hidden(p, work->hidden + row * p->width,
357 work->packed_rows + row * work->packed_row_bytes)) {
358 atomic_store_explicit(&work->failed, 1, memory_order_relaxed);
359 return;
360 }
361 }
362}
363
364static void mix_grouped_rows(size_t begin, size_t end, void *data)
365{
366 struct routed_work *work = data;
367 const size_t width = work->projection->width;
368 for (size_t row = begin; row < end; ++row) {
369 size_t ordered_slots[1024];
370 const size_t first = row * work->top_k;
371 order_slots(work->ids + first, work->experts, work->top_k, ordered_slots);
372 for (size_t index = 0; index < work->top_k; ++index) {
373 const size_t route = first + ordered_slots[index];
374 if (combine_expert(work->staged_experts + route * width,
375 work->staged_output + row * width, width,
376 work->route_weights[route], index, 0)) {
377 atomic_store_explicit(&work->failed, 1, memory_order_relaxed);
378 return;
379 }
380 }
381 }
382}
383
384static void project_routes(size_t begin, size_t end, void *data)
385{
386 struct routed_work *work = data;
387 const struct routed_projection *p = work->projection;
388 for (size_t route = begin; route < end; ++route) {
389 const unsigned char *packed_hidden = hidden_workspace(p, work->row_work);
390 if (project_expert(p, work->hidden, packed_hidden, work->slots[work->ids[route]],
391 work->row_work + route * work->row_scratch,
392 work->staged_experts + route * p->width)) {
393 atomic_store_explicit(&work->failed, 1, memory_order_relaxed);
394 return;
395 }
396 }
397}
398
399static void project_rows(size_t begin, size_t end, void *data)
400{
401 struct routed_work *work = data;
402 const struct routed_projection *p = work->projection;
403 for (size_t row = begin; row < end; ++row) {
404 float *scratch = work->row_work + (work->scratch_rows > 1 ? row * work->row_scratch : 0);
405 unsigned char *packed_hidden = hidden_workspace(p, scratch);
406 const float *input = work->hidden + row * p->width;
407 if (pack_hidden(p, input, packed_hidden)) {
408 atomic_store_explicit(&work->failed, 1, memory_order_relaxed);
409 return;
410 }
411 size_t ordered_slots[1024];
412 if (!p->reference) order_slots(work->ids + row * work->top_k, work->experts, work->top_k, ordered_slots);
413 for (size_t index = 0; index < work->top_k; ++index) {
414 const size_t route = row * work->top_k + (p->reference ? index : ordered_slots[index]);
415 float *expert = work->staged_experts + route * p->width;
416 if (project_expert(p, input, packed_hidden, work->slots[work->ids[route]], scratch, expert) ||
417 combine_expert(expert, work->staged_output + row * p->width, p->width,
418 work->route_weights[route], index, p->reference)) {
419 atomic_store_explicit(&work->failed, 1, memory_order_relaxed);
420 return;
421 }
422 }
423 }
424}
425
426/* Keep each expert's weights hot across routed rows. Route results remain in
427 * their original slots and are mixed only after all expert work completes. */
428static void project_grouped_experts(size_t begin, size_t end, void *opaque)
429{
430 struct routed_work *work = opaque;
431 const struct routed_projection *p = work->projection;
432 const size_t scratch_per_expert = work->row_scratch * (p->packed_k->matvec_pair ? 2 : 1);
433 for (size_t id = begin; id < end; ++id) {
434 if (work->slots[id] < 0) continue;
435 float *scratch = work->row_work + id * scratch_per_expert;
436 size_t pending = SIZE_MAX;
437 for (size_t row = 0; row < work->rows; ++row) {
438 const unsigned char *packed = work->packed_rows + row * work->packed_row_bytes;
439 for (size_t i = 0; i < work->top_k; ++i) {
440 const size_t route = row * work->top_k + i;
441 if ((size_t)work->ids[route] != id) continue;
442 if (p->packed_k->matvec_pair && pending == SIZE_MAX) {
443 pending = route;
444 break;
445 }
446 const int status = pending == SIZE_MAX
447 ? project_expert(p, work->hidden + row * p->width, packed, work->slots[id], scratch,
448 work->staged_experts + route * p->width)
449 : project_expert_pair(p, work->slots[id],
450 work->packed_rows + pending / work->top_k * work->packed_row_bytes, packed,
451 scratch, scratch + work->row_scratch,
452 work->staged_experts + pending * p->width, work->staged_experts + route * p->width);
453 if (status) {
454 atomic_store_explicit(&work->failed, 1, memory_order_relaxed);
455 return;
456 }
457 pending = SIZE_MAX;
458 break;
459 }
460 }
461 if (pending != SIZE_MAX) {
462 const size_t row = pending / work->top_k;
463 if (project_expert(p, work->hidden + row * p->width,
464 work->packed_rows + row * work->packed_row_bytes, work->slots[id], scratch,
465 work->staged_experts + pending * p->width)) {
466 atomic_store_explicit(&work->failed, 1, memory_order_relaxed);
467 return;
468 }
469 }
470 }
471}
472
473static size_t collect_expert_pair_jobs(const struct routed_work *work, struct expert_pair_job *jobs)
474{
475 size_t count = 0;
476 for (size_t id = 0; id < work->experts; ++id) {
477 uint32_t pending = UINT32_MAX;
478 for (size_t route = 0; route < work->rows * work->top_k; ++route) {
479 if ((size_t)work->ids[route] != id) continue;
480 if (pending == UINT32_MAX) {
481 pending = (uint32_t)route;
482 } else {
483 jobs[count++] = (struct expert_pair_job){pending, (uint32_t)route};
484 pending = UINT32_MAX;
485 }
486 }
487 if (pending != UINT32_MAX) jobs[count++] = (struct expert_pair_job){pending, UINT32_MAX};
488 }
489 return count;
490}
491
492static void project_expert_pair_jobs(size_t begin, size_t end, void *opaque)
493{
494 struct routed_work *work = opaque;
495 const struct routed_projection *p = work->projection;
496 const int worker = ck_threadpool_thread_id(work->pool);
497 const size_t scratch_slot = worker < 0 ? 0 : (size_t)worker;
498 float *scratch = work->row_work + scratch_slot * 2 * work->row_scratch;
499 for (size_t i = begin; i < end; ++i) {
500 const uint32_t first = work->jobs[i].first, second = work->jobs[i].second;
501 const size_t row = first / work->top_k;
502 const size_t slot = work->slots[work->ids[first]];
503 const unsigned char *packed = work->packed_rows + row * work->packed_row_bytes;
504 float *out = work->staged_experts + (size_t)first * p->width;
505 const int status = second == UINT32_MAX
506 ? project_expert(p, work->hidden + row * p->width, packed, slot, scratch, out)
507 : project_expert_pair(p, slot, packed,
508 work->packed_rows + second / work->top_k * work->packed_row_bytes,
509 scratch, scratch + work->row_scratch, out, work->staged_experts + (size_t)second * p->width);
510 if (status) {
511 atomic_store_explicit(&work->failed, 1, memory_order_relaxed);
512 return;
513 }
514 }
515}
516
517static int routed_softcapped_experts(ROUTED_EXPERT_ARGS, size_t down_matrix_bytes,
518 enum expert_storage storage, enum expert_storage down_storage,
519 int quantize, int reference_moe, const struct packed_k_expert_ops *packed_k, int uncapped,
520 const float *gate_up_scales, const float *down_scales, size_t scale_elements)
521{
522 /* Bounds keep workspace arithmetic valid on supported size_t widths. */
523 if (!rows || rows > CHECKED_ROUTED_EXPERT_MAX_ROWS || !hidden_dim || hidden_dim > 8192 ||
524 !inner || inner > 8192 || !experts || experts > 1024 || !slots ||
525 slots > experts || !top_k || top_k > experts ||
526 (!uncapped && (!isfinite(gate_cap) || gate_cap <= 0.0f || !isfinite(up_cap) || up_cap <= 0.0f)))
527 return -1;
528 const size_t gate_block = packed_block_elements(storage);
529 const size_t down_block = packed_block_elements(down_storage);
530 if (hidden_dim % gate_block || inner % down_block) return -1;
531 const size_t gate_row = hidden_dim / gate_block * packed_block_bytes(storage);
532 const size_t down_row = inner / down_block * packed_block_bytes(down_storage);
533 const size_t gate_expert_bytes = inner * gate_row;
534 const size_t down_expert_bytes = hidden_dim * down_row;
535 const size_t route_count = rows * top_k;
536 const size_t output_elements = rows * hidden_dim;
537 const size_t row_outputs = route_count * hidden_dim;
538 if (packed_k && ((uintptr_t)gate % _Alignof(block_q4_K) ||
539 (uintptr_t)up % _Alignof(block_q4_K))) return -1;
540 if (packed_k && packed_k->down_q5 && down_storage == EXPERT_Q5_0 &&
541 (uintptr_t)down % _Alignof(block_q5_0)) return -1;
542 const size_t packed_row_bytes = packed_k && packed_k->hidden_q8_0
543 ? hidden_dim / QK8_0 * sizeof(block_q8_0) : hidden_dim / QK_K * sizeof(block_q8_K);
544 size_t packed_bytes = packed_k ? packed_row_bytes :
545 quantize ? gate_row + down_row : 0;
546 if (packed_k && (packed_k->down_q5 || packed_k->down_mixed))
547 packed_bytes += inner / QK8_0 * sizeof(block_q8_0);
548 const size_t packed_elements = (packed_bytes + sizeof(float) - 1) / sizeof(float);
549 const size_t row_scratch = 3 * inner + packed_elements;
550 const size_t staged_elements = row_outputs + output_elements;
551 const size_t scratch_rows = work_elements >= staged_elements + rows * row_scratch ? rows : 1;
552 const size_t route_scratch = rows == 1 && top_k > 1 &&
553 work_elements >= staged_elements + top_k * row_scratch ? top_k : 0;
554 const int grouped = packed_k && packed_k->group_rows && rows > 1;
555 const size_t grouped_pack_elements = grouped ? rows * packed_row_bytes / sizeof(float) : 0;
556 const int dispatch_pairs = grouped && packed_k->dispatch_pairs;
557 const size_t jobs_elements = dispatch_pairs ? 2 * route_count : 0;
558 const size_t scratch_count = dispatch_pairs ? 2 * CK_THREADPOOL_MAX_THREADS :
559 grouped ? experts * (packed_k->matvec_pair ? 2 : 1) : route_scratch ? route_scratch : scratch_rows;
560 const size_t scratch_elements = staged_elements + grouped_pack_elements + jobs_elements + scratch_count * row_scratch;
561 if (hidden_elements < output_elements || id_elements < route_count || route_elements < route_count ||
562 expert_slot_elements < experts || expert_output_elements < row_outputs ||
563 output_capacity < output_elements || work_elements < scratch_elements ||
564 scratch_elements > SIZE_MAX / sizeof(float)) return -1;
565 if (gate_expert_bytes > SIZE_MAX / slots || down_expert_bytes > SIZE_MAX / slots ||
566 matrix_bytes != gate_expert_bytes * slots ||
567 down_matrix_bytes != down_expert_bytes * slots) return -1;
568 const void *buffers[] = {hidden, ids, route_weights, expert_slots, gate, up, down,
569 expert_output, output, work};
570 const size_t sizes[] = {output_elements * sizeof(float), route_count * sizeof(int32_t),
571 route_count * sizeof(float), experts * sizeof(int32_t),
572 matrix_bytes, matrix_bytes, down_matrix_bytes, row_outputs * sizeof(float),
573 output_elements * sizeof(float),
574 scratch_elements * sizeof(float)};
575 for (size_t i = 0; i < sizeof(buffers) / sizeof(buffers[0]); ++i) {
576 if (!region(buffers[i], sizes[i])) return -1;
577 if (i < 4 || i >= 7) {
578 if ((uintptr_t)buffers[i] % _Alignof(float)) return -1;
579 }
580 if (i < 7) continue;
581 for (size_t j = 0; j < i; ++j)
582 if (contains_overlap(buffers[i], sizes[i], buffers[j], sizes[j])) return -1;
583 }
584 if (packed_k && packed_k->require_scales) {
585 if (scale_elements != slots || !ck_checked_region(gate_up_scales, slots) ||
586 !ck_checked_region(down_scales, slots)) return -1;
587 for (size_t i = 7; i < sizeof(buffers) / sizeof(buffers[0]); ++i)
588 if (contains_overlap(buffers[i], sizes[i], gate_up_scales, slots * sizeof(float)) ||
589 contains_overlap(buffers[i], sizes[i], down_scales, slots * sizeof(float))) return -1;
590 if (!ck_checked_finite(gate_up_scales, slots) || !ck_checked_finite(down_scales, slots)) return -2;
591 }
592 if (!ck_checked_finite(hidden, output_elements) ||
593 !ck_checked_finite(route_weights, route_count)) return -2;
594
595 unsigned char assigned[1024] = {0};
596 for (size_t id = 0; id < experts; ++id) {
597 const int32_t slot = expert_slots[id];
598 if (slot == -1) continue;
599 if (slot < 0 || (size_t)slot >= slots || assigned[slot]) return -2;
600 assigned[slot] = 1;
601 }
602 for (size_t row = 0; row < rows; ++row) {
603 for (size_t k = 0; k < top_k; ++k) {
604 const size_t route = row * top_k + k;
605 const int32_t id = ids[route];
606 if (id < 0 || (size_t)id >= experts || expert_slots[id] < 0 ||
607 (size_t)expert_slots[id] >= slots || route_weights[route] < 0.0f) return -2;
608 for (size_t prev = 0; prev < k; ++prev)
609 if (id == ids[row * top_k + prev]) return -2;
610 }
611 }
612
613 const float gate_reciprocal = reference_moe ? 1.0f / gate_cap : 0.0f;
614 const float up_reciprocal = reference_moe ? 1.0f / up_cap : 0.0f;
615 if (!isfinite(gate_reciprocal) || !isfinite(up_reciprocal)) return -3;
616
617 float *staged_experts = work;
618 float *staged_output = staged_experts + row_outputs;
619 float *packed_rows = staged_output + output_elements;
620 float *jobs_buffer = packed_rows + grouped_pack_elements;
621 float *row_work = jobs_buffer + jobs_elements;
622 memset(staged_output, 0, output_elements * sizeof(float));
623 const struct routed_projection projection = {
624 .gate = gate, .up = up, .down = down, .width = hidden_dim, .inner = inner,
625 .gate_up_scales = gate_up_scales, .down_scales = down_scales,
626 .gate_row = gate_row, .down_row = down_row,
627 .packed_hidden_bytes = packed_row_bytes,
628 .gate_expert_bytes = gate_expert_bytes, .down_expert_bytes = down_expert_bytes,
629 .gate_cap = gate_cap, .up_cap = up_cap, .gate_reciprocal = gate_reciprocal, .up_reciprocal = up_reciprocal,
630 .storage = storage, .down_storage = down_storage, .quantize = quantize, .reference = reference_moe, .uncapped = uncapped, .packed_k = packed_k,
631 };
632 struct routed_work task = {
633 .projection = &projection, .hidden = hidden, .route_weights = route_weights,
634 .ids = ids, .slots = expert_slots, .row_work = row_work,
635 .staged_experts = staged_experts, .staged_output = staged_output,
636 .packed_rows = (unsigned char *)packed_rows, .packed_row_bytes = packed_row_bytes,
637 .row_scratch = row_scratch, .scratch_rows = scratch_rows, .rows = rows, .experts = experts, .top_k = top_k,
638 };
639 atomic_init(&task.failed, 0);
640 if (grouped) {
642 if (atomic_load_explicit(&task.failed, memory_order_relaxed)) return -3;
643 if (dispatch_pairs) {
644 struct expert_pair_job *jobs = (struct expert_pair_job *)jobs_buffer;
645 const size_t count = collect_expert_pair_jobs(&task, jobs);
646 task.jobs = jobs;
647 task.pool = ck_threadpool_global();
649 } else {
651 }
652 if (atomic_load_explicit(&task.failed, memory_order_relaxed)) return -3;
654 if (atomic_load_explicit(&task.failed, memory_order_relaxed)) return -3;
655 } else if (route_scratch) {
656 unsigned char *packed_hidden = hidden_workspace(&projection, row_work);
657 if (pack_hidden(&projection, hidden, packed_hidden)) return -3;
658 /* Each task writes its declared slot. Mixing preserves the FP32 order. */
659 ck_checked_parallel_for(top_k, 1, project_routes, &task);
660 if (atomic_load_explicit(&task.failed, memory_order_relaxed)) return -3;
661 size_t ordered_slots[1024];
662 if (!reference_moe) order_slots(ids, experts, top_k, ordered_slots);
663 for (size_t index = 0; index < top_k; ++index) {
664 const size_t route = reference_moe ? index : ordered_slots[index];
665 if (combine_expert(staged_experts + route * hidden_dim, staged_output, hidden_dim,
666 route_weights[route], index, reference_moe)) return -3;
667 }
668 } else {
669 ck_checked_parallel_for(rows, scratch_rows > 1 ? 1 : (int)rows, project_rows, &task);
670 if (atomic_load_explicit(&task.failed, memory_order_relaxed)) return -3;
671 }
672 memcpy(expert_output, staged_experts, row_outputs * sizeof(float));
673 memcpy(output, staged_output, output_elements * sizeof(float));
674 return 0;
675}
676
677#define ROUTED_EXPERT_FORWARD(storage, down_storage, down_bytes, quantize, reference_moe, packed_k) \
678 routed_softcapped_experts(hidden, ids, route_weights, expert_slots, gate, up, down, \
679 expert_output, output, work, rows, hidden_dim, inner, experts, \
680 slots, top_k, matrix_bytes, hidden_elements, id_elements, route_elements, \
681 expert_slot_elements, expert_output_elements, output_capacity, \
682 work_elements, gate_cap, up_cap, down_bytes, storage, down_storage, quantize, reference_moe, packed_k, 0, NULL, NULL, 0)
683
688
690{
691 return routed_softcapped_experts(hidden, ids, route_weights, expert_slots, gate, up, down,
692 expert_output, output, work, rows, hidden_dim, inner, experts, slots, top_k, matrix_bytes,
693 hidden_elements, id_elements, route_elements, expert_slot_elements, expert_output_elements,
694 output_capacity, work_elements, 0.0f, 0.0f, matrix_bytes, EXPERT_F32, EXPERT_F32, 0, 0, NULL, 1, NULL, NULL, 0);
695}
696
698{
699 return routed_softcapped_experts(hidden, ids, route_weights, expert_slots, gate, up, down,
700 expert_output, output, work, rows, hidden_dim, inner, experts, slots, top_k, matrix_bytes,
701 hidden_elements, id_elements, route_elements, expert_slot_elements, expert_output_elements,
702 output_capacity, work_elements, 0.0f, 0.0f, down_matrix_bytes,
703 EXPERT_Q3_K, EXPERT_Q5_1, 0, 0, NULL, 1, NULL, NULL, 0);
704}
705
710
715
716
721
726
727
729{
730 return ROUTED_EXPERT_FORWARD(EXPERT_Q4_K, EXPERT_Q5_0, down_matrix_bytes, 0, 0, NULL);
731}
732
734{
735 return ROUTED_EXPERT_FORWARD(EXPERT_Q4_K, EXPERT_Q8_0, down_matrix_bytes, 0, 0, NULL);
736}
737
739{
740 return ROUTED_EXPERT_FORWARD(EXPERT_Q4_K, EXPERT_Q5_0, down_matrix_bytes, 0, 0, &q4_hidden);
741}
742
744{
745 return ROUTED_EXPERT_FORWARD(EXPERT_Q4_K, EXPERT_Q8_0, down_matrix_bytes, 0, 0, &q4_hidden);
746}
747
752
757
759{
760 return ROUTED_EXPERT_FORWARD(EXPERT_Q4_K, EXPERT_Q5_0, down_matrix_bytes, 0, 0, &q4_grouped);
761}
762
764{
765 return ROUTED_EXPERT_FORWARD(EXPERT_Q4_K, EXPERT_Q8_0, down_matrix_bytes, 0, 0, &q4_grouped);
766}
767
772
777
779{
780 return ROUTED_EXPERT_FORWARD(EXPERT_Q4_K, EXPERT_Q5_0, down_matrix_bytes, 0, 0, &q4_pair_jobs);
781}
782
784{
785 return ROUTED_EXPERT_FORWARD(EXPERT_Q4_K, EXPERT_Q8_0, down_matrix_bytes, 0, 0, &q4_pair_jobs);
786}
787
788static void iq2_xs_expert_matvec(float *out, const void *weight, const void *input, int rows, int width)
789{
790 const ck_block_iq2_xs *w = weight;
791 const size_t blocks = (size_t)width / QK_K;
792 for (int row = 0; row < rows; ++row)
793 out[row] = ck_dot_iq2_xs_q8_k(blocks, w + (size_t)row * blocks, input);
794}
795
796static void nvfp4_expert_matvec(float *out, const void *weight, const void *input, int rows, int width)
797{
798 gemv_nvfp4_q8_0(out, weight, NULL, input, rows, width);
799}
800
801static void iq2_s_expert_matvec(float *out, const void *weight, const void *input, int rows, int width)
802{
803 const ck_block_iq2_s *w = weight;
804 const size_t blocks = (size_t)width / QK_K;
805 for (int row = 0; row < rows; ++row)
806 out[row] = ck_dot_iq2_s_q8_k(blocks, w + (size_t)row * blocks, input);
807}
808
809static void iq3_xxs_expert_matvec(float *out, const void *weight, const void *input, int rows, int width)
810{
811 const ck_block_iq3_xxs *w = weight;
812 const size_t blocks = (size_t)width / QK_K;
813 for (int row = 0; row < rows; ++row)
814 out[row] = ck_dot_iq3_xxs_q8_k(blocks, w + (size_t)row * blocks, input);
815}
816
817static void iq3_s_expert_matvec(float *out, const void *weight, const void *input, int rows, int width)
818{
819 const ck_block_iq3_s *w = weight;
820 const size_t blocks = (size_t)width / QK_K;
821 for (int row = 0; row < rows; ++row)
822 out[row] = ck_dot_iq3_s_q8_k(blocks, w + (size_t)row * blocks, input);
823}
824
825static void q3_k_expert_matvec(float *out, const void *weight, const void *input, int rows, int width)
826{
827 const ck_block_q3_k *w = weight;
828 const size_t blocks = (size_t)width / QK_K;
829 for (int row = 0; row < rows; ++row)
830 out[row] = ck_dot_q3_k_q8_k(blocks, w + (size_t)row * blocks, input);
831}
832
833static void iq4_nl_expert_matvec(float *out, const void *weight, const void *input, int rows, int width)
834{
835 const unsigned char *w = weight;
836 const block_q8_0 *x = input;
837 for (int r = 0; r < rows; ++r) {
838 float sum = 0.0f;
839 for (int b = 0; b < width / QK8_0; ++b, w += IQ4_NL_BLOCK_BYTES) {
840 const float d = ck_fp16_to_fp32_soft((uint16_t)(w[0] | (uint16_t)w[1] << 8)) *
841 CK_FP16_TO_FP32(x[b].d);
842 int32_t dot = 0;
843 for (unsigned j = 0; j < 16; ++j) {
844 dot += x[b].qs[j] * iq4_nl_values[w[2 + j] & 15];
845 dot += x[b].qs[j + 16] * iq4_nl_values[w[2 + j] >> 4];
846 }
847 const volatile float contribution = d * (float)dot;
848 sum += contribution;
849 }
850 out[r] = sum;
851 }
852}
853
854#define SCALED_NVFP4_ROUTED_PROVIDER(name, gate_storage, dot, packer, q8_0) \
855 int name(ROUTED_EXPERT_ARGS, size_t down_matrix_bytes, const float *gate_up_scales, \
856 const float *down_scales, size_t scale_elements) \
857 { \
858 static const struct packed_k_expert_ops kernels = { \
859 .pack = packer, .matvec = dot, .down_mixed = nvfp4_expert_matvec, \
860 .hidden_q8_0 = q8_0, .require_scales = 1, \
861 }; \
862 return routed_softcapped_experts(hidden, ids, route_weights, expert_slots, gate, up, down, \
863 expert_output, output, work, rows, hidden_dim, inner, experts, slots, top_k, matrix_bytes, \
864 hidden_elements, id_elements, route_elements, expert_slot_elements, expert_output_elements, \
865 output_capacity, work_elements, gate_cap, up_cap, down_matrix_bytes, gate_storage, EXPERT_NVFP4, \
866 0, 1, &kernels, 0, gate_up_scales, down_scales, scale_elements); \
867 }
868
879
880#define IQ4_NL_ROUTED_PROVIDER(name, gate_storage, dot) \
881 int name(ROUTED_EXPERT_ARGS, size_t down_matrix_bytes) \
882 { \
883 static const struct packed_k_expert_ops kernels = { \
884 .pack = ck_pack_q4_input_q8_k, .matvec = dot, .down_mixed = iq4_nl_expert_matvec, \
885 }; \
886 return routed_softcapped_experts(hidden, ids, route_weights, expert_slots, gate, up, down, \
887 expert_output, output, work, rows, hidden_dim, inner, experts, slots, top_k, matrix_bytes, \
888 hidden_elements, id_elements, route_elements, expert_slot_elements, expert_output_elements, \
889 output_capacity, work_elements, gate_cap, up_cap, down_matrix_bytes, gate_storage, EXPERT_IQ4_NL, \
890 0, 1, &kernels, 0, NULL, NULL, 0); \
891 }
892
static double dot(const float *a, const float *b, size_t n)
static int slot(const ck_diffusion_session *s, span *out, size_t offset, size_t count, size_t width)
static int ck_checked_finite(const float *ptr, size_t n)
static int ck_checked_region(const float *ptr, size_t n)
static void ck_checked_parallel_for(size_t count, int grain, ck_checked_range_fn fn, void *args)
void gemv_q4_k_q8_k_pair_checked_cpu(float *, float *, const void *, const void *, const void *, int, int)
void gemv_q4_k_q8_k_avx2(float *, const void *, const void *, int, int)
static int ck_pack_q4_input_q8_k(const float *input, unsigned char *packed, size_t width)
#define CK_THREADPOOL_MAX_THREADS
ck_threadpool_t * ck_threadpool_global(void)
int ck_threadpool_thread_id(const ck_threadpool_t *pool)
#define QK5_0
void gemv_nvfp4_q8_0(float *output, const void *weights, const float *weight_scales, const void *activations, int rows, int cols)
static float ck_fp16_to_fp32_soft(ck_half h)
Convert FP16 (ck_half) to FP32 — software implementation.
#define QK_NVFP4
#define CK_FP16_TO_FP32(x)
#define QK5_1
#define QK8_0
#define QK_K
int softcapped_swiglu_rows_checked_f32(const float *gate, size_t gate_elements, const float *up, size_t up_elements, float *output, size_t output_elements, size_t elements, float gate_cap, float up_cap)
int silu_mul_rows_checked_f32(const float *gate, size_t gate_elements, const float *up, size_t up_elements, float *output, size_t output_elements, size_t elements)
#define CHECKED_ROUTED_EXPERT_MAX_ROWS
static int buffers(const void **inputs, const size_t *sizes, size_t count, float *out, size_t on, float *scratch, size_t sn, size_t needed)
void gemv_q5_0_q8_0_pair_checked_cpu(float *out0, float *out1, const void *weights, const void *x0, const void *x1, int rows, int width)
void gemv_q5_0_q8_0_checked_cpu(float *out, const void *weights, const void *input, int rows, int width)
static float ck_dot_iq2_s_q8_k(size_t blocks, const ck_block_iq2_s *weights, const block_q8_K *input)
static float ck_dot_iq2_xs_q8_k(size_t blocks, const ck_block_iq2_xs *weights, const block_q8_K *input)
static float ck_dot_iq3_s_q8_k(size_t blocks, const ck_block_iq3_s *weights, const block_q8_K *input)
static float ck_dot_iq3_xxs_q8_k(size_t blocks, const ck_block_iq3_xxs *weights, const block_q8_K *input)
static float ck_packed_q4_k_dot_ordered(const float *input, const unsigned char *weight, size_t width)
static float ck_packed_q5_1_dot_ordered(const float *input, const unsigned char *weight, size_t width)
static float ck_packed_q5_0_dot_ordered(const float *input, const unsigned char *weight, size_t width)
static float ck_packed_q3_k_dot_ordered(const float *input, const unsigned char *weight, size_t width)
static float ck_dot_q3_k_q8_k(size_t blocks, const ck_block_q3_k *weights, const block_q8_K *input)
static float ck_q8_activation_dot(const unsigned char *weight, const unsigned char *input, size_t width)
static void ck_q8_activation_dot_pair(const unsigned char *gate, const unsigned char *up, const unsigned char *input, size_t width, float *g, float *u)
static int ck_pack_q8_activation_rne(const float *input, unsigned char *packed, size_t width)
static float ck_packed_q8_dot_ordered(const float *input, const unsigned char *weight, size_t width)
int routed_softcapped_experts_checked_q4_k_q8_0(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements, float gate_cap, float up_cap, size_t down_matrix_bytes)
static int region(const void *p, size_t bytes)
int routed_softcapped_experts_checked_q8_0_q8_activation(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements, float gate_cap, float up_cap)
static void iq2_s_expert_matvec(float *out, const void *weight, const void *input, int rows, int width)
static float expert_dot(const float *input, const unsigned char *weight, size_t width, enum expert_storage storage, const unsigned char *activation)
static void pack_grouped_rows(size_t begin, size_t end, void *data)
static void q3_k_expert_matvec(float *out, const void *weight, const void *input, int rows, int width)
void gemv_q5_0_q8_0_pair_checked_cpu(float *, float *, const void *, const void *, const void *, int, int)
int routed_softcapped_experts_checked_q4_k_q8_0_grouped_pair(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements, float gate_cap, float up_cap, size_t down_matrix_bytes)
int routed_softcapped_experts_checked_iq3_xxs_iq4_nl(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements, float gate_cap, float up_cap, size_t down_matrix_bytes)
int routed_softcapped_experts_checked_q4_k_q8_0_q8_k_hidden(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements, float gate_cap, float up_cap, size_t down_matrix_bytes)
static int pack_hidden(const struct routed_projection *p, const float *input, unsigned char *packed)
int routed_softcapped_experts_checked_q4_k_q5_0_grouped(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements, float gate_cap, float up_cap, size_t down_matrix_bytes)
static int routed_softcapped_experts(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements, float gate_cap, float up_cap, size_t down_matrix_bytes, enum expert_storage storage, enum expert_storage down_storage, int quantize, int reference_moe, const struct packed_k_expert_ops *packed_k, int uncapped, const float *gate_up_scales, const float *down_scales, size_t scale_elements)
int routed_softcapped_experts_checked_iq3_xxs_nvfp4_scaled(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements, float gate_cap, float up_cap, size_t down_matrix_bytes, const float *gate_up_scales, const float *down_scales, size_t scale_elements)
int routed_softcapped_experts_checked_iq4_nl(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements, float gate_cap, float up_cap)
static const struct packed_k_expert_ops q4_grouped
static const int8_t iq4_nl_values[16]
int routed_softcapped_experts_checked_f32(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements, float gate_cap, float up_cap)
static void iq3_xxs_expert_matvec(float *out, const void *weight, const void *input, int rows, int width)
#define ROUTED_EXPERT_FORWARD(storage, down_storage, down_bytes, quantize, reference_moe, packed_k)
static void iq2_xs_expert_matvec(float *out, const void *weight, const void *input, int rows, int width)
static const struct packed_k_expert_ops q4_pair_jobs
int routed_softcapped_experts_checked_q3_k_nvfp4_scaled(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements, float gate_cap, float up_cap, size_t down_matrix_bytes, const float *gate_up_scales, const float *down_scales, size_t scale_elements)
static int prepare_expert_activation(const struct routed_projection *p, float *scratch, unsigned char *packed_activation)
int routed_softcapped_experts_checked_q8_0(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements, float gate_cap, float up_cap)
static int contains_overlap(const void *p, size_t pn, const void *q, size_t qn)
int routed_softcapped_experts_checked_q4_k_q5_0(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements, float gate_cap, float up_cap, size_t down_matrix_bytes)
int routed_softcapped_experts_checked_q4_k_q8_0_grouped(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements, float gate_cap, float up_cap, size_t down_matrix_bytes)
static void iq4_nl_expert_matvec(float *out, const void *weight, const void *input, int rows, int width)
static size_t packed_block_elements(enum expert_storage storage)
static size_t packed_block_bytes(enum expert_storage storage)
static unsigned char * hidden_workspace(const struct routed_projection *p, float *scratch)
static void order_slots(const int32_t *ids, size_t experts, size_t top_k, size_t *ordered_slots)
static int finish_expert(const struct routed_projection *p, size_t slot, float *scratch, float *output)
static unsigned char * expert_packed_activation(const struct routed_projection *p, float *scratch)
int routed_softcapped_experts_checked_q4_k_q8_0_q8_k_hidden_q8_0_down(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements, float gate_cap, float up_cap, size_t down_matrix_bytes)
void gemv_q5_0_q8_0_checked_cpu(float *, const void *, const void *, int, int)
#define SCALED_NVFP4_ROUTED_PROVIDER(name, gate_storage, dot, packer, q8_0)
int routed_softcapped_experts_checked_q4_k_q5_0_pair_jobs(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements, float gate_cap, float up_cap, size_t down_matrix_bytes)
int routed_softcapped_experts_checked_iq2_s_nvfp4_scaled(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements, float gate_cap, float up_cap, size_t down_matrix_bytes, const float *gate_up_scales, const float *down_scales, size_t scale_elements)
static const struct packed_k_expert_ops q4_hidden
static const struct packed_k_expert_ops q4_grouped_pairs
static void nvfp4_expert_matvec(float *out, const void *weight, const void *input, int rows, int width)
static const struct packed_k_expert_ops q4_hidden_and_down
static void project_rows(size_t begin, size_t end, void *data)
int routed_softcapped_experts_checked_q4_k_q5_0_q8_k_hidden(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements, float gate_cap, float up_cap, size_t down_matrix_bytes)
static void project_grouped_experts(size_t begin, size_t end, void *opaque)
int routed_softcapped_experts_checked_q4_k_q5_0_q8_k_hidden_q8_0_down(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements, float gate_cap, float up_cap, size_t down_matrix_bytes)
static int project_expert(const struct routed_projection *p, const float *input, const unsigned char *packed_hidden, size_t slot, float *scratch, float *output)
int routed_swiglu_experts_checked_f32(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements)
static void iq3_s_expert_matvec(float *out, const void *weight, const void *input, int rows, int width)
static int project_expert_pair(const struct routed_projection *p, size_t slot, const unsigned char *x0, const unsigned char *x1, float *work0, float *work1, float *out0, float *out1)
int routed_softcapped_experts_checked_iq3_s_iq4_nl(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements, float gate_cap, float up_cap, size_t down_matrix_bytes)
static void project_routes(size_t begin, size_t end, void *data)
int routed_softcapped_experts_checked_nvfp4_nvfp4_scaled(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements, float gate_cap, float up_cap, size_t down_matrix_bytes, const float *gate_up_scales, const float *down_scales, size_t scale_elements)
#define IQ4_NL_ROUTED_PROVIDER(name, gate_storage, dot)
int routed_swiglu_experts_checked_q3_k_q5_1(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements, size_t down_matrix_bytes)
static void mix_grouped_rows(size_t begin, size_t end, void *data)
int routed_softcapped_experts_checked_q4_k_q8_0_pair_jobs(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements, float gate_cap, float up_cap, size_t down_matrix_bytes)
int routed_softcapped_experts_checked_iq2_xs_nvfp4_scaled(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements, float gate_cap, float up_cap, size_t down_matrix_bytes, const float *gate_up_scales, const float *down_scales, size_t scale_elements)
static int combine_expert(const float *expert, float *mixture, size_t width, float weight, size_t index, int reference)
static size_t collect_expert_pair_jobs(const struct routed_work *work, struct expert_pair_job *jobs)
static void project_expert_pair_jobs(size_t begin, size_t end, void *opaque)
int routed_softcapped_experts_checked_q4_k_q5_0_grouped_pair(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements, float gate_cap, float up_cap, size_t down_matrix_bytes)
int routed_softcapped_experts_checked_q8_0_q8_activation_scaled_situ(const float *hidden, const int32_t *ids, const float *route_weights, const int32_t *expert_slots, const void *gate, const void *up, const void *down, float *expert_output, float *output, float *work, size_t rows, size_t hidden_dim, size_t inner, size_t experts, size_t slots, size_t top_k, size_t matrix_bytes, size_t hidden_elements, size_t id_elements, size_t route_elements, size_t expert_slot_elements, size_t expert_output_elements, size_t output_capacity, size_t work_elements, float gate_cap, float up_cap)
int8_t qs[32]
const int32_t * ids
Definition tokenizer.h:447
int32_t id
Definition tokenizer.h:317
const uint8_t size_t count
Definition true_bpe.h:211
uint32_t end
Definition utf8.c:215