← Back to C-Kernel-Engine Docs Doxygen Source Documentation
 
Loading...
Searching...
No Matches
ck_parity_api.h File Reference

C-Kernel-Engine Parity Testing API. More...

#include <stddef.h>
#include <stdint.h>

Go to the source code of this file.

Macros

#define CK_BLOCK_Q4_0_SIZE   18
 
#define CK_BLOCK_Q4_K_SIZE   144
 
#define CK_BLOCK_Q6_K_SIZE   210
 
#define CK_BLOCK_Q8_K_SIZE   292
 
#define CK_QK4_0   32 /* Elements per Q4_0 block */
 
#define CK_QK8_0   32 /* Elements per Q8_0 block */
 
#define CK_QK_K   256 /* Elements per K-quant super-block */
 

Functions

int ck_get_block_q4_k_size (void)
 Get Q4_K block size in bytes.
 
int ck_get_block_q6_k_size (void)
 Get Q6_K block size in bytes.
 
int ck_get_block_q8_k_size (void)
 Get Q8_K block size in bytes.
 
int ck_get_qk_k (void)
 Get QK_K (elements per super-block)
 
void ck_test_attention_causal (const float *q, const float *k, const float *v, float *out, int num_heads, int num_kv_heads, int tokens, int seq_len, int head_dim)
 Multi-head causal attention for prefill (head-major layout)
 
void ck_test_attn_gate_sigmoid_mul (const float *x, const float *gate, float *out, int rows, int dim)
 Multiply attention output rows by sigmoid(gate) elementwise.
 
void ck_test_dequant_q4_0 (const void *src, float *dst, int n)
 Dequantize Q4_0 data to FP32.
 
void ck_test_dequant_q4_k (const void *src, float *dst, int n)
 Dequantize Q4_K data to FP32.
 
void ck_test_dequant_q6_k (const void *src, float *dst, int n)
 Dequantize Q6_K data to FP32.
 
void ck_test_gated_deltanet_autoregressive (const float *q, const float *k, const float *v, const float *g, const float *beta, const float *state_in, float *state_out, float *out, int num_heads, int state_dim, float norm_eps)
 Gated DeltaNet autoregressive update.
 
void ck_test_gemm_q4_k (const void *weight_q4k, const float *input_f32, float *output, int rows, int cols, int n_tokens)
 Q4_K GEMM - batched matrix multiply with quantized weights.
 
void ck_test_gemm_q5_0 (const void *weight_q5_0, const float *input_f32, float *output, int rows, int cols, int n_tokens)
 Q5_0 GEMM - batched matrix multiply with Q5_0 weights (32-element blocks)
 
void ck_test_gemm_q6_k (const void *weight_q6k, const float *input_f32, float *output, int rows, int cols, int n_tokens)
 Q6_K GEMM - batched matrix multiply with Q6_K weights.
 
void ck_test_gemm_q8_0 (const void *weight_q8_0, const float *input_f32, float *output, int rows, int cols, int n_tokens)
 Q8_0 GEMM - batched matrix multiply with Q8_0 weights (32-element blocks)
 
void ck_test_gemv_q4_k (const void *weight_q4k, const float *input_f32, float *output, int cols)
 Q4_K GEMV - dot product of quantized weights and FP32 input.
 
void ck_test_gemv_q5_0 (const void *weight_q5_0, const float *input_f32, float *output, int rows, int cols)
 Q5_0 GEMV - matrix-vector multiply with Q5_0 weights.
 
void ck_test_gemv_q5_0_q8_0 (const void *weight_q5_0, const float *input_f32, float *output, int rows, int cols)
 Q5_0 x Q8_0 quantized GEMV - matches llama.cpp's approach.
 
void ck_test_gemv_q6_k (const void *weight_q6k, const float *input_f32, float *output, int cols)
 Q6_K GEMV.
 
void ck_test_gemv_q8_0 (const void *weight_q8_0, const float *input_f32, float *output, int rows, int cols)
 Q8_0 GEMV - matrix-vector multiply with Q8_0 weights.
 
void ck_test_gemv_q8_0_q8_0 (const void *weight_q8_0, const float *input_f32, float *output, int rows, int cols)
 Q8_0 x Q8_0 quantized GEMV - matches llama.cpp's approach.
 
void ck_test_outproj_mlp_fused_q5_0 (const float *attn_out, const float *residual, const float *ln2_gamma, const void *wo, const void *w1, const void *w2, float *output, int tokens, int num_heads, int head_dim, int embed_dim, int intermediate, float eps, int w2_is_q6k)
 Test mega-fused OutProj + MLP kernel (Q5_0 weights)
 
void ck_test_quantize_q8_k (const float *src, void *dst, int n)
 Quantize FP32 to Q8_K (for activations)
 
void ck_test_recurrent_conv_state_update (const float *state_in, const float *q, const float *k, const float *v, float *conv_x, float *state_out, int history_len, int num_seqs, int num_tokens, int q_dim, int k_dim, int v_dim)
 Build the recurrent convolution input history window.
 
void ck_test_recurrent_dt_gate (const float *alpha, const float *dt_bias, const float *a, float *gate, int rows, int dim)
 Transform recurrent alpha rows into the DeltaNet gate.
 
void ck_test_recurrent_norm_gate (const float *x, const float *gate, const float *weight, float *out, int rows, int num_heads, int head_dim, float eps)
 Per-head RMSNorm followed by SiLU(z) gating for recurrent outputs.
 
void ck_test_recurrent_qk_l2_norm (float *q, float *k, int rows, int q_dim, int k_dim, int head_dim, float eps)
 Apply per-head L2 normalization to recurrent Q/K rows in-place.
 
void ck_test_recurrent_silu (const float *x, float *out, int rows, int dim)
 Apply SiLU elementwise to recurrent rows.
 
void ck_test_recurrent_split_conv_qkv (const float *packed_qkv, float *q, float *k, float *v, int rows, int q_dim, int k_dim, int v_dim)
 Split the post-convolution recurrent packed QKV rows.
 
void ck_test_recurrent_split_qkv (const float *packed_qkv, float *q, float *k, float *v, int rows, int q_dim, int k_dim, int v_dim)
 Split a packed recurrent QKV matrix into explicit Q, K, and V outputs.
 
void ck_test_rmsnorm (const float *input, const float *weight, float *output, int n_tokens, int dim, float eps)
 RMSNorm.
 
void ck_test_rope (float *q, float *k, int n_tokens, int n_heads, int n_heads_kv, int head_dim, int pos_offset, float theta)
 RoPE (Rotary Position Embedding)
 
void ck_test_rope_interleaved (float *q, float *k, int n_tokens, int n_heads, int n_heads_kv, int head_dim, int pos_offset, float theta)
 RoPE with interleaved format (for llama.cpp compatibility)
 
void ck_test_softmax (const float *input, float *output, int n)
 Softmax (simple, non-causal)
 
void ck_test_split_q_gate (const float *packed_qg, float *q, float *gate, int rows, int q_dim, int gate_dim, int group_dim)
 Split a packed full-attention Q+gate matrix into Q rows and gate rows.
 
void ck_test_ssm_conv1d (const float *conv_x, const float *kernel, float *out, int kernel_size, int num_channels, int num_tokens, int num_seqs)
 qwen3next/Qwen3.5 SSM causal depthwise convolution.
 
void ck_test_swiglu (const float *gate_up, float *output, int n_tokens, int intermediate_dim)
 SwiGLU activation.
 
void ck_test_vec_dot_q4_k_q8_k (const void *weight_q4_k, const void *input_q8_k, float *output, int cols)
 
