prefill: STRATA_HCD_EXACT says once when it cannot take the kernel (no tuning table, or a solution other than 1176 / 1177), and hip_prefill_hcd_exact_parity checks the kernel against hipBLASLt

The kernel reproduces one hipBLASLt solution's summation order; the test compares both on the same inputs for T from 64
to 16384 (bitwise) and SKIPs without STRATA_HIPBLASLT_TUNING.
This commit is contained in:
Niko1221 committed 2026-10-05 11:27:31 +02:00
1 parent 6bd7e930da
commit e51fa6c362
3 files changed
+125 -2

No files matched your search

+6
View File
@@ -563,6 +563,12 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP)
target_link_libraries(hip_prefill_wmma_gemm_parity PRIVATE strata_prefill strata_hip_runtime)
add_test(NAME hip_prefill_wmma_gemm_parity COMMAND hip_prefill_wmma_gemm_parity)
list(APPEND _strata_hip_tests hip_prefill_wmma_gemm_parity)
# S (STRATA_HCD_EXACT): the exact HC down kernel against hipBLASLt solution 1176 / 1177 (needs STRATA_HIPBLASLT_TUNING; exit 77 else)
add_executable(hip_prefill_hcd_exact_parity tests/hip/prefill_hcd_exact_parity.cpp)
target_link_libraries(hip_prefill_hcd_exact_parity PRIVATE strata_prefill strata_hip_runtime)
add_test(NAME hip_prefill_hcd_exact_parity COMMAND hip_prefill_hcd_exact_parity)
set_tests_properties(hip_prefill_hcd_exact_parity PROPERTIES SKIP_RETURN_CODE 77)
list(APPEND _strata_hip_tests hip_prefill_hcd_exact_parity)
# S6: the AMD fast router against the portable kernel, bitwise
add_executable(hip_router_fast tests/hip/router_fast.cpp)
target_link_libraries(hip_router_fast PRIVATE strata_kernels strata_hip_runtime)
+17 -2
View File
@@ -477,9 +477,24 @@ constexpr int64_t kXSliceElems = 16ll << 20; // 32 MiB of FP16 activations per
#endif
bool Gemm::bf16_hcd_exact(const uint16_t* X, int64_t ldx, const uint16_t* W, float* Y, int64_t T, int64_t N, int64_t K) {
#if defined(__HIPCC__) && defined(STRATA_HIPBLASLT_AVAILABLE)
if (!hipblaslt_state_ || N != 320 || K != 10240 || T < 1) return false;
static std::atomic<bool> told{false};
if (!hipblaslt_state_ || N != 320 || K != 10240 || T < 1) {
if (N == 320 && K == 10240 && !told.exchange(true))
std::fprintf(stderr, "strata: STRATA_HCD_EXACT: no hipBLASLt tuning table is loaded, so the exact kernel has nothing to match: "
"the hyper-connection down projection stays on hipBLAS\n");
return false;
}
// The kernel copies hipBLASLt solution 1176 / 1177's summation order, which is only true of the library build the
// table was calibrated with: take it only when the table makes hipBLASLt pick one of those two for this T.
const int id = hipblaslt_solution_for(hipblaslt_state_, strata::prefill::hipblaslt::InputType::bf16, T, N, K, N, 0.0f);
if (id != 1176 && id != 1177) return false;
if (id != 1176 && id != 1177) {
if (T >= 4096 && !told.exchange(true)) // (a short chunk may legitimately get another solution: not worth a line)
std::fprintf(stderr, "strata: STRATA_HCD_EXACT: the tuning table gives the hyper-connection down projection (N 320, K 10240) "
"solution %d at T %lld, not 1176 / 1177: hipBLASLt runs it (a changed table or library; "
"tools/hip/gfx1151-hipblaslt-100401.txt is the one measured)\n",
id, (long long) T);
return false;
}
return strata_pf_hcdown_exact_bf16(X, ldx, W, Y, T, N, K, stream_);
#else
(void) X; (void) ldx; (void) W; (void) Y; (void) T; (void) N; (void) K;
+102
View File
@@ -0,0 +1,102 @@
// STRATA_HCD_EXACT's kernel against hipBLASLt, bit for bit. Gemm::bf16_hcd_exact (the hyper-connection read's down
// projection, N 320 x K 10240, BF16 in, FP32 out) reproduces hipBLASLt solutions 1176 / 1177's k order, StaggerU
// included; it launches only when the tuning table makes hipBLASLt take one of them for the shape. This test runs both
// for several chunk sizes T on the same random inputs (the activations at the token stride the engine uses, K + 64) and
// requires every output to be equal: a hipBLASLt update that renumbers its solutions or changes their order shows up
// here as a mismatch (or as the kernel declining, which is reported).
//
// Needs STRATA_HIPBLASLT_TUNING (the gfx1151 table in tools/hip) on a gfx11 card: exit 77 (SKIP) otherwise, or when no
// T is taken by the kernel at all.
#include <cuda_runtime.h>
#include "strata/kernels/bf16_bits.hpp"
#include "strata/prefill/gemm.hpp"
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <random>
#include <string>
#include <vector>
#define HIP_CHECK(call) do { \
const hipError_t status_ = (call); \
if (status_ != hipSuccess) { \
std::fprintf(stderr, "HIP failure at %s:%d: %s: %s\n", __FILE__, __LINE__, #call, hipGetErrorString(status_)); \
std::exit(2); \
} \
} while (0)
int main() {
if (std::getenv("STRATA_HIPBLASLT_TUNING") == nullptr) {
std::printf("SKIP: STRATA_HIPBLASLT_TUNING is not set (the kernel runs only where the table picks solution 1176 / 1177)\n");
return 77;
}
hipDeviceProp_t prop{};
int dev = 0;
HIP_CHECK(hipGetDevice(&dev));
HIP_CHECK(hipGetDeviceProperties(&prop, dev));
if (std::strncmp(prop.gcnArchName, "gfx11", 5) != 0) {
std::printf("SKIP: %s is not a gfx11 card\n", prop.gcnArchName);
return 77;
}
constexpr int64_t N = 320, K = 10240, LDX = K + 64;
const int64_t Ts[] = {64, 1000, 2047, 2048, 3333, 4096, 6000, 8191, 16384};
hipStream_t stream;
HIP_CHECK(hipStreamCreate(&stream));
strata::prefill::Gemm gemm;
std::string err;
if (!gemm.init(stream, 1 << 20, err)) {
std::fprintf(stderr, "Gemm::init: %s\n", err.c_str());
return 2;
}
std::mt19937 rng(12345);
std::uniform_real_distribution<float> dist(-1.0f, 1.0f);
std::vector<uint16_t> w((size_t) N * K);
for (auto& v : w) v = strata::kernels::bf16_from_f32(0.05f * dist(rng));
uint16_t* dw = nullptr;
HIP_CHECK(hipMalloc((void**) &dw, w.size() * 2));
HIP_CHECK(hipMemcpy(dw, w.data(), w.size() * 2, hipMemcpyHostToDevice));
int ran = 0, bad = 0, declined = 0;
for (const int64_t T : Ts) {
std::vector<uint16_t> x((size_t) T * LDX, 0);
for (int64_t t = 0; t < T; ++t)
for (int64_t k = 0; k < K; ++k) x[(size_t) t * LDX + k] = strata::kernels::bf16_from_f32(dist(rng));
uint16_t* dx = nullptr;
float *dy_ref = nullptr, *dy = nullptr;
HIP_CHECK(hipMalloc((void**) &dx, x.size() * 2));
HIP_CHECK(hipMalloc((void**) &dy_ref, (size_t) T * N * 4));
HIP_CHECK(hipMalloc((void**) &dy, (size_t) T * N * 4));
HIP_CHECK(hipMemcpy(dx, x.data(), x.size() * 2, hipMemcpyHostToDevice));
HIP_CHECK(hipMemset(dy_ref, 0xff, (size_t) T * N * 4));
HIP_CHECK(hipMemset(dy, 0xee, (size_t) T * N * 4));
gemm.bf16(dx, dw, dy_ref, T, N, K, 0, 0.0f, LDX); // hipBLASLt (the table's solution), the same stride
HIP_CHECK(hipStreamSynchronize(stream));
if (!gemm.bf16_hcd_exact(dx, LDX, dw, dy, T, N, K)) {
std::printf("T %5lld: the kernel declines (the table does not pick solution 1176 / 1177 here): hipBLASLt runs it\n", (long long) T);
++declined;
} else {
HIP_CHECK(hipStreamSynchronize(stream));
std::vector<float> a((size_t) T * N), b((size_t) T * N);
HIP_CHECK(hipMemcpy(a.data(), dy_ref, a.size() * 4, hipMemcpyDeviceToHost));
HIP_CHECK(hipMemcpy(b.data(), dy, b.size() * 4, hipMemcpyDeviceToHost));
int64_t diff = 0;
for (size_t i = 0; i < a.size(); ++i)
if (std::memcmp(&a[i], &b[i], 4) != 0) ++diff;
std::printf("T %5lld: %lld of %lld outputs differ\n", (long long) T, (long long) diff, (long long) a.size());
if (diff) ++bad;
++ran;
}
HIP_CHECK(hipFree(dx));
HIP_CHECK(hipFree(dy_ref));
HIP_CHECK(hipFree(dy));
}
HIP_CHECK(hipFree(dw));
if (ran == 0) {
std::printf("SKIP: the table sends no tested T to solution 1176 / 1177\n");
return 77;
}
std::printf("%s: %d chunk sizes compared, %d declined, %d with differences\n", bad ? "FAIL" : "PASS", ran, declined, bad);
return bad ? 1 : 0;
}