← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
routed_softcapped_experts_checked.c File Reference
#include "checked_float_buffer.h"
#include "checked_parallel.h"
#include "checked_q4_k_activation.h"
#include "packed_q8_dot_ordered.h"
#include "packed_k_dot_ordered.h"
#include "packed_iq2_xs_dot.h"
#include "packed_iq2_s_dot.h"
#include "packed_iq3_xxs_dot.h"
#include "packed_iq3_s_dot.h"
#include "packed_q3_k_dot.h"
#include "packed_q8_activation_checked.h"
#include "ckernel_transformer_rows_checked.h"
#include <string.h>
#include <stdatomic.h>

Go to the source code of this file.

Macros

#define EXPERT_ARGS
#define IQ4_NL_BLOCK_BYTES   18
#define IQ4_NL_ROUTED_PROVIDER(name, gate_storage, dot)
#define ROUTED_EXPERT_ARGS   EXPERT_ARGS, float gate_cap, float up_cap
#define ROUTED_EXPERT_FORWARD(storage, down_storage, down_bytes, quantize, reference_moe, packed_k)
#define SCALED_NVFP4_ROUTED_PROVIDER(name, gate_storage, dot, packer, q8_0)

Enumerations

enum  expert_storage {
  EXPERT_F32 , EXPERT_Q8_0 , EXPERT_IQ4_NL , EXPERT_Q4_K ,
  EXPERT_Q5_0 , EXPERT_IQ2_XS , EXPERT_IQ2_S , EXPERT_IQ3_XXS ,
  EXPERT_IQ3_S , EXPERT_Q3_K , EXPERT_NVFP4 , EXPERT_Q5_1
}

Functions

static size_t collect_expert_pair_jobs (const struct routed_work *work, struct expert_pair_job *jobs)
static int combine_expert (const float *expert, float *mixture, size_t width, float weight, size_t index, int reference)
static int contains_overlap (const void *p, size_t pn, const void *q, size_t qn)
static float expert_dot (const float *input, const unsigned char *weight, size_t width, enum expert_storage storage, const unsigned char *activation)
static unsigned char * expert_packed_activation (const struct routed_projection *p, float *scratch)
static int finish_expert (const struct routed_projection *p, size_t slot, float *scratch, float *output)
void gemv_q5_0_q8_0_checked_cpu (float *, const void *, const void *, int, int)
void gemv_q5_0_q8_0_pair_checked_cpu (float *, float *, const void *, const void *, const void *, int, int)
static unsigned char * hidden_workspace (const struct routed_projection *p, float *scratch)
static void iq2_s_expert_matvec (float *out, const void *weight, const void *input, int rows, int width)
static void iq2_xs_expert_matvec (float *out, const void *weight, const void *input, int rows, int width)
static void iq3_s_expert_matvec (float *out, const void *weight, const void *input, int rows, int width)
static void iq3_xxs_expert_matvec (float *out, const void *weight, const void *input, int rows, int width)
static void iq4_nl_expert_matvec (float *out, const void *weight, const void *input, int rows, int width)
static void mix_grouped_rows (size_t begin, size_t end, void *data)
static void nvfp4_expert_matvec (float *out, const void *weight, const void *input, int rows, int width)
static void order_slots (const int32_t *ids, size_t experts, size_t top_k, size_t *ordered_slots)
static void pack_grouped_rows (size_t begin, size_t end, void *data)
static int pack_hidden (const struct routed_projection *p, const float *input, unsigned char *packed)
static size_t packed_block_bytes (enum expert_storage storage)
static size_t packed_block_elements (enum expert_storage storage)
static int prepare_expert_activation (const struct routed_projection *p, float *scratch, unsigned char *packed_activation)
static int project_expert (const struct routed_projection *p, const float *input, const unsigned char *packed_hidden, size_t slot, float *scratch, float *output)
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)
static void project_expert_pair_jobs (size_t begin, size_t end, void *opaque)
static void project_grouped_experts (size_t begin, size_t end, void *opaque)
static void project_routes (size_t begin, size_t end, void *data)
static void project_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)
static int region (const void *p, size_t 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_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)
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)
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)
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)
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_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)
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)
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)
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_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)
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_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_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)
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)
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)
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)
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_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_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)
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)
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)
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)
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)
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)
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)

Variables

static const int8_t iq4_nl_values [16]
static const struct packed_k_expert_ops q4_grouped
static const struct packed_k_expert_ops q4_grouped_pairs
static const struct packed_k_expert_ops q4_hidden
static const struct packed_k_expert_ops q4_hidden_and_down
static const struct packed_k_expert_ops q4_pair_jobs

Macro Definition Documentation

◆ EXPERT_ARGS

#define EXPERT_ARGS
Value:
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
const int32_t * ids
Definition tokenizer.h:447

Definition at line 117 of file routed_softcapped_experts_checked.c.

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

Referenced by routed_swiglu_experts_checked_f32(), and routed_swiglu_experts_checked_q3_k_q5_1().

