From b3bc47751212a58baa2a5d0e83bee1e334554656 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Fri, 30 Oct 2026 15:41:53 +0100 Subject: [PATCH] diagnostics: STRATA_QSA_STAT (overlap of consecutive queries' selections), hip_pa_bench target and PA_BENCH_REF (compare two variants of the prompt attention on a recorded selection) --- CMakeLists.txt | 3 +++ src/kernels/cuda/qsa_prompt_attn.cu | 30 +++++++++++++++++++++++++++++ tests/cuda/pa_bench.cpp | 17 ++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index ee7e9f1b..c6800f50 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1098,6 +1098,9 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP) add_test(NAME hip_router_fast COMMAND hip_router_fast) list(APPEND _strata_hip_tests hip_router_fast) # S6: the RDNA4 matrix-core prompt attention (STRATA_HIP_WMMA) against the FP32 kernel and FP64; skips off gfx12 + # the prompt attention on a recorded selection (STRATA_QSA_DUMP), for A/B of kernel variants (STRATA_QSA_UNION=1) + add_executable(hip_pa_bench tests/cuda/pa_bench.cpp) + target_link_libraries(hip_pa_bench PRIVATE strata_kernels strata_hip_runtime) add_executable(hip_prompt_attn_wmma src/kernels/qsa_prompt_attn_parity.cpp) target_link_libraries(hip_prompt_attn_wmma PRIVATE strata_kernels strata_hip_runtime) add_test(NAME hip_prompt_attn_wmma COMMAND hip_prompt_attn_wmma 32768 2048 3) diff --git a/src/kernels/cuda/qsa_prompt_attn.cu b/src/kernels/cuda/qsa_prompt_attn.cu index 82b199b1..3f6ae4ed 100644 --- a/src/kernels/cuda/qsa_prompt_attn.cu +++ b/src/kernels/cuda/qsa_prompt_attn.cu @@ -19,6 +19,8 @@ #include #include +#include +#include #include #include #include @@ -2243,6 +2245,34 @@ bool hip_wmma_usable(int64_t n_q, int64_t cap) { bool launch_wmma(const float* q, const QsaAttnPools& pools, const int32_t* ids, const int32_t* steps, int64_t cap, const QsaShapes& s, float* attn, int64_t n_q, cudaStream_t st) { + { // STRATA_QSA_STAT=1 (diagnostic): how much consecutive queries' selections overlap (the row-union attention's sharing factor) + static const bool stat_on = std::getenv("STRATA_QSA_STAT") != nullptr; + static int stat_calls = 0; + if (stat_on && n_q >= 2048 && stat_calls < 4) { + ++stat_calls; + cudaStreamSynchronize(st); + std::vector h((size_t) (n_q * cap)), hs((size_t) (n_q * kStepCount)); + cudaMemcpy(h.data(), ids, h.size() * 4, cudaMemcpyDeviceToHost); + cudaMemcpy(hs.data(), steps, hs.size() * 4, cudaMemcpyDeviceToHost); + for (int R : {4, 8, 16, 32}) { + double mean_n = 0, mean_u = 0; int64_t groups = 0; + for (int64_t g0 = n_q / 2; g0 + R <= n_q; g0 += 4 * R) { // the second half of the chunk (longer contexts) + std::vector all; + int64_t sum_n = 0; + for (int r = 0; r < R; ++r) { + const int n = hs[(size_t) (g0 + r) * kStepCount + kStepWidth]; + sum_n += n; + all.insert(all.end(), h.begin() + (size_t) (g0 + r) * cap, h.begin() + (size_t) (g0 + r) * cap + n); + } + std::sort(all.begin(), all.end()); + const int64_t u = std::unique(all.begin(), all.end()) - all.begin(); + mean_n += (double) sum_n / R; mean_u += (double) u; ++groups; + } + if (groups) std::fprintf(stderr, "strata qsa stat: call %d n_q %lld cap %lld rows/group %d: mean selection %.0f, mean union %.0f (x%.2f; K/V reads per row %.2fx lower)\n", + stat_calls, (long long) n_q, (long long) cap, R, mean_n / groups, mean_u / groups, mean_u / mean_n, R * mean_n / mean_u); + } + } + } const float scale_log2 = 1.4426950408889634f / sqrtf((float) HD); for (int64_t q0 = 0; q0 < n_q; q0 += 65535) { const int64_t nb = n_q - q0 < 65535 ? n_q - q0 : 65535; diff --git a/tests/cuda/pa_bench.cpp b/tests/cuda/pa_bench.cpp index 7f7215e0..afbf658e 100644 --- a/tests/cuda/pa_bench.cpp +++ b/tests/cuda/pa_bench.cpp @@ -103,6 +103,23 @@ int main(int argc, char** argv) { for (float x : out) ss += (double) x * x; std::printf("prompt attention: median %.3f ms (min %.3f) over %d; output hash %016llx, rms %.6g\n", ms[ms.size() / 2], ms[0], reps, (unsigned long long) h, std::sqrt(ss / (double) out.size())); + if (const char* rf = std::getenv("PA_BENCH_REF")) { // another variant's PA_BENCH_OUT: relative RMS and worst difference + std::vector ref(out.size()); + if (std::FILE* g = std::fopen(rf, "rb")) { + const size_t got = std::fread(ref.data(), 4, ref.size(), g); + std::fclose(g); + if (got == ref.size()) { + double e2 = 0, r2 = 0, worst = 0; + for (size_t i = 0; i < out.size(); ++i) { + const double d = (double) out[i] - ref[i]; + e2 += d * d; r2 += (double) ref[i] * ref[i]; + worst = std::max(worst, std::fabs(d)); + } + std::printf("against %s: relative RMS %.3e, worst |diff| %.3e (reference rms %.4g)\n", rf, std::sqrt(e2 / std::max(r2, 1e-30)), + worst, std::sqrt(r2 / (double) out.size())); + } else std::printf("PA_BENCH_REF %s: short file\n", rf); + } + } if (const char* o = std::getenv("PA_BENCH_OUT")) { // the output, for an accuracy comparison of two variants if (std::FILE* g = std::fopen(o, "wb")) { std::fwrite(out.data(), 4, out.size(), g); std::fclose(g); } }