1#ifndef CK_PACKED_K_DOT_ORDERED_H
2#define CK_PACKED_K_DOT_ORDERED_H
11#define CK_DEFINE_PACKED_ORDERED_DOT(name, type, block_width, decode) \
12static inline float name(const float *input, const unsigned char *weight, size_t width) \
15 for (size_t first = 0; first < width; first += block_width, weight += sizeof(type)) { \
17 float values[block_width]; \
18 memcpy(&block, weight, sizeof(block)); \
19 decode(&block, values, block_width); \
20 for (size_t channel = 0; channel < block_width; ++channel) \
21 sum += (double)input[first + channel] * (double)values[channel]; \
31#undef CK_DEFINE_PACKED_ORDERED_DOT
34 const float *input,
const unsigned char *weight,
size_t width)
37 for (
size_t first = 0; first < width; first +=
QK_K, weight +=
sizeof(
block_q5_K)) {
39 memcpy(&block, weight,
sizeof(block));
41 uint8_t scales[8], mins[8];
43 for (
int group = 0; group < 8; ++group) {
44 const float scale = d * scales[group], minimum = dmin * mins[group];
45 for (
int lane = 0; lane < 32; ++lane) {
47 sum += (double)input[first + group * 32 + lane] * value;
55 const float *input,
const unsigned char *weight,
size_t width)
58 for (
size_t first = 0; first < width; first +=
QK_K, weight +=
sizeof(
block_q3_K)) {
60 memcpy(&block, weight,
sizeof(block));
62 for (
unsigned group = 0; group < 16; ++group) {
63 const unsigned lo = (block.
scales[group % 8] >> (4 * (group / 8))) & 15u;
64 const unsigned hi = (block.
scales[8 + group % 4] >> (2 * (group / 4))) & 3u;
65 const float scale = d * ((int)(lo | (hi << 4)) - 32);
66 const unsigned plane = group / 2;
67 const unsigned lane_start = (group % 2) * 16;
68 for (
unsigned lane = lane_start; lane < lane_start + 16; ++lane) {
69 const int low = (block.
qs[(plane / 4) * 32 + lane] >> (2 * (plane % 4))) & 3u;
70 const int code = low - ((block.
hmask[lane] & (1u << plane)) ? 0 : 4);
71 const float value = scale * code;
72 sum += (double)input[first + group * 16 + lane - lane_start] * value;
79#if defined(__x86_64__) && (defined(__GNUC__) || defined(__clang__))
84#if defined(__GNUC__) && !defined(__clang__)
88static inline
float ck_packed_q4_k_dot_ordered_avx2(
89 const
float *input, const
unsigned char *weight,
size_t width)
92#pragma clang fp contract(off)
95 const __m256i
mask = _mm256_set1_epi32(15);
96 for (
size_t first = 0; first < width; first +=
QK_K, weight +=
sizeof(
block_q4_K)) {
98 memcpy(&block, weight,
sizeof(block));
99 const float scale = _cvtsh_ss(block.
d), minimum = _cvtsh_ss(block.
dmin);
100 uint8_t scales[8], mins[8];
102 for (
int group = 0; group < 8; ++group) {
103 const __m256 d = _mm256_set1_ps(scale * (
float)scales[group]);
104 const __m256 m = _mm256_set1_ps(minimum * (
float)mins[group]);
105 const uint8_t *qs = block.
qs + (group / 2) * 32;
106 const float *row = input + first + group * 32;
107 for (
int j = 0; j < 32; j += 8) {
108 const __m128i bytes = _mm_loadl_epi64((
const __m128i *)(qs + j));
109 __m256i codes = _mm256_cvtepu8_epi32(bytes);
110 if (group & 1) codes = _mm256_srli_epi32(codes, 4);
111 codes = _mm256_and_si256(codes,
mask);
112 const __m256 expanded = _mm256_sub_ps(_mm256_mul_ps(d, _mm256_cvtepi32_ps(codes)), m);
113 const __m256 x = _mm256_loadu_ps(row + j);
115 _mm256_storeu_pd(products, _mm256_mul_pd(
116 _mm256_cvtps_pd(_mm256_castps256_ps128(x)),
117 _mm256_cvtps_pd(_mm256_castps256_ps128(expanded))));
118 _mm256_storeu_pd(products + 4, _mm256_mul_pd(
119 _mm256_cvtps_pd(_mm256_extractf128_ps(x, 1)),
120 _mm256_cvtps_pd(_mm256_extractf128_ps(expanded, 1))));
121 for (
int k = 0; k < 8; ++k) sum += products[k];
130 const float *input,
const unsigned char *weight,
size_t width)
132#if defined(__x86_64__) && (defined(__GNUC__) || defined(__clang__))
133 if (__builtin_cpu_supports(
"avx2") && __builtin_cpu_supports(
"f16c"))
134 return ck_packed_q4_k_dot_ordered_avx2(input, weight, width);
void dequant_q5_0_row(const void *src, float *dst, size_t n_elements)
Dequantize Q5_0 row (multiple blocks).
void dequant_q5_1_row(const void *src, float *dst, size_t n_elements)
Dequantize Q5_1 row (multiple blocks).
void dequant_q6_k_row(const void *src, float *dst, size_t n_elements)
Dequantize Q6_K row (multiple blocks).
void dequant_q4_k_row(const void *src, float *dst, size_t n_elements)
Dequantize Q4_K row (multiple blocks).
Quantization block structures for weight-only quantization.
static uint8_t q5_k_quant_value(const block_q5_K *block, int subblock, int i)
#define CK_FP16_TO_FP32(x)
static void unpack_q4_k_scales(const uint8_t *scales, uint8_t *sc, uint8_t *m)
Unpack Q4_K sub-block scales and mins.
static float ck_packed_q4_k_dot_ordered(const float *input, const unsigned char *weight, size_t width)
static float ck_packed_q5_k_dot_ordered(const float *input, const unsigned char *weight, size_t width)
static float ck_packed_q5_1_dot_ordered(const float *input, const unsigned char *weight, size_t width)
static float ck_packed_q5_0_dot_ordered(const float *input, const unsigned char *weight, size_t width)
static float ck_packed_q4_k_dot_ordered_scalar(const float *input, const unsigned char *weight, size_t width)
#define CK_DEFINE_PACKED_ORDERED_DOT(name, type, block_width, decode)
static float ck_packed_q3_k_dot_ordered(const float *input, const unsigned char *weight, size_t width)
static float ck_packed_q6_k_dot_ordered(const float *input, const unsigned char *weight, size_t width)
int32_t int32_t int32_t int32_t int32_t mask
__attribute__((visibility("default"))) CKTokenizer *ck_tokenizer_create(CKTokenizerType type)