okami.mom/strata

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

Niko1221tests: gemm_f16_io_parity builds its BF16 weights by hand and refuses an all-zero reference (#835)1e3a9a470

main
5.4 KiB112 linesraw
1// #835: the FP16-in / FP16-out prompt GEMMs (Gemm::set_f16_io, STRATA_HIP_PROMPT_F16=1) against a CPU reference.
2// f16() and bf16() (its weights BF16, its activations the FP16 image) write FP16 into Y's own rows and widen them in
3// place; the columns past N must stay untouched and a beta = 1 call must keep the ordinary FP32 path.  The tolerance
4// is FP16's: one rounding of each output.
5#include <hip/hip_runtime.h>
6#include <hip/hip_bfloat16.h>
7#include <hip/hip_fp16.h>
8#include "strata/prefill/gemm.hpp"
9#include <algorithm>
10#include <cmath>
11#include <cstdio>
12#include <cstdlib>
13#include <cstring>
14#include <random>
15#include <string>
16#include <vector>
17
18#define CHECK(call) do { const auto e = (call); if (e != hipSuccess) { \
19    std::fprintf(stderr, "%s: %s\n", #call, hipGetErrorString(e)); std::exit(2); } } while (0)
20
21struct Buffer {
22    void* p = nullptr;
23    explicit Buffer(size_t bytes) { CHECK(hipMalloc(&p, bytes)); }
24    ~Buffer() { if (p) (void) hipFree(p); }
25};
26
27// bf16_w: the weights are BF16 (the bf16() entry); the activations are always the FP16 image on this path.
28bool run(strata::prefill::Gemm& gemm, hipStream_t stream, bool bf16_w, int t, int n, int k, int ldy, float beta) {
29    std::mt19937 rng(835 + t + n + (bf16_w ? 1 : 0));
30    std::uniform_real_distribution<float> dist(-0.25f, 0.25f);
31    std::vector<uint16_t> x((size_t) t * k), w((size_t) n * k);
32    std::vector<float> xf(x.size()), wf(w.size());
33    for (size_t i = 0; i < x.size(); ++i) {
34        const __half v = __float2half_rn(dist(rng));
35        std::memcpy(&x[i], &v, 2);
36        xf[i] = __half2float(v);
37    }
38    for (size_t i = 0; i < w.size(); ++i) {
39        if (bf16_w) {
40            // round to nearest even by hand (hip_bfloat16's host constructor gave zeros on ROCm 7.14)
41            const float f = dist(rng);
42            uint32_t bits;
43            std::memcpy(&bits, &f, 4);
44            bits += 0x7fffu + ((bits >> 16) & 1u);
45            w[i] = (uint16_t) (bits >> 16);
46            const uint32_t wide = static_cast<uint32_t>(w[i]) << 16;
47            std::memcpy(&wf[i], &wide, 4);
48        } else {
49            const __half v = __float2half_rn(dist(rng));
50            std::memcpy(&w[i], &v, 2);
51            wf[i] = __half2float(v);
52        }
53    }
54    constexpr int offset = 5;
55    std::vector<float> initial(offset + (size_t) t * ldy + 8, -777.25f);
56    for (int r = 0; r < t; ++r)
57        for (int c = 0; c < n; ++c) initial[offset + (size_t) r * ldy + c] = (r + c) % 17 * 0.03125f;
58    Buffer dx(x.size() * 2), dw(w.size() * 2), dy(initial.size() * 4);
59    CHECK(hipMemcpyAsync(dx.p, x.data(), x.size() * 2, hipMemcpyHostToDevice, stream));
60    CHECK(hipMemcpyAsync(dw.p, w.data(), w.size() * 2, hipMemcpyHostToDevice, stream));
61    CHECK(hipMemcpyAsync(dy.p, initial.data(), initial.size() * 4, hipMemcpyHostToDevice, stream));
62    if (bf16_w) gemm.bf16((const uint16_t*) dx.p, (const uint16_t*) dw.p, (float*) dy.p + offset, t, n, k, ldy, beta);
63    else gemm.f16((const uint16_t*) dx.p, (const uint16_t*) dw.p, (float*) dy.p + offset, t, n, k, ldy, beta);
64    CHECK(hipStreamSynchronize(stream));
65    std::vector<float> got(initial.size());
66    CHECK(hipMemcpy(got.data(), dy.p, got.size() * 4, hipMemcpyDeviceToHost));
67    std::vector<bool> active(got.size());
68    double diff2 = 0, ref2 = 0, maximum = 0;
69    bool ok = true;
70    for (int r = 0; r < t; ++r)
71        for (int c = 0; c < n; ++c) {
72            const size_t j = offset + (size_t) r * ldy + c;
73            active[j] = true;
74            double ref = beta * initial[j];
75            for (int i = 0; i < k; ++i) ref += (double) xf[(size_t) r * k + i] * wf[(size_t) c * k + i];
76            const double d = (double) got[j] - ref;
77            ok = ok && std::isfinite(got[j]);
78            diff2 += d * d;
79            ref2 += ref * ref;
80            maximum = std::max(maximum, std::abs(d));
81        }
82    for (size_t j = 0; j < got.size(); ++j)
83        if (!active[j]) ok = ok && got[j] == initial[j];
84    const double rel = std::sqrt(diff2 / std::max(ref2, 1e-300));
85    // one FP16 rounding of the output (beta = 0), or the ordinary FP32 path (beta = 1)
86    ok = ok && rel < (beta == 0.0f ? 2e-3 : 1e-4);
87    ok = ok && ref2 > 0;   // an all-zero reference would pass anything
88    std::printf("%s %s T=%d N=%d K=%d ldy=%d beta=%.1f rel_l2=%.3g max_abs=%.3g ref_rms=%.3g\n", ok ? "PASS" : "FAIL",
89                bf16_w ? "BF16W" : "F16", t, n, k, ldy, beta, rel, maximum, std::sqrt(ref2 / ((double) t * n)));
90    return ok;
91}
92
93int main() {
94    setvbuf(stdout, nullptr, _IONBF, 0);
95    hipStream_t stream;
96    CHECK(hipStreamCreateWithFlags(&stream, hipStreamNonBlocking));
97    bool ok = true;
98    {
99        strata::prefill::Gemm gemm;
100        std::string error;
101        if (!gemm.init(stream, 2560 * 640, error)) { std::fprintf(stderr, "%s\n", error.c_str()); return 2; }
102        gemm.set_f16_io(true);
103        ok = run(gemm, stream, false, 32, 640, 2560, 648, 0) && ok;     // N < ldy: the columns past N stay untouched
104        ok = run(gemm, stream, false, 64, 2560, 640, 2560, 0) && ok;
105        ok = run(gemm, stream, false, 17, 96, 2560, 96, 0) && ok;       // T and N not multiples of the block
106        ok = run(gemm, stream, true, 16, 96, 2560, 100, 0) && ok;       // BF16 weights through the FP16 scratch
107        ok = run(gemm, stream, true, 48, 640, 2560, 640, 0) && ok;
108        ok = run(gemm, stream, false, 64, 640, 640, 640, 1) && ok;      // beta = 1 keeps the FP32 path
109    }
110    CHECK(hipStreamDestroy(stream));
111    return ok ? 0 : 1;
112}