Top-K selection kernels for MoE router dispatch. More...
#include <stdint.h>#include <stddef.h>#include <float.h>#include <math.h>#include "bf16_utils.h"Go to the source code of this file.
Functions | |
| int | argmax_f32 (const float *scores, int n) |
| Find index of maximum value. | |
| static double | ck_moe_llama_softmax_row (float *probabilities, const float *logits, int n_experts, float max_value) |
| static void | ck_topk_insert_desc (int idx, float val, int *indices, float *values, int k) |
| static void | group_limited_topk_router_f32_impl (const float *scores, const float *correction_bias, int *indices, float *weights, int rows, int n_experts, int top_k, int n_group, int topk_group, int norm_topk_prob, float routed_scaling_factor, int apply_sigmoid) |
| void | group_limited_topk_router_sigmoid_f32 (const float *logits, const float *correction_bias, int *indices, float *weights, int rows, int n_experts, int top_k, int n_group, int topk_group, int norm_topk_prob, float routed_scaling_factor) |
| int | moe_softmax_topk_router_llama_f32_workspace (const float *logits, int *indices, float *weights, int rows, int n_experts, int top_k, float routed_scaling_factor, void *workspace, size_t workspace_bytes) |
| int | moe_softmax_topk_router_pytorch_bf16_workspace (const float *logits, int *indices, float *weights, int rows, int n_experts, int top_k, float routed_scaling_factor, void *workspace, size_t workspace_bytes) |
| size_t | moe_softmax_topk_router_workspace_bytes (int n_experts) |
| void | nemotron_group_limited_topk_router_f32 (const float *scores, const float *correction_bias, int *indices, float *weights, int rows, int n_experts, int top_k, int n_group, int topk_group, int norm_topk_prob, float routed_scaling_factor) |
| void | speculative_commit_one_i32 (int accepted, int verified_token, int *token_buffer, int *token_count, int max_tokens, int *target_position, int *draft_position, int *accepted_count, int *rejected_count) |
| Commit one verified speculative token and update decode counters. | |
| void | speculative_verify_greedy_f32 (const float *target_logits, int vocab_size, int draft_token, int *accepted, int *verified_token) |
| Greedy one-token speculative verification. | |
| void | topk_batched_f32 (const float *scores, int num_tokens, int n_experts, int k, int *indices, float *weights) |
| Batched top-K selection for multiple tokens. | |
| void | topk_f32 (const float *scores, int n, int k, int *indices, float *values) |
| Find top-K indices and values from a score vector. | |
| void | topk_softmax_backward_f32 (const int *indices, const float *weights, const float *d_weights, float *d_scores, int num_tokens, int n_experts_or_keys, int k) |
| Backward for hard top-k followed by softmax over selected values. | |
| void | topk_softmax_f32 (const float *scores, int n, int k, int *indices, float *weights) |
| Find top-K indices with softmax-normalized weights. | |
Top-K selection kernels for MoE router dispatch.
After changes: make test && make llamacpp-parity-full
Provides efficient top-K selection from a score vector. Used in Mixture-of-Experts models to select which experts process each token.
Operations:
Definition in file topk_kernels.c.
| int argmax_f32 | ( | const float * | scores, |
| int | n | ||
| ) |
Find index of maximum value.
| scores | Input scores [n] |
| n | Number of scores |
Definition at line 500 of file topk_kernels.c.
Referenced by speculative_verify_greedy_f32().
|
static |
Definition at line 282 of file topk_kernels.c.
Referenced by moe_softmax_topk_router_llama_f32_workspace().
|
static |
Definition at line 668 of file topk_kernels.c.
Referenced by group_limited_topk_router_f32_impl().
|
static |
Definition at line 683 of file topk_kernels.c.
References ck_topk_insert_desc(), and start.
Referenced by group_limited_topk_router_sigmoid_f32(), and nemotron_group_limited_topk_router_f32().
| void group_limited_topk_router_sigmoid_f32 | ( | const float * | logits, |
| const float * | correction_bias, | ||
| int * | indices, | ||
| float * | weights, | ||
| int | rows, | ||
| int | n_experts, | ||
| int | top_k, | ||
| int | n_group, | ||
| int | topk_group, | ||
| int | norm_topk_prob, | ||
| float | routed_scaling_factor | ||
| ) |
Definition at line 798 of file topk_kernels.c.
References group_limited_topk_router_f32_impl().
| int moe_softmax_topk_router_llama_f32_workspace | ( | const float * | logits, |
| int * | indices, | ||
| float * | weights, | ||
| int | rows, | ||
| int | n_experts, | ||
| int | top_k, | ||
| float | routed_scaling_factor, | ||
| void * | workspace, | ||
| size_t | workspace_bytes | ||
| ) |
Definition at line 316 of file topk_kernels.c.
References ck_moe_llama_softmax_row(), moe_softmax_topk_router_workspace_bytes(), and topk_f32().
Referenced by moe_softmax_topk_router_pytorch_bf16_workspace().
| int moe_softmax_topk_router_pytorch_bf16_workspace | ( | const float * | logits, |
| int * | indices, | ||
| float * | weights, | ||
| int | rows, | ||
| int | n_experts, | ||
| int | top_k, | ||
| float | routed_scaling_factor, | ||
| void * | workspace, | ||
| size_t | workspace_bytes | ||
| ) |
Definition at line 375 of file topk_kernels.c.
References bf16_to_float(), float_to_bf16(), and moe_softmax_topk_router_llama_f32_workspace().
| size_t moe_softmax_topk_router_workspace_bytes | ( | int | n_experts | ) |
Definition at line 177 of file topk_kernels.c.
Referenced by moe_softmax_topk_router_llama_f32_workspace().
| void nemotron_group_limited_topk_router_f32 | ( | const float * | scores, |
| const float * | correction_bias, | ||
| int * | indices, | ||
| float * | weights, | ||
| int | rows, | ||
| int | n_experts, | ||
| int | top_k, | ||
| int | n_group, | ||
| int | topk_group, | ||
| int | norm_topk_prob, | ||
| float | routed_scaling_factor | ||
| ) |
Definition at line 780 of file topk_kernels.c.
References group_limited_topk_router_f32_impl().
| void speculative_commit_one_i32 | ( | int | accepted, |
| int | verified_token, | ||
| int * | token_buffer, | ||
| int * | token_count, | ||
| int | max_tokens, | ||
| int * | target_position, | ||
| int * | draft_position, | ||
| int * | accepted_count, | ||
| int * | rejected_count | ||
| ) |
Commit one verified speculative token and update decode counters.
This is the minimal state transition for the first Gemma4 assistant bridge: greedy, one draft token, target remains authoritative. For this milestone the draft cache is kept synchronized with the target position after each token. Multi-token speculative decoding can later replace this with prefix accept and partial draft-cache rollback.
Definition at line 611 of file topk_kernels.c.
| void speculative_verify_greedy_f32 | ( | const float * | target_logits, |
| int | vocab_size, | ||
| int | draft_token, | ||
| int * | accepted, | ||
| int * | verified_token | ||
| ) |
Greedy one-token speculative verification.
The draft model proposes draft_token. The target model is authoritative: if draft_token equals argmax(target_logits), the candidate is accepted and emitted. Otherwise the target argmax is emitted and the draft path must be reset or rewound by the runtime loop.
| target_logits | Target/backbone logits [vocab_size] |
| vocab_size | Number of logits |
| draft_token | Candidate token from draft/assistant model |
| accepted | Output scalar: 1 if accepted, 0 otherwise |
| verified_token | Output scalar: accepted draft token or target argmax |
Definition at line 585 of file topk_kernels.c.
References argmax_f32(), and vocab_size.
| void topk_batched_f32 | ( | const float * | scores, |
| int | num_tokens, | ||
| int | n_experts, | ||
| int | k, | ||
| int * | indices, | ||
| float * | weights | ||
| ) |
Batched top-K selection for multiple tokens.
| scores | Input scores [num_tokens, n_experts] |
| num_tokens | Number of tokens |
| n_experts | Number of experts |
| k | Number of experts to select per token |
| indices | Output: selected expert indices [num_tokens, k] |
| weights | Output: routing weights [num_tokens, k] (can be NULL for no softmax) |
Definition at line 465 of file topk_kernels.c.
References topk_f32(), and topk_softmax_f32().
| void topk_f32 | ( | const float * | scores, |
| int | n, | ||
| int | k, | ||
| int * | indices, | ||
| float * | values | ||
| ) |
Find top-K indices and values from a score vector.
| scores | Input scores [n] |
| n | Number of scores (e.g., number of experts) |
| k | Number of top scores to select |
| indices | Output: indices of top-K scores [k], sorted descending by value |
| values | Output: top-K score values [k], sorted descending (can be NULL) |
Definition at line 51 of file topk_kernels.c.
Referenced by moe_softmax_topk_router_llama_f32_workspace(), topk_batched_f32(), and topk_softmax_f32().
| void topk_softmax_backward_f32 | ( | const int * | indices, |
| const float * | weights, | ||
| const float * | d_weights, | ||
| float * | d_scores, | ||
| int | num_tokens, | ||
| int | n_experts_or_keys, | ||
| int | k | ||
| ) |
Backward for hard top-k followed by softmax over selected values.
Matches PyTorch behavior for: values, indices = torch.topk(scores, k, dim=-1) weights = torch.softmax(values, dim=-1)
The hard selected indices are treated as fixed for this backward pass. Gradients are scattered only to selected scores; unselected scores are zero.
Definition at line 408 of file topk_kernels.c.
Referenced by deepseek_dsa_topk_softmax_backward_f32().
| void topk_softmax_f32 | ( | const float * | scores, |
| int | n, | ||
| int | k, | ||
| int * | indices, | ||
| float * | weights | ||
| ) |
Find top-K indices with softmax-normalized weights.
| scores | Input scores [n] (router logits) |
| n | Number of scores |
| k | Number of top scores to select |
| indices | Output: indices of top-K scores [k] |
| weights | Output: softmax-normalized weights for selected [k], sum to 1.0 |
Definition at line 136 of file topk_kernels.c.
References topk_f32().
Referenced by topk_batched_f32().