bench: fused_gr_bench cold-weight mode with the norm/down/up split and an output hash; router_chain_bench (router GEMV, top-10 and doorbell per layer in a graph)

This commit is contained in:
Niko1221 committed 2026-10-11 04:04:07 +02:00
1 parent 85d90002b6
commit bb519f5bca
3 files changed
+240 -3

No files matched your search

+3
View File
@@ -840,6 +840,9 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP)
add_executable(fused_gr_bench src/kernels/fused_gr_bench.cpp)
target_include_directories(fused_gr_bench PRIVATE ${_strata_gpu_include_directories})
target_link_libraries(fused_gr_bench PRIVATE strata_kernels ${_strata_gpu_runtime_target})
add_executable(router_chain_bench src/kernels/router_chain_bench.cpp)
target_include_directories(router_chain_bench PRIVATE ${_strata_gpu_include_directories})
target_link_libraries(router_chain_bench PRIVATE strata_kernels ${_strata_gpu_runtime_target})
add_executable(gr_parity src/kernels/gr_parity.cpp)
target_include_directories(gr_parity PRIVATE ${_strata_gpu_include_directories})
target_link_libraries(gr_parity PRIVATE strata_kernels ${_strata_gpu_runtime_target})
+62 -3
View File
@@ -1,11 +1,15 @@
// src/kernels/fused_gr_bench.cpp - fused_gr_read_multi: the AMD fast kernels (STRATA_GR_FAST) against the old
// ones, bit for bit on every output (lo, rs, inject, mixed, the in-place R), and each one's time.
//
// build/fused_gr_bench [iters] [T min] [T max]
// build/fused_gr_bench [iters] [T min] [T max] [cold sets]
// With "cold sets" > 1 it also times the read with the weights rotating over that many copies (a layer's weights are
// cold in the engine) and prints the per-stage split (norm / down / up) from the GPU stamps.
#include "strata/kernels/fused_gr.hpp"
#include "strata/kernels/verify_kernels.hpp"
#include <cuda_runtime.h>
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
@@ -26,6 +30,7 @@ int main(int argc, char** argv) {
setvbuf(stdout, nullptr, _IONBF, 0);
const int iters = argc > 1 ? std::atoi(argv[1]) : 500;
const int t_lo = argc > 2 ? std::atoi(argv[2]) : 1, t_hi = argc > 3 ? std::atoi(argv[3]) : 6;
const int cold_sets = argc > 4 ? std::atoi(argv[4]) : 0;
const int N = 2560, HC = 4, D = N * HC, LR = 320, TM = K::kFusedGrMaxT;
std::mt19937 rng(7);
std::normal_distribution<float> nd(0.f, 1.f);
@@ -107,10 +112,64 @@ int main(int argc, char** argv) {
cudaEventElapsedTime(&ms, e0, e1);
us[f] = std::fmin(us[f], 1e3 * ms / iters);
}
std::printf("T %d apply %d inject %d | old %6.1f us fast %6.1f us | %s\n", T, apply, inject, us[0], us[1],
same ? "bitwise equal" : "DIFFERS");
unsigned long long h = 1469598103934665603ull; // FNV-1a of the outputs: compare across runs (env switches)
for (size_t b = 0; b < out[0].size() * 4; ++b) h = (h ^ ((const unsigned char*) out[0].data())[b]) * 1099511628211ull;
std::printf("T %d apply %d inject %d | old %6.1f us fast %6.1f us | %s | hash %016llx\n", T, apply, inject,
us[0], us[1], same ? "bitwise equal" : "DIFFERS", h);
if (!same) ++failures;
}
if (cold_sets > 1) {
// the engine reads a different layer's weights every time: rotate over `cold_sets` copies (13 MB each)
std::vector<uint16_t*> cwd(cold_sets), cwu(cold_sets), cwi(cold_sets);
std::vector<float*> cwn(cold_sets);
for (int c = 0; c < cold_sets; ++c) {
cudaMalloc((void**) &cwd[c], wd.size() * 2); cudaMemcpy(cwd[c], wd.data(), wd.size() * 2, cudaMemcpyHostToDevice);
cudaMalloc((void**) &cwu[c], wu.size() * 2); cudaMemcpy(cwu[c], wu.data(), wu.size() * 2, cudaMemcpyHostToDevice);
cudaMalloc((void**) &cwi[c], wi.size() * 2); cudaMemcpy(cwi[c], wi.data(), wi.size() * 2, cudaMemcpyHostToDevice);
cudaMalloc((void**) &cwn[c], wn.size() * 4); cudaMemcpy(cwn[c], wn.data(), wn.size() * 4, cudaMemcpyHostToDevice);
}
unsigned long long* dst;
cudaMalloc((void**) &dst, (size_t) iters * 4 * 8);
std::vector<unsigned long long> hst((size_t) iters * 4);
std::vector<K::FusedGrArgs> as((size_t) cold_sets * 8);
for (int T = t_lo; T <= t_hi; ++T) for (int f = 0; f < 2; ++f) {
K::fused_gr_set_fast(f);
for (int c = 0; c < cold_sets; ++c)
for (int t = 0; t < T; ++t) {
K::FusedGrArgs& a = as[(size_t) c * 8 + t];
a = K::FusedGrArgs();
a.R = dR + (size_t) t * D; a.R_out = dR + (size_t) t * D; a.apply = 1;
a.bo_prev = dbo + (size_t) t * N; a.inj_prev = dinj + (size_t) t * HC;
a.w_norm = cwn[c]; a.w_down = cwd[c]; a.w_up = cwu[c]; a.w_inject = cwi[c];
a.lo = dlo + (size_t) t * LR; a.rs = drs + (size_t) t * HC;
a.inject_out = dio + (size_t) t * HC; a.mixed = dmix + (size_t) t * N;
}
for (int rep = 0; rep < 2; ++rep) {
for (int i = 0; i < iters; ++i) {
K::gpu_stamp(dst, i * 4, s);
K::fused_gr_read_multi(&as[(size_t) (i % cold_sets) * 8], T, dxn, s, dst, i * 4 + 1);
K::gpu_stamp(dst, i * 4 + 3, s);
}
cudaStreamSynchronize(s);
}
cudaEventRecord(e0, s);
for (int i = 0; i < iters; ++i) K::fused_gr_read_multi(&as[(size_t) (i % cold_sets) * 8], T, dxn, s);
cudaEventRecord(e1, s);
cudaEventSynchronize(e1);
float ms = 0;
cudaEventElapsedTime(&ms, e0, e1);
cudaMemcpy(hst.data(), dst, hst.size() * 8, cudaMemcpyDeviceToHost);
// stamps: 0 start, 1 after norm, 2 after down, 3 after up (a ~1-2 us stamp kernel between each)
std::vector<double> n_, d_, u_;
for (int i = 1; i < iters; ++i) {
const unsigned long long* p = &hst[(size_t) i * 4];
n_.push_back((double) (p[1] - p[0])); d_.push_back((double) (p[2] - p[1])); u_.push_back((double) (p[3] - p[2]));
}
auto med = [](std::vector<double>& v) { std::sort(v.begin(), v.end()); return v[v.size() / 2] / 1e3; };
std::printf("COLD(%d sets) fast %d T %d | read %6.1f us (events) | stamps median: norm %5.1f down %5.1f up %5.1f us (incl. a stamp kernel each)\n",
cold_sets, f, T, 1e3 * ms / iters, med(n_), med(d_), med(u_));
}
}
std::printf("fused_gr_bench: %d differing\n", failures);
return failures ? 1 : 0;
}
+175
View File
@@ -0,0 +1,175 @@
// src/kernels/router_chain_bench.cpp - what the FFN-side router chain of one layer costs in a captured graph:
// the router GEMV (bf16 [512][2560], T rows), the top-10, the doorbell publish; 48 layers' worth with distinct
// weights (cold), as one graph, per-layer microseconds. Bench only (no parity: the kernels are the engine's own).
//
// build/router_chain_bench [T=2] [reps=20]
#include "strata/kernels/bf16_gemv.hpp"
#include "strata/kernels/elementwise.hpp"
#include "strata/kernels/native_router.hpp"
#include "strata/kernels/verify_kernels.hpp"
#include <cuda_runtime.h>
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <random>
#include <vector>
namespace K = strata::kernels;
#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)
int main(int argc, char** argv) {
setvbuf(stdout, nullptr, _IONBF, 0);
const int T = argc > 1 ? std::atoi(argv[1]) : 2;
const int reps = argc > 2 ? std::atoi(argv[2]) : 20;
const int L = 48, N = 2560, NE = 512, KK = 10;
std::mt19937 rng(3);
std::normal_distribution<float> nd(0.f, 1.f);
std::vector<uint16_t> w((size_t) NE * N);
for (auto& x : w) {
const float f = 0.05f * nd(rng);
uint32_t u;
std::memcpy(&u, &f, 4);
x = (uint16_t) ((u + 0x7fff + ((u >> 16) & 1)) >> 16);
}
std::vector<float> xh((size_t) T * N);
for (auto& x : xh) x = nd(rng);
std::vector<uint16_t*> dw(L);
for (int l = 0; l < L; ++l) {
CK(cudaMalloc((void**) &dw[l], w.size() * 2));
CK(cudaMemcpy(dw[l], w.data(), w.size() * 2, cudaMemcpyHostToDevice));
}
float *dx, *dlog, *dwt, *dxo;
int32_t *dids, *dids_o, *dres;
CK(cudaMalloc((void**) &dx, xh.size() * 4));
CK(cudaMemcpy(dx, xh.data(), xh.size() * 4, cudaMemcpyHostToDevice));
CK(cudaMalloc((void**) &dlog, (size_t) T * NE * 4));
CK(cudaMalloc((void**) &dwt, (size_t) T * KK * 4));
CK(cudaMalloc((void**) &dids, (size_t) T * KK * 4));
CK(cudaMalloc((void**) &dids_o, (size_t) T * KK * 4));
CK(cudaMalloc((void**) &dxo, xh.size() * 4));
CK(cudaMalloc((void**) &dres, NE * 4));
std::vector<int32_t> res(NE);
for (int i = 0; i < NE; ++i) res[i] = i;
CK(cudaMemcpy(dres, res.data(), NE * 4, cudaMemcpyHostToDevice));
uint32_t* hseq;
CK(cudaHostAlloc((void**) &hseq, 64, cudaHostAllocMapped | cudaHostAllocPortable));
*hseq = 0;
uint32_t* dseq;
CK(cudaHostGetDevicePointer((void**) &dseq, hseq, 0));
int* dz;
CK(cudaMalloc((void**) &dz, 16));
CK(cudaMemset(dz, 0, 16));
cudaStream_t s;
CK(cudaStreamCreate(&s));
{ // outputs of the router GEMV for several shapes and token counts as FNV hashes: compare across STRATA_MMVF_PF=0/1
const int shapes[][2] = {{2560, 512}, {2560, 256}, {2560, 64}, {1280, 513}, {640, 100}, {4096, 50}, {2048, 7}, {256, 33}, {8192, 90}};
for (const auto& sh : shapes) {
const int ni = sh[0], no = sh[1];
std::vector<uint16_t> wh((size_t) no * ni);
for (auto& v : wh) {
const float f = 0.05f * nd(rng);
uint32_t u;
std::memcpy(&u, &f, 4);
v = (uint16_t) ((u + 0x7fff + ((u >> 16) & 1)) >> 16);
}
std::vector<float> xs((size_t) 8 * ni);
for (auto& v : xs) v = nd(rng);
uint16_t* dwp;
float *dxp, *dyp;
CK(cudaMalloc((void**) &dwp, wh.size() * 2));
CK(cudaMemcpy(dwp, wh.data(), wh.size() * 2, cudaMemcpyHostToDevice));
CK(cudaMalloc((void**) &dxp, xs.size() * 4));
CK(cudaMemcpy(dxp, xs.data(), xs.size() * 4, cudaMemcpyHostToDevice));
CK(cudaMalloc((void**) &dyp, (size_t) 8 * no * 4));
for (int tk = 1; tk <= 8; ++tk) {
CK(cudaMemset(dyp, 0xff, (size_t) 8 * no * 4));
K::bf16_gemv_fp32_mmvf_multi(dxp, ni, dwp, dyp, no, ni, no, tk, s);
CK(cudaStreamSynchronize(s));
std::vector<float> yh((size_t) 8 * no);
CK(cudaMemcpy(yh.data(), dyp, yh.size() * 4, cudaMemcpyDeviceToHost));
unsigned long long h = 1469598103934665603ull;
for (size_t b = 0; b < yh.size() * 4; ++b) h = (h ^ ((const unsigned char*) yh.data())[b]) * 1099511628211ull;
std::printf("PARITY n_in %d n_out %d T %d hash %016llx\n", ni, no, tk, h);
}
cudaFree(dwp); cudaFree(dxp); cudaFree(dyp);
}
}
cudaEvent_t e0, e1;
CK(cudaEventCreate(&e0));
CK(cudaEventCreate(&e1));
{ // 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)
const int outs[] = {512, 256, 128, 64, 48, 8, 1};
const int ins[] = {2560, 2560, 2560, 2560, 2560, 2560, 2560};
for (int si = 0; si < 7; ++si) {
const int no = outs[si], ni = ins[si];
std::vector<uint16_t*> dws(L);
for (int l = 0; l < L; ++l) {
CK(cudaMalloc((void**) &dws[l], (size_t) no * ni * 2));
CK(cudaMemcpy(dws[l], w.data(), (size_t) no * ni * 2 < w.size() * 2 ? (size_t) no * ni * 2 : w.size() * 2, cudaMemcpyHostToDevice));
}
for (int tk : {1, 2, 3, 4, 6}) {
cudaGraph_t g;
cudaGraphExec_t ge;
CK(cudaStreamBeginCapture(s, cudaStreamCaptureModeThreadLocal));
for (int l = 0; l < L; ++l) K::bf16_gemv_fp32_mmvf_multi(dx, N, dws[l], dlog, NE, ni, no, tk, s);
CK(cudaStreamEndCapture(s, &g));
CK(cudaGraphInstantiate(&ge, g, nullptr, nullptr, 0));
for (int i = 0; i < 5; ++i) CK(cudaGraphLaunch(ge, s));
CK(cudaStreamSynchronize(s));
std::vector<float> ms;
for (int r = 0; r < reps; ++r) {
CK(cudaEventRecord(e0, s));
CK(cudaGraphLaunch(ge, s));
CK(cudaEventRecord(e1, s));
CK(cudaEventSynchronize(e1));
float m = 0;
CK(cudaEventElapsedTime(&m, e0, e1));
ms.push_back(m);
}
std::sort(ms.begin(), ms.end());
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);
cudaGraphExecDestroy(ge);
cudaGraphDestroy(g);
}
for (int l = 0; l < L; ++l) cudaFree(dws[l]);
}
}
// mode bits: 1 mmvf, 2 route, 4 doorbell, 8 an empty kernel per layer (the node floor)
const char* names[] = {"empty node", "mmvf", "route", "mmvf+route", "doorbell", "mmvf+doorbell", "route+doorbell", "mmvf+route+doorbell"};
for (int mode = 0; mode < 8; ++mode) {
cudaGraph_t g;
cudaGraphExec_t ge;
CK(cudaStreamBeginCapture(s, cudaStreamCaptureModeThreadLocal));
for (int l = 0; l < L; ++l) {
if (mode == 0) K::gpu_stamp((unsigned long long*) dz, 0, s);
if (mode & 1) K::bf16_gemv_fp32_mmvf_multi(dx, N, dw[l], dlog, NE, N, NE, T, s);
if (mode & 2) K::native_router_top10_multi(dlog, dids, dwt, T, s);
if (mode & 4) K::doorbell_publish_res(dx, dids, dres, NE, (int64_t) T * N, (int64_t) T * KK, dxo, dids_o, dseq, s);
}
CK(cudaStreamEndCapture(s, &g));
CK(cudaGraphInstantiate(&ge, g, nullptr, nullptr, 0));
for (int i = 0; i < 5; ++i) CK(cudaGraphLaunch(ge, s));
CK(cudaStreamSynchronize(s));
std::vector<float> ms;
for (int r = 0; r < reps; ++r) {
CK(cudaEventRecord(e0, s));
CK(cudaGraphLaunch(ge, s));
CK(cudaEventRecord(e1, s));
CK(cudaEventSynchronize(e1));
float m = 0;
CK(cudaEventElapsedTime(&m, e0, e1));
ms.push_back(m);
}
std::sort(ms.begin(), ms.end());
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);
cudaGraphExecDestroy(ge);
cudaGraphDestroy(g);
}
return 0;
}