okami.mom/strata

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

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

main
6.3 KiB145 linesraw
1// src/kernels/cpu/agg_bandwidth_bench.cpp
2// Step 5.3 (v2, cold-corrected): the TRUE cold aggregate ceiling of the engine's 15 workers.
3//   A cold stream : 15 threads each do ONE pass over a fresh 64 MB slice (960 MB total >> L3) -> raw cold read ceiling
4//   B cold matvec : 15 threads each run the U matvec over FRESH groups (60 groups = 292 MB, read once) -> the engine shape, cold
5// Both pinned to the engine's exact worker set: P-cores 2,4,6,8,10,12,14 + E-cores 16..23.
6#define GGML_COMMON_DECL_CPP
7#define GGML_COMMON_IMPL_CPP
8#include "ggml-common.h"
9
10#include <immintrin.h>
11#include <atomic>
12#include <chrono>
13#include <cstdint>
14#include <cstdio>
15#include <cstring>
16#include <random>
17#include <thread>
18#include <vector>
19#ifdef __linux__
20#include <sched.h>
21#endif
22
23static const int cores[15] = {2, 4, 6, 8, 10, 12, 14, 16, 17, 18, 19, 20, 21, 22, 23};
24static void pin_to(int cpu) {
25#ifdef __linux__
26    cpu_set_t set;
27    CPU_ZERO(&set);
28    CPU_SET(cpu, &set);
29    sched_setaffinity(0, sizeof(set), &set);
30#else
31    (void) cpu;
32#endif
33}
34
35static inline float h2f(uint16_t h) {
36    return _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128((int) h)));
37}
38static inline float hsum_float_8(__m256 x) {
39    __m128 res = _mm256_extractf128_ps(x, 1);
40    res = _mm_add_ps(res, _mm256_castps256_ps128(x));
41    res = _mm_add_ps(res, _mm_movehl_ps(res, res));
42    res = _mm_add_ss(res, _mm_movehdup_ps(res));
43    return _mm_cvtss_f32(res);
44}
45template <int NT>
46static void dot_maddubs(const block_q8_0* x, int nb, const void* const* act, float* res) {
47    __m256 acc[NT];
48    for (int t = 0; t < NT; ++t) acc[t] = _mm256_setzero_ps();
49    for (int ib = 0; ib < nb; ++ib) {
50        const float xd = h2f(x[ib].d);
51        const __m256i qx = _mm256_loadu_si256((const __m256i*) x[ib].qs);
52        const __m256i ax = _mm256_sign_epi8(qx, qx);
53        for (int t = 0; t < NT; ++t) {
54            const uint8_t* yb = (const uint8_t*) act[t] + (size_t) ib * 36;
55            const float dy = h2f(*(const uint16_t*) (yb + 0));
56            const __m256 d = _mm256_set1_ps(xd * dy);
57            const __m256i qy = _mm256_loadu_si256((const __m256i*) (yb + 4));
58            const __m256i sy = _mm256_sign_epi8(qy, qx);
59            const __m256 q = _mm256_cvtepi32_ps(_mm256_madd_epi16(_mm256_set1_epi16(1), _mm256_maddubs_epi16(ax, sy)));
60            acc[t] = _mm256_fmadd_ps(q, d, acc[t]);
61        }
62    }
63    for (int t = 0; t < NT; ++t) res[t] = hsum_float_8(acc[t]);
64}
65static void rows_maddubs(const block_q8_0* w, int R, int nb, const void* const* act, int nt, float* out) {
66    for (int r = 0; r < R; ++r) {
67        float s[4];
68        dot_maddubs<1>(w + (size_t) r * nb, nb, act, s);
69        out[r] = s[0];
70    }
71}
72
73int main() {
74    constexpr int R = 2560, N = 1792, NB = N / 32, T = 15;
75    const size_t group_bytes = (size_t) R * NB * 34;   // 4.87 MB per group
76    std::mt19937 rng(7);
77    std::uniform_int_distribution<int> qd(-127, 127);
78    const uint16_t scale_half = 0x2000u;
79
80    // ---- A: cold raw stream, 15 threads x 64 MB (960 MB, read once) ----
81    {
82        const size_t per = (size_t) 64 * 1024 * 1024;
83        std::vector<uint8_t> buf((size_t) T * per);
84        for (size_t i = 0; i < buf.size(); ++i) buf[i] = (uint8_t)(i & 0xff);
85        std::vector<uint64_t> sums(T, 0);
86        std::atomic<int> go{0};
87        std::vector<std::thread> th;
88        for (int t = 0; t < T; ++t)
89            th.emplace_back([&, t]() {
90                pin_to(cores[t]);
91                const uint8_t* p = buf.data() + (size_t) t * per;
92                uint64_t s = 0;
93                while (go.load() == 0) {}
94                const auto t0 = std::chrono::steady_clock::now();
95                for (size_t j = 0; j < per / 8; ++j) s += ((const uint64_t*) p)[j];
96                const auto t1 = std::chrono::steady_clock::now();
97                sums[t] = s;
98                (void) t1;
99            });
100        const auto w0 = std::chrono::steady_clock::now();
101        go.store(1);
102        for (auto& x : th) x.join();
103        const auto w1 = std::chrono::steady_clock::now();
104        const double wall = std::chrono::duration<double, std::milli>(w1 - w0).count();
105        volatile uint64_t sink = 0; for (auto s : sums) sink += s; (void) sink;
106        std::printf("A cold raw-stream  15x64MB read-once: wall %.3f ms = %.1f GB/s aggregate\n", wall, (double) T * per / wall / 1e6);
107    }
108
109    // ---- B: cold matvec, 15 threads x 4 fresh groups each (60 groups = 292 MB, read once) ----
110    {
111        const int K = 60;   // 4 groups per thread, disjoint
112        std::vector<block_q8_0> w((size_t) K * R * NB);
113        for (auto& b : w) { b.d = scale_half; for (int j = 0; j < 32; ++j) b.qs[j] = (int8_t) qd(rng); }
114        std::vector<uint8_t> actb((size_t) NB * 36);
115        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); }
116        const void* actp = actb.data();
117        std::vector<float> out((size_t) T * R, 0.f);
118        std::atomic<int> go{0};
119        std::vector<std::thread> th;
120        for (int t = 0; t < T; ++t)
121            th.emplace_back([&, t]() {
122                pin_to(cores[t]);
123                float* o = out.data() + (size_t) t * R;
124                while (go.load() == 0) {}
125                const auto t0 = std::chrono::steady_clock::now();
126                for (int g = 0; g < 4; ++g) {   // thread t reads groups t, t+15, t+30, t+45 (disjoint, cold)
127                    const block_q8_0* gw = w.data() + (size_t) (t + g * T) * R * NB;
128                    rows_maddubs(gw, R, NB, &actp, 1, o);
129                }
130                const auto t1 = std::chrono::steady_clock::now();
131                (void) t1;
132            });
133        const auto w0 = std::chrono::steady_clock::now();
134        go.store(1);
135        for (auto& x : th) x.join();
136        const auto w1 = std::chrono::steady_clock::now();
137        const double wall = std::chrono::duration<double, std::milli>(w1 - w0).count();
138        const double bytes = (double) K * group_bytes;
139        const double flops = (double) K * R * N * 2;
140        volatile float sink = 0; for (int t = 0; t < T; ++t) sink += out[(size_t) t * R]; (void) sink;
141        std::printf("B cold matvec      15 workers x 4 groups read-once: wall %.3f ms = %.1f GB/s aggregate = %.1f GFLOP/s\n",
142                    wall, bytes / wall / 1e6, flops / wall / 1e6);
143    }
144    return 0;
145}