okami.mom/strata

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

Niko1221Trial2 merge f-042-strix-pf (66545a497)3fe5edd31

main
10.7 KiB233 linesraw
1// src/prefill/pf_cover_test.cu - FULL-OUTPUT coverage check of the gfx11 prompt GEMMs (wmma_gemm.cu): every output element is
2// poisoned with NaN before the launch and must be written (finite) after it, and a stride-sampled set of rows (every
3// 37th row, the first, the last 130, all columns) is compared with an FP64 host reference.  It guards against the
4// failure class halo-box/strix-llama.cpp #213 fixed in its own WMMA kernels: a tile variant that covers only part of its
5// output (a skipped second 16-token fragment, a 128-column tile that stops at 64) - which a few-row parity sample at a
6// shape that picks another variant does not see.  Covered:
7//   strata_wmma_gemm_f16 / _bf16       the 16x16 (1 wave) and 64x64 (4 waves) kernels       (T, N, K from tails to 8191)
8//   strata_pf_gemm_f16_ld              kernel_bk64 (N >= 1024), kernel<256,64>, kernel<128,32> (N < 1024), padded strides
9//   strata_pf_hcdown_bf16              the HC down + inject GEMM (nd 320 + ni 4, K 10240)
10//   strata_pf_hcdown_exact_bf16        the hipBLASLt-order HC down GEMM
11// Exit 77 without a gfx11 GPU.   pf_cover_test           the checks (a few minutes of GPU time at most)
12#include "wmma_gemm.h"
13
14#include <cuda_runtime.h>
15
16#include <cmath>
17#include <cstdint>
18#include <cstdio>
19#include <cstring>
20#include <random>
21#include <vector>
22
23namespace {
24
25bool ck(cudaError_t e, const char* what) {
26    if (e != cudaSuccess) std::printf("FAIL: %s: %s\n", what, cudaGetErrorString(e));
27    return e == cudaSuccess;
28}
29uint16_t f16_bits(float f) {
30    _Float16 h = (_Float16) f;
31    uint16_t u;
32    std::memcpy(&u, &h, 2);
33    return u;
34}
35float f16_val(uint16_t u) {
36    _Float16 h;
37    std::memcpy(&h, &u, 2);
38    return (float) h;
39}
40uint16_t bf16_bits(float f) {
41    uint32_t u;
42    std::memcpy(&u, &f, 4);
43    u += 0x7fffu + ((u >> 16) & 1u);
44    return (uint16_t) (u >> 16);
45}
46float bf16_val(uint16_t h) {
47    const uint32_t u = (uint32_t) h << 16;
48    float f;
49    std::memcpy(&f, &u, 4);
50    return f;
51}
52
53struct Mat {
54    std::vector<uint16_t> bits;   // rows x ld
55    std::vector<float> val;       // rows x ld (the value the bits stand for)
56    uint16_t* dev = nullptr;
57    int64_t rows = 0, ld = 0;
58};
59Mat make(int64_t rows, int64_t cols, int64_t ld, bool bf16, std::mt19937& rng, float scale) {
60    Mat m;
61    m.rows = rows;
62    m.ld = ld;
63    m.bits.assign((size_t) rows * ld, 0);
64    m.val.assign((size_t) rows * ld, 0.0f);
65    std::normal_distribution<float> nd(0.0f, scale);
66    for (int64_t r = 0; r < rows; ++r)
67        for (int64_t c = 0; c < cols; ++c) {
68            const float x = nd(rng);
69            const uint16_t b = bf16 ? bf16_bits(x) : f16_bits(x);
70            m.bits[(size_t) r * ld + c] = b;
71            m.val[(size_t) r * ld + c] = bf16 ? bf16_val(b) : f16_val(b);
72        }
73    if (!ck(cudaMalloc(&m.dev, m.bits.size() * 2), "malloc")) std::exit(2);
74    ck(cudaMemcpy(m.dev, m.bits.data(), m.bits.size() * 2, cudaMemcpyHostToDevice), "up");
75    return m;
76}
77
78struct Result {
79    int64_t unwritten = 0, bad = 0, checked = 0;
80    double worst = 0;
81};
82
83// Y [T x ldy], columns [c0, c0 + n) of row t from X rows and W rows
84Result check_out(const std::vector<float>& y, int64_t T, int64_t ldy, const Mat& X, const Mat& W, int64_t w0, int64_t n,
85                 int64_t K, int64_t c0) {
86    Result r;
87    for (int64_t t = 0; t < T; ++t)
88        for (int64_t j = 0; j < n; ++j) {
89            const float v = y[(size_t) t * ldy + c0 + j];
90            if (!std::isfinite(v)) ++r.unwritten;
91        }
92    auto row_ok = [&](int64_t t) {
93        double scale = 0;
94        std::vector<double> ref((size_t) n);
95        for (int64_t j = 0; j < n; ++j) {
96            double s = 0, a = 0;
97            for (int64_t k = 0; k < K; ++k) {
98                const double p = (double) X.val[(size_t) t * X.ld + k] * (double) W.val[(size_t) (w0 + j) * W.ld + k];
99                s += p;
100                a += std::fabs(p);
101            }
102            ref[(size_t) j] = s;
103            scale = std::max(scale, a);
104        }
105        for (int64_t j = 0; j < n; ++j) {
106            const double d = std::fabs((double) y[(size_t) t * ldy + c0 + j] - ref[(size_t) j]) / (scale + 1e-30);
107            r.worst = std::max(r.worst, d);
108            if (!(d < 2e-4)) ++r.bad;   // FP32 accumulate over K: ~1e-6 of the absolute sum, 2e-4 leaves room for any order
109            ++r.checked;
110        }
111    };
112    for (int64_t t = 0; t < T; t += 37) row_ok(t);
113    for (int64_t t = std::max<int64_t>(0, T - 130); t < T; ++t) row_ok(t);
114    return r;
115}
116
117int fails = 0;
118void report(const char* what, int64_t T, int64_t N, int64_t K, const Result& r) {
119    const bool ok = r.unwritten == 0 && r.bad == 0;
120    if (!ok) ++fails;
121    std::printf("%-26s T %5lld N %5lld K %5lld  unwritten %lld  bad %lld of %lld  worst rel %.2e  %s\n", what, (long long) T,
122                (long long) N, (long long) K, (long long) r.unwritten, (long long) r.bad, (long long) r.checked, r.worst,
123                ok ? "OK" : "FAIL");
124}
125
126}  // namespace
127
128int main() {
129    int ndev = 0;
130    if (cudaGetDeviceCount(&ndev) != cudaSuccess || ndev == 0) {
131        std::printf("SKIP: no GPU\n");
132        return 77;
133    }
134    std::mt19937 rng(777);
135    float* dy = nullptr;
136    const size_t YMAX = (size_t) 8191 * 4128;   // T x ldy at the largest shapes
137    if (!ck(cudaMalloc(&dy, YMAX * 4), "malloc y")) return 2;
138    std::vector<float> y;
139    auto fetch = [&](int64_t T, int64_t ldy) {
140        y.resize((size_t) T * ldy);
141        ck(cudaDeviceSynchronize(), "sync");
142        ck(cudaMemcpy(y.data(), dy, y.size() * 4, cudaMemcpyDeviceToHost), "down");
143    };
144    bool any_ran = false;
145
146    // ---- the generic WMMA GEMMs (T x K) . (N x K)^T, FP16 and BF16
147    struct S { int64_t T, N, K; };
148    const S generic[] = {{1, 16, 32}, {17, 40, 64}, {31, 31 + 1, 48}, {32, 32, 16}, {33, 96, 128}, {100, 130, 256},
149                         {513, 200, 192}, {1000, 576, 512}, {4093, 2560, 256}, {8191, 512, 128}};
150    for (bool bf : {false, true}) {
151        for (const S& s : generic) {
152            Mat X = make(s.T, s.K, s.K, bf, rng, 1.0f), W = make(s.N, s.K, s.K, bf, rng, 1.0f);
153            ck(cudaMemset(dy, 0xff, (size_t) s.T * s.N * 4), "poison");
154            const bool ran = bf ? strata_wmma_gemm_bf16(X.dev, W.dev, dy, s.T, s.N, s.K, s.N, 0.0f, nullptr)
155                                : strata_wmma_gemm_f16(X.dev, W.dev, dy, s.T, s.N, s.K, s.N, 0.0f, nullptr);
156            if (!ran) { std::printf("%s T %lld N %lld K %lld: not run (no gfx11 device?)\n", bf ? "wmma bf16" : "wmma f16", (long long) s.T, (long long) s.N, (long long) s.K); continue; }
157            any_ran = true;
158            fetch(s.T, s.N);
159            report(bf ? "wmma_gemm_bf16" : "wmma_gemm_f16", s.T, s.N, s.K, check_out(y, s.T, s.N, X, W, 0, s.N, s.K, 0));
160            cudaFree(X.dev);
161            cudaFree(W.dev);
162        }
163    }
164
165    // ---- the S23 128-row GEMM: bk64 (N >= 1024, K % 64), <256,64> (N >= 1024, K % 32 only), <128,32> (N < 1024); padded strides
166    const S pf[] = {{64, 512, 64}, {100, 640, 128}, {513, 1000 - 1000 + 768, 192}, {1000, 1024, 256}, {4093, 1280, 96},
167                    {4093, 2560, 2048}, {8191, 2560, 2048}, {8191, 1024, 4096}, {8191, 768, 2560}, {8191, 4096, 32 * 5}};
168    for (const S& s : pf) {
169        for (int pad : {0, 64}) {
170            const int64_t ldx = s.K + pad, ldw = s.K + pad;
171            Mat X = make(s.T, s.K, ldx, false, rng, 1.0f), W = make(s.N, s.K, ldw, false, rng, 1.0f);
172            for (float beta : {0.0f, 1.0f}) {
173                // beta 1: Y starts as a known finite value 1.0 so only an unwritten tile (left at 1.0 + nothing) differs
174                if (beta == 0.0f) ck(cudaMemset(dy, 0xff, (size_t) s.T * s.N * 4), "poison");
175                else {
176                    std::vector<float> ones((size_t) s.T * s.N, 1.0f);
177                    ck(cudaMemcpy(dy, ones.data(), ones.size() * 4, cudaMemcpyHostToDevice), "ones");
178                }
179                if (!strata_pf_gemm_f16_ld(X.dev, ldx, W.dev, ldw, dy, s.T, s.N, s.K, s.N, beta, nullptr)) {
180                    std::printf("pf_gemm_f16_ld T %lld N %lld K %lld pad %d: not run\n", (long long) s.T, (long long) s.N, (long long) s.K, pad);
181                    continue;
182                }
183                any_ran = true;
184                fetch(s.T, s.N);
185                if (beta == 1.0f) for (float& v : y) v -= 1.0f;   // the reference has no addend; an unwritten element stays 0.0 and fails the compare
186                char name[64];
187                std::snprintf(name, sizeof name, "pf_gemm pad %d beta %d", pad, (int) beta);
188                report(name, s.T, s.N, s.K, check_out(y, s.T, s.N, X, W, 0, s.N, s.K, 0));
189            }
190            cudaFree(X.dev);
191            cudaFree(W.dev);
192        }
193    }
194
195    // ---- HC down + inject (nd 320 + ni 4, K 10240) and the hipBLASLt-order down (N 320): BF16, token stride 10240 + 64
196    const int64_t HK = 10240, HLD = HK + 64;
197    for (int64_t T : {64, 100, 513, 1000, 4093, 8191}) {
198        Mat X = make(T, HK, HLD, true, rng, 1.0f), Wd = make(320, HK, HK, true, rng, 0.05f), Wi = make(4, HK, HK, true, rng, 0.05f);
199        float *dlo = nullptr, *dinj = nullptr;
200        ck(cudaMalloc(&dlo, (size_t) T * 320 * 4), "malloc");
201        ck(cudaMalloc(&dinj, (size_t) T * 4 * 4), "malloc");
202        ck(cudaMemset(dlo, 0xff, (size_t) T * 320 * 4), "poison");
203        ck(cudaMemset(dinj, 0xff, (size_t) T * 4 * 4), "poison");
204        if (strata_pf_hcdown_bf16(X.dev, HLD, Wd.dev, Wi.dev, 320, 4, dlo, dinj, T, HK, nullptr)) {
205            any_ran = true;
206            ck(cudaDeviceSynchronize(), "sync");
207            std::vector<float> lo((size_t) T * 320), inj((size_t) T * 4);
208            ck(cudaMemcpy(lo.data(), dlo, lo.size() * 4, cudaMemcpyDeviceToHost), "down");
209            ck(cudaMemcpy(inj.data(), dinj, inj.size() * 4, cudaMemcpyDeviceToHost), "down");
210            report("hcdown lo", T, 320, HK, check_out(lo, T, 320, X, Wd, 0, 320, HK, 0));
211            report("hcdown inj", T, 4, HK, check_out(inj, T, 4, X, Wi, 0, 4, HK, 0));
212        } else std::printf("hcdown T %lld: not run\n", (long long) T);
213        ck(cudaMemset(dlo, 0xff, (size_t) T * 320 * 4), "poison");
214        if (strata_pf_hcdown_exact_bf16(X.dev, HLD, Wd.dev, dlo, T, 320, HK, nullptr)) {
215            any_ran = true;
216            ck(cudaDeviceSynchronize(), "sync");
217            std::vector<float> lo((size_t) T * 320);
218            ck(cudaMemcpy(lo.data(), dlo, lo.size() * 4, cudaMemcpyDeviceToHost), "down");
219            report("hcdown_exact", T, 320, HK, check_out(lo, T, 320, X, Wd, 0, 320, HK, 0));
220        } else std::printf("hcdown_exact T %lld: not run\n", (long long) T);
221        cudaFree(dlo);
222        cudaFree(dinj);
223        cudaFree(X.dev);
224        cudaFree(Wd.dev);
225        cudaFree(Wi.dev);
226    }
227    if (!any_ran) {
228        std::printf("SKIP: no gfx11 WMMA device\n");
229        return 77;
230    }
231    std::printf(fails == 0 ? "ALL OK\n" : "%d FAILED\n", fails);
232    return fails == 0 ? 0 : 1;
233}