mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-04 15:28:40 +00:00
286 lines
16 KiB
Plaintext
286 lines
16 KiB
Plaintext
// tests/ling32/ling32_kernel_test.cu - the CUDA kernels of the Ling-3.2-Air engine against their CPU definitions.
|
|
// Usage: ling32_kernel_test DATA_DIR FIXTURE.strata (DATA_DIR = engine/tests/ling32/data: pack_s{4,5,6,8}.bin)
|
|
// Grows with the engine: step 2 = FWHT-128, Q8_1, S_K GEMV (Q8 and float activations), float GEMV, RMSNorm, embedding.
|
|
#include "ling32_kernels.hpp"
|
|
|
|
#include "strata/ling32/ling32_file.hpp"
|
|
#include "strata/ling32/ling32_sk.hpp"
|
|
|
|
#include <cmath>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <fstream>
|
|
#include <random>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
using namespace strata::ling32;
|
|
using namespace strata::ling32::gpu;
|
|
using strata::quant::sk::BlockQ8_1_G64;
|
|
using strata::quant::sk::SkCode;
|
|
|
|
#define CK(call) do { cudaError_t e_ = (call); if (e_ != cudaSuccess) { std::printf("CUDA error %s: %s\n", #call, cudaGetErrorString(e_)); return 2; } } while (0)
|
|
|
|
namespace {
|
|
int g_fail = 0;
|
|
void check(bool ok, const std::string& what) {
|
|
std::printf(" %-104s %s\n", what.c_str(), ok ? "ok" : "FAIL");
|
|
if (!ok) ++g_fail;
|
|
}
|
|
bool slurp(const std::string& p, std::vector<uint8_t>& out) {
|
|
std::ifstream f(p, std::ios::binary);
|
|
if (!f) return false;
|
|
out.assign(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>());
|
|
return true;
|
|
}
|
|
template <class T> T* dev(const std::vector<T>& h) {
|
|
T* d = nullptr;
|
|
cudaMalloc(&d, h.size() * sizeof(T));
|
|
cudaMemcpy(d, h.data(), h.size() * sizeof(T), cudaMemcpyHostToDevice);
|
|
return d;
|
|
}
|
|
template <class T> std::vector<T> host(const T* d, size_t n) {
|
|
std::vector<T> h(n);
|
|
cudaMemcpy(h.data(), d, n * sizeof(T), cudaMemcpyDeviceToHost);
|
|
return h;
|
|
}
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv) {
|
|
if (argc < 3) { std::printf("usage: ling32_kernel_test DATA_DIR FIXTURE.strata\n"); return 2; }
|
|
const std::string dir = argv[1];
|
|
int ndev = 0;
|
|
CK(cudaGetDeviceCount(&ndev));
|
|
cudaDeviceProp prop;
|
|
CK(cudaGetDeviceProperties(&prop, 0));
|
|
std::printf("device 0: %s (sm_%d%d)\n", prop.name, prop.major, prop.minor);
|
|
std::mt19937 rng(11);
|
|
std::normal_distribution<float> nd(0, 1);
|
|
|
|
std::printf("[FWHT-128 + Q8_1: bit-exact against the CPU functions]\n");
|
|
for (int K : {256, 2560, 768}) {
|
|
const int M = 5;
|
|
std::vector<float> x(M * K);
|
|
for (auto& v : x) v = nd(rng) * (0.1f + (rng() % 100) * 0.05f);
|
|
float* dx = dev(x);
|
|
Q8Blk* dq;
|
|
float* dy;
|
|
CK(cudaMalloc(&dq, size_t(M) * K / 64 * sizeof(Q8Blk)));
|
|
CK(cudaMalloc(&dy, x.size() * 4));
|
|
for (int fw = 0; fw < 2; ++fw) {
|
|
quant_q8(dx, dq, M, K, fw != 0, 0);
|
|
CK(cudaDeviceSynchronize());
|
|
auto q = host(dq, size_t(M) * K / 64);
|
|
size_t bad = 0;
|
|
for (int m = 0; m < M; ++m) {
|
|
std::vector<BlockQ8_1_G64> ref(K / 64);
|
|
std::vector<float> t(K);
|
|
if (fw) fwht128_f32(x.data() + m * K, t.data(), K); else std::memcpy(t.data(), x.data() + m * K, K * 4);
|
|
strata::quant::sk::quantize_q8_1_g64(t.data(), ref.data(), K);
|
|
for (int b = 0; b < K / 64; ++b) {
|
|
const Q8Blk& g = q[size_t(m) * (K / 64) + b];
|
|
if (std::memcmp(g.qs, ref[b].qs, 64) || g.d != ref[b].d || std::fabs(g.s - ref[b].s) > 1e-6f * (1 + std::fabs(ref[b].s))) ++bad;
|
|
}
|
|
}
|
|
check(bad == 0, std::string(fw ? "FWHT-128 + " : "plain ") + "Q8_1, K=" + std::to_string(K) + ": " + std::to_string(M * K / 64) + " blocks identical (" + std::to_string(bad) + " differ)");
|
|
}
|
|
fwht128(dx, dy, M, K, 0);
|
|
CK(cudaDeviceSynchronize());
|
|
auto y = host(dy, x.size());
|
|
size_t bad = 0;
|
|
for (int m = 0; m < M; ++m) {
|
|
std::vector<float> t(K);
|
|
fwht128_f32(x.data() + m * K, t.data(), K);
|
|
for (int i = 0; i < K; ++i) bad += std::memcmp(&t[i], &y[size_t(m) * K + i], 4) != 0;
|
|
}
|
|
check(bad == 0, "float FWHT-128, K=" + std::to_string(K) + ": identical to the CPU (" + std::to_string(bad) + " differ)");
|
|
cudaFree(dx); cudaFree(dq); cudaFree(dy);
|
|
}
|
|
|
|
std::printf("[S_K GEMV: all codecs, several shapes and token counts]\n");
|
|
for (int code : {4, 5, 6, 8}) {
|
|
std::vector<uint8_t> bin;
|
|
if (!slurp(dir + "/pack_s" + std::to_string(code) + ".bin", bin)) { check(false, "pack_s" + std::to_string(code) + ".bin missing"); continue; }
|
|
const int sbb = sk_superblock_bytes(code), nsb_total = int(bin.size() / sbb); // 96
|
|
uint8_t* dW = dev(bin);
|
|
for (int m : {1, 2, 3, 6}) {
|
|
const int K = 256 * m, rows = nsb_total / m;
|
|
std::vector<float> wdeq(size_t(rows) * K);
|
|
strata::quant::sk::dequant_sk_row(bin.data(), static_cast<SkCode>(code), wdeq.data(), wdeq.size());
|
|
for (int N : {1, 3, 8, 16, 21, 70}) {
|
|
std::vector<float> x(size_t(N) * K);
|
|
float grp = 1.0f;
|
|
for (size_t i = 0; i < x.size(); ++i) { // each 64-group scaled by 10^-U(0, 8): the real activations span decades
|
|
if (i % 64 == 0) grp = std::pow(10.0f, -8.0f * std::uniform_real_distribution<float>(0.f, 1.f)(rng));
|
|
x[i] = nd(rng) * grp;
|
|
}
|
|
float* dx = dev(x);
|
|
Q8Blk* dq;
|
|
float *dout, *dout2, *dout3;
|
|
CK(cudaMalloc(&dq, size_t(N) * K / 64 * sizeof(Q8Blk)));
|
|
CK(cudaMalloc(&dout, size_t(N) * rows * 4));
|
|
CK(cudaMalloc(&dout2, size_t(N) * rows * 4));
|
|
CK(cudaMalloc(&dout3, size_t(N) * rows * 4));
|
|
quant_q8(dx, dq, N, K, false, 0);
|
|
gemv_sk(dW, code, rows, K, dq, true, N, dout, rows, false, 0);
|
|
gemv_sk(dW, code, rows, K, dx, false, N, dout2, rows, false, 0);
|
|
gemv_sk(dW, code, rows, K, dx, false, N, dout2, rows, true, 0); // accumulate: out2 = 2 * float result
|
|
double e_modes[4] = {0, 0, 0, 0};
|
|
for (int md = 3; md >= 0; --md) {
|
|
g_tc_mode = md;
|
|
gemm_sk_tc(dW, code, rows, K, dx, N, dout3, rows, false, 0);
|
|
gemm_sk_tc(dW, code, rows, K, dx, N, dout3, rows, true, 0);
|
|
CK(cudaDeviceSynchronize());
|
|
auto om = host(dout3, size_t(N) * rows);
|
|
for (int t = 0; t < N; ++t)
|
|
for (int r = 0; r < rows; ++r) {
|
|
double ref = 0, mag = 0;
|
|
for (int i = 0; i < K; ++i) { const double p = double(wdeq[size_t(r) * K + i]) * x[size_t(t) * K + i]; ref += p; mag += std::fabs(p); }
|
|
e_modes[md] = std::fmax(e_modes[md], std::fabs(om[size_t(t) * rows + r] / 2 - ref) / (mag + 1e-9));
|
|
}
|
|
}
|
|
if (N == 70 && K == 1536) std::printf(" S%d_K tensor-core error vs double (of sum|w x|), mode 0..3: %.2e %.2e %.2e %.2e\n", code, e_modes[0], e_modes[1], e_modes[2], e_modes[3]);
|
|
g_tc_mode = 0;
|
|
gemm_sk_tc(dW, code, rows, K, dx, N, dout3, rows, false, 0);
|
|
gemm_sk_tc(dW, code, rows, K, dx, N, dout3, rows, true, 0);
|
|
CK(cudaDeviceSynchronize());
|
|
auto o1 = host(dout, size_t(N) * rows), o2 = host(dout2, size_t(N) * rows), o3 = host(dout3, size_t(N) * rows);
|
|
double e_q8 = 0, e_f = 0, e_acc = 0, e_tc = 0;
|
|
for (int t = 0; t < N; ++t) {
|
|
std::vector<BlockQ8_1_G64> xq(K / 64);
|
|
strata::quant::sk::quantize_q8_1_g64(x.data() + size_t(t) * K, xq.data(), K);
|
|
for (int r = 0; r < rows; ++r) {
|
|
double ref = 0, mag = 0;
|
|
for (int i = 0; i < K; ++i) { const double p = double(wdeq[size_t(r) * K + i]) * x[size_t(t) * K + i]; ref += p; mag += std::fabs(p); }
|
|
const float cpu_q8 = strata::quant::sk::dot_sk_q8_1_row(bin.data() + size_t(r) * (K / 256) * sbb, static_cast<SkCode>(code), xq.data(), K);
|
|
e_q8 = std::fmax(e_q8, std::fabs(o1[size_t(t) * rows + r] - cpu_q8) / (mag + 1e-9));
|
|
e_f = std::fmax(e_f, std::fabs(o2[size_t(t) * rows + r] / 2 - ref) / (mag + 1e-9));
|
|
e_tc = std::fmax(e_tc, std::fabs(o3[size_t(t) * rows + r] / 2 - ref) / (mag + 1e-9));
|
|
e_acc = std::fmax(e_acc, std::fabs(o2[size_t(t) * rows + r] - 2 * (o2[size_t(t) * rows + r] / 2)));
|
|
}
|
|
}
|
|
check(e_q8 < 2e-5 && e_f < 2e-5,
|
|
"S" + std::to_string(code) + "_K [" + std::to_string(rows) + " x " + std::to_string(K) + "] N=" + std::to_string(N) + ": Q8 path vs dot_sk_q8_1_row " + std::to_string(e_q8) + ", float path vs double " + std::to_string(e_f));
|
|
check(e_tc < 3e-5, "S" + std::to_string(code) + "_K [" + std::to_string(rows) + " x " + std::to_string(K) + "] N=" + std::to_string(N) + ": tensor-core GEMM (exact integer weights, hi+lo activations) vs double, relative to sum|w*x|: " + std::to_string(e_tc));
|
|
cudaFree(dx); cudaFree(dq); cudaFree(dout); cudaFree(dout2); cudaFree(dout3);
|
|
}
|
|
}
|
|
cudaFree(dW);
|
|
}
|
|
|
|
std::printf("[float GEMV, RMSNorm, embedding on the fixture]\n");
|
|
{
|
|
const int rows = 70, K = 260, N = 6; // K % 4 == 0, rows not a multiple of 4
|
|
std::vector<float> W(size_t(rows) * K), x(size_t(N) * K);
|
|
for (auto& v : W) v = nd(rng);
|
|
for (auto& v : x) v = nd(rng);
|
|
float *dW = dev(W), *dx = dev(x), *dout;
|
|
CK(cudaMalloc(&dout, size_t(N) * rows * 4));
|
|
gemv_f32(dW, rows, K, dx, N, dout, 0);
|
|
CK(cudaDeviceSynchronize());
|
|
auto o = host(dout, size_t(N) * rows);
|
|
double e = 0;
|
|
for (int t = 0; t < N; ++t)
|
|
for (int r = 0; r < rows; ++r) {
|
|
double ref = 0, mag = 0;
|
|
for (int i = 0; i < K; ++i) { ref += double(W[size_t(r) * K + i]) * x[size_t(t) * K + i]; mag += std::fabs(double(W[size_t(r) * K + i]) * x[size_t(t) * K + i]); }
|
|
e = std::fmax(e, std::fabs(o[size_t(t) * rows + r] - ref) / mag);
|
|
}
|
|
check(e < 1e-5, "gemv_f32 [70 x 260] x 6 tokens: max rel error " + std::to_string(e));
|
|
// rmsnorm with and without gamma
|
|
const int D = 2560;
|
|
std::vector<float> xn(3 * D), gm(D);
|
|
for (auto& v : xn) v = nd(rng) * 3;
|
|
for (auto& v : gm) v = 1 + 0.1f * nd(rng);
|
|
float *dxn = dev(xn), *dgm = dev(gm), *dy;
|
|
CK(cudaMalloc(&dy, xn.size() * 4));
|
|
double e2 = 0;
|
|
for (int g = 0; g < 2; ++g) {
|
|
rmsnorm(dxn, g ? dgm : nullptr, 1e-6f, dy, 3, D, 0);
|
|
CK(cudaDeviceSynchronize());
|
|
auto y = host(dy, xn.size());
|
|
for (int t = 0; t < 3; ++t) {
|
|
double ss = 0;
|
|
for (int i = 0; i < D; ++i) ss += double(xn[t * D + i]) * xn[t * D + i];
|
|
const double r = 1.0 / std::sqrt(ss / D + 1e-6);
|
|
for (int i = 0; i < D; ++i) e2 = std::fmax(e2, std::fabs(y[t * D + i] - xn[t * D + i] * r * (g ? gm[i] : 1.0)) / (std::fabs(y[t * D + i]) + 1e-3));
|
|
}
|
|
}
|
|
check(e2 < 1e-5, "rmsnorm (with and without gamma): max rel error " + std::to_string(e2));
|
|
cudaFree(dW); cudaFree(dx); cudaFree(dout); cudaFree(dxn); cudaFree(dgm); cudaFree(dy);
|
|
}
|
|
{
|
|
Ling32File f;
|
|
std::string err;
|
|
if (!f.open(argv[2], err)) { check(false, "fixture opens: " + err); }
|
|
else {
|
|
const TensorInfo* emb = f.man().find("model.word_embeddings.weight");
|
|
std::vector<uint8_t> raw;
|
|
f.read_tensor(*emb, 1, raw, err);
|
|
std::vector<float> ref;
|
|
f.read_float(*emb, 1, ref, err);
|
|
const int D = f.cfg().D;
|
|
std::vector<int> ids = {0, 7, 511, 42, 42};
|
|
uint8_t* dW = dev(raw);
|
|
int* di = dev(ids);
|
|
float* dx;
|
|
CK(cudaMalloc(&dx, ids.size() * D * 4));
|
|
embed_s8k(dW, D, di, int(ids.size()), dx, 0);
|
|
CK(cudaDeviceSynchronize());
|
|
auto x = host(dx, ids.size() * D);
|
|
size_t bad = 0;
|
|
for (size_t t = 0; t < ids.size(); ++t)
|
|
for (int i = 0; i < D; ++i) bad += std::memcmp(&x[t * D + i], &ref[size_t(ids[t]) * D + i], 4) != 0;
|
|
check(bad == 0, "embedding rows of the fixture (S8_K) identical to the CPU decode (" + std::to_string(bad) + " differ)");
|
|
cudaFree(dW); cudaFree(di); cudaFree(dx);
|
|
}
|
|
}
|
|
{ // chunk-parallel KDA (the 32- and the 16-token chunk forms, the latter is what a card with 64 KiB of shared memory runs) against the sequential recurrence; N = 40 is no multiple of either chunk
|
|
for (int hd : {64, 128}) {
|
|
const int H = 2, N = 40, C = H * hd;
|
|
std::mt19937 rng(7 + hd);
|
|
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));
|
|
const auto 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);
|
|
const auto 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);
|
|
float *dq = dev(qn), *dk = dev(kn), *dv = dev(vc), *df = dev(fr), *dg = dev(gr), *dbeta = dev(br), *dA = dev(Al), *ddb = dev(db), *don = dev(on), *dout, *dS;
|
|
CK(cudaMalloc(&dout, size_t(N) * C * 4));
|
|
CK(cudaMalloc(&dS, S0.size() * 4));
|
|
auto ck = [&](cudaError_t e, const char* what) {
|
|
if (e != cudaSuccess) check(false, std::string("chunk-parallel KDA test: ") + what + ": " + cudaGetErrorString(e));
|
|
};
|
|
auto run = [&](bool chunked, int force, std::vector<float>& o, std::vector<float>& st) {
|
|
g_kda_chunk_force = force;
|
|
ck(cudaMemcpy(dS, S0.data(), S0.size() * 4, cudaMemcpyHostToDevice), "upload of the state");
|
|
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);
|
|
ck(cudaDeviceSynchronize(), "kda_step");
|
|
ck(cudaGetLastError(), "kda_step launch");
|
|
o = host(dout, size_t(N) * C);
|
|
st = host(dS, S0.size());
|
|
};
|
|
std::vector<float> o_ref, s_ref, o32, s32, o16, s16;
|
|
run(false, 0, o_ref, s_ref);
|
|
const size_t need32 = (5ull * 32 * (hd + 1) + 2ull * 32 * 32 + 32 + hd) * sizeof(float) + 256;
|
|
const bool has32 = need32 <= static_cast<size_t>(device_smem_optin()); // a 64 KiB device (RDNA) cannot run the 32-token form at head dim 128: it is not selected there either
|
|
if (has32) run(true, 32, o32, s32);
|
|
else { o32 = o_ref; s32 = s_ref; std::printf(" (32-token KDA chunks need %zu bytes of shared memory: not run on this device)\n", need32); }
|
|
run(true, 16, o16, s16);
|
|
g_kda_chunk_force = 0;
|
|
auto 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) { 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);
|
|
};
|
|
const double e32 = std::fmax(relerr(o32, o_ref), relerr(s32, s_ref)), e16 = std::fmax(relerr(o16, o_ref), relerr(s16, s_ref));
|
|
check(e32 < 2e-4 && e16 < 2e-4, "chunk-parallel KDA hd " + std::to_string(hd) + ", N=40: 32-token chunks max rel error " + std::to_string(e32) + ", 16-token chunks " + std::to_string(e16) + " (outputs and state vs the sequential recurrence)");
|
|
cudaFree(dq); cudaFree(dk); cudaFree(dv); cudaFree(df); cudaFree(dg); cudaFree(dbeta); cudaFree(dA); cudaFree(ddb); cudaFree(don); cudaFree(dout); cudaFree(dS);
|
|
}
|
|
}
|
|
std::printf("%s (%d failed)\n", g_fail ? "FAIL" : "PASS", g_fail);
|
|
return g_fail ? 1 : 0;
|
|
}
|