okami.mom/strata

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

Niko1221Merge f-027-gh-eng: GitHub engine items for 0.2.76ca60e9a0

main
9.1 KiB160 linesraw
1// src/kernels/cpu/tq_avx2.cpp - the trellis expert rows (TQ2_T / TQK6 / TQK7) on AVX2 + FMA (tq_cpu.hpp).
2// Compiled for AVX2 only and reached behind cpu_avx2_ok(), so nothing here can fault on an older CPU.
3//
4// Per 128-weight block: the 32 states in scalar code (tq_states.hpp: one 32-bit read, a shift and a mask each, used
5// as table indices straight away), four 8-byte state-table rows per YMM register as scalar loads, then per token
6// VPMADDWD against the unpacked int16 activations and a VPHADDD tree that leaves the four exact sub-block sums
7// S[k] in one XMM, so the float part is the scalar kernel's, lane for lane.  The weights are decoded once per block
8// for all the tokens of the group.
9//
10// Measured on Zen 5 (cycles per block, one token, AVX2): ~48 with this scheme; computing the states with byte
11// shuffles instead (VPSHUFB / VPSRLVD per 8 states) and moving them to GPRs or through memory was slower (60-80),
12// and ggml-cpu's AVX2 vec_dot for these types (agentionai llama.cpp, the same state table) takes about 2.3x as long.
13#include "strata/kernels/cpu/tq_cpu.hpp"
14
15#include "tq_states.hpp"
16
17#include <immintrin.h>
18
19namespace strata::kernels::cpu::tq_detail {
20namespace {
21
22// four state-table rows (8 bytes each) into one YMM: a load and three broadcast loads blended in.  Broadcast loads
23// and VPBLENDD avoid the shuffle port that VPINSRQ / VINSERTI128 need (the two measure the same on Zen 5).
24inline __m256i rows4(const long long* tab, uint32_t s0, uint32_t s1, uint32_t s2, uint32_t s3) {
25    __m256i v = _mm256_castsi128_si256(_mm_loadl_epi64((const __m128i*) (tab + s0)));
26    v = _mm256_blend_epi32(v, _mm256_set1_epi64x(tab[s1]), 0x0C);
27    v = _mm256_blend_epi32(v, _mm256_set1_epi64x(tab[s2]), 0x30);
28    return _mm256_blend_epi32(v, _mm256_set1_epi64x(tab[s3]), 0xC0);
29}
30
31template <int KB>
32void rows_t(const int16_t* table, const uint8_t* w, size_t row_bytes, int n, const TqAct* const* a, int nt, float* const* out, int r0,
33            int r1) {
34    const int nb = n / 128;
35    constexpr size_t bsz = 2 + 4 * KB;
36    const long long* tab = (const long long*) table;
37    for (int r = r0; r < r1; ++r) {
38        const uint8_t* x = w + (size_t) r * row_bytes;
39        __m128 acc[kTqMaxTokens];
40        for (int t = 0; t < nt; ++t) acc[t] = _mm_setzero_ps();
41        for (int i = 0; i < nb; ++i) {
42            const uint8_t* blk = x + (size_t) i * bsz;
43            const uint8_t* qs = blk + 2;
44            const uint64_t wr = tq_wrap<KB>(qs);
45            __m256i wv[8];   // wv[m] = weights 16m .. 16m+15 (states 4m .. 4m+3)
46#if defined(__GNUC__)
47#pragma GCC unroll 8
48#endif
49            for (int m = 0; m < 8; ++m)
50                wv[m] = rows4(tab, tq_state<KB>(qs, wr, 4 * m), tq_state<KB>(qs, wr, 4 * m + 1),
51                              tq_state<KB>(qs, wr, 4 * m + 2), tq_state<KB>(qs, wr, 4 * m + 3));
52            const __m128 dx = _mm_set1_ps(tq_half_to_float(tq_ld16(blk)));
53            for (int t = 0; t < nt; ++t) {
54                const __m256i* q = (const __m256i*) (a[t]->q + 128 * i);
55                const __m256i p0 = _mm256_add_epi32(_mm256_madd_epi16(wv[0], _mm256_load_si256(q + 0)),
56                                                    _mm256_madd_epi16(wv[1], _mm256_load_si256(q + 1)));
57                const __m256i p1 = _mm256_add_epi32(_mm256_madd_epi16(wv[2], _mm256_load_si256(q + 2)),
58                                                    _mm256_madd_epi16(wv[3], _mm256_load_si256(q + 3)));
59                const __m256i p2 = _mm256_add_epi32(_mm256_madd_epi16(wv[4], _mm256_load_si256(q + 4)),
60                                                    _mm256_madd_epi16(wv[5], _mm256_load_si256(q + 5)));
61                const __m256i p3 = _mm256_add_epi32(_mm256_madd_epi16(wv[6], _mm256_load_si256(q + 6)),
62                                                    _mm256_madd_epi16(wv[7], _mm256_load_si256(q + 7)));
63                // per 128-bit lane [p0, p1, p2, p3] partial sums, then the two lanes added: S[0..3], exact
64                const __m256i h = _mm256_hadd_epi32(_mm256_hadd_epi32(p0, p1), _mm256_hadd_epi32(p2, p3));
65                const __m128i s = _mm_add_epi32(_mm256_castsi256_si128(h), _mm256_extracti128_si256(h, 1));
66                const __m128 sc = _mm_mul_ps(dx, _mm_loadu_ps(a[t]->d + 4 * i));
67                acc[t] = _mm_fmadd_ps(_mm_cvtepi32_ps(s), sc, acc[t]);
68            }
69        }
70        for (int t = 0; t < nt; ++t) {
71            alignas(16) float v[4];
72            _mm_store_ps(v, acc[t]);
73            out[t][r] = ((v[0] + v[1]) + (v[2] + v[3])) * (1.0f / 8192.0f);
74        }
75    }
76}
77
78// TQP (147-149): 64 pair-steps per 128-value block.  A step's two weights are the FIRST pair of its state
79// table row, so 8 states make one YMM of 16 int16 weights, and a 32-value group (16 steps) is two YMM of
80// weights against two YMM of activations: VPMADDWD, one exact int32 sum per group, then the four group sums
81// go into the same per-token __m128 the other kernels use, so the float part is the scalar kernel's.
82inline __m256i pairs8(const int16_t* tab, uint32_t s0, uint32_t s1, uint32_t s2, uint32_t s3, uint32_t s4,
83                      uint32_t s5, uint32_t s6, uint32_t s7) {
84    const __m128i a0 = _mm_unpacklo_epi32(_mm_loadl_epi64((const __m128i*) (tab + 4 * s0)),
85                                          _mm_loadl_epi64((const __m128i*) (tab + 4 * s1)));
86    const __m128i a1 = _mm_unpacklo_epi32(_mm_loadl_epi64((const __m128i*) (tab + 4 * s2)),
87                                          _mm_loadl_epi64((const __m128i*) (tab + 4 * s3)));
88    const __m128i a2 = _mm_unpacklo_epi32(_mm_loadl_epi64((const __m128i*) (tab + 4 * s4)),
89                                          _mm_loadl_epi64((const __m128i*) (tab + 4 * s5)));
90    const __m128i a3 = _mm_unpacklo_epi32(_mm_loadl_epi64((const __m128i*) (tab + 4 * s6)),
91                                          _mm_loadl_epi64((const __m128i*) (tab + 4 * s7)));
92    return _mm256_set_m128i(_mm_unpacklo_epi64(a2, a3), _mm_unpacklo_epi64(a0, a1));
93}
94
95template <int KB>
96void rows_tqp_t(const uint8_t* w, size_t row_bytes, int n, const TqAct* const* a, int nt, float* const* out, int r0,
97                int r1) {
98    const int nb = n / 128;
99    constexpr size_t bsz = 2 + 8 * KB;
100    const int16_t* tab = tq_state_table();
101    for (int r = r0; r < r1; ++r) {
102        const uint8_t* x = w + (size_t) r * row_bytes;
103        __m128 acc[kTqMaxTokens];
104        for (int t = 0; t < nt; ++t) acc[t] = _mm_setzero_ps();
105        for (int i = 0; i < nb; ++i) {
106            const uint8_t* blk = x + (size_t) i * bsz;
107            uint32_t st[64];
108            tq_states_tqp<KB>(blk + 2, st);
109            __m256i wv[8];   // wv[2g], wv[2g+1]: the weights of the 32-value group g
110#pragma GCC unroll 4
111            for (int g = 0; g < 4; ++g) {
112                const uint32_t* s = st + 16 * g;
113                wv[2 * g] = pairs8(tab, s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]);
114                wv[2 * g + 1] = pairs8(tab, s[8], s[9], s[10], s[11], s[12], s[13], s[14], s[15]);
115            }
116            const __m128 dx = _mm_set1_ps(tq_half_to_float(tq_ld16(blk)));
117            for (int t = 0; t < nt; ++t) {
118                const __m256i* q = (const __m256i*) (a[t]->q + 128 * i);
119                int32_t sum[4];
120#pragma GCC unroll 4
121                for (int g = 0; g < 4; ++g) {
122                    const __m256i p = _mm256_add_epi32(_mm256_madd_epi16(wv[2 * g], _mm256_load_si256(q + 2 * g)),
123                                                       _mm256_madd_epi16(wv[2 * g + 1], _mm256_load_si256(q + 2 * g + 1)));
124                    __m128i s = _mm_add_epi32(_mm256_castsi256_si128(p), _mm256_extracti128_si256(p, 1));
125                    s = _mm_hadd_epi32(s, s);
126                    s = _mm_hadd_epi32(s, s);
127                    sum[g] = _mm_cvtsi128_si32(s);
128                }
129                const __m128 sc = _mm_mul_ps(dx, _mm_loadu_ps(a[t]->d + 4 * i));
130                const __m128i s4 = _mm_set_epi32(sum[3], sum[2], sum[1], sum[0]);
131                acc[t] = _mm_fmadd_ps(_mm_cvtepi32_ps(s4), sc, acc[t]);
132            }
133        }
134        for (int t = 0; t < nt; ++t) {
135            alignas(16) float v[4];
136            _mm_store_ps(v, acc[t]);
137            out[t][r] = ((v[0] + v[1]) + (v[2] + v[3])) * (1.0f / 8192.0f);
138        }
139    }
140}
141
142}  // namespace
143
144void tq_rows_avx2_tqp(int kb, const uint8_t* w, size_t row_bytes, int n, const TqAct* const* a, int nt,
145                      float* const* out, int r0, int r1) {
146    if (kb == 5) rows_tqp_t<5>(w, row_bytes, n, a, nt, out, r0, r1);
147    else if (kb == 6) rows_tqp_t<6>(w, row_bytes, n, a, nt, out, r0, r1);
148    else rows_tqp_t<7>(w, row_bytes, n, a, nt, out, r0, r1);
149}
150
151void tq_rows_avx2(int kb, const int16_t* table, const uint8_t* w, size_t row_bytes, int n, const TqAct* const* a, int nt,
152                  float* const* out, int r0, int r1) {
153    if (kb == 8) rows_t<8>(table, w, row_bytes, n, a, nt, out, r0, r1);
154    else if (kb == 6) rows_t<6>(table, w, row_bytes, n, a, nt, out, r0, r1);
155    else if (kb == 5) rows_t<5>(table, w, row_bytes, n, a, nt, out, r0, r1);   // experimental TQK5X
156    else if (kb == 4) rows_t<4>(table, w, row_bytes, n, a, nt, out, r0, r1);   // experimental TQK4X
157    else rows_t<7>(table, w, row_bytes, n, a, nt, out, r0, r1);
158}
159
160}  // namespace strata::kernels::cpu::tq_detail