okami.mom/strata

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

Niko1221Merge f-143-amdk: gfx12 decode node cuts (HC norm from registers, host-flag wait folded into the copy, HC up kernel owning its q8_1 groups, hit counts in the combine); bitwise exact, +1.6-2.0% decode331b591b3

main
8.2 KiB175 linesraw
1// src/kernels/router_chain_bench.cpp - what the FFN-side router chain of one layer costs in a captured graph:
2// the router GEMV (bf16 [512][2560], T rows), the top-10, the doorbell publish; 48 layers' worth with distinct
3// weights (cold), as one graph, per-layer microseconds.  Bench only (no parity: the kernels are the engine's own).
4//
5//     build/router_chain_bench [T=2] [reps=20]
6#include "strata/kernels/bf16_gemv.hpp"
7#include "strata/kernels/elementwise.hpp"
8#include "strata/kernels/native_router.hpp"
9#include "strata/kernels/verify_kernels.hpp"
10
11#include <cuda_runtime.h>
12
13#include <algorithm>
14#include <cstdint>
15#include <cstdio>
16#include <cstdlib>
17#include <cstring>
18#include <random>
19#include <vector>
20
21namespace K = strata::kernels;
22
23#define CK(x) do { cudaError_t e_ = (x); if (e_ != cudaSuccess) { std::fprintf(stderr, "%s: %s\n", #x, cudaGetErrorString(e_)); std::exit(1); } } while (0)
24
25
26int main(int argc, char** argv) {
27    setvbuf(stdout, nullptr, _IONBF, 0);
28    const int T = argc > 1 ? std::atoi(argv[1]) : 2;
29    const int reps = argc > 2 ? std::atoi(argv[2]) : 20;
30    const int L = 48, N = 2560, NE = 512, KK = 10;
31    std::mt19937 rng(3);
32    std::normal_distribution<float> nd(0.f, 1.f);
33    std::vector<uint16_t> w((size_t) NE * N);
34    for (auto& x : w) {
35        const float f = 0.05f * nd(rng);
36        uint32_t u;
37        std::memcpy(&u, &f, 4);
38        x = (uint16_t) ((u + 0x7fff + ((u >> 16) & 1)) >> 16);
39    }
40    std::vector<float> xh((size_t) T * N);
41    for (auto& x : xh) x = nd(rng);
42    std::vector<uint16_t*> dw(L);
43    for (int l = 0; l < L; ++l) {
44        CK(cudaMalloc((void**) &dw[l], w.size() * 2));
45        CK(cudaMemcpy(dw[l], w.data(), w.size() * 2, cudaMemcpyHostToDevice));
46    }
47    float *dx, *dlog, *dwt, *dxo;
48    int32_t *dids, *dids_o, *dres;
49    CK(cudaMalloc((void**) &dx, xh.size() * 4));
50    CK(cudaMemcpy(dx, xh.data(), xh.size() * 4, cudaMemcpyHostToDevice));
51    CK(cudaMalloc((void**) &dlog, (size_t) T * NE * 4));
52    CK(cudaMalloc((void**) &dwt, (size_t) T * KK * 4));
53    CK(cudaMalloc((void**) &dids, (size_t) T * KK * 4));
54    CK(cudaMalloc((void**) &dids_o, (size_t) T * KK * 4));
55    CK(cudaMalloc((void**) &dxo, xh.size() * 4));
56    CK(cudaMalloc((void**) &dres, NE * 4));
57    std::vector<int32_t> res(NE);
58    for (int i = 0; i < NE; ++i) res[i] = i;
59    CK(cudaMemcpy(dres, res.data(), NE * 4, cudaMemcpyHostToDevice));
60    uint32_t* hseq;
61    CK(cudaHostAlloc((void**) &hseq, 64, cudaHostAllocMapped | cudaHostAllocPortable));
62    *hseq = 0;
63    uint32_t* dseq;
64    CK(cudaHostGetDevicePointer((void**) &dseq, hseq, 0));
65    int* dz;
66    CK(cudaMalloc((void**) &dz, 16));
67    CK(cudaMemset(dz, 0, 16));
68    cudaStream_t s;
69    CK(cudaStreamCreate(&s));
70    {   // outputs of the router GEMV for several shapes and token counts as FNV hashes: compare across STRATA_MMVF_PF=0/1
71        const int shapes[][2] = {{2560, 512}, {2560, 256}, {2560, 64}, {1280, 513}, {640, 100}, {4096, 50}, {2048, 7}, {256, 33}, {8192, 90}};
72        for (const auto& sh : shapes) {
73            const int ni = sh[0], no = sh[1];
74            std::vector<uint16_t> wh((size_t) no * ni);
75            for (auto& v : wh) {
76                const float f = 0.05f * nd(rng);
77                uint32_t u;
78                std::memcpy(&u, &f, 4);
79                v = (uint16_t) ((u + 0x7fff + ((u >> 16) & 1)) >> 16);
80            }
81            std::vector<float> xs((size_t) 8 * ni);
82            for (auto& v : xs) v = nd(rng);
83            uint16_t* dwp;
84            float *dxp, *dyp;
85            CK(cudaMalloc((void**) &dwp, wh.size() * 2));
86            CK(cudaMemcpy(dwp, wh.data(), wh.size() * 2, cudaMemcpyHostToDevice));
87            CK(cudaMalloc((void**) &dxp, xs.size() * 4));
88            CK(cudaMemcpy(dxp, xs.data(), xs.size() * 4, cudaMemcpyHostToDevice));
89            CK(cudaMalloc((void**) &dyp, (size_t) 8 * no * 4));
90            for (int tk = 1; tk <= 8; ++tk) {
91                CK(cudaMemset(dyp, 0xff, (size_t) 8 * no * 4));
92                K::bf16_gemv_fp32_mmvf_multi(dxp, ni, dwp, dyp, no, ni, no, tk, s);
93                CK(cudaStreamSynchronize(s));
94                std::vector<float> yh((size_t) 8 * no);
95                CK(cudaMemcpy(yh.data(), dyp, yh.size() * 4, cudaMemcpyDeviceToHost));
96                unsigned long long h = 1469598103934665603ull;
97                for (size_t b = 0; b < yh.size() * 4; ++b) h = (h ^ ((const unsigned char*) yh.data())[b]) * 1099511628211ull;
98                std::printf("PARITY n_in %d n_out %d T %d hash %016llx\n", ni, no, tk, h);
99            }
100            cudaFree(dwp); cudaFree(dxp); cudaFree(dyp);
101        }
102    }
103    cudaEvent_t e0, e1;
104    CK(cudaEventCreate(&e0));
105    CK(cudaEventCreate(&e1));
106    {   // the BF16 GEMV by shape and token count, 48 distinct weight copies in one graph: us per call (STRATA_MMVF_PF=0 / 1 to compare)
107        const int outs[] = {512, 256, 128, 64, 48, 8, 1};
108        const int ins[] = {2560, 2560, 2560, 2560, 2560, 2560, 2560};
109        for (int si = 0; si < 7; ++si) {
110            const int no = outs[si], ni = ins[si];
111            std::vector<uint16_t*> dws(L);
112            for (int l = 0; l < L; ++l) {
113                CK(cudaMalloc((void**) &dws[l], (size_t) no * ni * 2));
114                CK(cudaMemcpy(dws[l], w.data(), (size_t) no * ni * 2 < w.size() * 2 ? (size_t) no * ni * 2 : w.size() * 2, cudaMemcpyHostToDevice));
115            }
116            for (int tk : {1, 2, 3, 4, 6}) {
117                cudaGraph_t g;
118                cudaGraphExec_t ge;
119                CK(cudaStreamBeginCapture(s, cudaStreamCaptureModeThreadLocal));
120                for (int l = 0; l < L; ++l) K::bf16_gemv_fp32_mmvf_multi(dx, N, dws[l], dlog, NE, ni, no, tk, s);
121                CK(cudaStreamEndCapture(s, &g));
122                CK(cudaGraphInstantiate(&ge, g, nullptr, nullptr, 0));
123                for (int i = 0; i < 5; ++i) CK(cudaGraphLaunch(ge, s));
124                CK(cudaStreamSynchronize(s));
125                std::vector<float> ms;
126                for (int r = 0; r < reps; ++r) {
127                    CK(cudaEventRecord(e0, s));
128                    CK(cudaGraphLaunch(ge, s));
129                    CK(cudaEventRecord(e1, s));
130                    CK(cudaEventSynchronize(e1));
131                    float m = 0;
132                    CK(cudaEventElapsedTime(&m, e0, e1));
133                    ms.push_back(m);
134                }
135                std::sort(ms.begin(), ms.end());
136                std::printf("SHAPE n_in %d n_out %3d T %d : %6.2f us per call\n", ni, no, tk, 1e3 * ms[ms.size() / 2] / L);
137                cudaGraphExecDestroy(ge);
138                cudaGraphDestroy(g);
139            }
140            for (int l = 0; l < L; ++l) cudaFree(dws[l]);
141        }
142    }
143    // mode bits: 1 mmvf, 2 route, 4 doorbell, 8 an empty kernel per layer (the node floor)
144    const char* names[] = {"empty node", "mmvf", "route", "mmvf+route", "doorbell", "mmvf+doorbell", "route+doorbell", "mmvf+route+doorbell"};
145    for (int mode = 0; mode < 8; ++mode) {
146        cudaGraph_t g;
147        cudaGraphExec_t ge;
148        CK(cudaStreamBeginCapture(s, cudaStreamCaptureModeThreadLocal));
149        for (int l = 0; l < L; ++l) {
150            if (mode == 0) K::gpu_stamp((unsigned long long*) dz, 0, s);
151            if (mode & 1) K::bf16_gemv_fp32_mmvf_multi(dx, N, dw[l], dlog, NE, N, NE, T, s);
152            if (mode & 2) K::native_router_top10_multi(dlog, dids, dwt, T, s);
153            if (mode & 4) K::doorbell_publish_res(dx, dids, dres, NE, (int64_t) T * N, (int64_t) T * KK, dxo, dids_o, dseq, s);
154        }
155        CK(cudaStreamEndCapture(s, &g));
156        CK(cudaGraphInstantiate(&ge, g, nullptr, nullptr, 0));
157        for (int i = 0; i < 5; ++i) CK(cudaGraphLaunch(ge, s));
158        CK(cudaStreamSynchronize(s));
159        std::vector<float> ms;
160        for (int r = 0; r < reps; ++r) {
161            CK(cudaEventRecord(e0, s));
162            CK(cudaGraphLaunch(ge, s));
163            CK(cudaEventRecord(e1, s));
164            CK(cudaEventSynchronize(e1));
165            float m = 0;
166            CK(cudaEventElapsedTime(&m, e0, e1));
167            ms.push_back(m);
168        }
169        std::sort(ms.begin(), ms.end());
170        std::printf("T %d %-20s : %6.2f us per layer (median of %d graphs of %d layers)\n", T, names[mode], 1e3 * ms[ms.size() / 2] / L, reps, L);
171        cudaGraphExecDestroy(ge);
172        cudaGraphDestroy(g);
173    }
174    return 0;
175}