okami.mom/strata

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

Niko1221rc1 merge df2-mgpu 3e7b8343b + HIP GEMM table (b353ce7d4): generate.cpp conflicted in 2 hunks, both sides keptee21447ea

main
4.7 KiB88 linesraw
1// src/core/dflash2_gemm_bench.cpp - the drafter's Q8 GEMM (kernels/dflash2.hpp df2_gemm) on random planar weights: GB/s per shape and column count.
2//
3//     dflash2_gemm_bench [iters] [q4]      (q4: the Q4_0 pack's GEMM, kernels/cuda/dflash2.cu gemm4_*)
4//
5// Weight sets are cycled through 2 GiB of distinct matrices so the L2 cache cannot hold them (the drafter reads each matrix once per round).
6#include "strata/kernels/dflash2.hpp"
7
8#include <cuda_runtime.h>
9
10#include <cstdint>
11#include <cstdio>
12#include <cstdlib>
13#include <cstring>
14#include <vector>
15
16int main(int argc, char** argv) {
17    const int iters = argc > 1 ? std::atoi(argv[1]) : 50;
18    const bool q4 = argc > 2 && std::strcmp(argv[2], "q4") == 0;
19    const int wnum = q4 ? 18 : 34;   // bytes of a 32-weight block: the Q8 pack's int8 + fp16 scale, the Q4_0 pack's nibbles + fp16 scale
20    struct Shape { const char* name; int n_in, n_out; int mats; };
21    const Shape shapes[] = {{"gate+up 2560->7680 x2", 2560, 7680, 2}, {"down 7680->2560", 7680, 2560, 1}, {"qkv 2560->(4096,1024,1024)", 2560, 4096, 3},
22                            {"o 4096->2560", 4096, 2560, 1}, {"fc 12800->2560", 12800, 2560, 1}, {"kv 2560->1024 x2", 2560, 1024, 2},
23                            {"akp 2560->640", 2560, 640, 1}, {"sel 2560->256", 2560, 256, 1}};
24    cudaEvent_t a, b;
25    cudaEventCreate(&a);
26    cudaEventCreate(&b);
27    for (const Shape& sh : shapes) {
28        const size_t wbytes = (size_t) sh.n_out * sh.n_in * wnum / 32;
29        const size_t per_set = wbytes * sh.mats;
30        const int sets = (int) std::max<size_t>(2, ((size_t) 1 << 30) / per_set);
31        std::vector<uint8_t*> w((size_t) sets * sh.mats);
32        for (auto& p : w) {
33            if (cudaMalloc((void**) &p, wbytes) != cudaSuccess) { std::fprintf(stderr, "alloc\n"); return 1; }
34            cudaMemset(p, 0x11, wbytes);
35        }
36        uint8_t* xq;
37        float *xd, *y;
38        cudaMalloc((void**) &xq, (size_t) sh.n_in * 8);
39        cudaMalloc((void**) &xd, (size_t) sh.n_in / 32 * 8 * 4);
40        cudaMalloc((void**) &y, (size_t) 8 * sh.n_out * 3 * 4);
41        {
42            std::vector<int8_t> hx((size_t) sh.n_in * 8);
43            uint32_t rs = 777u;
44            for (auto& v : hx) { rs = rs * 1664525u + 1013904223u; v = (int8_t) (rs >> 20); }
45            cudaMemcpy(xq, hx.data(), hx.size(), cudaMemcpyHostToDevice);
46            std::vector<float> ones((size_t) sh.n_in / 32 * 8);
47            for (auto& v : ones) { rs = rs * 1664525u + 1013904223u; v = 0.005f + 0.001f * (float) ((rs >> 20) % 16); }
48            cudaMemcpy(xd, ones.data(), ones.size() * 4, cudaMemcpyHostToDevice);
49        }
50        std::vector<float> yref;
51        std::vector<int> vars = {1, 2, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23};
52        if (q4) for (int v = 24; v <= 45; ++v) vars.push_back(v);
53        for (int var : vars) {
54            strata::kernels::df2_gemm_variant(var);
55            const int ncols = 8;
56            const size_t ny = (size_t) 8 * sh.n_out * (sh.mats == 3 ? 3 : sh.mats);
57            {   // bitwise against variant 1
58                strata::kernels::Df2Mat m[3];
59                for (int i = 0; i < sh.mats; ++i) m[i] = {w[(size_t) i], y + (size_t) i * 8 * sh.n_out, sh.n_out, q4};
60                if (sh.mats == 3) { m[1].n_out = 1024; m[2].n_out = 1024; }
61                cudaMemset(y, 0, ny * 4);
62                strata::kernels::df2_gemm(m, sh.mats, xq, xd, sh.n_in, ncols, nullptr);
63                cudaDeviceSynchronize();
64                std::vector<float> hy(ny);
65                cudaMemcpy(hy.data(), y, ny * 4, cudaMemcpyDeviceToHost);
66                if (var == 1) yref = hy;
67                std::printf("%s %-30s variant %2d: %s  ", q4 ? "q4" : "q8", sh.name, var, std::memcmp(hy.data(), yref.data(), ny * 4) == 0 ? "bitwise" : "DIFFERS");
68            }
69            cudaDeviceSynchronize();
70            cudaEventRecord(a);
71            for (int it = 0; it < iters; ++it) {
72                strata::kernels::Df2Mat m[3];
73                for (int i = 0; i < sh.mats; ++i) m[i] = {w[(size_t) (it % sets) * sh.mats + i], y + (size_t) i * 8 * sh.n_out, sh.n_out, q4};
74                if (sh.mats == 3) { m[1].n_out = 1024; m[2].n_out = 1024; }
75                strata::kernels::df2_gemm(m, sh.mats, xq, xd, sh.n_in, ncols, nullptr);
76            }
77            cudaEventRecord(b);
78            cudaEventSynchronize(b);
79            float ms;
80            cudaEventElapsedTime(&ms, a, b);
81            const double bytes = sh.mats == 3 ? (double) (4096 + 1024 + 1024) * sh.n_in * wnum / 32 : (double) per_set;
82            std::printf("%7.1f us  %6.1f GB/s\n", 1000.0 * ms / iters, bytes * iters / (ms / 1000.0) / 1e9);
83        }
84        for (auto p : w) cudaFree(p);
85        cudaFree(xq); cudaFree(xd); cudaFree(y);
86    }
87    return 0;
88}