◆ IQ4_NL_BLOCK_BYTES

#define IQ4_NL_BLOCK_BYTES   18

Definition at line 32 of file routed_softcapped_experts_checked.c.

Referenced by iq4_nl_expert_matvec(), and packed_block_bytes().

◆ IQ4_NL_ROUTED_PROVIDER

#define IQ4_NL_ROUTED_PROVIDER ( name,
gate_storage,
dot )
Value:
int name(ROUTED_EXPERT_ARGS, size_t down_matrix_bytes) \
{ \
static const struct packed_k_expert_ops kernels = { \
.pack = ck_pack_q4_input_q8_k, .matvec = dot, .down_mixed = iq4_nl_expert_matvec, \
}; \
return routed_softcapped_experts(hidden, ids, route_weights, expert_slots, gate, up, down, \
expert_output, output, work, rows, hidden_dim, inner, experts, slots, top_k, matrix_bytes, \
hidden_elements, id_elements, route_elements, expert_slot_elements, expert_output_elements, \
output_capacity, work_elements, gate_cap, up_cap, down_matrix_bytes, gate_storage, EXPERT_IQ4_NL, \
0, 1, &kernels, 0, NULL, NULL, 0); \
}
static double dot(const float *a, const float *b, size_t n)
static int ck_pack_q4_input_q8_k(const float *input, unsigned char *packed, size_t width)
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)
static void iq4_nl_expert_matvec(float *out, const void *weight, const void *input, int rows, int width)

Definition at line 880 of file routed_softcapped_experts_checked.c.

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 }

Referenced by routed_softcapped_experts_checked_iq3_xxs_iq4_nl().

◆ ROUTED_EXPERT_ARGS

◆ ROUTED_EXPERT_FORWARD

#define ROUTED_EXPERT_FORWARD ( storage,
down_storage,
down_bytes,
quantize,
reference_moe,
packed_k )
Value:
routed_softcapped_experts(hidden, ids, route_weights, expert_slots, gate, up, down, \
expert_output, output, work, rows, hidden_dim, inner, experts, \
slots, top_k, matrix_bytes, hidden_elements, id_elements, route_elements, \
expert_slot_elements, expert_output_elements, output_capacity, \
work_elements, gate_cap, up_cap, down_bytes, storage, down_storage, quantize, reference_moe, packed_k, 0, NULL, NULL, 0)

Definition at line 677 of file routed_softcapped_experts_checked.c.

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)

Referenced by routed_softcapped_experts_checked_f32(), routed_softcapped_experts_checked_iq4_nl(), routed_softcapped_experts_checked_q4_k_q5_0(), routed_softcapped_experts_checked_q4_k_q5_0_grouped(), routed_softcapped_experts_checked_q4_k_q5_0_grouped_pair(), routed_softcapped_experts_checked_q4_k_q5_0_pair_jobs(), routed_softcapped_experts_checked_q4_k_q5_0_q8_k_hidden(), routed_softcapped_experts_checked_q4_k_q5_0_q8_k_hidden_q8_0_down(), routed_softcapped_experts_checked_q4_k_q8_0(), routed_softcapped_experts_checked_q4_k_q8_0_grouped(), routed_softcapped_experts_checked_q4_k_q8_0_grouped_pair(), routed_softcapped_experts_checked_q4_k_q8_0_pair_jobs(), routed_softcapped_experts_checked_q4_k_q8_0_q8_k_hidden(), routed_softcapped_experts_checked_q4_k_q8_0_q8_k_hidden_q8_0_down(), routed_softcapped_experts_checked_q8_0(), routed_softcapped_experts_checked_q8_0_q8_activation(), and routed_softcapped_experts_checked_q8_0_q8_activation_scaled_situ().

◆ SCALED_NVFP4_ROUTED_PROVIDER

#define SCALED_NVFP4_ROUTED_PROVIDER ( name,
gate_storage,
dot,
packer,
q8_0 )
Value:
int name(ROUTED_EXPERT_ARGS, 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 kernels = { \
.pack = packer, .matvec = dot, .down_mixed = nvfp4_expert_matvec, \
.hidden_q8_0 = q8_0, .require_scales = 1, \
}; \
return routed_softcapped_experts(hidden, ids, route_weights, expert_slots, gate, up, down, \
expert_output, output, work, rows, hidden_dim, inner, experts, slots, top_k, matrix_bytes, \
hidden_elements, id_elements, route_elements, expert_slot_elements, expert_output_elements, \
output_capacity, work_elements, gate_cap, up_cap, down_matrix_bytes, gate_storage, EXPERT_NVFP4, \
0, 1, &kernels, 0, gate_up_scales, down_scales, scale_elements); \
}
static void nvfp4_expert_matvec(float *out, const void *weight, const void *input, int rows, int width)

Definition at line 854 of file routed_softcapped_experts_checked.c.

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 }