void ck_test_vec_dot_q5_0_q8_0 (const void *weight_q5_0, const void *input_q8_0, float *output, int cols)
 Direct Q5_0 x Q8_0 dot product (takes pre-quantized Q8_0 input)
 
void ck_test_vec_dot_q6_k_q8_k (const void *weight_q6_k, const void *input_q8_k, float *output, int cols)
 
void ck_test_vec_dot_q8_0_q8_0 (const void *weight_q8_0, const void *input_q8_0, float *output, int cols)
 Direct Q8_0 x Q8_0 dot product (takes pre-quantized Q8_0 input)
 

Detailed Description

C-Kernel-Engine Parity Testing API.

Exposes individual CK kernels for parity testing against llama.cpp/ggml. This API mirrors the test-kernel-parity.cpp interface in llama.cpp.

Usage:

  1. Build as shared library: libck_parity.so
  2. Load from Python using ctypes
  3. Call functions with matching signatures to test-kernel-parity.cpp

Definition in file ck_parity_api.h.

Macro Definition Documentation

◆ CK_BLOCK_Q4_0_SIZE

#define CK_BLOCK_Q4_0_SIZE   18

Definition at line 36 of file ck_parity_api.h.

◆ CK_BLOCK_Q4_K_SIZE

#define CK_BLOCK_Q4_K_SIZE   144

Definition at line 33 of file ck_parity_api.h.

◆ CK_BLOCK_Q6_K_SIZE

#define CK_BLOCK_Q6_K_SIZE   210

Definition at line 34 of file ck_parity_api.h.

◆ CK_BLOCK_Q8_K_SIZE

#define CK_BLOCK_Q8_K_SIZE   292

Definition at line 35 of file ck_parity_api.h.

◆ CK_QK4_0

#define CK_QK4_0   32 /* Elements per Q4_0 block */

Definition at line 29 of file ck_parity_api.h.

◆ CK_QK8_0

#define CK_QK8_0   32 /* Elements per Q8_0 block */

Definition at line 30 of file ck_parity_api.h.

◆ CK_QK_K

#define CK_QK_K   256 /* Elements per K-quant super-block */

Definition at line 28 of file ck_parity_api.h.

Function Documentation

◆ ck_get_block_q4_k_size()

int ck_get_block_q4_k_size ( void  )

Get Q4_K block size in bytes.

Definition at line 1026 of file ck_parity_api.c.

1027{
1028 return sizeof(block_q4_K);
1029}

◆ ck_get_block_q6_k_size()

int ck_get_block_q6_k_size ( void  )

Get Q6_K block size in bytes.

Definition at line 1031 of file ck_parity_api.c.

1032{
1033 return sizeof(block_q6_K);
1034}

◆ ck_get_block_q8_k_size()

int ck_get_block_q8_k_size ( void  )

Get Q8_K block size in bytes.

Definition at line 1036 of file ck_parity_api.c.

1037{
1038 return sizeof(block_q8_K);
1039}

◆ ck_get_qk_k()

int ck_get_qk_k ( void  )

Get QK_K (elements per super-block)

Definition at line 1041 of file ck_parity_api.c.

1042{
1043 return QK_K;
1044}
#define QK_K

References QK_K.

◆ ck_test_attention_causal()

void ck_test_attention_causal ( const float *  q,
const float *  k,
const float *  v,
float *  out,
int  num_heads,
int  num_kv_heads,
int  tokens,
int  seq_len,
int  head_dim 
)

Multi-head causal attention for prefill (head-major layout)

Layout (head-major, matches llama.cpp test): Q: [num_heads, tokens, head_dim] K: [num_kv_heads, seq_len, head_dim] V: [num_kv_heads, seq_len, head_dim] out: [num_heads, tokens, head_dim]

Supports GQA (grouped-query attention) where num_heads > num_kv_heads. Causal masking: token t can only attend to positions 0..t (inclusive).

Parameters
qQuery [num_heads, tokens, head_dim]
kKey [num_kv_heads, seq_len, head_dim]
vValue [num_kv_heads, seq_len, head_dim]
outOutput [num_heads, tokens, head_dim]
num_headsNumber of query heads
num_kv_headsNumber of key/value heads (for GQA)
tokensNumber of query tokens
seq_lenKey/value sequence length (for prefill: seq_len == tokens)
head_dimDimension per head

Definition at line 884 of file ck_parity_api.c.

893{
894 /* For prefill, seq_len == tokens, and kv_stride == tokens.
895 * The CK kernel expects strided KV layout with kv_stride_tokens parameter.
896 * For parity testing with contiguous tensors, kv_stride = seq_len.
897 */
899 q, k, v, out,
900 num_heads, num_kv_heads, tokens,
901 head_dim, head_dim, /* aligned_head_dim = head_dim for testing */
902 seq_len /* kv_stride_tokens = seq_len for contiguous KV */
903 );
904}
void attention_forward_causal_head_major_gqa_flash_strided(const float *q, const float *k, const float *v, float *output, int num_heads, int num_kv_heads, int num_tokens, int head_dim, int aligned_head_dim, int kv_stride_tokens)

References attention_forward_causal_head_major_gqa_flash_strided().

◆ ck_test_attn_gate_sigmoid_mul()

void ck_test_attn_gate_sigmoid_mul ( const float *  x,
const float *  gate,
float *  out,
int  rows,
int  dim 
)

Multiply attention output rows by sigmoid(gate) elementwise.

Layout: x : [rows, dim] gate : [rows, dim] out : [rows, dim]

Definition at line 859 of file ck_parity_api.c.

864{
865 attn_gate_sigmoid_mul_forward(x, gate, out, rows, 1, dim);
866}
void attn_gate_sigmoid_mul_forward(const float *x, const float *gate, float *out, int rows, int num_heads, int state_dim)

References attn_gate_sigmoid_mul_forward().

◆ ck_test_dequant_q4_0()

void ck_test_dequant_q4_0 ( const void *  src,
float *  dst,
int  n 
)

Dequantize Q4_0 data to FP32.

Definition at line 202 of file ck_parity_api.c.

203{
204 dequant_q4_0_row(src, dst, (size_t)n);
205}
void dequant_q4_0_row(const void *src, float *dst, size_t n_elements)
Dequantize Q4_0 row (multiple blocks)

References dequant_q4_0_row().

◆ ck_test_dequant_q4_k()

void ck_test_dequant_q4_k ( const void *  src,
float *  dst,
int  n 
)

Dequantize Q4_K data to FP32.

Parameters
srcInput Q4_K blocks
dstOutput FP32 values
nNumber of elements (must be multiple of 256)

Definition at line 192 of file ck_parity_api.c.

193{
194 dequant_q4_k_row(src, dst, (size_t)n);
195}
void dequant_q4_k_row(const void *src, float *dst, size_t n_elements)
Dequantize Q4_K row (multiple blocks)

References dequant_q4_k_row().

◆ ck_test_dequant_q6_k()

void ck_test_dequant_q6_k ( const void *  src,
float *  dst,
int  n 
)

Dequantize Q6_K data to FP32.

Definition at line 197 of file ck_parity_api.c.

198{
199 dequant_q6_k_row(src, dst, (size_t)n);
200}
void dequant_q6_k_row(const void *src, float *dst, size_t n_elements)
Dequantize Q6_K row (multiple blocks)

References dequant_q6_k_row().

◆ ck_test_gated_deltanet_autoregressive()

void ck_test_gated_deltanet_autoregressive ( const float *  q,
const float *  k,
const float *  v,
const float *  g,
const float *  beta,
const float *  state_in,
float *  state_out,
float *  out,
int  num_heads,
int  state_dim,
float  norm_eps 
)

