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..5ec1e676 100644 --- a/src/dsv41/ds4_gg_cpu.cpp +++ b/src/dsv41/ds4_gg_cpu.cpp @@ -77,26 +77,80 @@ void gg_cpu_quant(int act, const float* x, int k, void* dst) { t->from_float_ref(x, dst, k); } -// Strata's AVX2 multi-token rows for the i-quants (iq256_rows: each 32-value chunk decoded once, every token applies it; -// the integer sums are ggml's, only the float additions come in another order). Opt-in until the end-to-end A/B is done: DS4_GG_IQ256=1. -static int g_iq256 = -1; // -1: from the environment and the CPU, 0 off, 1 on +// Strata's AVX2 multi-token rows for the i-quants (iq256x_rows: each 32-value chunk decoded once, every token applies it). They +// are ggml-cpu's vec_dot bit for bit (the integer sums are ggml's and the float additions come in ggml's order: ds4_gg_bench --exact, +// and own_ok() below checks it once per type on this CPU against the ggml-cpu build in use), so taking them or not never changes an +// answer, at any number of tokens - the choice is speed only. Measured on the rows alone: no faster than ggml's dot at one token +// (both wait for the codebook lookups), x1.7-2.9 at 2-4 tokens, x1.4 on IQ3_S already at one; so by default ggml's dot below +// min_tokens(type) and these rows from there. DS4_GG_IQ256=0 never takes them, =1 always (the old opt-in). +static int g_iq256 = -1; // -1: from the environment and the CPU, 0 off, 1 always, 2 from min_tokens(type) void gg_cpu_set_iq256(int mode) { g_iq256 = mode; } -static bool use_iq256(int type) { +static int min_tokens(int type) { return type == 21 ? 1 : type == 29 ? 3 : 2; } +static bool own_ok(int type) { + static std::once_flag once[32]; + static bool ok[32]; + const int k = type & 31; + std::call_once(once[k], [&] { + ok[k] = false; + const ggml_type_traits_cpu* tc = ggml_get_type_traits_cpu((ggml_type) type); + const ggml_type_traits_cpu* q8 = tc ? ggml_get_type_traits_cpu(tc->vec_dot_type) : nullptr; + if (tc == nullptr || tc->vec_dot == nullptr || q8 == nullptr || q8->from_float == nullptr) return; + const int n = 1024, rows = 7; + const size_t rb = ggml_row_size((ggml_type) type, n), bb = ggml_type_size((ggml_type) type), ab = ggml_row_size(tc->vec_dot_type, n); + uint64_t st = 0x9E3779B97F4A7C15ull + (uint64_t) type; + auto next = [&st] { st ^= st << 13; st ^= st >> 7; st ^= st << 17; return st; }; + std::vector w(rb * rows); + for (auto& b : w) b = (uint8_t) next(); + for (size_t o = 0; o < w.size(); o += bb) { + const ggml_fp16_t d = ggml_fp32_to_fp16(0.002f + 0.001f * (float) (next() % 16)); + if (type == 29) { // IQ1_M keeps its f16 in the top nibbles of the four scale words + uint16_t sc[4]; + std::memcpy(sc, &w[o + 48], 8); + for (int j = 0; j < 4; ++j) sc[j] = (uint16_t) ((sc[j] & 0x0fff) | ((d >> (4 * j)) & 0xf) << 12); + std::memcpy(&w[o + 48], sc, 8); + } else { + std::memcpy(&w[o], &d, sizeof d); + } + } + for (int nt : {1, 2, 3, 4, 5, 6, 8, 16, 33}) { + std::vector> a((size_t) nt, std::vector(ab)); + std::vector ap((size_t) nt); + std::vector> o0((size_t) nt, std::vector(rows)), o1 = o0; + std::vector p1((size_t) nt); + for (int t = 0; t < nt; ++t) { + std::vector x((size_t) n); + for (auto& v : x) v = ((float) (next() % 2001) - 1000.0f) * 1e-3f; + q8->from_float(x.data(), a[(size_t) t].data(), n); + ap[(size_t) t] = a[(size_t) t].data(); + p1[(size_t) t] = o1[(size_t) t].data(); + for (int r = 0; r < rows; ++r) tc->vec_dot(n, &o0[(size_t) t][(size_t) r], 0, w.data() + (size_t) r * rb, 0, ap[(size_t) t], 0, 1); + } + strata::kernels::cpu::iq256x_rows(type, w.data(), rb, n, ap.data(), nt, p1.data(), 0, rows); + for (int t = 0; t < nt; ++t) + if (std::memcmp(o0[(size_t) t].data(), o1[(size_t) t].data(), rows * sizeof(float)) != 0) { + if (dbg() || std::getenv("DS4_GG_VERBOSE")) std::fprintf(stderr, "ds4: CPU lane type %d: AVX2 rows differ from ggml-cpu's dot at %d tokens, not used\n", type, nt); + return; + } + } + ok[k] = true; + }); + return ok[k]; +} +static bool use_iq256(int type, int nt) { if (g_iq256 < 0) { const char* v = std::getenv("DS4_GG_IQ256"); - bool avx2 = false; -#if defined(__x86_64__) && (defined(__GNUC__) || defined(__clang__)) - __builtin_cpu_init(); - avx2 = __builtin_cpu_supports("avx2") && __builtin_cpu_supports("fma") && __builtin_cpu_supports("f16c"); -#endif - g_iq256 = avx2 && v != nullptr && std::atoi(v) != 0 ? 1 : 0; + // (ggml-cpu's own answer: built with AVX2 + FMA + F16C, which is also what makes its dots the AVX2 ones these rows match) + const bool avx2 = ggml_cpu_has_avx2() && ggml_cpu_has_fma() && ggml_cpu_has_f16c(); + g_iq256 = !avx2 ? 0 : v == nullptr ? 2 : std::atoi(v) != 0 ? 1 : 0; } - return g_iq256 == 1 && strata::kernels::cpu::iq256_supported(type); + if (g_iq256 == 0 || !strata::kernels::cpu::iq256x_supported(type)) return false; + if (g_iq256 == 2 && nt < min_tokens(type)) return false; + return own_ok(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); + if (nt > 0 && use_iq256(type, nt)) { + 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); @@ -107,8 +161,14 @@ static void dots(int type, const uint8_t* w, size_t rb, int n, const void* const void gg_cpu_gate_up(const IqCpu& f, const uint8_t* gate, const uint8_t* up, const void* const* act, int nt, float* const* ff, int r0, int r1, float limit) { - static thread_local std::vector tmp[8]; - float* uo[8]; + constexpr int kMaxTok = 64; // (the prompt path: a chunk's tokens an expert) + static thread_local std::vector tmp[kMaxTok]; + float* uo[kMaxTok]; + if (nt > kMaxTok) { // (more than a pass takes: in pieces) + for (int t0 = 0; t0 < nt; t0 += kMaxTok) + gg_cpu_gate_up(f, gate, up, act + t0, nt - t0 < kMaxTok ? nt - t0 : kMaxTok, ff + t0, r0, r1, limit); + return; + } for (int t = 0; t < nt; ++t) { tmp[t].resize((size_t) r1); uo[t] = tmp[t].data(); diff --git a/src/dsv41/ds4_gguf_cpu.hpp b/src/dsv41/ds4_gguf_cpu.hpp index d983a1dd..192a0968 100644 --- a/src/dsv41/ds4_gguf_cpu.hpp +++ b/src/dsv41/ds4_gguf_cpu.hpp @@ -55,7 +55,8 @@ bool gg_cpu_fmt(int tg, int td, int n_embd, int n_ff, IqCpu& f, std::string& err void gg_cpu_quant(int act, const float* x, int k, void* dst); void gg_cpu_gate_up(const IqCpu& f, const uint8_t* gate, const uint8_t* up, const void* const* act, int nt, float* const* ff, int r0, int r1, float limit); -/// -1: from the environment (DS4_GG_IQ256=1 turns Strata's AVX2 i-quant rows on, default off), 0 ggml-cpu, 1 forced (benches). +/// -1: from the environment (default: Strata's AVX2 i-quant rows, bit-identical to ggml-cpu's dot, from 2 tokens - 1 on IQ3_S, 3 on IQ1_M; +/// DS4_GG_IQ256=0 never, =1 always), 0 ggml-cpu, 1 always (benches), 2 the default rule. void gg_cpu_set_iq256(int mode); void gg_cpu_down(const IqCpu& f, const uint8_t* down, const void* const* hq, int nt, float* const* out, int r0, int r1); diff --git a/src/kernels/cpu/iq_avx2.cpp b/src/kernels/cpu/iq_avx2.cpp index 3e10aa6e..0ab33d21 100644 --- a/src/kernels/cpu/iq_avx2.cpp +++ b/src/kernels/cpu/iq_avx2.cpp @@ -32,6 +32,7 @@ #include #include #include +#include // STRATA_AVXVNNI (CMake: the compiler has the AVX-VNNI intrinsics): GCC and Clang compile them only inside functions // with target("avxvnni") - the VNNI copy of iq_avx2_rows.inl. @@ -534,6 +535,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..6c6eb02c 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]; @@ -86,8 +86,12 @@ inline void row_dot(const uint8_t* row, int nblocks, const block_q8_K* const* y, for (int i = 0; i < nblocks; ++i) { const uint8_t* blk = row + (size_t) i * Fmt32::bytes; rows_ahead(blk, pf); - __m256i acci[NT]; - for (int t = 0; t < NT; ++t) acci[t] = _mm256_setzero_si256(); + // EX: independent integer accumulators a token (A of them) cut the dependent madd+add chain from eight steps to 8/A - + // few tokens have too little else in flight to hide it (integer sums, the same bits) + constexpr int A = EX ? (NT == 1 ? 4 : NT == 2 ? 2 : 1) : 1; + __m256i acci[NT][A]; + for (int t = 0; t < NT; ++t) + for (int a = 0; a < A; ++a) acci[t][a] = _mm256_setzero_si256(); for (int j = 0; j < 4; ++j) { for (int half = 0; half < 2; ++half) { __m256i g, sgn, sc; @@ -96,15 +100,20 @@ inline void row_dot(const uint8_t* row, int nblocks, const block_q8_K* const* y, for (int t = 0; t < NT; ++t) { const __m256i yv = _mm256_loadu_si256((const __m256i*) (y[t][i].qs + off)); const __m256i ys = _mm256_sign_epi8(yv, sgn); - acci[t] = madd_add(acci[t], _mm256_maddubs_epi16(g, ys), sc); + acci[t][(2 * j + half) % A] = madd_add(acci[t][(2 * j + half) % A], _mm256_maddubs_epi16(g, ys), sc); } } } - const float dx = 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 a = 1; a < A; ++a) acci[t][0] = _mm256_add_epi32(acci[t][0], acci[t][a]); + // 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][0]), 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): @@ -156,8 +165,10 @@ inline void row_dot_iq2xs(const uint8_t* row, int nblocks, const block_q8_K* con __m128i st = _mm_set1_epi64x((long long) u64(blk + 66)); st = _mm_unpacklo_epi8(_mm_and_si128(st, m4), _mm_and_si128(_mm_srli_epi16(st, 4), m4)); const __m128i scales = _mm_add_epi8(_mm_slli_epi16(st, 1), m1); - __m256i acc[NT][2]; - for (int t = 0; t < NT; ++t) acc[t][0] = acc[t][1] = _mm256_setzero_si256(); + constexpr int CH = GGML ? 4 : 2; // (EX: four independent integer chains a token) + __m256i acc[NT][CH]; + for (int t = 0; t < NT; ++t) + for (int c = 0; c < CH; ++c) acc[t][c] = _mm256_setzero_si256(); for (int jj = 0; jj < 2; ++jj) { // 128 values (16 u16) per step const __m256i q2 = _mm256_loadu_si256((const __m256i*) (blk + 2 + 32 * jj)); const __m256i p7 = _mm256_srli_epi16(q2, 9); // 7 sign bits per u16, even bytes @@ -183,7 +194,7 @@ inline void row_dot_iq2xs(const uint8_t* row, int nblocks, const block_q8_K* con for (int t = 0; t < NT; ++t) { const __m256i yv = _mm256_loadu_si256((const __m256i*) (y[t][i].qs + off)); const __m256i ys = _mm256_sign_epi8(yv, sgn); - acc[t][h & 1] = madd_add(acc[t][h & 1], _mm256_maddubs_epi16(g, ys), sc); + acc[t][h % CH] = madd_add(acc[t][h % CH], _mm256_maddubs_epi16(g, ys), sc); } } } @@ -191,98 +202,404 @@ inline void row_dot_iq2xs(const uint8_t* row, int nblocks, const block_q8_K* con const float d = h2f(u16(blk)) * (GGML ? y[t][i].d : 0.125f); const float scale = GGML ? d : d * y[t][i].d; accf[t] = _mm256_fmadd_ps(_mm256_set1_ps(scale), - _mm256_cvtepi32_ps(_mm256_add_epi32(acc[t][0], acc[t][1])), accf[t]); + _mm256_cvtepi32_ps(CH == 4 ? _mm256_add_epi32(_mm256_add_epi32(acc[t][0], acc[t][1]), _mm256_add_epi32(acc[t][2], acc[t][3])) + : _mm256_add_epi32(acc[t][0], acc[t][1])), accf[t]); } } 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. Both are kept lane for lane as ggml has them, so the bits are ggml's. +// - the grid values are -1/0/1 bytes: |g| and g's sign (the two halves of ggml's mul_add_epi8) are taken once per weight chunk; +// - the delta dot needs no weight bytes beyond one sign a group of 8: sign(q8, delta) summed in pairs and times the scale is +// (pair sums of q8) times (+-scale), so the pair sums of every activation chunk are made once per call (iq1m_pre_build) and +// a token's delta dot is one madd per 32 values; +// - the indices' high bits and the delta signs of eight groups come out of one 32-bit load by variable shifts (ggml's +// non-BMI2 path branches on every bit). + +// the pair sums maddubs(1, q8) of every 32-value chunk of an activation: 16 int16 a chunk, 8 chunks a block +STRATA_ROWS_FN inline void iq1m_pre_build(const block_q8_K* y, int nblocks, int16_t* p) { + const __m256i one8 = _mm256_set1_epi8(1); + for (int i = 0; i < nblocks; ++i) + for (int c = 0; c < 8; ++c) + _mm256_storeu_si256((__m256i*) (p + ((size_t) i * 8 + c) * 16), + _mm256_maddubs_epi16(one8, _mm256_loadu_si256((const __m256i*) (y[i].qs + 32 * c)))); } -template STRATA_ROWS_FN +template STRATA_ROWS_FN +inline void row_dot_iq1m(const uint8_t* row, int nblocks, const block_q8_K* const* y, const int16_t* const* P, float* res) { + const __m256i mask = _mm256_set1_epi16(0x7); + const __m256i mone = _mm256_set1_epi16(1); + const __m256i mtwo8 = _mm256_set1_epi8(2); + const __m256i scales_shift = _mm256_set_epi64x(9, 3, 6, 0); + const __m256i nib_shift = _mm256_setr_epi32(0, 4, 8, 12, 16, 20, 24, 28); + const __m256i m7 = _mm256_set1_epi32(7), m8 = _mm256_set1_epi32(8); + 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)); + // the block's four 64-value steps decoded once into a small stack frame, then each token runs over them with two + // accumulators in registers (NT tokens' worth of accumulators and decode together do not fit 16 registers) + struct Dec { __m256i a1, g1, a2, g2, sa, sb, ssa, ssb; }; + Dec dec[4]; + for (int it = 0; it < 4; ++it) { + const __m256i v8 = _mm256_srlv_epi32(_mm256_set1_epi32((int) u32(qh + 4 * it)), nib_shift); // group k's qh nibble + const __m256i idx = _mm256_or_si256(_mm256_cvtepu8_epi32(_mm_loadl_epi64((const __m128i*) (qs + 8 * it))), + _mm256_slli_epi32(_mm256_and_si256(v8, m7), 8)); + alignas(32) int32_t ix[8]; + _mm256_store_si256((__m256i*) ix, idx); + const __m256i g1 = _mm256_set_epi64x((long long) iq1s_grid[ix[3]], (long long) iq1s_grid[ix[2]], + (long long) iq1s_grid[ix[1]], (long long) iq1s_grid[ix[0]]); + const __m256i g2 = _mm256_set_epi64x((long long) iq1s_grid[ix[7]], (long long) iq1s_grid[ix[6]], + (long long) iq1s_grid[ix[5]], (long long) iq1s_grid[ix[4]]); + const __m256i neg = _mm256_cmpeq_epi32(_mm256_and_si256(v8, m8), m8); // delta -1: all ones a group + const __m256i n1 = _mm256_cvtepi32_epi64(_mm256_castsi256_si128(neg)); // 4 int16 lanes a group of 8 values + const __m256i n2 = _mm256_cvtepi32_epi64(_mm256_extracti128_si256(neg, 1)); + const __m256i sc_a = _mm256_shuffle_epi8(scales, sidx1); + const __m256i sc_b = _mm256_shuffle_epi8(scales, sidx2); + dec[it] = Dec{_mm256_sign_epi8(g1, g1), g1, _mm256_sign_epi8(g2, g2), g2, sc_a, sc_b, + _mm256_sign_epi16(sc_a, _mm256_or_si256(n1, mone)), _mm256_sign_epi16(sc_b, _mm256_or_si256(n2, mone))}; + sidx1 = _mm256_add_epi8(sidx1, mtwo8); + sidx2 = _mm256_add_epi8(sidx2, mtwo8); + } + const float dx = h2f(d16); + for (int t = 0; t < NT; ++t) { + __m256i s1 = _mm256_setzero_si256(), s2 = _mm256_setzero_si256(); + for (int it = 0; it < 4; ++it) { + const Dec& dc = dec[it]; + 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(dc.a1, _mm256_sign_epi8(q8a, dc.g1)); + const __m256i dot2 = _mm256_maddubs_epi16(dc.a2, _mm256_sign_epi8(q8b, dc.g2)); + s1 = _mm256_add_epi32(s1, _mm256_add_epi32(_mm256_madd_epi16(dot1, dc.sa), _mm256_madd_epi16(dot2, dc.sb))); + const int16_t* pp = P[t] + ((size_t) i * 8 + 2 * it) * 16; + s2 = _mm256_add_epi32(s2, _mm256_add_epi32(_mm256_madd_epi16(_mm256_loadu_si256((const __m256i*) pp), dc.ssa), + _mm256_madd_epi16(_mm256_loadu_si256((const __m256i*) (pp + 16)), dc.ssb))); + } + const __m256 d = _mm256_set1_ps(y[t][i].d * dx); + acc1[t] = _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(s1), acc1[t]); + acc2[t] = _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(s2), 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 +inline void row_dot_any(const uint8_t* row, int nblocks, const block_q8_K* const* y, float* res, + const int16_t* const* aux = nullptr) { + if constexpr (TY == 29) row_dot_iq1m(row, nblocks, y, aux, 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]; for (int t = 0; t < NT; ++t) y[t] = (const block_q8_K*) act[t]; const int nb = n / QK_K; + const int16_t* aux[NT] = {}; + if constexpr (TY == 29) { // (IQ1_M's activation pair sums, once for the rows of this call) + static thread_local std::vector pre; + pre.resize((size_t) NT * nb * 128); + for (int t = 0; t < NT; ++t) { + iq1m_pre_build(y[t], nb, pre.data() + (size_t) t * nb * 128); + aux[t] = pre.data() + (size_t) t * nb * 128; + } + } 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, aux); + row_dot_any(blob + up_off + (size_t) r * gu_row, nb, y, u, aux); 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]; + const int nb = n / QK_K; + const int16_t* aux[NT] = {}; + if constexpr (TY == 29) { // (IQ1_M's activation pair sums, once for the rows of this call) + static thread_local std::vector pre; + pre.resize((size_t) NT * nb * 128); + for (int t = 0; t < NT; ++t) { + iq1m_pre_build(y[t], nb, pre.data() + (size_t) t * nb * 128); + aux[t] = pre.data() + (size_t) t * nb * 128; + } + } 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, nb, y, res, aux); for (int t = 0; t < NT; ++t) out[t][r] = res[t]; } } -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; - } +// ---- many tokens an expert (the prompt: a chunk's tokens routed to one expert), EX rows only. +// The weights of a 256-value block are decoded once into signed int8 (the grid magnitude times the sign) and every token of the +// call applies them. The activations come as u8 (q8 + 128, made once per call for all the rows): maddubs(act_u8, w_s8) needs no +// per-token sign, and sum(w * q) = sum(w * (q + 128)) - 128 * sum(w) with the second sum made once per weight block (corr), so the +// integer sums are exactly ggml's, lane for lane (no int16 overflow: |w| <= 62 for these grids, 255 * 62 * 2 < 32767). +// A token costs, per 32 values, one maddubs, one madd and one add (the single-token form has a sign on top). +constexpr int kWideTokens = 64; + +STRATA_ROWS_FN inline void wide_prep(const block_q8_K* y, int nb, uint8_t* u) { + const __m256i flip = _mm256_set1_epi8((char) 0x80); + for (int i = 0; i < nb; ++i) + for (int c = 0; c < 8; ++c) + _mm256_storeu_si256((__m256i*) (u + (size_t) i * 256 + 32 * c), + _mm256_xor_si256(_mm256_loadu_si256((const __m256i*) (y[i].qs + 32 * c)), flip)); } template STRATA_ROWS_FN +void dot_rows_wide(const uint8_t* w, size_t row_bytes, int n, const void* const* act, int T, float* const* out, int r0, int r1) { + // tokens in tiles of kTile and rows in groups of kRows: a tile's activation slice of one block (kTile * 256 B) and the group's + // accumulators (kRows * kTile * 32 B) stay in L1 while the group's rows go past, the tile's whole activation in L2 +#ifndef STRATA_WIDE_TILE +#define STRATA_WIDE_TILE 16 +#endif +#ifndef STRATA_WIDE_ROWS +#define STRATA_WIDE_ROWS 8 +#endif + constexpr int kTile = STRATA_WIDE_TILE, kRows = STRATA_WIDE_ROWS; + const int nb = n / QK_K; + static thread_local std::vector ubuf; + static thread_local std::vector accb; + ubuf.resize((size_t) (T < kTile ? T : kTile) * n); + accb.resize((size_t) kRows * kTile * 8); + const __m256i one8 = _mm256_set1_epi8(1); + const int pf = prefetch_distance(); + for (int t0 = 0; t0 < T; t0 += kTile) { + const int tt = T - t0 < kTile ? T - t0 : kTile; + const block_q8_K* y[kTile]; + for (int t = 0; t < tt; ++t) { + y[t] = (const block_q8_K*) act[t0 + t]; + wide_prep(y[t], nb, ubuf.data() + (size_t) t * n); + } + for (int rg = r0; rg < r1; rg += kRows) { + const int rr = r1 - rg < kRows ? r1 - rg : kRows; + for (int k = 0; k < rr * tt; ++k) _mm256_storeu_ps(&accb[(size_t) k * 8], _mm256_setzero_ps()); + for (int i = 0; i < nb; ++i) { + for (int q = 0; q < rr; ++q) { + const uint8_t* blk = w + (size_t) (rg + q) * row_bytes + (size_t) i * Fmt32::bytes; + rows_ahead(blk, pf); + __m256i w8[8], sc[8]; + __m256i corr = _mm256_setzero_si256(); + for (int j = 0; j < 4; ++j) + for (int half = 0; half < 2; ++half) { + __m256i g, sgn, s; + Fmt32::decode(blk, j, half, g, sgn, s); + const int k = 2 * j + half; + w8[k] = _mm256_sign_epi8(g, sgn); + sc[k] = s; + corr = madd_add(corr, _mm256_maddubs_epi16(one8, w8[k]), s); + } + const __m256i corr128 = _mm256_slli_epi32(corr, 7); + const float dx = h2f(u16(blk)); + float* accq = &accb[(size_t) q * tt * 8]; + for (int t = 0; t < tt; ++t) { + const uint8_t* u = ubuf.data() + (size_t) t * n + (size_t) i * 256; + __m256i a0 = _mm256_setzero_si256(), a1 = _mm256_setzero_si256(); + for (int k = 0; k < 8; k += 2) { + a0 = madd_add(a0, _mm256_maddubs_epi16(_mm256_loadu_si256((const __m256i*) (u + 32 * k)), w8[k]), sc[k]); + a1 = madd_add(a1, _mm256_maddubs_epi16(_mm256_loadu_si256((const __m256i*) (u + 32 * k + 32)), w8[k + 1]), sc[k + 1]); + } + const __m256i s = _mm256_sub_epi32(_mm256_add_epi32(a0, a1), corr128); + float* ap = accq + (size_t) t * 8; + _mm256_storeu_ps(ap, _mm256_fmadd_ps(_mm256_set1_ps(dx * y[t][i].d), _mm256_cvtepi32_ps(s), _mm256_loadu_ps(ap))); + } + } + } + for (int q = 0; q < rr; ++q) + for (int t = 0; t < tt; ++t) + out[t0 + t][rg + q] = Fmt32::K * ggml_hsum8(_mm256_loadu_ps(&accb[((size_t) q * tt + t) * 8])); + } + } +} + +// IQ1_M the same way: its grid bytes are already signed (-1, 0, 1), and the delta dot keeps the pair sums of the activation +// (maddubs(1, q8), made once per tile). Two float accumulators a (row, token), ggml's sum d*sumi1 and sum d*sumi2. +STRATA_ROWS_FN +void dot_rows_wide_iq1m(const uint8_t* w, size_t row_bytes, int n, const void* const* act, int T, float* const* out, int r0, int r1) { + constexpr int kTile = STRATA_WIDE_TILE, kRows = STRATA_WIDE_ROWS; + const int nb = n / QK_K; + static thread_local std::vector ubuf; + static thread_local std::vector pbuf; + static thread_local std::vector accb; + ubuf.resize((size_t) (T < kTile ? T : kTile) * n); + pbuf.resize((size_t) (T < kTile ? T : kTile) * n); + accb.resize((size_t) kRows * kTile * 16); + const __m256i one8 = _mm256_set1_epi8(1); + const __m256i mask = _mm256_set1_epi16(0x7); + const __m256i mone = _mm256_set1_epi16(1); + const __m256i mtwo8 = _mm256_set1_epi8(2); + const __m256i scales_shift = _mm256_set_epi64x(9, 3, 6, 0); + const __m256i nib_shift = _mm256_setr_epi32(0, 4, 8, 12, 16, 20, 24, 28); + const __m256i m7 = _mm256_set1_epi32(7), m8 = _mm256_set1_epi32(8); + const int pf = prefetch_distance(); + for (int t0 = 0; t0 < T; t0 += kTile) { + const int tt = T - t0 < kTile ? T - t0 : kTile; + const block_q8_K* y[kTile]; + for (int t = 0; t < tt; ++t) { + y[t] = (const block_q8_K*) act[t0 + t]; + wide_prep(y[t], nb, ubuf.data() + (size_t) t * n); + iq1m_pre_build(y[t], nb, pbuf.data() + (size_t) t * n); + } + for (int rg = r0; rg < r1; rg += kRows) { + const int rr = r1 - rg < kRows ? r1 - rg : kRows; + for (int k = 0; k < rr * tt * 2; ++k) _mm256_storeu_ps(&accb[(size_t) k * 8], _mm256_setzero_ps()); + for (int i = 0; i < nb; ++i) { + for (int q = 0; q < rr; ++q) { + const uint8_t* blk = w + (size_t) (rg + q) * row_bytes + (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 g1[4], g2[4], sa[4], sb[4], ssa[4], ssb[4]; + __m256i corr = _mm256_setzero_si256(); + for (int it = 0; it < 4; ++it) { + const __m256i v8 = _mm256_srlv_epi32(_mm256_set1_epi32((int) u32(qh + 4 * it)), nib_shift); + const __m256i idx = _mm256_or_si256(_mm256_cvtepu8_epi32(_mm_loadl_epi64((const __m128i*) (qs + 8 * it))), + _mm256_slli_epi32(_mm256_and_si256(v8, m7), 8)); + alignas(32) int32_t ix[8]; + _mm256_store_si256((__m256i*) ix, idx); + g1[it] = _mm256_set_epi64x((long long) iq1s_grid[ix[3]], (long long) iq1s_grid[ix[2]], + (long long) iq1s_grid[ix[1]], (long long) iq1s_grid[ix[0]]); + g2[it] = _mm256_set_epi64x((long long) iq1s_grid[ix[7]], (long long) iq1s_grid[ix[6]], + (long long) iq1s_grid[ix[5]], (long long) iq1s_grid[ix[4]]); + const __m256i neg = _mm256_cmpeq_epi32(_mm256_and_si256(v8, m8), m8); + const __m256i n1 = _mm256_cvtepi32_epi64(_mm256_castsi256_si128(neg)); + const __m256i n2 = _mm256_cvtepi32_epi64(_mm256_extracti128_si256(neg, 1)); + sa[it] = _mm256_shuffle_epi8(scales, sidx1); + sb[it] = _mm256_shuffle_epi8(scales, sidx2); + ssa[it] = _mm256_sign_epi16(sa[it], _mm256_or_si256(n1, mone)); + ssb[it] = _mm256_sign_epi16(sb[it], _mm256_or_si256(n2, mone)); + sidx1 = _mm256_add_epi8(sidx1, mtwo8); + sidx2 = _mm256_add_epi8(sidx2, mtwo8); + corr = madd_add(madd_add(corr, _mm256_maddubs_epi16(one8, g1[it]), sa[it]), _mm256_maddubs_epi16(one8, g2[it]), sb[it]); + } + const __m256i corr128 = _mm256_slli_epi32(corr, 7); + const float dx = h2f(d16); + float* accq = &accb[(size_t) q * tt * 16]; + for (int t = 0; t < tt; ++t) { + const uint8_t* u = ubuf.data() + (size_t) t * n + (size_t) i * 256; + const int16_t* pp = pbuf.data() + (size_t) t * n + (size_t) i * 128; + __m256i a0 = _mm256_setzero_si256(), a1 = _mm256_setzero_si256(); + __m256i b0 = _mm256_setzero_si256(), b1 = _mm256_setzero_si256(); + for (int it = 0; it < 4; ++it) { + a0 = madd_add(a0, _mm256_maddubs_epi16(_mm256_loadu_si256((const __m256i*) (u + 64 * it)), g1[it]), sa[it]); + a1 = madd_add(a1, _mm256_maddubs_epi16(_mm256_loadu_si256((const __m256i*) (u + 64 * it + 32)), g2[it]), sb[it]); + b0 = madd_add(b0, _mm256_loadu_si256((const __m256i*) (pp + 32 * it)), ssa[it]); + b1 = madd_add(b1, _mm256_loadu_si256((const __m256i*) (pp + 32 * it + 16)), ssb[it]); + } + const __m256i s1 = _mm256_sub_epi32(_mm256_add_epi32(a0, a1), corr128); + const __m256i s2 = _mm256_add_epi32(b0, b1); + const __m256 d = _mm256_set1_ps(y[t][i].d * dx); + float* ap = accq + (size_t) t * 16; + _mm256_storeu_ps(ap, _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(s1), _mm256_loadu_ps(ap))); + _mm256_storeu_ps(ap + 8, _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(s2), _mm256_loadu_ps(ap + 8))); + } + } + } + for (int q = 0; q < rr; ++q) + for (int t = 0; t < tt; ++t) { + const float* ap = &accb[((size_t) q * tt + t) * 16]; + out[t0 + t][rg + q] = ggml_hsum8(_mm256_loadu_ps(ap)) + 0.125f * ggml_hsum8(_mm256_loadu_ps(ap + 8)); + } + } + } +} + +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; + } +} + +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) { + if constexpr (EX) { + if (nt >= 7) { // (a prompt chunk's tokens: one decode of a block for all of them) + if constexpr (TY == 29) dot_rows_wide_iq1m(w, row_bytes, n, act, nt, out, r0, r1); + else dot_rows_wide(w, row_bytes, n, act, nt, out, r0, r1); + return; + } + } 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; + case 5: if constexpr (EX) { dot_rows(w, row_bytes, n, act, out, r0, r1); break; } // (the DeepSeek lane's verify + [[fallthrough]]; // rounds: one decode of 5 or 6) + case 6: if constexpr (EX) { dot_rows(w, row_bytes, n, act, out, r0, r1); break; } + [[fallthrough]]; 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..27604acb 100644 --- a/tests/dsv41/gg_dot_bench.cpp +++ b/tests/dsv41/gg_dot_bench.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -26,10 +27,103 @@ using clk = std::chrono::steady_clock; static double secs(clk::time_point a) { return std::chrono::duration(clk::now() - a).count(); } +// ---- --expert: the engine's expert pipeline on random experts (gate+up+SwiGLU, quantize h, down) in jobs of 64 rows over N threads, for +// ggml-cpu's dots (mode 0), the default rule (2) and the AVX2 rows always (1); GB/s of all three matrices' bytes, outputs compared bit for bit. +static void fill_blocks(uint8_t* w, size_t bytes, int type, uint64_t seed) { + uint64_t st = 0x9E3779B97F4A7C15ull ^ (seed * 0xD1B54A32D192ED03ull); + auto next = [&st] { st ^= st << 13; st ^= st >> 7; st ^= st << 17; return st; }; + for (size_t o = 0; o + 8 <= bytes; o += 8) { const uint64_t v = next(); std::memcpy(w + o, &v, 8); } + const size_t bb = ggml_type_size((ggml_type) type); + for (size_t o = 0; o + bb <= bytes; o += bb) { + const ggml_fp16_t d = ggml_fp32_to_fp16(0.002f + 0.001f * (float) (next() % 16)); + if (type == 29) { + 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); + } + } +} + +static int run_expert(int tg, int td, int threads, long mb, int nt, int reps) { + ggml_cpu_init(); + const int E = 5120, FF = 2304; + IqCpu f; + std::string err; + if (!gg_cpu_fmt(tg, td, E, FF, f, err)) { std::fprintf(stderr, "gg_cpu_fmt: %s\n", err.c_str()); return 2; } + const size_t gbytes = f.gu_row * FF, dbytes = f.d_row * E, ebytes = 2 * gbytes + dbytes; + const int ne = (int) std::max(2L, (long) ((size_t) mb * 1000000 / ebytes)); + std::vector gw((size_t) ne * gbytes), uw((size_t) ne * gbytes), dw((size_t) ne * dbytes); + for (int e = 0; e < ne; ++e) { + fill_blocks(&gw[(size_t) e * gbytes], gbytes, tg, 3 * (uint64_t) e + 1); + fill_blocks(&uw[(size_t) e * gbytes], gbytes, tg, 3 * (uint64_t) e + 2); + fill_blocks(&dw[(size_t) e * dbytes], dbytes, td, 3 * (uint64_t) e + 3); + } + uint64_t st = 12345; + auto next = [&st] { st ^= st << 13; st ^= st >> 7; st ^= st << 17; return st; }; + std::vector> act((size_t) nt, std::vector(f.act_bytes)); + for (int t = 0; t < nt; ++t) { + std::vector x((size_t) E); + for (auto& v : x) v = ((float) (next() % 2001) - 1000.0f) * 1e-3f; + gg_cpu_quant(f.gu_act, x.data(), E, act[(size_t) t].data()); + } + std::vector h((size_t) ne * nt * FF), y((size_t) ne * nt * E); + std::vector hq((size_t) ne * nt * f.h_bytes); + const int RB = 64; + auto run_jobs = [&](long njobs, const std::function& fn) { + std::atomic cur{0}; + std::vector th; + for (int k = 0; k < threads; ++k) + th.emplace_back([&] { for (;;) { const long j = cur.fetch_add(1); if (j >= njobs) break; fn(j); } }); + for (auto& t : th) t.join(); + }; + auto pass = [&](int mode) { + gg_cpu_set_iq256(mode); + double best = 1e30; + for (int r = 0; r < reps + 1; ++r) { + const auto t0 = clk::now(); + const long gb = (FF + RB - 1) / RB, db = (E + RB - 1) / RB; + run_jobs((long) ne * gb, [&](long j) { + const int e = (int) (j / gb), r0 = (int) (j % gb) * RB, r1 = std::min(FF, r0 + RB); + const void* ap[64]; + float* hp[64]; + for (int t = 0; t < nt; ++t) { ap[t] = act[(size_t) t].data(); hp[t] = &h[((size_t) e * nt + t) * FF]; } + gg_cpu_gate_up(f, &gw[(size_t) e * gbytes], &uw[(size_t) e * gbytes], ap, nt, hp, r0, r1, 10.0f); + }); + run_jobs((long) ne * nt, [&](long j) { gg_cpu_quant(f.d_act, &h[(size_t) j * FF], FF, &hq[(size_t) j * f.h_bytes]); }); + run_jobs((long) ne * db, [&](long j) { + const int e = (int) (j / db), r0 = (int) (j % db) * RB, r1 = std::min(E, r0 + RB); + const void* hp[64]; + float* yp[64]; + for (int t = 0; t < nt; ++t) { hp[t] = &hq[((size_t) e * nt + t) * f.h_bytes]; yp[t] = &y[((size_t) e * nt + t) * E]; } + gg_cpu_down(f, &dw[(size_t) e * dbytes], hp, nt, yp, r0, r1); + }); + if (r > 0) best = std::min(best, secs(t0)); + } + return best; + }; + const double gb = (double) ne * ebytes / 1e9; + std::vector y0; + double t0s = 0; + int rc = 0; + for (int mode : {0, 2, 1}) { + const double t = pass(mode); + if (mode == 0) { y0 = y; t0s = t; } + const bool same = std::memcmp(y0.data(), y.data(), y.size() * 4) == 0; + std::printf("expert gate/up %d down %d nt %d threads %d: %d experts, %.2f GB | mode %s %.3f s = %.1f GB/s (x%.2f) %s\n", tg, td, nt, + threads, ne, gb, mode == 0 ? "ggml " : mode == 2 ? "auto " : "own ", t, gb / t, t0s / t, same ? "bit-identical" : "DIFFERENT"); + if (!same) rc = 1; + } + return rc; +} + int main(int argc, char** argv) { int type = 18, threads = 32, nt = 1, reps = 3; long mb = 3072; - bool exact = false; + bool exact = false, expert = false; + int dtype = -1; for (int i = 1; i < argc; ++i) { std::string a = argv[i]; auto val = [&] { return i + 1 < argc ? std::atol(argv[++i]) : 0L; }; @@ -39,7 +133,10 @@ int main(int argc, char** argv) { else if (a == "--nt") nt = (int) val(); else if (a == "--reps") reps = (int) val(); else if (a == "--exact") exact = true; + else if (a == "--expert") expert = true; + else if (a == "--dtype") dtype = (int) val(); } + if (expert) return run_expert(type, dtype < 0 ? type : dtype, threads, mb, nt, reps); ggml_cpu_init(); const int n = 5120; // a gate / up row IqCpu f; @@ -57,7 +154,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);