okami.mom/strata

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

waveringanaedge0-strata: add the engine for Edge0 145B and Edge0 120Bfd19246dc

main
8.5 KiB185 linesraw
1// src/kernels/cpu/lat_vnni_bench.cpp
2// Step 5.1 microbenchmark: the latent U matvec (Q8_0 weights x Q8_1 acts, H=2560 x ELL=1792) on two paths -
3// the current AVX2 maddubs dot (kq_avx2.cpp lat_q8_0_q8_1_dot) and an AVX-VNNI dpbusd dot (target("avxvnni")),
4// against a double reference.  Reports throughput (GFLOP/s) and the output delta (max/mean/p99).
5// No GPU, no model: random Q8_0/Q8_1 blocks of the model's U shape.
6#define GGML_COMMON_DECL_CPP
7#define GGML_COMMON_IMPL_CPP
8#include "ggml-common.h"
9
10#include <immintrin.h>
11#include <algorithm>
12#include <chrono>
13#include <cmath>
14#include <cstdint>
15#include <cstdio>
16#include <cstring>
17#include <random>
18#include <vector>
19
20static inline float h2f(uint16_t h) {
21    return _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128((int) h)));
22}
23static inline float hsum_float_8(__m256 x) {
24    __m128 res = _mm256_extractf128_ps(x, 1);
25    res = _mm_add_ps(res, _mm256_castps256_ps128(x));
26    res = _mm_add_ps(res, _mm_movehl_ps(res, res));
27    res = _mm_add_ss(res, _mm_movehdup_ps(res));
28    return _mm_cvtss_f32(res);
29}
30
31// ---- the current maddubs inner dot (kq_avx2.cpp lat_q8_0_q8_1_dot), verbatim ----
32template <int NT>
33static void dot_maddubs(const block_q8_0* x, int nb, const void* const* act, float* res) {
34    __m256 acc[NT];
35    for (int t = 0; t < NT; ++t) acc[t] = _mm256_setzero_ps();
36    for (int ib = 0; ib < nb; ++ib) {
37        const float xd = h2f(x[ib].d);
38        const __m256i qx = _mm256_loadu_si256((const __m256i*) x[ib].qs);
39        const __m256i ax = _mm256_sign_epi8(qx, qx);   // |weight|
40        for (int t = 0; t < NT; ++t) {
41            const uint8_t* yb = (const uint8_t*) act[t] + (size_t) ib * 36;
42            const float dy = h2f(*(const uint16_t*) (yb + 0));
43            const __m256 d = _mm256_set1_ps(xd * dy);
44            const __m256i qy = _mm256_loadu_si256((const __m256i*) (yb + 4));
45            const __m256i sy = _mm256_sign_epi8(qy, qx);
46            const __m256 q = _mm256_cvtepi32_ps(_mm256_madd_epi16(_mm256_set1_epi16(1), _mm256_maddubs_epi16(ax, sy)));
47            acc[t] = _mm256_fmadd_ps(q, d, acc[t]);
48        }
49    }
50    for (int t = 0; t < NT; ++t) res[t] = hsum_float_8(acc[t]);
51}
52
53// ---- the AVX-VNNI dpbusd inner dot (per-block zero-src + cvt + fmadd, like expert.cpp row_dot_multi) ----
54template <int NT>
55__attribute__((target("avxvnni")))
56static void dot_vnni(const block_q8_0* x, int nb, const void* const* act, float* res) {
57    __m256 acc[NT];
58    for (int t = 0; t < NT; ++t) acc[t] = _mm256_setzero_ps();
59    for (int ib = 0; ib < nb; ++ib) {
60        const float xd = h2f(x[ib].d);
61        const __m256i qx = _mm256_loadu_si256((const __m256i*) x[ib].qs);
62        const __m256i ax = _mm256_sign_epi8(qx, qx);   // |weight| (unsigned operand of dpbusd)
63        for (int t = 0; t < NT; ++t) {
64            const uint8_t* yb = (const uint8_t*) act[t] + (size_t) ib * 36;
65            const float dy = h2f(*(const uint16_t*) (yb + 0));
66            const __m256 d = _mm256_set1_ps(xd * dy);
67            const __m256i qy = _mm256_loadu_si256((const __m256i*) (yb + 4));
68            const __m256i sy = _mm256_sign_epi8(qy, qx);
69            const __m256 q = _mm256_cvtepi32_ps(_mm256_dpbusd_avx_epi32(_mm256_setzero_si256(), ax, sy));
70            acc[t] = _mm256_fmadd_ps(q, d, acc[t]);
71        }
72    }
73    for (int t = 0; t < NT; ++t) res[t] = hsum_float_8(acc[t]);
74}
75
76// one full U matvec: R rows x (nb*32) cols, nt tokens; out[t*R + r].
77static void rows_maddubs(const block_q8_0* w, int R, int nb, const void* const* act, int nt, float* out) {
78    for (int r = 0; r < R; ++r) {
79        const block_q8_0* wr = w + (size_t) r * nb;
80        float s[4];
81        switch (nt) {
82            case 1: dot_maddubs<1>(wr, nb, act, s); out[r] = s[0]; break;
83            case 2: dot_maddubs<2>(wr, nb, act, s); out[r] = s[0]; out[R + r] = s[1]; break;
84            case 3: dot_maddubs<3>(wr, nb, act, s); for (int t = 0; t < 3; ++t) out[(size_t) t * R + r] = s[t]; break;
85            default: dot_maddubs<4>(wr, nb, act, s); for (int t = 0; t < 4; ++t) out[(size_t) t * R + r] = s[t]; break;
86        }
87    }
88}
89__attribute__((target("avxvnni")))
90static void rows_vnni(const block_q8_0* w, int R, int nb, const void* const* act, int nt, float* out) {
91    for (int r = 0; r < R; ++r) {
92        const block_q8_0* wr = w + (size_t) r * nb;
93        float s[4];
94        switch (nt) {
95            case 1: dot_vnni<1>(wr, nb, act, s); out[r] = s[0]; break;
96            case 2: dot_vnni<2>(wr, nb, act, s); out[r] = s[0]; out[R + r] = s[1]; break;
97            case 3: dot_vnni<3>(wr, nb, act, s); for (int t = 0; t < 3; ++t) out[(size_t) t * R + r] = s[t]; break;
98            default: dot_vnni<4>(wr, nb, act, s); for (int t = 0; t < 4; ++t) out[(size_t) t * R + r] = s[t]; break;
99        }
100    }
101}
102
103int main() {
104    constexpr int R = 2560, N = 1792, NB = N / 32;   // the U matvec shape H x ELL
105    std::mt19937 rng(7);
106    std::uniform_int_distribution<int> qd(-127, 127);
107    const uint16_t scale_half = 0x2000u;   // 0.0078125, a valid f16 block scale
108
109    std::vector<block_q8_0> w((size_t) R * NB);
110    for (auto& b : w) {
111        b.d = scale_half;
112        for (int j = 0; j < 32; ++j) b.qs[j] = (int8_t) qd(rng);
113    }
114    // acts as raw bytes: block_q8_1 = 36 B/block (d +0, s +2 [unused], qs +4)
115    std::vector<uint8_t> actb((size_t) 4 * NB * 36);
116    for (int t = 0; t < 4; ++t)
117        for (int ib = 0; ib < NB; ++ib) {
118            uint8_t* p = actb.data() + ((size_t) t * NB + ib) * 36;
119            std::memcpy(p + 0, &scale_half, 2);
120            *(uint16_t*)(p + 2) = 0;
121            for (int j = 0; j < 32; ++j) p[4 + j] = (uint8_t)(int8_t) qd(rng);
122        }
123
124    for (int nt : {1, 2, 4}) {
125        const void* actp[4];
126        for (int t = 0; t < nt; ++t) actp[t] = actb.data() + (size_t) t * NB * 36;
127
128        // double reference
129        std::vector<double> ref((size_t) nt * R, 0.0);
130        for (int t = 0; t < nt; ++t)
131            for (int r = 0; r < R; ++r) {
132                double s = 0;
133                const block_q8_0* wr = &w[(size_t) r * NB];
134                const uint8_t* ar = actb.data() + (size_t) t * NB * 36;
135                for (int ib = 0; ib < NB; ++ib) {
136                    int dot = 0;
137                    const int8_t* wq = wr[ib].qs;
138                    const int8_t* aq = (const int8_t*)(ar + (size_t) ib * 36 + 4);
139                    for (int j = 0; j < 32; ++j) dot += (int) wq[j] * (int) aq[j];
140                    s += (double) h2f(wr[ib].d) * (double) h2f(*(const uint16_t*)(ar + (size_t) ib * 36 + 0)) * (double) dot;
141                }
142                ref[(size_t) t * R + r] = s;
143            }
144
145        std::vector<float> om((size_t) nt * R), ov((size_t) nt * R);
146        std::fill(om.begin(), om.end(), 0.f);
147        std::fill(ov.begin(), ov.end(), 0.f);
148
149        rows_maddubs(w.data(), R, NB, actp, nt, om.data());
150        rows_vnni(w.data(), R, NB, actp, nt, ov.data());
151
152        auto rel = [&](const std::vector<float>& o) {
153            double num = 0, den = 0;
154            for (size_t i = 0; i < o.size(); ++i) { num += std::fabs(o[i] - ref[i]); den += std::fabs(ref[i]); }
155            return num / (den + 1e-30);
156        };
157        std::printf("nt=%d  rel-vs-ref  maddubs %.2e  vnni %.2e\n", nt, rel(om), rel(ov));
158
159        std::vector<double> dd((size_t) nt * R);
160        for (size_t i = 0; i < dd.size(); ++i) dd[i] = std::fabs((double) om[i] - (double) ov[i]);
161        std::sort(dd.begin(), dd.end());
162        double dmax = dd.back(), dsum = 0;
163        for (double v : dd) dsum += v;
164        std::printf("  delta maddubs-vs-vnni: max %.3e  mean %.3e  p99 %.3e\n",
165                    dmax, dsum / (double) dd.size(), dd[(size_t) (0.99 * (dd.size() - 1))]);
166
167        const int it = 40;
168        auto bench = [&](void (*fn)(const block_q8_0*, int, int, const void* const*, int, float*), float* o) {
169            double best = 1e30;
170            for (int i = 0; i < it; ++i) {
171                const auto t0 = std::chrono::steady_clock::now();
172                fn(w.data(), R, NB, actp, nt, o);
173                const auto t1 = std::chrono::steady_clock::now();
174                best = std::min(best, std::chrono::duration<double, std::milli>(t1 - t0).count());
175            }
176            return best;
177        };
178        const double tm = bench(rows_maddubs, om.data());
179        const double tv = bench(rows_vnni, ov.data());
180        const double flops = 2.0 * (double) R * (double) N * (double) nt;
181        std::printf("  throughput (best of %d): maddubs %.3f ms = %.1f GFLOP/s | vnni %.3f ms = %.1f GFLOP/s | speedup %.3fx\n",
182                    it, tm, flops / tm / 1e6, tv, flops / tv / 1e6, tm / tv);
183    }
184    return 0;
185}