Gated DeltaNet autoregressive update.

Layout: q, k, v [num_heads, state_dim] g, beta [num_heads] state_* [num_heads, state_dim, state_dim] row-major per head out [num_heads, state_dim]

This mirrors the single-token recurrent update used by qwen3next in llama.cpp after projections/convolution but before output projection.

Definition at line 741 of file ck_parity_api.c.

752{
754 q,
755 k,
756 v,
757 g,
758 beta,
759 state_in,
760 state_out,
761 out,
762 num_heads,
763 state_dim,
764 norm_eps);
765}
void gated_deltanet_autoregressive_forward(const float *q, const float *k, const float *v, const float *g, const float *beta, const float *state_in, float *state_out, float *out, int num_heads, int state_dim, float norm_eps)

References gated_deltanet_autoregressive_forward().

◆ ck_test_gemm_q4_k()

void ck_test_gemm_q4_k ( const void *  weight_q4k,
const float *  input_f32,
float *  output,
int  rows,
int  cols,
int  n_tokens 
)

Q4_K GEMM - batched matrix multiply with quantized weights.

Computes: output[t,r] = sum_k(weight[r,k] * input[t,k])

Parameters
weight_q4kQ4_K quantized weights [rows, cols]
input_f32FP32 input [n_tokens, cols]
outputFP32 output [n_tokens, rows]
rowsNumber of output rows
colsNumber of columns (must be multiple of 256)
n_tokensBatch size

Definition at line 434 of file ck_parity_api.c.

438{
439 /* Allocate Q8_K buffer for quantized activations */
440 int n_blocks_per_row = cols / CK_QK_K;
441 block_q8_K *q8_data = (block_q8_K *)malloc(n_tokens * n_blocks_per_row * sizeof(block_q8_K));
442 if (!q8_data) {
443 memset(output, 0, n_tokens * rows * sizeof(float));
444 return;
445 }
446
447 /* Quantize all input tokens */
448 for (int t = 0; t < n_tokens; t++) {
449 quantize_row_q8_k(input_f32 + t * cols,
450 q8_data + t * n_blocks_per_row, cols);
451 }
452
453 /* Use gemm_nt_q4_k_q8_k: C[M,N] = A[M,K] * B[N,K]^T
454 * Our layout: output[n_tokens, rows] = input[n_tokens, cols] * weight[rows, cols]^T
455 * So: M = n_tokens, N = rows, K = cols
456 */
457 gemm_nt_q4_k_q8_k(q8_data, weight_q4k, NULL, output, n_tokens, rows, cols);
458
459 free(q8_data);
460}
void quantize_row_q8_k(const float *x, void *vy, int k)
void gemm_nt_q4_k_q8_k(const void *A_q8, const void *B, const float *bias, float *C, int M, int N, int K)
#define CK_QK_K

References CK_QK_K, gemm_nt_q4_k_q8_k(), and quantize_row_q8_k().

◆ ck_test_gemm_q5_0()

void ck_test_gemm_q5_0 ( const void *  weight_q5_0,
const float *  input_f32,
float *  output,
int  rows,
int  cols,
int  n_tokens 
)

Q5_0 GEMM - batched matrix multiply with Q5_0 weights (32-element blocks)

Computes: output[t,r] = sum_k(weight[r,k] * input[t,k])

Parameters
weight_q5_0Q5_0 quantized weights [rows, cols]
input_f32FP32 input [n_tokens, cols]
outputFP32 output [n_tokens, rows]
rowsNumber of output rows
colsNumber of columns (must be multiple of 32)
n_tokensBatch size

Q5_0 GEMM - batched matrix multiply with Q5_0 weights (32-element blocks)

Used for MLP W1 (gate/up projection) and attention Q/K with Q5_0 weights.

Definition at line 533 of file ck_parity_api.c.

537{
538 /* Allocate Q8_0 buffer for quantized activations */
539 int n_blocks_per_row = cols / CK_QK8_0;
540 block_q8_0 *q8_data = (block_q8_0 *)malloc(n_tokens * n_blocks_per_row * sizeof(block_q8_0));
541 if (!q8_data) {
542 memset(output, 0, n_tokens * rows * sizeof(float));
543 return;
544 }
545
546 /* Quantize all input tokens */
547 for (int t = 0; t < n_tokens; t++) {
548 quantize_row_q8_0(input_f32 + t * cols,
549 q8_data + t * n_blocks_per_row, cols);
550 }
551
552 /* Use gemm_nt_q5_0_q8_0: C[M,N] = A[M,K] * B[N,K]^T
553 * Our layout: output[n_tokens, rows] = input[n_tokens, cols] * weight[rows, cols]^T
554 * So: M = n_tokens, N = rows, K = cols
555 */
556 gemm_nt_q5_0_q8_0(q8_data, weight_q5_0, NULL, output, n_tokens, rows, cols);
557
558 free(q8_data);
559}
void gemm_nt_q5_0_q8_0(const void *A_q8, const void *B_q5, const float *bias, float *C, int M, int N, int K)
Batch GEMM with Q5_0 weights and Q8_0 activations for prefill.
void quantize_row_q8_0(const float *x, void *vy, int k)
Quantize FP32 to Q8_0 format (scalar reference)
#define CK_QK8_0

References CK_QK8_0, gemm_nt_q5_0_q8_0(), and quantize_row_q8_0().

◆ ck_test_gemm_q6_k()

void ck_test_gemm_q6_k ( const void *  weight_q6k,
const float *  input_f32,
float *  output,
int  rows,
int  cols,
int  n_tokens 
)

Q6_K GEMM - batched matrix multiply with Q6_K weights.

Computes: output[t,r] = sum_k(weight[r,k] * input[t,k])

Parameters
weight_q6kQ6_K quantized weights [rows, cols]
input_f32FP32 input [n_tokens, cols]
outputFP32 output [n_tokens, rows]
rowsNumber of output rows
colsNumber of columns (must be multiple of 256)
n_tokensBatch size

Q6_K GEMM - batched matrix multiply with Q6_K weights.

Used for MLP W2 (down projection) with Q6_K weights.

Definition at line 467 of file ck_parity_api.c.

471{
472 /* Allocate Q8_K buffer for quantized activations */
473 int n_blocks_per_row = cols / CK_QK_K;
474 block_q8_K *q8_data = (block_q8_K *)malloc(n_tokens * n_blocks_per_row * sizeof(block_q8_K));
475 if (!q8_data) {
476 memset(output, 0, n_tokens * rows * sizeof(float));
477 return;
478 }
479
480 /* Quantize all input tokens */
481 for (int t = 0; t < n_tokens; t++) {
482 quantize_row_q8_k(input_f32 + t * cols,
483 q8_data + t * n_blocks_per_row, cols);
484 }
485
486 /* Use gemm_nt_q6_k_q8_k: C[M,N] = A[M,K] * B[N,K]^T
487 * Our layout: output[n_tokens, rows] = input[n_tokens, cols] * weight[rows, cols]^T
488 * So: M = n_tokens, N = rows, K = cols
489 */
490 gemm_nt_q6_k_q8_k(q8_data, weight_q6k, NULL, output, n_tokens, rows, cols);
491
492 free(q8_data);
493}
void gemm_nt_q6_k_q8_k(const void *A_q8, const void *B, const float *bias, float *C, int M, int N, int K)
NT GEMM: C = A @ B^T where A is Q8_K and B is Q6_K.

References CK_QK_K, gemm_nt_q6_k_q8_k(), and quantize_row_q8_k().

◆ ck_test_gemm_q8_0()

