← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
packed_q8_activation_checked.h
Go to the documentation of this file.
1#ifndef CK_PACKED_Q8_ACTIVATION_CHECKED_H
2#define CK_PACKED_Q8_ACTIVATION_CHECKED_H
3
4#include "ckernel_quant.h"
5#include <math.h>
6#include <string.h>
7
8#if defined(__x86_64__) && (defined(__GNUC__) || defined(__clang__))
9#include <immintrin.h>
10#define CK_Q8_X86_SIMD 1
11#endif
12
13/* Input rows are finite. Emit little-endian Q8_0 blocks with nearest-even codes. */
14static inline int ck_pack_q8_activation_rne_scalar(const float *input, unsigned char *packed, size_t width)
15{
16 for (size_t i = 0; i < width; i += QK8_0, packed += sizeof(block_q8_0)) {
17 float peak = 0.0f;
18 for (size_t j = 0; j < QK8_0; ++j)
19 peak = fmaxf(peak, fabsf(input[i + j]));
20 const uint16_t scale = ck_fp32_to_fp16_soft(peak / 127.0f);
21 if ((scale & UINT16_C(0x7c00)) == UINT16_C(0x7c00)) return -1;
22 packed[0] = (unsigned char)scale;
23 packed[1] = (unsigned char)(scale >> 8);
24 const float inverse = scale ? 127.0f / peak : 0.0f;
25 for (size_t j = 0; j < QK8_0; ++j) {
26 const float value = input[i + j] * inverse;
27 const float magnitude = fminf(127.0f, fabsf(value));
28 int rounded = (int)magnitude;
29 const float fraction = magnitude - (float)rounded;
30 if (fraction > 0.5f || (fraction == 0.5f && (rounded & 1))) ++rounded;
31 packed[2 + j] = (unsigned char)(value < 0.0f ? -rounded : rounded);
32 }
33 }
34 return 0;
35}
36
37#ifdef CK_Q8_X86_SIMD
38static inline __attribute__((target("avx2"))) __m128i
39ck_q8_pack_round8(__m256 input, __m256 inverse)
40{
41 const __m256 scaled = _mm256_mul_ps(input, inverse);
42 const __m256 clamped = _mm256_max_ps(_mm256_min_ps(scaled, _mm256_set1_ps(127.0f)),
43 _mm256_set1_ps(-127.0f));
44 /* Select nearest-even explicitly instead of using the host rounding mode. */
45 const __m256 rounded = _mm256_round_ps(clamped, _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
46 const __m256i codes = _mm256_cvttps_epi32(rounded);
47 return _mm_packs_epi32(_mm256_castsi256_si128(codes), _mm256_extracti128_si256(codes, 1));
48}
49
50static __attribute__((target("avx2"))) int
51ck_pack_q8_activation_rne_avx2(const float *input, unsigned char *packed, size_t width)
52{
53 const __m256 abs_mask = _mm256_castsi256_ps(_mm256_set1_epi32(0x7fffffff));
54 for (size_t i = 0; i < width; i += QK8_0, packed += sizeof(block_q8_0)) {
55 const __m256 x0 = _mm256_loadu_ps(input + i);
56 const __m256 x1 = _mm256_loadu_ps(input + i + 8);
57 const __m256 x2 = _mm256_loadu_ps(input + i + 16);
58 const __m256 x3 = _mm256_loadu_ps(input + i + 24);
59 const __m256 zero = _mm256_setzero_ps();
60 const __m256 a0 = _mm256_max_ps(_mm256_and_ps(x0, abs_mask), zero);
61 const __m256 a1 = _mm256_max_ps(_mm256_and_ps(x1, abs_mask), zero);
62 const __m256 a2 = _mm256_max_ps(_mm256_and_ps(x2, abs_mask), zero);
63 const __m256 a3 = _mm256_max_ps(_mm256_and_ps(x3, abs_mask), zero);
64 const __m256 maximum = _mm256_max_ps(_mm256_max_ps(a0, a1), _mm256_max_ps(a2, a3));
65 __m128 peak4 = _mm_max_ps(_mm256_castps256_ps128(maximum), _mm256_extractf128_ps(maximum, 1));
66 peak4 = _mm_max_ps(peak4, _mm_movehl_ps(peak4, peak4));
67 const float peak = _mm_cvtss_f32(_mm_max_ss(peak4, _mm_movehdup_ps(peak4)));
68 /* Keep the scale and reciprocal arithmetic identical to the scalar path. */
69 const uint16_t scale = ck_fp32_to_fp16_soft(peak / 127.0f);
70 if ((scale & UINT16_C(0x7c00)) == UINT16_C(0x7c00)) return -1;
71 packed[0] = (unsigned char)scale;
72 packed[1] = (unsigned char)(scale >> 8);
73 const __m256 inverse = _mm256_set1_ps(scale ? 127.0f / peak : 0.0f);
74 const __m128i q0 = ck_q8_pack_round8(x0, inverse);
75 const __m128i q1 = ck_q8_pack_round8(x1, inverse);
76 const __m128i q2 = ck_q8_pack_round8(x2, inverse);
77 const __m128i q3 = ck_q8_pack_round8(x3, inverse);
78 _mm_storeu_si128((__m128i *)(packed + 2), _mm_packs_epi16(q0, q1));
79 _mm_storeu_si128((__m128i *)(packed + 18), _mm_packs_epi16(q2, q3));
80 }
81 return 0;
82}
83#endif
84
85static inline int ck_pack_q8_activation_rne(const float *input, unsigned char *packed, size_t width)
86{
87#ifdef CK_Q8_X86_SIMD
88 if (__builtin_cpu_supports("avx2")) return ck_pack_q8_activation_rne_avx2(input, packed, width);
89#endif
90 return ck_pack_q8_activation_rne_scalar(input, packed, width);
91}
92
93static inline float ck_q8_activation_dot_scalar(const unsigned char *weight, const unsigned char *input,
94 size_t width)
95{
96 /* The x86 GGML reduction groups four signed products into each of
97 * eight FP32 FMA lanes, then combines lanes in a fixed tree. */
98 float lanes[8] = {0};
99 for (size_t i = 0; i < width; i += QK8_0, weight += sizeof(block_q8_0), input += sizeof(block_q8_0)) {
100 const float dw = ck_fp16_to_fp32_soft((uint16_t)(weight[0] | ((uint16_t)weight[1] << 8)));
101 const float dx = ck_fp16_to_fp32_soft((uint16_t)(input[0] | ((uint16_t)input[1] << 8)));
102 const float scale = dw * dx;
103 for (size_t lane = 0; lane < 8; ++lane) {
104 int dot = 0;
105 for (size_t j = 2 + 4 * lane; j < 6 + 4 * lane; ++j) {
106 const int w = weight[j], x = input[j];
107 dot += (w < 128 ? w : w - 256) * (x < 128 ? x : x - 256);
108 }
109 lanes[lane] = fmaf(scale, (float)dot, lanes[lane]);
110 }
111 }
112 return ((lanes[0] + lanes[4]) + (lanes[2] + lanes[6]))
113 + ((lanes[1] + lanes[5]) + (lanes[3] + lanes[7]));
114}
115
116#ifdef CK_Q8_X86_SIMD
117static inline __attribute__((target("avx2"))) __m256
118ck_q8_activation_products(const unsigned char *weight, __m256i activation)
119{
120 const __m256i w = _mm256_loadu_si256((const __m256i *)(weight + 2));
121 /* Packed activations are in [-127, 127], so the signed transfer and
122 * each pair sum are exact even for a weight code of -128. */
123 const __m256i pairs = _mm256_maddubs_epi16(_mm256_abs_epi8(w), _mm256_sign_epi8(activation, w));
124 return _mm256_cvtepi32_ps(_mm256_madd_epi16(pairs, _mm256_set1_epi16(1)));
125}
126
127static inline __attribute__((target("avx2"))) float
128ck_q8_activation_reduce(__m256 sum)
129{
130 const __m128 halves = _mm_add_ps(_mm256_castps256_ps128(sum), _mm256_extractf128_ps(sum, 1));
131 const __m128 pairs = _mm_add_ps(halves, _mm_movehl_ps(halves, halves));
132 return _mm_cvtss_f32(_mm_add_ss(pairs, _mm_movehdup_ps(pairs)));
133}
134
135static inline uint16_t ck_q8_x86_scale_bits(const unsigned char *block)
136{
137 uint16_t scale;
138 memcpy(&scale, block, sizeof(scale));
139 return scale;
140}
141
142static __attribute__((target("avx2,fma,f16c"))) float
143ck_q8_activation_dot_avx2_f16c(const unsigned char *weight, const unsigned char *input, size_t width)
144{
145 __m256 sum = _mm256_setzero_ps();
146 for (size_t i = 0; i < width; i += QK8_0, weight += sizeof(block_q8_0), input += sizeof(block_q8_0)) {
147 const __m256i x = _mm256_loadu_si256((const __m256i *)(input + 2));
148 const uint32_t scales = ck_q8_x86_scale_bits(weight) | ((uint32_t)ck_q8_x86_scale_bits(input) << 16);
149 const __m128 decoded = _mm_cvtph_ps(_mm_cvtsi32_si128((int)scales));
150 const __m128 scale = _mm_mul_ss(decoded, _mm_shuffle_ps(decoded, decoded, _MM_SHUFFLE(1, 1, 1, 1)));
151 sum = _mm256_fmadd_ps(_mm256_broadcastss_ps(scale), ck_q8_activation_products(weight, x), sum);
152 }
153 return ck_q8_activation_reduce(sum);
154}
155
156static __attribute__((target("avx2,fma,f16c"))) void
157ck_q8_activation_dot_pair_avx2_f16c(const unsigned char *gate, const unsigned char *up,
158 const unsigned char *input, size_t width, float *g, float *u)
159{
160 __m256 gate_sum = _mm256_setzero_ps(), up_sum = _mm256_setzero_ps();
161 for (size_t i = 0; i < width; i += QK8_0) {
162 const __m256i x = _mm256_loadu_si256((const __m256i *)(input + 2));
163 const uint32_t weights = ck_q8_x86_scale_bits(gate) | ((uint32_t)ck_q8_x86_scale_bits(up) << 16);
164 const __m128i halves = _mm_set_epi32(0, 0, ck_q8_x86_scale_bits(input), (int)weights);
165 const __m128 decoded = _mm_cvtph_ps(halves);
166 const __m128 scales = _mm_mul_ps(decoded, _mm_shuffle_ps(decoded, decoded, _MM_SHUFFLE(2, 2, 2, 2)));
167 const __m128 up_scale = _mm_shuffle_ps(scales, scales, _MM_SHUFFLE(1, 1, 1, 1));
168 gate_sum = _mm256_fmadd_ps(_mm256_broadcastss_ps(scales), ck_q8_activation_products(gate, x), gate_sum);
169 up_sum = _mm256_fmadd_ps(_mm256_broadcastss_ps(up_scale), ck_q8_activation_products(up, x), up_sum);
170 gate += sizeof(block_q8_0);
171 up += sizeof(block_q8_0);
172 input += sizeof(block_q8_0);
173 }
174 *g = ck_q8_activation_reduce(gate_sum);
175 *u = ck_q8_activation_reduce(up_sum);
176}
177
178static inline int ck_q8_activation_has_f16c(void)
179{
180 return __builtin_cpu_supports("avx2") && __builtin_cpu_supports("fma") && __builtin_cpu_supports("f16c");
181}
182#endif
183
184static inline float ck_q8_activation_dot(const unsigned char *weight, const unsigned char *input,
185 size_t width)
186{
187#ifdef CK_Q8_X86_SIMD
188 if (ck_q8_activation_has_f16c())
189 return ck_q8_activation_dot_avx2_f16c(weight, input, width);
190#endif
191 return ck_q8_activation_dot_scalar(weight, input, width);
192}
193
194static inline void ck_q8_activation_dot_pair(const unsigned char *gate, const unsigned char *up,
195 const unsigned char *input, size_t width, float *g, float *u)
196{
197#ifdef CK_Q8_X86_SIMD
198 if (ck_q8_activation_has_f16c()) {
199 ck_q8_activation_dot_pair_avx2_f16c(gate, up, input, width, g, u);
200 return;
201 }
202#endif
203 *g = ck_q8_activation_dot_scalar(gate, input, width);
204 *u = ck_q8_activation_dot_scalar(up, input, width);
205}
206
207#endif
static double dot(const float *a, const float *b, size_t n)
Quantization block structures for weight-only quantization.
static float ck_fp16_to_fp32_soft(ck_half h)
Convert FP16 (ck_half) to FP32 — software implementation.
static ck_half ck_fp32_to_fp16_soft(float f)
Convert FP32 to FP16 (ck_half) — software implementation.
#define QK8_0
static float ck_q8_activation_dot_scalar(const unsigned char *weight, const unsigned char *input, size_t width)
static float ck_q8_activation_dot(const unsigned char *weight, const unsigned char *input, size_t width)
static void ck_q8_activation_dot_pair(const unsigned char *gate, const unsigned char *up, const unsigned char *input, size_t width, float *g, float *u)
static int ck_pack_q8_activation_rne_scalar(const float *input, unsigned char *packed, size_t width)
static int ck_pack_q8_activation_rne(const float *input, unsigned char *packed, size_t width)
static float rounded(float x)
__attribute__((visibility("default"))) CKTokenizer *ck_tokenizer_create(CKTokenizerType type)