okami.mom/strata

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

Francesco Albanocpu kernels: AVX-VNNI forms of the AVX-2 i-quant, IQ4_NL and Q2_0 rows (cpu_avxvnni_ok)b211ba173

main
3.1 KiB67 linesraw
1// src/kernels/cpu/q2_avx2_rows.inl - the Q2_0 row kernel of q2_avx2.cpp, included there twice like
2// iq_avx2_rows.inl: the AVX2 form (STRATA_ROWS_VNNI 0, namespace plain) and the AVX-VNNI one (1, namespace vnni), every
3// function under STRATA_ROWS_FN (GCC/Clang's target("avxvnni") in the VNNI copy).  Not a header.
4
5// The 2-bit codes (0..3) times the int8 activation, four products per int32 lane.  vpdpbusd is one uop where maddubs
6// + madd(ones) take two on the multiply ports; maddubs cannot saturate (a pair is at most 2 * 3 * 128), so the sums
7// are the same.
8STRATA_ROWS_FN inline __m256i dot4(__m256i codes, __m256i act) {
9#if STRATA_ROWS_VNNI
10    return _mm256_dpbusd_avx_epi32(_mm256_setzero_si256(), codes, act);
11#else
12    return _mm256_madd_epi16(_mm256_maddubs_epi16(codes, act), _mm256_set1_epi16(1));
13#endif
14}
15
16template <int NT> STRATA_ROWS_FN
17inline void row_multi(const uint8_t* row, const ActQ* const* a, int nblocks, float* res) {
18    __m256 acc[NT];
19    float corr[NT];
20    for (int t = 0; t < NT; ++t) { acc[t] = _mm256_setzero_ps(); corr[t] = 0.f; }
21    for (int b = 0; b < nblocks; ++b) {
22        const uint8_t* blk = row + (size_t) b * 18;
23        const float d = h2f(blk);
24        __m256i lo, hi;
25        unpack64(blk + 2, lo, hi);
26        for (int t = 0; t < NT; ++t) {
27            const int8_t* q = a[t]->q + b * 64;
28            const __m256i s0 = dot4(lo, _mm256_loadu_si256((const __m256i*) q));
29            const __m256i s1 = dot4(hi, _mm256_loadu_si256((const __m256i*) (q + 32)));
30            acc[t] = _mm256_fmadd_ps(_mm256_set1_ps(d * a[t]->scale[2 * b]), _mm256_cvtepi32_ps(s0), acc[t]);
31            acc[t] = _mm256_fmadd_ps(_mm256_set1_ps(d * a[t]->scale[2 * b + 1]), _mm256_cvtepi32_ps(s1), acc[t]);
32            corr[t] += d * (a[t]->hx[2 * b] + a[t]->hx[2 * b + 1]);
33        }
34    }
35    for (int t = 0; t < NT; ++t) {
36        const __m128 h = _mm_add_ps(_mm256_castps256_ps128(acc[t]), _mm256_extractf128_ps(acc[t], 1));
37        const __m128 s = _mm_add_ps(h, _mm_movehl_ps(h, h));
38        res[t] = _mm_cvtss_f32(_mm_add_ss(s, _mm_movehdup_ps(s))) - corr[t];
39    }
40}
41
42template <int NT> STRATA_ROWS_FN
43void rows(const uint8_t* w, size_t row_bytes, int nblocks, const ActQ* const* a, float* const* out, int r0, int r1) {
44    float res[NT];
45    for (int r = r0; r < r1; ++r) {
46        row_multi<NT>(w + (size_t) r * row_bytes, a, nblocks, res);
47        for (int t = 0; t < NT; ++t) out[t][r] = res[t];
48    }
49}
50
51STRATA_ROWS_FN void rows_nt(const uint8_t* w, size_t row_bytes, int nblocks, const ActQ* const* a, int nt,
52                            float* const* out, int r0, int r1) {
53    switch (nt) {
54        case 1: rows<1>(w, row_bytes, nblocks, a, out, r0, r1); break;
55        case 2: rows<2>(w, row_bytes, nblocks, a, out, r0, r1); break;
56        case 3: rows<3>(w, row_bytes, nblocks, a, out, r0, r1); break;
57        case 4: rows<4>(w, row_bytes, nblocks, a, out, r0, r1); break;
58        default:
59            for (int t0 = 0; t0 < nt; t0 += 4) {
60                const int k = nt - t0 < 4 ? nt - t0 : 4;
61                rows_nt(w, row_bytes, nblocks, a + t0, k, out + t0, r0, r1);
62            }
63    }
64}
65
66#undef STRATA_ROWS_VNNI
67#undef STRATA_ROWS_FN