void ck_test_gemm_q8_0 ( const void *  weight_q8_0,
const float *  input_f32,
float *  output,
int  rows,
int  cols,
int  n_tokens 
)

Q8_0 GEMM - batched matrix multiply with Q8_0 weights (32-element blocks)

Computes: output[t,r] = sum_k(weight[r,k] * input[t,k])

Parameters
weight_q8_0Q8_0 quantized weights [rows, cols]
input_f32FP32 input [n_tokens, cols]
outputFP32 output [n_tokens, rows]
rowsNumber of output rows
colsNumber of columns (must be multiple of 32)
n_tokensBatch size

Q8_0 GEMM - batched matrix multiply with Q8_0 weights (32-element blocks)

Used for attention V projection with Q8_0 weights.

Definition at line 500 of file ck_parity_api.c.

504{
505 /* Allocate Q8_0 buffer for quantized activations */
506 int n_blocks_per_row = cols / CK_QK8_0;
507 block_q8_0 *q8_data = (block_q8_0 *)malloc(n_tokens * n_blocks_per_row * sizeof(block_q8_0));
508 if (!q8_data) {
509 memset(output, 0, n_tokens * rows * sizeof(float));
510 return;
511 }
512
513 /* Quantize all input tokens */
514 for (int t = 0; t < n_tokens; t++) {
515 quantize_row_q8_0(input_f32 + t * cols,
516 q8_data + t * n_blocks_per_row, cols);
517 }
518
519 /* Use gemm_nt_q8_0_q8_0: C[M,N] = A[M,K] * B[N,K]^T
520 * Our layout: output[n_tokens, rows] = input[n_tokens, cols] * weight[rows, cols]^T
521 * So: M = n_tokens, N = rows, K = cols
522 */
523 gemm_nt_q8_0_q8_0(q8_data, weight_q8_0, NULL, output, n_tokens, rows, cols);
524
525 free(q8_data);
526}
void gemm_nt_q8_0_q8_0(const void *A_q8, const void *B_q8, const float *bias, float *C, int M, int N, int K)
gemm_nt_q8_0_q8_0 with optional bias (matches header signature)

References CK_QK8_0, gemm_nt_q8_0_q8_0(), and quantize_row_q8_0().

◆ ck_test_gemv_q4_k()

void ck_test_gemv_q4_k ( const void *  weight_q4k,
const float *  input_f32,
float *  output,
int  cols 
)

Q4_K GEMV - dot product of quantized weights and FP32 input.

Internally quantizes input to Q8_K, then computes dot product.

Parameters
weight_q4kQ4_K quantized weights [cols]
input_f32FP32 input vector [cols]
outputOutput scalar [1]
colsNumber of columns (must be multiple of 256)

Definition at line 220 of file ck_parity_api.c.

224{
225 /* Allocate Q8_K buffer for quantized activations */
226 int n_blocks = cols / CK_QK_K;
227 block_q8_K *q8_data = (block_q8_K *)malloc(n_blocks * sizeof(block_q8_K));
228 if (!q8_data) {
229 *output = 0.0f;
230 return;
231 }
232
233 /* Quantize input to Q8_K */
234 quantize_row_q8_k(input_f32, q8_data, cols);
235
236 /* Compute dot product using GEMV with M=1 */
237 gemv_q4_k_q8_k(output, weight_q4k, q8_data, 1, cols);
238
239 free(q8_data);
240}
void gemv_q4_k_q8_k(float *y, const void *W, const void *x_q8, int M, int K)

References CK_QK_K, gemv_q4_k_q8_k(), and quantize_row_q8_k().

◆ ck_test_gemv_q5_0()

void ck_test_gemv_q5_0 ( const void *  weight_q5_0,
const float *  input_f32,
float *  output,
int  rows,
int  cols 
)

Q5_0 GEMV - matrix-vector multiply with Q5_0 weights.

Parameters
weight_q5_0Q5_0 quantized weights [rows * cols]
input_f32FP32 input vector [cols]
outputFP32 output vector [rows]
rowsNumber of output rows
colsNumber of columns (must be multiple of 32)

Definition at line 263 of file ck_parity_api.c.

267{
268 /* Match llama.cpp's test_gemv_q5_0:
269 * 1. Quantize input to Q8_0 format
270 * 2. Use quantized dot product (vec_dot_q5_0_q8_0)
271 *
272 * This ensures parity with llama.cpp which always uses the
273 * quantized path, NOT the FP32 dequantization path.
274 */
275 int n_blocks = cols / CK_QK8_0;
276 block_q8_0 *q8_data = (block_q8_0 *)malloc(n_blocks * sizeof(block_q8_0));
277 if (!q8_data) {
278 for (int r = 0; r < rows; r++) output[r] = 0.0f;
279 return;
280 }
281
282 /* Quantize input to Q8_0 */
283 quantize_row_q8_0(input_f32, q8_data, cols);
284
285 /* Call the quantized GEMV kernel (same as ck_test_gemv_q5_0_q8_0) */
286 gemv_q5_0_q8_0(output, weight_q5_0, q8_data, rows, cols);
287
288 free(q8_data);
289}
void gemv_q5_0_q8_0(float *y, const void *W, const void *x_q8, int M, int K)
Matrix-vector multiply with Q5_0 weights and Q8_0 input.

References CK_QK8_0, gemv_q5_0_q8_0(), and quantize_row_q8_0().

◆ ck_test_gemv_q5_0_q8_0()

void ck_test_gemv_q5_0_q8_0 ( const void *  weight_q5_0,
const float *  input_f32,
float *  output,
int  rows,
int  cols 
)

Q5_0 x Q8_0 quantized GEMV - matches llama.cpp's approach.

This version quantizes the input to Q8_0 first, then uses integer dot products (like llama.cpp does). Use this for parity testing.

Parameters
weight_q5_0Q5_0 quantized weights [rows * cols]
input_f32FP32 input vector [cols] - will be quantized to Q8_0
outputFP32 output vector [rows]
rowsNumber of output rows
colsNumber of columns (must be multiple of 32)

Definition at line 319 of file ck_parity_api.c.

323{
324 /* This matches llama.cpp's approach:
325 * 1. Quantize input to Q8_0 format
326 * 2. Use quantized dot product (integer math)
327 * 3. Scale at the end
328 */
329 int n_blocks = cols / CK_QK8_0;
330 block_q8_0 *q8_data = (block_q8_0 *)malloc(n_blocks * sizeof(block_q8_0));
331 if (!q8_data) {
332 for (int r = 0; r < rows; r++) output[r] = 0.0f;
333 return;
334 }
335
336 /* Quantize input to Q8_0 */
337 quantize_row_q8_0(input_f32, q8_data, cols);
338
339 /* Call the quantized GEMV kernel */
340 gemv_q5_0_q8_0(output, weight_q5_0, q8_data, rows, cols);
341
342 free(q8_data);
343}

References CK_QK8_0, gemv_q5_0_q8_0(), and quantize_row_q8_0().

◆ ck_test_gemv_q6_k()

void ck_test_gemv_q6_k ( const void *  weight_q6k,
const float *  input_f32,
float *  output,
int  cols 
)

Q6_K GEMV.

Definition at line 242 of file ck_parity_api.c.

246{
247 /* Match runtime decode path:
248 * 1) quantize FP32 activation to Q8_K
249 * 2) run Q6_K x Q8_K GEMV kernel with M=1
250 */
251 int n_blocks = cols / CK_QK_K;
252 block_q8_K *q8_data = (block_q8_K *)malloc(n_blocks * sizeof(block_q8_K));
253 if (!q8_data) {
254 *output = 0.0f;
255 return;
256 }
257
258 quantize_row_q8_k(input_f32, q8_data, cols);
259 gemv_q6_k_q8_k(output, weight_q6k, q8_data, 1, cols);
260 free(q8_data);
261}
void gemv_q6_k_q8_k(float *y, const void *W, const void *x_q8, int M, int K)
GEMV: y = W @ x where W is Q6_K and x is Q8_K.