Referenced by routed_softcapped_experts_checked_iq2_s_nvfp4_scaled(), routed_softcapped_experts_checked_iq2_xs_nvfp4_scaled(), routed_softcapped_experts_checked_iq3_xxs_nvfp4_scaled(), and routed_softcapped_experts_checked_nvfp4_nvfp4_scaled().

Enumeration Type Documentation

◆ expert_storage

Enumerator
EXPERT_F32 
EXPERT_Q8_0 
EXPERT_IQ4_NL 
EXPERT_Q4_K 
EXPERT_Q5_0 
EXPERT_IQ2_XS 
EXPERT_IQ2_S 
EXPERT_IQ3_XXS 
EXPERT_IQ3_S 
EXPERT_Q3_K 
EXPERT_NVFP4 
EXPERT_Q5_1 

Definition at line 17 of file routed_softcapped_experts_checked.c.

Function Documentation

◆ collect_expert_pair_jobs()

size_t collect_expert_pair_jobs ( const struct routed_work * work,
struct expert_pair_job * jobs )
static

Definition at line 473 of file routed_softcapped_experts_checked.c.

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}
int32_t id
Definition tokenizer.h:317
const uint8_t size_t count
Definition true_bpe.h:211

References count, and id.

Referenced by routed_softcapped_experts().

◆ combine_expert()

int combine_expert ( const float * expert,
float * mixture,
size_t width,
float weight,
size_t index,
int reference )
inlinestatic

Definition at line 295 of file routed_softcapped_experts_checked.c.

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}

Referenced by mix_grouped_rows(), project_rows(), and routed_softcapped_experts().

◆ contains_overlap()

int contains_overlap ( const void * p,
size_t pn,
const void * q,
size_t qn )
static

Definition at line 107 of file routed_softcapped_experts_checked.c.

108{
109 return (uintptr_t)p < (uintptr_t)q + qn && (uintptr_t)q < (uintptr_t)p + pn;
110}

Referenced by routed_softcapped_experts().

◆ expert_dot()

float expert_dot ( const float * input,
const unsigned char * weight,
size_t width,
enum expert_storage storage,
const unsigned char * activation )
static

Definition at line 76 of file routed_softcapped_experts_checked.c.

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}
static float ck_fp16_to_fp32_soft(ck_half h)
Convert FP16 (ck_half) to FP32 — software implementation.
#define QK8_0
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_q8_activation_dot(const unsigned char *weight, const unsigned char *input, size_t width)
static float ck_packed_q8_dot_ordered(const float *input, const unsigned char *weight, size_t width)
static const int8_t iq4_nl_values[16]
static size_t packed_block_bytes(enum expert_storage storage)

References ck_fp16_to_fp32_soft(), ck_packed_q3_k_dot_ordered(), ck_packed_q4_k_dot_ordered(), ck_packed_q5_0_dot_ordered(), ck_packed_q5_1_dot_ordered(), ck_packed_q8_dot_ordered(), ck_q8_activation_dot(), EXPERT_F32, EXPERT_Q3_K, EXPERT_Q4_K, EXPERT_Q5_0, EXPERT_Q5_1, EXPERT_Q8_0, iq4_nl_values, packed_block_bytes(), and QK8_0.

Referenced by finish_expert(), and project_expert().

◆ expert_packed_activation()

unsigned char * expert_packed_activation ( const struct routed_projection * p,
float * scratch )
static

Definition at line 191 of file routed_softcapped_experts_checked.c.

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}

Referenced by finish_expert(), and project_expert_pair().

◆ finish_expert()

int finish_expert ( const struct routed_projection * p,
size_t slot,
float * scratch,
float * output )
static

Definition at line 221 of file routed_softcapped_experts_checked.c.

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}
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 float expert_dot(const float *input, const unsigned char *weight, size_t width, enum expert_storage storage, const unsigned char *activation)
static int prepare_expert_activation(const struct routed_projection *p, float *scratch, unsigned char *packed_activation)
static unsigned char * expert_packed_activation(const struct routed_projection *p, float *scratch)

References ck_checked_finite(), expert_dot(), expert_packed_activation(), EXPERT_Q5_0, prepare_expert_activation(), and slot().

Referenced by project_expert(), and project_expert_pair().

◆ gemv_q5_0_q8_0_checked_cpu()

void gemv_q5_0_q8_0_checked_cpu ( float * out,
const void * weights,
const void * input,
int rows,
int width )

Definition at line 1770 of file gemm_kernels_q5_0.c.

1771{
1772 const size_t blocks = (size_t)width / QK5_0;
1773 void (*dot)(int, float *, const void *, const void *) = vec_dot_q5_0_q8_0_ref;
1774#if defined(CK_Q5_RUNTIME_AVX2)
1775 if (__builtin_cpu_supports("avx2") && __builtin_cpu_supports("fma") &&
1776 __builtin_cpu_supports("f16c")) dot = vec_dot_q5_0_q8_0_avx2;
1777#endif
1778 for (int i = 0; i < rows; ++i)
1779 dot(width, out + i, (const block_q5_0 *)weights + (size_t)i * blocks, input);
1780}
#define QK5_0
void vec_dot_q5_0_q8_0_ref(int n, float *s, const void *vx, const void *vy)
Quantized dot product: Q5_0 weights x Q8_0 input (scalar reference).

