Expert-cache misses of the Gyro files run on the CPU workers. Until now those rows went through ggml-cpu's vec_dot one token at a time. Strata now has its own kernels for the three trellis types (tq_cpu.hpp), chosen at run time like the i-quant ones: - the 32768 x 4 int16 state table (one 8-byte lookup per four weights, built from the codebook in third_party/ggml/ggml-common-tq.h, now also visible to host code), scalar state extraction, four table rows per YMM by broadcast loads + VPBLENDD, VPMADDWD (AVX2) or VPDPWSSD (AVX-512 VNNI); - the weights of a block are decoded once for all the tokens routed to the expert (a verify window), then each token is a few instructions; - one fixed arithmetic for every ISA: exact int32 sums per Q8_0 sub-block, four float FMA chains, a fixed final sum. AVX2, AVX-512 and the scalar reference give the same bits, and a token gives the same bits alone or in a group of any size (no group-size rule needed, cf. #152). native_gu_rows / native_down_rows take them for types 144-146 on AVX2 and AVX-512 CPUs. STRATA_NO_TQ_KERNELS=1 restores ggml-cpu's vec_dot, STRATA_TQ_ISA=scalar|avx2|avx512 caps the ISA. A CPU without AVX2 keeps ggml-cpu. tq_cpu_parity (CPU only, ctest tq_cpu_parity and tq_cpu_parity_avx2): AVX2 / AVX-512 vs the scalar kernel bit for bit for groups of 1-8 tokens, vs ggml-cpu's vec_dot (rel ~1e-7, float order) and vs to_float in double (rel ~1e-5, the int16 table), plain and Hadamard-rotated, for Gyro-S (tqk6/tqk7) and Gyro-M (tq2_t) experts through the engine entry points. `tq_cpu_parity --bench` is the micro-benchmark (clock-calibrated cycles). Zen 5, one thread, weights streamed from DRAM, core cycles per 128-weight block (one token / per token in a group of three): ggml-cpu vec_dot, AVX2 build: 108-117 / 108-116 ggml-cpu vec_dot, AVX-512 build: 64-66 / 61-62 Strata AVX2: 47-49 / 22-23 Strata AVX-512: 47-49 / 22 (Q2_0's AVX2 kernel: ~20; the repacked AVX-512 Q2_0 kernel: ~7) Ported from agentionai/Strata rc2 7c85f1d (rc2 = 9ff02c9), author Laurent Zuijdwijk.
88 lines
4.9 KiB
C++
88 lines
4.9 KiB
C++
// src/kernels/cpu/tq_avx2.cpp - the trellis expert rows (TQ2_T / TQK6 / TQK7) on AVX2 + FMA (tq_cpu.hpp).
|
|
// Compiled for AVX2 only and reached behind cpu_avx2_ok(), so nothing here can fault on an older CPU.
|
|
//
|
|
// Per 128-weight block: the 32 states in scalar code (tq_states.hpp: one 32-bit read, a shift and a mask each, used
|
|
// as table indices straight away), four 8-byte state-table rows per YMM register as scalar loads, then per token
|
|
// VPMADDWD against the unpacked int16 activations and a VPHADDD tree that leaves the four exact sub-block sums
|
|
// S[k] in one XMM, so the float part is the scalar kernel's, lane for lane. The weights are decoded once per block
|
|
// for all the tokens of the group.
|
|
//
|
|
// Measured on Zen 5 (cycles per block, one token, AVX2): ~48 with this scheme; computing the states with byte
|
|
// shuffles instead (VPSHUFB / VPSRLVD per 8 states) and moving them to GPRs or through memory was slower (60-80),
|
|
// and ggml-cpu's AVX2 vec_dot for these types (agentionai llama.cpp, the same state table) takes about 2.3x as long.
|
|
#include "strata/kernels/cpu/tq_cpu.hpp"
|
|
|
|
#include "tq_states.hpp"
|
|
|
|
#include <immintrin.h>
|
|
|
|
namespace strata::kernels::cpu::tq_detail {
|
|
namespace {
|
|
|
|
// four state-table rows (8 bytes each) into one YMM: a load and three broadcast loads blended in. Broadcast loads
|
|
// and VPBLENDD avoid the shuffle port that VPINSRQ / VINSERTI128 need (the two measure the same on Zen 5).
|
|
inline __m256i rows4(const long long* tab, uint32_t s0, uint32_t s1, uint32_t s2, uint32_t s3) {
|
|
__m256i v = _mm256_castsi128_si256(_mm_loadl_epi64((const __m128i*) (tab + s0)));
|
|
v = _mm256_blend_epi32(v, _mm256_set1_epi64x(tab[s1]), 0x0C);
|
|
v = _mm256_blend_epi32(v, _mm256_set1_epi64x(tab[s2]), 0x30);
|
|
return _mm256_blend_epi32(v, _mm256_set1_epi64x(tab[s3]), 0xC0);
|
|
}
|
|
|
|
template <int KB>
|
|
void rows_t(const uint8_t* w, size_t row_bytes, int n, const TqAct* const* a, int nt, float* const* out, int r0,
|
|
int r1) {
|
|
const int nb = n / 128;
|
|
constexpr size_t bsz = 2 + 4 * KB;
|
|
const long long* tab = (const long long*) tq_state_table();
|
|
for (int r = r0; r < r1; ++r) {
|
|
const uint8_t* x = w + (size_t) r * row_bytes;
|
|
__m128 acc[kTqMaxTokens];
|
|
for (int t = 0; t < nt; ++t) acc[t] = _mm_setzero_ps();
|
|
for (int i = 0; i < nb; ++i) {
|
|
const uint8_t* blk = x + (size_t) i * bsz;
|
|
const uint8_t* qs = blk + 2;
|
|
const uint64_t wr = tq_wrap<KB>(qs);
|
|
__m256i wv[8]; // wv[m] = weights 16m .. 16m+15 (states 4m .. 4m+3)
|
|
#if defined(__GNUC__)
|
|
#pragma GCC unroll 8
|
|
#endif
|
|
for (int m = 0; m < 8; ++m)
|
|
wv[m] = rows4(tab, tq_state<KB>(qs, wr, 4 * m), tq_state<KB>(qs, wr, 4 * m + 1),
|
|
tq_state<KB>(qs, wr, 4 * m + 2), tq_state<KB>(qs, wr, 4 * m + 3));
|
|
const __m128 dx = _mm_set1_ps(tq_half_to_float(tq_ld16(blk)));
|
|
for (int t = 0; t < nt; ++t) {
|
|
const __m256i* q = (const __m256i*) (a[t]->q + 128 * i);
|
|
const __m256i p0 = _mm256_add_epi32(_mm256_madd_epi16(wv[0], _mm256_load_si256(q + 0)),
|
|
_mm256_madd_epi16(wv[1], _mm256_load_si256(q + 1)));
|
|
const __m256i p1 = _mm256_add_epi32(_mm256_madd_epi16(wv[2], _mm256_load_si256(q + 2)),
|
|
_mm256_madd_epi16(wv[3], _mm256_load_si256(q + 3)));
|
|
const __m256i p2 = _mm256_add_epi32(_mm256_madd_epi16(wv[4], _mm256_load_si256(q + 4)),
|
|
_mm256_madd_epi16(wv[5], _mm256_load_si256(q + 5)));
|
|
const __m256i p3 = _mm256_add_epi32(_mm256_madd_epi16(wv[6], _mm256_load_si256(q + 6)),
|
|
_mm256_madd_epi16(wv[7], _mm256_load_si256(q + 7)));
|
|
// per 128-bit lane [p0, p1, p2, p3] partial sums, then the two lanes added: S[0..3], exact
|
|
const __m256i h = _mm256_hadd_epi32(_mm256_hadd_epi32(p0, p1), _mm256_hadd_epi32(p2, p3));
|
|
const __m128i s = _mm_add_epi32(_mm256_castsi256_si128(h), _mm256_extracti128_si256(h, 1));
|
|
const __m128 sc = _mm_mul_ps(dx, _mm_loadu_ps(a[t]->d + 4 * i));
|
|
acc[t] = _mm_fmadd_ps(_mm_cvtepi32_ps(s), sc, acc[t]);
|
|
}
|
|
}
|
|
for (int t = 0; t < nt; ++t) {
|
|
alignas(16) float v[4];
|
|
_mm_store_ps(v, acc[t]);
|
|
out[t][r] = ((v[0] + v[1]) + (v[2] + v[3])) * (1.0f / 8192.0f);
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void tq_rows_avx2(int kb, const uint8_t* w, size_t row_bytes, int n, const TqAct* const* a, int nt,
|
|
float* const* out, int r0, int r1) {
|
|
if (kb == 8) rows_t<8>(w, row_bytes, n, a, nt, out, r0, r1);
|
|
else if (kb == 6) rows_t<6>(w, row_bytes, n, a, nt, out, r0, r1);
|
|
else rows_t<7>(w, row_bytes, n, a, nt, out, r0, r1);
|
|
}
|
|
|
|
} // namespace strata::kernels::cpu::tq_detail
|