← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
packed_iq2_xs_dot.h
Go to the documentation of this file.
1#ifndef CK_PACKED_IQ2_XS_DOT_H
2#define CK_PACKED_IQ2_XS_DOT_H
3
4#include "ckernel_quant.h"
5#include "iq2_xs_codebook.h"
6
7#if defined(__AVX2__)
8#include <immintrin.h>
9#endif
10
11typedef struct {
13 uint16_t codes[QK_K / 8];
14 uint8_t scales[QK_K / 32];
16
17_Static_assert(sizeof(ck_block_iq2_xs) == 74, "IQ2_XS GGUF layout");
18
19static inline uint8_t ck_iq2_xs_signs(unsigned code)
20{
21 unsigned parity = code ^ (code >> 4);
22 parity ^= parity >> 2;
23 parity ^= parity >> 1;
24 return (uint8_t)(code | ((parity & 1) << 7));
25}
26
27static inline float ck_dot_iq2_xs_q8_k(size_t blocks, const ck_block_iq2_xs *weights,
28 const block_q8_K *input)
29{
30 float result = 0.0f;
31 for (size_t b = 0; b < blocks; ++b) {
32 const ck_block_iq2_xs *w = weights + b;
33 const block_q8_K *x = input + b;
34 int32_t weighted = 0;
35#if defined(__AVX2__)
36 __m256i lanes = _mm256_setzero_si256();
37 const __m128i zero = _mm_setzero_si128();
38 const __m128i masks = _mm_set1_epi64x((int64_t)UINT64_C(0x8040201008040201));
39#endif
40 for (size_t g = 0; g < QK_K / 16; ++g) {
41 const unsigned a = w->codes[2 * g], c = w->codes[2 * g + 1];
42 const uint64_t low = ck_iq2_xs_codebook[a & 511], high = ck_iq2_xs_codebook[c & 511];
43 const unsigned sa = ck_iq2_xs_signs(a >> 9), sc = ck_iq2_xs_signs(c >> 9);
44 const int scale = 1 + 2 * ((w->scales[g / 2] >> (4 * (g % 2))) & 15);
45 const int8_t *row = x->qs + g * 16;
46#if defined(__AVX2__)
47 const __m128i grid = _mm_set_epi64x((int64_t)high, (int64_t)low);
48 const __m128i signs = _mm_set_epi64x((int64_t)(sc * UINT64_C(0x0101010101010101)),
49 (int64_t)(sa * UINT64_C(0x0101010101010101)));
50 const __m128i positive = _mm_cmpeq_epi8(_mm_and_si128(signs, masks), zero);
51 const __m128i signed_grid = _mm_blendv_epi8(_mm_sub_epi8(zero, grid), grid, positive);
52 const __m256i dot = _mm256_madd_epi16(_mm256_cvtepi8_epi16(signed_grid),
53 _mm256_cvtepi8_epi16(_mm_loadu_si128((const __m128i *)row)));
54 lanes = _mm256_add_epi32(lanes, _mm256_mullo_epi32(dot, _mm256_set1_epi32(scale)));
55#else
56 int32_t dot = 0;
57 for (size_t j = 0; j < 8; ++j) {
58 const int wa = (int)((low >> (8 * j)) & 255);
59 const int wc = (int)((high >> (8 * j)) & 255);
60 dot += row[j] * ((sa & (1u << j)) ? -wa : wa);
61 dot += row[j + 8] * ((sc & (1u << j)) ? -wc : wc);
62 }
63 weighted += scale * dot;
64#endif
65 }
66#if defined(__AVX2__)
67 int32_t sums[8];
68 _mm256_storeu_si256((__m256i *)sums, lanes);
69 for (size_t j = 0; j < 8; ++j) weighted += sums[j];
70#endif
71 const float scale = CK_FP16_TO_FP32(w->d) * x->d;
72 volatile float contribution = scale * (float)weighted;
73 result += contribution;
74 }
75 return result * 0.125f;
76}
77
78#endif
static double dot(const float *a, const float *b, size_t n)
Quantization block structures for weight-only quantization.
uint16_t ck_half
#define CK_FP16_TO_FP32(x)
#define QK_K
static const uint64_t ck_iq2_xs_codebook[512]
static float ck_dot_iq2_xs_q8_k(size_t blocks, const ck_block_iq2_xs *weights, const block_q8_K *input)
static uint8_t ck_iq2_xs_signs(unsigned code)
int8_t qs[256]
uint16_t codes[QK_K/8]
uint8_t scales[QK_K/32]