okami.mom/strata

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

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

main
8.6 KiB174 linesraw
1// src/prefill/lat_q3k_maps_test.cpp - M4's unit gate for the two entry maps the engine needs (native_expert_grouped's
2// semantics): ent_tok[e] = the activation row entry e reads (the engine's m.tok: several entries share one xl row),
3// ent_dst[e] = the output row it writes (m.ids_identity).  hq stays entry-indexed.
4//   1. ent_tok: an input whose rows are shared (two entries a row, the engine's shape) gives the same answer as the
5//      same data laid out one row an entry with no map;
6//   2. ent_dst: a permutation of the output rows;
7//   3. with both maps, fused == gu(ent_tok) -> swiglu -> down(ent_dst) byte for byte.
8#include "strata/prefill/lat_q3k_fused.hpp"
9#include "strata/prefill/lat_q3k_gu.hpp"
10#include "strata/prefill/lat_q3k_swiglu.hpp"
11#include "strata/prefill/lat_q3k_down.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 <vector>
23
24#define CK(e) do { const cudaError_t r_ = (e); if (r_ != cudaSuccess) { \
25    std::printf("FAIL %s: %s\n", #e, cudaGetErrorString(r_)); std::exit(1); } } while (0)
26
27namespace {
28constexpr int64_t ELL = 1280, FF = 640;
29constexpr int64_t GUB = 1280 * 550, DNB = ELL * FF / 32 * 18;
30constexpr int kXq = (int) (ELL / 32) * 36;
31constexpr int kHq = (int) strata::prefill::lat_q3k_fused_hbuf_row;
32constexpr int G = 64, EPG = 4, E = G * EPG;
33
34long long n_diff(const float* a, const float* b, size_t n) {
35    long long d = 0;
36    for (size_t i = 0; i < n; ++i) d += (a[i] != b[i]);
37    return d;
38}
39}  // namespace
40
41int main() {
42    std::mt19937 rng(61);
43    std::normal_distribution<float> nw(0.f, 0.05f), nd(0.f, 1.f);
44    constexpr int NBLOB = 4;
45
46    std::vector<uint8_t> blob((size_t) NBLOB * (GUB + DNB));
47    for (int i = 0; i < NBLOB; ++i) {
48        uint8_t* b = blob.data() + (size_t) i * (GUB + DNB);
49        std::vector<float> w((size_t) 1280 * ELL);
50        for (auto& v : w) v = nw(rng);
51        if ((size_t) ggml_quantize_chunk(GGML_TYPE_Q3_K, w.data(), b, 0, 1280, ELL, nullptr) != (size_t) GUB) {
52            std::printf("FAIL gu quantize\n"); return 1;
53        }
54        std::vector<float> wd((size_t) ELL * FF);
55        for (auto& v : wd) v = nw(rng);
56        if ((size_t) ggml_quantize_chunk(GGML_TYPE_IQ4_NL, wd.data(), b + GUB, 0, ELL, FF, nullptr) != (size_t) DNB) {
57            std::printf("FAIL dn quantize\n"); return 1;
58        }
59    }
60    const size_t EB = (size_t) (GUB + DNB);
61    uint8_t* dall = nullptr;
62    CK(cudaMalloc(&dall, blob.size()));
63    CK(cudaMemcpy(dall, blob.data(), blob.size(), cudaMemcpyHostToDevice));
64
65    // blobs of the groups: group g (whose row range is [4g, 4g+4)) gets expert (4g) % NBLOB
66    std::vector<const uint8_t*> bp((size_t) G);
67    for (int g = 0; g < G; ++g) bp[(size_t) g] = dall + (size_t) ((EPG * g) % NBLOB) * EB;
68    const uint8_t** dblob = nullptr;
69    CK(cudaMalloc(&dblob, (size_t) G * sizeof(uint8_t*)));
70    CK(cudaMemcpy(dblob, bp.data(), (size_t) G * sizeof(uint8_t*), cudaMemcpyHostToDevice));
71
72    // NTOK token rows, two entries each (the engine: entries of one token share the xl row)
73    const int NTOK = E / 2;
74    std::vector<uint8_t> xqt((size_t) NTOK * kXq);
75    {
76        std::uniform_int_distribution<int> u8(-127, 127);
77        for (int t = 0; t < NTOK; ++t)
78            for (int b = 0; b < (int) (ELL / 32); ++b) {
79                uint8_t* blk = xqt.data() + (size_t) t * kXq + 36 * b;
80                const uint16_t d16 = ggml_fp32_to_fp16(0.01f + 1e-4f * (t % 71)), s16 = 0;
81                std::memcpy(blk, &d16, 2); std::memcpy(blk + 2, &s16, 2);
82                for (int i = 0; i < 32; ++i) blk[4 + i] = (uint8_t) u8(rng);
83            }
84    }
85    uint8_t* dxqt = nullptr;
86    CK(cudaMalloc(&dxqt, xqt.size()));
87    CK(cudaMemcpy(dxqt, xqt.data(), xqt.size(), cudaMemcpyHostToDevice));
88    // the same data laid out one row an entry
89    std::vector<uint8_t> xqf((size_t) E * kXq);
90    for (int e = 0; e < E; ++e) std::memcpy(xqf.data() + (size_t) e * kXq, xqt.data() + (size_t) (e / 2) * kXq, kXq);
91    uint8_t* dxqf = nullptr;
92    CK(cudaMalloc(&dxqf, xqf.size()));
93    CK(cudaMemcpy(dxqf, xqf.data(), xqf.size(), cudaMemcpyHostToDevice));
94
95    int32_t *dgs = nullptr, *dcnt = nullptr, *dtok = nullptr, *ddst = nullptr, *dng = nullptr;
96    CK(cudaMalloc(&dgs, (size_t) (G + 1) * 4));
97    CK(cudaMalloc(&dcnt, (size_t) G * 4));
98    CK(cudaMalloc(&dtok, (size_t) E * 4));
99    CK(cudaMalloc(&ddst, (size_t) E * 4));
100    CK(cudaMalloc(&dng, 4));
101    std::vector<int32_t> gs((size_t) G + 1), cnt((size_t) G, EPG), tok((size_t) E), dst((size_t) E);
102    for (int g = 0; g <= G; ++g) gs[(size_t) g] = g * EPG;
103    for (int e = 0; e < E; ++e) { tok[(size_t) e] = e / 2; dst[(size_t) e] = e; }
104    // a permutation of the output rows
105    std::vector<int> pi((size_t) E);
106    for (int e = 0; e < E; ++e) pi[(size_t) e] = e;
107    std::mt19937 sh(11);
108    std::shuffle(pi.begin(), pi.end(), sh);
109    std::vector<int32_t> dstp((size_t) E);
110    for (int e = 0; e < E; ++e) dstp[(size_t) e] = pi[(size_t) e];
111    CK(cudaMemcpy(dgs, gs.data(), gs.size() * 4, cudaMemcpyHostToDevice));
112    CK(cudaMemcpy(dcnt, cnt.data(), cnt.size() * 4, cudaMemcpyHostToDevice));
113    CK(cudaMemcpy(dtok, tok.data(), tok.size() * 4, cudaMemcpyHostToDevice));
114    CK(cudaMemcpy(ddst, dstp.data(), dstp.size() * 4, cudaMemcpyHostToDevice));
115    CK(cudaMemcpy(dng, &G, 4, cudaMemcpyHostToDevice));
116
117    float* dgate = nullptr; float* dup = nullptr;
118    CK(cudaMalloc(&dgate, (size_t) E * FF * 4));
119    CK(cudaMalloc(&dup, (size_t) E * FF * 4));
120    uint8_t* dhq = nullptr; uint8_t* dhbuf = nullptr;
121    CK(cudaMalloc(&dhq, (size_t) E * kHq));
122    CK(cudaMalloc(&dhbuf, (size_t) E * kHq));
123    float* dy = nullptr; float* dy2 = nullptr;
124    CK(cudaMalloc(&dy, (size_t) E * ELL * 4));
125    CK(cudaMalloc(&dy2, (size_t) E * ELL * 4));
126
127    // ---- 1: no maps, the one-row-an-entry layout --------------------------------------------------------------
128    CK(cudaMemset(dy, 0, (size_t) E * ELL * 4));
129    strata::prefill::lat_q3k_fused(dblob, dgs, G, dxqf, dhbuf, dy, ELL, nullptr, dcnt, nullptr, nullptr);
130    CK(cudaDeviceSynchronize());
131    std::vector<float> y1((size_t) E * ELL);
132    CK(cudaMemcpy(y1.data(), dy, y1.size() * 4, cudaMemcpyDeviceToHost));
133
134    // ---- 2: ent_tok, the shared rows ---------------------------------------------------------------------------
135    CK(cudaMemset(dy2, 0, (size_t) E * ELL * 4));
136    strata::prefill::lat_q3k_fused(dblob, dgs, G, dxqt, dhbuf, dy2, ELL, nullptr, dcnt, dtok, nullptr);
137    CK(cudaDeviceSynchronize());
138    std::vector<float> y2((size_t) E * ELL);
139    CK(cudaMemcpy(y2.data(), dy2, y2.size() * 4, cudaMemcpyDeviceToHost));
140    std::printf("ent_tok : %lld of %zu floats differ from the one-row-an-entry layout <- must be 0\n",
141                n_diff(y1.data(), y2.data(), y1.size()), y1.size());
142    if (n_diff(y1.data(), y2.data(), y1.size()) != 0) { std::printf("FAIL ent_tok\n"); return 1; }
143
144    // ---- 3: ent_dst, a permutation of the output rows ----------------------------------------------------------
145    CK(cudaMemset(dy2, 0, (size_t) E * ELL * 4));
146    strata::prefill::lat_q3k_fused(dblob, dgs, G, dxqt, dhbuf, dy2, ELL, nullptr, dcnt, dtok, ddst);
147    CK(cudaDeviceSynchronize());
148    std::vector<float> y3((size_t) E * ELL);
149    CK(cudaMemcpy(y3.data(), dy2, y3.size() * 4, cudaMemcpyDeviceToHost));
150    long long bad = 0;
151    for (int e = 0; e < E; ++e)
152        for (int r = 0; r < ELL; ++r)
153            bad += (y3[(size_t) pi[(size_t) e] * ELL + r] != y1[(size_t) e * ELL + r]);
154    std::printf("ent_dst : %lld of %zu floats differ at the permuted rows               <- must be 0\n", bad, y1.size());
155    if (bad != 0) { std::printf("FAIL ent_dst\n"); return 1; }
156
157    // ---- 4: with both maps, fused == gu -> swiglu -> down ------------------------------------------------------
158    {
159        std::vector<float> yser((size_t) E * ELL);
160        CK(cudaMemset(dy, 0, (size_t) E * ELL * 4));
161        strata::prefill::lat_q3k_gu(dblob, dgs, G, dxqt, dgate, dup, FF, nullptr, dcnt, dtok);
162        strata::prefill::lat_q3k_swiglu_q8(dgate, dup, E, dhq, nullptr, nullptr);
163        strata::prefill::lat_q3k_down(dblob, dgs, G, dhq, dy, ELL, nullptr, dcnt, ddst);
164        CK(cudaDeviceSynchronize());
165        CK(cudaMemcpy(yser.data(), dy, yser.size() * 4, cudaMemcpyDeviceToHost));
166        const long long d = n_diff(yser.data(), y3.data(), yser.size());
167        std::printf("fused vs gu -> swiglu -> down (both maps): %lld of %zu floats differ  <- must be 0\n", d,
168                    yser.size());
169        if (d) { std::printf("FAIL map chain\n"); return 1; }
170    }
171
172    std::printf("PASS: M4 maps (ent_tok shared rows, ent_dst permutation, fused == serial)\n");
173    return 0;
174}