okami.mom/strata

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

Niko1221Merge f-own-2a (7c09c4a75) into f-own-int: the Strata quant layer and the x86 CPU rows9c143c403

main
5.0 KiB106 linesraw
1// tests/quant/quant_bench.cpp - one-thread speed of every dot kernel of the layer (all contracts and ISAs this CPU can run), no ggml.
2//
3// usage: quant_bench [--type NAME] [--mb N] [--reps N] [--n N] [--arith avx2|avx|generic]
4// For each weight type and each runnable kernel: GB/s of weight bytes with the rows streamed from DRAM (a --mb MB set) and with them resident in L2
5// (256 KB).  Interleaved A/B discipline: the kernels of one type run round-robin, `reps` rounds, the median of each is printed.
6#include <algorithm>
7#include <chrono>
8#include <cstdio>
9#include <cstdlib>
10#include <cstring>
11#include <string>
12#include <vector>
13
14#include "strata/quant/act.hpp"
15#include "strata/quant/dot.hpp"
16#include "strata/quant/isa.hpp"
17#include "strata/quant/types.hpp"
18
19namespace sq = strata::quant;
20
21namespace {
22uint64_t g_s = 88172645463325252ull;
23uint32_t rnd() {
24    g_s ^= g_s << 13;
25    g_s ^= g_s >> 7;
26    g_s ^= g_s << 17;
27    return (uint32_t) (g_s >> 16);
28}
29
30double run(const sq::DotKernel* k, const std::vector<uint8_t>& w, size_t rb, int n, size_t rows, const void* act, float* sink) {
31    using C = std::chrono::steady_clock;
32    const auto t0 = C::now();
33    float acc = 0.f;
34    for (size_t r = 0; r < rows; ++r) {
35        float s;
36        k->fn(n, &s, w.data() + r * rb, act);
37        acc += s;
38    }
39    *sink += acc;
40    return std::chrono::duration<double>(C::now() - t0).count();
41}
42}  // namespace
43
44int main(int argc, char** argv) {
45    const char* only = nullptr;
46    int mb = 256, reps = 7, n_el = 4096;
47    sq::Arith want = sq::default_arith();
48    for (int i = 1; i < argc; ++i) {
49        if (!std::strcmp(argv[i], "--type") && i + 1 < argc) only = argv[++i];
50        else if (!std::strcmp(argv[i], "--mb") && i + 1 < argc) mb = std::atoi(argv[++i]);
51        else if (!std::strcmp(argv[i], "--reps") && i + 1 < argc) reps = std::atoi(argv[++i]);
52        else if (!std::strcmp(argv[i], "--n") && i + 1 < argc) n_el = std::atoi(argv[++i]);
53        else if (!std::strcmp(argv[i], "--arith") && i + 1 < argc) {
54            const std::string a = argv[++i];
55            want = a == "avx" ? sq::Arith::Avx : a == "generic" ? sq::Arith::Generic : sq::Arith::Avx2;
56        }
57    }
58    std::printf("quant_bench: contract %s, best isa %s, DRAM set %d MB, L2 set 256 KB, n=%d, median of %d\n", sq::arith_name(want), sq::isa_name(sq::best_isa()),
59                mb, n_el, reps);
60    float sink = 0.f;
61    for (int t = 0; t < 150; ++t) {
62        const sq::TypeInfo* ti = sq::type_info(t);
63        if (ti == nullptr || !ti->has_dot || (only && std::strcmp(only, ti->name))) continue;
64        const sq::DotKernel* ks[16];
65        const int nk = sq::dot_kernels(ti->type, want, ks, 16);
66        std::vector<const sq::DotKernel*> run_k;
67        for (int i = 0; i < nk; ++i)
68            if (sq::isa_ok(ks[i]->isa)) run_k.push_back(ks[i]);
69        if (run_k.empty()) continue;
70        const sq::TypeInfo* ai = sq::type_info(ti->act);
71        const int unit = std::max(ti->blck, ai->blck);
72        const int n = n_el / unit * unit;
73        const size_t rb = sq::row_bytes(t, n);
74        const size_t rows_dram = (size_t) mb * 1048576 / rb, rows_l2 = std::max<size_t>(1, 256 * 1024 / rb);
75        std::vector<uint8_t> w(rows_dram * rb);
76        for (auto& b : w) b = (uint8_t) rnd();
77        // sane scales: first two bytes of each block as fp16 ~ 0.01
78        for (size_t o = 0; o + ti->bytes <= w.size(); o += ti->bytes) {
79            if (ti->type == sq::Type::IQ1_M || ti->type == sq::Type::NVFP4 || ti->type == sq::Type::MXFP4 || ti->type == sq::Type::Q2_K ||
80                ti->type == sq::Type::Q3_K || ti->type == sq::Type::Q6_K) continue;   // scale not at the front: random bytes still give finite-enough sums
81            w[o] = 0x66, w[o + 1] = 0x24;
82        }
83        std::vector<float> x((size_t) n);
84        for (auto& v : x) v = (float) ((int) (rnd() % 2001) - 1000) * 1e-3f;
85        std::vector<uint8_t> act((size_t) sq::row_bytes((int) ti->act, n));
86        const sq::ActKernel* ak = sq::act_kernel(ti->act, want);
87        if (ak == nullptr) continue;
88        ak->fn(x.data(), act.data(), n);
89        std::vector<std::vector<double>> td(run_k.size()), tl(run_k.size());
90        for (int rep = 0; rep < reps; ++rep)
91            for (size_t i = 0; i < run_k.size(); ++i) {
92                td[i].push_back(run(run_k[i], w, rb, n, rows_dram, act.data(), &sink));
93                double best = 1e9;
94                for (int r2 = 0; r2 < 8; ++r2) best = std::min(best, run(run_k[i], w, rb, n, rows_l2, act.data(), &sink));
95                tl[i].push_back(best);
96            }
97        for (size_t i = 0; i < run_k.size(); ++i) {
98            std::sort(td[i].begin(), td[i].end());
99            std::sort(tl[i].begin(), tl[i].end());
100            const double gd = (double) rows_dram * rb / td[i][td[i].size() / 2] / 1e9, gl = (double) rows_l2 * rb / tl[i][tl[i].size() / 2] / 1e9;
101            std::printf("  %-8s %-26s %-6s  DRAM %6.2f GB/s   L2 %6.2f GB/s\n", ti->name, run_k[i]->name, sq::isa_name(run_k[i]->isa), gd, gl);
102        }
103    }
104    if (sink == 12345.f) std::printf("\n");
105    return 0;
106}