References dot(), QK5_0, and vec_dot_q5_0_q8_0_ref().

Referenced by gemv_q5_0_q8_0_pair_checked_cpu().

◆ gemv_q5_0_q8_0_pair_checked_cpu()

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 )

Definition at line 1803 of file gemm_kernels_q5_0.c.

1805{
1806#if defined(CK_Q5_RUNTIME_AVX2)
1807 if (__builtin_cpu_supports("avx2") && __builtin_cpu_supports("fma") &&
1808 __builtin_cpu_supports("f16c")) {
1809 gemv_q5_0_q8_0_pair_avx2(out0, out1, weights, x0, x1, rows, (size_t)width / QK5_0);
1810 return;
1811 }
1812#endif
1813 gemv_q5_0_q8_0_checked_cpu(out0, weights, x0, rows, width);
1814 gemv_q5_0_q8_0_checked_cpu(out1, weights, x1, rows, width);
1815}
void gemv_q5_0_q8_0_checked_cpu(float *out, const void *weights, const void *input, int rows, int width)

References gemv_q5_0_q8_0_checked_cpu(), and QK5_0.

◆ hidden_workspace()

unsigned char * hidden_workspace ( const struct routed_projection * p,
float * scratch )
static

Definition at line 339 of file routed_softcapped_experts_checked.c.

340{
341 return p->quantize || p->packed_k ? (unsigned char *)(scratch + 3 * p->inner) : NULL;
342}

Referenced by project_routes(), project_rows(), and routed_softcapped_experts().

◆ iq2_s_expert_matvec()

void iq2_s_expert_matvec ( float * out,
const void * weight,
const void * input,
int rows,
int width )
static

Definition at line 801 of file routed_softcapped_experts_checked.c.

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}
#define QK_K
static float ck_dot_iq2_s_q8_k(size_t blocks, const ck_block_iq2_s *weights, const block_q8_K *input)

References ck_dot_iq2_s_q8_k(), and QK_K.

Referenced by routed_softcapped_experts_checked_iq2_s_nvfp4_scaled().

◆ iq2_xs_expert_matvec()

void iq2_xs_expert_matvec ( float * out,
const void * weight,
const void * input,
int rows,
int width )
static

Definition at line 788 of file routed_softcapped_experts_checked.c.

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}
static float ck_dot_iq2_xs_q8_k(size_t blocks, const ck_block_iq2_xs *weights, const block_q8_K *input)

References ck_dot_iq2_xs_q8_k(), and QK_K.

Referenced by routed_softcapped_experts_checked_iq2_xs_nvfp4_scaled().

◆ iq3_s_expert_matvec()

void iq3_s_expert_matvec ( float * out,
const void * weight,
const void * input,
int rows,
int width )
static

Definition at line 817 of file routed_softcapped_experts_checked.c.

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}
static float ck_dot_iq3_s_q8_k(size_t blocks, const ck_block_iq3_s *weights, const block_q8_K *input)

References ck_dot_iq3_s_q8_k(), and QK_K.

Referenced by routed_softcapped_experts_checked_iq3_s_iq4_nl().

◆ iq3_xxs_expert_matvec()

void iq3_xxs_expert_matvec ( float * out,
const void * weight,
const void * input,
int rows,
int width )
static

Definition at line 809 of file routed_softcapped_experts_checked.c.

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}
static float ck_dot_iq3_xxs_q8_k(size_t blocks, const ck_block_iq3_xxs *weights, const block_q8_K *input)

References ck_dot_iq3_xxs_q8_k(), and QK_K.

Referenced by routed_softcapped_experts_checked_iq3_xxs_iq4_nl(), and routed_softcapped_experts_checked_iq3_xxs_nvfp4_scaled().

◆ iq4_nl_expert_matvec()

void iq4_nl_expert_matvec ( float * out,
const void * weight,
const void * input,
int rows,
int width )
static

Definition at line 833 of file routed_softcapped_experts_checked.c.

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}
#define CK_FP16_TO_FP32(x)
int8_t qs[32]

References CK_FP16_TO_FP32, ck_fp16_to_fp32_soft(), dot(), IQ4_NL_BLOCK_BYTES, iq4_nl_values, QK8_0, and block_q8_0::qs.

◆ mix_grouped_rows()

void mix_grouped_rows ( size_t begin,
size_t end,
void * data )
static

Definition at line 364 of file routed_softcapped_experts_checked.c.

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}
static void order_slots(const int32_t *ids, size_t experts, size_t top_k, size_t *ordered_slots)
static int combine_expert(const float *expert, float *mixture, size_t width, float weight, size_t index, int reference)
uint32_t end
Definition utf8.c:215

