← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
packed_k_dot_ordered.h
Go to the documentation of this file.
1#ifndef CK_PACKED_K_DOT_ORDERED_H
2#define CK_PACKED_K_DOT_ORDERED_H
3
4#include "ckernel_engine.h"
5#include "ckernel_quant.h"
6
7#include <string.h>
8
9/* Decode one packed block at a time. FP32 operands are accumulated in channel
10 * order in FP64, without requiring a dense weight buffer. */
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) \
13{ \
14 double sum = 0.0; \
15 for (size_t first = 0; first < width; first += block_width, weight += sizeof(type)) { \
16 type block; \
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]; \
22 } \
23 return (float)sum; \
24}
25
30
31#undef CK_DEFINE_PACKED_ORDERED_DOT
32
33static inline float ck_packed_q5_k_dot_ordered(
34 const float *input, const unsigned char *weight, size_t width)
35{
36 double sum = 0.0;
37 for (size_t first = 0; first < width; first += QK_K, weight += sizeof(block_q5_K)) {
38 block_q5_K block;
39 memcpy(&block, weight, sizeof(block));
40 const float d = CK_FP16_TO_FP32(block.d), dmin = CK_FP16_TO_FP32(block.dmin);
41 uint8_t scales[8], mins[8];
42 unpack_q4_k_scales(block.scales, scales, mins);
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) {
46 const float value = scale * q5_k_quant_value(&block, group, lane) - minimum;
47 sum += (double)input[first + group * 32 + lane] * value;
48 }
49 }
50 }
51 return (float)sum;
52}
53
54static inline float ck_packed_q3_k_dot_ordered(
55 const float *input, const unsigned char *weight, size_t width)
56{
57 double sum = 0.0;
58 for (size_t first = 0; first < width; first += QK_K, weight += sizeof(block_q3_K)) {
59 block_q3_K block;
60 memcpy(&block, weight, sizeof(block));
61 const float d = CK_FP16_TO_FP32(block.d);
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;
73 }
74 }
75 }
76 return (float)sum;
77}
78
79#if defined(__x86_64__) && (defined(__GNUC__) || defined(__clang__))
80#include <immintrin.h>
81
82/* Vectorize unpacking and products while preserving the scalar sum order.
83 * Separate FP32 multiply/subtract preserves the packed storage conversion. */
84#if defined(__GNUC__) && !defined(__clang__)
85__attribute__((optimize("fp-contract=off")))
86#endif
87__attribute__((target("avx2,f16c")))
88static inline float ck_packed_q4_k_dot_ordered_avx2(
89 const float *input, const unsigned char *weight, size_t width)
90{
91#if defined(__clang__)
92#pragma clang fp contract(off)
93#endif
94 double sum = 0.0;
95 const __m256i mask = _mm256_set1_epi32(15);
96 for (size_t first = 0; first < width; first += QK_K, weight += sizeof(block_q4_K)) {
97 block_q4_K block;
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];
101 unpack_q4_k_scales(block.scales, scales, mins);
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);
114 double products[8];
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];
122 }
123 }
124 }
125 return (float)sum;
126}
127#endif
128
129static inline float ck_packed_q4_k_dot_ordered(
130 const float *input, const unsigned char *weight, size_t width)
131{
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);
135#endif
136 return ck_packed_q4_k_dot_ordered_scalar(input, weight, width);
137}
138
139#endif
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.
#define QK5_0
static uint8_t q5_k_quant_value(const block_q5_K *block, int subblock, int i)
#define CK_FP16_TO_FP32(x)
#define QK5_1
static void unpack_q4_k_scales(const uint8_t *scales, uint8_t *sc, uint8_t *m)
Unpack Q4_K sub-block scales and mins.
#define QK_K
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)
uint8_t scales[12]
uint8_t qs[256/4]
uint8_t hmask[256/8]
uint8_t scales[12]
uint8_t qs[256/2]
uint8_t scales[12]
int32_t int32_t int32_t int32_t int32_t mask
Definition tokenizer.h:235
__attribute__((visibility("default"))) CKTokenizer *ck_tokenizer_create(CKTokenizerType type)