mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-04 15:28:40 +00:00
The serial scan of a head over the chunks of 8 tokens ran as one work-group per head with one value column per lane and the 128-float state column in registers (spills on Xe). The state update is independent per value column, so the scan now runs as HD / 32 work-groups per head (32 columns each), G sub-groups per work-group, each owning HD / G key rows of the state in registers (lanes are the columns); the partial sums over d of kE s and qE s are exchanged through shared memory per chunk, U and the output row are computed redundantly per sub-group, the next chunk's tiles are prefetched into registers and stored into the other shared-memory buffer (two barriers per chunk). The gated RMS norm needs the whole row, so the scan writes the raw output into out and launch_chunk_norm normalises it in place (same expressions). Mode switch LING32_SY_KDA_SEQ: 0 the old scan, 4 / 8 the new one with 4 / 8 key-row groups (default 8); the decode / verify / snapshot path (launch_step, N <= 16) is untouched, state layout and final state are the same. ling32_sy_kda_test: chunk scan modes against the sequential recurrence (hd 64 / 128, N 40 / 100, and 32 heads x 2048 tokens: max rel error <= 2.9e-5, gate 2e-4) and timings. A750: pre + scan for 32 heads, hd 128, 2048 tokens 141.6 ms (old) -> 77.3 ms (4 groups) -> 44.0 ms (8 groups). Forward tests f32 / q8 / wide with --kda-seq 4 pass in every mode (top-1 12 / 12, KL 4.3e-12 vs 4.2e-12 old).
126 lines
6.0 KiB
C++
126 lines
6.0 KiB
C++
// tests/ling32/ling32_sy_kda_test.cpp - the SYCL chunk-parallel KDA prefill scan (sycl/ling32_sy_k_kda.cpp): the scan split over the value columns (modes 4 and 8, launch_chunk_seq2 + launch_chunk_norm) and the
|
|
// one-column-per-lane scan (mode 0, launch_chunk_seq) against the sequential recurrence (kda_step without chunking), outputs and final state, at head dim 64 and 128 and N = 40 / 100 (no multiple of the chunk);
|
|
// then, at the real shape (32 heads, head dim 128, 2048 tokens), the accuracy of every mode and the time of the pre + scan (+ norm) launches. usage: ling32_sy_kda_test [skip-big]
|
|
#include "ling32_kernels.hpp"
|
|
|
|
#include <chrono>
|
|
#include <cmath>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <random>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
using namespace strata::ling32::gpu;
|
|
|
|
namespace strata::ling32::gpu {
|
|
extern int g_sy_kda_seq_mode;
|
|
}
|
|
|
|
namespace {
|
|
int g_fail = 0;
|
|
void check(bool ok, const std::string& what, double v) {
|
|
std::printf(" %-110s %s (%.3g)\n", what.c_str(), ok ? "ok" : "FAIL", v);
|
|
if (!ok) ++g_fail;
|
|
}
|
|
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);
|
|
}
|
|
std::vector<float> down(const float* d, size_t n) {
|
|
std::vector<float> h(n);
|
|
cudaMemcpy(h.data(), d, n * 4, cudaMemcpyDeviceToHost);
|
|
return h;
|
|
}
|
|
double now_ms() { return std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now().time_since_epoch()).count(); }
|
|
|
|
struct Case {
|
|
int H, hd, N, C;
|
|
std::vector<float> qn, kn, vc, fr, gr, br, Al, db, on, S0;
|
|
float *dq, *dk, *dv, *df, *dg, *dbeta, *dA, *ddb, *don, *dout, *dS, *dscr;
|
|
size_t scr_floats;
|
|
Case(int H_, int hd_, int N_, unsigned seed) : H(H_), hd(hd_), N(N_), C(H_ * hd_) {
|
|
std::mt19937 rng(seed);
|
|
auto rnd = [&](size_t n, float lo, float hi) {
|
|
std::vector<float> v(n);
|
|
for (auto& x : v) x = lo + (hi - lo) * ((rng() & 0xFFFF) / 65535.0f);
|
|
return v;
|
|
};
|
|
const float ks = 1.0f / std::sqrt(static_cast<float>(hd));
|
|
qn = rnd(size_t(N) * C, -ks, ks); kn = rnd(size_t(N) * C, -ks, ks); vc = rnd(size_t(N) * C, -1.f, 1.f); fr = rnd(size_t(N) * C, -1.f, 1.f); gr = rnd(size_t(N) * C, -1.f, 1.f);
|
|
br = rnd(size_t(N) * H, -1.f, 1.f); Al = rnd(H, -1.f, 0.f); db = rnd(C, -0.5f, 0.5f); on = rnd(hd, 0.5f, 1.5f); S0 = rnd(size_t(H) * hd * hd, -0.05f, 0.05f);
|
|
dq = up(qn); dk = up(kn); dv = up(vc); df = up(fr); dg = up(gr); dbeta = up(br); dA = up(Al); ddb = up(db); don = up(on); dS = up(S0);
|
|
cudaMalloc(reinterpret_cast<void**>(&dout), size_t(N) * C * 4);
|
|
scr_floats = kda_chunk_scratch_floats(N, H, hd);
|
|
cudaMalloc(reinterpret_cast<void**>(&dscr), scr_floats * 4);
|
|
}
|
|
void run(bool chunked, int mode, std::vector<float>& o, std::vector<float>& st) {
|
|
g_sy_kda_seq_mode = mode;
|
|
cudaMemcpy(dS, S0.data(), S0.size() * 4, cudaMemcpyHostToDevice);
|
|
kda_step(dS, dq, dk, dv, df, C, dg, C, dbeta, dA, ddb, don, -5.0f, 1e-6f, dout, N, H, hd, chunked, 0, nullptr, chunked ? dscr : nullptr, chunked ? scr_floats : 0);
|
|
cudaDeviceSynchronize();
|
|
o = down(dout, size_t(N) * C);
|
|
st = down(dS, S0.size());
|
|
}
|
|
double time_ms(int mode, int reps) {
|
|
g_sy_kda_seq_mode = mode;
|
|
auto once = [&] { kda_step(dS, dq, dk, dv, df, C, dg, C, dbeta, dA, ddb, don, -5.0f, 1e-6f, dout, N, H, hd, true, 0, nullptr, dscr, scr_floats); };
|
|
once();
|
|
cudaDeviceSynchronize();
|
|
const double t0 = now_ms();
|
|
for (int i = 0; i < reps; ++i) once();
|
|
cudaDeviceSynchronize();
|
|
return (now_ms() - t0) / reps;
|
|
}
|
|
~Case() { cudaFree(dq); cudaFree(dk); cudaFree(dv); cudaFree(df); cudaFree(dg); cudaFree(dbeta); cudaFree(dA); cudaFree(ddb); cudaFree(don); cudaFree(dout); cudaFree(dS); cudaFree(dscr); }
|
|
};
|
|
|
|
double relerr(const std::vector<float>& x, const std::vector<float>& y) {
|
|
double num = 0, den = 0;
|
|
for (size_t i = 0; i < x.size(); ++i) {
|
|
if (!std::isfinite(x[i])) return 1e30;
|
|
num = std::fmax(num, std::fabs(double(x[i]) - y[i]));
|
|
den += double(y[i]) * y[i];
|
|
}
|
|
return num / (std::sqrt(den / x.size()) + 1e-30);
|
|
}
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv) {
|
|
cudaDeviceProp prop;
|
|
cudaGetDeviceProperties(&prop, 0);
|
|
std::printf("device: %s\n", prop.name);
|
|
bool big = true;
|
|
for (int i = 1; i < argc; ++i)
|
|
if (std::string(argv[i]) == "skip-big") big = false;
|
|
for (int hd : {64, 128})
|
|
for (int N : {40, 100}) {
|
|
Case c(2, hd, N, 7 + hd + N);
|
|
std::vector<float> o_ref, s_ref;
|
|
c.run(false, 0, o_ref, s_ref);
|
|
for (int mode : {0, 4, 8}) {
|
|
std::vector<float> o, st;
|
|
c.run(true, mode, o, st);
|
|
const double e = std::fmax(relerr(o, o_ref), relerr(st, s_ref));
|
|
check(e < 2e-4, "KDA chunk scan mode " + std::to_string(mode) + " hd " + std::to_string(hd) + " N " + std::to_string(N) + ": max rel error vs the sequential recurrence (outputs and state)", e);
|
|
}
|
|
}
|
|
if (big) {
|
|
Case c(32, 128, 2048, 99);
|
|
std::vector<float> o_ref, s_ref;
|
|
c.run(false, 0, o_ref, s_ref);
|
|
for (int mode : {0, 4, 8}) {
|
|
std::vector<float> o, st;
|
|
c.run(true, mode, o, st);
|
|
const double e = std::fmax(relerr(o, o_ref), relerr(st, s_ref));
|
|
check(e < 2e-4, "KDA chunk scan mode " + std::to_string(mode) + " at 32 heads, hd 128, N 2048: max rel error vs the sequential recurrence", e);
|
|
}
|
|
for (int mode : {0, 4, 8}) std::printf(" chunk path (pre + scan%s) mode %d, 32 heads, hd 128, 2048 tokens: %.3f ms per call\n", mode ? " + norm" : "", mode, c.time_ms(mode, mode == 0 ? 3 : 10));
|
|
}
|
|
std::printf(g_fail ? "FAIL (%d failures)\n" : "PASS (0 failures)\n", g_fail);
|
|
return g_fail ? 1 : 0;
|
|
}
|