mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-04 15:28:40 +00:00
214 lines
13 KiB
C++
214 lines
13 KiB
C++
// tests/ling32/ling32_sy_bench.cpp - decode-step cost model of Ling-3.2-Air on an Intel GPU from the SYCL kernels at the REAL shapes (random weights: timing only).
|
|
// Runs every kernel of a decode step (N = 1) in a loop, prints time per call and effective GB/s, then the sum for 35 KDA + 7 MLA layers, 40 MoE layers (top-8 routed + shared) and the lm_head.
|
|
// This is the all-resident (Mode A) floor of the kernels, without tier traffic. usage: ling32_sy_bench [ctx=1024] [code of the routed experts = 5]
|
|
#include "ling32_kernels.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <chrono>
|
|
#include <functional>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <random>
|
|
#include <vector>
|
|
|
|
using namespace strata::ling32;
|
|
using namespace strata::ling32::gpu;
|
|
|
|
namespace {
|
|
inline size_t sk_superblock_bytes_(int c) { return c == 2 ? 70 : c == 3 ? 102 : c == 4 ? 134 : c == 5 ? 166 : c == 6 ? 198 : 262; }
|
|
template <class T> T* dalloc(size_t n) { void* p = nullptr; if (cudaMalloc(&p, n * sizeof(T)) != cudaSuccess) { std::fprintf(stderr, "alloc of %zu bytes failed\n", n * sizeof(T)); std::exit(1); } return static_cast<T*>(p); }
|
|
std::mt19937 g_rng(7);
|
|
template <class T> T* drand(size_t n, float lo, float hi) {
|
|
std::vector<T> h(n);
|
|
std::uniform_real_distribution<float> d(lo, hi);
|
|
for (auto& v : h) v = static_cast<T>(d(g_rng));
|
|
T* p = dalloc<T>(n);
|
|
cudaMemcpy(p, h.data(), n * sizeof(T), cudaMemcpyHostToDevice);
|
|
return p;
|
|
}
|
|
// valid-ish S_K rows: random bytes, but the fp16 scale of every superblock is a small normal number
|
|
uint8_t* dsk(int rows, int K, int code) {
|
|
const size_t sb = sk_superblock_bytes_(code), n = static_cast<size_t>(rows) * (K / 256) * sb;
|
|
std::vector<uint8_t> h(n);
|
|
for (auto& b : h) b = static_cast<uint8_t>(g_rng());
|
|
for (size_t o = 0; o < n; o += sb) { h[o] = 0x00; h[o + 1] = 0x2c; } // fp16 0x2c00 = 0.0625
|
|
uint8_t* p = dalloc<uint8_t>(n);
|
|
cudaMemcpy(p, h.data(), n, cudaMemcpyHostToDevice);
|
|
return p;
|
|
}
|
|
double time_ms(int reps, const std::function<void()>& fn) {
|
|
fn();
|
|
cudaDeviceSynchronize();
|
|
const auto t0 = std::chrono::steady_clock::now();
|
|
for (int i = 0; i < reps; ++i) fn();
|
|
cudaDeviceSynchronize();
|
|
return std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count() / reps;
|
|
}
|
|
|
|
double g_total_ms = 0;
|
|
void row(const char* what, double ms, double bytes, double mult) {
|
|
std::printf(" %-52s %8.3f ms %6.1f GB/s x%-3.0f = %7.2f ms\n", what, ms, bytes > 0 ? bytes / (ms * 1e6) : 0.0, mult, ms * mult);
|
|
g_total_ms += ms * mult;
|
|
}
|
|
|
|
// trunk GEMV as the engine runs it by default (float activations, N = 1): the superblock kernel and the packed copy (repack at load, aligned 8-byte loads); the total counts the packed one
|
|
double gemv_rows(const char* name, uint8_t* W, int rows, int K, const float* xin, float* yout, bool acc) {
|
|
char buf[96];
|
|
const double bytes = static_cast<double>(rows) * (K / 256) * 262;
|
|
std::snprintf(buf, sizeof buf, "gemv_sk S8 %s float act", name);
|
|
const double a = time_ms(50, [&] { strata::ling32::gpu::gemv_sk(W, 8, rows, K, xin, false, 1, yout, rows, acc, 0); });
|
|
std::printf(" %-52s %8.3f ms %6.1f GB/s\n", buf, a, bytes / (a * 1e6));
|
|
int8_t* q = dalloc<int8_t>(static_cast<size_t>(rows) * K);
|
|
float* ep = dalloc<float>(static_cast<size_t>(rows) * (K / 64));
|
|
strata::ling32::gpu::repack_s8(W, rows, K, q, ep, 0);
|
|
cudaDeviceSynchronize();
|
|
std::snprintf(buf, sizeof buf, "gemv_s8p1 packed %s", name);
|
|
const double t = time_ms(50, [&] { strata::ling32::gpu::gemv_s8p1(q, ep, rows, K, xin, yout, acc, 0); });
|
|
row(buf, t, static_cast<double>(rows) * K + static_cast<double>(rows) * (K / 64) * 4, 1);
|
|
cudaFree(q);
|
|
cudaFree(ep);
|
|
return t;
|
|
}
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv) {
|
|
const int ctx = argc > 1 ? std::atoi(argv[1]) : 1024;
|
|
const int ecode = argc > 2 ? std::atoi(argv[2]) : 5;
|
|
const int D = 2560, H = 32, hd = 128, C = H * hd, I = 768, E = 512, V = 157184, nope = 128, rope = 64, kvl = 512;
|
|
cudaDeviceProp prop;
|
|
cudaGetDeviceProperties(&prop, 0);
|
|
std::printf("device: %s, ctx %d, routed expert code S%d\n", prop.name, ctx, ecode);
|
|
const int TC = 8; // trunk code of the S8 trunk
|
|
auto sb = [](int c) { return static_cast<double>(sk_superblock_bytes_(c)); };
|
|
|
|
float* x = drand<float>(D, -1.f, 1.f);
|
|
float* x2 = drand<float>(D, -1.f, 1.f);
|
|
Q8Blk* xq = dalloc<Q8Blk>(D / 64);
|
|
float* y = dalloc<float>(static_cast<size_t>(std::max(V, 8 * C)));
|
|
float* gamma = drand<float>(D, 0.5f, 1.5f);
|
|
|
|
// ------------------------------------------------------------------ KDA layer
|
|
std::printf("[KDA layer, decode N = 1]\n");
|
|
g_total_ms = 0;
|
|
{
|
|
uint8_t* Wqkvfg = dsk(5 * C, D, TC);
|
|
uint8_t* Wo = dsk(D, C, TC);
|
|
float* proj = drand<float>(5 * C, -1.f, 1.f);
|
|
float* S = drand<float>(static_cast<size_t>(H) * hd * hd, -0.1f, 0.1f);
|
|
float* hist = drand<float>(3 * C * 3, -1.f, 1.f);
|
|
float *qw = drand<float>(C * 4, -0.5f, 0.5f), *kw = drand<float>(C * 4, -0.5f, 0.5f), *vw = drand<float>(C * 4, -0.5f, 0.5f);
|
|
float *beta = drand<float>(H, -1.f, 1.f), *alog = drand<float>(H, -1.f, 0.f), *dtb = drand<float>(C, -1.f, 1.f), *onorm = drand<float>(hd, 0.5f, 1.5f);
|
|
float* bproj = drand<float>(static_cast<size_t>(H) * D, -0.1f, 0.1f);
|
|
float* kout = dalloc<float>(C);
|
|
row("rmsnorm [2560]", time_ms(200, [&] { rmsnorm(x, gamma, 1e-6f, x2, 1, D, 0); }), 0, 2);
|
|
row("quant_q8 (FWHT off) [2560]", time_ms(200, [&] { quant_q8(x, xq, 1, D, false, 0); }), 0, 2);
|
|
gemv_rows("qkvfg [20480 x 2560]", Wqkvfg, 5 * C, D, x, y, false);
|
|
row("kda_step_fused (32 heads, hd 128)", time_ms(50, [&] { kda_step_fused(S, proj, 5 * C, qw, kw, vw, hist, beta, alog, dtb, onorm, -5.0f, 1e-6f, kout, 1, H, hd, 0, nullptr, nullptr, 0, bproj, x, D); }),
|
|
static_cast<double>(H) * hd * hd * 8, 1);
|
|
gemv_rows("o [2560 x 4096]", Wo, D, C, kout, y, true);
|
|
std::printf(" KDA layer total %.2f ms\n", g_total_ms);
|
|
}
|
|
const double kda_ms = g_total_ms;
|
|
|
|
// ------------------------------------------------------------------ MLA layer
|
|
std::printf("[MLA layer, decode N = 1, context %d]\n", ctx);
|
|
g_total_ms = 0;
|
|
{
|
|
uint8_t *Wq = dsk(H * (nope + rope), D, TC), *Wkva = dsk(kvl + rope, D, TC), *Wd = dsk(D, H * 128, TC);
|
|
float* q = drand<float>(static_cast<size_t>(H) * (nope + rope), -1.f, 1.f);
|
|
float* ckv = drand<float>(kvl + rope, -1.f, 1.f);
|
|
float* ln = drand<float>(kvl, 0.5f, 1.5f);
|
|
float* cache = drand<float>(static_cast<size_t>(ctx + 8) * (kvl + rope), -1.f, 1.f);
|
|
int* d_pos = dalloc<int>(1);
|
|
const int p0 = ctx - 1;
|
|
cudaMemcpy(d_pos, &p0, 4, cudaMemcpyHostToDevice);
|
|
uint16_t *wk = drand<uint16_t>(static_cast<size_t>(H) * nope * kvl, 0, 0), *wv = drand<uint16_t>(static_cast<size_t>(H) * 128 * kvl, 0, 0);
|
|
{ // bf16 values around +-0.05
|
|
std::vector<uint16_t> h(static_cast<size_t>(H) * nope * kvl, 0x3d4c);
|
|
cudaMemcpy(wk, h.data(), h.size() * 2, cudaMemcpyHostToDevice);
|
|
cudaMemcpy(wv, h.data(), h.size() * 2, cudaMemcpyHostToDevice);
|
|
}
|
|
float *qabs = dalloc<float>(static_cast<size_t>(H) * kvl), *cx = dalloc<float>(static_cast<size_t>(H) * kvl), *gate = drand<float>(H, -1.f, 1.f), *av = dalloc<float>(H * 128);
|
|
float* gp = drand<float>(static_cast<size_t>(H) * D, -0.1f, 0.1f);
|
|
float* av_big = drand<float>(H * 128, -1.f, 1.f);
|
|
gemv_rows("q [6144 x 2560]", Wq, H * (nope + rope), D, x, y, false);
|
|
gemv_rows("kv_a [576 x 2560]", Wkva, kvl + rope, D, x, y, false);
|
|
row("gemv_f32 g_proj [32 x 2560]", time_ms(100, [&] { gemv_f32(gp, H, D, x, 1, y, 0); }), 0, 1);
|
|
row("mla_prep", time_ms(100, [&] { mla_prep(q, ckv, kvl + rope, ln, cache, d_pos, 1, H, nope, rope, kvl, 6.0e6, 1e-6f, LatentFmt::F32, ctx + 8, 0, 0, false, 0); }), 0, 1);
|
|
row("mla_qabs", time_ms(50, [&] { mla_qabs(q, wk, qabs, 1, H, nope, rope, kvl, 0); }), static_cast<double>(H) * nope * kvl * 2, 1);
|
|
row("mla_attn (original kernel)", time_ms(20, [&] { mla_attn(qabs, q, cache, d_pos, cx, 1, H, nope, rope, kvl, 0.07f, true, 0); }), static_cast<double>(ctx) * (kvl + rope) * 4, 1);
|
|
row("mla_out", time_ms(50, [&] { mla_out(cx, wv, gate, av, 1, H, 128, kvl, 0); }), static_cast<double>(H) * 128 * kvl * 2, 1);
|
|
gemv_rows("dense [2560 x 4096]", Wd, D, H * 128, av_big, y, true);
|
|
std::printf(" MLA layer total %.2f ms\n", g_total_ms);
|
|
}
|
|
const double mla_ms = g_total_ms;
|
|
|
|
// ------------------------------------------------------------------ MoE layer
|
|
std::printf("[MoE layer, decode N = 1: router + top-8 routed experts S%d + shared expert]\n", ecode);
|
|
g_total_ms = 0;
|
|
{
|
|
const int nsb_gu = D / 256, nsb_dn = I / 256;
|
|
const size_t gu_bytes = static_cast<size_t>(2 * I) * nsb_gu * sk_superblock_bytes_(ecode), dn_bytes = static_cast<size_t>(D) * nsb_dn * sk_superblock_bytes_(ecode);
|
|
const int Ep = 64; // 64 distinct slabs are enough to defeat the cache
|
|
const size_t stride = ((2 * I * 2 + gu_bytes + dn_bytes) + 16383) / 16384 * 16384;
|
|
std::vector<uint8_t> hs(static_cast<size_t>(Ep) * stride);
|
|
for (auto& b : hs) b = static_cast<uint8_t>(g_rng());
|
|
std::vector<ExpertDesc> desc(Ep);
|
|
for (int e = 0; e < Ep; ++e) {
|
|
const size_t off = static_cast<size_t>(e) * stride;
|
|
desc[e] = ExpertDesc{off, ecode, ecode, static_cast<int>(2 * I * 2 + gu_bytes), static_cast<int>(2 * I * 2 + gu_bytes + dn_bytes)};
|
|
for (int i = 0; i < I; ++i) { hs[off + 2 * i] = 0x00; hs[off + 2 * i + 1] = 0x3c; } // S_xd = 1.0
|
|
for (size_t o = 0; o < gu_bytes; o += sk_superblock_bytes_(ecode)) { hs[off + 2 * I * 2 + o] = 0x00; hs[off + 2 * I * 2 + o + 1] = 0x2c; }
|
|
for (size_t o = 0; o < dn_bytes; o += sk_superblock_bytes_(ecode)) { hs[off + 2 * I * 2 + gu_bytes + o] = 0x00; hs[off + 2 * I * 2 + gu_bytes + o + 1] = 0x2c; }
|
|
}
|
|
uint8_t* sec4 = dalloc<uint8_t>(hs.size());
|
|
cudaMemcpy(sec4, hs.data(), hs.size(), cudaMemcpyHostToDevice);
|
|
ExpertDesc* ddesc = dalloc<ExpertDesc>(Ep);
|
|
cudaMemcpy(ddesc, desc.data(), Ep * sizeof(ExpertDesc), cudaMemcpyHostToDevice);
|
|
std::vector<int> hidx(8);
|
|
int* didx = dalloc<int>(8);
|
|
float *gu = dalloc<float>(8 * 2 * I), *act = dalloc<float>(8 * I), *ye = dalloc<float>(8 * D), *rw = drand<float>(8, 0.1f, 0.5f);
|
|
float* gate_w = drand<float>(static_cast<size_t>(E) * D, -0.1f, 0.1f);
|
|
float *lg = dalloc<float>(E), *sel = drand<float>(E, -0.1f, 0.1f), *ow = dalloc<float>(8);
|
|
int* oidx = dalloc<int>(8);
|
|
uint8_t *Wg = dsk(I, D, TC), *Wu = dsk(I, D, TC), *Wd = dsk(D, I, TC);
|
|
float* sh = dalloc<float>(I);
|
|
int rot = 0;
|
|
auto pick = [&] { // 8 different slabs each call (a rotating window) so that every call reads cold-ish weights
|
|
for (int k = 0; k < 8; ++k) hidx[k] = (rot + 8 * k) % Ep;
|
|
rot = (rot + 1) % 8;
|
|
cudaMemcpy(didx, hidx.data(), 32, cudaMemcpyHostToDevice);
|
|
};
|
|
pick();
|
|
row("rmsnorm [2560]", time_ms(200, [&] { rmsnorm(x, gamma, 1e-6f, x2, 1, D, 0); }), 0, 1);
|
|
row("gemv_f32 router [512 x 2560]", time_ms(100, [&] { gemv_f32(gate_w, E, D, x, 1, lg, 0); }), static_cast<double>(E) * D * 4, 1);
|
|
row("alflb_route (top-8 of 512)", time_ms(100, [&] { alflb_route(lg, nullptr, sel, E, 8, 4, 8, 2.5f, true, oidx, ow, 1, 0); }), 0, 1);
|
|
row("fwht128 [2560] (expert input)", time_ms(200, [&] { fwht128(x, x2, 1, D, 0); }), 0, 1);
|
|
row("moe_gu 8 experts (float act)", time_ms(50, [&] { moe_gu(sec4, ddesc, 0, didx, 1, 8, Ep, D, I, x2, false, gu, 0, nullptr); }), 8.0 * gu_bytes, 1);
|
|
row("moe_act 8 pairs (swiglu + FWHT)", time_ms(100, [&] { moe_act(sec4, ddesc, 0, didx, gu, 1, 8, I, 7.0f, true, act, false, 0, nullptr); }), 0, 1);
|
|
row("moe_dn 8 experts (float act)", time_ms(50, [&] { moe_dn(sec4, ddesc, 0, didx, 1, 8, Ep, D, I, act, false, ye, 0, nullptr); }), 8.0 * dn_bytes, 1);
|
|
row("moe_combine (+ shared)", time_ms(100, [&] { moe_combine(x, ye, rw, 1, 8, D, 0, nullptr); }), 0, 1);
|
|
row("gemv2_swiglu_s8 shared gate+up [768 x 2560]", time_ms(100, [&] { gemv2_swiglu_s8(Wg, Wu, I, D, x2, 1, 7.0f, true, sh, I, 0); }), 2.0 * I * nsb_gu * sb(TC), 1);
|
|
row("gemv_sk S8 shared down [2560 x 768]", time_ms(100, [&] { gemv_sk(Wd, TC, D, I, sh, false, 1, y, D, false, 0); }), static_cast<double>(D) * nsb_dn * sb(TC), 1);
|
|
std::printf(" MoE layer total %.2f ms (experts read %.1f MB)\n", g_total_ms, 8.0 * (gu_bytes + dn_bytes) / 1e6);
|
|
}
|
|
const double moe_ms = g_total_ms;
|
|
|
|
// ------------------------------------------------------------------ head
|
|
g_total_ms = 0;
|
|
std::printf("[lm_head]\n");
|
|
{
|
|
uint8_t* Wh = dsk(V, D, TC);
|
|
Q8Blk* hq = dalloc<Q8Blk>(D / 64);
|
|
float* logits = dalloc<float>(V);
|
|
gemv_rows("lm_head [157184 x 2560]", Wh, V, D, x, logits, false);
|
|
(void)hq;
|
|
}
|
|
const double head_ms = g_total_ms;
|
|
|
|
// layer composition: 42 layers = 35 KDA + 7 MLA; 2 dense + 40 MoE (dense MLP not measured: counted like the shared expert x4)
|
|
const double step = 35 * kda_ms + 7 * mla_ms + 40 * moe_ms + head_ms;
|
|
std::printf("[decode step estimate, everything resident, kernels only]\n 35 x KDA %.2f + 7 x MLA %.2f + 40 x MoE %.2f + head %.2f = %.1f ms per token = %.1f tok/s\n", kda_ms, mla_ms, moe_ms, head_ms, step, 1000.0 / step);
|
|
return 0;
|
|
}
|