References combine_expert(), end, and order_slots().

Referenced by routed_softcapped_experts().

◆ nvfp4_expert_matvec()

void nvfp4_expert_matvec ( float * out,
const void * weight,
const void * input,
int rows,
int width )
static

Definition at line 796 of file routed_softcapped_experts_checked.c.

797{
798 gemv_nvfp4_q8_0(out, weight, NULL, input, rows, width);
799}
void gemv_nvfp4_q8_0(float *output, const void *weights, const float *weight_scales, const void *activations, int rows, int cols)

References gemv_nvfp4_q8_0().

Referenced by routed_softcapped_experts_checked_nvfp4_nvfp4_scaled().

◆ order_slots()

void order_slots ( const int32_t * ids,
size_t experts,
size_t top_k,
size_t * ordered_slots )
static

Definition at line 310 of file routed_softcapped_experts_checked.c.

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}

References id, ids, and slot().

Referenced by mix_grouped_rows(), project_rows(), and routed_softcapped_experts().

◆ pack_grouped_rows()

void pack_grouped_rows ( size_t begin,
size_t end,
void * data )
static

Definition at line 351 of file routed_softcapped_experts_checked.c.

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}
static int pack_hidden(const struct routed_projection *p, const float *input, unsigned char *packed)

References end, and pack_hidden().

Referenced by routed_softcapped_experts().

◆ pack_hidden()

int pack_hidden ( const struct routed_projection * p,
const float * input,
unsigned char * packed )
static

Definition at line 344 of file routed_softcapped_experts_checked.c.

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}
static int ck_pack_q8_activation_rne(const float *input, unsigned char *packed, size_t width)

References ck_pack_q8_activation_rne().

Referenced by pack_grouped_rows(), project_rows(), and routed_softcapped_experts().

◆ packed_block_bytes()

size_t packed_block_bytes ( enum expert_storage storage)
static

Definition at line 39 of file routed_softcapped_experts_checked.c.

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}

References EXPERT_IQ2_S, EXPERT_IQ2_XS, EXPERT_IQ3_S, EXPERT_IQ3_XXS, EXPERT_IQ4_NL, EXPERT_NVFP4, EXPERT_Q3_K, EXPERT_Q4_K, EXPERT_Q5_0, EXPERT_Q5_1, EXPERT_Q8_0, and IQ4_NL_BLOCK_BYTES.

Referenced by expert_dot(), and routed_softcapped_experts().

◆ packed_block_elements()

size_t packed_block_elements ( enum expert_storage storage)
static

Definition at line 57 of file routed_softcapped_experts_checked.c.

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}
#define QK_NVFP4
#define QK5_1

References EXPERT_F32, EXPERT_IQ2_S, EXPERT_IQ2_XS, EXPERT_IQ3_S, EXPERT_IQ3_XXS, EXPERT_NVFP4, EXPERT_Q3_K, EXPERT_Q4_K, EXPERT_Q5_0, EXPERT_Q5_1, QK5_0, QK5_1, QK8_0, QK_K, and QK_NVFP4.

Referenced by routed_softcapped_experts().

◆ prepare_expert_activation()

int prepare_expert_activation ( const struct routed_projection * p,
float * scratch,
unsigned char * packed_activation )
static

Definition at line 199 of file routed_softcapped_experts_checked.c.

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}
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)

References ck_pack_q8_activation_rne(), silu_mul_rows_checked_f32(), and softcapped_swiglu_rows_checked_f32().

Referenced by finish_expert(), and project_expert_pair().

◆ project_expert()

int project_expert ( const struct routed_projection * p,
const float * input,
const unsigned char * packed_hidden,
size_t slot,
float * scratch,
float * output )
static

Definition at line 244 of file routed_softcapped_experts_checked.c.

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}
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 finish_expert(const struct routed_projection *p, size_t slot, float *scratch, float *output)

References ck_q8_activation_dot_pair(), expert_dot(), finish_expert(), and slot().

Referenced by project_expert_pair_jobs(), project_grouped_experts(), project_routes(), and project_rows().

◆ project_expert_pair()

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 )
static

Definition at line 274 of file routed_softcapped_experts_checked.c.

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}

References ck_checked_finite(), expert_packed_activation(), EXPERT_Q5_0, finish_expert(), prepare_expert_activation(), and slot().

Referenced by project_expert_pair_jobs(), and project_grouped_experts().

◆ project_expert_pair_jobs()

void project_expert_pair_jobs ( size_t begin,
size_t end,
void * opaque )
static

Definition at line 492 of file routed_softcapped_experts_checked.c.

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}
int ck_threadpool_thread_id(const ck_threadpool_t *pool)
static int project_expert(const struct routed_projection *p, const float *input, const unsigned char *packed_hidden, size_t slot, float *scratch, float *output)
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)

References ck_threadpool_thread_id(), end, project_expert(), project_expert_pair(), and slot().

Referenced by routed_softcapped_experts().

