mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-04 07:18:50 +00:00
perf(cpu): the 4-row dot kernels of the native expert path are opt-in (#6552)
4414c2988 (0.2.20) put the dot_x4 kernels in front of the 1-row dots in native_gu_rows /
native_down_rows (Q4_K, Q5_K, Q6_K, Q8_0, IQ4_XS, IQ3_XXS ...), on by default. They give the
same bits (dot_x4_test) and share the activation loads, which pays with the weights in cache.
A cold expert is read from DRAM, and there the 4-row form is slower. New dot_x4_bench (one
thread, 512 MiB of rows, n = 2560), x4 speed against four 1-row calls:
Ryzen 7 255 (Zen 4, AVX-512 VNNI) Q4_0 0.77 Q8_0 0.42 Q4_K 0.26 Q5_K 0.33 Q6_K 0.24 IQ4_XS 0.27 IQ3_XXS 0.52
Ryzen 5 5600X (Zen 3, AVX2) Q8_0 0.85 Q4_K 0.29 IQ4_XS 0.32
i9-13900K (AVX-VNNI) Q8_0 1.20 Q4_K 0.86 IQ4_XS 0.80
(cache-resident: 0.8 - 1.3 everywhere)
This is the #6552 reporter's Ryzen 7 7700 (STRATA_NO_DOT_X4=1 brought the old speed back).
dot_x4_enabled() (strata/quant/dot_x4_policy.hpp): STRATA_DOT_X4=1 turns the kernels on,
STRATA_NO_DOT_X4 (any value) still turns them off and wins. The answers are the same bits either
way. dot_x4_policy_test covers the rule; dot_x4_bench is registered as an executable only.
This commit is contained in:
1 parent
6e45f7fbd3
commit
66f2ee2b0a
6 files changed
+136
-2
No files matched your search
@@ -487,6 +487,11 @@ if(STRATA_BUILD_TESTS)
|
||||
target_include_directories(low_vram_rules_test PRIVATE include)
|
||||
add_test(NAME low_vram_rules_test COMMAND low_vram_rules_test)
|
||||
|
||||
# the 4-row dot kernels of the native expert path are opt-in (STRATA_DOT_X4=1; #6552): header-only rule
|
||||
add_executable(dot_x4_policy_test src/core/dot_x4_policy_test.cpp)
|
||||
target_include_directories(dot_x4_policy_test PRIVATE include)
|
||||
add_test(NAME dot_x4_policy_test COMMAND dot_x4_policy_test)
|
||||
|
||||
# the serve watchdog's limits (STRATA_WATCHDOG_S / _IO_S) parse without undefined behaviour (#6861: negative seconds in the log)
|
||||
add_executable(watchdog_limits_test src/core/watchdog_limits_test.cpp)
|
||||
target_include_directories(watchdog_limits_test PRIVATE include)
|
||||
|
||||
@@ -166,6 +166,10 @@ if(STRATA_BUILD_TESTS OR STRATA_BUILD_CONVERSATION_TESTS)
|
||||
target_link_libraries(dot_x4_test PRIVATE strata_quant)
|
||||
target_compile_features(dot_x4_test PRIVATE cxx_std_17)
|
||||
add_test(NAME dot_x4_test COMMAND dot_x4_test)
|
||||
# speed of the same kernels, 4-row against four 1-row calls, per type (a measurement, not a test: #6552, STRATA_NO_DOT_X4)
|
||||
add_executable(dot_x4_bench tests/core/dot_x4_bench.cpp)
|
||||
target_link_libraries(dot_x4_bench PRIVATE strata_quant)
|
||||
target_compile_features(dot_x4_bench PRIVATE cxx_std_17)
|
||||
# 3-Tier NVMe streaming primitives: 2-priority chunked I/O scheduler, proj_ready[3], pe128 wave planner, lookahead v2
|
||||
add_executable(ssd_stream_test tests/core/ssd_stream_test.cpp)
|
||||
target_include_directories(ssd_stream_test PRIVATE include)
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
// strata/quant/dot_x4_policy.hpp - whether the native expert path uses the 4-row shared-activation dot kernels (header-only, no CPU needed to test).
|
||||
//
|
||||
// 4414c2988 (0.2.20) put dot_x4 kernels in front of the 1-row dots of the cold-expert path (Q4_K, Q5_K, Q6_K, Q8_0, IQ4_XS, IQ3_XXS ...). They give the
|
||||
// same bits (dot_x4_test) and share the activation loads, which pays when the weights sit in cache. A cold expert is read from DRAM, and there the
|
||||
// 4-row form is slower: dot_x4_bench, one thread, 512 MiB of rows, n = 2560 (x4 speed against four 1-row calls):
|
||||
// Ryzen 7 255 (Zen 4, AVX-512 VNNI): Q4_0 0.77 Q8_0 0.42 Q4_K 0.26 Q5_K 0.33 Q6_K 0.24 IQ4_XS 0.27 IQ3_XXS 0.52 (cache-resident: 0.8 - 1.3)
|
||||
// i9-13900K (AVX-VNNI): Q8_0 1.20 Q4_K 0.86 IQ4_XS 0.80
|
||||
// and a Ryzen 7 7700 user (#6552) got the 0.1.65 speed back with STRATA_NO_DOT_X4=1. So since 0.2.21 the 4-row kernels are opt-in:
|
||||
// STRATA_DOT_X4=1 turns them on, STRATA_NO_DOT_X4 (any value) still turns them off and wins.
|
||||
#pragma once
|
||||
|
||||
namespace strata::quant {
|
||||
|
||||
inline bool dot_x4_enabled(const char* env_on, const char* env_off) noexcept {
|
||||
if (env_off != nullptr) return false;
|
||||
return env_on != nullptr && env_on[0] == '1' && env_on[1] == '\0';
|
||||
}
|
||||
|
||||
} // namespace strata::quant
|
||||
@@ -0,0 +1,21 @@
|
||||
// src/core/dot_x4_policy_test.cpp - the 4-row dot kernels of the native expert path are opt-in (#6552).
|
||||
#include "strata/quant/dot_x4_policy.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
static int fails = 0;
|
||||
#define CHECK(c) do { if (!(c)) { std::printf("FAIL line %d: %s\n", __LINE__, #c); ++fails; } } while (0)
|
||||
|
||||
int main() {
|
||||
using strata::quant::dot_x4_enabled;
|
||||
CHECK(!dot_x4_enabled(nullptr, nullptr)); // the default: off
|
||||
CHECK(dot_x4_enabled("1", nullptr)); // STRATA_DOT_X4=1
|
||||
CHECK(!dot_x4_enabled("0", nullptr));
|
||||
CHECK(!dot_x4_enabled("", nullptr));
|
||||
CHECK(!dot_x4_enabled("11", nullptr));
|
||||
CHECK(!dot_x4_enabled("1", "")); // STRATA_NO_DOT_X4 set (even empty) wins, as before
|
||||
CHECK(!dot_x4_enabled("1", "1"));
|
||||
CHECK(!dot_x4_enabled(nullptr, "1"));
|
||||
if (fails == 0) std::printf("dot_x4_policy_test: ok\n");
|
||||
return fails == 0 ? 0 : 1;
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
// Default keeps the existing dispatch. WindowInvariant preserves that dispatch's single-token arithmetic
|
||||
// while retaining shared weight decoding where the grouped kernel gives the same result.
|
||||
#include "strata/kernels/cpu/native_expert.hpp"
|
||||
#include "strata/quant/dot_x4_policy.hpp"
|
||||
#include "strata/kernels/cpu/expert.hpp"
|
||||
#include "strata/kernels/cpu/iq_avx512.hpp"
|
||||
#include "strata/kernels/cpu/iq_avx2.hpp"
|
||||
@@ -732,7 +733,7 @@ void native_gu_rows_impl(const NativeFmt& f, const uint8_t* blob, const void* co
|
||||
}
|
||||
const Tr dot = traits(f.gu_type);
|
||||
#if defined(STRATA_OWN_CPU_QUANT)
|
||||
static const bool x4_on = std::getenv("STRATA_NO_DOT_X4") == nullptr;
|
||||
static const bool x4_on = strata::quant::dot_x4_enabled(std::getenv("STRATA_DOT_X4"), std::getenv("STRATA_NO_DOT_X4")); // opt-in since 0.2.21 (#6552: slower than the 1-row dots on DRAM-bound experts)
|
||||
const strata::quant::DotX4Kernel* x4 =
|
||||
(x4_on && (Arithmetic == NativeArithmetic::Default || flavour_avx2()))
|
||||
? strata::quant::dot_x4_kernel((strata::quant::Type) f.gu_type, strata::quant::Arith::Avx2)
|
||||
@@ -857,7 +858,7 @@ void native_down_rows_impl(const NativeFmt& f, const uint8_t* blob, const void*
|
||||
}
|
||||
const Tr dot = traits(f.d_type);
|
||||
#if defined(STRATA_OWN_CPU_QUANT)
|
||||
static const bool x4_on = std::getenv("STRATA_NO_DOT_X4") == nullptr;
|
||||
static const bool x4_on = strata::quant::dot_x4_enabled(std::getenv("STRATA_DOT_X4"), std::getenv("STRATA_NO_DOT_X4")); // opt-in since 0.2.21 (#6552: slower than the 1-row dots on DRAM-bound experts)
|
||||
const strata::quant::DotX4Kernel* x4 =
|
||||
(x4_on && (Arithmetic == NativeArithmetic::Default || flavour_avx2()))
|
||||
? strata::quant::dot_x4_kernel((strata::quant::Type) f.d_type, strata::quant::Arith::Avx2)
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// tests/core/dot_x4_bench.cpp - speed of the 4-row shared-activation kernels (dot_x4_kernel, STRATA_NO_DOT_X4 off) against four calls of the
|
||||
// 1-row kernel (STRATA_NO_DOT_X4=1 reaches it) on the same rows, per weight type: one cache-resident block (4 MiB of rows) and one DRAM stream
|
||||
// (512 MiB of rows, the shape of a cold expert read). Prints GB/s and the ratio; always exits 0 (a measurement, not a gate). #6552.
|
||||
#include "strata/quant/dot.hpp"
|
||||
#include "strata/quant/types.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
|
||||
using namespace strata::quant;
|
||||
|
||||
static double now_s() {
|
||||
return std::chrono::duration<double>(std::chrono::steady_clock::now().time_since_epoch()).count();
|
||||
}
|
||||
|
||||
static void bench_type(Type wt, Type at, int n, size_t total_bytes, const char* regime) {
|
||||
const TypeInfo* wi = type_info(wt);
|
||||
const TypeInfo* ai = type_info(at);
|
||||
const DotKernel* k1 = dot_kernel(wt, Arith::Avx2);
|
||||
const DotX4Kernel* k4 = dot_x4_kernel(wt, Arith::Avx2);
|
||||
if (wi == nullptr || ai == nullptr || k1 == nullptr || k4 == nullptr) {
|
||||
std::printf("%-8s %-8s skipped (no x4 kernel on this CPU)\n", wi ? wi->name : "?", regime);
|
||||
return;
|
||||
}
|
||||
const size_t w_row = row_bytes((int) wt, n), a_row = row_bytes((int) at, n);
|
||||
size_t rows = total_bytes / w_row;
|
||||
rows -= rows % 4;
|
||||
std::vector<uint8_t> w(rows * w_row), a(a_row);
|
||||
std::mt19937 rng(1234u ^ (uint32_t) wt);
|
||||
for (auto& b : w) b = (uint8_t) (rng() & 0xFF);
|
||||
for (auto& b : a) b = (uint8_t) (rng() & 0xFF);
|
||||
// activation scales: small normal values so nothing runs through slow denormal / NaN paths
|
||||
if (at == Type::Q8_K) {
|
||||
for (int i = 0; i < n / 256; ++i) {
|
||||
uint8_t* bp = a.data() + (size_t) i * ai->bytes;
|
||||
const float d = 0.01f;
|
||||
std::memcpy(bp, &d, 4);
|
||||
int16_t* bs = (int16_t*) (bp + 4 + 256);
|
||||
for (int g = 0; g < 16; ++g) bs[g] = 0;
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < n / ai->blck; ++i) {
|
||||
const uint16_t h = 0x2C00; // 0.0625
|
||||
std::memcpy(a.data() + (size_t) i * ai->bytes, &h, 2);
|
||||
}
|
||||
}
|
||||
std::vector<float> out(rows);
|
||||
const int passes = total_bytes > (64u << 20) ? 3 : 200;
|
||||
double best1 = 1e30, best4 = 1e30;
|
||||
volatile float sink = 0.f;
|
||||
for (int rep = 0; rep < 4; ++rep) {
|
||||
double t = now_s();
|
||||
for (int p = 0; p < passes; ++p)
|
||||
for (size_t r = 0; r < rows; ++r) k1->fn(n, &out[r], w.data() + r * w_row, a.data());
|
||||
best1 = std::min(best1, now_s() - t);
|
||||
sink += out[0];
|
||||
t = now_s();
|
||||
for (int p = 0; p < passes; ++p)
|
||||
for (size_t r = 0; r < rows; r += 4) k4->fn(n, &out[r], w.data() + r * w_row, w_row, a.data());
|
||||
best4 = std::min(best4, now_s() - t);
|
||||
sink += out[1];
|
||||
}
|
||||
const double gb = (double) passes * (double) rows * (double) w_row / 1e9;
|
||||
std::printf("%-8s %-6s n=%d 1-row (%s) %6.2f GB/s x4 (%s) %6.2f GB/s x4/1-row %.3f\n", wi->name, regime, n, k1->name, gb / best1,
|
||||
k4->name, gb / best4, best1 / best4);
|
||||
}
|
||||
|
||||
int main() {
|
||||
const int n = 2560; // Qwen3.8's hidden size: a gate/up row (a down row has the 512-wide ff)
|
||||
const struct { Type w, a; } types[] = {{Type::Q4_0, Type::Q8_0}, {Type::Q8_0, Type::Q8_0}, {Type::Q4_K, Type::Q8_K}, {Type::Q5_K, Type::Q8_K},
|
||||
{Type::Q6_K, Type::Q8_K}, {Type::IQ4_XS, Type::Q8_K}, {Type::IQ3_XXS, Type::Q8_K}, {Type::IQ3_S, Type::Q8_K}};
|
||||
for (const auto& t : types) {
|
||||
bench_type(t.w, t.a, n, 4u << 20, "cache");
|
||||
bench_type(t.w, t.a, n, 512u << 20, "dram");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user