okami.mom/strata

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

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

main
10.1 KiB199 linesraw
1// src/prefill/lat_q3k_onegroup_test.cpp - the shape the engine actually runs when it walks one expert at a time:
2// ONE group with thousands of rows (so the launcher's blockIdx.z fill is active), and the boundaries around a tile.
3// The reference is native_expert_grouped, which has no z fill at all - the serial chain of my own kernels cannot be
4// the reference here, because the gu and the down both carry the same z fill and a defect in it cancels out.
5#include "strata/prefill/lat_q3k_fused.hpp"
6#include "strata/prefill/lat_q3k_gu.hpp"
7#include "strata/prefill/lat_q3k_swiglu.hpp"
8#include "strata/prefill/lat_q3k_down.hpp"
9#include "strata/kernels/iq_kernels.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 E = 42450;          // the engine's entry space (the dumps' off[NG])
33constexpr int NTOK = E;           // one xl row an entry here (the engine shares rows; the maps are tested elsewhere)
34
35long long n_diff(const float* a, const float* b, size_t n) {
36    long long d = 0;
37    for (size_t i = 0; i < n; ++i) d += (a[i] != b[i]);
38    return d;
39}
40}  // namespace
41
42int main() {
43    std::mt19937 rng(83);
44    std::normal_distribution<float> nw(0.f, 0.05f);
45    std::vector<uint8_t> blob((size_t) NBLOB * (GUB + DNB));
46    for (int i = 0; i < NBLOB; ++i) {
47        uint8_t* b = blob.data() + (size_t) i * (GUB + DNB);
48        std::vector<float> w((size_t) 1280 * ELL);
49        for (auto& v : w) v = nw(rng);
50        if ((size_t) ggml_quantize_chunk(GGML_TYPE_Q3_K, w.data(), b, 0, 1280, ELL, nullptr) != (size_t) GUB) {
51            std::printf("FAIL gu quantize\n"); return 1;
52        }
53        std::vector<float> wd((size_t) ELL * FF);
54        for (auto& v : wd) v = nw(rng);
55        if ((size_t) ggml_quantize_chunk(GGML_TYPE_IQ4_NL, wd.data(), b + GUB, 0, ELL, FF, nullptr) != (size_t) DNB) {
56            std::printf("FAIL dn quantize\n"); return 1;
57        }
58    }
59    const size_t EB = (size_t) (GUB + DNB);
60    uint8_t* dall = nullptr;
61    CK(cudaMalloc(&dall, blob.size()));
62    CK(cudaMemcpy(dall, blob.data(), blob.size(), cudaMemcpyHostToDevice));
63    std::vector<const uint8_t*> bp((size_t) 8);
64    for (int g = 0; g < 8; ++g) bp[(size_t) g] = dall + (size_t) (g % NBLOB) * EB;
65    const uint8_t** dblob = nullptr;
66    CK(cudaMalloc(&dblob, 8 * sizeof(uint8_t*)));
67    CK(cudaMemcpy(dblob, bp.data(), 8 * sizeof(uint8_t*), cudaMemcpyHostToDevice));
68
69    std::vector<uint8_t> xq((size_t) NTOK * kXq);
70    {
71        std::uniform_int_distribution<int> u8(-127, 127);
72        for (size_t i = 0; i < xq.size(); ++i) xq[i] = (uint8_t) u8(rng);
73        for (int t = 0; t < NTOK; ++t)
74            for (int k = 0; k < (int) (ELL / 32); ++k) {
75                uint8_t* b = xq.data() + (size_t) t * kXq + 36 * k;
76                const uint16_t d16 = ggml_fp32_to_fp16(0.01f + 1e-4f * ((t + k) % 97)), s16 = 0;
77                std::memcpy(b, &d16, 2); std::memcpy(b + 2, &s16, 2);
78            }
79    }
80    uint8_t* dxq = nullptr;
81    CK(cudaMalloc(&dxq, xq.size()));
82    CK(cudaMemcpy(dxq, xq.data(), xq.size(), cudaMemcpyHostToDevice));
83
84    int32_t *dgs = nullptr, *dcnt = nullptr, *dident = nullptr, *dng = nullptr;
85    CK(cudaMalloc(&dgs, 9 * 4)); CK(cudaMalloc(&dcnt, 8 * 4));
86    CK(cudaMalloc(&dident, (size_t) E * 4)); CK(cudaMalloc(&dng, 4));
87    std::vector<int32_t> ident((size_t) E);
88    for (int e = 0; e < E; ++e) ident[(size_t) e] = e;
89    CK(cudaMemcpy(dident, ident.data(), ident.size() * 4, cudaMemcpyHostToDevice));
90    int32_t* dident_tok = nullptr;   // native dereferences ent_tok as well: the identity, so entry e reads row e
91    CK(cudaMalloc(&dident_tok, (size_t) E * 4));
92    CK(cudaMemcpy(dident_tok, ident.data(), ident.size() * 4, cudaMemcpyHostToDevice));
93
94    float* dgate = nullptr; float* dup = nullptr;
95    CK(cudaMalloc(&dgate, (size_t) E * FF * 4)); CK(cudaMalloc(&dup, (size_t) E * FF * 4));
96    uint8_t* dhq = nullptr; uint8_t* dhbuf = nullptr;
97    CK(cudaMalloc(&dhq, (size_t) E * kHq)); CK(cudaMalloc(&dhbuf, (size_t) E * kHq));
98    float *dy = nullptr, *dy2 = nullptr, *dyn = nullptr;
99    CK(cudaMalloc(&dy, (size_t) E * ELL * 4)); CK(cudaMalloc(&dy2, (size_t) E * ELL * 4));
100    CK(cudaMalloc(&dyn, (size_t) E * ELL * 4));
101    const auto L = strata::kernels::native_expert_layout(GGML_TYPE_Q3_K, GGML_TYPE_IQ4_NL, ELL, FF);
102    void* scratch = nullptr;
103    CK(cudaMalloc(&scratch, strata::kernels::native_expert_scratch_bytes(E, FF)));
104
105    std::vector<float> ys((size_t) E * ELL), yf((size_t) E * ELL), yn((size_t) E * ELL);
106
107    // one case: ng groups with (starts, counts); every row zeroed first so an unwritten row shows
108    auto run_case = [&](const char* tag, int ng, const int32_t* starts, const int32_t* counts, bool vs_native) -> int {
109        CK(cudaMemcpy(dgs, starts, (size_t) (ng + 1) * 4, cudaMemcpyHostToDevice));
110        CK(cudaMemcpy(dcnt, counts, (size_t) ng * 4, cudaMemcpyHostToDevice));
111        CK(cudaMemcpy(dng, &ng, 4, cudaMemcpyHostToDevice));
112        int total = 0;
113        for (int g = 0; g < ng; ++g) total += counts[g];
114        // fused
115        CK(cudaMemset(dy2, 0, (size_t) E * ELL * 4));
116        strata::prefill::lat_q3k_fused(dblob, dgs, ng, dxq, dhbuf, dy2, ELL, nullptr, dcnt, nullptr, dident);
117        // serial, same buffers
118        CK(cudaMemset(dy, 0, (size_t) E * ELL * 4));
119        strata::prefill::lat_q3k_gu(dblob, dgs, ng, dxq, dgate, dup, FF, nullptr, dcnt, nullptr);
120        strata::prefill::lat_q3k_swiglu_q8(dgate, dup, E, dhq, nullptr, nullptr);
121        strata::prefill::lat_q3k_down(dblob, dgs, ng, dhq, dy, ELL, nullptr, dcnt, dident);
122        CK(cudaDeviceSynchronize());
123        CK(cudaMemcpy(ys.data(), dy, ys.size() * 4, cudaMemcpyDeviceToHost));
124        CK(cudaMemcpy(yf.data(), dy2, yf.size() * 4, cudaMemcpyDeviceToHost));
125        const long long dser = n_diff(ys.data(), yf.data(), ys.size());
126        double rel = 0, cosmin = 1.0;
127        int bad = 0, first_bad = -1, first_rel = -1;
128        if (vs_native) {
129            strata::kernels::native_expert_set_mode(-1, 0);
130            CK(cudaMemset(dyn, 0, (size_t) E * ELL * 4));
131            strata::kernels::native_expert_grouped(L, (const unsigned long long*) dblob, dgs, dng, dident, dident_tok,
132                                                   ng, E, dxq, scratch, dyn, nullptr, 0);
133            CK(cudaDeviceSynchronize());
134            CK(cudaMemcpy(yn.data(), dyn, yn.size() * 4, cudaMemcpyDeviceToHost));
135            double sabs = 0, sref = 0;
136            for (int g = 0; g < ng; ++g)
137                for (int e = starts[g]; e < starts[g] + counts[g]; ++e) {
138                    double num = 0, aa = 0, bb = 0;
139                    for (int r = 0; r < ELL; ++r) {
140                        const double a = yf[(size_t) e * ELL + r], b = yn[(size_t) e * ELL + r];
141                        sabs += std::fabs(a - b); sref += std::fabs(b);
142                        num += a * b; aa += a * a; bb += b * b;
143                    }
144                    const double c = num / (std::sqrt(aa) * std::sqrt(bb) + 1e-30);
145                    cosmin = std::min(cosmin, c);
146                    if (c < 0.99) {
147                        ++bad;
148                        if (first_bad < 0) first_bad = e - starts[g];
149                        if (first_rel < 0 && std::fabs(yf[(size_t) e * ELL] - yn[(size_t) e * ELL]) > 1e-9)
150                            first_rel = e - starts[g];
151                    }
152                }
153            rel = sref > 0 ? sabs / sref : 0.0;
154        }
155        std::printf("%-28s ng=%d rows=%5d | fused vs serial: %lld diff | vs native: rel %.3e cos_min %.9f bad %d "
156                    "(first bad row-in-group %d)\n", tag, ng, total, dser, rel, cosmin, bad, first_bad);
157        const bool ok = dser == 0 && (!vs_native || (rel <= 1e-3 && cosmin >= 0.9999 && bad == 0));
158        if (!ok) {
159            std::printf("   FAIL %s\n", tag);
160            for (int g = 0; g < ng; ++g) {
161                long long dg = 0;
162                int fb = -1;
163                for (int e = starts[g]; e < starts[g] + counts[g]; ++e) {
164                    const long long d = n_diff(yf.data() + (size_t) e * ELL, ys.data() + (size_t) e * ELL, ELL);
165                    if (d && fb < 0) fb = e - starts[g];
166                    dg += d;
167                }
168                std::printf("   group %d (start %d, cnt %d): %lld floats differ from serial, first at row %d\n", g,
169                            starts[g], counts[g], dg, fb);
170            }
171        }
172        return ok ? 0 : 1;
173    };
174
175    int fails = 0;
176    // the engine's real big groups, one group a launch (what the per-expert walk does)
177    {
178        const int32_t s1[2] = {32028, 32028 + 2749}, c1[1] = {2749};
179        fails += run_case("nb=1 cnt=2749 (group 327)", 1, s1, c1, true);
180        const int32_t s2[2] = {34777, 34777 + 400}, c2[1] = {400};
181        fails += run_case("nb=1 cnt=400 (group 334)", 1, s2, c2, true);
182        const int32_t s3[2] = {37469, 37469 + 2376}, c3[1] = {2376};
183        fails += run_case("nb=1 cnt=2376 (group 434)", 1, s3, c3, true);
184        const int32_t s4[3] = {30567, 31035, 31076}, c4[2] = {468, 41};
185        fails += run_case("nb=2 cnt={468,41} (253/254)", 2, s4, c4, true);
186    }
187    // the boundaries around a tile, and the counts the engine really reaches
188    {
189        const int sizes[] = {1, 2, 63, 64, 65, 127, 128, 129, 255, 256, 257, 468, 1231, 2748, 2749, 3098};
190        for (int c : sizes) {
191            const int32_t s[2] = {0, c}, ct[1] = {c};
192            char tag[64];
193            std::snprintf(tag, sizeof tag, "nb=1 sweep cnt=%d", c);
194            fails += run_case(tag, 1, s, ct, (c == 2749 || c == 3098));
195        }
196    }
197    std::printf("%s: one-group / z-fill / tile boundaries (fails %d)\n", fails == 0 ? "PASS" : "FAIL", fails);
198    return fails == 0 ? 0 : 1;
199}