okami.mom/strata

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

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

main
12.1 KiB257 linesraw
1// src/prefill/lat_q3k_engine_shape_test.cpp - "our output differs by how much", per shape.
2// ONE input, two paths, byte for byte the same arguments: the frozen serial chain
3// (lat_q3k_gu -> lat_q3k_swiglu_q8 -> lat_q3k_down) and lat_q3k_fused.  hbuf is compared on its own, so a difference
4// points at the fused epilogue (hbuf) or at the down (out).
5// Shapes: the engine's own 512-group table from the dump (dump_ref/g-off.bin, g-cnt.bin, g-tok.bin), the single groups
6// the engine failed on, nb=2, the tile boundaries, and the 128 x 4 regression.
7#include "strata/prefill/lat_q3k_fused.hpp"
8#include "strata/prefill/lat_q3k_gu.hpp"
9#include "strata/prefill/lat_q3k_swiglu.hpp"
10#include "strata/prefill/lat_q3k_down.hpp"
11#include "strata/kernels/iq_kernels.hpp"
12
13#include <cuda_runtime.h>
14#include <ggml.h>
15
16#include <algorithm>
17#include <cmath>
18#include <cstdio>
19#include <cstdlib>
20#include <cstring>
21#include <random>
22#include <string>
23#include <vector>
24
25#define CK(e) do { const cudaError_t r_ = (e); if (r_ != cudaSuccess) { \
26    std::printf("FAIL %s: %s\n", #e, cudaGetErrorString(r_)); std::exit(1); } } while (0)
27
28namespace {
29constexpr int64_t ELL = 1280, FF = 640;
30constexpr int64_t GUB = 1280 * 550, DNB = ELL * FF / 32 * 18;
31constexpr int kXq = (int) (ELL / 32) * 36;
32constexpr int kHq = (int) strata::prefill::lat_q3k_fused_hbuf_row;
33constexpr int NBLOB = 4;
34constexpr int E = 42450;          // the engine's entry space (the dumps' off[NG])
35constexpr int NG = 512;
36const std::string DUMP = "/home/user/lat-s26/q3k/dump_ref/";
37
38template <class T>
39std::vector<T> rd(const char* name, size_t expect) {
40    std::vector<T> v(expect);
41    std::FILE* f = std::fopen((DUMP + name).c_str(), "rb");
42    if (f == nullptr) { std::printf("FAIL cannot open %s\n", name); std::exit(1); }
43    const size_t n = std::fread(v.data(), sizeof(T), expect, f);
44    std::fclose(f);
45    if (n != expect) { std::printf("FAIL %s: %zu of %zu\n", name, n, expect); std::exit(1); }
46    return v;
47}
48}  // namespace
49
50struct Shape {
51    std::string tag;
52    std::vector<int32_t> starts;   // ng + 1
53    std::vector<int32_t> counts;   // ng
54    int ng() const { return (int) counts.size(); }
55};
56
57static bool hbuf_at_ok(const std::vector<uint8_t>& a, const std::vector<uint8_t>& b, size_t at) {
58    return a[at] == b[at];
59}
60
61int main() {
62    std::vector<int32_t> off = rd<int32_t>("g-off.bin", NG + 1);
63    std::vector<int32_t> cnt = rd<int32_t>("g-cnt.bin", NG);
64    std::vector<int32_t> tok = rd<int32_t>("g-tok.bin", (size_t) E);
65    if (off[NG] != E) { std::printf("FAIL off[NG] %d != %d\n", off[NG], E); return 1; }
66
67    // 4 different experts (never one blob replicated: a wrong row range would still write the right values)
68    std::mt19937 rng(91);
69    std::normal_distribution<float> nw(0.f, 0.05f);
70    std::vector<uint8_t> blob((size_t) NBLOB * (GUB + DNB));
71    for (int i = 0; i < NBLOB; ++i) {
72        uint8_t* b = blob.data() + (size_t) i * (GUB + DNB);
73        std::vector<float> w((size_t) 1280 * ELL);
74        for (auto& v : w) v = nw(rng);
75        if ((size_t) ggml_quantize_chunk(GGML_TYPE_Q3_K, w.data(), b, 0, 1280, ELL, nullptr) != (size_t) GUB) {
76            std::printf("FAIL gu quantize\n"); return 1;
77        }
78        std::vector<float> wd((size_t) ELL * FF);
79        for (auto& v : wd) v = nw(rng);
80        if ((size_t) ggml_quantize_chunk(GGML_TYPE_IQ4_NL, wd.data(), b + GUB, 0, ELL, FF, nullptr) != (size_t) DNB) {
81            std::printf("FAIL dn quantize\n"); return 1;
82        }
83    }
84    const size_t EB = (size_t) (GUB + DNB);
85    uint8_t* dall = nullptr;
86    CK(cudaMalloc(&dall, blob.size()));
87    CK(cudaMemcpy(dall, blob.data(), blob.size(), cudaMemcpyHostToDevice));
88    std::vector<const uint8_t*> bp((size_t) NG);
89    for (int g = 0; g < NG; ++g) bp[(size_t) g] = dall + (size_t) (g % NBLOB) * EB;
90    const uint8_t** dblob = nullptr;
91    CK(cudaMalloc(&dblob, (size_t) NG * sizeof(uint8_t*)));
92    CK(cudaMemcpy(dblob, bp.data(), (size_t) NG * sizeof(uint8_t*), cudaMemcpyHostToDevice));
93
94    int ntok = 0;
95    for (int32_t t : tok) ntok = std::max(ntok, (int) t);
96    ++ntok;
97    std::vector<uint8_t> xq((size_t) ntok * kXq);
98    {
99        std::uniform_int_distribution<int> u8(-127, 127);
100        for (size_t i = 0; i < xq.size(); ++i) xq[i] = (uint8_t) u8(rng);
101        for (int t = 0; t < ntok; ++t)
102            for (int k = 0; k < (int) (ELL / 32); ++k) {
103                uint8_t* b = xq.data() + (size_t) t * kXq + 36 * k;
104                const uint16_t d16 = ggml_fp32_to_fp16(0.01f + 1e-4f * ((t + k) % 97)), s16 = 0;
105                std::memcpy(b, &d16, 2); std::memcpy(b + 2, &s16, 2);
106            }
107    }
108    uint8_t* dxq = nullptr;
109    CK(cudaMalloc(&dxq, xq.size()));
110    CK(cudaMemcpy(dxq, xq.data(), xq.size(), cudaMemcpyHostToDevice));
111    int32_t* dtok = nullptr;
112    CK(cudaMalloc(&dtok, (size_t) E * 4));
113    CK(cudaMemcpy(dtok, tok.data(), tok.size() * 4, cudaMemcpyHostToDevice));
114    std::vector<int32_t> ident((size_t) E);
115    for (int e = 0; e < E; ++e) ident[(size_t) e] = e;
116    int32_t* dident = nullptr;
117    CK(cudaMalloc(&dident, (size_t) E * 4));
118    CK(cudaMemcpy(dident, ident.data(), ident.size() * 4, cudaMemcpyHostToDevice));
119
120    int32_t *dgs = nullptr, *dcnt = nullptr, *dng = nullptr;
121    CK(cudaMalloc(&dgs, (size_t) (NG + 1) * 4));
122    CK(cudaMalloc(&dcnt, (size_t) NG * 4));
123    CK(cudaMalloc(&dng, 4));
124    float* dgate = nullptr; float* dup = nullptr;
125    CK(cudaMalloc(&dgate, (size_t) E * FF * 4)); CK(cudaMalloc(&dup, (size_t) E * FF * 4));
126    uint8_t* dhq = nullptr; uint8_t* dhbuf = nullptr;
127    CK(cudaMalloc(&dhq, (size_t) E * kHq)); CK(cudaMalloc(&dhbuf, (size_t) E * kHq));
128    float* dfu = nullptr; float* dse = nullptr;
129    CK(cudaMalloc(&dfu, (size_t) E * ELL * 4)); CK(cudaMalloc(&dse, (size_t) E * ELL * 4));
130
131    int sms = 0;
132    { int dev = 0; cudaGetDevice(&dev); cudaDeviceGetAttribute(&sms, cudaDevAttrMultiProcessorCount, dev); }
133    const int kItems = (int) (ELL / 256);   // the launchers' fb count (n_embd / 256 for the down, 5 here)
134    int gz_last = 1;
135
136    std::vector<float> yfu((size_t) E * ELL), yse((size_t) E * ELL);
137    std::vector<uint8_t> hfu((size_t) E * kHq), hse((size_t) E * kHq);
138
139    // one shape: identical arguments to both paths, hbuf and out compared separately
140    auto run = [&](const Shape& sh, bool print_only) -> int {
141        const int ng = sh.ng();
142        CK(cudaMemcpy(dgs, sh.starts.data(), (size_t) (ng + 1) * 4, cudaMemcpyHostToDevice));
143        CK(cudaMemcpy(dcnt, sh.counts.data(), (size_t) ng * 4, cudaMemcpyHostToDevice));
144        CK(cudaMemcpy(dng, &ng, 4, cudaMemcpyHostToDevice));
145        gz_last = (sms > 0 && ng * kItems < sms) ? 8 : 1;
146        CK(cudaMemset(dhbuf, 0x00, (size_t) E * kHq));
147        CK(cudaMemset(dhq, 0x00, (size_t) E * kHq));
148        CK(cudaMemset(dfu, 0, (size_t) E * ELL * 4));
149        CK(cudaMemset(dse, 0, (size_t) E * ELL * 4));
150        strata::prefill::lat_q3k_fused(dblob, dgs, ng, dxq, dhbuf, dfu, ELL, nullptr, dcnt, dtok, dident);
151        strata::prefill::lat_q3k_gu(dblob, dgs, ng, dxq, dgate, dup, FF, nullptr, dcnt, dtok);
152        strata::prefill::lat_q3k_swiglu_q8(dgate, dup, E, dhq, nullptr, nullptr);
153        strata::prefill::lat_q3k_down(dblob, dgs, ng, dhq, dse, ELL, nullptr, dcnt, dident);
154        CK(cudaDeviceSynchronize());
155        CK(cudaMemcpy(yfu.data(), dfu, yfu.size() * 4, cudaMemcpyDeviceToHost));
156        CK(cudaMemcpy(yse.data(), dse, yse.size() * 4, cudaMemcpyDeviceToHost));
157        CK(cudaMemcpy(hfu.data(), dhbuf, hfu.size(), cudaMemcpyDeviceToHost));
158        CK(cudaMemcpy(hse.data(), dhq, hse.size(), cudaMemcpyDeviceToHost));
159
160        // the rows this shape owns
161        std::vector<char> mine((size_t) E, 0);
162        for (int g = 0; g < ng; ++g)
163            for (int e = sh.starts[(size_t) g]; e < sh.starts[(size_t) g] + sh.counts[(size_t) g]; ++e) mine[(size_t) e] = 1;
164
165        // hbuf: per entry, the 68 bytes the down reads (codes + the two d's)
166        long long hb_diff = 0, hb_tot = 0, hb_pad = 0;
167        int hb_first_e = -1, hb_first_c = -1, hb_first_b = -1, hb_zdiff = 0;
168        for (int e = 0; e < E; ++e) {
169            if (!mine[(size_t) e]) continue;
170            for (int c = 0; c < 10; ++c)
171                for (int b = 0; b < 80; ++b) {
172                    const size_t at = (size_t) e * kHq + (size_t) c * 80 + (size_t) b;
173                    ++hb_tot;
174                    if (hbuf_at_ok(hfu, hse, at)) continue;
175                    if (b >= 68) { ++hb_pad; continue; }
176                    ++hb_diff;
177                    if (hb_first_e < 0) { hb_first_e = e; hb_first_c = c; hb_first_b = b; }
178                }
179        }
180        // out: 1280 floats an entry
181        long long o_diff = 0, o_tot = 0;
182        double o_max = 0, o_abs = 0, o_ref = 0;
183        int o_first_e = -1, o_first_r = -1;
184        for (int e = 0; e < E; ++e) {
185            if (!mine[(size_t) e]) continue;
186            for (int r = 0; r < ELL; ++r) {
187                const size_t at = (size_t) e * ELL + r;
188                const double a = yfu[at], b2 = yse[at];
189                ++o_tot;
190                if (a == b2) continue;
191                ++o_diff;
192                const double d = std::fabs(a - b2);
193                o_max = std::max(o_max, d); o_abs += d; o_ref += std::fabs(b2);
194                if (o_first_e < 0) { o_first_e = e; o_first_r = r; }
195            }
196        }
197        // where the first hbuf difference sits in the tiles (which z slice would own that row)
198        int rowin = -1, zs = -1, gid = -1;
199        if (hb_first_e >= 0)
200            for (int g = 0; g < ng; ++g)
201                if (hb_first_e >= sh.starts[(size_t) g] && hb_first_e < sh.starts[(size_t) g] + sh.counts[(size_t) g]) {
202                    gid = g; rowin = hb_first_e - sh.starts[(size_t) g]; zs = (rowin / 64) % gz_last; break;
203                }
204        std::printf("%-34s ng=%-4d rows=%-6zu gz=%d\n", sh.tag.c_str(), ng, (size_t) std::count(mine.begin(), mine.end(), 1),
205                    gz_last);
206        std::printf("   hbuf: differing %lld / %lld bytes (pad %lld)  first at entry %d chunk %d byte %d"
207                    " (group %d, row %d, z %d)\n", hb_diff, hb_tot, hb_pad, hb_first_e, hb_first_c, hb_first_b, gid,
208                    rowin, zs);
209        std::printf("   out : differing %lld / %lld values  max|d| %.3e  rel %.3e  first at entry %d row %d\n", o_diff,
210                    o_tot, o_max, o_ref > 0 ? o_abs / o_ref : 0.0, o_first_e, o_first_r);
211        return (hb_diff == 0 && o_diff == 0) ? 0 : 1;
212    };
213
214    // ---- the shape table ---------------------------------------------------------------------------------------
215    std::vector<Shape> shapes;
216    {   // 1. the engine's own table
217        Shape s; s.tag = "engine table (512 groups, dump)";
218        s.starts = off; s.counts = cnt;
219        shapes.push_back(s);
220    }
221    {   // 2. the single groups the engine failed on
222        const int32_t one[][2] = {{468, 30567}, {41, 31035}, {2749, 32028}, {400, 34777}, {2376, 37469}};
223        for (const auto& v : one) {
224            Shape s;
225            char t[64]; std::snprintf(t, sizeof t, "nb=1 cnt=%d start=%d", v[0], v[1]);
226            s.tag = t; s.starts = {v[1], v[1] + v[0]}; s.counts = {v[0]};
227            shapes.push_back(s);
228        }
229    }
230    {   // 3. nb=2
231        Shape s; s.tag = "nb=2 cnt={468,41} start={30567,31035}";
232        s.starts = {30567, 31035, 31076}; s.counts = {468, 41};
233        shapes.push_back(s);
234    }
235    {   // 4. every count from 1 to 64, then the boundaries and the engine's big sizes
236        std::vector<int> sizes;
237        for (int c = 1; c <= 64; ++c) sizes.push_back(c);
238        for (int c : {65, 128, 256, 512, 1024, 1375, 2062, 2748, 2749, 3098}) sizes.push_back(c);
239        for (int c : sizes) {
240            Shape s;
241            char t[64]; std::snprintf(t, sizeof t, "nb=1 sweep cnt=%d", c);
242            s.tag = t; s.starts = {0, c}; s.counts = {c};
243            shapes.push_back(s);
244        }
245    }
246    {   // 5. the 128 x 4 regression
247        Shape s; s.tag = "regression 128 groups x 4 entries";
248        for (int g = 0; g <= 128; ++g) s.starts.push_back(g * 4);
249        s.counts.assign(128, 4);
250        shapes.push_back(s);
251    }
252
253    int fails = 0;
254    for (const Shape& sh : shapes) fails += run(sh, false);
255    std::printf("%s: %zu shapes, %d with a difference\n", fails == 0 ? "PASS" : "FAIL", shapes.size(), fails);
256    return fails == 0 ? 0 : 1;
257}