okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221prefill: 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 hipBLASLte51fa6c36

main
4.7 KiB102 linesraw
1// STRATA_HCD_EXACT's kernel against hipBLASLt, bit for bit.  Gemm::bf16_hcd_exact (the hyper-connection read's down
2// projection, N 320 x K 10240, BF16 in, FP32 out) reproduces hipBLASLt solutions 1176 / 1177's k order, StaggerU
3// included; it launches only when the tuning table makes hipBLASLt take one of them for the shape.  This test runs both
4// for several chunk sizes T on the same random inputs (the activations at the token stride the engine uses, K + 64) and
5// requires every output to be equal: a hipBLASLt update that renumbers its solutions or changes their order shows up
6// here as a mismatch (or as the kernel declining, which is reported).
7//
8// Needs STRATA_HIPBLASLT_TUNING (the gfx1151 table in tools/hip) on a gfx11 card: exit 77 (SKIP) otherwise, or when no
9// T is taken by the kernel at all.
10#include <cuda_runtime.h>
11
12#include "strata/kernels/bf16_bits.hpp"
13#include "strata/prefill/gemm.hpp"
14
15#include <cstdint>
16#include <cstdio>
17#include <cstdlib>
18#include <cstring>
19#include <random>
20#include <string>
21#include <vector>
22
23#define HIP_CHECK(call) do { \
24    const hipError_t status_ = (call); \
25    if (status_ != hipSuccess) { \
26        std::fprintf(stderr, "HIP failure at %s:%d: %s: %s\n", __FILE__, __LINE__, #call, hipGetErrorString(status_)); \
27        std::exit(2); \
28    } \
29} while (0)
30
31int main() {
32    if (std::getenv("STRATA_HIPBLASLT_TUNING") == nullptr) {
33        std::printf("SKIP: STRATA_HIPBLASLT_TUNING is not set (the kernel runs only where the table picks solution 1176 / 1177)\n");
34        return 77;
35    }
36    hipDeviceProp_t prop{};
37    int dev = 0;
38    HIP_CHECK(hipGetDevice(&dev));
39    HIP_CHECK(hipGetDeviceProperties(&prop, dev));
40    if (std::strncmp(prop.gcnArchName, "gfx11", 5) != 0) {
41        std::printf("SKIP: %s is not a gfx11 card\n", prop.gcnArchName);
42        return 77;
43    }
44    constexpr int64_t N = 320, K = 10240, LDX = K + 64;
45    const int64_t Ts[] = {64, 1000, 2047, 2048, 3333, 4096, 6000, 8191, 16384};
46    hipStream_t stream;
47    HIP_CHECK(hipStreamCreate(&stream));
48    strata::prefill::Gemm gemm;
49    std::string err;
50    if (!gemm.init(stream, 1 << 20, err)) {
51        std::fprintf(stderr, "Gemm::init: %s\n", err.c_str());
52        return 2;
53    }
54    std::mt19937 rng(12345);
55    std::uniform_real_distribution<float> dist(-1.0f, 1.0f);
56    std::vector<uint16_t> w((size_t) N * K);
57    for (auto& v : w) v = strata::kernels::bf16_from_f32(0.05f * dist(rng));
58    uint16_t* dw = nullptr;
59    HIP_CHECK(hipMalloc((void**) &dw, w.size() * 2));
60    HIP_CHECK(hipMemcpy(dw, w.data(), w.size() * 2, hipMemcpyHostToDevice));
61    int ran = 0, bad = 0, declined = 0;
62    for (const int64_t T : Ts) {
63        std::vector<uint16_t> x((size_t) T * LDX, 0);
64        for (int64_t t = 0; t < T; ++t)
65            for (int64_t k = 0; k < K; ++k) x[(size_t) t * LDX + k] = strata::kernels::bf16_from_f32(dist(rng));
66        uint16_t* dx = nullptr;
67        float *dy_ref = nullptr, *dy = nullptr;
68        HIP_CHECK(hipMalloc((void**) &dx, x.size() * 2));
69        HIP_CHECK(hipMalloc((void**) &dy_ref, (size_t) T * N * 4));
70        HIP_CHECK(hipMalloc((void**) &dy, (size_t) T * N * 4));
71        HIP_CHECK(hipMemcpy(dx, x.data(), x.size() * 2, hipMemcpyHostToDevice));
72        HIP_CHECK(hipMemset(dy_ref, 0xff, (size_t) T * N * 4));
73        HIP_CHECK(hipMemset(dy, 0xee, (size_t) T * N * 4));
74        gemm.bf16(dx, dw, dy_ref, T, N, K, 0, 0.0f, LDX);   // hipBLASLt (the table's solution), the same stride
75        HIP_CHECK(hipStreamSynchronize(stream));
76        if (!gemm.bf16_hcd_exact(dx, LDX, dw, dy, T, N, K)) {
77            std::printf("T %5lld: the kernel declines (the table does not pick solution 1176 / 1177 here): hipBLASLt runs it\n", (long long) T);
78            ++declined;
79        } else {
80            HIP_CHECK(hipStreamSynchronize(stream));
81            std::vector<float> a((size_t) T * N), b((size_t) T * N);
82            HIP_CHECK(hipMemcpy(a.data(), dy_ref, a.size() * 4, hipMemcpyDeviceToHost));
83            HIP_CHECK(hipMemcpy(b.data(), dy, b.size() * 4, hipMemcpyDeviceToHost));
84            int64_t diff = 0;
85            for (size_t i = 0; i < a.size(); ++i)
86                if (std::memcmp(&a[i], &b[i], 4) != 0) ++diff;
87            std::printf("T %5lld: %lld of %lld outputs differ\n", (long long) T, (long long) diff, (long long) a.size());
88            if (diff) ++bad;
89            ++ran;
90        }
91        HIP_CHECK(hipFree(dx));
92        HIP_CHECK(hipFree(dy_ref));
93        HIP_CHECK(hipFree(dy));
94    }
95    HIP_CHECK(hipFree(dw));
96    if (ran == 0) {
97        std::printf("SKIP: the table sends no tested T to solution 1176 / 1177\n");
98        return 77;
99    }
100    std::printf("%s: %d chunk sizes compared, %d declined, %d with differences\n", bad ? "FAIL" : "PASS", ran, declined, bad);
101    return bad ? 1 : 0;
102}