okami.mom/strata

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

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

main
13.4 KiB218 linesraw
1// Minimal: is the fused A stage's 11x gap between the unit test and the engine a cache effect?
2// Same kernel, same shape (64 groups x 83 entries), two buffer sizes: small (xq/out/hbuf fit in L2) and big
3// (the engine's 42,510 entries: xq 61 MB, out 218 MB, hbuf 34 MB).  Only the buffer size changes.
4#include "strata/prefill/lat_q3k_fused.hpp"
5
6#include <cuda_runtime.h>
7#include <ggml.h>
8
9#include <cmath>
10#include <cstdio>
11#include <cstdlib>
12#include <cstring>
13#include <random>
14#include <vector>
15
16#define CK(e) do { const cudaError_t r_ = (e); if (r_ != cudaSuccess) { \
17    std::printf("FAIL %s: %s\n", #e, cudaGetErrorString(r_)); std::exit(1); } } while (0)
18
19int main() {
20    constexpr int64_t ELL = 1280, FF = 640;
21    constexpr int64_t GUB = 1280 * 550, DNB = 1280 * 360;      // one expert's blob
22    constexpr int64_t BLOB = GUB + DNB;
23    constexpr int kHq = (int) strata::prefill::lat_q3k_fused_hbuf_row;   // 800
24    const int64_t gub = GUB, dnb = DNB, blob = BLOB;
25
26    // 64 distinct real experts (74 MB, past L2), each with its own quantized weights
27    std::mt19937 rng(11);
28    std::normal_distribution<float> nw(0.f, 0.05f);
29    std::vector<uint8_t> one((size_t) blob);
30    {
31        std::vector<float> w((size_t) 1280 * ELL);
32        for (auto& v : w) v = nw(rng);
33        if (ggml_quantize_chunk(GGML_TYPE_Q3_K, w.data(), one.data(), 0, 1280, ELL, nullptr) != gub) { std::printf("FAIL q3k\n"); return 1; }
34        std::vector<float> wd((size_t) ELL * FF);
35        for (auto& v : wd) v = nw(rng);
36        if (ggml_quantize_chunk(GGML_TYPE_IQ4_NL, wd.data(), one.data() + gub, 0, ELL, FF, nullptr) != dnb) { std::printf("FAIL iq4\n"); return 1; }
37    }
38    std::vector<uint8_t> blobs((size_t) 64 * blob);
39    for (int g = 0; g < 64; ++g) std::memcpy(blobs.data() + (size_t) g * blob, one.data(), blob);
40    uint8_t* dblobs = nullptr; CK(cudaMalloc(&dblobs, blobs.size()));
41    CK(cudaMemcpy(dblobs, blobs.data(), blobs.size(), cudaMemcpyHostToDevice));
42    std::vector<unsigned long long> gp((size_t) 64);
43    for (int g = 0; g < 64; ++g) gp[(size_t) g] = (unsigned long long) (dblobs + (size_t) g * blob);
44    unsigned long long* dgp = nullptr; CK(cudaMalloc(&dgp, gp.size() * 8));
45    CK(cudaMemcpy(dgp, gp.data(), gp.size() * 8, cudaMemcpyHostToDevice));
46
47    // one valid q8_1 activation row (40 blocks x 36 B), replicated
48    std::vector<float> x((size_t) ELL);
49    std::normal_distribution<float> nd(0.f, 1.f);
50    for (auto& v : x) v = nd(rng);
51    struct H8 { uint16_t d, s; int8_t qs[32]; };
52    std::vector<H8> row((size_t) ELL / 32);
53    for (int b = 0; b < ELL / 32; ++b) {
54        float amax = 0.f, sum = 0.f;
55        for (int i = 0; i < 32; ++i) { const float v = x[(size_t) 32 * b + i]; amax = std::fmax(amax, std::fabs(v)); sum += v; }
56        const float d = amax / 127.0f;
57        row[(size_t) b].d = ggml_fp32_to_fp16(d);
58        row[(size_t) b].s = ggml_fp32_to_fp16(sum);
59        for (int i = 0; i < 32; ++i) row[(size_t) b].qs[i] = (int8_t) std::lround(d != 0.f ? x[(size_t) 32 * b + i] / d : 0.f);
60    }
61
62    int dev = 0, sms = 0;
63    cudaGetDevice(&dev); cudaDeviceGetAttribute(&sms, cudaDevAttrMultiProcessorCount, dev);
64
65    auto bench = [&](const char* tag, int nbig, bool spread) {
66        // 64 groups x 83 entries inside a buffer of nbig entries
67        std::vector<int32_t> starts((size_t) 65), cnt((size_t) 64), tok((size_t) nbig), ident((size_t) nbig);
68        for (int g = 0; g < 64; ++g) starts[(size_t) g] = spread ? g * (nbig / 64) : g * 83;
69        starts[64] = spread ? nbig : 64 * 83;
70        for (int g = 0; g < 64; ++g) cnt[(size_t) g] = 83;
71        const int ntok = nbig < 33960 ? nbig : 33960;
72        for (int e = 0; e < nbig; ++e) { tok[(size_t) e] = (int32_t) ((e * 7) % ntok); ident[(size_t) e] = e; }
73        int32_t* ds = nullptr; int32_t* dc = nullptr; int32_t* dt = nullptr; int32_t* di = nullptr;
74        CK(cudaMalloc(&ds, 65 * 4)); CK(cudaMalloc(&dc, 64 * 4)); CK(cudaMalloc(&dt, (size_t) nbig * 4)); CK(cudaMalloc(&di, (size_t) nbig * 4));
75        CK(cudaMemcpy(ds, starts.data(), 65 * 4, cudaMemcpyHostToDevice));
76        CK(cudaMemcpy(dc, cnt.data(), 64 * 4, cudaMemcpyHostToDevice));
77        CK(cudaMemcpy(dt, tok.data(), (size_t) nbig * 4, cudaMemcpyHostToDevice));
78        CK(cudaMemcpy(di, ident.data(), (size_t) nbig * 4, cudaMemcpyHostToDevice));
79        std::vector<H8> xq((size_t) nbig * (ELL / 32));
80        for (int e = 0; e < nbig; ++e) std::memcpy(xq.data() + (size_t) e * (ELL / 32), row.data(), row.size() * sizeof(H8));
81        uint8_t* dxq = nullptr; void* dhbuf = nullptr; float* dout = nullptr;
82        CK(cudaMalloc(&dxq, xq.size() * sizeof(H8)));
83        CK(cudaMalloc(&dhbuf, (size_t) nbig * kHq));
84        CK(cudaMalloc(&dout, (size_t) nbig * ELL * 4));
85        CK(cudaMemcpy(dxq, xq.data(), xq.size() * sizeof(H8), cudaMemcpyHostToDevice));
86        auto run = [&] { strata::prefill::lat_q3k_fused((const uint8_t* const*) dgp, ds, 64, dxq, dhbuf, dout, ELL, nullptr, dc, dt, di); };
87        run(); CK(cudaDeviceSynchronize());
88        cudaEvent_t e0, e1; CK(cudaEventCreate(&e0)); CK(cudaEventCreate(&e1));
89        const int it = 20;
90        CK(cudaEventRecord(e0));
91        for (int i = 0; i < it; ++i) run();
92        CK(cudaEventRecord(e1)); CK(cudaEventSynchronize(e1));
93        float ms = 0; CK(cudaEventElapsedTime(&ms, e0, e1));
94        const double entries = 64.0 * 83.0 * it;
95        std::printf("%-28s nbig %6d  %.3f ms / %d iters  = %.4f us/entry  (xq %.1f MB, out %.1f MB)\n", tag, nbig,
96                    ms / it, it, ms * 1000.0 / entries, (double) nbig * 1440 / 1e6, (double) nbig * ELL * 4 / 1e6);
97        cudaFree(ds); cudaFree(dc); cudaFree(dt); cudaFree(di); cudaFree(dxq); cudaFree(dhbuf); cudaFree(dout);
98    };
99
100    // the engine's own group-length distribution (g-cnt.bin dumped from a 4K run), in batches of 64 groups
101    {
102        std::FILE* f = std::fopen("/home/user/lat-s26/q3k/cnt.bin", "rb");
103        if (f != nullptr) {
104            std::vector<int32_t> real((size_t) 512);
105            const size_t got = std::fread(real.data(), 4, 512, f); std::fclose(f);
106            if (got == 512) {
107                int64_t tot = 0; int32_t mx = 0;
108                for (int32_t c : real) { tot += c; mx = std::max(mx, c); }
109                std::printf("engine cnt: 512 groups, sum %lld, max %d\n", (long long) tot, mx);
110                std::vector<int32_t> starts((size_t) 513);
111                starts[0] = 0;
112                for (int g = 0; g < 512; ++g) starts[(size_t) g + 1] = starts[(size_t) g] + real[(size_t) g];
113                const int nbig = (int) starts[512];
114                // buffers sized for the whole layer
115                std::vector<int32_t> tok((size_t) nbig), ident((size_t) nbig);
116                const int ntok = nbig < 33960 ? nbig : 33960;
117                for (int e = 0; e < nbig; ++e) { tok[(size_t) e] = (int32_t) ((e * 7) % ntok); ident[(size_t) e] = e; }
118                int32_t* ds = nullptr; int32_t* dc = nullptr; int32_t* dt = nullptr; int32_t* di = nullptr;
119                CK(cudaMalloc(&ds, 513 * 4)); CK(cudaMalloc(&dc, 512 * 4));
120                CK(cudaMalloc(&dt, (size_t) nbig * 4)); CK(cudaMalloc(&di, (size_t) nbig * 4));
121                CK(cudaMemcpy(ds, starts.data(), 513 * 4, cudaMemcpyHostToDevice));
122                CK(cudaMemcpy(dc, real.data(), 512 * 4, cudaMemcpyHostToDevice));
123                CK(cudaMemcpy(dt, tok.data(), (size_t) nbig * 4, cudaMemcpyHostToDevice));
124                CK(cudaMemcpy(di, ident.data(), (size_t) nbig * 4, cudaMemcpyHostToDevice));
125                std::vector<H8> xq((size_t) nbig * (ELL / 32));
126                for (int e = 0; e < nbig; ++e) std::memcpy(xq.data() + (size_t) e * (ELL / 32), row.data(), row.size() * sizeof(H8));
127                uint8_t* dxq = nullptr; void* dhbuf = nullptr; float* dout = nullptr;
128                CK(cudaMalloc(&dxq, xq.size() * sizeof(H8))); CK(cudaMalloc(&dhbuf, (size_t) nbig * kHq));
129                CK(cudaMalloc(&dout, (size_t) nbig * ELL * 4));
130                CK(cudaMemcpy(dxq, xq.data(), xq.size() * sizeof(H8), cudaMemcpyHostToDevice));
131                auto run_all = [&] {
132                    for (int q0 = 0; q0 < 512; q0 += 64) {
133                        const int nb = std::min(64, 512 - q0);
134                        strata::prefill::lat_q3k_fused((const uint8_t* const*) dgp, ds + q0, nb, dxq, dhbuf, dout, ELL,
135                                                       nullptr, dc + q0, dt, di);
136                    }
137                };
138                run_all(); CK(cudaDeviceSynchronize());
139                cudaEvent_t e0, e1; CK(cudaEventCreate(&e0)); CK(cudaEventCreate(&e1));
140                const int it = 20;
141                CK(cudaEventRecord(e0));
142                for (int i = 0; i < it; ++i) run_all();
143                CK(cudaEventRecord(e1)); CK(cudaEventSynchronize(e1));
144                float ms = 0; CK(cudaEventElapsedTime(&ms, e0, e1));
145                std::printf("%-28s nbig %6d  %.3f ms / %d iters  = %.4f us/entry  (engine cnt, 8 batches of 64)\n",
146                            "engine cnt distribution", nbig, ms / it, it, ms * 1000.0 / ((double) tot * it));
147                cudaFree(ds); cudaFree(dc); cudaFree(dt); cudaFree(di); cudaFree(dxq); cudaFree(dhbuf); cudaFree(dout);
148            }
149        }
150    }
151    // the same engine distribution, flattened into pseudo-groups of <= 64 rows (host-side tile split, no kernel change)
152    {
153        std::FILE* f = std::fopen("/home/user/lat-s26/q3k/cnt.bin", "rb");
154        if (f != nullptr) {
155            std::vector<int32_t> real((size_t) 512);
156            if (std::fread(real.data(), 4, 512, f) == 512) {
157                std::fclose(f);
158                std::vector<int32_t> ps, pc; std::vector<int> pg;
159                int64_t tot = 0; int32_t base = 0;
160                for (int g = 0; g < 512; ++g) {
161                    for (int32_t off = 0; off < real[(size_t) g]; off += 64) {
162                        ps.push_back(base + off);
163                        pc.push_back(std::min<int32_t>(64, real[(size_t) g] - off));
164                        pg.push_back(g);
165                        tot += pc.back();
166                    }
167                    base += real[(size_t) g];
168                }
169                const int np = (int) ps.size(), nbig = (int) base;
170                std::vector<unsigned long long> pgp((size_t) np);
171                for (int i = 0; i < np; ++i) pgp[(size_t) i] = gp[(size_t) (pg[(size_t) i] % 64)];
172                unsigned long long* dpgp = nullptr;
173                int32_t* ds = nullptr; int32_t* dc = nullptr; int32_t* dt = nullptr; int32_t* di = nullptr;
174                std::vector<int32_t> tok((size_t) nbig), ident((size_t) nbig);
175                const int ntok = nbig < 33960 ? nbig : 33960;
176                for (int e = 0; e < nbig; ++e) { tok[(size_t) e] = (int32_t) ((e * 7) % ntok); ident[(size_t) e] = e; }
177                CK(cudaMalloc(&dpgp, (size_t) np * 8)); CK(cudaMalloc(&ds, (size_t) np * 4)); CK(cudaMalloc(&dc, (size_t) np * 4));
178                CK(cudaMalloc(&dt, (size_t) nbig * 4)); CK(cudaMalloc(&di, (size_t) nbig * 4));
179                CK(cudaMemcpy(dpgp, pgp.data(), (size_t) np * 8, cudaMemcpyHostToDevice));
180                CK(cudaMemcpy(ds, ps.data(), (size_t) np * 4, cudaMemcpyHostToDevice));
181                CK(cudaMemcpy(dc, pc.data(), (size_t) np * 4, cudaMemcpyHostToDevice));
182                CK(cudaMemcpy(dt, tok.data(), (size_t) nbig * 4, cudaMemcpyHostToDevice));
183                CK(cudaMemcpy(di, ident.data(), (size_t) nbig * 4, cudaMemcpyHostToDevice));
184                std::vector<H8> xq((size_t) nbig * (ELL / 32));
185                for (int e = 0; e < nbig; ++e) std::memcpy(xq.data() + (size_t) e * (ELL / 32), row.data(), row.size() * sizeof(H8));
186                uint8_t* dxq = nullptr; void* dhbuf = nullptr; float* dout = nullptr;
187                CK(cudaMalloc(&dxq, xq.size() * sizeof(H8))); CK(cudaMalloc(&dhbuf, (size_t) nbig * kHq));
188                CK(cudaMalloc(&dout, (size_t) nbig * ELL * 4));
189                CK(cudaMemcpy(dxq, xq.data(), xq.size() * sizeof(H8), cudaMemcpyHostToDevice));
190                for (int pb : {64, 240, 480}) {
191                    auto run_all = [&] {
192                        for (int q0 = 0; q0 < np; q0 += pb) {
193                            const int nb = std::min(pb, np - q0);
194                            strata::prefill::lat_q3k_fused((const uint8_t* const*) (dpgp + q0), ds + q0, nb, dxq, dhbuf,
195                                                           dout, ELL, nullptr, dc + q0, dt, di);
196                        }
197                    };
198                    run_all(); CK(cudaDeviceSynchronize());
199                    cudaEvent_t e0, e1; CK(cudaEventCreate(&e0)); CK(cudaEventCreate(&e1));
200                    const int it = 20;
201                    CK(cudaEventRecord(e0));
202                    for (int i = 0; i < it; ++i) run_all();
203                    CK(cudaEventRecord(e1)); CK(cudaEventSynchronize(e1));
204                    float ms = 0; CK(cudaEventElapsedTime(&ms, e0, e1));
205                    std::printf("%-28s tiles %5d  batch %3d  %.3f ms = %.4f us/entry\n", "engine cnt, tile-flattened", np, pb,
206                                ms / it, ms * 1000.0 / ((double) tot * it));
207                    cudaEventDestroy(e0); cudaEventDestroy(e1);
208                }
209                cudaFree(dpgp); cudaFree(ds); cudaFree(dc); cudaFree(dt); cudaFree(di); cudaFree(dxq); cudaFree(dhbuf); cudaFree(dout);
210            }
211        }
212    }
213    bench("small (compact, fits L2)", 64 * 83, false);
214    bench("big (engine spread)", 42510, true);
215    bench("big (compact, same size)", 42510, false);
216    std::printf("%s\n", "PASS: cache test");
217    return 0;
218}