okami.mom/strata

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

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

main
5.1 KiB114 linesraw
1// src/kernels/cpu/lat_bandwidth_bench.cpp
2// Step 5.2: is the U matvec memory-bound?  Measures the maddubs kernel's throughput in two regimes:
3//   hot  = one group (H x ELL Q8_0 = ~4.87 MB), re-read every iteration (fits L3)  -> compute-bound ceiling
4//   cold = K groups (> 4x L3), each read once and cycled (streamed from RAM)        -> the arena's streaming rate
5// Reports GFLOP/s and the weight stream GB/s for both, so (a) bandwidth vs (b) dilution is decided.
6#define GGML_COMMON_DECL_CPP
7#define GGML_COMMON_IMPL_CPP
8#include "ggml-common.h"
9
10#include <immintrin.h>
11#include <chrono>
12#include <cstdint>
13#include <cstdio>
14#include <cstring>
15#include <random>
16#include <vector>
17
18static inline float h2f(uint16_t h) {
19    return _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128((int) h)));
20}
21static inline float hsum_float_8(__m256 x) {
22    __m128 res = _mm256_extractf128_ps(x, 1);
23    res = _mm_add_ps(res, _mm256_castps256_ps128(x));
24    res = _mm_add_ps(res, _mm_movehl_ps(res, res));
25    res = _mm_add_ss(res, _mm_movehdup_ps(res));
26    return _mm_cvtss_f32(res);
27}
28template <int NT>
29static void dot_maddubs(const block_q8_0* x, int nb, const void* const* act, float* res) {
30    __m256 acc[NT];
31    for (int t = 0; t < NT; ++t) acc[t] = _mm256_setzero_ps();
32    for (int ib = 0; ib < nb; ++ib) {
33        const float xd = h2f(x[ib].d);
34        const __m256i qx = _mm256_loadu_si256((const __m256i*) x[ib].qs);
35        const __m256i ax = _mm256_sign_epi8(qx, qx);
36        for (int t = 0; t < NT; ++t) {
37            const uint8_t* yb = (const uint8_t*) act[t] + (size_t) ib * 36;
38            const float dy = h2f(*(const uint16_t*) (yb + 0));
39            const __m256 d = _mm256_set1_ps(xd * dy);
40            const __m256i qy = _mm256_loadu_si256((const __m256i*) (yb + 4));
41            const __m256i sy = _mm256_sign_epi8(qy, qx);
42            const __m256 q = _mm256_cvtepi32_ps(_mm256_madd_epi16(_mm256_set1_epi16(1), _mm256_maddubs_epi16(ax, sy)));
43            acc[t] = _mm256_fmadd_ps(q, d, acc[t]);
44        }
45    }
46    for (int t = 0; t < NT; ++t) res[t] = hsum_float_8(acc[t]);
47}
48static void rows_maddubs(const block_q8_0* w, int R, int nb, const void* const* act, int nt, float* out) {
49    for (int r = 0; r < R; ++r) {
50        float s[4];
51        switch (nt) {
52            case 1: dot_maddubs<1>(w + (size_t) r * nb, nb, act, s); out[r] = s[0]; break;
53            case 2: dot_maddubs<2>(w + (size_t) r * nb, nb, act, s); out[r] = s[0]; out[R + r] = s[1]; break;
54            default: dot_maddubs<4>(w + (size_t) r * nb, nb, act, s); for (int t = 0; t < 4; ++t) out[(size_t) t * R + r] = s[t]; break;
55        }
56    }
57}
58
59int main() {
60    constexpr int R = 2560, N = 1792, NB = N / 32;
61    const size_t group_bytes = (size_t) R * NB * 34;   // one group's Q8_0 weights
62    std::mt19937 rng(7);
63    std::uniform_int_distribution<int> qd(-127, 127);
64    const uint16_t scale_half = 0x2000u;
65
66    const int K = 32;   // cold: 32 groups = 32 * 4.87 MB ~ 156 MB > 4x L3 (36 MB)
67    std::vector<block_q8_0> w((size_t) K * R * NB);
68    for (auto& b : w) { b.d = scale_half; for (int j = 0; j < 32; ++j) b.qs[j] = (int8_t) qd(rng); }
69    std::vector<uint8_t> actb((size_t) 4 * NB * 36);
70    for (size_t p = 0; p < actb.size(); p += 36) { std::memcpy(actb.data() + p, &scale_half, 2); for (int j = 0; j < 32; ++j) actb[p + 4 + j] = (uint8_t)(int8_t) qd(rng); }
71    const void* actp[4];
72    for (int t = 0; t < 4; ++t) actp[t] = actb.data() + (size_t) t * NB * 36;
73
74    std::vector<float> out((size_t) 4 * R);
75    std::fill(out.begin(), out.end(), 0.f);
76    volatile float sink = 0.f;
77
78    for (int nt : {1, 4}) {
79        const double flops = 2.0 * (double) R * (double) N * (double) nt;
80        const int it = 40;
81
82        // hot: group 0 re-read every iteration
83        {
84            double best = 1e30;
85            for (int i = 0; i < it; ++i) {
86                const auto t0 = std::chrono::steady_clock::now();
87                rows_maddubs(w.data(), R, NB, actp, nt, out.data());
88                const auto t1 = std::chrono::steady_clock::now();
89                best = std::min(best, std::chrono::duration<double, std::milli>(t1 - t0).count());
90            }
91            std::printf("nt=%d HOT   (1 group, L3-resident): %.3f ms = %.1f GFLOP/s  (weight stream %.1f GB/s)\n",
92                        nt, best, flops / best / 1e6, (double) group_bytes / best / 1e6);
93            sink += out[0];
94        }
95
96        // cold: cycle K groups, each read once per pass
97        {
98            double best = 1e30;
99            for (int i = 0; i < it; ++i) {
100                const int g = i % K;
101                const block_q8_0* gw = w.data() + (size_t) g * R * NB;
102                const auto t0 = std::chrono::steady_clock::now();
103                rows_maddubs(gw, R, NB, actp, nt, out.data());
104                const auto t1 = std::chrono::steady_clock::now();
105                best = std::min(best, std::chrono::duration<double, std::milli>(t1 - t0).count());
106            }
107            std::printf("nt=%d COLD  (%d groups cycled, streamed): %.3f ms = %.1f GFLOP/s  (weight stream %.1f GB/s)\n",
108                        nt, K, best, flops / best / 1e6, (double) group_bytes / best / 1e6);
109            sink += out[0];
110        }
111    }
112    (void) sink;
113    return 0;
114}