okami.mom/strata

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

Niko1221Merge branch 'f-split-int' into f-split-vision7c5e84612

main
4.2 KiB76 linesraw
1// Exact multi-row BF16/F32 GEMV checks against independent single-row calls.
2// Run with STRATA_MMVF_ROWS=1; synthetic data, no model or private prompts.
3#include "strata/kernels/bf16_gemv.hpp"
4#include <cuda_runtime.h>
5#include <cstdint>
6#include <cstdio>
7#include <cstdlib>
8#include <cstring>
9#include <random>
10#include <vector>
11
12namespace k = strata::kernels;
13static void ck(cudaError_t e) {
14    if (e != cudaSuccess) { std::fprintf(stderr, "%s\n", cudaGetErrorString(e)); std::exit(2); }
15}
16template<class T> static T* alloc(size_t n) {
17    T* p = nullptr; ck(cudaMalloc(&p, n * sizeof(T))); return p;
18}
19int main() {
20    const char* flag = std::getenv("STRATA_MMVF_ROWS");
21    if (!flag || flag[0] != '1') { std::fprintf(stderr, "set STRATA_MMVF_ROWS=1\n"); return 2; }
22    cudaStream_t s; ck(cudaStreamCreate(&s));
23    const int shapes[][2] = {{64,64},{192,67},{512,65},{640,128},{2560,48},
24                             {2560,128},{2560,512},{2560,2560},{10240,320}};
25    size_t cases = 0, values = 0, mismatches = 0;
26    for (const auto& shape : shapes) for (int nt = 1; nt <= 8; ++nt)
27    for (int pad : {0,2}) for (int range : {0,1,2}) {
28        const int ni = shape[0], no = shape[1], ldx = ni + pad, ldy = no + pad + 1;
29        std::mt19937 rng(1234 + nt * 17 + pad * 31 + range * 53 + ni + no);
30        std::uniform_real_distribution<float> d(-1.f, 1.f);
31        const float xs = range == 0 ? 1.f : range == 1 ? 1.e-12f : 1.e12f;
32        const float ws = range == 0 ? 1.f : range == 1 ? 1.e-8f : 1.e8f;
33        std::vector<float> x((size_t) nt * ldx);
34        std::vector<uint16_t> w((size_t) (no + 1) * ni);
35        for (auto& v : x) v = d(rng) * xs;
36        for (auto& v : w) { const float f = d(rng) * ws; uint32_t b; std::memcpy(&b,&f,4); v = uint16_t(b >> 16); }
37        float* dx = alloc<float>(x.size()); uint16_t* dw = alloc<uint16_t>(w.size());
38        const size_t count = (size_t) nt * ldy;
39        float* ref = alloc<float>(count); float* got = alloc<float>(count);
40        float* aux = alloc<float>((size_t)nt * 3); float* aref = alloc<float>((size_t)nt * 3);
41        ck(cudaMemcpy(dx,x.data(),x.size()*4,cudaMemcpyHostToDevice));
42        ck(cudaMemcpy(dw,w.data(),w.size()*2,cudaMemcpyHostToDevice));
43        ck(cudaMemset(ref,0xff,count*4)); ck(cudaMemset(got,0xff,count*4));
44        ck(cudaMemset(aux,0xff,(size_t)nt*12)); ck(cudaMemset(aref,0xff,(size_t)nt*12));
45        for (int t = 0; t < nt; ++t) {
46            k::bf16_gemv_fp32_mmvf(dx+(size_t)t*ldx,dw,ref+(size_t)t*ldy,ni,no,s);
47            k::bf16_gemv_fp32_mmvf(dx+(size_t)t*ldx,dw+(size_t)no*ni,aref+t*3,ni,1,s);
48        }
49        ck(cudaStreamSynchronize(s));
50        cudaGraph_t graph; cudaGraphExec_t exec;
51        ck(cudaStreamBeginCapture(s,cudaStreamCaptureModeThreadLocal));
52        k::bf16_gemv_fp32_mmvf_multi(dx,ldx,dw,got,ldy,ni,no,nt,s);
53        ck(cudaStreamEndCapture(s,&graph)); ck(cudaGraphInstantiate(&exec,graph,0));
54        ck(cudaGraphLaunch(exec,s)); ck(cudaStreamSynchronize(s));
55        auto compare = [&](float* a, float* b, size_t n) {
56            std::vector<uint32_t> aa(n),bb(n);
57            ck(cudaMemcpy(aa.data(),a,n*4,cudaMemcpyDeviceToHost));
58            ck(cudaMemcpy(bb.data(),b,n*4,cudaMemcpyDeviceToHost));
59            size_t bad = 0; for (size_t i=0;i<n;++i) bad += aa[i]!=bb[i];
60            values += n; mismatches += bad;
61            if (bad) std::printf("FAIL ni=%d no=%d nt=%d pad=%d range=%d different=%zu\n",ni,no,nt,pad,range,bad);
62        };
63        compare(ref,got,count);
64        ck(cudaMemset(got,0xff,count*4));
65        const bool fused = k::bf16_gemv_fp32_mmvf_multi_aux(dx,ldx,dw,got,ldy,ni,no,nt,
66                                                          dw+(size_t)no*ni,aux,3,s);
67        if (fused) { ck(cudaStreamSynchronize(s)); compare(ref,got,count); compare(aref,aux,(size_t)nt*3); }
68        if (fused != (nt >= 2 && no >= 64)) { std::printf("FAIL eligibility\n"); ++mismatches; }
69        ck(cudaGraphExecDestroy(exec)); ck(cudaGraphDestroy(graph));
70        ck(cudaFree(dx)); ck(cudaFree(dw)); ck(cudaFree(ref)); ck(cudaFree(got)); ck(cudaFree(aux)); ck(cudaFree(aref));
71        ++cases;
72    }
73    ck(cudaStreamDestroy(s));
74    std::printf("mmvf_rows_parity: %zu cases, %zu values, %zu mismatches\n",cases,values,mismatches);
75    return mismatches ? 1 : 0;
76}