okami.mom/strata

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

Niko1221Merge branch 'f-split-int' into f-split-vision7c5e84612

main
6.0 KiB126 linesraw
1// tests/hip/gdn_rec_head.cpp - the prompt GDN recurrence's four-lanes-per-column kernel + norm (gdn_recurrence_variant 1)
2// against the column-split kernels + the norm kernel (variant 0, the default before it):
3// y, its FP16 bits and the final state must be BITWISE equal, over chunk lengths around the 8-token staging and a
4// long one, from a nonzero state.  Synthetic inputs in the model's ranges (unit q/k rows, log-decay gates < 0).
5#include "strata/prefill/kernels.hpp"
6
7#include <cuda_runtime.h>
8
9#include <cmath>
10#include <cstdint>
11#include <cstdio>
12#include <cstdlib>
13#include <cstring>
14#include <random>
15#include <vector>
16
17namespace {
18constexpr int S = 128, HK = 16, HV = 48, C = 10240;
19void ck(cudaError_t e, const char* w) {
20    if (e != cudaSuccess) { std::fprintf(stderr, "%s: %s\n", w, cudaGetErrorString(e)); std::exit(2); }
21}
22template <typename T> T* dev(const std::vector<T>& h) {
23    T* d = nullptr;
24    ck(cudaMalloc(&d, h.size() * sizeof(T)), "malloc");
25    ck(cudaMemcpy(d, h.data(), h.size() * sizeof(T), cudaMemcpyHostToDevice), "upload");
26    return d;
27}
28template <typename T> std::vector<T> host(const T* d, size_t n) {
29    std::vector<T> h(n);
30    ck(cudaMemcpy(h.data(), d, n * sizeof(T), cudaMemcpyDeviceToHost), "download");
31    return h;
32}
33}  // namespace
34
35int main() {
36    // variants 0 and 1 are the references here; STRATA_GDN_CHUNKED=1 would turn them into the opt-in chunked recurrence
37    // (tests/hip/gdn_chunked.cpp), whose bits differ
38#ifdef _WIN32
39    _putenv_s("STRATA_GDN_CHUNKED", "");
40#else
41    unsetenv("STRATA_GDN_CHUNKED");
42#endif
43    std::mt19937 rng(11);
44    std::normal_distribution<float> nd(0.f, 1.f);
45    std::uniform_real_distribution<float> ud(0.f, 1.f);
46    int failures = 0;
47    const int64_t lens[] = {1, 7, 8, 9, 17, 100, 2048, 2051};
48    for (const int64_t T : lens) {
49        std::vector<float> h((size_t) T * C), gate((size_t) T * HV), beta((size_t) T * HV), z((size_t) T * HV * S),
50            gamma(S), state((size_t) S * HV * S);
51        for (int64_t t = 0; t < T; ++t) {
52            float* ht = h.data() + t * C;
53            for (int i = 0; i < C; ++i) ht[i] = nd(rng);
54            for (int hh = 0; hh < 2 * HK; ++hh) {   // q and k heads: unit rows, as after gdn_conv's L2 norm
55                double ss = 0;
56                for (int d = 0; d < S; ++d) ss += (double) ht[hh * S + d] * ht[hh * S + d];
57                for (int d = 0; d < S; ++d) ht[hh * S + d] = (float) (ht[hh * S + d] / std::sqrt(ss + 1e-6));
58            }
59        }
60        for (auto& x : gate) x = -2.0f * ud(rng);
61        for (auto& x : beta) x = ud(rng);
62        for (auto& x : z) x = nd(rng);
63        for (auto& x : gamma) x = 0.5f + ud(rng);
64        for (auto& x : state) x = 0.1f * nd(rng);
65        const float* dh = dev(h);
66        const float* dg = dev(gate);
67        const float* db = dev(beta);
68        const float* dz = dev(z);
69        const float* dgm = dev(gamma);
70        std::vector<float> y_ref, st_ref;
71        std::vector<uint16_t> y16_ref;
72        for (int variant : {0, 3, 1, 4, 5}) {
73            float* dst = dev(state);
74            float* dy = nullptr;
75            uint16_t* dy16 = nullptr;
76            ck(cudaMalloc(&dy, (size_t) T * HV * S * 4), "malloc");
77            ck(cudaMalloc(&dy16, (size_t) T * HV * S * 2), "malloc");
78            strata::prefill::gdn_recurrence_variant(variant, dst, dh, dg, db, dz, dgm, 1e-6f, dy, dy16, T, nullptr);
79            ck(cudaDeviceSynchronize(), "run");
80            auto y = host(dy, (size_t) T * HV * S);
81            auto y16 = host(dy16, (size_t) T * HV * S);
82            auto st = host(dst, state.size());
83            if (variant == 0) {
84                y_ref = y; y16_ref = y16; st_ref = st;
85            } else {
86                size_t dy_n = 0, d16 = 0, dsn = 0;
87                for (size_t i = 0; i < y.size(); ++i) dy_n += std::memcmp(&y[i], &y_ref[i], 4) != 0;
88                for (size_t i = 0; i < y16.size(); ++i) d16 += y16[i] != y16_ref[i];
89                for (size_t i = 0; i < st.size(); ++i) dsn += std::memcmp(&st[i], &st_ref[i], 4) != 0;
90                // (y, the FP32 scratch, is not part of the contract: 0.1.39's norm kernel no longer stores the normalized value there,
91                // while the head kernels store it unless STRATA_GDN_NOY; what the out projection reads is y16, and the state)
92                const bool ok = d16 == 0 && dsn == 0;
93                failures += !ok;
94                std::printf("%s T %lld variant %d vs 0: y %zu, y16 %zu, state %zu values differ\n", ok ? "PASS" : "FAIL",
95                            (long long) T, variant, dy_n, d16, dsn);
96            }
97            cudaFree(dst); cudaFree(dy); cudaFree(dy16);
98        }
99        // timing at the long lengths: variant 0 vs 1
100        if (T >= 2048) {
101            float* dst = dev(state);
102            float* dy = nullptr;
103            uint16_t* dy16 = nullptr;
104            ck(cudaMalloc(&dy, (size_t) T * HV * S * 4), "malloc");
105            ck(cudaMalloc(&dy16, (size_t) T * HV * S * 2), "malloc");
106            for (int variant : {0, 1}) {
107                cudaEvent_t a, b;
108                cudaEventCreate(&a); cudaEventCreate(&b);
109                strata::prefill::gdn_recurrence_variant(variant, dst, dh, dg, db, dz, dgm, 1e-6f, dy, dy16, T, nullptr);
110                cudaEventRecord(a);
111                for (int r = 0; r < 3; ++r)
112                    strata::prefill::gdn_recurrence_variant(variant, dst, dh, dg, db, dz, dgm, 1e-6f, dy, dy16, T, nullptr);
113                cudaEventRecord(b);
114                ck(cudaEventSynchronize(b), "time");
115                float ms = 0;
116                cudaEventElapsedTime(&ms, a, b);
117                std::printf("  T %lld variant %d: %.3f ms per call (%.2f us per token)\n", (long long) T, variant, ms / 3,
118                            ms / 3 * 1000 / (double) T);
119            }
120            cudaFree(dst); cudaFree(dy); cudaFree(dy16);
121        }
122        cudaFree((void*) dh); cudaFree((void*) dg); cudaFree((void*) db); cudaFree((void*) dz); cudaFree((void*) dgm);
123    }
124    std::printf("FAILURES: %d\n", failures);
125    return failures ? 1 : 0;
126}