← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
packed_q8_activation_checked.h File Reference
#include "ckernel_quant.h"
#include <math.h>
#include <string.h>

Go to the source code of this file.

Functions

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

Function Documentation

◆ ck_pack_q8_activation_rne()

int ck_pack_q8_activation_rne ( const float * input,
unsigned char * packed,
size_t width )
inlinestatic

Definition at line 85 of file packed_q8_activation_checked.h.

86{
87#ifdef CK_Q8_X86_SIMD
88 if (__builtin_cpu_supports("avx2")) return ck_pack_q8_activation_rne_avx2(input, packed, width);
89#endif
90 return ck_pack_q8_activation_rne_scalar(input, packed, width);
91}
static int ck_pack_q8_activation_rne_scalar(const float *input, unsigned char *packed, size_t width)

References ck_pack_q8_activation_rne_scalar().

Referenced by linear_rows_nvfp4_q8_0_checked_f32(), linear_rows_q8_0_checked(), pack_hidden(), prepare_expert_activation(), and routed_softcapped_experts_checked_nvfp4_nvfp4_scaled().

◆ ck_pack_q8_activation_rne_scalar()

int ck_pack_q8_activation_rne_scalar ( const float * input,
unsigned char * packed,
size_t width )
inlinestatic

Definition at line 14 of file packed_q8_activation_checked.h.

15{
16 for (size_t i = 0; i < width; i += QK8_0, packed += sizeof(block_q8_0)) {
17 float peak = 0.0f;
18 for (size_t j = 0; j < QK8_0; ++j)
19 peak = fmaxf(peak, fabsf(input[i + j]));
20 const uint16_t scale = ck_fp32_to_fp16_soft(peak / 127.0f);
21 if ((scale & UINT16_C(0x7c00)) == UINT16_C(0x7c00)) return -1;
22 packed[0] = (unsigned char)scale;
23 packed[1] = (unsigned char)(scale >> 8);
24 const float inverse = scale ? 127.0f / peak : 0.0f;
25 for (size_t j = 0; j < QK8_0; ++j) {
26 const float value = input[i + j] * inverse;
27 const float magnitude = fminf(127.0f, fabsf(value));
28 int rounded = (int)magnitude;
29 const float fraction = magnitude - (float)rounded;
30 if (fraction > 0.5f || (fraction == 0.5f && (rounded & 1))) ++rounded;
31 packed[2 + j] = (unsigned char)(value < 0.0f ? -rounded : rounded);
32 }
33 }
34 return 0;
35}
static ck_half ck_fp32_to_fp16_soft(float f)
Convert FP32 to FP16 (ck_half) — software implementation.
#define QK8_0
static float rounded(float x)

References ck_fp32_to_fp16_soft(), QK8_0, and rounded().

Referenced by ck_pack_q8_activation_rne().

◆ ck_q8_activation_dot()

float ck_q8_activation_dot ( const unsigned char * weight,
const unsigned char * input,
size_t width )
inlinestatic

Definition at line 184 of file packed_q8_activation_checked.h.

186{
187#ifdef CK_Q8_X86_SIMD
188 if (ck_q8_activation_has_f16c())
189 return ck_q8_activation_dot_avx2_f16c(weight, input, width);
190#endif
191 return ck_q8_activation_dot_scalar(weight, input, width);
192}
static float ck_q8_activation_dot_scalar(const unsigned char *weight, const unsigned char *input, size_t width)

References ck_q8_activation_dot_scalar().

Referenced by expert_dot(), linear_rows_q8_0_checked(), and q8_projection_dot().

◆ ck_q8_activation_dot_pair()

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

Definition at line 194 of file packed_q8_activation_checked.h.

196{
197#ifdef CK_Q8_X86_SIMD
198 if (ck_q8_activation_has_f16c()) {
199 ck_q8_activation_dot_pair_avx2_f16c(gate, up, input, width, g, u);
200 return;
201 }
202#endif
203 *g = ck_q8_activation_dot_scalar(gate, input, width);
204 *u = ck_q8_activation_dot_scalar(up, input, width);
205}

References ck_q8_activation_dot_scalar().

Referenced by project_expert().

◆ ck_q8_activation_dot_scalar()

float ck_q8_activation_dot_scalar ( const unsigned char * weight,
const unsigned char * input,
size_t width )
inlinestatic

Definition at line 93 of file packed_q8_activation_checked.h.

95{
96 /* The x86 GGML reduction groups four signed products into each of
97 * eight FP32 FMA lanes, then combines lanes in a fixed tree. */
98 float lanes[8] = {0};
99 for (size_t i = 0; i < width; i += QK8_0, weight += sizeof(block_q8_0), input += sizeof(block_q8_0)) {
100 const float dw = ck_fp16_to_fp32_soft((uint16_t)(weight[0] | ((uint16_t)weight[1] << 8)));
101 const float dx = ck_fp16_to_fp32_soft((uint16_t)(input[0] | ((uint16_t)input[1] << 8)));
102 const float scale = dw * dx;
103 for (size_t lane = 0; lane < 8; ++lane) {
104 int dot = 0;
105 for (size_t j = 2 + 4 * lane; j < 6 + 4 * lane; ++j) {
106 const int w = weight[j], x = input[j];
107 dot += (w < 128 ? w : w - 256) * (x < 128 ? x : x - 256);
108 }
109 lanes[lane] = fmaf(scale, (float)dot, lanes[lane]);
110 }
111 }
112 return ((lanes[0] + lanes[4]) + (lanes[2] + lanes[6]))
113 + ((lanes[1] + lanes[5]) + (lanes[3] + lanes[7]));
114}
static double dot(const float *a, const float *b, size_t n)
static float ck_fp16_to_fp32_soft(ck_half h)
Convert FP16 (ck_half) to FP32 — software implementation.

References ck_fp16_to_fp32_soft(), dot(), and QK8_0.

Referenced by ck_q8_activation_dot(), and ck_q8_activation_dot_pair().