Files
Strata/engine/tests/ling32/ling32_sy_mla_fd_test.cpp
Niko1221 d46d0af01c ling32 sycl: MLA flash decoding tuning (one row per QK batch, runtime split count up to 128, PV row prefetch switch), test up to ctx 131072
B70, mla_attn alone: ctx 8192 3.57 ms -> 0.148 ms, ctx 1024 0.448 -> 0.031 ms, ctx 257 0.144 -> 0.021 ms; max abs diff to the original kernel 4e-8. Env switches for tuning and A/B: LING32_SY_MLA_FD_RB (1|2), _PVU (1|2|4), _NS (slices, default 48, max 128); LING32_SY_MLA_ORIG=1 forces the original kernel.
2026-12-03 00:18:30 +01:00

94 lines
5.6 KiB
C++

// tests/ling32/ling32_sy_mla_fd_test.cpp - the flash-decoding MLA attention (sycl/ling32_sy_k_mla_fd.cpp) against the original per-head kernel (mla_attn_orig, ling32_sy_k_mla.cpp).
// Real geometry (32 heads, nope 128, rope 64, kv_lora 512), fp32 latent cache. Contexts 1, 2, 31, 32, 33, 64, 257, 1024, 8192, 32768, 131072 with N = 1, and spans of N = 3 and 16 tokens (each token sees its own prefix);
// three score regimes (flat, normal, peaked) so that the softmax merge across slices is exercised with very different maxima.
// The summation order changes, so the result is not bit-exact: it must stay within 1e-5 absolute and relative (relative against max(|ref|, 0.1)). Prints the timing of both kernels as well.
// usage: ling32_sy_mla_fd_test exit 0 = pass
#include "ling32_kernels.hpp"
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <random>
#include <vector>
using namespace strata::ling32;
using namespace strata::ling32::gpu;
namespace strata::ling32::gpu {
void mla_attn_orig(const float* qabs, const float* q, const void* cache, const int* d_pos0, float* ctx, int N, int H, int nope, int rope, int kv_lora, float scale, bool cache_f32_b, cudaStream_t s);
bool mla_attn_fd_supported(int N, int H, int rope, int kv_lora, bool cache_f32);
void mla_attn_fd(const float* qabs, const float* q, const void* cache, const int* d_pos0, float* ctx, int N, int H, int nope, int rope, int kv_lora, float scale, cudaStream_t s, int stages);
} // namespace strata::ling32::gpu
namespace {
template <class T> T* up(const std::vector<T>& h) {
void* p = nullptr;
if (cudaMalloc(&p, h.size() * sizeof(T)) != cudaSuccess) { std::fprintf(stderr, "alloc failed\n"); std::exit(1); }
cudaMemcpy(p, h.data(), h.size() * sizeof(T), cudaMemcpyHostToDevice);
return static_cast<T*>(p);
}
double now_ms() { return std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now().time_since_epoch()).count(); }
} // namespace
int main() {
const int H = 32, nope = 128, rope = 64, kvl = 512, W = kvl + rope;
const float scale = 1.0f / std::sqrt(static_cast<float>(nope + rope));
std::mt19937 rng(11);
int fails = 0;
struct Case { int ctx, N; float qs; const char* what; };
const Case cases[] = {{1, 1, 0.1f, "flat"}, {2, 1, 0.1f, "flat"}, {31, 1, 0.3f, "normal"}, {32, 1, 0.3f, "normal"}, {33, 1, 0.3f, "normal"}, {64, 1, 0.3f, "normal"},
{257, 1, 0.1f, "flat"}, {1024, 1, 0.3f, "normal"}, {1024, 1, 1.5f, "peaked"}, {8192, 1, 0.1f, "flat"}, {8192, 1, 0.3f, "normal"}, {8192, 1, 1.5f, "peaked"},
{32768, 1, 0.3f, "normal"}, {131072, 1, 0.3f, "normal"}, {3000, 3, 0.3f, "normal"}, {1000, 16, 0.3f, "normal"}, {97, 16, 1.5f, "peaked"}};
for (const Case& c : cases) {
const int ctx = c.ctx, N = c.N, rows = ctx + 8;
std::vector<float> cache(static_cast<size_t>(rows) * W), qabs(static_cast<size_t>(N) * H * kvl), q(static_cast<size_t>(N) * H * (nope + rope));
std::uniform_real_distribution<float> du(-1.0f, 1.0f);
for (auto& v : cache) v = du(rng);
for (auto& v : qabs) v = du(rng) * c.qs;
for (auto& v : q) v = du(rng) * c.qs;
const int p0 = ctx - N; // the last token sees ctx rows
float *dc = up(cache), *dqa = up(qabs), *dq = up(q);
int* dp = up(std::vector<int>{p0});
std::vector<float> z(static_cast<size_t>(N) * H * kvl, 0.0f);
float *o_ref = up(z), *o_fd = up(z);
mla_attn_orig(dqa, dq, dc, dp, o_ref, N, H, nope, rope, kvl, scale, true, 0);
mla_attn_fd(dqa, dq, dc, dp, o_fd, N, H, nope, rope, kvl, scale, 0, 3);
cudaDeviceSynchronize();
std::vector<float> a(z.size()), bb(z.size());
cudaMemcpy(a.data(), o_ref, a.size() * 4, cudaMemcpyDeviceToHost);
cudaMemcpy(bb.data(), o_fd, bb.size() * 4, cudaMemcpyDeviceToHost);
double mabs = 0, mrel = 0;
bool bad = false;
for (size_t i = 0; i < a.size(); ++i) {
if (!std::isfinite(bb[i])) bad = true;
const double d = std::fabs(static_cast<double>(a[i]) - bb[i]);
mabs = std::max(mabs, d);
mrel = std::max(mrel, d / std::max(std::fabs(static_cast<double>(a[i])), 0.1));
}
const bool ok = !bad && mabs <= 1e-5 && mrel <= 1e-5; // mrel: relative to max(|ref|, 0.1)
if (!ok) ++fails;
// timing (N = 1 cases only)
double t_ref = 0, t_fd = 0, t_s1 = 0;
if (N == 1) {
const int reps = ctx >= 32768 ? 5 : (ctx >= 1024 ? 20 : 50);
double t0 = now_ms();
for (int i = 0; i < reps; ++i) mla_attn_orig(dqa, dq, dc, dp, o_ref, N, H, nope, rope, kvl, scale, true, 0);
cudaDeviceSynchronize();
t_ref = (now_ms() - t0) / reps;
t0 = now_ms();
for (int i = 0; i < reps; ++i) mla_attn_fd(dqa, dq, dc, dp, o_fd, N, H, nope, rope, kvl, scale, 0, 3);
cudaDeviceSynchronize();
t_fd = (now_ms() - t0) / reps;
t0 = now_ms();
for (int i = 0; i < reps; ++i) mla_attn_fd(dqa, dq, dc, dp, o_fd, N, H, nope, rope, kvl, scale, 0, 1);
cudaDeviceSynchronize();
t_s1 = (now_ms() - t0) / reps;
}
std::printf("ctx %5d N %2d %-6s max abs %.3e max rel %.3e orig %.3f ms fd %.3f ms (stage 1 alone %.3f) %s\n", ctx, N, c.what, mabs, mrel, t_ref, t_fd, t_s1, ok ? "ok" : "FAIL");
cudaFree(dc); cudaFree(dqa); cudaFree(dq); cudaFree(dp); cudaFree(o_ref); cudaFree(o_fd);
}
std::printf(fails ? "ling32_sy_mla_fd_test: %d FAILED\n" : "ling32_sy_mla_fd_test: all ok (%d)\n", fails);
return fails ? 1 : 0;
}