References CK_QK_K, gemv_q6_k_q8_k(), and quantize_row_q8_k().

◆ ck_test_gemv_q8_0()

void ck_test_gemv_q8_0 ( const void *  weight_q8_0,
const float *  input_f32,
float *  output,
int  rows,
int  cols 
)

Q8_0 GEMV - matrix-vector multiply with Q8_0 weights.

Parameters
weight_q8_0Q8_0 quantized weights [rows * cols]
input_f32FP32 input vector [cols]
outputFP32 output vector [rows]
rowsNumber of output rows
colsNumber of columns (must be multiple of 32)

Definition at line 291 of file ck_parity_api.c.

295{
296 /* Match llama.cpp's test_gemv_q8_0:
297 * 1. Quantize input to Q8_0 format
298 * 2. Use quantized dot product (vec_dot_q8_0_q8_0)
299 *
300 * This ensures parity with llama.cpp which always uses the
301 * quantized path, NOT the FP32 dequantization path.
302 */
303 int n_blocks = cols / CK_QK8_0;
304 block_q8_0 *q8_data = (block_q8_0 *)malloc(n_blocks * sizeof(block_q8_0));
305 if (!q8_data) {
306 for (int r = 0; r < rows; r++) output[r] = 0.0f;
307 return;
308 }
309
310 /* Quantize input to Q8_0 */
311 quantize_row_q8_0(input_f32, q8_data, cols);
312
313 /* Call the quantized GEMV kernel (same as ck_test_gemv_q8_0_q8_0) */
314 gemv_q8_0_q8_0(output, weight_q8_0, q8_data, rows, cols);
315
316 free(q8_data);
317}
void gemv_q8_0_q8_0(float *y, const void *W, const void *x_q8, int M, int K)
Matrix-vector multiply with Q8_0 weights and Q8_0 input.

References CK_QK8_0, gemv_q8_0_q8_0(), and quantize_row_q8_0().

◆ ck_test_gemv_q8_0_q8_0()

void ck_test_gemv_q8_0_q8_0 ( const void *  weight_q8_0,
const float *  input_f32,
float *  output,
int  rows,
int  cols 
)

Q8_0 x Q8_0 quantized GEMV - matches llama.cpp's approach.

This version quantizes the input to Q8_0 first, then uses integer dot products (like llama.cpp does). Use this for parity testing.

Parameters
weight_q8_0Q8_0 quantized weights [rows * cols]
input_f32FP32 input vector [cols] - will be quantized to Q8_0
outputFP32 output vector [rows]
rowsNumber of output rows
colsNumber of columns (must be multiple of 32)

Definition at line 345 of file ck_parity_api.c.

349{
350 /* This matches llama.cpp's approach:
351 * 1. Quantize input to Q8_0 format
352 * 2. Use quantized dot product (integer math)
353 * 3. Scale at the end
354 */
355 int n_blocks = cols / CK_QK8_0;
356 block_q8_0 *q8_data = (block_q8_0 *)malloc(n_blocks * sizeof(block_q8_0));
357 if (!q8_data) {
358 for (int r = 0; r < rows; r++) output[r] = 0.0f;
359 return;
360 }
361
362 /* Quantize input to Q8_0 */
363 quantize_row_q8_0(input_f32, q8_data, cols);
364
365 /* Call the quantized GEMV kernel */
366 gemv_q8_0_q8_0(output, weight_q8_0, q8_data, rows, cols);
367
368 free(q8_data);
369}

References CK_QK8_0, gemv_q8_0_q8_0(), and quantize_row_q8_0().

◆ ck_test_outproj_mlp_fused_q5_0()

void ck_test_outproj_mlp_fused_q5_0 ( const float *  attn_out,
const float *  residual,
const float *  ln2_gamma,
const void *  wo,
const void *  w1,
const void *  w2,
float *  output,
int  tokens,
int  num_heads,
int  head_dim,
int  embed_dim,
int  intermediate,
float  eps,
int  w2_is_q6k 
)

Test mega-fused OutProj + MLP kernel (Q5_0 weights)

This tests the mega_fused_outproj_mlp_prefill kernel which fuses:

  1. Quantize attention output (head-major) to Q8_0
  2. OutProj: attn_out @ W_o (Q5_0) → h1
  3. Residual: h1 += residual
  4. RMSNorm: h1 → ln2_out
  5. MLP: silu(ln2_out @ W_gate) * (ln2_out @ W_up) @ W2
  6. Residual: output += h1
Parameters
attn_outAttention output [num_heads, tokens, head_dim] (FP32, head-major)
residualResidual input [tokens, embed_dim] (FP32)
ln2_gammaRMSNorm gamma [embed_dim] (FP32)
woOutProj weights [embed_dim, embed_dim] (Q5_0)
w1MLP W1 weights [2*intermediate, embed_dim] (Q5_0)
w2MLP W2 weights [embed_dim, intermediate] (Q4_K or Q6_K)
outputOutput [tokens, embed_dim] (FP32)
tokensNumber of tokens
num_headsNumber of attention heads
head_dimDimension per head
embed_dimEmbedding dimension (= num_heads * head_dim)
intermediateMLP intermediate dimension
epsRMSNorm epsilon
w2_is_q6kIf true, W2 is Q6_K; if false, W2 is Q4_K

This is a simplified wrapper for parity testing that:

  • Uses Q5_0 for W_o and W1 weights
  • Uses Q4_K for W2 weights
  • Allocates scratch internally
Parameters
attn_outAttention output [num_heads, tokens, head_dim] (FP32, head-major)
residualResidual input [tokens, embed_dim] (FP32)
ln2_gammaRMSNorm gamma [embed_dim] (FP32)
woOutProj weights [embed_dim, embed_dim] (Q5_0)
w1MLP W1 weights [2*intermediate, embed_dim] (Q5_0)
w2MLP W2 weights [embed_dim, intermediate] (Q4_K or Q6_K)
outputOutput [tokens, embed_dim] (FP32)
tokensNumber of tokens
num_headsNumber of attention heads
head_dimDimension per head
embed_dimEmbedding dimension (= num_heads * head_dim)
intermediateMLP intermediate dimension
epsRMSNorm epsilon
w2_is_q6kIf true, W2 is Q6_K; if false, W2 is Q4_K

Definition at line 959 of file ck_parity_api.c.

