From cf45533cf68b3180ccce75d18e4dcfceae568418 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Fri, 23 Oct 2026 17:22:57 +0200 Subject: [PATCH] dsv41 cpu lane: AVX2 multi-token i-quant rows in ggml's float order (bit-identical to ggml-cpu at 1..8 tokens), IQ1_M rows The main engine's iq256_* rows keep their float order (EX=false, the default; hash of their outputs on all types and 1..8 tokens is identical before/after, phaseA-tests/cpul/hash_iq256.cpp). The new iq256x_rows (EX=true) sums as ggml-cpu does - block scale f16(d)*y.d, the format constant after the sum, ggml's hsum - so a token's dot no longer depends on how many tokens share the pass (the greedy ids of a DSpark run changed with DS4_GG_IQ256=1 because the old order differed in the last bits). IQ1_M (29), single-token in ggml, gets a multi-token AVX2 row. ds4_gg_bench --exact: 0 differing bits for IQ1_M, IQ2_XXS/XS/S, IQ3_XXS/S at 1..8 tokens. --- include/strata/kernels/cpu/iq_avx2.hpp | 7 ++ src/dsv41/ds4_gg_cpu.cpp | 4 +- src/kernels/cpu/iq_avx2.cpp | 17 +++ src/kernels/cpu/iq_avx2_rows.inl | 158 ++++++++++++++++++------- tests/dsv41/gg_dot_bench.cpp | 9 +- 5 files changed, 151 insertions(+), 44 deletions(-) diff --git a/include/strata/kernels/cpu/iq_avx2.hpp b/include/strata/kernels/cpu/iq_avx2.hpp index 3147ed55..ca5fa258 100644 --- a/include/strata/kernels/cpu/iq_avx2.hpp +++ b/include/strata/kernels/cpu/iq_avx2.hpp @@ -23,6 +23,13 @@ void iq256_gu_rows(int ggml_type, const uint8_t* blob, size_t gu_row, size_t up_ void iq256_rows(int ggml_type, const uint8_t* w, size_t row_bytes, int n, const void* const* act, int nt, float* const* out, int r0, int r1); +/// The DeepSeek-V4.1 CPU lane's rows (src/dsv41/ds4_gg_cpu.cpp). The same loops as iq256_rows / iq256_gu_rows with ggml's float +/// order, so a token's dot is ggml-cpu's vec_dot bit for bit at any number of tokens (the main engine's iq256_* are unchanged), +/// and IQ1_M (29), whose ggml dot is single-token. The caller checks AVX2 support. +bool iq256x_supported(int ggml_type) noexcept; +void iq256x_rows(int ggml_type, const uint8_t* w, size_t row_bytes, int n, const void* const* act, int nt, + float* const* out, int r0, int r1); + /// The kernels' variants, a mask. Every variant computes the same bits; they differ in speed from core to core. /// kIq256Gather: IQ3_XXS, IQ3_S and IQ2_S read their grid entries with one gather (STRATA_IQ256_GATHER). /// kIq256Vnni: AVX-VNNI's vpdpwssd / vpdpbusd, every format and iq4nl256_down_rows (cpu_avxvnni_ok). diff --git a/src/dsv41/ds4_gg_cpu.cpp b/src/dsv41/ds4_gg_cpu.cpp index 24b3635e..e5af30ae 100644 --- a/src/dsv41/ds4_gg_cpu.cpp +++ b/src/dsv41/ds4_gg_cpu.cpp @@ -91,12 +91,12 @@ static bool use_iq256(int type) { #endif g_iq256 = avx2 && v != nullptr && std::atoi(v) != 0 ? 1 : 0; } - return g_iq256 == 1 && strata::kernels::cpu::iq256_supported(type); + return g_iq256 == 1 && strata::kernels::cpu::iq256x_supported(type); } static void dots(int type, const uint8_t* w, size_t rb, int n, const void* const* act, int nt, float* const* out, int r0, int r1) { if (nt > 0 && use_iq256(type)) { - strata::kernels::cpu::iq256_rows(type, w, rb, n, act, nt, out, r0, r1); + strata::kernels::cpu::iq256x_rows(type, w, rb, n, act, nt, out, r0, r1); return; } const ggml_type_traits_cpu* tc = ggml_get_type_traits_cpu((ggml_type) type); diff --git a/src/kernels/cpu/iq_avx2.cpp b/src/kernels/cpu/iq_avx2.cpp index 3e10aa6e..ad6daf0f 100644 --- a/src/kernels/cpu/iq_avx2.cpp +++ b/src/kernels/cpu/iq_avx2.cpp @@ -534,6 +534,23 @@ void iq256_rows(int type, const uint8_t* w, size_t row_bytes, int n, const void* iq256_rows_v(iq256_variant(), type, w, row_bytes, n, act, nt, out, r0, r1); } +bool iq256x_supported(int type) noexcept { return type == 16 || type == 17 || type == 18 || type == 21 || type == 22 || type == 29; } + +void iq256x_rows(int type, const uint8_t* w, size_t row_bytes, int n, const void* const* act, int nt, float* const* out, + int r0, int r1) { + const int variant = iq256_variant(); + const bool g = (variant & kIq256Gather) != 0; +#if STRATA_AVXVNNI + if (variant & kIq256Vnni) { + if (g) vnni::dot_type(type, nt, w, row_bytes, n, act, out, r0, r1); + else vnni::dot_type(type, nt, w, row_bytes, n, act, out, r0, r1); + return; + } +#endif + if (g) plain::dot_type(type, nt, w, row_bytes, n, act, out, r0, r1); + else plain::dot_type(type, nt, w, row_bytes, n, act, out, r0, r1); +} + void iq4nl256_down_rows_v(int variant, const uint8_t* w, size_t row_bytes, int n, const void* const* hq, int nt, float* const* out, int r0, int r1) { const block_q8_0* y[8]; diff --git a/src/kernels/cpu/iq_avx2_rows.inl b/src/kernels/cpu/iq_avx2_rows.inl index 5489d8ee..e8101d21 100644 --- a/src/kernels/cpu/iq_avx2_rows.inl +++ b/src/kernels/cpu/iq_avx2_rows.inl @@ -78,7 +78,7 @@ void gu_ggml_one(const uint8_t* blob, size_t gu_row, size_t up_off, int n, const } } -template STRATA_ROWS_FN +template STRATA_ROWS_FN inline void row_dot(const uint8_t* row, int nblocks, const block_q8_K* const* y, float* res) { const int pf = prefetch_distance(); __m256 accf[NT]; @@ -100,11 +100,14 @@ inline void row_dot(const uint8_t* row, int nblocks, const block_q8_K* const* y, } } } - const float dx = h2f(u16(blk)) * Fmt32::K; + // EX (the DeepSeek CPU lane's rows): ggml's float order, so a token's dot is the same bits as ggml-cpu's whatever + // number of tokens share the pass - block scale f16(d) * y.d, the format's constant K after the sum, ggml's hsum. + // Without EX the order of the main engine's rows (unchanged). + const float dx = EX ? h2f(u16(blk)) : h2f(u16(blk)) * Fmt32::K; for (int t = 0; t < NT; ++t) accf[t] = _mm256_fmadd_ps(_mm256_set1_ps(dx * y[t][i].d), _mm256_cvtepi32_ps(acci[t]), accf[t]); } - for (int t = 0; t < NT; ++t) res[t] = hsum8(accf[t]); + for (int t = 0; t < NT; ++t) res[t] = EX ? Fmt32::K * ggml_hsum8(accf[t]) : hsum8(accf[t]); } // ---- IQ2_XS (17) with ggml's vectorized sign decode (arch/x86/quants.c, ggml_vec_dot_iq2_xs_q8_K): @@ -197,13 +200,84 @@ inline void row_dot_iq2xs(const uint8_t* row, int nblocks, const block_q8_K* con for (int t = 0; t < NT; ++t) res[t] = GGML ? 0.125f * ggml_hsum8(accf[t]) : hsum8(accf[t]); } -template STRATA_ROWS_FN -inline void row_dot_any(const uint8_t* row, int nblocks, const block_q8_K* const* y, float* res) { - if constexpr (TY == 17) row_dot_iq2xs(row, nblocks, y, res); - else row_dot(row, nblocks, y, res); +// ---- IQ1_M (29): 56-byte blocks (qs[32], qh[16], scales[8] holding the 3-bit sub-scales and, in their top nibbles, the +// block's f16 d), ggml's ggml_vec_dot_iq1_m_q8_K (AVX2): s = hsum(sum d*sumi1) + 0.125 * hsum(sum d*sumi2), sumi1 the +// grid dot, sumi2 the +-1 delta dot. The grid values are -1/0/1 bytes: |g| and the sign half of ggml's mul_add_epi8 +// are taken once per weight chunk, each token then costs two signs, two maddubs, two madds and the adds. +template STRATA_ROWS_FN +inline void row_dot_iq1m(const uint8_t* row, int nblocks, const block_q8_K* const* y, float* res) { + const __m256i mask = _mm256_set1_epi16(0x7); + const __m256i mone = _mm256_set1_epi16(1); + const __m256i mone8 = _mm256_set1_epi8(1); + const __m256i mtwo8 = _mm256_set1_epi8(2); + const __m256i scales_shift = _mm256_set_epi64x(9, 3, 6, 0); + const __m256i neg8 = _mm256_set1_epi8(-1); + const int pf = prefetch_distance(); + __m256 acc1[NT], acc2[NT]; + for (int t = 0; t < NT; ++t) acc1[t] = acc2[t] = _mm256_setzero_ps(); + for (int i = 0; i < nblocks; ++i) { + const uint8_t* blk = row + (size_t) i * 56; + rows_ahead(blk, pf); + const uint8_t* qs = blk; + const uint8_t* qh = blk + 32; + const uint8_t* scb = blk + 48; + const uint16_t sc0 = u16(scb), sc1 = u16(scb + 2), sc2 = u16(scb + 4), sc3 = u16(scb + 6); + const uint16_t d16 = (uint16_t) ((sc0 >> 12) | ((sc1 >> 8) & 0x00f0) | ((sc2 >> 4) & 0x0f00) | (sc3 & 0xf000)); + __m256i scales = _mm256_set1_epi64x((long long) u64(scb)); + scales = _mm256_srlv_epi64(scales, scales_shift); + scales = _mm256_add_epi16(_mm256_slli_epi16(_mm256_and_si256(scales, mask), 1), mone); + __m256i sidx1 = _mm256_set1_epi16(0x0100); + __m256i sidx2 = _mm256_add_epi8(sidx1, _mm256_set1_epi8(8)); + __m256i s1[NT], s2[NT]; + for (int t = 0; t < NT; ++t) s1[t] = s2[t] = _mm256_setzero_si256(); + for (int it = 0; it < 4; ++it) { + const uint8_t* q = qs + 8 * it; + const uint8_t* h = qh + 4 * it; + const __m256i g1 = _mm256_set_epi64x( + (long long) iq1s_grid[q[3] | (((uint16_t) h[1] << 4) & 0x700)], (long long) iq1s_grid[q[2] | (((uint16_t) h[1] << 8) & 0x700)], + (long long) iq1s_grid[q[1] | (((uint16_t) h[0] << 4) & 0x700)], (long long) iq1s_grid[q[0] | (((uint16_t) h[0] << 8) & 0x700)]); + const __m256i g2 = _mm256_set_epi64x( + (long long) iq1s_grid[q[7] | (((uint16_t) h[3] << 4) & 0x700)], (long long) iq1s_grid[q[6] | (((uint16_t) h[3] << 8) & 0x700)], + (long long) iq1s_grid[q[5] | (((uint16_t) h[2] << 4) & 0x700)], (long long) iq1s_grid[q[4] | (((uint16_t) h[2] << 8) & 0x700)]); + const __m256i dl1 = _mm256_set_epi64x(h[1] & 0x80 ? -1LL : 0x0101010101010101LL, h[1] & 0x08 ? -1LL : 0x0101010101010101LL, + h[0] & 0x80 ? -1LL : 0x0101010101010101LL, h[0] & 0x08 ? -1LL : 0x0101010101010101LL); + const __m256i dl2 = _mm256_set_epi64x(h[3] & 0x80 ? -1LL : 0x0101010101010101LL, h[3] & 0x08 ? -1LL : 0x0101010101010101LL, + h[2] & 0x80 ? -1LL : 0x0101010101010101LL, h[2] & 0x08 ? -1LL : 0x0101010101010101LL); + const __m256i a1 = _mm256_sign_epi8(g1, g1), a2 = _mm256_sign_epi8(g2, g2); + const __m256i sc_a = _mm256_shuffle_epi8(scales, sidx1); + const __m256i sc_b = _mm256_shuffle_epi8(scales, sidx2); + sidx1 = _mm256_add_epi8(sidx1, mtwo8); + sidx2 = _mm256_add_epi8(sidx2, mtwo8); + for (int t = 0; t < NT; ++t) { + const __m256i q8a = _mm256_loadu_si256((const __m256i*) (y[t][i].qs + 64 * it)); + const __m256i q8b = _mm256_loadu_si256((const __m256i*) (y[t][i].qs + 64 * it + 32)); + const __m256i dot1 = _mm256_maddubs_epi16(a1, _mm256_sign_epi8(q8a, g1)); + const __m256i dot2 = _mm256_maddubs_epi16(a2, _mm256_sign_epi8(q8b, g2)); + const __m256i dot3 = _mm256_maddubs_epi16(mone8, _mm256_sign_epi8(q8a, dl1)); + const __m256i dot4 = _mm256_maddubs_epi16(mone8, _mm256_sign_epi8(q8b, dl2)); + s1[t] = madd_add(madd_add(s1[t], dot1, sc_a), dot2, sc_b); + s2[t] = madd_add(madd_add(s2[t], dot3, sc_a), dot4, sc_b); + } + } + (void) neg8; + const float dx = h2f(d16); + for (int t = 0; t < NT; ++t) { + const __m256 d = _mm256_set1_ps(y[t][i].d * dx); + acc1[t] = _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(s1[t]), acc1[t]); + acc2[t] = _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(s2[t]), acc2[t]); + } + } + for (int t = 0; t < NT; ++t) res[t] = ggml_hsum8(acc1[t]) + 0.125f * ggml_hsum8(acc2[t]); } -template STRATA_ROWS_FN +template STRATA_ROWS_FN +inline void row_dot_any(const uint8_t* row, int nblocks, const block_q8_K* const* y, float* res) { + if constexpr (TY == 29) row_dot_iq1m(row, nblocks, y, res); + else if constexpr (TY == 17) row_dot_iq2xs(row, nblocks, y, res); + else row_dot(row, nblocks, y, res); +} + +template STRATA_ROWS_FN void gu_rows(const uint8_t* blob, size_t gu_row, size_t up_off, int n, const void* const* act, float* const* ff, int r0, int r1) { const block_q8_K* y[NT]; @@ -211,78 +285,80 @@ void gu_rows(const uint8_t* blob, size_t gu_row, size_t up_off, int n, const voi const int nb = n / QK_K; float g[NT], u[NT]; for (int r = r0; r < r1; ++r) { - row_dot_any(blob + (size_t) r * gu_row, nb, y, g); - row_dot_any(blob + up_off + (size_t) r * gu_row, nb, y, u); + row_dot_any(blob + (size_t) r * gu_row, nb, y, g); + row_dot_any(blob + up_off + (size_t) r * gu_row, nb, y, u); for (int t = 0; t < NT; ++t) ff[t][r] = (g[t] / (1.f + std::exp(-g[t]))) * u[t]; } } -template STRATA_ROWS_FN +template STRATA_ROWS_FN void dot_rows(const uint8_t* w, size_t row_bytes, int n, const void* const* act, float* const* out, int r0, int r1) { const block_q8_K* y[NT]; for (int t = 0; t < NT; ++t) y[t] = (const block_q8_K*) act[t]; float res[NT]; for (int r = r0; r < r1; ++r) { - row_dot_any(w + (size_t) r * row_bytes, n / QK_K, y, res); + row_dot_any(w + (size_t) r * row_bytes, n / QK_K, y, res); for (int t = 0; t < NT; ++t) out[t][r] = res[t]; } } -template STRATA_ROWS_FN +template STRATA_ROWS_FN void gu_rows_nt(int nt, const uint8_t* blob, size_t gu_row, size_t up_off, int n, const void* const* act, float* const* ff, int r0, int r1) { switch (nt) { - case 1: gu_rows(blob, gu_row, up_off, n, act, ff, r0, r1); break; - case 2: gu_rows(blob, gu_row, up_off, n, act, ff, r0, r1); break; - case 3: gu_rows(blob, gu_row, up_off, n, act, ff, r0, r1); break; - case 4: gu_rows(blob, gu_row, up_off, n, act, ff, r0, r1); break; - case 5: gu_rows(blob, gu_row, up_off, n, act, ff, r0, r1); break; - case 6: gu_rows(blob, gu_row, up_off, n, act, ff, r0, r1); break; - case 7: gu_rows(blob, gu_row, up_off, n, act, ff, r0, r1); break; - default: gu_rows(blob, gu_row, up_off, n, act, ff, r0, r1); break; + case 1: gu_rows(blob, gu_row, up_off, n, act, ff, r0, r1); break; + case 2: gu_rows(blob, gu_row, up_off, n, act, ff, r0, r1); break; + case 3: gu_rows(blob, gu_row, up_off, n, act, ff, r0, r1); break; + case 4: gu_rows(blob, gu_row, up_off, n, act, ff, r0, r1); break; + case 5: gu_rows(blob, gu_row, up_off, n, act, ff, r0, r1); break; + case 6: gu_rows(blob, gu_row, up_off, n, act, ff, r0, r1); break; + case 7: gu_rows(blob, gu_row, up_off, n, act, ff, r0, r1); break; + default: gu_rows(blob, gu_row, up_off, n, act, ff, r0, r1); break; } } -template STRATA_ROWS_FN +template STRATA_ROWS_FN void dot_rows_nt(int nt, const uint8_t* w, size_t row_bytes, int n, const void* const* act, float* const* out, int r0, int r1) { switch (nt) { - case 1: dot_rows(w, row_bytes, n, act, out, r0, r1); break; - case 2: dot_rows(w, row_bytes, n, act, out, r0, r1); break; - case 3: dot_rows(w, row_bytes, n, act, out, r0, r1); break; - case 4: dot_rows(w, row_bytes, n, act, out, r0, r1); break; + case 1: dot_rows(w, row_bytes, n, act, out, r0, r1); break; + case 2: dot_rows(w, row_bytes, n, act, out, r0, r1); break; + case 3: dot_rows(w, row_bytes, n, act, out, r0, r1); break; + case 4: dot_rows(w, row_bytes, n, act, out, r0, r1); break; default: for (int t0 = 0; t0 < nt; t0 += 4) { const int k = nt - t0 < 4 ? nt - t0 : 4; - dot_rows_nt(k, w, row_bytes, n, act + t0, out + t0, r0, r1); + dot_rows_nt(k, w, row_bytes, n, act + t0, out + t0, r0, r1); } } } // The decode of each format for a variant: the gathered one (kIq256Gather) for IQ3_XXS, IQ3_S and IQ2_S. -template STRATA_ROWS_FN +template STRATA_ROWS_FN void gu_type(int type, int nt, const uint8_t* blob, size_t gu_row, size_t up_off, int n, const void* const* act, float* const* ff, int r0, int r1) { switch (type) { - case 16: gu_rows_nt<16>(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break; - case 17: gu_rows_nt<17>(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break; - case 18: gu_rows_nt(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break; - case 21: gu_rows_nt(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break; - case 22: gu_rows_nt(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break; - case 23: gu_rows_nt<23>(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break; + case 16: gu_rows_nt<16, EX>(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break; + case 17: gu_rows_nt<17, EX>(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break; + case 18: gu_rows_nt(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break; + case 21: gu_rows_nt(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break; + case 22: gu_rows_nt(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break; + case 23: gu_rows_nt<23, EX>(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break; + case 29: gu_rows_nt<29, EX>(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break; default: break; } } -template STRATA_ROWS_FN +template STRATA_ROWS_FN void dot_type(int type, int nt, const uint8_t* w, size_t row_bytes, int n, const void* const* act, float* const* out, int r0, int r1) { switch (type) { - case 16: dot_rows_nt<16>(nt, w, row_bytes, n, act, out, r0, r1); break; - case 17: dot_rows_nt<17>(nt, w, row_bytes, n, act, out, r0, r1); break; - case 18: dot_rows_nt(nt, w, row_bytes, n, act, out, r0, r1); break; - case 21: dot_rows_nt(nt, w, row_bytes, n, act, out, r0, r1); break; - case 22: dot_rows_nt(nt, w, row_bytes, n, act, out, r0, r1); break; - case 23: dot_rows_nt<23>(nt, w, row_bytes, n, act, out, r0, r1); break; + case 16: dot_rows_nt<16, EX>(nt, w, row_bytes, n, act, out, r0, r1); break; + case 17: dot_rows_nt<17, EX>(nt, w, row_bytes, n, act, out, r0, r1); break; + case 18: dot_rows_nt(nt, w, row_bytes, n, act, out, r0, r1); break; + case 21: dot_rows_nt(nt, w, row_bytes, n, act, out, r0, r1); break; + case 22: dot_rows_nt(nt, w, row_bytes, n, act, out, r0, r1); break; + case 23: dot_rows_nt<23, EX>(nt, w, row_bytes, n, act, out, r0, r1); break; + case 29: dot_rows_nt<29, EX>(nt, w, row_bytes, n, act, out, r0, r1); break; default: break; } } diff --git a/tests/dsv41/gg_dot_bench.cpp b/tests/dsv41/gg_dot_bench.cpp index b8012e49..03b002e1 100644 --- a/tests/dsv41/gg_dot_bench.cpp +++ b/tests/dsv41/gg_dot_bench.cpp @@ -57,7 +57,14 @@ int main(int argc, char** argv) { const size_t bb = ggml_type_size((ggml_type) type); for (size_t o = 0; o + bb <= w.size(); o += bb) { // the blocks' F16 scale: a small normal value const ggml_fp16_t d = ggml_fp32_to_fp16(0.002f + 0.001f * (float) (next() % 16)); - std::memcpy(&w[o], &d, sizeof d); + if (type == 29) { // IQ1_M: no d field - its f16 is spread over the top nibbles of the four scale words (bytes 48..55) + uint16_t sc[4]; + std::memcpy(sc, &w[o + 48], 8); + for (int k = 0; k < 4; ++k) sc[k] = (uint16_t) ((sc[k] & 0x0fff) | ((d >> (4 * k)) & 0xf) << 12); + std::memcpy(&w[o + 48], sc, 8); + } else { + std::memcpy(&w[o], &d, sizeof d); + } } std::vector> a((size_t) nt, std::vector(f.act_bytes)); std::vector ap((size_t) nt);