diff --git a/CMakeLists.txt b/CMakeLists.txt index a82802bf..0febb009 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1005,6 +1005,13 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP) target_include_directories(gr_multi_parity PRIVATE ${_strata_gpu_include_directories}) target_link_libraries(gr_multi_parity PRIVATE strata_kernels ${_strata_gpu_runtime_target}) add_test(NAME gr_multi_parity COMMAND gr_multi_parity) + # STRATA_HC_PERSIST=1: the persistent hyper-connection read against the default multi read (bounded, two runs bitwise), + # and with the variable unset it never runs (a skip where the card cannot hold its 80 blocks, and on CUDA) + add_executable(hc_persist_parity src/kernels/hc_persist_parity.cpp) + target_include_directories(hc_persist_parity PRIVATE ${_strata_gpu_include_directories}) + target_link_libraries(hc_persist_parity PRIVATE strata_kernels ${_strata_gpu_runtime_target}) + add_test(NAME hc_persist_parity COMMAND hc_persist_parity) + add_test(NAME hc_persist_default_off COMMAND hc_persist_parity --off) if(STRATA_HIP_GFX906) # the S6 router (STRATA_HIP_ROUTER_FAST=1 on gfx906) against the portable kernel, bitwise, on wave64 add_executable(hip_router_fast tests/hip/router_fast.cpp) diff --git a/src/kernels/hc_persist_parity.cpp b/src/kernels/hc_persist_parity.cpp new file mode 100644 index 00000000..0e630060 --- /dev/null +++ b/src/kernels/hc_persist_parity.cpp @@ -0,0 +1,151 @@ +// src/kernels/hc_persist_parity.cpp - the STRATA_HC_PERSIST=1 hyper-connection read (one persistent kernel) against the +// default multi read (fused_gr_read_multi). +// +// 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 +// verifier calls it. The persistent read sums in another order, so it is bounded, not bitwise: every output (the mixed +// row, the inject outputs, the written R) must be finite and within 1e-4 of the default read relative to its largest +// magnitude, and two runs of the persistent read must agree bit for bit. Where the card does not hold the 80 blocks at +// 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. +// `--off`: with STRATA_HC_PERSIST unset the persistent read must never run (the default path is untouched). +// GPU, synthetic, no model. +#include "strata/kernels/fused_gr.hpp" + +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace k = strata::kernels; + +namespace { +constexpr int N = 2560, HC = 4, D = N * HC, LR = 320, TM = 8; +void ck(cudaError_t e, const char* w) { + if (e != cudaSuccess) { + std::fprintf(stderr, "%s: %s\n", w, cudaGetErrorString(e)); + std::exit(2); + } +} +uint16_t bf16(float x) { + uint32_t u; + std::memcpy(&u, &x, 4); + return (uint16_t) ((u + 0x7fff + ((u >> 16) & 1)) >> 16); +} +template +T* up(const std::vector& h) { + void* p = nullptr; + ck(cudaMalloc(&p, h.size() * sizeof(T)), "malloc"); + ck(cudaMemcpy(p, h.data(), h.size() * sizeof(T), cudaMemcpyHostToDevice), "h2d"); + return (T*) p; +} +template +T* dev(size_t n) { + void* p = nullptr; + ck(cudaMalloc(&p, n * sizeof(T)), "malloc"); + return (T*) p; +} +} // namespace + +int main(int argc, char** argv) { + const bool off = argc > 1 && std::strcmp(argv[1], "--off") == 0; + if (off) unsetenv("STRATA_HC_PERSIST"); + else setenv("STRATA_HC_PERSIST", "1", 1); + k::fused_gr_check(); + k::fused_gr_persist_init(); + + std::mt19937 rng(7); + std::normal_distribution nd(0.f, 1.f); + std::vector R((size_t) TM * D), bo((size_t) TM * N), inj((size_t) TM * HC), wn(D); + for (auto& x : R) x = nd(rng); + for (auto& x : bo) x = 0.5f * nd(rng); + for (auto& x : inj) x = nd(rng); + for (auto& x : wn) x = 1.f + 0.3f * nd(rng); + std::vector wd((size_t) LR * D), wu((size_t) D * LR), wi((size_t) HC * D); + for (auto& x : wd) x = bf16(0.02f * nd(rng)); + for (auto& x : wu) x = bf16(0.05f * nd(rng)); + for (auto& x : wi) x = bf16(0.02f * nd(rng)); + float* dR = dev(R.size()); + float* dbo = up(bo); + float* dinj = up(inj); + float* dwn = up(wn); + uint16_t* dwd = up(wd); + uint16_t* dwu = up(wu); + uint16_t* dwi = up(wi); + float* dxn = dev((size_t) TM * D); + float* dlo = dev((size_t) TM * LR); + float* drs = dev((size_t) TM * HC); + float* dio = dev((size_t) TM * HC); + float* dmix = dev((size_t) TM * N); + + int bad = 0, ran = 0; + for (int T = 1; T <= TM; ++T) { + k::FusedGrArgs a[TM]; + for (int t = 0; t < T; ++t) { + a[t] = k::FusedGrArgs{}; + a[t].R = dR + (size_t) t * D; + a[t].R_out = dR + (size_t) t * D; + a[t].apply = true; + a[t].bo_prev = dbo + (size_t) t * N; + a[t].inj_prev = dinj + (size_t) t * HC; + a[t].w_norm = dwn; + a[t].w_down = dwd; + a[t].w_up = dwu; + a[t].w_inject = dwi; + a[t].eps = 1e-6f; + a[t].lo = dlo + (size_t) t * LR; + a[t].rs = drs + (size_t) t * HC; + a[t].inject_out = dio + (size_t) t * HC; + a[t].mixed = dmix + (size_t) t * N; + } + // 0: the default read, 1 and 2: the persistent read twice; each output is mixed | inject | R + std::vector out[3]; + bool used = true; + for (int w = 0; w < 3; ++w) { + ck(cudaMemcpy(dR, R.data(), R.size() * 4, cudaMemcpyHostToDevice), "R"); + ck(cudaMemset(dmix, 0xff, (size_t) TM * N * 4), "mix"); + ck(cudaMemset(dio, 0xff, (size_t) TM * HC * 4), "io"); + ck(cudaDeviceSynchronize(), "sync"); + if (w == 0) k::fused_gr_read_multi(a, T, dxn, nullptr); + else used = k::fused_gr_read_persist(a, T, nullptr) && used; + ck(cudaDeviceSynchronize(), "read"); + out[w].resize((size_t) T * (N + HC + D)); + ck(cudaMemcpy(out[w].data(), dmix, (size_t) T * N * 4, cudaMemcpyDeviceToHost), "d2h mix"); + ck(cudaMemcpy(out[w].data() + (size_t) T * N, dio, (size_t) T * HC * 4, cudaMemcpyDeviceToHost), "d2h io"); + ck(cudaMemcpy(out[w].data() + (size_t) T * (N + HC), dR, (size_t) T * D * 4, cudaMemcpyDeviceToHost), "d2h R"); + if (w == 1 && !used) break; + } + if (off) { + if (used) { + std::printf("T=%d: the persistent read ran with STRATA_HC_PERSIST unset FAIL\n", T); + ++bad; + } + continue; + } + if (!used) { + std::printf("T=%d: the persistent read did not run on this card (skip)\n", T); + continue; + } + ++ran; + double mref = 0.0, md = 0.0; + bool finite = true; + for (size_t i = 0; i < out[0].size(); ++i) { + finite = finite && std::isfinite(out[1][i]); + mref = std::fmax(mref, std::fabs((double) out[0][i])); + md = std::fmax(md, std::fabs((double) out[1][i] - out[0][i])); + } + const double rel = md / (mref > 0 ? mref : 1.0); + const bool same = std::memcmp(out[1].data(), out[2].data(), out[1].size() * 4) == 0; + const bool ok = finite && rel < 1e-4 && same; + std::printf("T=%d: max relative difference %.3g against the default read, %s, two runs %s %s\n", T, rel, + finite ? "finite" : "NON-FINITE", same ? "bitwise equal" : "DIFFER", ok ? "ok" : "FAIL"); + if (!ok) ++bad; + } + if (off) std::printf("%s: STRATA_HC_PERSIST unset, the persistent read never ran\n", bad ? "FAIL" : "PASS"); + else if (ran == 0) std::printf("SKIP: the persistent read is not used on this card\n"); + else std::printf("%s: %d token counts checked\n", bad ? "FAIL" : "PASS", ran); + return bad ? 1 : 0; +}