okami.mom/strata

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

Niko1221Merge f-intel-quants into f-144-rel9adf4e01a

main
4.7 KiB109 linesraw
1// src/kernels/q8_gemv_bench.cpp - MODEL-UPDATE: the dense Q8_0 GEMVs of Unsloth's files (attention, SSM, shared expert,
2// hyper-connections, head: all Q8_0 there) at the engine's shapes, 1..6 columns (a verify window's tokens), through
3// native_mmvq - the call the engine makes - against the card's memory bandwidth.  Enough copies of each matrix are
4// cycled to miss the GPU's last-level cache, as a layer's call does.
5//
6//     build/q8_gemv_bench [--cols 1,2,3,4,6] [--iters 50] [--bw GB/s]
7#include "strata/kernels/native_mmvq.hpp"
8
9#include <cuda_runtime.h>
10
11#include <algorithm>
12#include <cstdint>
13#include <cstdio>
14#include <cstdlib>
15#include <cstring>
16#include <random>
17#include <string>
18#include <vector>
19
20namespace K = strata::kernels;
21
22namespace {
23struct Shape { const char* what; int n_in, n_out; };
24const Shape SHAPES[] = {
25    {"GDN qkv 2560->10240", 2560, 10240},  {"GDN gate 2560->6144", 2560, 6144}, {"GDN/QSA out 6144->2560", 6144, 2560},
26    {"QSA q 2560->12288", 2560, 12288},    {"QSA k/v 2560->512", 2560, 512},    {"HC down 10240->320", 10240, 320},
27    {"HC up 320->10240", 320, 10240},      {"shared gate/up 2560->640", 2560, 640}, {"shared down 640->2560", 640, 2560},
28    {"head 2560->248320", 2560, 248320},
29};
30void ck(cudaError_t e, const char* w) {
31    if (e != cudaSuccess) { std::fprintf(stderr, "%s: %s\n", w, cudaGetErrorString(e)); std::exit(1); }
32}
33std::vector<int> ints(const std::string& s) {
34    std::vector<int> v;
35    size_t from = 0;
36    for (;;) {
37        const size_t c = s.find(',', from);
38        v.push_back(std::atoi(s.substr(from, c == std::string::npos ? std::string::npos : c - from).c_str()));
39        if (c == std::string::npos) break;
40        from = c + 1;
41    }
42    return v;
43}
44}  // namespace
45
46int main(int argc, char** argv) {
47    std::vector<int> cols = {1, 2, 3, 4, 6};
48    int iters = 50;
49    double bw = 0;
50    for (int i = 1; i < argc; ++i) {
51        const std::string a = argv[i];
52        if (a == "--cols" && i + 1 < argc) cols = ints(argv[++i]);
53        else if (a == "--iters" && i + 1 < argc) iters = std::atoi(argv[++i]);
54        else if (a == "--bw" && i + 1 < argc) bw = std::atof(argv[++i]);
55    }
56    cudaDeviceProp prop;
57    ck(cudaGetDeviceProperties(&prop, 0), "props");
58    std::printf("q8_gemv_bench: %s\n", prop.name);
59    std::mt19937 rng(7);
60    for (const Shape& s : SHAPES) {
61        const size_t bytes = (size_t) s.n_out * (s.n_in / 32) * 34;
62        const int copies = (int) std::max<size_t>(1, std::min<size_t>(16, ((size_t) 512 << 20) / bytes));
63        std::vector<uint8_t> host(bytes);
64        for (size_t b = 0; b < bytes; b += 34) {   // fp16 scale 0.01 (0x211f), then random int8 weights
65            host[b] = 0x1f;
66            host[b + 1] = 0x21;
67            for (int k = 0; k < 32; ++k) host[b + 2 + k] = (uint8_t) (rng() & 0xff);
68        }
69        std::vector<void*> w(copies);
70        for (auto& p : w) {
71            ck(cudaMalloc(&p, bytes), "malloc w");
72            ck(cudaMemcpy(p, host.data(), bytes, cudaMemcpyHostToDevice), "copy w");
73        }
74        const int maxc = *std::max_element(cols.begin(), cols.end());
75        std::vector<float> xh((size_t) s.n_in * maxc);
76        for (auto& v : xh) v = std::normal_distribution<float>(0.f, 1.f)(rng);
77        float *x = nullptr, *y = nullptr;
78        void* xq = nullptr;
79        ck(cudaMalloc(&x, xh.size() * 4), "x");
80        ck(cudaMemcpy(x, xh.data(), xh.size() * 4, cudaMemcpyHostToDevice), "x copy");
81        ck(cudaMalloc(&xq, K::native_q8_1_bytes(s.n_in, maxc)), "xq");
82        ck(cudaMalloc(&y, (size_t) s.n_out * maxc * 4), "y");
83        cudaStream_t st;
84        ck(cudaStreamCreate(&st), "stream");
85        cudaEvent_t e0, e1;
86        cudaEventCreate(&e0);
87        cudaEventCreate(&e1);
88        std::printf("\n%s: %.2f MB, %d copies\n", s.what, bytes / 1e6, copies);
89        for (int c : cols) {
90            K::native_quantize_q8_1(x, xq, s.n_in, c, st);
91            for (int i = 0; i < 3; ++i) K::native_mmvq(8, w[i % copies], xq, y, s.n_in, s.n_out, c, st);
92            cudaEventRecord(e0, st);
93            for (int i = 0; i < iters; ++i) K::native_mmvq(8, w[i % copies], xq, y, s.n_in, s.n_out, c, st);
94            cudaEventRecord(e1, st);
95            ck(cudaEventSynchronize(e1), "sync");
96            float ms = 0;
97            cudaEventElapsedTime(&ms, e0, e1);
98            const double us = 1000.0 * ms / iters, gbs = bytes / (us * 1e3);
99            std::printf("  %d col%s: %8.1f us  %7.1f GB/s%s\n", c, c == 1 ? " " : "s", us, gbs,
100                        bw > 0 ? (" = " + std::to_string((int) (100 * gbs / bw + 0.5)) + "% of bw").c_str() : "");
101        }
102        for (auto p : w) cudaFree(p);
103        cudaFree(x);
104        cudaFree(xq);
105        cudaFree(y);
106        cudaStreamDestroy(st);
107    }
108    return 0;
109}