okami.mom/strata

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

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

main
5.3 KiB73 linesraw
1// The engine's STRATA_HC_PERSIST read (public API) against the default read, per token count: the largest relative
2// difference from the default read, and the time per read in a graph of 48 cold reads. Run with STRATA_HC_PERSIST=1
3#include <hip/hip_runtime.h>
4#include "strata/kernels/fused_gr.hpp"
5#include <cmath>
6#include <cstdio>
7#include <cstdlib>
8#include <cstring>
9#include <random>
10#include <vector>
11namespace K = strata::kernels;
12#define CK(x) do { hipError_t e_ = (x); if (e_ != hipSuccess) { std::fprintf(stderr, "%s:%d %s\n", __FILE__, __LINE__, hipGetErrorString(e_)); std::exit(1); } } while (0)
13constexpr int N = 2560, HC = 4, D = N * HC, LR = 320, TM = 8;
14static uint16_t bf16(float x) { uint32_t u; std::memcpy(&u, &x, 4); return (uint16_t) ((u + 0x7fff + ((u >> 16) & 1)) >> 16); }
15int main(int argc, char** argv) {
16    setvbuf(stdout, nullptr, _IONBF, 0);
17    const int W = argc > 1 ? std::atoi(argv[1]) : 40;
18    K::fused_gr_check();
19    K::fused_gr_persist_init();
20    std::mt19937 rng(5); std::normal_distribution<float> nd(0.f, 1.f);
21    std::vector<float> R((size_t) TM * D), bo((size_t) TM * N), inj((size_t) TM * HC), wn(D);
22    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);
23    const size_t WD = (size_t) LR * D, WU = WD, WI = (size_t) HC * D;
24    uint16_t *dwd, *dwu, *dwi; CK(hipMalloc(&dwd, WD * 2 * W)); CK(hipMalloc(&dwu, WU * 2 * W)); CK(hipMalloc(&dwi, WI * 2 * W));
25    { std::vector<uint16_t> b(WD); for (int s = 0; s < W; ++s) {
26        for (auto& x : b) x = bf16(0.02f * nd(rng)); CK(hipMemcpy(dwd + WD * s, b.data(), WD * 2, hipMemcpyHostToDevice));
27        for (auto& x : b) x = bf16(0.05f * nd(rng)); CK(hipMemcpy(dwu + WU * s, b.data(), WU * 2, hipMemcpyHostToDevice));
28        for (size_t i = 0; i < WI; ++i) b[i] = bf16(0.02f * nd(rng)); CK(hipMemcpy(dwi + WI * s, b.data(), WI * 2, hipMemcpyHostToDevice)); } }
29    float *dR, *dbo, *dinj, *dwn, *dxn, *dlo, *drs, *dio, *dmix;
30    CK(hipMalloc(&dR, R.size() * 4)); CK(hipMalloc(&dbo, bo.size() * 4)); CK(hipMalloc(&dinj, inj.size() * 4)); CK(hipMalloc(&dwn, D * 4));
31    CK(hipMalloc(&dxn, (size_t) TM * D * 4)); CK(hipMalloc(&dlo, (size_t) TM * LR * 4)); CK(hipMalloc(&drs, TM * HC * 4));
32    CK(hipMalloc(&dio, TM * HC * 4)); CK(hipMalloc(&dmix, (size_t) TM * N * 4));
33    CK(hipMemcpy(dbo, bo.data(), bo.size() * 4, hipMemcpyHostToDevice)); CK(hipMemcpy(dinj, inj.data(), inj.size() * 4, hipMemcpyHostToDevice));
34    CK(hipMemcpy(dwn, wn.data(), D * 4, hipMemcpyHostToDevice));
35    hipStream_t st; CK(hipStreamCreateWithFlags(&st, hipStreamNonBlocking));
36    hipEvent_t e0, e1; hipEventCreate(&e0); hipEventCreate(&e1);
37    auto args = [&](K::FusedGrArgs* a, int T, int set) {
38        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;
39            a[t].bo_prev = dbo + (size_t) t * N; a[t].inj_prev = dinj + t * HC; a[t].w_norm = dwn; a[t].w_down = dwd + WD * set;
40            a[t].w_up = dwu + WU * set; a[t].w_inject = dwi + WI * set; a[t].eps = 1e-6f; a[t].lo = dlo + t * LR; a[t].rs = drs + t * HC;
41            a[t].inject_out = dio + t * HC; a[t].mixed = dmix + (size_t) t * N; } };
42    for (int T = 1; T <= TM; ++T) {
43        K::FusedGrArgs a[TM]; args(a, T, 0);
44        std::vector<float> out[2];
45        bool ran = true;
46        for (int w = 0; w < 2; ++w) {
47            CK(hipMemcpy(dR, R.data(), R.size() * 4, hipMemcpyHostToDevice)); CK(hipMemset(dmix, 0xff, (size_t) TM * N * 4)); CK(hipDeviceSynchronize());
48            if (w == 0) K::fused_gr_read_multi(a, T, dxn, st); else ran = K::fused_gr_read_persist(a, T, st);
49            CK(hipStreamSynchronize(st));
50            out[w].resize((size_t) T * N); CK(hipMemcpy(out[w].data(), dmix, out[w].size() * 4, hipMemcpyDeviceToHost));
51        }
52        if (!ran) { std::printf("T %d: persistent read not used here (falls back)\n", T); continue; }
53        double mr = 0, md = 0; bool fin = true;
54        for (size_t i = 0; i < out[0].size(); ++i) { fin = fin && std::isfinite(out[1][i]); mr = std::fmax(mr, std::fabs(out[0][i])); md = std::fmax(md, std::fabs(out[1][i] - out[0][i])); }
55        double us[2];
56        for (int w = 0; w < 2; ++w) {
57            double best = 1e30;
58            for (int r = 0; r < 3; ++r) {
59                hipGraph_t g; hipGraphExec_t ge;
60                CK(hipStreamBeginCapture(st, hipStreamCaptureModeGlobal));
61                for (int i = 0; i < 48; ++i) { args(a, T, (i * 7) % W); if (w == 0) K::fused_gr_read_multi(a, T, dxn, st); else K::fused_gr_read_persist(a, T, st); }
62                CK(hipStreamEndCapture(st, &g)); CK(hipGraphInstantiate(&ge, g, nullptr, nullptr, 0));
63                for (int i = 0; i < 2; ++i) CK(hipGraphLaunch(ge, st));
64                CK(hipEventRecord(e0, st)); for (int i = 0; i < 10; ++i) CK(hipGraphLaunch(ge, st)); CK(hipEventRecord(e1, st)); CK(hipEventSynchronize(e1));
65                float ms; hipEventElapsedTime(&ms, e0, e1); best = std::fmin(best, 1e3 * ms / 480);
66                hipGraphExecDestroy(ge); hipGraphDestroy(g);
67            }
68            us[w] = best;
69        }
70        std::printf("T %d: default %6.1f us  persistent %6.1f us  (%.2fx)  max rel diff %.2e%s\n", T, us[0], us[1], us[0] / us[1], md / mr, fin ? "" : "  NON-FINITE");
71    }
72    return 0;
73}