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