11#ifndef CKERNEL_QUANT_H
12#define CKERNEL_QUANT_H
110#if defined(__cplusplus)
111static_assert(
sizeof(
block_q8_0) == 34,
"block_q8_0 ABI size changed");
113_Static_assert(
sizeof(
block_q8_0) == 34,
"block_q8_0 ABI size changed");
125#define QK_NVFP4_SUB 16
132#if defined(__cplusplus)
133static_assert(
sizeof(
block_nvfp4) == 36,
"block_nvfp4 ABI size changed");
135_Static_assert(
sizeof(
block_nvfp4) == 36,
"block_nvfp4 ABI size changed");
142 const void *activations,
float weight_scale);
144 const void *activations,
float weight_scale);
146 const float *weight_scales,
const void *activations,
150 const float *hidden,
const int *indices,
const float *routing_weights,
151 const void *expert_gate,
const float *expert_gate_scales,
152 const void *expert_up,
const float *expert_up_scales,
153 const void *expert_down,
const float *expert_down_scales,
154 float *output,
int rows,
int hidden_dim,
int intermediate_dim,
155 int n_experts,
int top_k,
void *workspace,
size_t workspace_bytes);
157 const float *hidden,
const float *routed,
158 const void *shared_gate,
const float *shared_gate_scale,
159 const void *shared_up,
const float *shared_up_scale,
160 const void *shared_down,
const float *shared_down_scale,
161 float *output,
int rows,
int hidden_dim,
int intermediate_dim,
162 float combination_scale,
void *workspace,
size_t workspace_bytes);
176#define K_SCALE_SIZE 12
214#if defined(__cplusplus)
215static_assert(
sizeof(
block_q8_K) == 292,
"block_q8_K ABI size changed");
217_Static_assert(
sizeof(
block_q8_K) == 292,
"block_q8_K ABI size changed");
229 case 0:
return QK4_0;
230 case 1:
return QK8_0;
264 return (n_elements / block_size) * type_size;
288 uint8_t *sc, uint8_t *m) {
290 sc[0] = scales[0] & 0x3F;
291 sc[1] = scales[1] & 0x3F;
292 sc[2] = scales[2] & 0x3F;
293 sc[3] = scales[3] & 0x3F;
295 m[0] = scales[4] & 0x3F;
296 m[1] = scales[5] & 0x3F;
297 m[2] = scales[6] & 0x3F;
298 m[3] = scales[7] & 0x3F;
302 sc[4] = (scales[8] & 0x0F) | ((scales[0] >> 6) << 4);
303 sc[5] = (scales[9] & 0x0F) | ((scales[1] >> 6) << 4);
304 sc[6] = (scales[10] & 0x0F) | ((scales[2] >> 6) << 4);
305 sc[7] = (scales[11] & 0x0F) | ((scales[3] >> 6) << 4);
307 m[4] = (scales[8] >> 4) | ((scales[4] >> 6) << 4);
308 m[5] = (scales[9] >> 4) | ((scales[5] >> 6) << 4);
309 m[6] = (scales[10] >> 4) | ((scales[6] >> 6) << 4);
310 m[7] = (scales[11] >> 4) | ((scales[7] >> 6) << 4);
326 uint32_t sign = (h & 0x8000) << 16;
327 uint32_t exp = (h >> 10) & 0x1F;
328 uint32_t mant = h & 0x3FF;
338 while ((mant & 0x400) == 0) {
343 result = sign | ((exp + 127 - 15) << 23) | (mant << 13);
345 }
else if (exp == 31) {
346 result = sign | 0x7F800000 | (mant << 13);
348 result = sign | ((exp + 127 - 15) << 23) | (mant << 13);
351 union { uint32_t u;
float f; } u;
357 union {
float f; uint32_t u; } u;
363 union { uint32_t u;
float f; } u;
372#if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)) && (!defined(__cplusplus) || __cplusplus >= 201703L)
373 const float scale_to_inf = 0x1.0p+112f;
374 const float scale_to_zero = 0x1.0p-110f;
379 float base = (fabsf(f) * scale_to_inf) * scale_to_zero;
382 const uint32_t shl1_w = w + w;
383 const uint32_t sign = w & UINT32_C(0x80000000);
384 uint32_t bias = shl1_w & UINT32_C(0xFF000000);
385 if (bias < UINT32_C(0x71000000)) {
386 bias = UINT32_C(0x71000000);
391 const uint32_t exp_bits = (bits >> 13) & UINT32_C(0x00007C00);
392 const uint32_t mantissa_bits = bits & UINT32_C(0x00000FFF);
393 const uint32_t nonsign = exp_bits + mantissa_bits;
395 return (
ck_half) ((sign >> 16) | (shl1_w > UINT32_C(0xFF000000) ? UINT16_C(0x7E00) : nonsign));
403#include <immintrin.h>
408static inline float ck_fp16_to_fp32_simd(
ck_half h) {
415static inline ck_half ck_fp32_to_fp16_simd(
float f) {
416 return (
ck_half)_cvtss_sh(f, _MM_FROUND_TO_NEAREST_INT);
425 return ck_fp16_to_fp32_simd(h);
433 return ck_fp32_to_fp16_simd(f);
440#define CK_FP16_TO_FP32(x) ck_fp16_to_fp32(x)
441#define CK_FP32_TO_FP16(x) ck_fp32_to_fp16(x)
442#define CK_FP16_TO_FP32_SIMD(x) ck_fp16_to_fp32_simd(x)
443#define CK_FP32_TO_FP16_SIMD(x) ck_fp32_to_fp16_simd(x)
444#define CK_FP16_TO_FP32_SOFT(x) ck_fp16_to_fp32_soft(x)
445#define CK_FP32_TO_FP16_SOFT(x) ck_fp32_to_fp16_soft(x)
449#define ggml_fp16_to_fp32 ck_fp16_to_fp32
450#define ggml_fp32_to_fp16 ck_fp32_to_fp16
451#define GGML_FP16_TO_FP32 CK_FP16_TO_FP32
452#define GGML_FP32_TO_FP16 CK_FP32_TO_FP16
458void gemm_nt_q5_0_sse_v2(
const float *A,
const void *B,
const float *bias,
float *
C,
int M,
int N,
int K);
459void gemm_nt_q6_k_sse(
const float *A,
const void *B,
const float *bias,
float *
C,
int M,
int N,
int K);
460void gemm_nt_q6_k_ref(
const float *A,
const void *B,
const float *bias,
float *
C,
int M,
int N,
int K);
463void rmsnorm_q8_k_fused(
const float *input,
const float *gamma,
void *vy,
int tokens,
int d_model,
int aligned_embed_dim,
float eps);
466void 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);
void gemm_nt_q5_0_sse_v2(const float *A, const void *B, const float *bias, float *C, int M, int N, int K)
int moe_swiglu_expert_forward_nvfp4_workspace(const float *hidden, const int *indices, const float *routing_weights, const void *expert_gate, const float *expert_gate_scales, const void *expert_up, const float *expert_up_scales, const void *expert_down, const float *expert_down_scales, float *output, int rows, int hidden_dim, int intermediate_dim, int n_experts, int top_k, void *workspace, size_t workspace_bytes)
static uint32_t ck_fp32_to_bits(float f)
static float ck_fp32_from_bits(uint32_t u32)
void gemv_nvfp4_q8_0(float *output, const void *weights, const float *weight_scales, const void *activations, int rows, int cols)
float ck_ue4m3_to_fp32(uint8_t value)
static float ck_fp16_to_fp32_soft(ck_half h)
Convert FP16 (ck_half) to FP32 — software implementation.
int moe_swiglu_shared_forward_nvfp4_workspace(const float *hidden, const float *routed, const void *shared_gate, const float *shared_gate_scale, const void *shared_up, const float *shared_up_scale, const void *shared_down, const float *shared_down_scale, float *output, int rows, int hidden_dim, int intermediate_dim, float combination_scale, void *workspace, size_t workspace_bytes)
void gemm_nt_q6_k_ref(const float *A, const void *B, const float *bias, float *C, int M, int N, int K)
void vec_dot_nvfp4_q8_0_ref(int n, float *output, const void *weights, const void *activations, float weight_scale)
size_t moe_swiglu_nvfp4_workspace_bytes(int hidden_dim, int intermediate_dim)
void gemm_nt_q6_k_sse(const float *A, const void *B, const float *bias, float *C, int M, int N, int K)
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.
static size_t ck_quant_type_size(int type)
Get the byte size per block for a quant type.
void rmsnorm_q8_k_fused(const float *input, const float *gamma, void *vy, int tokens, int d_model, int aligned_embed_dim, float eps)
static ck_half ck_fp32_to_fp16(float f)
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 gemm_nt_q5_0_q8_0_unroll_avx(const void *A_q8, const void *B_q5, const float *bias, float *C, int M, int N, int K)
static float ck_fp16_to_fp32(ck_half h)
void quantize_row_q8_k_sse(const float *x, void *vy, int k)
static size_t ck_quant_block_size(int type)
Get the block size (number of weights per block) for a quant type.
static void unpack_q4_k_scales(const uint8_t *scales, uint8_t *sc, uint8_t *m)
Unpack Q4_K sub-block scales and mins.
void quantize_row_q8_0(const float *x, void *vy, int k)
Quantize FP32 to Q8_0 format (scalar reference)
static size_t ck_quant_row_size(int type, int64_t n_elements)
Calculate total bytes needed for n_elements with given quant type.
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.
static ck_half ck_fp32_to_fp16_soft(float f)
Convert FP32 to FP16 (ck_half) — software implementation.
void vec_dot_nvfp4_q8_0(int n, float *output, const void *weights, const void *activations, float weight_scale)
void dequantize_row_nvfp4(const void *weights, float *output, int k, float weight_scale)
void gemv_q4_k_q8_k_sse(float *y, const void *W, const void *x_q8, int M, int K)