◆ project_grouped_experts()

void project_grouped_experts ( size_t begin,
size_t end,
void * opaque )
static

Definition at line 428 of file routed_softcapped_experts_checked.c.

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}

References end, id, project_expert(), and project_expert_pair().

Referenced by routed_softcapped_experts().

◆ project_routes()

void project_routes ( size_t begin,
size_t end,
void * data )
static

Definition at line 384 of file routed_softcapped_experts_checked.c.

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}
static unsigned char * hidden_workspace(const struct routed_projection *p, float *scratch)

References end, hidden_workspace(), and project_expert().

Referenced by routed_softcapped_experts().

◆ project_rows()

void project_rows ( size_t begin,
size_t end,
void * data )
static

Definition at line 399 of file routed_softcapped_experts_checked.c.

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}

References combine_expert(), end, hidden_workspace(), order_slots(), pack_hidden(), and project_expert().

Referenced by routed_softcapped_experts().

◆ q3_k_expert_matvec()

void q3_k_expert_matvec ( float * out,
const void * weight,
const void * input,
int rows,
int width )
static

Definition at line 825 of file routed_softcapped_experts_checked.c.

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}
static float ck_dot_q3_k_q8_k(size_t blocks, const ck_block_q3_k *weights, const block_q8_K *input)

References ck_dot_q3_k_q8_k(), and QK_K.

Referenced by routed_softcapped_experts_checked_q3_k_nvfp4_scaled().

◆ region()

int region ( const void * p,
size_t bytes )
static

Definition at line 112 of file routed_softcapped_experts_checked.c.

113{
114 return p && bytes && (uintptr_t)p <= UINTPTR_MAX - bytes;
115}

Referenced by routed_softcapped_experts().

◆ routed_softcapped_experts()

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 )
static

Definition at line 517 of file routed_softcapped_experts_checked.c.

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}
static void ck_checked_parallel_for(size_t count, int grain, ck_checked_range_fn fn, void *args)
#define CK_THREADPOOL_MAX_THREADS
ck_threadpool_t * ck_threadpool_global(void)
#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)
static int region(const void *p, size_t bytes)
static void pack_grouped_rows(size_t begin, size_t end, void *data)
static int contains_overlap(const void *p, size_t pn, const void *q, size_t qn)
static size_t packed_block_elements(enum expert_storage storage)
static void project_rows(size_t begin, size_t end, void *data)
static void project_grouped_experts(size_t begin, size_t end, void *opaque)
static void project_routes(size_t begin, size_t end, void *data)
static void mix_grouped_rows(size_t begin, size_t end, void *data)
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)

References buffers(), CHECKED_ROUTED_EXPERT_MAX_ROWS, ck_checked_finite(), ck_checked_parallel_for(), ck_checked_region(), ck_threadpool_global(), CK_THREADPOOL_MAX_THREADS, collect_expert_pair_jobs(), combine_expert(), contains_overlap(), count, EXPERT_Q5_0, hidden_workspace(), id, ids, mix_grouped_rows(), order_slots(), pack_grouped_rows(), pack_hidden(), packed_block_bytes(), packed_block_elements(), project_expert_pair_jobs(), project_grouped_experts(), project_routes(), project_rows(), QK8_0, QK_K, region(), ROUTED_EXPERT_ARGS, and slot().

Referenced by routed_swiglu_experts_checked_f32(), and routed_swiglu_experts_checked_q3_k_q5_1().

◆ routed_softcapped_experts_checked_f32()

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 )

Definition at line 684 of file routed_softcapped_experts_checked.c.

685{
686 return ROUTED_EXPERT_FORWARD(EXPERT_F32, EXPERT_F32, matrix_bytes, 0, 0, NULL);
687}
#define ROUTED_EXPERT_FORWARD(storage, down_storage, down_bytes, quantize, reference_moe, packed_k)

References EXPERT_F32, ROUTED_EXPERT_ARGS, and ROUTED_EXPERT_FORWARD.

◆ routed_softcapped_experts_checked_iq2_s_nvfp4_scaled()

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 )

◆ routed_softcapped_experts_checked_iq2_xs_nvfp4_scaled()

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 )

◆ routed_softcapped_experts_checked_iq3_s_iq4_nl()

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 )

◆ routed_softcapped_experts_checked_iq3_xxs_iq4_nl()

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 )

◆ routed_softcapped_experts_checked_iq3_xxs_nvfp4_scaled()

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 )

◆ routed_softcapped_experts_checked_iq4_nl()

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 )

Definition at line 711 of file routed_softcapped_experts_checked.c.

712{
713 return ROUTED_EXPERT_FORWARD(EXPERT_IQ4_NL, EXPERT_IQ4_NL, matrix_bytes, 0, 0, NULL);
714}

References EXPERT_IQ4_NL, ROUTED_EXPERT_ARGS, and ROUTED_EXPERT_FORWARD.

◆ routed_softcapped_experts_checked_nvfp4_nvfp4_scaled()

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 )

