okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-2a (7c09c4a75) into f-own-int: the Strata quant layer and the x86 CPU rows9c143c403

main
3.2 KiB62 linesraw
1void quantize_q8_K_avx2(const float* x, void* vy, int64_t k) {
2    block_q8_K* y = (block_q8_K*) vy;
3    const int64_t nb = k / QK_K;
4    const __m256 absm = _mm256_castsi256_ps(_mm256_set1_epi32(0x7fffffff));
5    const __m256 magic = _mm256_set1_ps(12582912.f);
6    const __m256i mant = _mm256_set1_epi32(0x007fffff), off = _mm256_set1_epi32(0x00400000);
7    const __m256i c127 = _mm256_set1_epi32(127);
8    const __m256i perm = _mm256_setr_epi32(0, 4, 1, 5, 2, 6, 3, 7);
9    for (int64_t i = 0; i < nb; ++i, x += QK_K) {
10        __m256 m = _mm256_setzero_ps();
11        for (int j = 0; j < QK_K; j += 8) m = _mm256_max_ps(_mm256_and_ps(_mm256_loadu_ps(x + j), absm), m);
12        __m128 h = _mm_max_ps(_mm256_castps256_ps128(m), _mm256_extractf128_ps(m, 1));
13        h = _mm_max_ps(h, _mm_movehl_ps(h, h));
14        h = _mm_max_ss(h, _mm_movehdup_ps(h));
15        const float amax = _mm_cvtss_f32(h);
16        if (!amax) {
17            y[i].d = 0;
18            std::memset(y[i].qs, 0, QK_K);
19            continue;
20        }
21        float mx = 0.f;
22        const __m256 va = _mm256_set1_ps(amax);
23        for (int j = 0; j < QK_K; j += 8) {
24            const int msk = _mm256_movemask_ps(_mm256_cmp_ps(_mm256_and_ps(_mm256_loadu_ps(x + j), absm), va, _CMP_EQ_OQ));
25            if (msk) {
26                int b = 0;
27                while (!((msk >> b) & 1)) ++b;
28                mx = x[j + b];
29                break;
30            }
31        }
32        const float iscale = -127.f / mx;
33        const __m256 vis = _mm256_set1_ps(iscale);
34        for (int j = 0; j < QK_K; j += 32) {
35            __m256i v[4];
36            for (int q = 0; q < 4; ++q) {
37                __m256 p = _mm256_mul_ps(vis, _mm256_loadu_ps(x + j + 8 * q));
38#if defined(__GNUC__) && !defined(__clang__)
39                // GCC fuses a multiply and an add into an FMA by default (-ffp-contract=fast); the reference rounds
40                // the product before the add, so the product must stay a separate value
41                __asm__("" : "+x"(p));
42#endif
43                const __m256 t = _mm256_add_ps(p, magic);
44                v[q] = _mm256_min_epi32(_mm256_sub_epi32(_mm256_and_si256(_mm256_castps_si256(t), mant), off), c127);
45                // the reference stores MIN(127, v) into an int8 (keeps the low byte), and its bsums add those bytes:
46                // the same here, so even a degenerate block (iscale overflowing to inf) gives the same bytes
47                v[q] = _mm256_srai_epi32(_mm256_slli_epi32(v[q], 24), 24);
48            }
49            for (int q = 0; q < 2; ++q) {   // two 16-value sums
50                const __m256i s = _mm256_add_epi32(v[2 * q], v[2 * q + 1]);
51                __m128i s4 = _mm_add_epi32(_mm256_castsi256_si128(s), _mm256_extracti128_si256(s, 1));
52                s4 = _mm_add_epi32(s4, _mm_shuffle_epi32(s4, 0x4E));
53                s4 = _mm_add_epi32(s4, _mm_shuffle_epi32(s4, 0xB1));
54                y[i].bsums[j / 16 + q] = (int16_t) _mm_cvtsi128_si32(s4);
55            }
56            const __m256i p16a = _mm256_packs_epi32(v[0], v[1]), p16b = _mm256_packs_epi32(v[2], v[3]);
57            const __m256i p8 = _mm256_permutevar8x32_epi32(_mm256_packs_epi16(p16a, p16b), perm);
58            _mm256_storeu_si256((__m256i*) (y[i].qs + j), p8);
59        }
60        y[i].d = 1 / iscale;
61    }
62}