Files
leleka/crates/cuda-driver/oracle/drivers/s_gemv_legacy.cpp
T

275 lines
14 KiB
C++

// Executes every kernel of s_gemv_legacy.cu (the Original's legacy S-form GEMVs: s_gemv, s_gemv_split, the Q8_K / Q8_0
// activation kernels, s2_gemv, s2_gemv_quads, s2_gemv_q8, s2_gemv_fast) on the host shim and compares each with an FP64
// reference of y[o] = sum_i (cb[code] * scale + offset) * x[i], over S2 / S4 / S8 forms, group sizes 16..64, both
// codebooks, with and without offset, several row-split widths and n_out values that are not multiples of the block.
// * "exact" inputs (powers-of-two scales, small integer activations) make every intermediate exactly representable in
// float, so EVERY summation order must reproduce the reference bit for bit;
// * "random" inputs are checked against a condition-number bound: |y - ref| <= 1e-4 * sum|term|.
// stdout: the golden fixture JSON (inputs and kernel outputs of a few small exact cases) consumed by
// s_gemv_legacy.rs (oracle/fixtures/s_gemv_legacy_golden.json); the PASS/FAIL report goes to stderr.
#include SHIM_KERNEL_0
#include <cstring>
#include <tuple>
#include <string>
struct Rng {
uint64_t s;
uint32_t next() { s = s * 6364136223846793005ULL + 1442695040888963407ULL; return (uint32_t) (s >> 33); }
int range(int lo, int hi) { return lo + (int) (next() % (uint32_t) (hi - lo + 1)); } // inclusive
float unit() { return (float) next() / 2147483648.0f * 2.0f - 1.0f; } // [-1, 1)
};
static const int kTbl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
static uint16_t to_h(float f) { return __float2half_rn(f).bits; }
static float from_h(uint16_t b) { return __half2float(__ushort_as_half(b)); }
struct Form { int bits, bias, group, codebook, has_offset; };
struct Case {
Form f; long long n_in, n_out;
std::vector<uint8_t> codes; // packed
std::vector<int> raw_codes; // n_out * n_in
std::vector<float> scales, offset;
std::vector<uint16_t> x; // fp16 bits
std::vector<uint8_t> q8k, q80; // block_q8_K (292 B / 256) and block_q8_0 (34 B / 32)
std::vector<double> xf, x8k, x80; // dequantised activations
};
static Case make(Form f, long long n_in, long long n_out, bool exact, uint64_t seed) {
Rng r{seed};
Case c; c.f = f; c.n_in = n_in; c.n_out = n_out;
const int per = 8 / f.bits;
const long long ng = n_in / f.group;
c.raw_codes.resize(n_out * n_in);
c.codes.assign(n_out * n_in / per, 0);
for (long long e = 0; e < n_out * n_in; ++e) {
const int code = (int) (r.next() & ((1u << f.bits) - 1));
c.raw_codes[e] = code;
c.codes[e / per] |= (uint8_t) (code << ((e % per) * f.bits));
}
c.scales.resize(n_out * ng);
c.offset.resize(f.has_offset ? n_out * ng : 0);
static const float sc[3] = {0.25f, 0.5f, 1.0f}, of[5] = {-1.0f, -0.5f, 0.0f, 0.5f, 1.0f};
for (auto& s : c.scales) s = exact ? sc[r.next() % 3] : 0.02f * (1.0f + 0.9f * r.unit());
for (auto& o : c.offset) o = exact ? of[r.next() % 5] : 0.05f * r.unit();
c.x.resize(n_in); c.xf.resize(n_in);
for (long long i = 0; i < n_in; ++i) {
const float v = exact ? (float) r.range(-8, 8) : 1.5f * r.unit();
c.x[i] = to_h(v); c.xf[i] = from_h(c.x[i]);
}
// Q8_K: { f32 d; int8 qs[256]; int16 bsums[16] }
static const float d8[3] = {0.125f, 0.25f, 0.5f};
if (n_in % 256 == 0) {
c.q8k.assign(n_in / 256 * 292, 0); c.x8k.resize(n_in);
for (long long b = 0; b < n_in / 256; ++b) {
const float d = exact ? d8[r.next() % 3] : 0.01f * (1.0f + 0.5f * r.unit());
std::memcpy(&c.q8k[b * 292], &d, 4);
for (int j = 0; j < 256; ++j) {
const int q = exact ? r.range(-15, 15) : r.range(-127, 127);
c.q8k[b * 292 + 4 + j] = (uint8_t) (int8_t) q;
c.x8k[b * 256 + j] = (double) (d * (float) q);
}
}
}
if (n_in % 32 == 0) {
c.q80.assign(n_in / 32 * 34, 0); c.x80.resize(n_in);
for (long long b = 0; b < n_in / 32; ++b) {
const float dv = exact ? d8[r.next() % 3] : 0.01f * (1.0f + 0.5f * r.unit());
const uint16_t dh = to_h(dv);
std::memcpy(&c.q80[b * 34], &dh, 2);
const float d = from_h(dh);
for (int j = 0; j < 32; ++j) {
const int q = exact ? r.range(-15, 15) : r.range(-127, 127);
c.q80[b * 34 + 2 + j] = (uint8_t) (int8_t) q;
c.x80[b * 32 + j] = (double) (d * (float) q);
}
}
}
return c;
}
// FP64 reference and the sum of absolute terms (the error bound's scale) for activation vector `a`.
static void reference(const Case& c, const std::vector<double>& a, std::vector<double>& y, std::vector<double>& mag) {
y.assign(c.n_out, 0.0); mag.assign(c.n_out, 0.0);
const long long ng = c.n_in / c.f.group;
for (long long o = 0; o < c.n_out; ++o)
for (long long i = 0; i < c.n_in; ++i) {
const int code = c.raw_codes[o * c.n_in + i];
const double cb = c.f.codebook ? (double) kTbl[code & 15] : (double) (code + c.f.bias);
const long long g = o * ng + i / c.f.group;
const double w = cb * (double) c.scales[g] + (c.f.has_offset ? (double) c.offset[g] : 0.0);
y[o] += w * a[i];
mag[o] += std::fabs(w * a[i]);
}
}
static int g_fail = 0, g_checks = 0;
static void check(const std::string& what, const Case& c, const std::vector<float>& got, const std::vector<double>& a, bool exact) {
std::vector<double> ref, mag;
reference(c, a, ref, mag);
double worst = 0;
for (long long o = 0; o < c.n_out; ++o) {
const double err = std::fabs((double) got[o] - ref[o]);
const double bound = exact ? 0.0 : 1e-4 * mag[o] + 1e-12;
worst = std::max(worst, mag[o] > 0 ? err / mag[o] : err);
if (err > bound) {
std::fprintf(stderr, "FAIL %s o=%lld got %.9g ref %.9g\n", what.c_str(), o, (double) got[o], ref[o]);
++g_fail;
return;
}
}
++g_checks;
if (getenv("VERBOSE")) std::fprintf(stderr, "ok %s (worst rel-to-magnitude %.2e)\n", what.c_str(), worst);
}
static std::string tag(const Case& c, const char* k, int extra = -1) {
char b[160];
std::snprintf(b, sizeof b, "%s bits=%d bias=%d g=%d cb=%d off=%d n_in=%lld n_out=%lld", k, c.f.bits, c.f.bias, c.f.group, c.f.codebook,
c.f.has_offset, c.n_in, c.n_out);
std::string s = b;
if (extra >= 0) s += " tpr=" + std::to_string(extra);
return s;
}
static int shift_of(int group) { int s = 0; while ((1 << s) < group) ++s; return s; }
static const float* offp(const Case& c) { return c.f.has_offset ? c.offset.data() : nullptr; }
// the entry points differ per width, so the dispatch is spelled out
static std::vector<float> run_s_gemv(const Case& c) {
std::vector<float> y(c.n_out, -1.0f);
shim_launch(dim3((unsigned) ((c.n_out + 127) / 128)), dim3(128), 0, [&] {
auto a = std::make_tuple(c.x.data(), c.codes.data(), c.scales.data(), offp(c), y.data(), c.n_in, c.n_out, c.f.bias, c.f.codebook, c.f.group, c.f.has_offset);
if (c.f.bits == 2) std::apply(leleka_s_gemv_2, a);
else if (c.f.bits == 4) std::apply(leleka_s_gemv_4, a);
else std::apply(leleka_s_gemv_8, a);
});
return y;
}
static std::vector<float> run_split(const Case& c, int tpr) {
std::vector<float> y(c.n_out, -1.0f);
shim_launch(dim3((unsigned) c.n_out), dim3(tpr), (size_t) tpr * 4, [&] {
auto a = std::make_tuple(c.x.data(), c.codes.data(), c.scales.data(), offp(c), y.data(), c.n_in, c.n_out, c.f.bias, c.f.codebook, c.f.group,
shift_of(c.f.group), c.f.has_offset, tpr);
if (c.f.bits == 2) std::apply(leleka_s_gemv_split_2, a);
else if (c.f.bits == 4) std::apply(leleka_s_gemv_split_4, a);
else std::apply(leleka_s_gemv_split_8, a);
});
return y;
}
static std::vector<float> run_q8k(const Case& c) {
std::vector<float> y(c.n_out, -1.0f);
shim_launch(dim3((unsigned) ((c.n_out + 127) / 128)), dim3(128), 0, [&] {
auto a = std::make_tuple(c.q8k.data(), c.codes.data(), c.scales.data(), offp(c), y.data(), c.n_in, c.n_out, c.f.bias, c.f.codebook, c.f.group, c.f.has_offset);
if (c.f.bits == 4) std::apply(leleka_s_gemv_q8k_4, a); else std::apply(leleka_s_gemv_q8k_8, a);
});
return y;
}
static std::vector<float> run_q8_split(const Case& c, bool q8k) {
std::vector<float> y(c.n_out, -1.0f);
shim_launch(dim3((unsigned) ((c.n_out + 7) / 8)), dim3(256), 0, [&] {
auto a = std::make_tuple(q8k ? c.q8k.data() : c.q80.data(), c.codes.data(), c.scales.data(), offp(c), y.data(), c.n_in, c.n_out, c.f.bias,
c.f.codebook, shift_of(c.f.group), c.f.has_offset);
if (q8k) { if (c.f.bits == 4) std::apply(leleka_s_gemv_q8k_split_4, a); else std::apply(leleka_s_gemv_q8k_split_8, a); }
else { if (c.f.bits == 4) std::apply(leleka_s_gemv_q8_0_split_4, a); else std::apply(leleka_s_gemv_q8_0_split_8, a); }
});
return y;
}
static std::vector<float> run_s2(const Case& c, int which, int tpr) { // 0 naive, 1 quads, 2 q8, 3 fast staged, 4 fast unstaged
std::vector<float> y(c.n_out, -1.0f);
if (which == 0) {
shim_launch(dim3((unsigned) ((c.n_out + 127) / 128)), dim3(128), 0, [&] {
leleka_s2_gemv(c.x.data(), c.codes.data(), c.scales.data(), y.data(), c.n_in, c.n_out); });
return y;
}
const size_t smem = (size_t) tpr * 4 + (which == 3 ? 4096 * 2 : 0);
shim_launch(dim3((unsigned) c.n_out), dim3(tpr), smem, [&] {
if (which == 1) leleka_s2_gemv_quads(c.x.data(), c.codes.data(), c.scales.data(), y.data(), c.n_in, c.n_out, tpr);
else if (which == 2) leleka_s2_gemv_q8(c.q80.data(), c.codes.data(), c.scales.data(), y.data(), c.n_in, c.n_out, tpr);
else if (which == 3) leleka_s2_gemv_fast_staged(c.x.data(), c.codes.data(), c.scales.data(), y.data(), c.n_in, c.n_out, tpr);
else leleka_s2_gemv_fast_unstaged(c.x.data(), c.codes.data(), c.scales.data(), y.data(), c.n_in, c.n_out, tpr);
});
return y;
}
static void exercise(Form f, long long n_in, long long n_out, bool exact, uint64_t seed) {
Case c = make(f, n_in, n_out, exact, seed);
check(tag(c, "s_gemv", 0), c, run_s_gemv(c), c.xf, exact);
for (int tpr : {1, 32, 64, 256}) check(tag(c, "s_gemv_split", tpr), c, run_split(c, tpr), c.xf, exact);
if (f.bits != 2 && !c.q8k.empty() && n_in % f.group == 0) {
check(tag(c, "s_gemv_q8k"), c, run_q8k(c), c.x8k, exact);
check(tag(c, "s_gemv_q8k_split"), c, run_q8_split(c, true), c.x8k, exact);
}
if (f.bits != 2 && !c.q80.empty() && f.group % 16 == 0) check(tag(c, "s_gemv_q8_0_split"), c, run_q8_split(c, false), c.x80, exact);
if (f.bits == 2 && f.bias == -1 && f.group == 64 && !f.has_offset && !f.codebook) {
check(tag(c, "s2_gemv"), c, run_s2(c, 0, 0), c.xf, exact);
for (int tpr : {1, 32, 128}) {
check(tag(c, "s2_gemv_quads", tpr), c, run_s2(c, 1, tpr), c.xf, exact);
check(tag(c, "s2_gemv_q8", tpr), c, run_s2(c, 2, tpr), c.x80, exact);
check(tag(c, "s2_gemv_fast_staged", tpr), c, run_s2(c, 3, tpr), c.xf, exact);
check(tag(c, "s2_gemv_fast_unstaged", tpr), c, run_s2(c, 4, tpr), c.xf, exact);
}
}
}
// ---- golden fixture -----------------------------------------------------------------------------------------------
template <typename T> static void emit_array(const char* name, const std::vector<T>& v, bool last = false) {
std::printf("\"%s\":[", name);
for (size_t i = 0; i < v.size(); ++i) {
if constexpr (std::is_floating_point_v<T>) std::printf("%s%.9g", i ? "," : "", (double) v[i]);
else std::printf("%s%lld", i ? "," : "", (long long) v[i]);
}
std::printf("]%s", last ? "" : ",");
}
static bool g_first_case = true;
static void emit_case(const char* name, Form f, long long n_in, long long n_out, uint64_t seed, bool exact = true) {
Case c = make(f, n_in, n_out, exact, seed);
if (!g_first_case) std::printf(",\n");
g_first_case = false;
std::printf("{\"name\":\"%s\",\"exact\":%d,\"bits\":%d,\"bias\":%d,\"group\":%d,\"codebook\":%d,\"has_offset\":%d,\"n_in\":%lld,\"n_out\":%lld,", name, (int) exact, f.bits,
f.bias, f.group, f.codebook, f.has_offset, n_in, n_out);
emit_array("codes", c.codes); emit_array("scales", c.scales); emit_array("offset", c.offset); emit_array("x", c.x);
emit_array("q8k", c.q8k); emit_array("q80", c.q80);
emit_array("y_fp16", run_split(c, 64));
std::vector<float> none;
emit_array("y_q8k", (f.bits != 2 && !c.q8k.empty()) ? run_q8_split(c, true) : none);
emit_array("y_q80", (f.bits != 2) ? run_q8_split(c, false) : run_s2(c, 2, 32), true);
std::printf("}");
}
int main() {
// (bits, bias, group, codebook, has_offset)
const Form forms[] = {
{2, -1, 64, 0, 0}, // S2 (Q2_0)
{2, -2, 32, 0, 1}, // S2 with an offset
{4, -8, 32, 0, 0}, // Q4_0 style
{4, 0, 32, 0, 1}, // Q4_K style (min)
{4, 0, 32, 1, 0}, // IQ4_NL / IQ4_XS codebook
{4, -8, 16, 0, 0},
{8, -16, 32, 0, 1}, // Q5_K style
{8, -128, 32, 0, 0}, // Q8_0
{8, -16, 64, 0, 0},
};
uint64_t seed = 1;
for (const Form& f : forms)
for (long long n_in : {256LL, 768LL})
for (long long n_out : {1LL, 5LL, 13LL})
for (bool exact : {true, false}) exercise(f, n_in, n_out, exact, seed++);
// sizes that exercise the Q8_0 path alone (n_in % 32 == 0 but not % 256) and the tail of the lane stride
for (const Form& f : forms)
for (long long n_in : {64LL, 192LL, 544LL})
if (n_in % f.group == 0) for (bool exact : {true, false}) exercise(f, n_in, 9, exact, seed++);
std::fprintf(stderr, "s_gemv_legacy: %d kernel runs checked, %d failures\n", g_checks, g_fail);
std::fprintf(stderr, g_fail ? "FAIL\n" : "PASS\n");
std::printf("{\"cases\":[\n");
emit_case("s2_g64", forms[0], 128, 3, 9001);
emit_case("s4_iq4nl_g32", forms[4], 256, 3, 9002);
emit_case("s4_k_offset_g32", forms[3], 256, 3, 9003);
emit_case("s8_q5k_g32", forms[6], 256, 3, 9004);
emit_case("s8_q80_g32", forms[7], 64, 3, 9005);
// random (non-exact) inputs: the kernel outputs depend on the summation order, which s_gemv_legacy.rs models
emit_case("s4_iq4nl_random", forms[4], 768, 3, 9006, false);
emit_case("s8_q5k_random", forms[6], 768, 3, 9007, false);
emit_case("s4_k_offset_random", forms[3], 512, 3, 9008, false);
std::printf("\n]}\n");
return g_fail ? 1 : 0;
}