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

Go to the source code of this file.

Data Structures

struct  ck_block_iq3_s

Functions

static float ck_dot_iq3_s_q8_k (size_t blocks, const ck_block_iq3_s *weights, const block_q8_K *input)

Function Documentation

◆ ck_dot_iq3_s_q8_k()

float ck_dot_iq3_s_q8_k ( size_t blocks,
const ck_block_iq3_s * weights,
const block_q8_K * input )
inlinestatic

Definition at line 16 of file packed_iq3_s_dot.h.

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}
#define CK_FP16_TO_FP32(x)
#define QK_K
static const uint32_t ck_iq3_s_codebook[512]
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]

References CK_FP16_TO_FP32, ck_iq3_s_codebook, block_q8_K::d, ck_block_iq3_s::d, ck_block_iq3_s::high, ck_block_iq3_s::low, QK_K, block_q8_K::qs, ck_block_iq3_s::scales, and ck_block_iq3_s::signs.

Referenced by iq3_s_expert_matvec().