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.5 KiB156 linesraw
1// src/kernels/hc_persist_parity.cpp - the STRATA_HC_PERSIST=1 hyper-connection read (one persistent kernel) against the
2// default multi read (fused_gr_read_multi).
3//
4// Real geometry (n_embd 2560, hc 4, hc_lr 320), every T = 1..8, the pending write applied and the inject rows on, as the
5// verifier calls it. The persistent read sums in another order, so it is bounded, not bitwise: every output (the mixed
6// row, the inject outputs, the written R) must be finite and within 1e-4 of the default read relative to its largest
7// magnitude, and two runs of the persistent read must agree bit for bit. Where the card does not hold the 80 blocks at
8// once (or on CUDA, where the read is not built) the read reports that it did not run and the test passes as a skip.
9// `--off`: with STRATA_HC_PERSIST unset the persistent read must never run (the default path is untouched).
10// GPU, synthetic, no model.
11#include "strata/kernels/fused_gr.hpp"
12
13#include <cuda_runtime.h>
14
15#include <cmath>
16#include <cstdint>
17#include <cstdio>
18#include <cstdlib>
19#include <cstring>
20#include <random>
21#include <vector>
22
23#if defined(_WIN32)   // MSVC has no setenv / unsetenv
24#define setenv(k, v, o) _putenv_s((k), (v))
25#define unsetenv(k) _putenv_s((k), "")
26#endif
27
28namespace k = strata::kernels;
29
30namespace {
31constexpr int N = 2560, HC = 4, D = N * HC, LR = 320, TM = 8;
32void ck(cudaError_t e, const char* w) {
33    if (e != cudaSuccess) {
34        std::fprintf(stderr, "%s: %s\n", w, cudaGetErrorString(e));
35        std::exit(2);
36    }
37}
38uint16_t bf16(float x) {
39    uint32_t u;
40    std::memcpy(&u, &x, 4);
41    return (uint16_t) ((u + 0x7fff + ((u >> 16) & 1)) >> 16);
42}
43template <typename T>
44T* up(const std::vector<T>& h) {
45    void* p = nullptr;
46    ck(cudaMalloc(&p, h.size() * sizeof(T)), "malloc");
47    ck(cudaMemcpy(p, h.data(), h.size() * sizeof(T), cudaMemcpyHostToDevice), "h2d");
48    return (T*) p;
49}
50template <typename T>
51T* dev(size_t n) {
52    void* p = nullptr;
53    ck(cudaMalloc(&p, n * sizeof(T)), "malloc");
54    return (T*) p;
55}
56}  // namespace
57
58int main(int argc, char** argv) {
59    const bool off = argc > 1 && std::strcmp(argv[1], "--off") == 0;
60    if (off) unsetenv("STRATA_HC_PERSIST");
61    else setenv("STRATA_HC_PERSIST", "1", 1);
62    k::fused_gr_check();
63    k::fused_gr_persist_init();
64
65    std::mt19937 rng(7);
66    std::normal_distribution<float> nd(0.f, 1.f);
67    std::vector<float> R((size_t) TM * D), bo((size_t) TM * N), inj((size_t) TM * HC), wn(D);
68    for (auto& x : R) x = nd(rng);
69    for (auto& x : bo) x = 0.5f * nd(rng);
70    for (auto& x : inj) x = nd(rng);
71    for (auto& x : wn) x = 1.f + 0.3f * nd(rng);
72    std::vector<uint16_t> wd((size_t) LR * D), wu((size_t) D * LR), wi((size_t) HC * D);
73    for (auto& x : wd) x = bf16(0.02f * nd(rng));
74    for (auto& x : wu) x = bf16(0.05f * nd(rng));
75    for (auto& x : wi) x = bf16(0.02f * nd(rng));
76    float* dR = dev<float>(R.size());
77    float* dbo = up(bo);
78    float* dinj = up(inj);
79    float* dwn = up(wn);
80    uint16_t* dwd = up(wd);
81    uint16_t* dwu = up(wu);
82    uint16_t* dwi = up(wi);
83    float* dxn = dev<float>((size_t) TM * D);
84    float* dlo = dev<float>((size_t) TM * LR);
85    float* drs = dev<float>((size_t) TM * HC);
86    float* dio = dev<float>((size_t) TM * HC);
87    float* dmix = dev<float>((size_t) TM * N);
88
89    int bad = 0, ran = 0;
90    for (int T = 1; T <= TM; ++T) {
91        k::FusedGrArgs a[TM];
92        for (int t = 0; t < T; ++t) {
93            a[t] = k::FusedGrArgs{};
94            a[t].R = dR + (size_t) t * D;
95            a[t].R_out = dR + (size_t) t * D;
96            a[t].apply = true;
97            a[t].bo_prev = dbo + (size_t) t * N;
98            a[t].inj_prev = dinj + (size_t) t * HC;
99            a[t].w_norm = dwn;
100            a[t].w_down = dwd;
101            a[t].w_up = dwu;
102            a[t].w_inject = dwi;
103            a[t].eps = 1e-6f;
104            a[t].lo = dlo + (size_t) t * LR;
105            a[t].rs = drs + (size_t) t * HC;
106            a[t].inject_out = dio + (size_t) t * HC;
107            a[t].mixed = dmix + (size_t) t * N;
108        }
109        // 0: the default read, 1 and 2: the persistent read twice; each output is mixed | inject | R
110        std::vector<float> out[3];
111        bool used = true;
112        for (int w = 0; w < 3; ++w) {
113            ck(cudaMemcpy(dR, R.data(), R.size() * 4, cudaMemcpyHostToDevice), "R");
114            ck(cudaMemset(dmix, 0xff, (size_t) TM * N * 4), "mix");
115            ck(cudaMemset(dio, 0xff, (size_t) TM * HC * 4), "io");
116            ck(cudaDeviceSynchronize(), "sync");
117            if (w == 0) k::fused_gr_read_multi(a, T, dxn, nullptr);
118            else used = k::fused_gr_read_persist(a, T, nullptr) && used;
119            ck(cudaDeviceSynchronize(), "read");
120            out[w].resize((size_t) T * (N + HC + D));
121            ck(cudaMemcpy(out[w].data(), dmix, (size_t) T * N * 4, cudaMemcpyDeviceToHost), "d2h mix");
122            ck(cudaMemcpy(out[w].data() + (size_t) T * N, dio, (size_t) T * HC * 4, cudaMemcpyDeviceToHost), "d2h io");
123            ck(cudaMemcpy(out[w].data() + (size_t) T * (N + HC), dR, (size_t) T * D * 4, cudaMemcpyDeviceToHost), "d2h R");
124            if (w == 1 && !used) break;
125        }
126        if (off) {
127            if (used) {
128                std::printf("T=%d: the persistent read ran with STRATA_HC_PERSIST unset  FAIL\n", T);
129                ++bad;
130            }
131            continue;
132        }
133        if (!used) {
134            std::printf("T=%d: the persistent read did not run on this card (skip)\n", T);
135            continue;
136        }
137        ++ran;
138        double mref = 0.0, md = 0.0;
139        bool finite = true;
140        for (size_t i = 0; i < out[0].size(); ++i) {
141            finite = finite && std::isfinite(out[1][i]);
142            mref = std::fmax(mref, std::fabs((double) out[0][i]));
143            md = std::fmax(md, std::fabs((double) out[1][i] - out[0][i]));
144        }
145        const double rel = md / (mref > 0 ? mref : 1.0);
146        const bool same = std::memcmp(out[1].data(), out[2].data(), out[1].size() * 4) == 0;
147        const bool ok = finite && rel < 1e-4 && same;
148        std::printf("T=%d: max relative difference %.3g against the default read, %s, two runs %s  %s\n", T, rel,
149                    finite ? "finite" : "NON-FINITE", same ? "bitwise equal" : "DIFFER", ok ? "ok" : "FAIL");
150        if (!ok) ++bad;
151    }
152    if (off) std::printf("%s: STRATA_HC_PERSIST unset, the persistent read never ran\n", bad ? "FAIL" : "PASS");
153    else if (ran == 0) std::printf("SKIP: the persistent read is not used on this card\n");
154    else std::printf("%s: %d token counts checked\n", bad ? "FAIL" : "PASS", ran);
155    return bad ? 1 : 0;
156}