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)

This commit is contained in:
Niko1221 committed 2026-10-30 15:41:53 +01:00
1 parent 1cfffa2ede
commit b3bc477512
3 files changed
+50

No files matched your search

+3
View File
@@ -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)
+30
View File
@@ -19,6 +19,8 @@
#include <atomic>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <type_traits>
@@ -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<int32_t> 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<int32_t> 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;
+17
View File
@@ -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<float> 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); }
}