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.3 KiB206 linesraw
1// src/prefill/lat_mmq_bench.cpp - the latent A stage at the engine's own shapes and group sizes: gate/up Q3_K
2// [1280 x 1280], down IQ4_NL [1280 x 640], ELL = 1280, n experts a launch, R rows each.  Every stage timed alone.
3//
4//     lat_mmq_bench [R ...]        default R = 95 (a 4K chunk: 42450 entries / 512 experts / 15.6 a group)
5//
6// The gather is the engine's per-expert `gather_native` into the MMQ group buffers, with a plain D2D memcpy of the
7// same bytes as the reference: the question is whether that copy or the products are the A stage's cost.
8#include "strata/prefill/moe_mmq.hpp"
9
10#include "ggml.h"
11
12#include <cuda_runtime.h>
13
14#include <algorithm>
15#include <cstdio>
16#include <cstdlib>
17#include <cstring>
18#include <random>
19#include <vector>
20
21namespace mmq = strata::prefill::mmq;
22
23namespace {
24
25void ck(cudaError_t e, const char* what) {
26    if (e != cudaSuccess) { std::fprintf(stderr, "%s: %s\n", what, cudaGetErrorString(e)); std::exit(1); }
27}
28
29std::vector<uint8_t> weights(ggml_type t, int64_t rows, int64_t cols, int experts, std::mt19937& rng) {
30    const size_t per = mmq::matrix_bytes(t, rows, cols), bs = ggml_type_size(t);
31    std::vector<uint8_t> w((size_t) experts * per);
32    std::uniform_int_distribution<int> byte(0, 255);
33    for (auto& b : w) b = (uint8_t) byte(rng);
34    for (size_t i = 0; i < w.size() / bs; ++i) {
35        const uint16_t d = (uint16_t) (0x2800 + byte(rng) % 0x800);
36        std::memcpy(w.data() + i * bs + (t == GGML_TYPE_Q3_K ? bs - 2 : 0), &d, 2);
37    }
38    return w;
39}
40
41cudaStream_t g_s = nullptr;   // the engine's compute stream is not the default one
42
43float med(void (*fire)(void*), void* arg) {
44    cudaEvent_t a, b;
45    cudaEventCreate(&a); cudaEventCreate(&b);
46    std::vector<float> ts;
47    for (int i = 0; i < 26; ++i) {
48        cudaEventRecord(a, g_s);
49        fire(arg);
50        cudaEventRecord(b, g_s);
51        ck(cudaEventSynchronize(b), "sync");
52        float ms = 0; cudaEventElapsedTime(&ms, a, b);
53        if (i >= 3) ts.push_back(ms);
54    }
55    cudaEventDestroy(a); cudaEventDestroy(b);
56    std::sort(ts.begin(), ts.end());
57    return ts[ts.size() / 2];
58}
59
60const int64_t ELL = 1280, FF = 640;
61const size_t GU_B = 1280 * 550;          // Q3_K [1280, 1280]: 5 blocks of 256 x 110 B
62const size_t DN_B = 1280 * 20 * 18;      // IQ4_NL [1280, 640]: 20 blocks of 32 x 18 B
63
64struct Ctx {
65    int n = 16;
66    int64_t R = 95;
67    std::vector<uint8_t> wgu, wdn;
68    std::vector<float> xl;
69    std::vector<int32_t> bounds;
70    void *dgu = nullptr, *ddn = nullptr, *dxl = nullptr, *dxq = nullptr, *dgo = nullptr, *dho = nullptr;
71    void *dhq = nullptr, *ddo = nullptr, *db = nullptr, *dids = nullptr, *ggu = nullptr, *gdn = nullptr;
72    mmq::Context ctx;
73    mmq::Product gu, dn;
74    mmq::Product g2s, d2s;   // the engine's geometry, timed
75};
76
77void fire_gather(void* a) {   // one launch per expert, as the engine does it
78    Ctx* c = (Ctx*) a;
79    for (int q = 0; q < c->n; ++q)
80        mmq::gather_native(c->ddn, (const uint8_t*) c->ddn + DN_B + GU_B / 2, GU_B / 2,
81                           (const uint8_t*) c->ddn + DN_B + GU_B, DN_B,
82                           (uint8_t*) c->ggu + (size_t) q * GU_B, (uint8_t*) c->gdn + (size_t) q * DN_B, g_s);
83}
84void fire_gather_group(void* a) {   // one launch for the whole group
85    Ctx* c = (Ctx*) a;
86    mmq::GatherGroup g;
87    g.first = 0;
88    g.n = std::min(c->n, mmq::kGatherGroupMax);
89    for (int q = 0; q < g.n; ++q) g.blob[q] = (const uint8_t*) c->ddn;
90    mmq::gather_native_group(g, DN_B + GU_B / 2, GU_B / 2, DN_B + GU_B, DN_B, c->ggu, GU_B, c->gdn, DN_B, g_s);
91}
92void fire_memcpy(void* a) {
93    Ctx* c = (Ctx*) a;
94    for (int q = 0; q < c->n; ++q) {
95        ck(cudaMemcpyAsync((uint8_t*) c->ggu + (size_t) q * GU_B, c->ddn, GU_B, cudaMemcpyDeviceToDevice, nullptr), "cp");
96        ck(cudaMemcpyAsync((uint8_t*) c->gdn + (size_t) q * DN_B, c->ddn, DN_B, cudaMemcpyDeviceToDevice, nullptr), "cp2");
97    }
98}
99void fire_quant(void* a) {
100    Ctx* c = (Ctx*) a;
101    mmq::quantize(c->xl.data(), nullptr, c->dxq, GGML_TYPE_Q3_K, ELL, ELL, (int64_t) c->n * c->R, nullptr);
102}
103void fire_gu(void* a) { Ctx* c = (Ctx*) a; c->ctx.run(c->gu, g_s); }
104void fire_swiglu(void* a) {
105    Ctx* c = (Ctx*) a;
106    mmq::swiglu((const float*) c->dgo, (float*) c->dho, (int64_t) c->n * c->R, 640, false, nullptr);
107}
108void fire_quanth(void* a) {
109    Ctx* c = (Ctx*) a;
110    mmq::quantize((const float*) c->dho, nullptr, c->dhq, GGML_TYPE_IQ4_NL, 640, 640, (int64_t) c->n * c->R, nullptr);
111}
112void fire_dn(void* a) { Ctx* c = (Ctx*) a; c->ctx.run(c->dn, g_s); }
113
114}  // namespace
115
116int main(int argc, char** argv) {
117    std::vector<int64_t> Rs;
118    for (int i = 1; i < argc; ++i) Rs.push_back(std::atoll(argv[i]));
119    if (Rs.empty()) Rs = {95};
120    std::printf("lat_mmq_bench: gu Q3_K [1280 x 1280] (%.0f KB), dn IQ4_NL [1280 x 640] (%.0f KB), medians of 23\n",
121                GU_B / 1024.0, DN_B / 1024.0);
122    ck(cudaStreamCreate(&g_s), "stream");
123    std::mt19937 rng(7);
124    for (int64_t R : Rs)
125        for (int n : {1, 4, 16}) {
126            Ctx c;
127            c.n = n; c.R = R;
128            const int64_t T = (int64_t) n * R;
129            c.wgu = weights(GGML_TYPE_Q3_K, 1280, ELL, n, rng);
130            c.wdn = weights(GGML_TYPE_IQ4_NL, ELL, FF, n, rng);
131            std::normal_distribution<float> nd(0.f, 1.f);
132            c.xl.resize((size_t) T * ELL);
133            for (auto& v : c.xl) v = nd(rng);
134            c.bounds.resize((size_t) n + 1);
135            for (int e = 0; e <= n; ++e) c.bounds[(size_t) e] = (int32_t) (e * R);
136            auto alloc = [](void** p, size_t bytes) { ck(cudaMalloc(p, bytes ? bytes : 16), "alloc"); };
137            alloc(&c.dgu, c.wgu.size());
138            alloc(&c.ddn, c.wdn.size() + GU_B + DN_B);   // room for the "expert blob" the gather reads
139            alloc(&c.ggu, (size_t) n * GU_B);
140            alloc(&c.gdn, (size_t) n * DN_B);
141            alloc(&c.dxl, c.xl.size() * 4);
142            alloc(&c.dxq, mmq::q8_bytes(T, ELL));
143            alloc(&c.dgo, (size_t) T * 1280 * 4);
144            alloc(&c.dho, (size_t) T * 640 * 4);
145            alloc(&c.dhq, mmq::q8_bytes(T, 640));
146            alloc(&c.ddo, (size_t) T * ELL * 4);
147            alloc(&c.db, ((size_t) n + 1) * 4);
148            alloc(&c.dids, (size_t) (T + 1024) * 4);
149            ck(cudaMemcpy(c.dgu, c.wgu.data(), c.wgu.size(), cudaMemcpyHostToDevice), "w");
150            ck(cudaMemcpy(c.ddn, c.wdn.data(), c.wdn.size(), cudaMemcpyHostToDevice), "w2");
151            ck(cudaMemcpy(c.dxl, c.xl.data(), c.xl.size() * 4, cudaMemcpyHostToDevice), "x");
152            ck(cudaMemcpy(c.db, c.bounds.data(), ((size_t) n + 1) * 4, cudaMemcpyHostToDevice), "b");
153            mmq::iota((int32_t*) c.dids, T + 1024, nullptr);
154            mmq::quantize(c.xl.data(), nullptr, c.dxq, GGML_TYPE_Q3_K, ELL, ELL, T, nullptr);
155            mmq::swiglu((const float*) c.dgo, (float*) c.dho, T, 640, false, nullptr);
156            mmq::quantize((const float*) c.dho, nullptr, c.dhq, GGML_TYPE_IQ4_NL, 640, 640, T, nullptr);
157            c.gu.w = c.dgu; c.gu.type = GGML_TYPE_Q3_K; c.gu.w_rows = 1280; c.gu.w_cols = ELL;
158            c.gu.expert_bytes = GU_B; c.gu.n = n; c.gu.xq = c.dxq; c.gu.bounds = (const int32_t*) c.db;
159            c.gu.ids = (const int32_t*) c.dids; c.gu.total_rows = T; c.gu.max_rows = R;
160            c.gu.dst = (float*) c.dgo; c.gu.ld_dst = 1280;
161            c.dn.w = c.ddn; c.dn.type = GGML_TYPE_IQ4_NL; c.dn.w_rows = ELL; c.dn.w_cols = 640;
162            c.dn.expert_bytes = DN_B; c.dn.n = n; c.dn.xq = c.dhq; c.dn.bounds = (const int32_t*) c.db;
163            c.dn.ids = (const int32_t*) c.dids; c.dn.total_rows = T; c.dn.max_rows = R;
164            c.dn.dst = (float*) c.ddo; c.dn.ld_dst = ELL;
165            if (n == 16) {   // the engine's own layer-0 group: uneven bounds, maxr 370, total_rows 1278
166                const int32_t ub[17] = {0, 266, 267, 637, 827, 828, 1019, 1206, 1207, 1212, 1213, 1216, 1218, 1219,
167                                        1263, 1277, 1278};
168                ck(cudaMemcpy(c.db, ub, 17 * 4, cudaMemcpyHostToDevice), "ub");
169                const int64_t UN = 1278;
170                ck(cudaStreamSynchronize(g_s), "pre");
171                mmq::Product g2 = c.gu;
172                g2.total_rows = UN; g2.max_rows = 370;
173                c.ctx.run(g2, g_s);
174                std::printf("  UNEVEN gu n=16 nr=%lld maxr=370: %s\n", (long long) UN,
175                            cudaGetErrorString(cudaStreamSynchronize(g_s)));
176                mmq::Product d2 = c.dn;
177                d2.total_rows = UN; d2.max_rows = 370;
178                c.ctx.run(d2, g_s);
179                c.g2s = g2; c.d2s = d2;
180                {
181                    auto tg = [](void* q) { Ctx* x = (Ctx*) q; x->ctx.run(x->g2s, g_s); };
182                    auto td = [](void* q) { Ctx* x = (Ctx*) q; x->ctx.run(x->d2s, g_s); };
183                    const float tg2 = med(tg, &c), td2 = med(td, &c);
184                    std::printf("  UNEVEN TIMED n=16 nr=1278 maxr=370: gu %7.1f us | dn %7.1f us  (dn/gu %.1fx)\n",
185                                tg2 * 1000, td2 * 1000, tg2 > 0 ? td2 / tg2 : 0.0);
186                }
187                std::printf("  UNEVEN dn n=16 nr=%lld maxr=370: %s\n", (long long) UN,
188                            cudaGetErrorString(cudaStreamSynchronize(g_s)));
189                ck(cudaMemcpy(c.db, c.bounds.data(), ((size_t) n + 1) * 4, cudaMemcpyHostToDevice), "ub2");
190            }
191            const double mb = (double) n * (GU_B + DN_B) / 1e6;
192            const float tg = med(fire_gather, &c), tgg = med(fire_gather_group, &c), tc = med(fire_memcpy, &c);
193            const float tq = med(fire_quant, &c), tgu = med(fire_gu, &c), ts = med(fire_swiglu, &c);
194            const float th = med(fire_quanth, &c), td = med(fire_dn, &c);
195            std::printf("  R %3lld n %2d rows %5lld | gather %8.1f us (%5.0f GB/s per-exp) | gather_group %7.1f us"
196                        " (%5.0f GB/s) | memcpy %7.1f us (%5.0f GB/s) [%.1f MB]\n",
197                        (long long) R, n, (long long) T, tg * 1000, mb / (tg * 1e3) / 1e3, tgg * 1000,
198                        mb / (tgg * 1e3) / 1e3, tc * 1000, mb / (tc * 1e3) / 1e3, mb);
199            std::printf("                    | quantize %6.1f | gu %7.1f | swiglu %5.1f | quantH %5.1f | dn %7.1f us"
200                        "  (sum %.1f us)\n", tq * 1000, tgu * 1000, ts * 1000, th * 1000, td * 1000,
201                        (tq + tgu + ts + th + td) * 1000);
202            for (void* p : {c.dgu, c.ddn, c.ggu, c.gdn, c.dxl, c.dxq, c.dgo, c.dho, c.dhq, c.ddo, c.db, c.dids})
203                cudaFree(p);
204        }
205    return 0;
206}