1#ifndef CK_PACKED_Q8_ACTIVATION_CHECKED_H
2#define CK_PACKED_Q8_ACTIVATION_CHECKED_H
8#if defined(__x86_64__) && (defined(__GNUC__) || defined(__clang__))
10#define CK_Q8_X86_SIMD 1
16 for (
size_t i = 0; i < width; i +=
QK8_0, packed +=
sizeof(
block_q8_0)) {
18 for (
size_t j = 0; j <
QK8_0; ++j)
19 peak = fmaxf(peak, fabsf(input[i + j]));
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));
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);
39ck_q8_pack_round8(__m256 input, __m256 inverse)
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));
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));
51ck_pack_q8_activation_rne_avx2(const
float *input,
unsigned char *packed,
size_t width)
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)));
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));
88 if (__builtin_cpu_supports(
"avx2"))
return ck_pack_q8_activation_rne_avx2(input, packed, width);
102 const float scale = dw * dx;
103 for (
size_t lane = 0; lane < 8; ++lane) {
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);
109 lanes[lane] = fmaf(scale, (
float)
dot, lanes[lane]);
112 return ((lanes[0] + lanes[4]) + (lanes[2] + lanes[6]))
113 + ((lanes[1] + lanes[5]) + (lanes[3] + lanes[7]));
118ck_q8_activation_products(const
unsigned char *weight, __m256i activation)
120 const __m256i w = _mm256_loadu_si256((
const __m256i *)(weight + 2));
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)));
128ck_q8_activation_reduce(__m256 sum)
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)));
135static inline uint16_t ck_q8_x86_scale_bits(
const unsigned char *block)
138 memcpy(&scale, block,
sizeof(scale));
143ck_q8_activation_dot_avx2_f16c(const
unsigned char *weight, const
unsigned char *input,
size_t width)
145 __m256 sum = _mm256_setzero_ps();
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);
153 return ck_q8_activation_reduce(sum);
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)
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);
174 *g = ck_q8_activation_reduce(gate_sum);
175 *u = ck_q8_activation_reduce(up_sum);
178static inline int ck_q8_activation_has_f16c(
void)
180 return __builtin_cpu_supports(
"avx2") && __builtin_cpu_supports(
"fma") && __builtin_cpu_supports(
"f16c");
188 if (ck_q8_activation_has_f16c())
189 return ck_q8_activation_dot_avx2_f16c(weight, input, width);
195 const unsigned char *input,
size_t width,
float *g,
float *u)
198 if (ck_q8_activation_has_f16c()) {
199 ck_q8_activation_dot_pair_avx2_f16c(gate, up, input, width, g, u);
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.
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)