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.5 KiB256 linesraw
1// src/prefill/lat_q3k_fusedv5_test.cpp - the equivalence gate of moe_fused_q3k.cu (v5's kernel with the q3k decode)
2// against the frozen chain lat_q3k_gu -> lat_q3k_swiglu_q8 -> lat_q3k_down, on the same input, byte for byte:
3//   shape 1: 128 groups x 4 entries (the unit tests' shape)
4//   shape 2: the engine's 512 groups with the dumped off[]/cnt[] (42450 entries, off[] not monotonic)
5#include "strata/prefill/moe_fused_q3k.hpp"
6#include "strata/prefill/lat_q3k_fused.hpp"
7#include "strata/prefill/lat_q3k_gu.hpp"
8#include "strata/prefill/lat_q3k_swiglu.hpp"
9#include "strata/prefill/lat_q3k_down.hpp"
10
11#include <cuda_runtime.h>
12#include <ggml.h>
13
14#include <algorithm>
15#include <cmath>
16#include <cstdio>
17#include <cstdlib>
18#include <cstring>
19#include <random>
20#include <string>
21#include <vector>
22
23#define CK(e) do { const cudaError_t r_ = (e); if (r_ != cudaSuccess) { \
24    std::printf("FAIL %s: %s\n", #e, cudaGetErrorString(r_)); std::exit(1); } } while (0)
25
26namespace {
27constexpr int64_t ELL = 1280, FF = 640;
28constexpr int64_t GUB = 1280 * 550, DNB = ELL * FF / 32 * 18;
29constexpr int kXq = (int) (ELL / 32) * 36;
30constexpr int kHq = (int) strata::prefill::lat_q3k_fused_hbuf_row;
31constexpr int NBLOB = 4;
32constexpr int NG = 512, E = 42450;          // the engine's entry space (the dumps' off[NG])
33const std::string DUMP = "/home/user/lat-s26/q3k/dump_ref/";
34
35template <class T>
36std::vector<T> rd(const char* name, size_t expect) {
37    std::vector<T> v(expect);
38    std::FILE* f = std::fopen((DUMP + name).c_str(), "rb");
39    if (f == nullptr) { std::printf("FAIL cannot open %s\n", name); std::exit(1); }
40    const size_t n = std::fread(v.data(), sizeof(T), expect, f);
41    std::fclose(f);
42    if (n != expect) { std::printf("FAIL %s: %zu of %zu\n", name, n, expect); std::exit(1); }
43    return v;
44}
45
46template <class T>
47void upl(T* dst, const std::vector<T>& src) {
48    CK(cudaMemcpy(dst, src.data(), src.size() * sizeof(T), cudaMemcpyHostToDevice));
49}
50}  // namespace
51
52static void step(const char* m) { std::printf("[step] %s\n", m); std::fflush(stdout); }
53
54int main() {
55    std::mt19937 rng(101);
56    step("start");
57    std::normal_distribution<float> nw(0.f, 0.05f);
58    std::vector<uint8_t> blob((size_t) NBLOB * (GUB + DNB));
59    for (int i = 0; i < NBLOB; ++i) {
60        uint8_t* b = blob.data() + (size_t) i * (GUB + DNB);
61        std::vector<float> w((size_t) 1280 * ELL);
62        for (auto& v : w) v = nw(rng);
63        if ((size_t) ggml_quantize_chunk(GGML_TYPE_Q3_K, w.data(), b, 0, 1280, ELL, nullptr) != (size_t) GUB) {
64            std::printf("FAIL gu quantize\n"); return 1;
65        }
66        std::vector<float> wd((size_t) ELL * FF);
67        for (auto& v : wd) v = nw(rng);
68        if ((size_t) ggml_quantize_chunk(GGML_TYPE_IQ4_NL, wd.data(), b + GUB, 0, ELL, FF, nullptr) != (size_t) DNB) {
69            std::printf("FAIL dn quantize\n"); return 1;
70        }
71    }
72    step("blobs quantized");
73    const size_t EB = (size_t) (GUB + DNB);
74    uint8_t* dall = nullptr;
75    CK(cudaMalloc(&dall, blob.size()));
76    CK(cudaMemcpy(dall, blob.data(), blob.size(), cudaMemcpyHostToDevice));
77
78    // 512 experts, one blob each (the v5 batch holds the whole layer)
79    std::vector<const uint8_t*> bp((size_t) NG);
80    for (int g = 0; g < NG; ++g) bp[(size_t) g] = dall + (size_t) (g % NBLOB) * EB;
81    strata::prefill::fused::WideBatch wb;
82    wb.e0 = 0; wb.e1 = NG;
83    for (int g = 0; g < NG; ++g) wb.blob[g] = bp[(size_t) g];
84    const uint8_t** dblob = nullptr;
85    CK(cudaMalloc(&dblob, (size_t) NG * sizeof(uint8_t*)));
86    CK(cudaMemcpy(dblob, bp.data(), (size_t) NG * sizeof(uint8_t*), cudaMemcpyHostToDevice));
87
88    step("blobs on device");
89    std::vector<uint8_t> xq((size_t) E * kXq);
90    {
91        std::uniform_int_distribution<int> u8(-127, 127);
92        for (size_t i = 0; i < xq.size(); ++i) xq[i] = (uint8_t) u8(rng);
93        for (int t = 0; t < E; ++t)
94            for (int k = 0; k < (int) (ELL / 32); ++k) {
95                uint8_t* b = xq.data() + (size_t) t * kXq + 36 * k;
96                const uint16_t d16 = ggml_fp32_to_fp16(0.01f + 1e-4f * ((t + k) % 97)), s16 = 0;
97                std::memcpy(b, &d16, 2); std::memcpy(b + 2, &s16, 2);
98            }
99    }
100    uint8_t* dxq = nullptr;
101    CK(cudaMalloc(&dxq, xq.size()));
102    CK(cudaMemcpy(dxq, xq.data(), xq.size(), cudaMemcpyHostToDevice));
103    std::vector<int32_t> ident((size_t) E);
104    for (int e = 0; e < E; ++e) ident[(size_t) e] = e;
105    int32_t* dident = nullptr;
106    CK(cudaMalloc(&dident, (size_t) E * 4));
107    upl(dident, ident);
108
109    step("xq ready");
110    int32_t *dgs = nullptr, *dcnt = nullptr, *dng = nullptr;
111    CK(cudaMalloc(&dgs, (size_t) (NG + 1) * 4));
112    CK(cudaMalloc(&dcnt, (size_t) NG * 4));
113    CK(cudaMalloc(&dng, 4));
114    float* dgate = nullptr; float* dup = nullptr;
115    CK(cudaMalloc(&dgate, (size_t) E * FF * 4)); CK(cudaMalloc(&dup, (size_t) E * FF * 4));
116    uint8_t* dhq = nullptr; uint8_t* dhbuf = nullptr;
117    CK(cudaMalloc(&dhq, (size_t) E * kHq)); CK(cudaMalloc(&dhbuf, (size_t) E * kHq));
118    float* dref = nullptr; float* dv5 = nullptr;
119    CK(cudaMalloc(&dref, (size_t) E * ELL * 4)); CK(cudaMalloc(&dv5, (size_t) E * ELL * 4));
120    // the v5 path's grouping tables (group_latent: the same counts/offsets, the 64-row tile list per expert)
121    void* htables = nullptr;
122    CK(cudaMalloc(&htables, strata::prefill::fused::latent_group_bytes(E, NG)));
123    void* dtables = nullptr;
124    CK(cudaMalloc(&dtables, strata::prefill::fused::latent_group_bytes(E, NG)));
125    const strata::prefill::fused::LatentGeom lg{1280, 640, 550, 360, 352000, 704000, 0};
126
127    std::vector<float> yref((size_t) E * ELL), yv5((size_t) E * ELL);
128    std::vector<uint8_t> href((size_t) E * kHq), hv5((size_t) E * kHq);
129
130    step("buffers ready");
131    auto run_shape = [&](const char* tag, const std::vector<int32_t>& off, const std::vector<int32_t>& cnt) -> int {
132        step(tag);
133        const int ng = (int) cnt.size();
134        upl(dgs, off); upl(dcnt, cnt);
135        CK(cudaMemcpy(dng, &ng, 4, cudaMemcpyHostToDevice));
136        int total = 0;
137        for (int c : cnt) total += c;
138        step("  reference");
139        // reference: the frozen chain
140        CK(cudaMemset(dhq, 0, (size_t) E * kHq));
141        CK(cudaMemset(dref, 0, (size_t) E * ELL * 4));
142        strata::prefill::lat_q3k_gu(dblob, dgs, ng, dxq, dgate, dup, FF, nullptr, dcnt, nullptr);
143        strata::prefill::lat_q3k_swiglu_q8(dgate, dup, E, dhq, nullptr, nullptr);
144        strata::prefill::lat_q3k_down(dblob, dgs, ng, dhq, dref, ELL, nullptr, dcnt, nullptr);
145        step("  tables");
146        // under test: moe_fused_q3k (v5's kernel + the q3k decode)
147        CK(cudaMemset(htables, 0, strata::prefill::fused::latent_group_bytes(E, NG)));
148        {
149            std::vector<char> t(strata::prefill::fused::latent_group_bytes(E, NG));
150            strata::prefill::fused::group_latent(cnt.data(), off.data(), NG, t.data(), t.size());
151            CK(cudaMemcpy(dtables, t.data(), t.size(), cudaMemcpyHostToDevice));
152        }
153        CK(cudaMemset(dhbuf, 0, (size_t) E * kHq));
154        CK(cudaMemset(dv5, 0, (size_t) E * ELL * 4));
155        step("  v5 launch");
156        strata::prefill::fused_q3k::experts_latent_q3k(wb, lg, NG, E, dtables, dxq, dident, dhbuf, dv5, nullptr);
157        CK(cudaDeviceSynchronize());
158        step("  copies");
159        CK(cudaMemcpy(yref.data(), dref, yref.size() * 4, cudaMemcpyDeviceToHost));
160        CK(cudaMemcpy(yv5.data(), dv5, yv5.size() * 4, cudaMemcpyDeviceToHost));
161        CK(cudaMemcpy(href.data(), dhq, href.size(), cudaMemcpyDeviceToHost));
162        CK(cudaMemcpy(hv5.data(), dhbuf, hv5.size(), cudaMemcpyDeviceToHost));
163
164        long long hdiff = 0, odiff = 0, h_first = -1, o_first = -1;
165        double omax = 0, oabs = 0, oref = 0;
166        std::vector<char> mine((size_t) E, 0);
167        for (int g = 0; g < ng; ++g)
168            for (int e = off[(size_t) g]; e < off[(size_t) g] + cnt[(size_t) g]; ++e) mine[(size_t) e] = 1;
169        for (int e = 0; e < E; ++e) {
170            if (!mine[(size_t) e]) continue;
171            for (size_t b = 0; b < (size_t) kHq; ++b) {
172                const size_t at = (size_t) e * kHq + b;
173                if (href[at] != hv5[at]) { ++hdiff; if (h_first < 0) h_first = (long long) at; }
174            }
175            for (int r = 0; r < ELL; ++r) {
176                const size_t at = (size_t) e * ELL + r;
177                const double a = yref[at], b2 = yv5[at];
178                if (a != b2) {
179                    ++odiff; if (o_first < 0) o_first = (long long) at;
180                    const double d = std::fabs(a - b2);
181                    omax = std::max(omax, d); oabs += d;
182                }
183                oref += std::fabs(b2);
184            }
185        }
186        {   // is either side non-finite, and what does the first difference look like?
187            long long nf_ref = 0, nf_v5 = 0;
188            for (int e = 0; e < E; ++e) {
189                if (!mine[(size_t) e]) continue;
190                for (int r = 0; r < ELL; ++r) {
191                    const float a = yref[(size_t) e * ELL + r], b2 = yv5[(size_t) e * ELL + r];
192                    if (!std::isfinite(a)) ++nf_ref;
193                    if (!std::isfinite(b2)) ++nf_v5;
194                }
195            }
196            std::printf("%-30s ng=%d rows=%d  non-finite: ref %lld, v5 %lld\n", tag, ng, total, nf_ref, nf_v5);
197            if (o_first >= 0) {
198                const int e = (int) (o_first / ELL), r = (int) (o_first % ELL);
199                std::printf("   first diff at entry %d row %d: ref %.6g  v5 %.6g   (ref[0:4] %.6g %.6g %.6g %.6g)\n", e, r,
200                            (double) yref[(size_t) o_first], (double) yv5[(size_t) o_first],
201                            (double) yref[(size_t) e * ELL], (double) yref[(size_t) e * ELL + 1],
202                            (double) yref[(size_t) e * ELL + 2], (double) yref[(size_t) e * ELL + 3]);
203            }
204        }
205        std::printf("   hbuf: differing %lld / %lld B  first at byte %lld\n", hdiff, (long long) total * kHq, h_first);
206        std::printf("   out : differing %lld / %lld values  max|d| %.3e  rel %.3e  first at value %lld\n", odiff,
207                    (long long) total * ELL, omax, oref > 0 ? oabs / oref : 0.0, o_first);
208        return (hdiff == 0 && odiff == 0) ? 0 : 1;
209    };
210
211    int fails = 0;
212    {   // shape 1: 128 groups x 4 entries - padded to the pack's 512 experts, which is what group_latent walks
213        std::vector<int32_t> off((size_t) NG + 1), cnt((size_t) NG, 0);
214        for (int g = 0; g < 128; ++g) cnt[(size_t) g] = 4;
215        for (int g = 0; g <= NG; ++g) off[(size_t) g] = 4 * std::min(g, 128);
216        fails += run_shape("128 groups x 4 entries", off, cnt);
217    }
218    {   // shape 2: the engine's own table (already 512 experts)
219        std::vector<int32_t> off = rd<int32_t>("g-off.bin", NG + 1);
220        std::vector<int32_t> cnt = rd<int32_t>("g-cnt.bin", NG);
221        fails += run_shape("engine table (512 groups)", off, cnt);
222    }
223
224    // timing (reported only after the gate passes)
225    if (fails == 0) {
226        std::vector<int32_t> off = rd<int32_t>("g-off.bin", NG + 1);
227        std::vector<int32_t> cnt = rd<int32_t>("g-cnt.bin", NG);
228        upl(dgs, off); upl(dcnt, cnt);
229        CK(cudaMemcpy(dng, &NG, 4, cudaMemcpyHostToDevice));
230        cudaEvent_t t0, t1; CK(cudaEventCreate(&t0)); CK(cudaEventCreate(&t1));
231        strata::prefill::fused_q3k::experts_latent_q3k(wb, lg, NG, E, dtables, dxq, dident, dhbuf, dv5, nullptr);
232        CK(cudaDeviceSynchronize());
233        CK(cudaEventRecord(t0));
234        for (int i = 0; i < 20; ++i)
235            strata::prefill::fused_q3k::experts_latent_q3k(wb, lg, NG, E, dtables, dxq, dident, dhbuf, dv5, nullptr);
236        CK(cudaEventRecord(t1)); CK(cudaEventSynchronize(t1));
237        float ms = 0; CK(cudaEventElapsedTime(&ms, t0, t1));
238        std::printf("experts_latent_q3k: %.3f ms / 20 = %.4f ms  %.4f us/entry (engine shape, %d entries)\n", ms,
239                    ms / 20.0, ms / 20.0 * 1000.0 / E, E);
240        std::printf("frozen chain for comparison:");
241        CK(cudaEventRecord(t0));
242        for (int i = 0; i < 20; ++i) {
243            strata::prefill::lat_q3k_gu(dblob, dgs, NG, dxq, dgate, dup, FF, nullptr, dcnt, nullptr);
244            strata::prefill::lat_q3k_swiglu_q8(dgate, dup, E, dhq, nullptr, nullptr);
245            strata::prefill::lat_q3k_down(dblob, dgs, NG, dhq, dref, ELL, nullptr, dcnt, nullptr);
246        }
247        CK(cudaEventRecord(t1)); CK(cudaEventSynchronize(t1));
248        CK(cudaEventElapsedTime(&ms, t0, t1));
249        std::printf(" %.4f ms  %.4f us/entry\n", ms / 20.0, ms / 20.0 * 1000.0 / E);
250        CK(cudaEventDestroy(t0)); CK(cudaEventDestroy(t1));
251    }
252
253    std::printf("%s: moe_fused_q3k equivalent to the frozen chain (%d shapes differ)\n", fails == 0 ? "PASS" : "FAIL",
254                fails);
255    return fails == 0 ? 0 : 1;
256}