974{
975 /* CK uses dtype enum: CK_DT_Q5_0 = 11, CK_DT_Q4_K = 7, CK_DT_Q6_K = 8 */
976 const int CK_DT_Q5_0_VAL = 11;
977 const int CK_DT_Q4_K_VAL = 7;
978 const int CK_DT_Q6_K_VAL = 8;
979
980 /* For parity testing, aligned = actual (no padding) */
981 int aligned_embed_dim = embed_dim;
982 int aligned_head_dim = head_dim;
983 int aligned_intermediate = intermediate;
984
985 /* Ensure intermediate is multiple of 256 (QK_K) for K-quants */
986 if ((intermediate % 256) != 0) {
987 aligned_intermediate = ((intermediate + 255) / 256) * 256;
988 }
989
990 /* Allocate scratch */
992 tokens, aligned_embed_dim, num_heads, aligned_head_dim, aligned_intermediate);
993
994 void *scratch = malloc(scratch_size);
995 if (!scratch) {
996 return;
997 }
998
999 /* Call the mega-fused kernel */
1001 output,
1002 attn_out,
1003 residual,
1004 ln2_gamma,
1005 wo, NULL, CK_DT_Q5_0_VAL, /* W_o with Q5_0 */
1006 w1, NULL, CK_DT_Q5_0_VAL, /* W1 with Q5_0 */
1007 w2, NULL, w2_is_q6k ? CK_DT_Q6_K_VAL : CK_DT_Q4_K_VAL, /* W2 with Q4_K or Q6_K */
1008 tokens,
1009 embed_dim,
1010 aligned_embed_dim,
1011 num_heads,
1012 aligned_head_dim,
1013 intermediate,
1014 aligned_intermediate,
1015 eps,
1016 scratch
1017 );
1018
1019 free(scratch);
1020}
void mega_fused_outproj_mlp_prefill(float *output, const float *attn_out, const float *residual, const float *ln2_gamma, const void *wo, const float *bo, int wo_dt, const void *w1, const float *b1, int w1_dt, const void *w2, const float *b2, int w2_dt, int tokens, int embed_dim, int aligned_embed_dim, int num_heads, int aligned_head_dim, int intermediate_dim, int aligned_intermediate_dim, float eps, void *scratch)
size_t mega_fused_outproj_mlp_prefill_scratch_size(int tokens, int aligned_embed_dim, int num_heads, int aligned_head_dim, int aligned_intermediate_dim)
Get scratch buffer size for mega_fused_outproj_mlp_prefill.

References mega_fused_outproj_mlp_prefill(), and mega_fused_outproj_mlp_prefill_scratch_size().

◆ ck_test_quantize_q8_k()

void ck_test_quantize_q8_k ( const float *  src,
void *  dst,
int  n 
)

Quantize FP32 to Q8_K (for activations)

Parameters
srcInput FP32 values
dstOutput Q8_K blocks
nNumber of elements (must be multiple of 256)

Definition at line 211 of file ck_parity_api.c.

212{
213 quantize_row_q8_k(src, dst, n);
214}

References quantize_row_q8_k().

◆ ck_test_recurrent_conv_state_update()

void ck_test_recurrent_conv_state_update ( const float *  state_in,
const float *  q,
const float *  k,
const float *  v,
float *  conv_x,
float *  state_out,
int  history_len,
int  num_seqs,
int  num_tokens,
int  q_dim,
int  k_dim,
int  v_dim 
)

Build the recurrent convolution input history window.

Layout: state_in [num_seqs, channels, history_len] q [num_seqs * num_tokens, q_dim] k [num_seqs * num_tokens, k_dim] v [num_seqs * num_tokens, v_dim] conv_x [num_seqs, channels, history_len + num_tokens] state_out [num_seqs, channels, history_len]

Definition at line 811 of file ck_parity_api.c.

823{
825 state_in, q, k, v, conv_x, state_out, history_len, num_seqs, num_tokens, q_dim, k_dim, v_dim);
826}
void recurrent_conv_state_update_forward(const float *state_in, const float *q, const float *k, const float *v, float *conv_x, float *state_out, int history_len, int num_seqs, int num_tokens, int q_dim, int k_dim, int v_dim)

References recurrent_conv_state_update_forward().

◆ ck_test_recurrent_dt_gate()

void ck_test_recurrent_dt_gate ( const float *  alpha,
const float *  dt_bias,
const float *  a,
float *  gate,
int  rows,
int  dim 
)

Transform recurrent alpha rows into the DeltaNet gate.

Layout: alpha : [rows, dim] dt_bias : [dim] a : [dim] gate : [rows, dim]

Definition at line 801 of file ck_parity_api.c.

807{
808 recurrent_dt_gate_forward(alpha, dt_bias, a, gate, rows, 1, dim);
809}
void recurrent_dt_gate_forward(const float *alpha, const float *dt_bias, const float *a, float *gate, int rows, int num_heads, int state_dim)

References recurrent_dt_gate_forward().

◆ ck_test_recurrent_norm_gate()

void ck_test_recurrent_norm_gate ( const float *  x,
const float *  gate,
const float *  weight,
float *  out,
int  rows,
int  num_heads,
int  head_dim,
float  eps 
)

Per-head RMSNorm followed by SiLU(z) gating for recurrent outputs.

Definition at line 868 of file ck_parity_api.c.

876{
877 recurrent_norm_gate_forward(x, gate, weight, out, rows, num_heads, head_dim, eps);
878}
void recurrent_norm_gate_forward(const float *x, const float *gate, const float *weight, float *out, int rows, int num_heads, int head_dim, float eps)

References recurrent_norm_gate_forward().

◆ ck_test_recurrent_qk_l2_norm()

void ck_test_recurrent_qk_l2_norm ( float *  q,
float *  k,
int  rows,
int  q_dim,
int  k_dim,
int  head_dim,
float  eps 
)

Apply per-head L2 normalization to recurrent Q/K rows in-place.

Layout: q : [rows, q_dim] k : [rows, k_dim] where q_dim and k_dim are multiples of head_dim.

Definition at line 848 of file ck_parity_api.c.

855{
856 recurrent_qk_l2_norm_forward(q, k, rows, q_dim, k_dim, head_dim, eps);
857}
void recurrent_qk_l2_norm_forward(float *q, float *k, int rows, int q_dim, int k_dim, int head_dim, float eps)

References recurrent_qk_l2_norm_forward().

◆ ck_test_recurrent_silu()

void ck_test_recurrent_silu ( const float *  x,
float *  out,
int  rows,
int  dim 
)

Apply SiLU elementwise to recurrent rows.

Definition at line 828 of file ck_parity_api.c.

832{
833 recurrent_silu_forward(x, out, rows, dim);
834}
void recurrent_silu_forward(const float *x, float *out, int rows, int dim)

References recurrent_silu_forward().

◆ ck_test_recurrent_split_conv_qkv()

void ck_test_recurrent_split_conv_qkv ( const float *  packed_qkv,
float *  q,
float *  k,
float *  v,
int  rows,
int  q_dim,
int  k_dim,
int  v_dim 
)

Split the post-convolution recurrent packed QKV rows.

Definition at line 836 of file ck_parity_api.c.

844{
845 recurrent_split_conv_qkv_forward(packed_qkv, q, k, v, rows, q_dim, k_dim, v_dim);
846}
void recurrent_split_conv_qkv_forward(const float *packed_qkv, float *q, float *k, float *v, int rows, int q_dim, int k_dim, int v_dim)

References recurrent_split_conv_qkv_forward().

◆ ck_test_recurrent_split_qkv()

void ck_test_recurrent_split_qkv ( const float *  packed_qkv,
float *  q,
float *  k,
float *  v,
int  rows,
int  q_dim,
int  k_dim,
int  v_dim 
)

Split a packed recurrent QKV matrix into explicit Q, K, and V outputs.

Layout: packed_qkv : [rows, q_dim + k_dim + v_dim] q : [rows, q_dim] k : [rows, k_dim] v : [rows, v_dim]

Definition at line 789 of file ck_parity_api.c.

797{
798 recurrent_split_qkv_forward(packed_qkv, q, k, v, rows, q_dim, k_dim, v_dim);
799}
void recurrent_split_qkv_forward(const float *packed_qkv, float *q, float *k, float *v, int rows, int q_dim, int k_dim, int v_dim)

References recurrent_split_qkv_forward().

◆ ck_test_rmsnorm()

void ck_test_rmsnorm ( const float *  input,
const float *  weight,
float *  output,
int  n_tokens,
int  dim,
float  eps 
)

