← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
packed_iq3_s_dot.h
Go to the documentation of this file.
1#ifndef CK_PACKED_IQ3_S_DOT_H
2#define CK_PACKED_IQ3_S_DOT_H
3
4#include "ckernel_quant.h"
5#include "iq3_s_codebook.h"
6#include "packed_iq3_avx2.h"
7
8typedef struct {
10 uint8_t low[QK_K / 4], high[QK_K / 32];
11 uint8_t signs[QK_K / 8], scales[QK_K / 64];
13
14_Static_assert(sizeof(ck_block_iq3_s) == 110, "IQ3_S GGUF layout");
15
16static inline float ck_dot_iq3_s_q8_k(size_t blocks, const ck_block_iq3_s *weights,
17 const block_q8_K *input)
18{
19 float result = 0.0f;
20 for (size_t b = 0; b < blocks; ++b) {
21 const ck_block_iq3_s *w = weights + b;
22 const block_q8_K *x = input + b;
23#if defined(__AVX2__)
24 __m256i weighted_lanes = _mm256_setzero_si256();
25#else
26 int32_t weighted = 0;
27#endif
28 for (size_t g = 0; g < QK_K / 32; ++g) {
29 const unsigned scale = 1 + 2 * ((w->scales[g / 2] >> (4 * (g % 2))) & 15);
30#if defined(__AVX2__)
31 const __m256i group_scale = _mm256_set1_epi32((int)scale);
32 const unsigned high = w->high[g];
33 for (size_t i = 0; i < 8; i += 4) {
34 const uint8_t *codes = w->low + g * 8 + i;
35 const unsigned bits = high >> i;
36 const uint64_t lo = ck_iq3_grid_pair(
37 ck_iq3_s_codebook[codes[0] | ((bits & 1) << 8)],
38 ck_iq3_s_codebook[codes[1] | ((bits & 2) << 7)]);
39 const uint64_t hi = ck_iq3_grid_pair(
40 ck_iq3_s_codebook[codes[2] | ((bits & 4) << 6)],
41 ck_iq3_s_codebook[codes[3] | ((bits & 8) << 5)]);
42 const uint8_t *signs = w->signs + g * 4 + i / 2;
43 weighted_lanes = ck_iq3_accumulate_16(weighted_lanes, lo, hi, signs[0], signs[1],
44 x->qs + g * 32 + i * 4, group_scale);
45 }
46#else
47 int32_t sum = 0;
48 for (size_t sub = 0; sub < 4; ++sub) {
49 const unsigned signs = w->signs[g * 4 + sub];
50 for (size_t part = 0; part < 2; ++part) {
51 const size_t i = 2 * sub + part;
52 const unsigned code = w->low[g * 8 + i] | (((w->high[g] >> i) & 1) << 8);
53 const uint32_t grid = ck_iq3_s_codebook[code];
54 for (size_t j = 0; j < 4; ++j) {
55 const size_t lane = part * 4 + j;
56 const int mag = (int)((grid >> (8 * j)) & 255);
57 sum += x->qs[g * 32 + sub * 8 + lane] * ((signs & (1u << lane)) ? -mag : mag);
58 }
59 }
60 }
61 weighted += (int32_t)scale * sum;
62#endif
63 }
64#if defined(__AVX2__)
65 const int32_t weighted = ck_iq3_sum_lanes(weighted_lanes);
66#endif
67 const float scale = CK_FP16_TO_FP32(w->d) * x->d;
68 const volatile float contribution = scale * (float)weighted;
69 result += contribution;
70 }
71 return result;
72}
73
74#endif
Quantization block structures for weight-only quantization.
uint16_t ck_half
#define CK_FP16_TO_FP32(x)
#define QK_K
static const uint32_t ck_iq3_s_codebook[512]
static float ck_dot_iq3_s_q8_k(size_t blocks, const ck_block_iq3_s *weights, const block_q8_K *input)
int8_t qs[256]
uint8_t high[QK_K/32]
uint8_t scales[QK_K/64]
uint8_t low[QK_K/4]
uint8_t signs[QK_K/8]