okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3fe5edd31
main
1// src/prefill/qsa_nr_parity.cu - the QSA layer's fused norm + rope (rms_rope_rows, STRATA_QSA_FUSE_NR) against the chain it 2// replaces (split_q + rms_rows + rope), compared BIT FOR BIT over the whole output, for the three uses in the prompt path: 3// query Qf [T, 24, 512] -> q [T, 24, 256] (split, norm, rotate) 4// key Kc [T, 2, 256] in place (norm, rotate) 5// indexer q_idx [T, 4, 128] with row stride 512, in place (norm, rotate) 6// Every output element is checked (not a sample), the output buffers are poisoned with NaN first, and T runs through 7// 1 .. 8191 with several start positions, so a tile or row that is not written shows up. Exit 77 when there is no GPU. 8// qsa_nr_parity the checks; qsa_nr_parity --bench and the timings at T = 2048 / 8191 9#include "strata/kernels/rope_scaling.hpp" 10#include "strata/prefill/kernels.hpp" 11 12#include <cuda_runtime.h> 13 14#include <algorithm> 15#include <cmath> 16#include <cstdint> 17#include <cstdio> 18#include <cstring> 19#include <random> 20#include <string> 21#include <vector> 22 23namespace { 24 25bool ck(cudaError_t e, const char* what) { 26 if (e != cudaSuccess) std::printf("FAIL: %s: %s\n", what, cudaGetErrorString(e)); 27 return e == cudaSuccess; 28} 29template <typename T> T* dalloc(size_t n) { 30 T* p = nullptr; 31 if (!ck(cudaMalloc(&p, n * sizeof(T)), "cudaMalloc")) std::exit(2); 32 return p; 33} 34template <typename T> std::vector<T> download(const T* d, size_t n) { 35 std::vector<T> h(n); 36 ck(cudaMemcpy(h.data(), d, n * sizeof(T), cudaMemcpyDeviceToHost), "download"); 37 return h; 38} 39void fill(std::vector<float>& v, std::mt19937& rng, float scale) { 40 std::normal_distribution<float> nd(0.0f, scale); 41 for (float& x : v) x = nd(rng); 42} 43size_t diffs(const std::vector<float>& a, const std::vector<float>& b, size_t& first) { 44 size_t n = 0; 45 first = (size_t) -1; 46 for (size_t i = 0; i < a.size(); ++i) { 47 uint32_t x, y; 48 std::memcpy(&x, &a[i], 4); 49 std::memcpy(&y, &b[i], 4); 50 if (x != y) { 51 if (first == (size_t) -1) first = i; 52 ++n; 53 } 54 } 55 return n; 56} 57 58} // namespace 59 60int main(int argc, char** argv) { 61 using namespace strata::prefill; 62 int ndev = 0; 63 if (cudaGetDeviceCount(&ndev) != cudaSuccess || ndev == 0) { 64 std::printf("SKIP: no GPU\n"); 65 return 77; 66 } 67 const bool bench = argc > 1 && std::string(argv[1]) == "--bench"; 68 const strata::kernels::RopeScaling& rs = strata::kernels::rope_scaling(); 69 const int64_t TMAX = 8191; 70 std::mt19937 rng(12345); 71 std::vector<float> hqf((size_t) TMAX * 24 * 512), hk((size_t) TMAX * 512), hqi((size_t) TMAX * 512), wq(256), wk(256), wi(128); 72 fill(hqf, rng, 2.0f); 73 fill(hk, rng, 3.0f); 74 fill(hqi, rng, 1.5f); 75 for (float& x : wq) x = 0.5f + std::abs(std::normal_distribution<float>(0.0f, 1.0f)(rng)); 76 for (float& x : wk) x = 0.5f + std::abs(std::normal_distribution<float>(0.0f, 1.0f)(rng)); 77 for (float& x : wi) x = 0.5f + std::abs(std::normal_distribution<float>(0.0f, 1.0f)(rng)); 78 // a few rows with large and tiny magnitudes, and an all-zero row (rsqrt(eps)) 79 for (int d = 0; d < 256; ++d) { hqf[(size_t) 3 * 24 * 512 + 512 * 5 + d] = 0.0f; hqf[(size_t) 4 * 24 * 512 + d] *= 1e4f; hk[(size_t) 7 * 512 + d] *= 1e-6f; } 80 float *dqf = dalloc<float>(hqf.size()), *dk0 = dalloc<float>(hk.size()), *dk1 = dalloc<float>(hk.size()), 81 *dq0 = dalloc<float>((size_t) TMAX * 24 * 256), *dq1 = dalloc<float>((size_t) TMAX * 24 * 256), 82 *di0 = dalloc<float>(hqi.size()), *di1 = dalloc<float>(hqi.size()), *dwq = dalloc<float>(256), *dwk = dalloc<float>(256), 83 *dwi = dalloc<float>(128); 84 ck(cudaMemcpy(dqf, hqf.data(), hqf.size() * 4, cudaMemcpyHostToDevice), "up"); 85 ck(cudaMemcpy(dwq, wq.data(), 1024, cudaMemcpyHostToDevice), "up"); 86 ck(cudaMemcpy(dwk, wk.data(), 1024, cudaMemcpyHostToDevice), "up"); 87 ck(cudaMemcpy(dwi, wi.data(), 512, cudaMemcpyHostToDevice), "up"); 88 const float eps = 1e-6f; 89 int fails = 0; 90 const int64_t Ts[] = {1, 2, 7, 31, 33, 64, 257, 1000, 2048, 4093, 8191}; 91 const int64_t P0s[] = {0, 5, 4093, 131071}; 92 for (int64_t T : Ts) { 93 for (int64_t p0 : P0s) { 94 // old chain 95 ck(cudaMemset(dq0, 0xff, (size_t) T * 24 * 256 * 4), "poison"); 96 ck(cudaMemset(dq1, 0xff, (size_t) T * 24 * 256 * 4), "poison"); 97 ck(cudaMemcpy(dk0, hk.data(), (size_t) T * 512 * 4, cudaMemcpyHostToDevice), "up"); 98 ck(cudaMemcpy(dk1, hk.data(), (size_t) T * 512 * 4, cudaMemcpyHostToDevice), "up"); 99 ck(cudaMemcpy(di0, hqi.data(), (size_t) T * 512 * 4, cudaMemcpyHostToDevice), "up"); 100 ck(cudaMemcpy(di1, hqi.data(), (size_t) T * 512 * 4, cudaMemcpyHostToDevice), "up"); 101 split_q(dqf, dq0, T, nullptr); 102 rms_rows(dq0, dwq, T * 24, 256, 256, eps, nullptr); 103 rope(dq0, T, 24, 256, 6144, p0, rs, nullptr); 104 rms_rows(dk0, dwk, T * 2, 256, 256, eps, nullptr); 105 rope(dk0, T, 2, 256, 512, p0, rs, nullptr); 106 rms_rows(di0, dwi, T * 4, 128, 128, eps, nullptr); 107 rope(di0, T, 4, 128, 512, p0, rs, nullptr); 108 // fused 109 rms_rope_rows(dqf, 24 * 512, 512, dq1, 6144, dwq, T, 24, 256, eps, p0, rs, nullptr); 110 rms_rope_rows(dk1, 512, 256, dk1, 512, dwk, T, 2, 256, eps, p0, rs, nullptr); 111 rms_rope_rows(di1, 512, 128, di1, 512, dwi, T, 4, 128, eps, p0, rs, nullptr); 112 if (!ck(cudaDeviceSynchronize(), "sync")) return 1; 113 size_t f0 = 0, f1 = 0, f2 = 0; 114 const size_t nq = diffs(download(dq0, (size_t) T * 24 * 256), download(dq1, (size_t) T * 24 * 256), f0); 115 const size_t nk = diffs(download(dk0, (size_t) T * 512), download(dk1, (size_t) T * 512), f1); 116 const size_t ni = diffs(download(di0, (size_t) T * 512), download(di1, (size_t) T * 512), f2); 117 // the poison must be gone everywhere in the fused query output 118 const std::vector<float> q1 = download(dq1, (size_t) T * 24 * 256); 119 size_t nan = 0; 120 for (float x : q1) nan += x != x; 121 const bool ok = nq == 0 && nk == 0 && ni == 0 && nan == 0; 122 if (!ok) ++fails; 123 std::printf("T %5lld p0 %6lld q diff %zu (first %lld) k diff %zu (first %lld) idx diff %zu (first %lld) NaN %zu %s\n", 124 (long long) T, (long long) p0, nq, (long long) f0, nk, (long long) f1, ni, (long long) f2, nan, ok ? "OK" : "FAIL"); 125 } 126 } 127 if (bench) { 128 cudaEvent_t a, b; 129 cudaEventCreate(&a); 130 cudaEventCreate(&b); 131 for (int64_t T : {(int64_t) 2048, (int64_t) 8191}) { 132 auto time = [&](auto&& fn) { 133 for (int i = 0; i < 3; ++i) fn(); 134 cudaEventRecord(a); 135 for (int i = 0; i < 20; ++i) fn(); 136 cudaEventRecord(b); 137 cudaEventSynchronize(b); 138 float ms = 0; 139 cudaEventElapsedTime(&ms, a, b); 140 return ms / 20.0f; 141 }; 142 const float old_ms = time([&] { 143 split_q(dqf, dq0, T, nullptr); 144 rms_rows(dq0, dwq, T * 24, 256, 256, eps, nullptr); 145 rope(dq0, T, 24, 256, 6144, 100, rs, nullptr); 146 rms_rows(dk0, dwk, T * 2, 256, 256, eps, nullptr); 147 rope(dk0, T, 2, 256, 512, 100, rs, nullptr); 148 rms_rows(di0, dwi, T * 4, 128, 128, eps, nullptr); 149 rope(di0, T, 4, 128, 512, 100, rs, nullptr); 150 }); 151 const float new_ms = time([&] { 152 rms_rope_rows(dqf, 24 * 512, 512, dq1, 6144, dwq, T, 24, 256, eps, 100, rs, nullptr); 153 rms_rope_rows(dk1, 512, 256, dk1, 512, dwk, T, 2, 256, eps, 100, rs, nullptr); 154 rms_rope_rows(di1, 512, 128, di1, 512, dwi, T, 4, 128, eps, 100, rs, nullptr); 155 }); 156 std::printf("bench T %lld: chain %.3f ms, fused %.3f ms (x%.2f) per QSA layer\n", (long long) T, old_ms, new_ms, 157 old_ms / new_ms); 158 } 159 } 160 std::printf(fails == 0 ? "ALL OK\n" : "%d FAILED\n", fails); 161 return fails == 0 ? 0 : 1; 162}