okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
40ba0cf93
main
1// src/prefill/gemm_f32_parity.cu - Pascal: Gemm::f16's opt-in FP32 route (cublasSgemm on FP32 copies of the FP16 2// operands, STRATA_DENSE_F32=1) against the cuBLAS FP16-operand call it replaces, bitwise, over the dense projections' 3// shapes and a sweep of chunk sizes T (GPU, synthetic, no model). Whether the two agree is a property of the cuBLAS 4// build (measured: 12.4.5.8 on a Tesla P40, 5,201 sizes): run this before setting STRATA_DENSE_F32=1. 5// 6// build/gemm_f32_parity [--quick | --full] [--bench] 7// default: every 7th T from the route's first to 8192, every T of 7000-7100 and around the powers of two, 8192; 8// --quick: every 251st T; --full: every T. --bench prints both times for a few T. Exit 0: every output bitwise 9// equal (or the route is not built here: not the experimental CUDA 12 engine). 10#include "strata/prefill/gemm.hpp" 11 12#include <cuda_fp16.h> 13#include <cuda_runtime.h> 14 15#include <algorithm> 16#include <cstdint> 17#include <cstdio> 18#include <cstring> 19#include <set> 20#include <string> 21#include <vector> 22 23namespace { 24__global__ void fill_f16(__half* p, int64_t n, unsigned seed, float scale) { 25 const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; 26 if (i >= n) return; 27 unsigned x = (unsigned) i * 2654435761u + seed; 28 x ^= x >> 16; x *= 2246822519u; x ^= x >> 13; x *= 3266489917u; x ^= x >> 16; 29 p[i] = __float2half(((float) (x & 0xffff) / 32768.0f - 1.0f) * scale); 30} 31__global__ void count_diff(const unsigned* a, const unsigned* b, int64_t n, unsigned* out) { 32 const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; 33 if (i < n && a[i] != b[i]) atomicAdd(out, 1u); 34} 35bool ok(cudaError_t e, const char* what) { 36 if (e != cudaSuccess) std::printf("FAIL: %s: %s\n", what, cudaGetErrorString(e)); 37 return e == cudaSuccess; 38} 39struct Shape { const char* what; int64_t N, K, t_min; }; 40} // namespace 41 42int main(int argc, char** argv) { 43 setvbuf(stdout, nullptr, _IONBF, 0); 44 bool quick = false, full = false, bench = false; 45 for (int i = 1; i < argc; ++i) { 46 const std::string a = argv[i]; 47 quick |= a == "--quick"; full |= a == "--full"; bench |= a == "--bench"; 48 } 49 int dev = 0, maj = 0, min = 0; 50 cudaGetDevice(&dev); 51 cudaDeviceGetAttribute(&maj, cudaDevAttrComputeCapabilityMajor, dev); 52 cudaDeviceGetAttribute(&min, cudaDevAttrComputeCapabilityMinor, dev); 53 std::printf("gemm_f32_parity: compute capability %d.%d\n", maj, min); 54 cudaStream_t st = nullptr; 55 if (!ok(cudaStreamCreate(&st), "stream")) return 1; 56 strata::prefill::Gemm gemm; 57 std::string err; 58 if (!gemm.init(st, 0, err)) { std::printf("FAIL: %s\n", err.c_str()); return 1; } 59 // the shapes the route takes and its first T for each (gemm.cu, dense_f32_min_t) 60 const Shape shapes[] = {{"gdn qkv", 10240, 2560, 256}, {"qsa query", 12288, 2560, 256}, {"gdn gate", 6144, 2560, 512}, 61 {"gdn out", 2560, 6144, 4096}, {"qsa out", 2560, 4096, 4096}}; 62 const int64_t TMAX = 8192; 63 unsigned* cnt = nullptr; 64 if (!ok(cudaMalloc((void**) &cnt, 4), "counter")) return 1; 65 cudaEvent_t e0, e1; 66 cudaEventCreate(&e0); 67 cudaEventCreate(&e1); 68 long long cases = 0, bad_cases = 0; 69 for (const Shape& s : shapes) { 70 const int64_t N = s.N, K = s.K; 71 uint16_t *W = nullptr, *X = nullptr; 72 float *Ya = nullptr, *Yb = nullptr; 73 if (!ok(cudaMalloc((void**) &W, (size_t) N * K * 2), "W") || !ok(cudaMalloc((void**) &X, (size_t) TMAX * K * 2), "X") || 74 !ok(cudaMalloc((void**) &Ya, (size_t) TMAX * N * 4), "Ya") || !ok(cudaMalloc((void**) &Yb, (size_t) TMAX * N * 4), "Yb")) return 1; 75 fill_f16<<<(unsigned) ((N * K + 255) / 256), 256, 0, st>>>((__half*) W, N * K, 1u + (unsigned) N, 0.05f); 76 fill_f16<<<(unsigned) ((TMAX * K + 255) / 256), 256, 0, st>>>((__half*) X, TMAX * K, 7u + (unsigned) K, 2.0f); 77 std::set<int64_t> ts; 78 const int64_t step = full ? 1 : quick ? 251 : 7; 79 for (int64_t t = s.t_min; t <= TMAX; t += step) ts.insert(t); 80 ts.insert(s.t_min); ts.insert(TMAX); 81 if (!quick) { 82 for (int64_t t = 7000; t <= 7100; ++t) if (t >= s.t_min) ts.insert(t); 83 for (int64_t p = 256; p <= TMAX; p *= 2) for (int64_t d = -3; d <= 3; ++d) if (p + d >= s.t_min && p + d <= TMAX) ts.insert(p + d); 84 for (int64_t t = 6540; t <= 6570; ++t) if (t >= s.t_min) ts.insert(t); // around the activation slices' first split (K 2560) 85 } 86 long long bad = 0, bad_outputs = 0; 87 int64_t first_bad = 0; 88 for (int64_t T : ts) { 89 strata::prefill::gemm_set_dense_f32(0); 90 gemm.f16(X, W, Ya, T, N, K); 91 strata::prefill::gemm_set_dense_f32(1); 92 gemm.f16(X, W, Yb, T, N, K); 93 cudaMemsetAsync(cnt, 0, 4, st); 94 count_diff<<<(unsigned) ((T * N + 255) / 256), 256, 0, st>>>((const unsigned*) Ya, (const unsigned*) Yb, T * N, cnt); 95 unsigned nd = 0; 96 cudaMemcpyAsync(&nd, cnt, 4, cudaMemcpyDeviceToHost, st); 97 if (!ok(cudaStreamSynchronize(st), "sync")) return 1; 98 ++cases; 99 if (nd) { if (!bad) first_bad = T; ++bad; bad_outputs += nd; } 100 } 101 std::printf("%-10s %5lld x %-5lld: %zu sizes from T %lld to %lld, %lld with different bits%s\n", s.what, (long long) N, (long long) K, ts.size(), 102 (long long) *ts.begin(), (long long) *ts.rbegin(), bad, bad ? "" : " (bitwise equal)"); 103 if (bad) std::printf("FAIL %s: first at T %lld, %lld outputs in all\n", s.what, (long long) first_bad, bad_outputs); 104 bad_cases += bad; 105 if (bench) { 106 for (int64_t T : {(int64_t) 560, (int64_t) 2048, (int64_t) 4096, (int64_t) 7040, (int64_t) 7041}) { 107 if (T < s.t_min) continue; 108 double ms[2] = {0, 0}; 109 for (int on = 0; on < 2; ++on) { 110 strata::prefill::gemm_set_dense_f32(on); 111 gemm.f16(X, W, Ya, T, N, K); 112 cudaEventRecord(e0, st); 113 for (int i = 0; i < 3; ++i) gemm.f16(X, W, Ya, T, N, K); 114 cudaEventRecord(e1, st); 115 cudaEventSynchronize(e1); 116 float t = 0; 117 cudaEventElapsedTime(&t, e0, e1); 118 ms[on] = t / 3; 119 } 120 std::printf("BENCH %-10s T %5lld | cuBLAS FP16 %7.2f ms | FP32 copies %7.2f ms (the conversions included) | %.2fx\n", s.what, (long long) T, ms[0], 121 ms[1], ms[0] / ms[1]); 122 } 123 } 124 cudaFree(W); cudaFree(X); cudaFree(Ya); cudaFree(Yb); 125 } 126 strata::prefill::gemm_set_dense_f32(-1); 127 std::printf("gemm_f32_parity: %lld sizes, %s\n", cases, bad_cases ? "FAILED" : "OK (bitwise)"); 128 return bad_cases ? 1 : 0; 129}