RMSNorm.

Computes: output = (input / rms(input)) * weight where rms(x) = sqrt(mean(x^2) + eps)

Parameters
inputInput tensor [n_tokens, dim]
weightNormalization weights [dim]
outputOutput tensor [n_tokens, dim]
n_tokensNumber of tokens
dimHidden dimension
epsEpsilon for numerical stability

Definition at line 565 of file ck_parity_api.c.

569{
570 /* CK rmsnorm_forward has aligned_embed_dim parameter
571 * For testing, use dim as aligned_embed_dim (no padding) */
572 rmsnorm_forward(input, weight, output, NULL, n_tokens, dim, dim, eps);
573}
void rmsnorm_forward(const float *input, const float *gamma, float *output, float *rstd_cache, int tokens, int d_model, int aligned_embed_dim, float eps)

References rmsnorm_forward().

◆ ck_test_rope()

void ck_test_rope ( float *  q,
float *  k,
int  n_tokens,
int  n_heads,
int  n_heads_kv,
int  head_dim,
int  pos_offset,
float  theta 
)

RoPE (Rotary Position Embedding)

Applies rotary position embeddings to Q and K tensors.

NOTE: CK uses rotate-half format (split first/second halves) while some implementations use interleaved format. The test harness should account for this.

Parameters
qQuery tensor [n_tokens, n_heads * head_dim], modified in-place
kKey tensor [n_tokens, n_heads_kv * head_dim], modified in-place
n_tokensNumber of tokens
n_headsNumber of query heads
n_heads_kvNumber of key/value heads
head_dimDimension per head
pos_offsetStarting position for RoPE
thetaRoPE base frequency (typically 10000.0)

Definition at line 575 of file ck_parity_api.c.

578{
579 /* Precompute cos/sin cache */
580 int half_dim = head_dim / 2;
581 int max_seq = pos_offset + n_tokens;
582
583 float *cos_cache = (float *)malloc(max_seq * half_dim * sizeof(float));
584 float *sin_cache = (float *)malloc(max_seq * half_dim * sizeof(float));
585 if (!cos_cache || !sin_cache) {
586 free(cos_cache);
587 free(sin_cache);
588 return;
589 }
590
591 rope_precompute_cache(cos_cache, sin_cache, max_seq, head_dim, theta,
592 head_dim, "none", 1.0f);
593
594 /* CK RoPE expects layout [num_heads, num_tokens, head_dim]
595 * Reshape from [n_tokens, n_heads * head_dim] to [n_heads, n_tokens, head_dim]
596 */
597 float *q_reorder = (float *)malloc(n_heads * n_tokens * head_dim * sizeof(float));
598 float *k_reorder = (float *)malloc(n_heads_kv * n_tokens * head_dim * sizeof(float));
599
600 if (q_reorder && k_reorder) {
601 /* Reorder Q: [T, H*D] -> [H, T, D] */
602 for (int t = 0; t < n_tokens; t++) {
603 for (int h = 0; h < n_heads; h++) {
604 for (int d = 0; d < head_dim; d++) {
605 q_reorder[h * n_tokens * head_dim + t * head_dim + d] =
606 q[t * n_heads * head_dim + h * head_dim + d];
607 }
608 }
609 }
610
611 /* Reorder K: [T, H_kv*D] -> [H_kv, T, D] */
612 for (int t = 0; t < n_tokens; t++) {
613 for (int h = 0; h < n_heads_kv; h++) {
614 for (int d = 0; d < head_dim; d++) {
615 k_reorder[h * n_tokens * head_dim + t * head_dim + d] =
616 k[t * n_heads_kv * head_dim + h * head_dim + d];
617 }
618 }
619 }
620
621 /* Apply RoPE */
622 rope_forward_qk(q_reorder, k_reorder,
623 cos_cache, sin_cache,
624 n_heads, n_heads_kv, n_tokens,
625 head_dim, head_dim, pos_offset);
626
627 /* Reorder back: [H, T, D] -> [T, H*D] */
628 for (int t = 0; t < n_tokens; t++) {
629 for (int h = 0; h < n_heads; h++) {
630 for (int d = 0; d < head_dim; d++) {
631 q[t * n_heads * head_dim + h * head_dim + d] =
632 q_reorder[h * n_tokens * head_dim + t * head_dim + d];
633 }
634 }
635 }
636
637 for (int t = 0; t < n_tokens; t++) {
638 for (int h = 0; h < n_heads_kv; h++) {
639 for (int d = 0; d < head_dim; d++) {
640 k[t * n_heads_kv * head_dim + h * head_dim + d] =
641 k_reorder[h * n_tokens * head_dim + t * head_dim + d];
642 }
643 }
644 }
645 }
646
647 free(q_reorder);
648 free(k_reorder);
649 free(cos_cache);
650 free(sin_cache);
651}
void rope_precompute_cache(float *cos_cache, float *sin_cache, int max_seq_len, int head_dim, float base, int rotary_dim, const char *scaling_type, float scaling_factor)
void rope_forward_qk(float *q, float *k, const float *cos_cache, const float *sin_cache, int num_heads, int num_kv_heads, int num_tokens, int head_dim, int aligned_head_dim, int pos_offset)

References rope_forward_qk(), and rope_precompute_cache().

◆ ck_test_rope_interleaved()

void ck_test_rope_interleaved ( float *  q,
float *  k,
int  n_tokens,
int  n_heads,
int  n_heads_kv,
int  head_dim,
int  pos_offset,
float  theta 
)

RoPE with interleaved format (for llama.cpp compatibility)

Uses interleaved format: (x0, x1) -> (x0*cos - x1*sin, x0*sin + x1*cos)

Definition at line 653 of file ck_parity_api.c.

656{
657 /* Interleaved RoPE format (matches llama.cpp):
658 * (x0, x1) -> (x0*cos - x1*sin, x0*sin + x1*cos)
659 * Applied to consecutive pairs of elements
660 */
661
662 /* Precompute inverse frequencies */
663 float *inv_freq = (float *)malloc((head_dim / 2) * sizeof(float));
664 if (!inv_freq) return;
665
666 for (int i = 0; i < head_dim / 2; i++) {
667 inv_freq[i] = 1.0f / powf(theta, (float)(2 * i) / head_dim);
668 }
669
670 /* Apply RoPE to Q */
671 for (int t = 0; t < n_tokens; t++) {
672 int pos = pos_offset + t;
673 for (int h = 0; h < n_heads; h++) {
674 float *qh = q + t * n_heads * head_dim + h * head_dim;
675
676 for (int i = 0; i < head_dim / 2; i++) {
677 float freq = pos * inv_freq[i];
678 float cos_val = cosf(freq);
679 float sin_val = sinf(freq);
680
681 /* Interleaved format */
682 float x0 = qh[i * 2];
683 float x1 = qh[i * 2 + 1];
684 qh[i * 2] = x0 * cos_val - x1 * sin_val;
685 qh[i * 2 + 1] = x0 * sin_val + x1 * cos_val;
686 }
687 }
688 }
689
690 /* Apply RoPE to K */
691 for (int t = 0; t < n_tokens; t++) {
692 int pos = pos_offset + t;
693 for (int h = 0; h < n_heads_kv; h++) {
694 float *kh = k + t * n_heads_kv * head_dim + h * head_dim;
695
696 for (int i = 0; i < head_dim / 2; i++) {
697 float freq = pos * inv_freq[i];
698 float cos_val = cosf(freq);
699 float sin_val = sinf(freq);
700
701 float x0 = kh[i * 2];
702 float x1 = kh[i * 2 + 1];
703 kh[i * 2] = x0 * cos_val - x1 * sin_val;
704 kh[i * 2 + 1] = x0 * sin_val + x1 * cos_val;
705 }
706 }
707 }
708
709 free(inv_freq);
710}

