okami.mom/strata

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

Niko1221Merge f-151-amd (d9070189c) into f-split-int258f81f20e

main
4.1 KiB84 linesraw
1// tests/hip/prefill_gr_write_bench.cpp - the hyper-connection write-back (gr_write_norm_rs: R += bo * 2 sigmoid(inj / 4), then the next
2// half's norm row scale and BF16 image) timed at the prompt path's shapes, with the bytes it moves, and a checksum of its
3// outputs so a variant can be compared bit for bit (run twice with STRATA_GRW_V=0 / 1).
4//   gr_write_bench [T=16384] [reps=20]
5#include "strata/prefill/kernels.hpp"
6
7#include <cuda_runtime.h>
8
9#include <cstdint>
10#include <cstdio>
11#include <cstdlib>
12#include <cstring>
13#include <random>
14#include <vector>
15
16namespace {
17constexpr int N = 2560, HC = 4, D = N * HC;
18bool ck(cudaError_t e, const char* what) {
19    if (e != cudaSuccess) { std::printf("FAIL: %s: %s\n", what, cudaGetErrorString(e)); return false; }
20    return true;
21}
22uint64_t fnv(const void* p, size_t n, uint64_t h = 1469598103934665603ull) {
23    const uint8_t* b = (const uint8_t*) p;
24    for (size_t i = 0; i < n; ++i) { h ^= b[i]; h *= 1099511628211ull; }
25    return h;
26}
27}  // namespace
28
29int main(int argc, char** argv) {
30    const int64_t T = argc > 1 ? std::atoll(argv[1]) : 16384;
31    const int reps = argc > 2 ? std::atoi(argv[2]) : 20;
32    std::mt19937 rng(7);
33    std::normal_distribution<float> nd(0.f, 1.f);
34    std::vector<float> R0((size_t) T * D), bo((size_t) T * N), inj((size_t) T * HC), w((size_t) HC * N);
35    for (auto& v : R0) v = nd(rng);
36    for (auto& v : bo) v = nd(rng);
37    for (auto& v : inj) v = nd(rng);
38    for (auto& v : w) v = 0.5f + 0.1f * nd(rng);
39    float *dR = nullptr, *dR0 = nullptr, *dbo = nullptr, *dinj = nullptr, *dw = nullptr, *drs = nullptr;
40    uint16_t* dx = nullptr;
41    bool ok = ck(cudaMalloc(&dR, R0.size() * 4), "R") && ck(cudaMalloc(&dR0, R0.size() * 4), "R0") &&
42              ck(cudaMalloc(&dbo, bo.size() * 4), "bo") && ck(cudaMalloc(&dinj, inj.size() * 4), "inj") &&
43              ck(cudaMalloc(&dw, w.size() * 4), "w") && ck(cudaMalloc(&drs, (size_t) T * HC * 4), "rs") &&
44              ck(cudaMalloc(&dx, (size_t) T * D * 2), "xn16");
45    if (!ok) return 2;
46    cudaMemcpy(dR0, R0.data(), R0.size() * 4, cudaMemcpyHostToDevice);
47    cudaMemcpy(dbo, bo.data(), bo.size() * 4, cudaMemcpyHostToDevice);
48    cudaMemcpy(dinj, inj.data(), inj.size() * 4, cudaMemcpyHostToDevice);
49    cudaMemcpy(dw, w.data(), w.size() * 4, cudaMemcpyHostToDevice);
50    cudaStream_t s;
51    cudaStreamCreate(&s);
52    // one reference pass for the checksum
53    cudaMemcpy(dR, dR0, R0.size() * 4, cudaMemcpyDeviceToDevice);
54    strata::prefill::gr_write_norm_rs(dR, dbo, dinj, HC, dw, 1e-6f, drs, dx, T, s, nullptr, D, false);
55    cudaStreamSynchronize(s);
56    std::vector<float> oR(R0.size()), ors((size_t) T * HC);
57    std::vector<uint16_t> ox((size_t) T * D);
58    cudaMemcpy(oR.data(), dR, oR.size() * 4, cudaMemcpyDeviceToHost);
59    cudaMemcpy(ors.data(), drs, ors.size() * 4, cudaMemcpyDeviceToHost);
60    cudaMemcpy(ox.data(), dx, ox.size() * 2, cudaMemcpyDeviceToHost);
61    std::printf("checksum R %016llx rs %016llx xn16 %016llx\n", (unsigned long long) fnv(oR.data(), oR.size() * 4),
62                (unsigned long long) fnv(ors.data(), ors.size() * 4), (unsigned long long) fnv(ox.data(), ox.size() * 2));
63    cudaEvent_t e0, e1;
64    cudaEventCreate(&e0);
65    cudaEventCreate(&e1);
66    float best = 1e30f, sum = 0;
67    for (int r = 0; r < reps; ++r) {
68        cudaMemcpy(dR, dR0, R0.size() * 4, cudaMemcpyDeviceToDevice);
69        cudaStreamSynchronize(s);
70        cudaEventRecord(e0, s);
71        strata::prefill::gr_write_norm_rs(dR, dbo, dinj, HC, dw, 1e-6f, drs, dx, T, s, nullptr, D, false);
72        cudaEventRecord(e1, s);
73        cudaEventSynchronize(e1);
74        float ms = 0;
75        cudaEventElapsedTime(&ms, e0, e1);
76        best = ms < best ? ms : best;
77        sum += ms;
78    }
79    // bytes: R read + write, bo read (x4 rows, from cache after the first), inj, w, rs, xn16 write
80    const double gb = ((double) T * D * 4 * 2 + (double) T * N * 4 + (double) T * D * 2) / 1e9;
81    std::printf("gr_write_norm_rs T=%lld: best %.3f ms, mean %.3f ms, %.0f GB/s at best (%.2f GB moved, bo counted once)\n",
82                (long long) T, best, sum / reps, gb / (best * 1e-3), gb);
83    return 0;
84}