◆ routed_softcapped_experts_checked_q3_k_nvfp4_scaled()

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 )

◆ routed_softcapped_experts_checked_q4_k_q5_0()

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 )

Definition at line 728 of file routed_softcapped_experts_checked.c.

729{
730 return ROUTED_EXPERT_FORWARD(EXPERT_Q4_K, EXPERT_Q5_0, down_matrix_bytes, 0, 0, NULL);
731}

References EXPERT_Q4_K, EXPERT_Q5_0, ROUTED_EXPERT_ARGS, and ROUTED_EXPERT_FORWARD.

◆ routed_softcapped_experts_checked_q4_k_q5_0_grouped()

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 )

Definition at line 758 of file routed_softcapped_experts_checked.c.

759{
760 return ROUTED_EXPERT_FORWARD(EXPERT_Q4_K, EXPERT_Q5_0, down_matrix_bytes, 0, 0, &q4_grouped);
761}
static const struct packed_k_expert_ops q4_grouped

References EXPERT_Q4_K, EXPERT_Q5_0, q4_grouped, ROUTED_EXPERT_ARGS, and ROUTED_EXPERT_FORWARD.

◆ routed_softcapped_experts_checked_q4_k_q5_0_grouped_pair()

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 )

Definition at line 768 of file routed_softcapped_experts_checked.c.

769{
770 return ROUTED_EXPERT_FORWARD(EXPERT_Q4_K, EXPERT_Q5_0, down_matrix_bytes, 0, 0, &q4_grouped_pairs);
771}
static const struct packed_k_expert_ops q4_grouped_pairs

References EXPERT_Q4_K, EXPERT_Q5_0, q4_grouped_pairs, ROUTED_EXPERT_ARGS, and ROUTED_EXPERT_FORWARD.

◆ routed_softcapped_experts_checked_q4_k_q5_0_pair_jobs()

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 )

Definition at line 778 of file routed_softcapped_experts_checked.c.

779{
780 return ROUTED_EXPERT_FORWARD(EXPERT_Q4_K, EXPERT_Q5_0, down_matrix_bytes, 0, 0, &q4_pair_jobs);
781}
static const struct packed_k_expert_ops q4_pair_jobs

References EXPERT_Q4_K, EXPERT_Q5_0, q4_pair_jobs, ROUTED_EXPERT_ARGS, and ROUTED_EXPERT_FORWARD.

◆ routed_softcapped_experts_checked_q4_k_q5_0_q8_k_hidden()

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 )

Definition at line 738 of file routed_softcapped_experts_checked.c.

739{
740 return ROUTED_EXPERT_FORWARD(EXPERT_Q4_K, EXPERT_Q5_0, down_matrix_bytes, 0, 0, &q4_hidden);
741}
static const struct packed_k_expert_ops q4_hidden

References EXPERT_Q4_K, EXPERT_Q5_0, q4_hidden, ROUTED_EXPERT_ARGS, and ROUTED_EXPERT_FORWARD.

◆ routed_softcapped_experts_checked_q4_k_q5_0_q8_k_hidden_q8_0_down()

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 )

Definition at line 748 of file routed_softcapped_experts_checked.c.

749{
750 return ROUTED_EXPERT_FORWARD(EXPERT_Q4_K, EXPERT_Q5_0, down_matrix_bytes, 0, 0, &q4_hidden_and_down);
751}
static const struct packed_k_expert_ops q4_hidden_and_down

References EXPERT_Q4_K, EXPERT_Q5_0, q4_hidden_and_down, ROUTED_EXPERT_ARGS, and ROUTED_EXPERT_FORWARD.

◆ routed_softcapped_experts_checked_q4_k_q8_0()

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 )

Definition at line 733 of file routed_softcapped_experts_checked.c.

734{
735 return ROUTED_EXPERT_FORWARD(EXPERT_Q4_K, EXPERT_Q8_0, down_matrix_bytes, 0, 0, NULL);
736}

References EXPERT_Q4_K, EXPERT_Q8_0, ROUTED_EXPERT_ARGS, and ROUTED_EXPERT_FORWARD.

◆ routed_softcapped_experts_checked_q4_k_q8_0_grouped()

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 )

Definition at line 763 of file routed_softcapped_experts_checked.c.

764{
765 return ROUTED_EXPERT_FORWARD(EXPERT_Q4_K, EXPERT_Q8_0, down_matrix_bytes, 0, 0, &q4_grouped);
766}

References EXPERT_Q4_K, EXPERT_Q8_0, q4_grouped, ROUTED_EXPERT_ARGS, and ROUTED_EXPERT_FORWARD.

◆ routed_softcapped_experts_checked_q4_k_q8_0_grouped_pair()

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 )

◆ routed_softcapped_experts_checked_q4_k_q8_0_pair_jobs()

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 )

Definition at line 783 of file routed_softcapped_experts_checked.c.

