mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-05 07:48:45 +00:00
Pure renames, no behaviour change: LING32_SY_KDA_SEQ -> STRATA_L32_KDA_SEQ (0 old scan, 4 / 8 split scan, default 8), LING32_SY_MLA_EXP -> STRATA_L32_MLA_EXP_MODE, LING32_SY_MLA_ORIG -> STRATA_L32_MLA_FD_ORIG, LING32_SY_MLA_FD_NS / _PVU / _RB -> STRATA_L32_MLA_FD_NS / _PVU / _RB, LING32_SY_MLA_EXP_ABS (test) -> STRATA_L32_MLA_EXP_ABS. Register entries (rollback for the switches, research for the tuning knobs; STRATA_SY_PROF_TOP of the profiling hook too), docs/SETTINGS.md regenerated, tools/settings_register.py --check clean and test_settings_register OK on this commit (based on the scratch merge8c8ed5cd0bof intf9f533c65f+ MLAefff44d494+ KDAc7f2967a16).
168 lines
7.7 KiB
C++
168 lines
7.7 KiB
C++
// tests/ling32/ling32_sy_mla_exp_test.cpp - the expanded-form MLA prefill on SYCL (sycl/ling32_sy_k_mla_exp.cpp) against the engine's absorbed pipeline (mla_qabs + mla_attn original kernel + mla_out) on random data
|
|
// of the real geometry (H heads, nope 128, rope 64, vhead 128, kv_lora 512, fp32 latent cache), for several (tokens, position) shapes and slab sizes. Port of ling32_mla_exp_test.cu.
|
|
// ling32_sy_mla_exp_test [full] [bench] "full": also the big cases (H = 32, 300 tokens at position 3000); "bench": times a 2048-token chunk at 16384 context against the absorbed path
|
|
// On a device without XMX the test needs STRATA_L32_MLA_EXP_MODE=1 (plain loops, slow); it skips otherwise.
|
|
#include "ling32_kernels.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <chrono>
|
|
#include <cmath>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <random>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
using namespace strata::ling32::gpu;
|
|
|
|
namespace {
|
|
int g_fail = 0;
|
|
void check(bool ok, const char* what, double v) {
|
|
std::printf(" %-100s %s (%.3g)\n", what, ok ? "ok" : "FAIL", v);
|
|
if (!ok) ++g_fail;
|
|
}
|
|
uint16_t f2bf(float f) { uint32_t u; std::memcpy(&u, &f, 4); u += 0x7fffu + ((u >> 16) & 1u); return static_cast<uint16_t>(u >> 16); }
|
|
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);
|
|
}
|
|
template <class T> T* up(const std::vector<T>& h) {
|
|
T* p = dalloc<T>(h.size());
|
|
cudaMemcpy(p, h.data(), h.size() * sizeof(T), cudaMemcpyHostToDevice);
|
|
return p;
|
|
}
|
|
template <class F> double time_ms(F f, int reps) {
|
|
f();
|
|
cudaDeviceSynchronize();
|
|
const auto t0 = std::chrono::steady_clock::now();
|
|
for (int i = 0; i < reps; ++i) f();
|
|
cudaDeviceSynchronize();
|
|
return std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count() / reps;
|
|
}
|
|
|
|
struct Data {
|
|
int H, kv_cap;
|
|
std::vector<float> q, cache, gate;
|
|
std::vector<uint16_t> wk, wv;
|
|
float *dq, *dcache, *dgate;
|
|
uint16_t *dwk, *dwv, *dwk16, *dwv16;
|
|
};
|
|
|
|
Data make_data(int H, int N_cap, int kv_cap, unsigned seed) {
|
|
Data d;
|
|
d.H = H;
|
|
d.kv_cap = kv_cap;
|
|
const int NOPE = 128, ROPE = 64, QW = NOPE + ROPE, R = 512, W = R + ROPE, V = 128;
|
|
std::mt19937 rng(seed);
|
|
std::normal_distribution<float> nd(0.f, 1.f);
|
|
d.q.resize(static_cast<size_t>(N_cap) * H * QW);
|
|
d.cache.resize(static_cast<size_t>(kv_cap) * W);
|
|
d.gate.resize(static_cast<size_t>(N_cap) * H);
|
|
d.wk.resize(static_cast<size_t>(H) * NOPE * R);
|
|
d.wv.resize(static_cast<size_t>(H) * V * R);
|
|
for (auto& v : d.q) v = nd(rng) * 0.5f;
|
|
for (auto& v : d.cache) v = nd(rng);
|
|
for (auto& v : d.gate) v = nd(rng);
|
|
for (auto& v : d.wk) v = f2bf(nd(rng) * 0.06f);
|
|
for (auto& v : d.wv) v = f2bf(nd(rng) * 0.06f);
|
|
d.dq = up(d.q); d.dcache = up(d.cache); d.dgate = up(d.gate);
|
|
d.dwk = up(d.wk); d.dwv = up(d.wv);
|
|
d.dwk16 = dalloc<uint16_t>(d.wk.size()); d.dwv16 = dalloc<uint16_t>(d.wv.size());
|
|
mla_exp_convert_weights(d.dwk, d.wk.size(), d.dwk16, 0);
|
|
mla_exp_convert_weights(d.dwv, d.wv.size(), d.dwv16, 0);
|
|
cudaDeviceSynchronize();
|
|
return d;
|
|
}
|
|
|
|
void run_case(const Data& d, int N, int pos0, int slab) {
|
|
const int H = d.H, NOPE = 128, ROPE = 64, R = 512, W = R + ROPE, V = 128;
|
|
const float scale = 1.0f / std::sqrt(192.0f);
|
|
float* qabs = dalloc<float>(static_cast<size_t>(N) * H * R);
|
|
float* ctx = dalloc<float>(static_cast<size_t>(N) * H * R);
|
|
float* ref = dalloc<float>(static_cast<size_t>(N) * H * V);
|
|
float* out = dalloc<float>(static_cast<size_t>(N) * H * V);
|
|
int* dpos = dalloc<int>(1);
|
|
cudaMemcpy(dpos, &pos0, 4, cudaMemcpyHostToDevice);
|
|
mla_qabs(d.dq, d.dwk, qabs, N, H, NOPE, ROPE, R, 0);
|
|
mla_attn(qabs, d.dq, d.dcache, dpos, ctx, N, H, NOPE, ROPE, R, scale, true, 0);
|
|
mla_out(ctx, d.dwv, d.dgate, ref, N, H, V, R, 0);
|
|
const size_t eb = mla_exp_workspace_bytes(N, H, slab);
|
|
void* ews = dalloc<char>(eb);
|
|
mla_exp_prefill(d.dq, d.dcache, W, d.dwk16, d.dwv16, d.dgate, out, N, H, pos0, scale, ews, eb, slab, 0);
|
|
cudaDeviceSynchronize();
|
|
std::vector<float> r(static_cast<size_t>(N) * H * V), o(r.size());
|
|
cudaMemcpy(r.data(), ref, r.size() * 4, cudaMemcpyDeviceToHost);
|
|
cudaMemcpy(o.data(), out, r.size() * 4, cudaMemcpyDeviceToHost);
|
|
double md = 0, mr = 0;
|
|
bool finite = true;
|
|
for (size_t i = 0; i < r.size(); ++i) {
|
|
if (!std::isfinite(o[i])) finite = false;
|
|
md = std::max(md, static_cast<double>(std::fabs(o[i] - r[i])));
|
|
mr = std::max(mr, static_cast<double>(std::fabs(r[i])));
|
|
}
|
|
char buf[160];
|
|
std::snprintf(buf, sizeof(buf), "H=%d N=%d pos0=%d slab=%d: expanded vs absorbed fp32 scalar (max |ref| %.3f)", H, N, pos0, slab, mr);
|
|
check(finite && md < 2e-5 * std::max(1.0, mr), buf, md);
|
|
cudaFree(qabs); cudaFree(ctx); cudaFree(ref); cudaFree(out); cudaFree(dpos); cudaFree(ews);
|
|
}
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv) {
|
|
cudaDeviceProp prop;
|
|
cudaGetDeviceProperties(&prop, 0);
|
|
std::printf("device: %s\n", prop.name);
|
|
bool full = false, bench = false;
|
|
for (int i = 1; i < argc; ++i) {
|
|
if (std::string(argv[i]) == "full") full = true;
|
|
if (std::string(argv[i]) == "bench") bench = true;
|
|
}
|
|
if (!mla_exp_ok(128, 64, 128, 512)) {
|
|
std::printf("no XMX on this device (STRATA_L32_MLA_EXP_MODE=1 forces the plain-loop mode): skipped\n");
|
|
return 0;
|
|
}
|
|
{
|
|
Data d = make_data(4, 96, 1500, 11);
|
|
run_case(d, 40, 0, 64); // first chunk
|
|
run_case(d, 70, 0, 256);
|
|
run_case(d, 96, 1000, 128); // later chunk: keys of earlier chunks in several slabs
|
|
run_case(d, 33, 1400, 512);
|
|
run_case(d, 17, 63, 64); // ragged edges
|
|
}
|
|
{
|
|
Data d = make_data(32, 300, full ? 3600 : 800, 5); // the real head count
|
|
run_case(d, 129, 0, 2048);
|
|
run_case(d, 100, 700, 128);
|
|
if (full) run_case(d, 300, 3000, 1024);
|
|
}
|
|
if (bench) {
|
|
const int H = 32, N = 2048, pos0 = 14336, W = 576, R = 512, NOPE = 128, ROPE = 64, V = 128;
|
|
Data d = make_data(H, N, pos0 + N, 3);
|
|
const float scale = 1.0f / std::sqrt(192.0f);
|
|
float* out = dalloc<float>(static_cast<size_t>(N) * H * V);
|
|
for (int slab : {1024, 2048, 4096}) {
|
|
const size_t eb = mla_exp_workspace_bytes(N, H, slab);
|
|
void* ews = dalloc<char>(eb);
|
|
const double t = time_ms([&] { mla_exp_prefill(d.dq, d.dcache, W, d.dwk16, d.dwv16, d.dgate, out, N, H, pos0, scale, ews, eb, slab, 0); }, 2);
|
|
std::printf(" expanded, N=%d context=%d slab %d keys (workspace %.0f MB): %.2f ms\n", N, pos0 + N, slab, eb / 1048576.0, t);
|
|
cudaFree(ews);
|
|
}
|
|
if (std::getenv("STRATA_L32_MLA_EXP_ABS")) { // the absorbed path (slow: minutes at this size)
|
|
float* qabs = dalloc<float>(static_cast<size_t>(N) * H * R);
|
|
float* ctx = dalloc<float>(static_cast<size_t>(N) * H * R);
|
|
float* ref = dalloc<float>(static_cast<size_t>(N) * H * V);
|
|
int* dpos = dalloc<int>(1);
|
|
cudaMemcpy(dpos, &pos0, 4, cudaMemcpyHostToDevice);
|
|
const double t = time_ms([&] {
|
|
mla_qabs(d.dq, d.dwk, qabs, N, H, NOPE, ROPE, R, 0);
|
|
mla_attn(qabs, d.dq, d.dcache, dpos, ctx, N, H, NOPE, ROPE, R, scale, true, 0);
|
|
mla_out(ctx, d.dwv, d.dgate, ref, N, H, V, R, 0);
|
|
}, 1);
|
|
std::printf(" absorbed original path (qabs + attention + out): %.2f ms\n", t);
|
|
}
|
|
}
|
|
std::printf(g_fail ? "FAIL (%d failures)\n" : "PASS (0 failures)\n", g_fail);
|
|
return g_fail ? 1 : 0;
|
|
}
|