← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
packed_q3_k_dot.h
Go to the documentation of this file.
1#ifndef CK_PACKED_Q3_K_DOT_H
2#define CK_PACKED_Q3_K_DOT_H
3
4#include "ckernel_quant.h"
5
6typedef struct {
7 uint8_t high[QK_K / 8], low[QK_K / 4], scales[12];
10
11_Static_assert(sizeof(ck_block_q3_k) == 110, "Q3_K GGUF layout");
12
13static inline float ck_dot_q3_k_q8_k(size_t blocks, const ck_block_q3_k *weights,
14 const block_q8_K *input)
15{
16 float sums[8] = {0};
17 for (size_t b = 0; b < blocks; ++b) {
18 const ck_block_q3_k *w = weights + b;
19 const block_q8_K *x = input + b;
20 int32_t weighted[8] = {0};
21 for (unsigned g = 0; g < QK_K / 16; ++g) {
22 const unsigned lo = (w->scales[g % 8] >> (4 * (g / 8))) & 15;
23 const unsigned hi = (w->scales[8 + g % 4] >> (2 * (g / 4))) & 3;
24 const int scale = (int)(lo | (hi << 4)) - 32;
25 const unsigned base = (g % 2) * 16;
26 const unsigned mask = 1u << (g / 2);
27 const unsigned shift = 2 * ((g / 2) % 4);
28 for (unsigned j = 0; j < 16; ++j) {
29 const int q = (w->low[32 * (g / 8) + base + j] >> shift) & 3;
30 const int signed_q = q - ((w->high[base + j] & mask) ? 0 : 4);
31 weighted[j % 8] += scale * signed_q * x->qs[g * 16 + j];
32 }
33 }
34 const float d = CK_FP16_TO_FP32(w->d) * x->d;
35 for (unsigned i = 0; i < 8; ++i) {
36 const volatile float contribution = d * (float)weighted[i];
37 sums[i] += contribution;
38 }
39 }
40 float result = 0;
41 for (unsigned i = 0; i < 8; ++i) result += sums[i];
42 return result;
43}
44
45#endif
Quantization block structures for weight-only quantization.
uint16_t ck_half
#define CK_FP16_TO_FP32(x)
#define QK_K
static float ck_dot_q3_k_q8_k(size_t blocks, const ck_block_q3_k *weights, const block_q8_K *input)
int8_t qs[256]
uint8_t scales[12]
uint8_t high[QK_K/8]
uint8_t low[QK_K/4]
int32_t int32_t int32_t int32_t int32_t mask
Definition tokenizer.h:235