okami.mom/strata

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

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

main
10.6 KiB218 linesraw
1// src/kernels/b6_mmvq_bench.cpp - B6: what a verify row costs in the dense projections, per kernel, per card.
2//
3//     build/b6_mmvq_bench [reps] [device, -1 = all] [B rows mode: 1 = 2 rows, or 2 / 4 / 8]
4//
5// The decode's dense projections at the model's own shapes and formats (qwen3.8-flash-next GSQ-RCO IQ3_XXS:
6// GDN qkv / gate / out, QSA q / out, shared expert, the head; the BF16 router), called with ncols = 1..5 and 8 as a
7// verify window of that many rows calls them.  Per call: CUDA events around each launch, every launch queued back to
8// back (no host gap inside a measurement), the median of `reps` (default 500) after a warm-up.  The weights rotate
9// over enough copies to exceed the L2, as the decode reads each layer's weights cold.  Synthetic bytes with sane
10// block scales (as mmvq_multi_parity): the time does not depend on the values.
11//
12// Two layouts side by side: the default exact layout (A) and STRATA_B6_MMVQ_ROWS (B, native_mmvq_set_b6_rows).  For
13// every case and ncols: B's output against A's, bit for bit (memcmp), and both against ncols single-column calls.
14// Exit code 1 if any comparison differs.  GPU time only: immune to the host and to any generated text.
15#include "strata/kernels/bf16_gemv.hpp"
16#include "strata/kernels/iq_kernels.hpp"
17#include "strata/kernels/native_mmvq.hpp"
18
19#include <cuda_runtime.h>
20
21#include <algorithm>
22#include <cmath>
23#include <cstdint>
24#include <cstdio>
25#include <cstdlib>
26#include <cstring>
27#include <random>
28#include <vector>
29
30namespace K = strata::kernels;
31
32namespace {
33
34struct Case {
35    const char* name;
36    int type;          // GGML type id; 30 = BF16 (bf16_gemv_fp32_mmvf_multi)
37    int n_in, n_out;
38    int block_elems, block_bytes;
39    int scale_at[2];   // byte offsets of the fp16 scales in a block; -1 = none
40};
41
42const Case CASES[] = {
43    {"gdn qkv Q6_K", 14, 2560, 10240, 256, 210, {208, -1}},
44    {"gdn qkv Q4_K", 12, 2560, 10240, 256, 144, {0, 2}},
45    {"gdn qkv Q5_K", 13, 2560, 10240, 256, 176, {0, 2}},
46    {"gdn qkv IQ4_XS", 23, 2560, 10240, 256, 136, {0, -1}},
47    {"gdn gate Q6_K", 14, 2560, 6144, 256, 210, {208, -1}},
48    {"gdn out Q6_K", 14, 6144, 2560, 256, 210, {208, -1}},
49    {"gdn out IQ4_XS", 23, 6144, 2560, 256, 136, {0, -1}},
50    {"gdn out Q4_K", 12, 6144, 2560, 256, 144, {0, 2}},
51    {"qsa q IQ4_XS", 23, 2560, 12288, 256, 136, {0, -1}},
52    {"qsa q Q6_K", 14, 2560, 12288, 256, 210, {208, -1}},
53    {"shexp up IQ3_S", 21, 2560, 640, 256, 110, {0, -1}},
54    {"shexp gate Q4_K", 12, 2560, 640, 256, 144, {0, 2}},
55    {"shexp down IQ4_NL", 20, 640, 2560, 32, 18, {0, -1}},
56    {"router BF16", 30, 2560, 512, 1, 2, {-1, -1}},
57    {"head Q5_K", 13, 2560, 248320, 256, 176, {0, 2}},
58};
59const int WIDTHS[] = {1, 2, 3, 4, 5, 8};
60
61bool ck(cudaError_t e, const char* what) {
62    if (e == cudaSuccess) return true;
63    std::printf("CUDA: %s: %s\n", what, cudaGetErrorString(e));
64    return false;
65}
66
67uint16_t sane_half(std::mt19937& rng) {
68    const uint32_t r = rng();
69    return (uint16_t) (((r >> 31) << 15) | ((5u + (r >> 10) % 5u) << 10) | (r & 0x3ffu));
70}
71uint16_t bf16(float x) {
72    uint32_t u;
73    std::memcpy(&u, &x, 4);
74    return (uint16_t) ((u + 0x7fff + ((u >> 16) & 1)) >> 16);
75}
76
77struct Buf {
78    std::vector<void*> w;   // weight copies
79    std::size_t wbytes = 0;
80    void* xq = nullptr;     // Q8_1 of 8 columns
81    float* xf = nullptr;    // the float activations (BF16 path)
82    float* y = nullptr;     // 8 columns of output
83};
84
85// one call of the case at `ncols` on weight copy `k` into y
86void call(const Case& c, const Buf& b, int k, int ncols, float* y, cudaStream_t s) {
87    if (c.type == 30)
88        K::bf16_gemv_fp32_mmvf_multi(b.xf, c.n_in, (const uint16_t*) b.w[(size_t) k], y, c.n_out, c.n_in, c.n_out, ncols, s);
89    else
90        K::native_mmvq(c.type, b.w[(size_t) k], b.xq, y, c.n_in, c.n_out, ncols, s);
91}
92// column j alone (ncols = 1), into y + j * n_out
93void call_col(const Case& c, const Buf& b, int j, float* y, cudaStream_t s) {
94    if (c.type == 30)
95        K::bf16_gemv_fp32_mmvf_multi(b.xf + (size_t) j * c.n_in, c.n_in, (const uint16_t*) b.w[0],
96                                     y + (size_t) j * c.n_out, c.n_out, c.n_in, c.n_out, 1, s);
97    else
98        K::native_mmvq(c.type, b.w[0], (const uint8_t*) b.xq + K::native_q8_1_bytes(c.n_in, 1) * (size_t) j,
99                       y + (size_t) j * c.n_out, c.n_in, c.n_out, 1, s);
100}
101
102double median_ms(const Case& c, const Buf& b, int ncols, int reps, cudaStream_t s, std::vector<cudaEvent_t>& ev) {
103    const int nk = (int) b.w.size();
104    for (int r = 0; r < 30; ++r) call(c, b, r % nk, ncols, b.y, s);   // warm-up
105    for (int r = 0; r < reps; ++r) {
106        cudaEventRecord(ev[(size_t) 2 * r], s);
107        call(c, b, r % nk, ncols, b.y, s);
108        cudaEventRecord(ev[(size_t) 2 * r + 1], s);
109    }
110    cudaStreamSynchronize(s);
111    std::vector<float> t((size_t) reps);
112    for (int r = 0; r < reps; ++r) cudaEventElapsedTime(&t[(size_t) r], ev[(size_t) 2 * r], ev[(size_t) 2 * r + 1]);
113    std::nth_element(t.begin(), t.begin() + reps / 2, t.end());
114    return t[(size_t) reps / 2];
115}
116
117}  // namespace
118
119int main(int argc, char** argv) {
120    setvbuf(stdout, nullptr, _IONBF, 0);
121    const int reps = argc > 1 ? std::max(50, std::atoi(argv[1])) : 500;
122    int n_dev = 0;
123    if (!ck(cudaGetDeviceCount(&n_dev), "device count")) return 1;
124    const int only = argc > 2 ? std::atoi(argv[2]) : -1;
125    const int bmode = argc > 3 ? std::atoi(argv[3]) : 1;
126    long long bad = 0;
127    for (int dev = 0; dev < n_dev; ++dev) {
128        if (only >= 0 && dev != only) continue;
129        cudaSetDevice(dev);
130        cudaDeviceProp pr{};
131        cudaGetDeviceProperties(&pr, dev);
132        std::printf("\n=== device %d: %s (sm_%d%d, L2 %d KB), %d reps, median ms per call, B = rows mode %d ===\n", dev, pr.name,
133                    pr.major, pr.minor, pr.l2CacheSize / 1024, reps, bmode);
134        std::printf("%-18s %4s %9s %9s %8s %8s  %s\n", "case", "cols", "A ms", "B ms", "A +col", "B +col",
135                    "bitwise B=A, A=1col, B=1col");
136        cudaStream_t s;
137        if (!ck(cudaStreamCreate(&s), "stream")) return 1;
138        std::vector<cudaEvent_t> ev((size_t) 2 * reps);
139        for (auto& e : ev) cudaEventCreate(&e);
140        for (const Case& c : CASES) {
141            if (c.type != 30 && !K::native_mmvq_supported(c.type)) {
142                std::printf("%-18s unsupported type %d\n", c.name, c.type);
143                continue;
144            }
145            std::mt19937 rng(77u + (unsigned) c.type * 131u + (unsigned) c.n_out);
146            Buf b;
147            b.wbytes = c.type == 30 ? (size_t) c.n_in * c.n_out * 2 : K::native_mmvq_weight_bytes(c.type, c.n_in, c.n_out);
148            std::vector<uint8_t> w(b.wbytes);
149            if (c.type == 30) {
150                std::normal_distribution<float> nd(0.f, 0.05f);
151                auto* p = (uint16_t*) w.data();
152                for (size_t i = 0; i < b.wbytes / 2; ++i) p[i] = bf16(nd(rng));
153            } else {
154                for (auto& x : w) x = (uint8_t) (rng() & 0xff);
155                const size_t nb = (size_t) c.n_out * (size_t) (c.n_in / c.block_elems);
156                for (size_t k = 0; k < nb; ++k)
157                    for (int at : c.scale_at)
158                        if (at >= 0) {
159                            const uint16_t h = sane_half(rng);
160                            std::memcpy(&w[k * (size_t) c.block_bytes + (size_t) at], &h, 2);
161                        }
162            }
163            const size_t copies = std::max<size_t>(1, std::min<size_t>(8, (96u << 20) / b.wbytes + 1));
164            bool ok = true;
165            for (size_t k = 0; k < copies && ok; ++k) {
166                void* d = nullptr;
167                ok = ck(cudaMalloc(&d, b.wbytes), "malloc w") && ck(cudaMemcpy(d, w.data(), b.wbytes, cudaMemcpyHostToDevice), "copy w");
168                b.w.push_back(d);
169            }
170            std::vector<float> x((size_t) 8 * c.n_in);
171            std::normal_distribution<float> nx(0.f, 1.f);
172            for (auto& v : x) v = nx(rng);
173            const size_t ybytes = (size_t) 8 * c.n_out * 4;
174            ok = ok && ck(cudaMalloc(&b.xf, x.size() * 4), "malloc x") &&
175                 ck(cudaMemcpy(b.xf, x.data(), x.size() * 4, cudaMemcpyHostToDevice), "copy x") &&
176                 ck(cudaMalloc(&b.y, ybytes), "malloc y");
177            if (ok && c.type != 30) {
178                ok = ck(cudaMalloc(&b.xq, K::native_q8_1_bytes(c.n_in, 8)), "malloc xq");
179                if (ok) K::quantize_q8_1_rows(b.xf, 8, c.n_in, b.xq, s);
180            }
181            float *ya = nullptr, *yb = nullptr, *y1 = nullptr;
182            ok = ok && ck(cudaMalloc(&ya, ybytes), "ya") && ck(cudaMalloc(&yb, ybytes), "yb") && ck(cudaMalloc(&y1, ybytes), "y1");
183            if (!ok) return 1;
184            double a1 = 0, b1 = 0;
185            for (int T : WIDTHS) {
186                std::vector<uint32_t> ha((size_t) T * c.n_out), hb(ha.size()), h1(ha.size());
187                K::native_mmvq_set_b6_rows(0);
188                const double ta = median_ms(c, b, T, reps, s, ev);
189                call(c, b, 0, T, ya, s);
190                for (int j = 0; j < T; ++j) call_col(c, b, j, y1, s);
191                K::native_mmvq_set_b6_rows(bmode);
192                const double tb = median_ms(c, b, T, reps, s, ev);
193                call(c, b, 0, T, yb, s);
194                K::native_mmvq_set_b6_rows(0);
195                if (!ck(cudaStreamSynchronize(s), "run")) return 1;
196                cudaMemcpy(ha.data(), ya, ha.size() * 4, cudaMemcpyDeviceToHost);
197                cudaMemcpy(hb.data(), yb, hb.size() * 4, cudaMemcpyDeviceToHost);
198                cudaMemcpy(h1.data(), y1, h1.size() * 4, cudaMemcpyDeviceToHost);
199                const bool ba = std::memcmp(ha.data(), hb.data(), ha.size() * 4) == 0;
200                const bool a_1 = std::memcmp(ha.data(), h1.data(), ha.size() * 4) == 0;
201                const bool b_1 = std::memcmp(hb.data(), h1.data(), hb.size() * 4) == 0;
202                long long nonfinite = 0;
203                for (uint32_t u : ha) { float f; std::memcpy(&f, &u, 4); if (!std::isfinite(f)) ++nonfinite; }
204                if (!ba || !b_1 || nonfinite) ++bad;
205                if (T == 1) { a1 = ta; b1 = tb; }
206                std::printf("%-18s %4d %9.4f %9.4f %8.4f %8.4f  %s %s %s%s\n", c.name, T, ta, tb,
207                            T > 1 ? (ta - a1) / (T - 1) : 0.0, T > 1 ? (tb - b1) / (T - 1) : 0.0, ba ? "ok" : "DIFF",
208                            a_1 ? "ok" : "diff", b_1 ? "ok" : "DIFF", nonfinite ? " NONFINITE" : "");
209            }
210            for (void* d : b.w) cudaFree(d);
211            cudaFree(b.xq); cudaFree(b.xf); cudaFree(b.y); cudaFree(ya); cudaFree(yb); cudaFree(y1);
212        }
213        for (auto& e : ev) cudaEventDestroy(e);
214        cudaStreamDestroy(s);
215    }
216    std::printf("\nb6_mmvq_bench: %s (%lld case/width comparisons failed)\n", bad ? "FAILED" : "ok", bad);
217    return bad ? 1 : 0;
218}