okami.mom/strata

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

Niko1221Int3 fix (test programs only; four were already broken on MSVC in v0.2.9, gr_v3_screen is new): setenv / unsetenv / __builtin_popcount shims so a Windows build with tests ON compiles0d2ee8def

main
6.7 KiB148 linesraw
1// bench/gdn_chunk_bench.cpp - v100/gdn-chunk: the chunked GDN prompt recurrence against the token recurrence,
2// real GDN geometry (S=128, 16 q/k heads, 48 v heads, C=10240), CUDA events.
3//
4// One call of `gdn_recurrence` per side (the whole prompt chunk for one GDN layer: conv output -> out norm),
5// A/B'd with STRATA_GDN_CHUNK=0 / =1 so the dispatch, the tail and the norm are timed exactly as the engine
6// runs them.  The two paths are not bit-exact (the chunked one reorders the sums); the bench reports the max
7// relative difference it saw so a silent divergence cannot hide behind a speedup.
8//
9//   usage: gdn_chunk_bench [T ...]        (default 1024 4096 8192)
10#include "strata/prefill/kernels.hpp"
11
12#include <cuda_runtime.h>
13
14#include <cstdint>
15#include <cstdio>
16#include <cstdlib>
17#include <cstring>
18#include <vector>
19
20#if defined(_WIN32)   // MSVC has no setenv / unsetenv
21#define setenv(k, v, o) _putenv_s((k), (v))
22#define unsetenv(k) _putenv_s((k), "")
23#endif
24
25namespace {
26
27constexpr int S = 128, HV = 48, C = 10240;
28
29void check(cudaError_t e, const char* what) {
30    if (e != cudaSuccess) {
31        std::fprintf(stderr, "%s: %s\n", what, cudaGetErrorString(e));
32        std::exit(1);
33    }
34}
35
36struct Rng {
37    uint64_t s = 12345;
38    float uni() {
39        s = s * 6364136223846793005ULL + 1442695040888963407ULL;
40        return (float) ((s >> 40) * (1.0 / 16777216.0));
41    }
42};
43
44float timed(const char* mode, const char* warp, float* st, float* st0, float* h, float* g, float* b, float* z,
45            float* ga, float* y, uint16_t* y16, int64_t T, int iters) {
46    setenv("STRATA_GDN_CHUNK", mode, 1);
47    setenv("STRATA_GDN_REC_WARP", warp, 1);
48    cudaEvent_t e0, e1;
49    check(cudaEventCreate(&e0), "event");
50    check(cudaEventCreate(&e1), "event");
51    check(cudaMemcpy(st, st0, (size_t) S * HV * S * 4, cudaMemcpyDeviceToDevice), "reset");
52    strata::prefill::gdn_recurrence(st, h, g, b, z, ga, 1e-5f, y, y16, T, nullptr);
53    check(cudaDeviceSynchronize(), "warmup");
54    check(cudaEventRecord(e0), "record");
55    for (int i = 0; i < iters; ++i) {
56        check(cudaMemcpy(st, st0, (size_t) S * HV * S * 4, cudaMemcpyDeviceToDevice), "reset");
57        strata::prefill::gdn_recurrence(st, h, g, b, z, ga, 1e-5f, y, y16, T, nullptr);
58    }
59    check(cudaEventRecord(e1), "record");
60    check(cudaEventSynchronize(e1), "sync");
61    float ms = 0;
62    check(cudaEventElapsedTime(&ms, e0, e1), "elapsed");
63    cudaEventDestroy(e0);
64    cudaEventDestroy(e1);
65    return ms / iters;
66}
67
68}  // namespace
69
70int main(int argc, char** argv) {
71    std::vector<int64_t> ts;
72    for (int i = 1; i < argc; ++i) ts.push_back(std::atoll(argv[i]));
73    if (ts.empty()) ts = {1024, 4096, 8192};
74    int64_t tmax = 0;
75    for (int64_t t : ts) tmax = t > tmax ? t : tmax;
76
77    int dev = 0;
78    cudaDeviceProp p{};
79    check(cudaGetDevice(&dev), "device");
80    check(cudaGetDeviceProperties(&p, dev), "props");
81    const int cc = p.major * 10 + p.minor;
82    std::printf("%s (cc %d)  GDN prompt recurrence: chunked vs token-by-token\n", p.name, cc);
83
84    Rng rng;
85    const size_t n_h = (size_t) tmax * C, n_y = (size_t) tmax * HV * S, n_g = (size_t) tmax * HV;
86    float *d_h, *d_y, *d_z, *d_g, *d_b, *d_ga, *d_st, *d_st0;
87    uint16_t* d_y16;
88    check(cudaMalloc(&d_h, n_h * 4), "malloc");
89    check(cudaMalloc(&d_y, n_y * 4), "malloc");
90    check(cudaMalloc(&d_z, n_y * 4), "malloc");
91    check(cudaMalloc(&d_g, n_g * 4), "malloc");
92    check(cudaMalloc(&d_b, n_g * 4), "malloc");
93    check(cudaMalloc(&d_ga, S * 4), "malloc");
94    check(cudaMalloc(&d_st, (size_t) S * HV * S * 4), "malloc");
95    check(cudaMalloc(&d_st0, (size_t) S * HV * S * 4), "malloc");
96    check(cudaMalloc(&d_y16, n_y * 2), "malloc");
97
98    std::vector<float> tmp((size_t) tmax * C);
99    for (size_t i = 0; i < tmp.size(); ++i) tmp[i] = rng.uni() - 0.5f;
100    // q/k rows are L2-normalised rows (as gdn_conv leaves them); v rows stay O(1).  A degenerate near-zero
101    // v would make the out-norm amplify rounding noise by 1/sqrt(eps) and the diff column meaningless.
102    for (int64_t t = 0; t < tmax; ++t)
103        for (int row = 0; row < 2 * 16; ++row) {
104            float* r = tmp.data() + (size_t) t * C + row * S;
105            double ss = 0;
106            for (int i = 0; i < S; ++i) ss += (double) r[i] * r[i];
107            const float nrm = (float) (1.0 / std::sqrt(ss + 1e-5));
108            for (int i = 0; i < S; ++i) r[i] *= nrm;
109        }
110    check(cudaMemcpy(d_h, tmp.data(), n_h * 4, cudaMemcpyHostToDevice), "H2D");
111    tmp.resize(n_g);
112    for (size_t i = 0; i < n_g; ++i) tmp[i] = -0.02f - 0.3f * rng.uni();
113    check(cudaMemcpy(d_g, tmp.data(), n_g * 4, cudaMemcpyHostToDevice), "H2D");
114    for (size_t i = 0; i < n_g; ++i) tmp[i] = 1.0f / (1.0f + std::exp(-(4.0f * rng.uni() - 2.0f)));
115    check(cudaMemcpy(d_b, tmp.data(), n_g * 4, cudaMemcpyHostToDevice), "H2D");
116    tmp.resize(n_y);
117    for (size_t i = 0; i < n_y; ++i) tmp[i] = 2.0f * rng.uni() - 1.0f;
118    check(cudaMemcpy(d_z, tmp.data(), n_y * 4, cudaMemcpyHostToDevice), "H2D");
119    tmp.resize(S);
120    for (int i = 0; i < S; ++i) tmp[i] = 0.5f + 2.0f * rng.uni();
121    check(cudaMemcpy(d_ga, tmp.data(), S * 4, cudaMemcpyHostToDevice), "H2D");
122    tmp.resize((size_t) S * HV * S);
123    for (size_t i = 0; i < tmp.size(); ++i) tmp[i] = 0.1f * (rng.uni() - 0.5f);
124    check(cudaMemcpy(d_st0, tmp.data(), tmp.size() * 4, cudaMemcpyHostToDevice), "H2D");
125
126    std::printf("%8s %12s %12s %12s %12s %12s   %s\n", "T", "pipe(rec)", "warp(rec)", "fast(rec)", "chunk FMA",
127                "chunk wmma", "max abs diff vs pipe");
128    for (int64_t T : ts) {
129        const int iters = T >= 4096 ? 5 : 20;
130        const float rec = timed("0", "0", d_st, d_st0, d_h, d_g, d_b, d_z, d_ga, d_y, d_y16, T, iters);
131        std::vector<float> y0((size_t) T * HV * S);
132        check(cudaMemcpy(y0.data(), d_y, y0.size() * 4, cudaMemcpyDeviceToHost), "D2H");
133        const float wrp = timed("0", "1", d_st, d_st0, d_h, d_g, d_b, d_z, d_ga, d_y, d_y16, T, iters);
134        const float fst = timed("0", "2", d_st, d_st0, d_h, d_g, d_b, d_z, d_ga, d_y, d_y16, T, iters);
135        const float fma = timed("2", "0", d_st, d_st0, d_h, d_g, d_b, d_z, d_ga, d_y, d_y16, T, iters);
136        const float ch = timed("1", "0", d_st, d_st0, d_h, d_g, d_b, d_z, d_ga, d_y, d_y16, T, iters);
137        std::vector<float> y1((size_t) T * HV * S);
138        check(cudaMemcpy(y1.data(), d_y, y1.size() * 4, cudaMemcpyDeviceToHost), "D2H");
139        double absd = 0;
140        for (size_t i = 0; i < y0.size(); ++i) {
141            const double d = std::fabs((double) y1[i] - (double) y0[i]);
142            if (d > absd) absd = d;
143        }
144        std::printf("%8lld %11.3fms %11.3fms %11.3fms %11.3fms %11.3fms   %.2e (wmma vs pipe)\n", (long long) T,
145                    rec, wrp, fst, fma, ch, absd);
146    }
147    return 0;
148}