← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
packed_iq2_s_dot.h File Reference
#include "ckernel_quant.h"
#include "iq2_s_codebook.h"

Go to the source code of this file.

Data Structures

struct  ck_block_iq2_s

Functions

static float ck_dot_iq2_s_q8_k (size_t blocks, const ck_block_iq2_s *weights, const block_q8_K *input)

Function Documentation

◆ ck_dot_iq2_s_q8_k()

float ck_dot_iq2_s_q8_k ( size_t blocks,
const ck_block_iq2_s * weights,
const block_q8_K * input )
inlinestatic

Definition at line 19 of file packed_iq2_s_dot.h.

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}
static double dot(const float *a, const float *b, size_t n)
#define CK_FP16_TO_FP32(x)
#define QK_K
static const uint64_t ck_iq2_s_codebook[1024]
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]

References CK_FP16_TO_FP32, ck_iq2_s_codebook, block_q8_K::d, ck_block_iq2_s::d, dot(), ck_block_iq2_s::high, ck_block_iq2_s::low, QK_K, block_q8_K::qs, ck_block_iq2_s::scales, and ck_block_iq2_s::signs.

Referenced by iq2_s_expert_matvec().