784{
785 return ROUTED_EXPERT_FORWARD(EXPERT_Q4_K, EXPERT_Q8_0, down_matrix_bytes, 0, 0, &q4_pair_jobs);
786}

References EXPERT_Q4_K, EXPERT_Q8_0, q4_pair_jobs, ROUTED_EXPERT_ARGS, and ROUTED_EXPERT_FORWARD.

◆ routed_softcapped_experts_checked_q4_k_q8_0_q8_k_hidden()

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 )

Definition at line 743 of file routed_softcapped_experts_checked.c.

744{
745 return ROUTED_EXPERT_FORWARD(EXPERT_Q4_K, EXPERT_Q8_0, down_matrix_bytes, 0, 0, &q4_hidden);
746}

References EXPERT_Q4_K, EXPERT_Q8_0, q4_hidden, ROUTED_EXPERT_ARGS, and ROUTED_EXPERT_FORWARD.

◆ routed_softcapped_experts_checked_q4_k_q8_0_q8_k_hidden_q8_0_down()

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 )

◆ routed_softcapped_experts_checked_q8_0()

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 )

Definition at line 706 of file routed_softcapped_experts_checked.c.

707{
708 return ROUTED_EXPERT_FORWARD(EXPERT_Q8_0, EXPERT_Q8_0, matrix_bytes, 0, 0, NULL);
709}

References EXPERT_Q8_0, ROUTED_EXPERT_ARGS, and ROUTED_EXPERT_FORWARD.

◆ routed_softcapped_experts_checked_q8_0_q8_activation()

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 )

Definition at line 717 of file routed_softcapped_experts_checked.c.

718{
719 return ROUTED_EXPERT_FORWARD(EXPERT_Q8_0, EXPERT_Q8_0, matrix_bytes, 1, 0, NULL);
720}

References EXPERT_Q8_0, ROUTED_EXPERT_ARGS, and ROUTED_EXPERT_FORWARD.

◆ routed_softcapped_experts_checked_q8_0_q8_activation_scaled_situ()

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 )

Definition at line 722 of file routed_softcapped_experts_checked.c.

723{
724 return ROUTED_EXPERT_FORWARD(EXPERT_Q8_0, EXPERT_Q8_0, matrix_bytes, 1, 1, NULL);
725}

References EXPERT_Q8_0, ROUTED_EXPERT_ARGS, and ROUTED_EXPERT_FORWARD.

◆ routed_swiglu_experts_checked_f32()

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 )

Definition at line 689 of file routed_softcapped_experts_checked.c.

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}

References EXPERT_ARGS, EXPERT_F32, ids, and routed_softcapped_experts().

◆ routed_swiglu_experts_checked_q3_k_q5_1()

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 )

Definition at line 697 of file routed_softcapped_experts_checked.c.

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}

References EXPERT_ARGS, EXPERT_Q3_K, EXPERT_Q5_1, ids, and routed_softcapped_experts().

Variable Documentation

◆ iq4_nl_values

const int8_t iq4_nl_values[16]
static
Initial value:
= {
-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113
}

Definition at line 35 of file routed_softcapped_experts_checked.c.

35 {
36 -127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113
37};

Referenced by expert_dot(), and iq4_nl_expert_matvec().

◆ q4_grouped

const struct packed_k_expert_ops q4_grouped
static
Initial value:
= {
.group_rows = 1,
}
void gemv_q4_k_q8_k_avx2(float *, const void *, const void *, int, int)

Definition at line 154 of file routed_softcapped_experts_checked.c.

154 {
155 .pack = ck_pack_q4_input_q8_k,
156 .matvec = gemv_q4_k_q8_k_avx2,
158 .group_rows = 1,
159};

Referenced by routed_softcapped_experts_checked_q4_k_q5_0_grouped(), and routed_softcapped_experts_checked_q4_k_q8_0_grouped().

◆ q4_grouped_pairs

const struct packed_k_expert_ops q4_grouped_pairs
static
Initial value:
= {
.group_rows = 1,
}
void gemv_q4_k_q8_k_pair_checked_cpu(float *, float *, const void *, const void *, const void *, int, int)
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)

Definition at line 161 of file routed_softcapped_experts_checked.c.

161 {
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};

Referenced by routed_softcapped_experts_checked_q4_k_q5_0_grouped_pair(), and routed_softcapped_experts_checked_q4_k_q8_0_grouped_pair().

◆ q4_hidden

const struct packed_k_expert_ops q4_hidden
static

◆ q4_hidden_and_down

const struct packed_k_expert_ops q4_hidden_and_down
static

◆ q4_pair_jobs

const struct packed_k_expert_ops q4_pair_jobs
static
Initial value:
= {
.group_rows = 1,
.dispatch_pairs = 1,
}

Definition at line 170 of file routed_softcapped_experts_checked.c.

170 {
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};

Referenced by routed_softcapped_experts_checked_q4_k_q5_0_pair_jobs(), and routed_softcapped_experts_checked_q4_k_q8_0_pair_jobs().