okami.mom/strata

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

Niko1221Merge f-own-2n (ead5a32ae) into f-own-int: the Arm NEON tier of the quant layer0a9381336

main
7.5 KiB166 linesraw
1// tests/quant/quant_bench_ggml.cpp - the layer's dot kernels and activation quantizers against ggml-cpu's, speed (links ggml-cpu: test only).
2//
3// For every dot type: R rows of n elements (64 rows of 8192: an L2-resident chunk; --rows 4096 streams from DRAM), one activation row; ours and ggml's alternate in interleaved rounds (ABAB...), each round
4// runs every row `reps` times; the report is the median of the per-round ratio ggml_time / our_time (> 1: ours is faster) and the number of rounds ours won.
5// Bit-exactness is the oracle's job (quant_parity); this only answers "is the row faster or slower than ggml's".
6//
7// usage: quant_bench_ggml [--rounds N] [--n ELEMENTS] [--rows R] [--type NAME]
8#include <algorithm>
9#include <chrono>
10#include <cstdio>
11#include <cstdlib>
12#include <cstring>
13#include <string>
14#include <vector>
15
16#include "ggml-cpu.h"
17#include "ggml.h"
18#include "strata/quant/act.hpp"
19#include "strata/quant/dot.hpp"
20#include "strata/quant/isa.hpp"
21#include "strata/quant/types.hpp"
22
23namespace sq = strata::quant;
24
25namespace {
26
27double now() { return std::chrono::duration<double>(std::chrono::steady_clock::now().time_since_epoch()).count(); }
28
29struct Rng {
30    uint64_t s = 88172645463325252ull;
31    float gauss() {
32        float a = 0;
33        for (int i = 0; i < 4; ++i) {
34            s ^= s >> 12; s ^= s << 25; s ^= s >> 27;
35            a += (float) ((s * 0x2545F4914F6CDD1Dull) >> 40) / 16777216.0f - 0.5f;
36        }
37        return a;
38    }
39};
40
41sq::Arith oracle_arith() {
42#if defined(__aarch64__)
43    return sq::Arith::Neon;
44#else
45    if (ggml_cpu_has_avx2() && ggml_cpu_has_fma()) return sq::Arith::Avx2;
46    if (ggml_cpu_has_avx()) return sq::Arith::Avx;
47    return sq::Arith::Generic;
48#endif
49}
50
51double median(std::vector<double> v) {
52    std::sort(v.begin(), v.end());
53    return v[v.size() / 2];
54}
55
56}  // namespace
57
58int main(int argc, char** argv) {
59    int rounds = 11, n = 8192, R = 64;
60    const char* only = nullptr;
61    for (int i = 1; i < argc; ++i) {
62        if (!std::strcmp(argv[i], "--rounds") && i + 1 < argc) rounds = std::atoi(argv[++i]);
63        else if (!std::strcmp(argv[i], "--n") && i + 1 < argc) n = std::atoi(argv[++i]);
64        else if (!std::strcmp(argv[i], "--rows") && i + 1 < argc) R = std::atoi(argv[++i]);   // 4096+ rows stream from DRAM like the pool's experts
65        else if (!std::strcmp(argv[i], "--type") && i + 1 < argc) only = argv[++i];
66    }
67    ggml_cpu_init();
68    const sq::Arith ar = oracle_arith();
69    std::printf("quant_bench_ggml: contract %s, n=%d, %d rounds (ratio = ggml time / ours; > 1 means ours is faster)\n", sq::arith_name(ar), n, rounds);
70    static const sq::Type kTypes[] = {sq::Type::Q2_0, sq::Type::Q4_0, sq::Type::Q4_1, sq::Type::Q5_0, sq::Type::Q5_1, sq::Type::Q8_0, sq::Type::IQ4_NL,
71                                      sq::Type::MXFP4, sq::Type::NVFP4, sq::Type::Q2_K, sq::Type::Q3_K, sq::Type::Q4_K, sq::Type::Q5_K, sq::Type::Q6_K,
72                                      sq::Type::IQ1_S, sq::Type::IQ1_M, sq::Type::IQ2_XXS, sq::Type::IQ2_XS, sq::Type::IQ2_S, sq::Type::IQ3_XXS,
73                                      sq::Type::IQ3_S, sq::Type::IQ4_XS};
74    Rng rng;
75    for (sq::Type st : kTypes) {
76        const sq::TypeInfo* ti = sq::type_info(st);
77        if (only != nullptr && std::strcmp(only, ti->name) != 0) continue;
78        const ggml_type gt = (ggml_type) (int) st;
79        const ggml_type_traits_cpu* tr = ggml_get_type_traits_cpu(gt);
80        const sq::DotKernel* k = sq::dot_kernel(st, ar);
81        if (tr == nullptr || tr->vec_dot == nullptr || k == nullptr) continue;
82        const ggml_type_traits_cpu* ta = ggml_get_type_traits_cpu(tr->vec_dot_type);
83        const int unit = std::max<int>(ti->blck, (int) ggml_blck_size(tr->vec_dot_type));
84        const int nn = n / unit * unit;
85        std::vector<float> f((size_t) nn * R);
86        for (float& v : f) v = rng.gauss();
87        std::vector<uint8_t> w(ggml_row_size(gt, nn) * R), act(ggml_row_size(tr->vec_dot_type, nn));
88        ggml_quantize_init(gt);
89        std::vector<float> im((size_t) nn, 1.f);
90        ggml_quantize_chunk(gt, f.data(), w.data(), 0, R, nn, ggml_quantize_requires_imatrix(gt) ? im.data() : nullptr);
91        ta->from_float(f.data(), act.data(), nn);
92        const size_t rb = ggml_row_size(gt, nn);
93        // enough repetitions for ~20 ms per arm
94        int reps = 1;
95        float sink = 0.f;
96        for (;; reps *= 2) {
97            const double t0 = now();
98            for (int r = 0; r < reps; ++r)
99                for (int row = 0; row < R; ++row) {
100                    float s;
101                    tr->vec_dot(nn, &s, 0, w.data() + rb * row, 0, act.data(), 0, 1);
102                    sink += s;
103                }
104            if (now() - t0 > 0.02 || reps >= 4096) break;
105        }
106        std::vector<double> ratios;
107        double tg_all = 0, to_all = 0;
108        int won = 0;
109        for (int rd = 0; rd < rounds; ++rd) {
110            double tg, to;
111            auto run_ggml = [&] {
112                const double t0 = now();
113                for (int r = 0; r < reps; ++r)
114                    for (int row = 0; row < R; ++row) {
115                        float s;
116                        tr->vec_dot(nn, &s, 0, w.data() + rb * row, 0, act.data(), 0, 1);
117                        sink += s;
118                    }
119                return now() - t0;
120            };
121            auto run_ours = [&] {
122                const double t0 = now();
123                for (int r = 0; r < reps; ++r)
124                    for (int row = 0; row < R; ++row) {
125                        float s;
126                        k->fn(nn, &s, w.data() + rb * row, act.data());
127                        sink += s;
128                    }
129                return now() - t0;
130            };
131            if (rd & 1) { to = run_ours(); tg = run_ggml(); } else { tg = run_ggml(); to = run_ours(); }
132            ratios.push_back(tg / to);
133            tg_all += tg;
134            to_all += to;
135            if (to < tg) ++won;
136        }
137        const double bytes = (double) rb * R * reps * rounds;
138        std::printf("  %-8s %-18s median ratio %.3f  ours faster in %2d/%d rounds   ours %.1f GB/s  ggml %.1f GB/s%s\n", ti->name, k->name, median(ratios), won, rounds,
139                    bytes / to_all / 1e9, bytes / tg_all / 1e9, sink == 12345.f ? " " : "");
140    }
141    // activation quantizers
142    for (sq::Type at : {sq::Type::Q8_0, sq::Type::Q8_1, sq::Type::Q8_K}) {
143        const sq::TypeInfo* ti = sq::type_info(at);
144        const ggml_type_traits_cpu* ta = ggml_get_type_traits_cpu((ggml_type) (int) at);
145        const sq::ActKernel* ak = sq::act_kernel(at, ar);
146        if (ta == nullptr || ta->from_float == nullptr || ak == nullptr) continue;
147        if (only != nullptr && std::strcmp(only, ti->name) != 0) continue;
148        const int nn = 4096;
149        std::vector<float> f((size_t) nn);
150        for (float& v : f) v = rng.gauss();
151        std::vector<uint8_t> o((size_t) nn / ti->blck * ti->bytes + 64);
152        const int reps = 2000;
153        std::vector<double> ratios;
154        int won = 0;
155        for (int rd = 0; rd < rounds; ++rd) {
156            double tg, to;
157            auto g = [&] { const double t0 = now(); for (int r = 0; r < reps; ++r) ta->from_float(f.data(), o.data(), nn); return now() - t0; };
158            auto u = [&] { const double t0 = now(); for (int r = 0; r < reps; ++r) ak->fn(f.data(), o.data(), nn); return now() - t0; };
159            if (rd & 1) { to = u(); tg = g(); } else { tg = g(); to = u(); }
160            ratios.push_back(tg / to);
161            if (to < tg) ++won;
162        }
163        std::printf("  act %-5s %-18s median ratio %.3f  ours faster in %2d/%d rounds\n", ti->name, ak->name, median(ratios), won, rounds);
164    }
165    return 0;
166}