32#define IQ4_NL_BLOCK_BYTES 18
36 -127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113
53 default:
return sizeof(float);
70 default:
return QK8_0;
76static float expert_dot(
const float *input,
const unsigned char *weight,
77 size_t width,
enum expert_storage storage,
const unsigned char *activation)
87 for (
size_t i = 0; i < width; ++i) {
89 memcpy(&value, weight + i *
sizeof(
float),
sizeof(
float));
90 sum += (double)input[i] * (
double)value;
94 for (
size_t i = 0; i < width; i +=
QK8_0, weight += block_bytes) {
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;
109 return (uintptr_t)p < (uintptr_t)q + qn && (uintptr_t)q < (uintptr_t)p + pn;
112static int region(
const void *p,
size_t bytes)
114 return p && bytes && (uintptr_t)p <= UINTPTR_MAX - bytes;
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, \
126#define ROUTED_EXPERT_ARGS EXPERT_ARGS, float gate_cap, float up_cap
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);
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;
187 int quantize, reference, uncapped;
188 const struct packed_k_expert_ops *packed_k;
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;
200 unsigned char *packed_activation)
202 float *gate_values = scratch, *up_values = gate_values + p->inner, *activation = up_values + p->inner;
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;
213 }
else if (p->uncapped) {
215 activation, p->inner, p->inner))
return -3;
217 activation, p->inner, p->inner, p->gate_cap, p->up_cap))
return -3;
221static int finish_expert(
const struct routed_projection *p,
size_t slot,
float *scratch,
float *output)
223 const unsigned char *down = p->down +
slot * p->down_expert_bytes;
224 const float *activation = scratch + 2 * p->inner;
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);
230 for (
size_t i = 0; i < p->width; ++i) output[i] *= p->down_scales[
slot];
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);
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;
245 const unsigned char *packed_hidden,
size_t slot,
float *scratch,
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;
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);
255 for (
size_t i = 0; i < p->inner; ++i) {
258 p->width, gate_values + i, up_values + i);
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);
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];
275 const unsigned char *x0,
const unsigned char *x1,
276 float *work0,
float *work1,
float *out0,
float *out1)
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) {
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);
295static inline int combine_expert(
const float *expert,
float *mixture,
size_t width,
296 float weight,
size_t index,
int reference)
298 for (
size_t i = 0; i < width; ++i) {
300 const volatile float weighted = expert[i] * weight;
301 mixture[i] = index == 0 ? weighted : mixture[i] + weighted;
303 mixture[i] += expert[i] * weight;
305 if (!isfinite(mixture[i]))
return -3;
310static void order_slots(
const int32_t *
ids,
size_t experts,
size_t top_k,
size_t *ordered_slots)
313 for (
size_t id = 0;
id < experts; ++
id)
315 if ((
size_t)
ids[
slot] ==
id) ordered_slots[next++] =
slot;
318struct expert_pair_job {
319 uint32_t first, second;
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");
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;
341 return p->quantize || p->packed_k ? (
unsigned char *)(scratch + 3 * p->inner) : NULL;
344static int pack_hidden(
const struct routed_projection *p,
const float *input,
unsigned char *packed)
346 if (!packed)
return 0;
347 if (p->packed_k)
return p->packed_k->pack(input, packed, p->width);
353 struct routed_work *work = data;
354 const struct routed_projection *p = work->projection;
355 for (
size_t row = begin; row <
end; ++row) {
357 work->packed_rows + row * work->packed_row_bytes)) {
358 atomic_store_explicit(&work->failed, 1, memory_order_relaxed);
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];
375 work->staged_output + row * width, width,
376 work->route_weights[route], index, 0)) {
377 atomic_store_explicit(&work->failed, 1, memory_order_relaxed);
386 struct routed_work *work = data;
387 const struct routed_projection *p = work->projection;
388 for (
size_t route = begin; route <
end; ++route) {
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);
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);
406 const float *input = work->hidden + row * p->width;
408 atomic_store_explicit(&work->failed, 1, memory_order_relaxed);
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);
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) {
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)
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);
454 atomic_store_explicit(&work->failed, 1, memory_order_relaxed);
461 if (pending != SIZE_MAX) {
462 const size_t row = pending / work->top_k;
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);
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;
483 jobs[
count++] = (
struct expert_pair_job){pending, (uint32_t)route};
484 pending = UINT32_MAX;
487 if (pending != UINT32_MAX) jobs[
count++] = (
struct expert_pair_job){pending, UINT32_MAX};
494 struct routed_work *work = opaque;
495 const struct routed_projection *p = work->projection;
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
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);
511 atomic_store_explicit(&work->failed, 1, memory_order_relaxed);
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)
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)))
530 if (hidden_dim % gate_block || inner % down_block)
return -1;
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
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))
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;
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) {
577 if (i < 4 || i >= 7) {
578 if ((uintptr_t)
buffers[i] % _Alignof(
float))
return -1;
581 for (
size_t j = 0; j < i; ++j)
584 if (packed_k && packed_k->require_scales) {
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;
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;
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;
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,
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,
639 atomic_init(&task.failed, 0);
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;
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) {
657 if (
pack_hidden(&projection, hidden, packed_hidden))
return -3;
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;
670 if (atomic_load_explicit(&task.failed, memory_order_relaxed))
return -3;
672 memcpy(expert_output, staged_experts, row_outputs *
sizeof(
float));
673 memcpy(output, staged_output, output_elements *
sizeof(
float));
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)
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);
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,
791 const size_t blocks = (size_t)width /
QK_K;
792 for (
int row = 0; row < rows; ++row)
804 const size_t blocks = (size_t)width /
QK_K;
805 for (
int row = 0; row < rows; ++row)
812 const size_t blocks = (size_t)width /
QK_K;
813 for (
int row = 0; row < rows; ++row)
820 const size_t blocks = (size_t)width /
QK_K;
821 for (
int row = 0; row < rows; ++row)
825static void q3_k_expert_matvec(
float *out,
const void *weight,
const void *input,
int rows,
int width)
828 const size_t blocks = (size_t)width /
QK_K;
829 for (
int row = 0; row < rows; ++row)
835 const unsigned char *w = weight;
837 for (
int r = 0; r < rows; ++r) {
843 for (
unsigned j = 0; j < 16; ++j) {
847 const volatile float contribution = d * (float)
dot;
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) \
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, \
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); \
880#define IQ4_NL_ROUTED_PROVIDER(name, gate_storage, dot) \
881 int name(ROUTED_EXPERT_ARGS, size_t down_matrix_bytes) \
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, \
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); \
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)
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 CK_FP16_TO_FP32(x)
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)
#define ROUTED_EXPERT_ARGS
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)
#define IQ4_NL_BLOCK_BYTES
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)
const uint8_t size_t count