◆ ck_test_softmax()

void ck_test_softmax ( const float *  input,
float *  output,
int  n 
)

Softmax (simple, non-causal)

Computes: output[i] = exp(input[i]) / sum(exp(input))

Parameters
inputInput tensor [n]
outputOutput tensor [n]
nNumber of elements

Definition at line 719 of file ck_parity_api.c.

720{
721 /* Find max for numerical stability */
722 float max_val = input[0];
723 for (int i = 1; i < n; i++) {
724 if (input[i] > max_val) max_val = input[i];
725 }
726
727 /* Compute exp and sum */
728 float sum = 0.0f;
729 for (int i = 0; i < n; i++) {
730 output[i] = expf(input[i] - max_val);
731 sum += output[i];
732 }
733
734 /* Normalize */
735 float inv_sum = 1.0f / sum;
736 for (int i = 0; i < n; i++) {
737 output[i] *= inv_sum;
738 }
739}

◆ ck_test_split_q_gate()

void ck_test_split_q_gate ( const float *  packed_qg,
float *  q,
float *  gate,
int  rows,
int  q_dim,
int  gate_dim,
int  group_dim 
)

Split a packed full-attention Q+gate matrix into Q rows and gate rows.

Layout: packed_qg : [rows, q_dim + gate_dim] q : [rows, q_dim] gate : [rows, gate_dim]

Definition at line 778 of file ck_parity_api.c.

785{
786 split_q_gate_forward(packed_qg, q, gate, rows, q_dim, gate_dim, group_dim);
787}
void split_q_gate_forward(const float *packed_qg, float *q, float *gate, int rows, int q_dim, int gate_dim, int group_dim)

References split_q_gate_forward().

◆ ck_test_ssm_conv1d()

void ck_test_ssm_conv1d ( const float *  conv_x,
const float *  kernel,
float *  out,
int  kernel_size,
int  num_channels,
int  num_tokens,
int  num_seqs 
)

qwen3next/Qwen3.5 SSM causal depthwise convolution.

Layout: conv_x [num_seqs, num_channels, kernel_size - 1 + num_tokens] kernel [num_channels, kernel_size] out [num_seqs, num_tokens, num_channels]

This mirrors ggml's GGML_OP_SSM_CONV used immediately before the DeltaNet recurrent update in qwen3next/Qwen3.5.

Definition at line 767 of file ck_parity_api.c.

774{
775 ssm_conv1d_forward(conv_x, kernel, out, kernel_size, num_channels, num_tokens, num_seqs);
776}
void ssm_conv1d_forward(const float *conv_x, const float *kernel, float *out, int kernel_size, int num_channels, int num_tokens, int num_seqs)

References ssm_conv1d_forward().

◆ ck_test_swiglu()

void ck_test_swiglu ( const float *  gate_up,
float *  output,
int  n_tokens,
int  intermediate_dim 
)

SwiGLU activation.

Computes: output = SiLU(gate) * up where SiLU(x) = x * sigmoid(x)

Parameters
gate_upInput tensor [n_tokens, 2 * intermediate_dim] Layout: [gate_0..gate_D-1, up_0..up_D-1] per token
outputOutput tensor [n_tokens, intermediate_dim]
n_tokensNumber of tokens
intermediate_dimIntermediate dimension

Definition at line 712 of file ck_parity_api.c.

715{
716 swiglu_forward(gate_up, output, n_tokens, intermediate_dim);
717}
void swiglu_forward(const float *input, float *output, int tokens, int dim)

References swiglu_forward().

◆ ck_test_vec_dot_q4_k_q8_k()

void ck_test_vec_dot_q4_k_q8_k ( const void *  weight_q4_k,
const void *  input_q8_k,
float *  output,
int  cols 
)

Direct Q4_K x Q8_K dot product using identical pre-quantized bytes.

Definition at line 375 of file ck_parity_api.c.

379{
380 /* M=1 reaches the production Q4 dispatch without requantizing input. */
381 gemv_q4_k_q8_k(output, weight_q4_k, input_q8_k, 1, cols);
382}

References gemv_q4_k_q8_k().

◆ ck_test_vec_dot_q5_0_q8_0()

void ck_test_vec_dot_q5_0_q8_0 ( const void *  weight_q5_0,
const void *  input_q8_0,
float *  output,
int  cols 
)

Direct Q5_0 x Q8_0 dot product (takes pre-quantized Q8_0 input)

This is a "direct" test that bypasses FP32-to-Q8_0 conversion. Useful for isolating kernel bugs from quantization bugs.

Parameters
weight_q5_0Q5_0 quantized weights [cols]
input_q8_0Q8_0 quantized input [cols] (pre-quantized!)
outputOutput scalar [1]
colsNumber of elements (must be multiple of 32)

Direct Q5_0 x Q8_0 dot product (takes pre-quantized Q8_0 input)

This is a "direct" test that bypasses FP32-to-Q8_0 conversion. Useful for isolating kernel bugs from quantization bugs.

Parameters
weight_q5_0Q5_0 quantized weights [cols]
input_q8_0Q8_0 quantized input [cols] (pre-quantized!)
outputOutput scalar [1]
colsNumber of elements (must be multiple of 32)

Definition at line 406 of file ck_parity_api.c.

410{
411 vec_dot_q5_0_q8_0(cols, output, weight_q5_0, input_q8_0);
412}
void vec_dot_q5_0_q8_0(int n, float *s, const void *vx, const void *vy)
Auto-dispatch quantized dot product Q5_0 x Q8_0.

References vec_dot_q5_0_q8_0().

◆ ck_test_vec_dot_q6_k_q8_k()

void ck_test_vec_dot_q6_k_q8_k ( const void *  weight_q6_k,
const void *  input_q8_k,
float *  output,
int  cols 
)

Direct Q6_K x Q8_K dot product using identical pre-quantized bytes.

Definition at line 384 of file ck_parity_api.c.

388{
389 /* Exercise the production M=1 provider. The scalar vec_dot helper is an
390 * internal architecture-neutral oracle and does not preserve the x86
391 * provider's declared lane reduction order. */
392 gemv_q6_k_q8_k(output, weight_q6_k, input_q8_k, 1, cols);
393}

References gemv_q6_k_q8_k().

◆ ck_test_vec_dot_q8_0_q8_0()

void ck_test_vec_dot_q8_0_q8_0 ( const void *  weight_q8_0,
const void *  input_q8_0,
float *  output,
int  cols 
)

Direct Q8_0 x Q8_0 dot product (takes pre-quantized Q8_0 input)

Parameters
weight_q8_0Q8_0 quantized weights [cols]
input_q8_0Q8_0 quantized input [cols] (pre-quantized!)
outputOutput scalar [1]
colsNumber of elements (must be multiple of 32)

Direct Q8_0 x Q8_0 dot product (takes pre-quantized Q8_0 input)

Parameters
weight_q8_0Q8_0 quantized weights [cols]
input_q8_0Q8_0 quantized input [cols] (pre-quantized!)
outputOutput scalar [1]
colsNumber of elements (must be multiple of 32)

Definition at line 422 of file ck_parity_api.c.

426{
427 vec_dot_q8_0_q8_0(cols, output, weight_q8_0, input_q8_0);
428}
void vec_dot_q8_0_q8_0(int n, float *s, const void *vx, const void *vy)
Auto-dispatch quantized dot product Q8_0 x Q8_0.

References vec_dot_q8_0_q8_0().