Scalar reference kernels for DeepSeek-style research ops. More...
#include <math.h>#include <float.h>#include <limits.h>#include <stddef.h>#include <stdint.h>#include <stdlib.h>#include <string.h>#include "bf16_utils.h"#include "ck_threadpool.h"Go to the source code of this file.
Functions | |
| void | deepseek_csa_attention_backward_f32 (const float *d_out, const float *q, const float *k, const float *v, const int *indices, const float *attn, float *d_q, float *d_k, float *d_v, int query_tokens, int key_tokens, int heads, int dim, int top_k, float scale) |
| void | deepseek_csa_attention_f32 (const float *q, const float *k, const float *v, const int *indices, float *out, float *attn, int query_tokens, int key_tokens, int heads, int dim, int top_k, float scale) |
| void | deepseek_dsa_topk_softmax_backward_f32 (const int *indices, const float *weights, const float *d_weights, float *d_scores, int tokens, int heads, int key_count, int top_k) |
| void | deepseek_dsa_topk_softmax_f32 (const float *scores, int *indices, float *weights, int tokens, int heads, int key_count, int top_k) |
| void | deepseek_hybrid_attention_f32 (const float *q, const float *k, const float *v, const int *indices, float *out, float *attn, int query_tokens, int key_tokens, int heads, int dim, int top_k, float scale, int mode) |
| void | deepseek_mhc_mix_backward_f32 (const float *d_out, const float *streams, const float *mix, float *d_streams, float *d_mix, int tokens, int n_streams, int dim) |
| void | deepseek_mhc_mix_f32 (const float *streams, const float *mix, float *out, int tokens, int n_streams, int dim) |
| void | deepseek_mla_attention_decode_f32 (const float *q, const float *k_cache, const float *v_cache, float *output, int num_heads, int num_kv_heads, int cache_len, int qk_head_dim, int v_head_dim, int max_seq_len, int cache_stride) |
| void | deepseek_mla_attention_decode_f32_workspace (const float *q, const float *k_cache, const float *v_cache, float *output, int num_heads, int num_kv_heads, int cache_len, int qk_head_dim, int v_head_dim, int max_seq_len, int cache_stride, float scale, float *scores, size_t scores_bytes) |
| void | deepseek_mla_attention_f32 (const float *q, const float *k, const float *v, float *output, int num_heads, int num_kv_heads, int num_tokens, int qk_head_dim, int v_head_dim) |
| void | deepseek_mla_attention_f32_parallel_dispatch (const float *q, const float *k, const float *v, float *output, int num_heads, int num_kv_heads, int num_tokens, int qk_head_dim, int v_head_dim, float scale, float *scores, size_t scores_bytes) |
| void | deepseek_mla_attention_f32_workspace (const float *q, const float *k, const float *v, float *output, int num_heads, int num_kv_heads, int num_tokens, int qk_head_dim, int v_head_dim, float scale, float *scores, size_t scores_bytes) |
| void | deepseek_mla_kv_cache_batch_store_f32 (float *k_cache, float *v_cache, const float *k, const float *v, int num_tokens, int num_kv_heads, int qk_head_dim, int v_head_dim, int max_seq_len, int cache_stride) |
| void | deepseek_mla_kv_cache_store_f32 (float *k_cache, float *v_cache, const float *k, const float *v, int pos, int num_kv_heads, int qk_head_dim, int v_head_dim, int max_seq_len, int cache_stride) |
| void | deepseek_mla_kv_decompress_bf16 (const float *compressed_kv, const uint16_t *kv_b_proj, float *k_nope, float *value, int tokens, int heads, int kv_lora_rank, int qk_nope_dim, int v_dim) |
| void | deepseek_mla_kv_decompress_bf16_parallel_dispatch (const float *compressed_kv, const uint16_t *kv_b_proj, float *k_nope, float *value, int tokens, int heads, int kv_lora_rank, int qk_nope_dim, int v_dim) |
| void | deepseek_mla_kv_decompress_bf16_token_range (const float *compressed_kv, const uint16_t *kv_b_proj, float *k_nope, float *value, int tokens, int heads, int kv_lora_rank, int qk_nope_dim, int v_dim, int token_begin, int token_end) |
| void | deepseek_mla_kv_decompress_f32 (const float *compressed_kv, const float *kv_b_proj, float *k_nope, float *value, int tokens, int heads, int kv_lora_rank, int qk_nope_dim, int v_dim) |
| void | deepseek_mla_partial_rope_concat_f32 (const float *q_nope, const float *q_pe, const float *k_nope, const float *k_pe, const float *cos, const float *sin, float *query, float *key, int tokens, int heads, int qk_nope_dim, int qk_rope_dim) |
| void | deepseek_mla_partial_rope_concat_packed_bf16_storage (const float *q_packed, const float *k_nope, const float *kv_a_packed, const float *cos, const float *sin, float *query, float *key, int tokens, int heads, int kv_lora_rank, int qk_nope_dim, int qk_rope_dim) |
| void | deepseek_mla_partial_rope_concat_packed_f32 (const float *q_packed, const float *k_nope, const float *kv_a_packed, const float *cos, const float *sin, float *query, float *key, int tokens, int heads, int kv_lora_rank, int qk_nope_dim, int qk_rope_dim) |
| static size_t | ds_mhc_idx (int t, int s, int d, int n_streams, int dim) |
| static size_t | ds_mix_idx (int t, int out_s, int in_s, int n_streams) |
| static void | ds_mla_apply_kimi_rope (const float *src, float *dst, const float *cos_row, const float *sin_row, int dim) |
| static void | ds_mla_attention_f32_query_range (const float *q, const float *k, const float *v, float *output, int num_heads, int num_kv_heads, int num_tokens, int qk_head_dim, int v_head_dim, float scale, float *scores, int query_begin, int query_end, int query_step) |
| static void | ds_mla_attention_f32_work (int ith, int nth, void *opaque) |
| static float | ds_mla_bf16_round (float value) |
| static void | ds_mla_kv_decompress_bf16_rows (int begin, int end, void *opaque) |
| static size_t | ds_mla_thd_idx (int t, int h, int d, int heads, int dim) |
| static size_t | ds_mla_tok_idx (int t, int d, int dim) |
| static size_t | ds_qkv_idx (int token, int head, int d, int heads, int dim) |
| static void | ds_softmax (float *x, int n) |
| 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. | |
Scalar reference kernels for DeepSeek-style research ops.
These kernels intentionally prioritize explicit math contracts over speed. They are used to pin PyTorch parity before adding SIMD/threaded variants.
Definition in file deepseek_kernels.c.
| void deepseek_csa_attention_backward_f32 | ( | const float * | d_out, |
| const float * | q, | ||
| const float * | k, | ||
| const float * | v, | ||
| const int * | indices, | ||
| const float * | attn, | ||
| float * | d_q, | ||
| float * | d_k, | ||
| float * | d_v, | ||
| int | query_tokens, | ||
| int | key_tokens, | ||
| int | heads, | ||
| int | dim, | ||
| int | top_k, | ||
| float | scale | ||
| ) |
Definition at line 621 of file deepseek_kernels.c.
References ds_qkv_idx().
| void deepseek_csa_attention_f32 | ( | const float * | q, |
| const float * | k, | ||
| const float * | v, | ||
| const int * | indices, | ||
| float * | out, | ||
| float * | attn, | ||
| int | query_tokens, | ||
| int | key_tokens, | ||
| int | heads, | ||
| int | dim, | ||
| int | top_k, | ||
| float | scale | ||
| ) |
Definition at line 562 of file deepseek_kernels.c.
References ds_qkv_idx(), and ds_softmax().
Referenced by deepseek_hybrid_attention_f32().
| void deepseek_dsa_topk_softmax_backward_f32 | ( | const int * | indices, |
| const float * | weights, | ||
| const float * | d_weights, | ||
| float * | d_scores, | ||
| int | tokens, | ||
| int | heads, | ||
| int | key_count, | ||
| int | top_k | ||
| ) |
Definition at line 153 of file deepseek_kernels.c.
References topk_softmax_backward_f32().
| void deepseek_dsa_topk_softmax_f32 | ( | const float * | scores, |
| int * | indices, | ||
| float * | weights, | ||
| int | tokens, | ||
| int | heads, | ||
| int | key_count, | ||
| int | top_k | ||
| ) |
Definition at line 86 of file deepseek_kernels.c.
| void deepseek_hybrid_attention_f32 | ( | const float * | q, |
| const float * | k, | ||
| const float * | v, | ||
| const int * | indices, | ||
| float * | out, | ||
| float * | attn, | ||
| int | query_tokens, | ||
| int | key_tokens, | ||
| int | heads, | ||
| int | dim, | ||
| int | top_k, | ||
| float | scale, | ||
| int | mode | ||
| ) |
Definition at line 683 of file deepseek_kernels.c.
References deepseek_csa_attention_f32().
| void deepseek_mhc_mix_backward_f32 | ( | const float * | d_out, |
| const float * | streams, | ||
| const float * | mix, | ||
| float * | d_streams, | ||
| float * | d_mix, | ||
| int | tokens, | ||
| int | n_streams, | ||
| int | dim | ||
| ) |
Definition at line 53 of file deepseek_kernels.c.
References ds_mhc_idx(), and ds_mix_idx().
| void deepseek_mhc_mix_f32 | ( | const float * | streams, |
| const float * | mix, | ||
| float * | out, | ||
| int | tokens, | ||
| int | n_streams, | ||
| int | dim | ||
| ) |
Definition at line 30 of file deepseek_kernels.c.
References ds_mhc_idx(), and ds_mix_idx().
| void deepseek_mla_attention_decode_f32 | ( | const float * | q, |
| const float * | k_cache, | ||
| const float * | v_cache, | ||
| float * | output, | ||
| int | num_heads, | ||
| int | num_kv_heads, | ||
| int | cache_len, | ||
| int | qk_head_dim, | ||
| int | v_head_dim, | ||
| int | max_seq_len, | ||
| int | cache_stride | ||
| ) |
Definition at line 1044 of file deepseek_kernels.c.
References deepseek_mla_attention_decode_f32_workspace().
| void deepseek_mla_attention_decode_f32_workspace | ( | const float * | q, |
| const float * | k_cache, | ||
| const float * | v_cache, | ||
| float * | output, | ||
| int | num_heads, | ||
| int | num_kv_heads, | ||
| int | cache_len, | ||
| int | qk_head_dim, | ||
| int | v_head_dim, | ||
| int | max_seq_len, | ||
| int | cache_stride, | ||
| float | scale, | ||
| float * | scores, | ||
| size_t | scores_bytes | ||
| ) |
Definition at line 978 of file deepseek_kernels.c.
References score.
Referenced by deepseek_mla_attention_decode_f32().
| void deepseek_mla_attention_f32 | ( | const float * | q, |
| const float * | k, | ||
| const float * | v, | ||
| float * | output, | ||
| int | num_heads, | ||
| int | num_kv_heads, | ||
| int | num_tokens, | ||
| int | qk_head_dim, | ||
| int | v_head_dim | ||
| ) |
Definition at line 879 of file deepseek_kernels.c.
References deepseek_mla_attention_f32_workspace().
| void deepseek_mla_attention_f32_parallel_dispatch | ( | const float * | q, |
| const float * | k, | ||
| const float * | v, | ||
| float * | output, | ||
| int | num_heads, | ||
| int | num_kv_heads, | ||
| int | num_tokens, | ||
| int | qk_head_dim, | ||
| int | v_head_dim, | ||
| float | scale, | ||
| float * | scores, | ||
| size_t | scores_bytes | ||
| ) |
Definition at line 839 of file deepseek_kernels.c.
References ck_threadpool_dispatch_n(), ck_threadpool_global(), ck_threadpool_n_threads(), deepseek_mla_attention_f32_workspace(), and ds_mla_attention_f32_work().
| void deepseek_mla_attention_f32_workspace | ( | const float * | q, |
| const float * | k, | ||
| const float * | v, | ||
| float * | output, | ||
| int | num_heads, | ||
| int | num_kv_heads, | ||
| int | num_tokens, | ||
| int | qk_head_dim, | ||
| int | v_head_dim, | ||
| float | scale, | ||
| float * | scores, | ||
| size_t | scores_bytes | ||
| ) |
Definition at line 768 of file deepseek_kernels.c.
References ds_mla_attention_f32_query_range().
Referenced by deepseek_mla_attention_f32(), and deepseek_mla_attention_f32_parallel_dispatch().
| void deepseek_mla_kv_cache_batch_store_f32 | ( | float * | k_cache, |
| float * | v_cache, | ||
| const float * | k, | ||
| const float * | v, | ||
| int | num_tokens, | ||
| int | num_kv_heads, | ||
| int | qk_head_dim, | ||
| int | v_head_dim, | ||
| int | max_seq_len, | ||
| int | cache_stride | ||
| ) |
Definition at line 900 of file deepseek_kernels.c.
| void deepseek_mla_kv_cache_store_f32 | ( | float * | k_cache, |
| float * | v_cache, | ||
| const float * | k, | ||
| const float * | v, | ||
| int | pos, | ||
| int | num_kv_heads, | ||
| int | qk_head_dim, | ||
| int | v_head_dim, | ||
| int | max_seq_len, | ||
| int | cache_stride | ||
| ) |
Definition at line 937 of file deepseek_kernels.c.
| void deepseek_mla_kv_decompress_bf16 | ( | const float * | compressed_kv, |
| const uint16_t * | kv_b_proj, | ||
| float * | k_nope, | ||
| float * | value, | ||
| int | tokens, | ||
| int | heads, | ||
| int | kv_lora_rank, | ||
| int | qk_nope_dim, | ||
| int | v_dim | ||
| ) |
Definition at line 270 of file deepseek_kernels.c.
References deepseek_mla_kv_decompress_bf16_token_range().
Referenced by deepseek_mla_kv_decompress_bf16_parallel_dispatch().
| void deepseek_mla_kv_decompress_bf16_parallel_dispatch | ( | const float * | compressed_kv, |
| const uint16_t * | kv_b_proj, | ||
| float * | k_nope, | ||
| float * | value, | ||
| int | tokens, | ||
| int | heads, | ||
| int | kv_lora_rank, | ||
| int | qk_nope_dim, | ||
| int | v_dim | ||
| ) |
Definition at line 308 of file deepseek_kernels.c.
References ck_threadpool_global(), ck_threadpool_n_threads(), ck_threadpool_parallel_for_n(), deepseek_mla_kv_decompress_bf16(), and ds_mla_kv_decompress_bf16_rows().
| void deepseek_mla_kv_decompress_bf16_token_range | ( | const float * | compressed_kv, |
| const uint16_t * | kv_b_proj, | ||
| float * | k_nope, | ||
| float * | value, | ||
| int | tokens, | ||
| int | heads, | ||
| int | kv_lora_rank, | ||
| int | qk_nope_dim, | ||
| int | v_dim, | ||
| int | token_begin, | ||
| int | token_end | ||
| ) |
Definition at line 225 of file deepseek_kernels.c.
References bf16_to_float(), ds_mla_thd_idx(), ds_mla_tok_idx(), and float_to_bf16().
Referenced by deepseek_mla_kv_decompress_bf16(), and ds_mla_kv_decompress_bf16_rows().
| void deepseek_mla_kv_decompress_f32 | ( | const float * | compressed_kv, |
| const float * | kv_b_proj, | ||
| float * | k_nope, | ||
| float * | value, | ||
| int | tokens, | ||
| int | heads, | ||
| int | kv_lora_rank, | ||
| int | qk_nope_dim, | ||
| int | v_dim | ||
| ) |
Definition at line 185 of file deepseek_kernels.c.
References ds_mla_thd_idx(), and ds_mla_tok_idx().
| void deepseek_mla_partial_rope_concat_f32 | ( | const float * | q_nope, |
| const float * | q_pe, | ||
| const float * | k_nope, | ||
| const float * | k_pe, | ||
| const float * | cos, | ||
| const float * | sin, | ||
| float * | query, | ||
| float * | key, | ||
| int | tokens, | ||
| int | heads, | ||
| int | qk_nope_dim, | ||
| int | qk_rope_dim | ||
| ) |
Definition at line 367 of file deepseek_kernels.c.
References ds_mla_apply_kimi_rope(), ds_mla_thd_idx(), and ds_mla_tok_idx().
| void deepseek_mla_partial_rope_concat_packed_bf16_storage | ( | const float * | q_packed, |
| const float * | k_nope, | ||
| const float * | kv_a_packed, | ||
| const float * | cos, | ||
| const float * | sin, | ||
| float * | query, | ||
| float * | key, | ||
| int | tokens, | ||
| int | heads, | ||
| int | kv_lora_rank, | ||
| int | qk_nope_dim, | ||
| int | qk_rope_dim | ||
| ) |
Definition at line 473 of file deepseek_kernels.c.
References ds_mla_bf16_round(), and ds_mla_thd_idx().
| void deepseek_mla_partial_rope_concat_packed_f32 | ( | const float * | q_packed, |
| const float * | k_nope, | ||
| const float * | kv_a_packed, | ||
| const float * | cos, | ||
| const float * | sin, | ||
| float * | query, | ||
| float * | key, | ||
| int | tokens, | ||
| int | heads, | ||
| int | kv_lora_rank, | ||
| int | qk_nope_dim, | ||
| int | qk_rope_dim | ||
| ) |
Definition at line 406 of file deepseek_kernels.c.
References ds_mla_thd_idx().
|
inlinestatic |
Definition at line 20 of file deepseek_kernels.c.
Referenced by deepseek_mhc_mix_backward_f32(), and deepseek_mhc_mix_f32().
|
inlinestatic |
Definition at line 25 of file deepseek_kernels.c.
Referenced by deepseek_mhc_mix_backward_f32(), and deepseek_mhc_mix_f32().
|
static |
Definition at line 350 of file deepseek_kernels.c.
Referenced by deepseek_mla_partial_rope_concat_f32().
|
static |
Definition at line 716 of file deepseek_kernels.c.
References ds_mla_thd_idx(), and score.
Referenced by deepseek_mla_attention_f32_workspace(), and ds_mla_attention_f32_work().
|
static |
Definition at line 813 of file deepseek_kernels.c.
References ds_mla_attention_f32_query_range(), and token.
Referenced by deepseek_mla_attention_f32_parallel_dispatch().
|
inlinestatic |
Definition at line 468 of file deepseek_kernels.c.
References bf16_to_float(), and float_to_bf16().
Referenced by deepseek_mla_partial_rope_concat_packed_bf16_storage().
|
static |
Definition at line 298 of file deepseek_kernels.c.
References deepseek_mla_kv_decompress_bf16_token_range(), and end.
Referenced by deepseek_mla_kv_decompress_bf16_parallel_dispatch().
|
inlinestatic |
Definition at line 180 of file deepseek_kernels.c.
Referenced by deepseek_mla_kv_decompress_bf16_token_range(), deepseek_mla_kv_decompress_f32(), deepseek_mla_partial_rope_concat_f32(), deepseek_mla_partial_rope_concat_packed_bf16_storage(), deepseek_mla_partial_rope_concat_packed_f32(), and ds_mla_attention_f32_query_range().
|
inlinestatic |
Definition at line 175 of file deepseek_kernels.c.
Referenced by deepseek_mla_kv_decompress_bf16_token_range(), deepseek_mla_kv_decompress_f32(), and deepseek_mla_partial_rope_concat_f32().
|
inlinestatic |
Definition at line 541 of file deepseek_kernels.c.
References token.
Referenced by deepseek_csa_attention_backward_f32(), and deepseek_csa_attention_f32().
|
static |
Definition at line 546 of file deepseek_kernels.c.
Referenced by deepseek_csa_attention_f32().
|
extern |
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().