From ea64da89c3a26b436e53de060bfd0b4d178864ea Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Wed, 14 Oct 2026 15:43:17 +0200 Subject: [PATCH] feat(kernels): legacy S-form GEMVs (s_gemv, split, Q8_K/Q8_0, s2_*) as .cu with planner, summation-order models and host-executed oracle Co-Authored-By: Claude Sonnet 5.5 --- .../oracle/drivers/s_gemv_legacy.cpp | 274 +++++ .../oracle/fixtures/s_gemv_legacy_golden.json | 10 + crates/cuda-driver/oracle/pure_modules.txt | 1 + crates/cuda-driver/oracle/run_shim_oracles.sh | 1 + .../cuda-driver/oracle/shim/cuda_host_shim.h | 6 + crates/cuda-driver/src/lib.rs | 1 + crates/cuda-driver/src/s_gemv_legacy.cu | 951 ++++++++++++++++++ crates/cuda-driver/src/s_gemv_legacy.ptx | Bin 0 -> 114 bytes crates/cuda-driver/src/s_gemv_legacy.rs | 788 +++++++++++++++ 9 files changed, 2032 insertions(+) create mode 100644 crates/cuda-driver/oracle/drivers/s_gemv_legacy.cpp create mode 100644 crates/cuda-driver/oracle/fixtures/s_gemv_legacy_golden.json create mode 100644 crates/cuda-driver/src/s_gemv_legacy.cu create mode 100644 crates/cuda-driver/src/s_gemv_legacy.ptx create mode 100644 crates/cuda-driver/src/s_gemv_legacy.rs diff --git a/crates/cuda-driver/oracle/drivers/s_gemv_legacy.cpp b/crates/cuda-driver/oracle/drivers/s_gemv_legacy.cpp new file mode 100644 index 00000000..e42ead6a --- /dev/null +++ b/crates/cuda-driver/oracle/drivers/s_gemv_legacy.cpp @@ -0,0 +1,274 @@ +// 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 +#include +#include + +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 codes; // packed + std::vector raw_codes; // n_out * n_in + std::vector scales, offset; + std::vector x; // fp16 bits + std::vector q8k, q80; // block_q8_K (292 B / 256) and block_q8_0 (34 B / 32) + std::vector 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& a, std::vector& y, std::vector& 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& got, const std::vector& a, bool exact) { + std::vector 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 run_s_gemv(const Case& c) { + std::vector 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 run_split(const Case& c, int tpr) { + std::vector 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 run_q8k(const Case& c) { + std::vector 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 run_q8_split(const Case& c, bool q8k) { + std::vector 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 run_s2(const Case& c, int which, int tpr) { // 0 naive, 1 quads, 2 q8, 3 fast staged, 4 fast unstaged + std::vector 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 static void emit_array(const char* name, const std::vector& v, bool last = false) { + std::printf("\"%s\":[", name); + for (size_t i = 0; i < v.size(); ++i) { + if constexpr (std::is_floating_point_v) 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 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; +} diff --git a/crates/cuda-driver/oracle/fixtures/s_gemv_legacy_golden.json b/crates/cuda-driver/oracle/fixtures/s_gemv_legacy_golden.json new file mode 100644 index 00000000..495d86b1 --- /dev/null +++ b/crates/cuda-driver/oracle/fixtures/s_gemv_legacy_golden.json @@ -0,0 +1,10 @@ +{"cases":[ +{"name":"s2_g64","exact":1,"bits":2,"bias":-1,"group":64,"codebook":0,"has_offset":0,"n_in":128,"n_out":3,"codes":[20,224,3,220,130,58,21,97,221,158,8,187,97,239,78,168,255,39,219,68,168,48,186,189,103,191,222,214,92,144,11,225,0,13,232,111,229,81,188,94,7,146,117,86,18,96,185,85,193,220,156,146,28,27,30,195,247,217,104,108,150,232,185,178,150,133,138,91,70,194,79,127,78,56,207,225,108,152,244,230,189,234,190,159,64,230,226,27,61,224,225,149,97,71,215,212],"scales":[1,1,0.25,1,1,0.25],"offset":[],"x":[17920,15360,16896,18432,0,50944,48128,49664,50432,16896,16384,51200,50944,49152,17920,16896,49152,15360,49152,49152,18432,50432,17664,15360,48128,0,50944,0,50688,49664,49152,50176,51200,17664,50944,17408,50176,16896,50944,50176,16896,50176,17408,18432,18432,50944,16896,16384,16384,50688,50432,51200,0,18176,16384,50688,50176,18432,15360,50176,16384,17664,50432,16384,17664,16896,49664,49664,49664,15360,49664,48128,17664,18176,17920,49152,17664,16384,17408,50176,50944,18432,50432,49664,51200,48128,18432,51200,50176,49152,17920,0,18432,18176,17920,16384,15360,18432,0,18432,50688,17408,50176,16384,49152,50176,0,18176,49664,18432,50944,48128,17920,49664,16384,50176,17408,0,48128,0,50688,50944,17920,16384,17920,18432,17664,16896],"q8k":[],"q80":[0,52,10,242,247,9,4,248,9,11,3,253,241,8,255,243,253,4,248,15,15,3,6,254,13,246,249,246,14,2,241,247,5,9,0,48,3,255,7,252,249,12,4,6,10,8,3,12,250,253,4,10,255,249,1,13,252,255,252,8,11,254,12,244,15,1,11,248,0,56,12,254,241,14,254,250,0,15,11,245,241,7,5,1,253,12,2,13,12,7,248,251,255,0,11,4,245,2,250,1,6,12,0,52,242,252,15,12,251,7,253,251,245,15,250,249,7,247,10,7,255,245,246,4,251,4,3,14,248,4,245,14,244,8,7,254],"y_fp16":[53,54.25,6.75],"y_q8k":[],"y_q80":[44.125,47.25,-24.0625]}, +{"name":"s4_iq4nl_g32","exact":1,"bits":4,"bias":0,"group":32,"codebook":1,"has_offset":0,"n_in":256,"n_out":3,"codes":[226,32,205,248,127,140,170,158,239,253,114,130,61,212,251,181,143,174,234,29,56,125,71,64,8,96,94,64,170,153,45,104,89,188,117,255,176,180,221,78,1,105,217,53,253,141,177,227,116,155,55,155,229,0,39,16,108,118,246,20,195,159,217,137,41,145,208,12,61,98,145,33,89,157,203,241,41,181,135,20,222,145,115,13,6,188,224,144,40,190,203,16,182,177,22,48,193,83,154,44,234,145,63,0,39,212,95,199,194,13,126,211,0,96,243,8,10,73,70,157,136,179,208,14,16,146,131,85,244,40,55,211,103,65,17,122,58,176,29,148,185,148,88,235,150,125,211,149,249,121,219,210,132,155,151,136,146,71,112,255,204,90,242,27,123,154,167,153,62,50,218,51,213,234,93,41,113,15,22,230,52,35,123,96,214,149,180,2,220,96,178,73,74,181,0,32,188,124,147,64,94,99,190,93,250,60,136,188,151,63,137,253,184,54,0,83,226,62,105,16,98,33,196,188,228,249,108,199,229,61,159,252,162,218,81,198,242,55,104,194,71,203,208,212,204,120,113,63,220,109,11,1,234,74,211,197,87,169,252,22,110,218,99,208,242,38,154,225,152,40,17,172,4,116,167,42,95,230,253,34,134,53,140,34,205,10,112,48,115,11,107,188,57,169,243,187,194,116,176,94,185,203,91,251,151,156,67,227,188,131,180,34,129,50,240,158,163,48,1,251,84,23,79,170,114,228,79,20,55,26,59,106,4,105,129,114,240,124,225,168,200,77,255,192,75,14,235,139,250,43,80,6,86,219,20,56,45,208,114,44,80,171,35,141,90,236,132,244,68,11,197,38,179,68,35,68,207,170,49,238,49,184,252,145],"scales":[0.25,0.25,1,1,0.25,0.25,0.25,0.5,0.5,0.5,0.5,0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.5,1,1,1,1,0.25],"offset":[],"x":[49152,18432,50688,51200,18432,17664,16384,0,49152,50432,17408,16896,17664,17920,49664,50944,49152,17408,17408,49152,51200,50944,50688,17920,50432,49664,50432,16896,50176,17408,50688,16384,50432,50688,18176,18432,50944,50944,17664,48128,49152,50176,50688,51200,51200,50688,17664,50944,15360,50176,0,18176,16384,49152,15360,48128,15360,51200,50432,50688,17664,48128,16384,49152,0,17920,51200,50688,17920,18176,51200,18176,50688,51200,16896,17408,15360,50944,18176,17664,0,49664,17664,17408,16384,50688,50432,49152,18432,49664,0,51200,0,49664,0,0,18176,16896,17920,0,16384,0,49152,50688,0,50944,17920,49664,50688,50944,18176,16896,17920,51200,51200,50944,48128,49152,0,48128,18176,18176,18176,49152,16896,49152,0,50688,0,17920,0,17920,17920,18176,48128,15360,50176,50176,50176,18176,51200,50176,49664,18176,16384,51200,50176,50944,50944,17408,16384,48128,51200,17408,15360,51200,48128,49152,18432,50176,50176,17408,16384,16384,17664,17664,48128,51200,17408,50944,17408,18176,17408,50688,50176,17408,17664,50944,50432,17408,50176,18176,50176,49664,50432,18432,17664,17408,50944,18432,50176,50944,50432,18432,18176,18176,18432,50432,50432,51200,51200,17408,49152,18432,18432,50176,49152,17664,17408,50944,15360,49152,50944,18176,51200,16896,48128,17408,48128,50688,49152,16384,16384,50944,18432,50944,49152,51200,50176,0,50944,18176,16896,15360,17664,50176,50432,16384,15360,17920,50176,16896,16384,18176,49664,50432,17664,48128,48128,51200,49152,48128,18432,17408,49152,17664],"q8k":[0,0,128,62,250,254,244,3,4,13,252,4,246,2,2,247,244,15,5,255,251,241,244,248,1,8,12,255,242,14,0,242,8,10,5,12,7,10,250,4,1,251,242,250,6,249,241,252,0,0,13,8,244,3,11,254,12,250,242,244,1,251,6,3,5,246,0,7,242,12,5,11,14,253,248,241,10,242,245,248,3,243,243,7,246,250,0,250,248,2,244,254,2,5,243,2,254,4,9,3,242,10,8,244,1,2,251,0,242,247,245,243,242,3,251,12,242,248,7,244,252,251,242,251,13,12,244,243,245,14,14,12,244,3,11,11,7,6,15,252,12,252,9,246,6,253,246,0,9,251,6,8,249,5,249,11,248,248,15,4,245,4,2,5,7,250,255,252,0,15,10,6,244,253,2,244,14,0,250,3,248,7,255,241,245,245,8,244,245,1,253,1,243,250,251,0,250,13,252,2,254,13,251,249,248,2,244,7,252,5,251,251,11,253,15,1,244,1,251,250,246,5,11,254,12,247,254,9,248,241,14,4,255,2,6,6,246,250,243,247,7,249,13,9,12,247,243,242,245,5,255,15,10,251,244,244,10,253,253,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"q80":[0,56,248,14,255,8,12,6,245,6,11,252,253,242,14,252,1,242,242,248,245,12,244,15,244,7,255,241,14,0,15,250,243,250,0,52,11,4,6,14,255,4,4,245,6,11,5,2,244,4,15,1,3,8,10,247,3,10,6,245,0,253,243,251,245,253,9,241,0,56,242,248,252,15,244,241,247,14,0,12,243,252,0,249,253,1,5,13,15,9,255,246,241,15,11,245,12,2,3,250,243,254,0,52,250,4,3,9,9,15,245,244,0,4,5,11,11,13,249,3,4,255,253,11,9,254,254,0,11,12,250,252,246,249,247,250,0,56,2,3,253,6,241,15,11,13,252,243,5,3,248,0,9,8,5,250,248,7,6,252,249,13,1,243,11,8,249,13,242,252,0,52,244,247,246,252,255,251,10,246,6,10,251,241,12,0,252,255,15,3,1,247,12,11,10,249,8,252,5,244,244,14,12,253,0,56,15,12,11,248,252,1,255,247,4,244,1,244,11,243,2,255,252,3,10,1,243,6,247,254,6,3,244,12,243,8,245,14,0,56,253,241,241,242,252,241,12,252,241,7,246,242,248,5,247,250,10,244,3,5,252,251,14,242,4,13,9,6,247,10,12,9],"y_fp16":[1786.25,-2517.25,-3545],"y_q8k":[-119.625,754.4375,2153.5625],"y_q80":[-889,-2127.625,1327.5]}, +{"name":"s4_k_offset_g32","exact":1,"bits":4,"bias":0,"group":32,"codebook":0,"has_offset":1,"n_in":256,"n_out":3,"codes":[249,75,74,235,103,136,208,116,207,22,15,0,141,119,41,220,144,139,18,153,184,230,22,236,195,241,225,74,38,77,166,95,119,12,3,96,73,127,82,116,98,224,174,215,11,148,128,151,225,145,107,199,131,26,233,124,12,91,108,180,171,106,120,233,197,114,255,143,249,82,202,52,84,113,49,198,94,68,224,67,233,110,208,252,171,132,14,14,186,155,206,30,247,203,167,32,140,161,193,26,237,200,93,99,213,210,196,205,30,22,117,190,56,25,5,110,168,244,212,232,57,210,135,27,12,77,203,207,238,59,160,79,87,229,58,87,233,225,147,171,252,129,254,246,14,22,65,216,242,126,180,237,221,35,70,111,212,218,2,180,139,205,142,224,77,113,107,6,152,80,26,111,227,86,75,81,244,6,148,109,163,212,209,187,235,87,152,141,224,84,217,1,145,149,241,10,101,181,181,62,30,184,160,200,21,95,242,54,49,249,163,185,149,127,72,154,207,16,90,5,173,228,3,26,169,251,21,79,53,157,245,76,110,55,15,146,47,34,185,48,106,75,155,208,99,36,242,129,217,225,119,106,45,130,227,73,8,184,206,170,4,116,199,69,189,18,251,108,195,219,218,61,207,134,78,58,118,110,178,48,110,5,25,30,110,123,1,91,66,241,87,158,217,90,175,13,39,66,90,243,255,83,192,49,251,195,70,235,237,56,86,18,230,174,120,113,196,247,117,16,166,9,241,4,93,204,27,2,203,134,48,228,247,155,139,129,43,71,180,61,145,94,102,65,160,176,219,197,12,73,156,171,223,175,82,95,40,254,85,125,188,130,190,172,107,204,107,119,18,181,78,240,50,206,85,111,29,247,198,89,14,221,19,85],"scales":[0.25,0.5,1,1,0.25,0.25,0.5,1,1,0.25,0.25,1,0.25,0.25,0.25,1,1,0.5,0.25,0.5,1,0.25,0.25,0.25],"offset":[-0.5,0,0.5,0,0,-1,-0.5,0.5,-0.5,-0.5,1,-1,0.5,-0.5,1,-1,1,0.5,0,-0.5,-1,0.5,1,0],"x":[50176,50176,49664,16896,17408,51200,15360,50688,17664,49152,17408,15360,16896,49152,50176,16384,18432,17664,16896,17664,18176,50432,17920,17408,50944,0,15360,49664,50432,16896,16896,51200,49152,18432,16896,18176,49664,50688,18176,15360,49152,18176,51200,16896,17664,0,18432,17408,0,51200,0,50688,15360,15360,18432,49664,49152,0,48128,51200,51200,17920,16384,50944,49664,16384,17408,17920,17664,15360,48128,50176,50176,17664,15360,17408,50944,17408,17664,16384,17408,51200,0,15360,51200,16896,15360,18432,17408,17408,16896,50688,48128,49664,50944,18432,50944,16384,18432,51200,51200,16896,49152,50432,50944,49152,50432,17920,17920,50176,50176,17408,17664,48128,17664,50432,50176,18432,50176,50176,50176,49152,51200,49152,50688,15360,50688,48128,17920,17920,18176,17664,50176,18176,18176,49152,17408,17408,49664,17664,50944,16896,49664,0,50176,17920,18432,17664,49664,50176,49664,15360,16896,49664,49664,50944,50176,49152,50688,17664,18176,50432,16896,17920,50688,16896,18432,50944,17920,50688,49152,17408,50432,17920,16896,49664,17408,17408,0,17408,50688,17664,49664,50688,50432,50688,16896,17664,17664,17664,17920,18432,0,17920,17920,0,50944,49664,49664,50432,48128,18176,51200,16384,50432,18432,50432,48128,16896,16896,15360,50688,48128,16384,49152,49664,18432,18176,15360,17408,18432,18432,50944,51200,50688,50688,50432,16896,16896,50688,49664,49664,49152,51200,17408,50432,50176,0,18432,50688,18176,50176,48128,50944,0,16896,18432,15360,18432,50176,49152,50176,17408,17664,17664,17408],"q8k":[0,0,0,62,246,249,246,5,14,1,6,241,14,246,3,6,5,6,9,9,5,6,14,11,243,241,4,245,251,1,243,241,13,15,10,12,6,253,9,14,12,251,9,244,10,9,249,243,255,7,12,10,244,248,248,251,9,2,248,9,13,251,246,251,0,249,12,2,253,7,243,248,12,248,245,3,250,10,247,9,12,253,251,2,250,8,13,13,243,244,246,5,245,3,13,12,248,10,255,10,14,3,255,243,10,243,250,8,11,2,250,247,7,242,251,242,249,4,1,242,252,14,254,246,12,251,12,14,254,251,12,1,10,241,243,243,10,10,241,253,15,3,250,11,254,255,1,14,8,13,12,251,241,9,15,241,0,250,248,255,5,243,7,252,10,10,10,6,9,5,248,250,246,243,247,255,250,5,255,8,1,9,8,1,3,255,252,243,7,9,244,245,1,7,253,15,248,250,249,245,11,5,13,243,247,13,4,247,247,245,1,13,247,247,13,254,253,252,246,12,5,250,4,251,250,249,241,243,254,5,244,15,242,250,8,2,241,15,248,252,1,246,245,247,14,252,13,254,6,8,251,249,243,3,12,244,251,14,246,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"q80":[0,48,242,4,12,248,15,251,248,253,245,250,1,2,248,249,2,13,245,8,241,14,13,0,251,251,247,243,253,2,252,3,242,14,0,56,242,251,1,11,244,252,8,7,5,5,243,6,252,254,250,255,14,245,253,244,252,241,1,248,11,10,251,246,254,242,248,13,0,52,9,246,250,243,15,253,3,13,15,3,244,0,244,241,2,244,255,8,0,253,0,5,245,8,7,244,6,14,254,248,5,255,0,52,248,10,4,255,247,248,246,9,244,5,0,247,5,2,12,9,13,13,250,12,12,2,255,9,242,250,8,14,254,13,249,243,0,52,242,4,1,252,15,5,252,250,13,10,5,0,8,8,247,248,249,1,251,249,10,249,12,250,242,241,8,253,254,253,2,254,0,56,253,14,252,5,248,5,1,9,3,244,248,254,254,255,246,12,253,243,8,250,241,9,244,13,4,246,252,249,15,14,242,243,0,48,250,0,6,13,249,4,255,254,250,14,6,5,250,12,10,243,244,241,243,244,0,14,15,3,241,246,0,5,8,12,0,6,0,48,246,6,15,12,3,250,8,250,242,10,246,3,246,241,2,248,13,247,4,254,5,248,9,10,244,2,14,4,6,3,246,253],"y_fp16":[-194.5,-30.25,472.75],"y_q8k":[-88.625,-103.71875,29.6875],"y_q80":[-20.34375,11.40625,-146.5625]}, +{"name":"s8_q5k_g32","exact":1,"bits":8,"bias":-16,"group":32,"codebook":0,"has_offset":1,"n_in":256,"n_out":3,"codes":[96,16,215,134,23,236,141,78,80,214,244,200,166,112,42,117,0,26,128,67,252,104,239,120,109,173,43,176,7,100,242,207,210,42,217,135,122,227,165,176,151,196,31,133,86,63,105,56,223,200,99,232,116,120,196,117,194,217,18,160,48,177,53,229,117,120,140,84,130,202,210,157,146,29,121,211,215,29,250,233,19,188,102,245,51,23,136,232,122,48,154,219,64,245,11,36,222,180,70,200,255,138,163,239,177,18,69,193,10,171,104,205,44,251,191,116,99,93,35,230,115,119,54,194,232,161,121,20,162,22,131,212,190,66,162,192,22,11,147,36,4,175,7,100,96,37,164,197,86,154,27,42,115,25,18,141,201,164,2,183,196,46,187,163,14,243,188,222,32,36,61,86,171,97,124,136,59,149,104,7,82,76,59,209,249,228,85,77,88,242,112,224,167,69,191,64,183,174,40,192,127,45,95,127,219,56,197,252,212,248,16,253,200,131,195,140,138,71,206,113,60,151,25,9,129,53,146,189,135,64,212,252,198,36,80,90,2,54,196,130,122,111,112,111,30,212,8,82,136,15,57,208,52,64,170,163,72,205,189,211,74,96,74,60,119,52,25,120,68,182,68,115,233,40,193,17,234,194,163,156,127,163,237,40,115,72,114,241,38,166,47,11,80,91,26,177,156,47,114,7,29,185,137,208,245,130,187,234,180,175,23,19,246,194,30,38,132,73,41,87,106,133,144,99,107,65,197,12,207,144,56,21,127,177,82,216,194,160,45,56,58,69,252,202,194,191,145,237,169,35,58,55,247,55,30,112,114,146,37,238,211,241,102,88,118,211,102,31,112,143,26,209,44,102,167,239,213,62,215,21,192,142,137,61,56,238,4,6,66,110,83,223,159,17,143,111,70,220,156,34,79,221,76,15,114,153,83,3,145,116,3,54,157,37,192,58,140,29,51,92,61,60,22,199,242,72,215,170,15,104,98,92,141,186,114,47,235,244,202,143,152,223,231,234,242,244,24,119,61,214,189,175,78,220,199,221,102,137,109,79,236,180,203,216,106,146,181,57,172,139,142,86,28,134,205,82,171,129,158,138,237,168,171,107,102,84,188,29,250,224,129,92,147,246,19,77,231,45,212,101,196,94,132,12,177,182,170,28,212,95,254,172,137,250,88,205,129,249,47,66,42,106,190,208,123,33,170,170,248,71,190,0,124,196,23,142,158,62,222,9,244,56,207,140,185,231,73,9,37,30,170,53,126,216,71,109,119,56,189,52,54,180,166,205,23,90,234,145,175,47,107,238,242,90,87,166,208,160,136,238,99,69,241,6,153,103,42,255,218,22,63,21,11,247,160,240,20,111,249,218,133,19,172,62,118,194,215,23,64,68,122,14,90,196,100,93,205,96,93,31,24,192,114,113,107,99,185,81,240,238,69,244,100,175,142,11,201,175,85,243,136,62,58,95,51,250,239,117,40,19,52,122,231,47,240,255,254,180,145,127,100,210,255,101,139,62,76,141,53,167,224,24,166,37,114,97,168,247,113,139,10,86,95,149,222,157,50,27,213,48,114,22,42,12,47,96,254,49,167,38,231,12,96,133,9,21,179,199,1,169,165,214,51,130,92,18,152,84,158,92,152,66,234,165,57,244,219,253,219,103,156,234,81,35,218,46,0,142,78,150,152,140,137,252,82,236,120,116,87,249,11,169,59,151,228,19,140,54,180,236,43,237,161,159,11,164,57,81],"scales":[0.5,0.25,1,0.25,1,0.5,1,1,1,0.25,0.5,0.25,0.5,0.25,1,1,0.25,0.5,0.25,0.25,1,0.25,0.25,0.5],"offset":[1,0.5,-0.5,-0.5,-1,-1,-0.5,-1,-1,0.5,-0.5,0.5,-1,-0.5,-0.5,0.5,0,0,-1,1,-0.5,-1,0.5,0.5],"x":[49664,16896,17920,16896,50688,17664,0,17408,49152,50944,0,50688,17664,50944,17664,50176,0,17920,17408,50944,17408,17664,48128,49664,15360,16896,15360,16896,49664,18176,17920,50944,50432,18176,16896,17408,17664,49152,50176,50432,17408,48128,51200,17408,50176,51200,49152,51200,49152,0,49152,51200,15360,50944,18432,49152,50432,50688,50944,49664,51200,0,0,49152,50944,18176,0,16896,15360,17408,17664,48128,15360,18432,50432,51200,50688,0,16896,51200,18176,0,16896,49152,49664,17408,16896,16896,16384,48128,18176,16896,18176,50176,16384,18432,17408,17664,50944,50432,17920,18432,50432,50176,16896,49152,48128,17920,49664,50944,50944,18176,49664,15360,17408,0,50944,16384,17408,16384,49152,50176,17920,16896,16896,18432,16896,49152,16896,0,17664,51200,15360,51200,50176,18432,17408,50688,15360,15360,49152,15360,17408,49152,17664,16384,16896,16896,49152,48128,18176,49664,15360,51200,51200,49152,50176,17920,48128,50176,50944,18432,15360,15360,16896,48128,50432,50688,50944,49152,49152,15360,0,51200,15360,16384,51200,17664,50688,0,17920,18176,17408,50944,50688,50688,49664,50688,51200,16384,50688,17408,50944,18432,16896,50176,17664,0,0,49664,49664,49664,50688,50688,17408,18176,49152,48128,16896,17664,17920,18432,17408,50944,16896,49664,50432,50432,18432,15360,15360,50176,49152,15360,49664,17664,0,49152,18176,15360,17408,18176,16384,17664,50944,17408,50688,50432,50688,18432,50432,18432,49152,16896,49664,17664,50944,18176,18176,50688,51200,16384,18432,15360,48128,17408],"q8k":[0,0,0,63,247,3,13,248,242,246,7,244,245,1,9,7,8,241,255,251,3,243,12,249,4,2,253,255,4,255,248,4,15,5,4,3,9,1,12,251,247,254,245,1,3,8,11,252,241,241,0,4,253,254,250,253,12,244,252,10,5,251,14,252,8,15,4,8,249,9,241,14,253,9,2,6,1,9,4,0,248,248,249,1,2,252,4,10,247,5,243,254,249,15,243,3,247,252,9,249,13,248,13,243,2,245,8,7,4,4,9,2,6,252,9,4,5,11,246,11,244,0,8,10,241,248,245,242,1,241,2,248,251,9,245,255,253,241,253,246,247,13,245,252,252,241,249,248,251,255,248,3,241,243,6,0,8,12,14,13,246,242,7,1,253,243,246,8,0,241,246,244,241,246,243,250,246,253,3,250,11,14,251,249,12,248,0,8,3,246,246,10,15,14,254,5,248,13,6,13,10,243,252,249,245,8,249,10,12,7,250,243,247,246,8,5,242,2,254,15,251,252,247,4,0,11,4,247,243,241,246,12,252,248,247,255,243,8,4,249,251,0,4,248,248,11,1,0,249,2,248,242,244,245,3,243,6,11,254,253,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"q80":[0,56,3,246,11,14,12,252,253,3,15,8,251,251,1,241,1,249,248,7,244,245,247,12,242,7,12,2,245,248,4,5,250,248,0,52,5,247,242,250,245,250,9,254,3,250,10,15,6,9,1,7,241,10,14,247,254,1,9,246,248,15,7,249,241,3,248,10,0,48,5,248,7,4,250,254,246,255,252,10,4,253,1,251,243,242,9,243,249,246,245,255,254,250,11,0,10,0,241,3,12,255,0,56,4,11,13,2,249,249,248,250,8,8,253,14,7,244,9,9,14,255,255,245,245,12,254,8,250,14,244,4,248,242,243,11,0,52,2,245,247,243,8,253,5,247,10,0,244,10,247,241,241,3,252,250,13,246,3,3,248,248,6,241,10,248,3,241,250,245,0,56,243,0,243,249,250,254,249,249,255,247,2,14,5,2,12,253,4,2,253,243,13,7,243,250,7,14,248,4,252,11,255,242,0,48,15,255,250,13,253,253,10,13,250,255,253,247,3,251,4,252,8,244,0,15,15,8,250,241,5,3,12,8,11,0,8,252,0,52,6,255,248,243,242,9,2,9,254,0,255,7,9,3,241,252,6,12,15,252,241,254,6,1,250,250,254,3,3,0,15,7],"y_fp16":[7156.75,5915,-826.5],"y_q8k":[-18083.375,-12083,-9271.625],"y_q80":[-5109.9375,-2294,-3311.375]}, +{"name":"s8_q80_g32","exact":1,"bits":8,"bias":-128,"group":32,"codebook":0,"has_offset":0,"n_in":64,"n_out":3,"codes":[118,97,146,247,4,68,111,126,152,245,160,232,28,83,176,100,208,40,185,84,57,193,254,112,29,35,222,58,197,7,169,145,20,11,70,38,50,54,81,103,215,140,184,108,102,141,166,81,186,228,133,209,54,177,78,101,63,129,71,155,233,169,236,197,179,74,60,9,80,180,211,243,75,214,164,95,188,101,192,220,68,242,45,205,137,100,90,99,57,241,33,156,208,114,14,127,91,123,156,40,66,78,79,242,16,124,192,82,235,103,4,210,220,53,100,50,41,51,131,80,91,85,129,239,216,75,8,26,110,80,20,209,62,116,85,8,162,39,243,163,237,241,170,197,59,5,152,195,124,130,255,71,24,60,145,55,114,169,242,26,47,47,232,95,155,233,139,140,5,254,102,227,89,194,155,0,45,207,69,166,101,108,249,177,106,168,127,174,216,90,192,111],"scales":[0.5,0.5,1,0.5,1,1],"offset":[],"x":[18176,15360,15360,17664,49152,49152,18432,15360,49664,16896,18176,50176,18432,15360,50688,0,50944,49664,16896,18432,17920,50176,50688,50432,49152,17920,18176,16896,18432,16896,16384,48128,15360,50944,49152,17664,48128,16896,0,48128,18176,17664,17408,18432,50432,48128,18432,50944,0,49664,50176,50176,15360,50176,18176,50432,50688,16384,0,15360,50688,18432,48128,50432],"q8k":[],"q80":[0,56,7,247,241,245,249,0,14,253,245,250,249,15,242,254,247,0,5,14,14,253,2,254,3,0,6,3,241,250,0,253,250,254,0,48,253,246,5,245,241,1,250,0,245,243,13,253,243,250,5,244,1,247,250,255,4,248,255,246,249,250,244,242,1,8,11,248],"y_fp16":[-762.5,1557,3872],"y_q8k":[],"y_q80":[77.25,2129.5625,-1740.375]}, +{"name":"s4_iq4nl_random","exact":0,"bits":4,"bias":0,"group":32,"codebook":1,"has_offset":0,"n_in":768,"n_out":3,"codes":[45,158,194,209,64,140,82,38,97,98,150,124,141,49,163,48,198,83,154,237,87,49,166,178,5,166,185,105,139,107,18,66,193,220,238,164,3,191,193,245,133,84,30,236,39,231,224,178,40,130,22,90,126,58,61,143,253,8,175,178,51,204,88,248,171,229,125,8,63,36,71,109,87,28,146,84,253,0,251,209,250,197,248,187,138,30,55,137,158,66,216,23,219,233,57,208,222,204,52,213,196,93,215,139,225,237,33,176,18,32,122,95,178,51,43,113,129,6,142,202,93,45,189,66,208,127,133,30,188,51,237,161,56,178,200,255,232,98,56,242,180,118,205,57,38,241,156,174,254,123,64,48,197,187,82,245,170,150,184,3,232,22,35,112,130,38,135,107,230,201,218,21,31,201,22,202,126,14,78,212,241,232,225,140,26,159,37,235,254,77,45,105,133,35,180,214,114,114,249,23,80,117,119,25,153,154,50,149,16,72,241,0,76,41,254,34,104,165,45,195,126,30,191,52,229,225,63,232,70,206,8,43,161,96,7,7,197,18,191,154,211,155,171,227,55,26,86,120,194,91,189,134,231,74,21,199,236,6,37,55,230,51,195,133,255,214,253,237,68,20,247,210,255,190,51,155,189,201,1,121,5,103,209,242,34,221,197,158,158,213,75,6,185,76,193,228,52,156,56,243,161,29,242,243,250,55,80,196,78,118,77,242,214,0,239,12,53,60,225,126,124,204,22,20,239,116,159,203,70,217,30,69,194,48,121,159,188,184,254,219,28,113,189,164,207,215,169,147,65,179,78,187,75,27,30,146,40,137,31,112,240,26,112,41,156,109,255,208,124,193,219,91,225,91,250,226,39,222,135,154,117,58,154,161,78,9,222,95,187,73,105,252,184,66,171,201,31,250,183,253,226,175,180,207,247,231,4,243,74,112,80,60,26,64,19,250,233,159,210,200,97,210,236,59,10,248,117,106,87,55,148,169,215,202,92,240,80,233,17,224,60,169,98,92,134,63,231,200,16,225,135,155,232,101,13,221,59,200,61,100,140,239,85,193,144,75,103,56,126,117,157,86,250,157,101,36,92,232,191,91,246,193,127,100,34,98,152,52,71,6,172,41,241,132,86,140,232,100,152,157,154,193,26,6,73,245,174,196,41,198,36,76,48,60,252,15,215,62,157,149,85,102,249,1,40,33,165,111,165,40,153,120,97,52,128,67,200,221,217,100,168,63,110,96,178,223,114,7,96,186,83,10,17,230,255,140,125,187,42,40,64,116,3,238,51,86,254,50,10,217,45,35,144,31,112,231,207,99,90,100,172,188,57,94,64,247,185,15,152,145,109,224,113,68,130,116,5,193,200,222,93,207,92,102,243,220,238,77,254,124,251,176,13,58,225,0,96,126,233,42,78,234,198,236,65,80,180,32,111,127,104,211,247,30,130,92,176,199,73,55,65,144,177,19,187,107,69,147,214,199,202,95,64,133,70,237,25,110,41,179,127,108,191,192,245,101,69,88,2,201,160,215,31,29,218,52,112,182,149,27,209,131,213,92,139,236,146,220,160,1,84,66,245,179,181,248,45,14,195,198,16,98,224,107,10,97,147,56,124,149,22,129,134,231,57,163,25,19,74,208,17,216,216,157,70,228,133,114,93,99,136,8,61,207,166,136,58,166,80,230,161,194,118,176,195,110,115,15,253,3,139,107,228,65,61,199,108,108,163,54,44,151,167,137,217,228,57,35,226,36,51,114,151,25,117,23,110,7,182,32,122,2,51,121,194,165,220,205,197,237,217,224,22,15,190,216,110,45,148,121,247,208,131,207,89,151,29,7,5,24,99,223,254,62,28,241,4,77,46,193,160,70,230,15,99,119,167,110,165,225,154,227,96,204,102,17,151,104,187,166,30,91,131,203,193,143,112,102,36,92,177,192,198,123,14,199,49,48,242,217,65,224,192,70,161,214,8,104,197,236,180,236,25,15,169,235,121,51,133,85,235,243,212,23,142,190,142,61,166,239,173,161,2,179,57,160,144,219,149,22,171,148,243,99,12,200,162,13,169,132,1,200,147,35,249,180,215,101,7,56,129,128,47,162,244,139,4,92,67,147,58,27,211,46,72,170,21,144,232,5,30,37,166,243,197,213,98,1,67,228,66,102,153,162,88,80,14,51,208,0,79,149,231,230,112,111,235,242,43,213,123,240,146,161,151,100,92,228,133,57,8,3,108,37,109,43,164,136,85,80,9,157,91,117,209,150,35,106,56,95,237,124,152,193,44,252,227,246,146,36,153,183,224,122,168,182,83,69,247,12,42,254,186,61,204,195,10,200,87,50,210,170,243,173,52,98,190,5,87,191,32,42,248,163,42,81,114,16,187,223,145,133,200,19,220,137,25,205,138,136,215,78,148,156,203,37,161,4,102,35,80,30,0,150,239,182,232,194,154,10,172,239,90,230,184,16,245,50,39,221,74,130,52,244,171,132,188,9,184,176,143,117,38,192,126,228,154,6,144,10,246,32,38,12,151,4,128,76,91,118,79,28,84,226,163,12,78,179,215,192,174,165,112,196,179,187,52,128,91,91,127,180,195,192,15,55,6,209,190,90,0,140],"scales":[0.0174512379,0.0337569937,0.0135420393,0.00663147075,0.0299002882,0.0172126256,0.0282736383,0.0160193127,0.0139285037,0.0319827162,0.0340701379,0.011736827,0.00729292631,0.0133618591,0.00266430248,0.00317317713,0.00974604115,0.010662918,0.0131769702,0.0112183448,0.0106343171,0.01339499,0.0180516411,0.0285110418,0.0300891735,0.0198923778,0.0209169481,0.0359902643,0.0340880305,0.0366668105,0.0166152995,0.0212544911,0.0170262288,0.0101065263,0.00381606817,0.0122522758,0.0161271505,0.0133531708,0.0277520325,0.0118997544,0.00319708092,0.00942376629,0.00354415295,0.0238172617,0.00659485348,0.00581929414,0.015937211,0.011637059,0.0241788756,0.028789537,0.0293296576,0.0112016136,0.00204434874,0.0200309455,0.018876547,0.0198778622,0.0310286414,0.014116006,0.00276953331,0.00928155612,0.0164574217,0.00239436491,0.0277324673,0.0222879481,0.00674949167,0.0195513479,0.0199159067,0.00335271121,0.0230896994,0.00766444672,0.0105484985,0.0194750559],"offset":[],"x":[14631,15683,47782,48096,47638,15589,48489,45117,15435,14456,8598,14903,44509,15634,43505,15637,13488,47285,46068,15559,43163,14691,48168,48275,13777,13239,48417,14457,46438,14865,15528,15851,15505,14914,12943,14116,45697,48129,47171,14489,47272,15692,48148,15486,15301,48010,48374,47181,13672,14458,14581,48337,14907,14497,15091,44929,15321,14282,15808,46176,13828,14692,13790,14576,14722,15662,15599,48276,45265,15518,47515,14435,44992,48582,15370,15567,13062,45772,15181,15428,12300,47265,14098,46893,45540,13420,15428,48483,47898,14286,47919,48299,15762,47828,48400,48560,48004,15793,13386,14768,15797,14711,48581,15059,48328,14404,46544,47297,45864,48313,46133,14403,13137,15678,45646,47887,15350,46406,14627,47071,15809,48356,48221,48086,13871,15792,47390,47201,48584,15252,45228,14764,13627,15273,15292,14347,47683,48613,15755,48171,15348,46859,47428,15760,15524,48185,48623,13338,45498,47636,47091,14455,47360,15818,46277,46158,14843,15549,48297,45888,47253,48463,45427,48169,46612,14433,15645,13964,46856,46646,15164,46227,14471,48093,45435,47852,11791,14613,15590,46828,14496,15038,47139,15514,15140,48478,15450,48589,13890,48464,15765,14372,47812,48601,47468,48455,46350,15520,15241,47544,46388,48170,46255,13233,13602,15683,47004,47755,48072,15487,15248,14451,15816,15263,15564,48466,15167,48333,14575,46757,14749,15518,14528,14502,14356,14521,15496,48496,14676,14560,12383,46547,15747,11845,10499,46239,47994,13965,13497,15724,47034,14571,48416,12094,11434,15064,9002,48367,12733,48297,48330,47974,14969,47439,48636,15732,11211,48254,14803,47097,13985,43535,11273,46688,47034,47807,15386,46251,15707,14964,14494,14018,47207,48023,48402,48363,10157,13730,12144,47358,48265,14804,48504,48195,48549,46193,47491,48570,14760,48423,14330,48270,47135,46267,13234,47586,47735,13866,13537,14113,15055,48570,48426,15477,43665,15253,15235,15623,48369,47159,48330,41116,15684,12771,15775,45733,15742,47293,12890,46245,47622,15361,48401,14090,46057,15536,48349,46882,11606,48599,48400,13322,46513,12927,15836,45131,46520,47455,12644,44655,15697,47099,14027,14804,46202,15417,15692,48284,15668,10094,48093,48096,46173,14212,15262,15666,14144,47134,13302,15694,15776,14901,14422,47426,46472,47575,47310,12728,15184,15730,11977,13634,12289,47567,47534,46880,13975,15053,13405,14317,46759,14839,48002,14463,47228,48510,48326,47461,15518,46628,46734,48590,48143,46355,47213,43743,15736,48551,46401,48026,48544,47633,47599,48417,13323,15666,43713,45257,15709,46519,47157,47832,46456,15513,14472,47257,15254,12826,46432,47227,13045,15509,15539,48422,47532,15265,45723,45461,48467,44300,14280,13698,46119,48454,47834,15825,15014,46536,45299,15465,15602,48621,48620,11310,15630,44578,47803,47018,13932,14105,15699,15430,48418,44683,14194,14645,8589,15138,14654,15372,14837,15041,47952,13419,14770,47967,15810,48479,14799,15022,15632,15405,47289,15685,15648,13578,48141,14704,14380,47049,14609,46514,48079,13914,46700,45505,15508,48188,14958,48575,47867,15636,15396,47768,14136,13640,48450,48158,14739,13800,14615,13503,14785,48187,15185,48136,14831,15236,46760,48541,48235,45650,15559,11747,48360,14889,48064,46775,15152,45315,13847,15144,46322,13855,45945,15349,15795,48104,47952,45065,15828,46972,13688,14678,48474,15367,48174,13573,48591,46534,46521,15204,15014,11348,15815,15072,15113,15659,15814,47383,15256,47172,44926,15700,48588,45990,15846,48457,48204,14804,11584,44971,13363,48119,47817,14491,47221,9654,48175,15227,15758,43493,46224,47802,14752,15580,47650,14072,45475,47477,48450,12966,14623,15790,14879,43791,47609,12977,12984,48373,15619,15153,13779,14549,15357,48218,47689,48094,14931,13636,48525,41661,45611,45763,48131,48219,10945,45689,15799,46381,15621,48177,9938,48171,15362,14631,48153,44291,46442,15079,46094,14533,12501,11311,15108,15502,47258,14403,13594,47627,48178,47782,14484,47247,47963,14759,48152,12248,14687,47915,14731,47207,47722,10821,15774,46927,47748,47142,12959,13943,46984,47363,15507,15202,47262,13604,15309,14581,15831,48443,12950,46430,45993,15532,43648,15658,47913,15212,12620,15601,14586,11329,13984,13318,14516,15766,15729,46036,15386,47730,47450,47214,15794,15715,13930,47383,15648,48338,48464,45575,48202,47521,15479,14514,47181,15744,15404,14736,48172,47427,15492,48385,48082,48069,14461,48301,48623,48252,14443,11878,48433,47572,48276,48523,14678,45134,46213,48390,47131,47676,47695,48290,15263,12693,15812,10941,48102,45798,48187,47557,48253,47941,15827,14110,15798,15492,14326,47143,13479,15743,14357,47096,46728,48361,13665,15709,15587,46681,7256,45583,47118,48332,15510,46075,15440,48339,48331,11790,44644,46388,48625,47912,48235,15772,12744,47827,15395,46447],"q8k":[166,247,3,60,188,11,254,126,86,1,85,179,214,210,136,91,34,215,12,113,62,167,8,144,114,36,117,47,166,253,232,161,240,80,133,49,131,24,223,237,121,65,15,106,254,166,138,181,29,57,51,176,43,98,122,36,3,46,232,219,192,39,41,143,94,209,74,157,105,174,56,100,57,21,239,159,82,85,28,146,41,113,50,234,168,106,60,191,55,89,131,117,3,83,215,158,242,171,255,193,201,197,109,121,133,154,155,88,231,184,226,54,136,183,152,232,48,243,96,250,33,116,32,123,243,20,19,125,227,80,191,67,137,155,67,57,186,238,82,85,34,49,90,143,27,178,2,119,189,82,108,176,255,199,123,89,57,196,11,234,191,63,40,57,124,141,44,72,199,116,13,15,246,78,150,49,14,59,215,164,180,121,151,149,109,33,15,213,95,168,218,2,185,123,219,200,220,43,22,35,145,205,35,2,228,20,51,194,203,116,1,11,116,180,64,15,224,39,177,52,215,197,130,70,113,21,211,22,11,230,197,131,69,95,77,0,215,65,59,151,56,233,12,182,68,117,174,238,253,80,164,79,169,35,62,140,231,193,142,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,166,224,19,60,226,247,167,144,30,161,151,229,181,81,43,143,235,174,152,222,158,99,17,252,55,115,75,176,183,166,206,88,240,183,52,40,107,154,67,43,17,97,199,124,221,113,98,79,36,250,76,145,74,62,244,204,30,31,50,115,4,161,111,201,100,111,23,83,241,136,45,111,200,5,65,146,199,86,25,119,242,104,190,212,87,38,27,148,29,96,85,242,14,68,234,93,44,241,30,231,214,11,230,247,93,225,140,227,8,161,97,200,189,5,33,247,189,78,74,87,180,38,116,187,28,202,182,127,160,245,24,35,161,55,206,74,197,77,155,66,76,210,61,177,137,188,163,82,245,184,48,220,198,50,114,92,34,210,116,186,37,192,142,101,252,234,62,33,229,191,81,214,223,61,41,80,50,16,125,158,89,91,178,93,164,217,183,48,7,48,40,150,172,9,187,133,202,97,41,186,44,63,244,48,205,194,102,80,52,49,33,5,123,90,230,156,55,178,143,77,121,240,170,149,44,92,94,42,190,167,132,252,158,122,189,235,250,56,161,237,92,214,51,114,89,205,108,201,3,76,254,48,16,94,118,27,200,180,169,160,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,199,159,61,60,201,157,57,204,196,121,106,188,115,47,84,17,177,168,241,167,250,214,243,197,115,207,244,21,53,120,48,163,0,200,112,158,200,220,61,117,173,230,64,164,185,135,213,104,84,130,86,32,254,147,64,91,219,61,78,236,109,232,92,98,28,171,197,237,1,232,247,182,69,46,100,208,214,63,63,152,65,75,41,151,206,122,249,185,50,190,142,216,246,157,8,8,176,102,74,150,248,24,181,6,224,136,235,86,139,132,55,45,57,111,176,165,220,110,170,141,9,150,190,27,0,53,68,55,120,237,35,166,57,79,122,45,199,182,28,76,217,34,100,173,97,8,178,54,43,61,121,202,62,16,184,205,64,77,69,205,36,168,130,87,130,81,94,62,205,83,127,150,28,8,79,127,156,40,192,182,12,34,205,251,163,163,126,173,230,207,180,93,222,129,37,57,165,135,16,6,182,70,70,217,152,23,138,43,186,250,189,79,113,228,159,154,11,119,6,36,11,45,187,241,29,214,243,160,64,56,17,70,57,6,199,200,185,61,76,83,41,195,90,144,213,96,176,233,4,97,172,81,153,228,182,194,66,241,111,183,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"q80":[31,32,192,27,177,106,95,187,12,194,40,45,77,234,90,153,123,251,78,23,209,148,173,159,96,156,1,245,148,190,176,140,20,26,60,33,55,207,207,215,207,80,27,0,149,129,232,185,17,142,1,45,142,20,144,89,37,213,240,28,150,84,245,207,249,111,129,211,41,32,72,148,248,192,9,185,14,96,226,144,58,202,27,175,145,218,97,228,77,86,232,74,164,211,167,133,239,81,169,207,107,155,188,31,101,108,178,212,187,255,51,83,92,32,160,22,172,84,77,182,103,216,184,41,18,184,117,164,140,74,130,126,115,177,85,8,109,34,233,199,49,11,12,66,240,12,13,106,38,238,131,56,196,58,134,70,29,126,240,22,58,175,119,112,35,118,91,108,100,85,224,34,1,44,203,164,203,107,137,214,133,93,4,50,21,70,54,156,175,75,173,56,50,233,81,120,167,103,119,84,126,211,163,147,247,33,232,13,131,186,113,217,54,120,232,174,29,181,235,77,232,139,234,58,102,106,65,106,86,120,148,170,87,72,1,158,169,228,51,29,234,161,119,210,194,202,197,218,30,51,29,81,233,3,190,121,61,247,107,132,130,41,9,177,103,15,26,109,45,198,8,178,110,33,25,89,21,54,118,248,44,188,255,15,140,243,154,3,196,86,119,57,152,89,247,109,141,130,88,45,110,154,47,171,181,82,162,31,219,156,162,228,9,56,47,129,156,135,208,226,226,64,65,171,72,33,254,29,19,106,45,56,177,203,202,40,98,253,37,3,167,32,98,195,33,95,162,63,35,168,185,111,2,106,178,132,134,137,226,21,144,178,54,27,170,97,191,219,181,140,190,192,120,117,55,35,69,121,79,0,132,244,183,159,216,71,248,145,205,238,17,97,168,199,146,40,177,87,71,214,107,218,178,16,184,10,97,242,38,33,68,78,68,183,247,8,38,101,228,90,166,248,61,212,74,231,82,157,72,18,171,122,185,47,247,35,170,160,18,50,238,99,34,34,174,105,209,248,149,105,21,161,224,51,155,0,91,221,64,42,228,45,86,13,255,91,176,97,146,168,130,23,4,99,101,90,171,33,84,186,16,224,221,239,77,237,254,26,28,19,119,179,57,132,182,228,56,74,164,30,160,172,182,130,141,42,106,44,81,140,176,31,117,206,41,106,126,62,162,121,218,169,195,36,44,190,188,27,114,22,227,179,133,82,101,136,12,161,166,235,64,248,22,105,108,33,101,2,99,31,248,52,175,67,140,125,3,252,48,213,112,96,43,15,43,162,0,167,20,137,224,171,46,189,167,27,51,122,61,31,41,154,225,100,63,68,18,175,27,236,215,35,123,133,12,109,14,17,121,127,168,209,129,137,214,116,197,217,145,226,140,163,140,33,3,179,223,7,49,194,151,249,16,205,172,60,68,234,125,12,181,32,249,86,228,57,14,75,131,218,194,68,228,55,231,253,113,32,173,180,87,10,22,117,210,177,58,97,82,149,8,35,145,231,147,241,196,142,12,253,88,239,248,100,6,135,172,42,157,71,150,33,213,28,249,235,108,31,241,160,62,56,104,94,74,147,181,203,232,234,250,9,71,230,244,237,235,102,97,168,105,85,119,218,46,33,135,180,44,27,193,190,26,42,126,223,118,175,249,106,111,154,60,13,2,50,140,249,235,234,74,96,235,21,12,57,249,31,36,31,203,32,248,110,91,215,118,216,8,176,234,91,22,132,11,120,192,69,201,115,202,85,104,136,79,150,152,134,146,64,219,119,163,35,21,194,71,183,40,31,9,57,60,31,47,121,111,224,131,73,33,199,196,198,183,213,162,208,36,34,228,177,177,164,89,242],"y_fp16":[-17.1430244,60.767704,13.6037979],"y_q8k":[-28.1634521,5.01223183,9.41923332],"y_q80":[-23.9869995,-25.3567753,12.0620832]}, +{"name":"s8_q5k_random","exact":0,"bits":8,"bias":-16,"group":32,"codebook":0,"has_offset":1,"n_in":768,"n_out":3,"codes":[164,3,9,219,223,245,52,221,41,52,248,40,8,24,188,64,114,68,44,120,179,114,27,95,125,14,69,77,65,76,23,20,151,205,32,100,162,220,169,213,86,29,234,58,134,40,31,133,112,28,200,163,188,37,99,70,55,208,176,146,91,153,89,132,47,237,157,114,237,135,213,161,188,73,250,119,133,244,75,194,167,94,187,125,51,254,254,89,182,115,46,31,239,107,22,54,85,8,71,232,202,213,166,248,204,81,181,116,174,224,59,222,61,170,173,174,182,225,66,37,43,16,23,73,184,159,39,37,7,196,54,204,60,217,188,152,187,95,180,162,193,119,240,135,243,197,127,191,199,81,200,130,98,131,142,137,196,181,208,224,5,47,66,217,181,213,43,233,207,178,183,254,181,132,216,239,16,66,255,227,140,173,116,113,77,49,211,111,218,43,96,140,249,170,251,145,26,181,196,28,39,171,53,72,85,175,174,13,208,169,27,78,6,233,246,123,14,87,24,18,80,87,89,99,42,78,28,207,60,3,216,205,15,82,114,171,92,145,230,96,79,32,156,4,213,198,207,149,140,204,218,2,109,187,25,184,166,218,133,211,215,100,124,34,200,98,22,245,18,78,140,45,247,152,82,41,111,139,217,129,119,79,178,182,66,230,245,116,238,24,170,218,11,145,81,111,216,207,64,70,105,178,172,68,110,112,211,212,225,64,77,173,220,79,170,130,10,196,232,28,39,202,138,40,96,203,197,85,212,212,29,64,155,196,55,61,0,164,167,94,10,97,65,149,45,50,20,3,4,128,178,78,193,15,37,160,60,220,219,100,177,118,250,185,71,228,103,60,143,113,2,53,89,31,181,165,211,32,224,19,212,212,129,195,171,77,115,207,229,138,112,60,139,146,219,235,11,97,164,48,0,130,250,43,9,22,20,152,53,76,109,58,93,233,16,208,250,155,210,129,251,145,253,44,250,98,146,38,165,147,234,150,37,148,55,72,222,129,56,91,73,187,225,157,30,174,83,106,25,170,3,206,119,30,224,86,215,138,78,2,142,117,171,183,45,166,45,28,100,251,243,109,162,144,253,112,17,176,151,160,118,47,154,176,38,198,31,78,142,156,182,12,23,188,58,108,144,156,207,141,169,178,15,190,250,209,114,232,6,114,44,164,174,201,245,17,168,255,172,59,236,40,125,252,7,114,171,95,122,106,194,44,126,37,216,22,63,182,183,172,129,74,131,70,58,106,176,91,234,29,43,123,101,60,114,100,182,12,55,200,189,191,23,2,46,87,29,239,35,107,94,196,22,181,217,250,156,70,155,140,39,196,56,189,234,118,28,222,173,7,39,179,57,40,122,230,194,141,184,217,39,94,229,10,72,53,89,83,15,52,46,197,99,230,44,172,46,134,203,195,191,255,50,190,203,194,236,71,183,133,144,93,37,5,18,31,230,165,195,10,174,156,126,91,104,60,95,6,122,60,44,85,171,70,105,91,186,92,244,132,66,48,32,92,102,43,194,215,227,23,142,122,39,222,179,104,66,60,239,69,245,14,98,119,52,2,149,145,164,34,89,184,8,233,125,237,35,181,65,45,185,40,144,166,228,60,254,223,61,149,8,186,115,34,152,218,194,192,33,61,237,197,81,14,92,48,216,196,157,235,29,179,69,221,34,180,186,68,11,22,100,38,164,50,193,214,21,174,123,146,126,209,165,40,59,145,253,226,133,16,162,67,126,133,33,251,197,246,21,181,246,225,122,120,209,22,114,204,214,210,230,166,4,153,255,131,209,125,83,241,128,219,159,172,249,181,131,222,71,97,167,237,196,24,122,183,122,9,48,110,157,139,0,93,95,121,54,18,222,15,43,162,113,237,43,81,227,64,154,213,101,151,144,206,85,178,147,43,167,14,131,252,137,172,224,232,109,48,253,57,115,216,244,62,51,42,19,232,44,129,108,249,248,99,18,41,216,78,165,48,68,39,32,19,45,80,209,95,164,21,110,49,156,140,127,82,214,120,232,117,173,29,47,133,144,188,185,168,195,73,61,176,103,115,185,180,32,4,239,89,251,94,115,45,149,85,55,100,37,26,15,154,1,116,44,179,160,67,8,51,70,78,20,27,20,212,69,209,22,122,68,7,74,219,181,6,128,162,224,46,175,152,72,91,218,153,22,16,33,75,171,222,64,102,12,227,131,12,211,82,116,189,102,203,52,60,9,101,150,122,216,85,69,24,191,104,213,187,236,91,184,80,27,78,182,135,170,113,225,97,209,163,100,223,145,82,135,206,95,102,228,74,205,165,4,69,153,230,206,25,82,214,86,1,215,166,195,173,23,160,156,188,220,181,107,43,75,45,36,193,41,202,117,58,255,198,217,175,0,186,185,117,113,39,101,187,4,235,22,95,154,182,33,72,13,144,125,211,119,236,101,225,80,151,18,252,253,122,133,169,175,79,242,3,83,173,103,143,166,240,220,203,149,220,38,155,199,86,149,39,250,179,253,224,153,72,111,26,221,82,35,174,2,224,19,216,190,88,27,189,67,193,61,81,5,25,109,76,1,207,197,66,214,85,99,18,154,113,229,206,255,231,43,62,128,68,78,223,149,4,41,188,23,134,79,149,124,201,7,29,20,112,208,59,196,238,180,172,31,236,162,77,72,151,252,66,233,49,60,73,177,82,60,180,189,243,138,18,159,150,30,152,12,229,59,214,86,251,24,139,30,126,100,153,221,184,219,238,195,11,51,124,65,1,177,131,95,44,189,38,36,245,54,48,109,26,228,183,143,241,167,113,31,5,170,212,99,86,123,29,105,40,30,132,165,198,181,64,96,47,131,171,77,23,241,227,137,196,23,18,3,90,206,198,44,28,106,16,185,108,2,55,147,158,209,7,204,165,240,55,139,192,58,0,230,106,90,89,70,106,36,218,115,108,55,218,61,70,69,160,54,151,140,143,106,136,219,180,18,203,164,107,192,204,117,159,195,143,163,137,110,237,113,116,236,34,39,168,35,153,62,232,107,88,53,69,14,208,223,52,154,230,86,155,30,90,243,44,188,208,110,215,125,147,17,235,96,211,107,17,249,227,238,177,85,196,151,169,167,120,102,155,46,255,242,192,182,236,21,6,40,31,147,179,236,142,217,153,199,80,184,164,14,86,57,246,38,196,214,25,242,102,54,89,153,211,112,12,177,109,207,90,221,137,95,193,253,8,245,94,184,120,146,218,83,176,130,56,121,203,30,97,77,168,38,172,136,171,248,113,3,153,32,177,175,51,96,114,12,166,253,83,140,83,175,218,73,229,98,76,22,14,174,160,67,64,115,143,228,180,136,154,148,229,8,3,102,211,168,41,89,152,136,243,243,173,222,184,107,71,219,177,39,64,91,169,156,108,150,45,175,221,25,116,238,233,251,71,65,165,143,222,85,30,68,212,25,43,107,55,30,45,16,92,254,199,146,222,230,112,150,209,99,41,69,42,109,73,213,133,241,233,250,58,112,145,139,211,62,229,7,10,30,33,118,11,74,121,226,151,134,223,227,88,206,142,106,167,231,28,81,103,21,217,34,59,102,240,117,199,210,132,38,150,103,132,139,23,219,210,254,2,66,97,164,31,108,73,78,151,219,218,0,70,57,34,213,85,12,225,110,179,24,211,235,46,167,186,13,194,216,176,66,131,81,1,144,122,75,189,74,233,33,189,183,196,82,224,63,20,155,67,77,115,61,222,139,221,41,211,170,13,129,138,186,146,227,242,68,156,96,197,231,208,17,178,141,211,186,18,43,57,129,49,233,243,248,138,47,157,66,178,58,3,109,38,163,35,51,217,19,18,106,146,8,65,55,190,72,77,177,152,205,95,169,141,42,218,166,169,93,33,60,183,114,142,63,219,117,129,20,153,98,172,76,3,143,250,122,230,87,0,82,168,1,27,77,10,162,190,232,156,220,14,200,232,23,199,239,62,101,177,249,149,117,244,235,125,128,251,204,157,70,68,27,201,21,154,63,178,161,215,190,99,21,195,228,156,185,199,123,102,106,84,134,130,204,25,139,68,166,142,87,167,177,8,222,78,11,200,185,243,202,43,38,60,0,150,80,160,223,249,45,159,196,7,157,168,243,102,171,86,187,198,40,132,188,101,74,74,97,236,169,35,38,68,57,187,129,147,116,227,197,232,84,162,253,66,235,176,215,46,100,193,8,125,95,92,6,174,220,13,81,202,123,143,189,92,199,95,81,196,209,158,138,146,127,84,116,191,241,234,157,65,68,126,144,216,218,101,29,2,104,27,157,70,107,0,167,18,114,176,99,236,237,253,234,59,64,248,92,84,151,26,246,218,172,201,175,73,98,202,118,91,11,167,204,130,79,196,164,156,81,214,200,201,192,73,113,187,100,237,68,87,54,188,247,17,173,252,68,12,88,27,226,35,176,139,247,129,229,55,46,114,7,179,20,215,161,143,46,78,177,229,28,129,35,209,102,206,99,230,82,44,228,46,125,64,9,190,6,72,160,208,18,132,98,119,69,144,178,202,237,234,37,157,186,75,192,36,153,55,215,162,40,2,159,142,44,169,14,82,125,127,242,164,129,234,122,194,36,43,206,142,229,11,175,27,223,211,8,23,195,161,86,247,6,53,193,96,139,116,248,236,187,145,32,116,168,22,75,52,47,224,247,45,246,7,114,94,186,58,153,235,206,4,82,40,62,62,209,206,101,106,191,184,194,78,136,50,194,129,212,141,247,169,66,99,83,220,124,160,239,135,67,136,174,234,190,85,222,103,204,241,169,22,23,233,122,10,112,13,175,225,178,65,15,128,66,126,195,134,6,10,29,4,24,198,54,223,234,79,210,75,216,54,221,86,238,174,119,65,85,248,12,214,64,148,103,35,249,8,76,203,49,239,224,39,244,53,109,101,226,242,138,218,109,77,23,109,103,171,64,182,182,181,192,30,49,50,55,83,66,238,109,141,66,130,89,234,140,107,99,96,73,0,32,216,4,139,109,146,90,242,16,22,100,93,24,67,175,71,99,151,190,41,210,159,97,21,47,178,253,197,135,15,181,118,249,221,68,218,238,5,75,128,31,22,73,104,91,208,248,27,93,121,69,236,138,16,211,202,27,173,212,184,123,116,146,107,240,216,173,29,21,155,240,47,99,21,186,55,219,254,120,39,144,78,203,70,185,0,184,212,228,181,161,149,105,75,119,98,245,17],"scales":[0.0205654949,0.0115850009,0.0303557143,0.0134876985,0.0334748887,0.0183063205,0.00376429199,0.00716433488,0.0246303324,0.0201560277,0.0230891462,0.0229169894,0.0101607023,0.0226191748,0.0145512577,0.00964369252,0.0226114672,0.0333654359,0.00728042703,0.0104846116,0.00912133045,0.0055358219,0.00739611965,0.0368672088,0.0211099572,0.0204524137,0.0198136996,0.00458139507,0.0280537717,0.0130244587,0.0377138592,0.0328860506,0.0345598496,0.0256789234,0.0365683846,0.0211914722,0.0347925574,0.00642839167,0.0232417025,0.00481326552,0.0169770811,0.0236987062,0.0375207737,0.013060689,0.0213967394,0.0222511925,0.0303350054,0.00614241697,0.0365095101,0.00482750637,0.0257725194,0.0311304517,0.012145794,0.0356364846,0.00897979084,0.0173053816,0.0031257486,0.0333431996,0.00425740331,0.0177645963,0.00884776842,0.0378229246,0.0137713049,0.0272410009,0.0249871016,0.0239926279,0.018254891,0.00243424042,0.0114804078,0.0171943493,0.0256438423,0.0101242187],"offset":[0.0202926043,0.0094255805,0.00609489111,-0.0246420708,0.0271565504,-0.0277623963,0.00489935884,0.031857349,-0.0297210757,-0.0490993671,-0.0350220352,0.043470826,0.0195392724,0.0133855883,-0.0219636951,-0.0195476767,0.0420922041,0.0237738546,0.0127473651,0.035712,0.0399087034,-0.0131679866,-0.0405286103,-0.020179471,0.0409559086,0.0172972512,0.0067506074,0.0267112087,-0.00595159549,0.0314700678,-0.0377798527,-0.0357288718,-0.00926128682,-0.048845619,-0.0348824486,0.0150132058,0.017837083,0.0181090888,0.0114949411,-0.0408724919,0.00149098039,-0.0350990631,0.0055788639,0.0448805578,0.00247459416,-0.0358310752,-0.00145534275,0.030870676,0.0464594178,-0.00466457894,-0.0043084682,-0.0303432774,0.0346172713,0.00273957849,0.0324582122,0.0113620525,-0.0349523239,-0.00583038339,0.00190214522,-0.0443400815,0.0360836871,-0.0339306891,0.00102490187,-0.00374789233,0.019367082,0.028932035,0.0235857908,0.00141608121,0.0369050093,0.0348899774,0.0314556956,-0.0240002256],"x":[14394,41379,15204,13511,13329,14157,15670,44710,48524,14987,47673,46037,46309,14913,46268,13851,46263,46144,48398,10748,14997,48020,47194,14529,48303,47356,15536,46910,15612,13906,48356,8093,48031,48011,14449,48601,14291,14673,14022,15503,42599,47211,47214,47392,42099,15834,11289,46538,46546,13540,13975,47228,14810,48325,13419,15481,14676,14465,47711,12535,48167,48138,15604,48495,48214,14716,15400,15811,14559,14937,13845,12671,13441,44359,48337,48239,48163,48323,47737,12573,15718,15789,45518,15806,47518,10942,47440,47681,14823,15616,15390,47385,13157,47361,48383,45310,12199,47965,48261,13240,48343,47659,15511,48258,45720,47771,48316,47209,43944,15109,47966,13408,48621,12597,48443,15216,47916,15466,48228,10783,46883,47778,48510,15566,14503,48583,48094,15111,14371,48224,14911,43419,48086,13595,47320,9271,14116,13830,14279,45632,13997,45539,12230,48316,13478,47522,48308,48031,46055,48505,15129,46855,48276,48538,48455,46877,46347,47919,47956,12033,48459,14542,7408,14897,7650,15628,15363,15762,48603,48480,15188,14680,47283,15503,15781,47783,14718,13015,13035,13426,46383,47829,48254,48107,48478,15604,14509,14803,13335,11111,46312,13254,14669,48477,47506,12341,15086,48266,48582,46351,48567,15507,15437,15364,11830,48408,47777,10593,46647,45897,13352,15648,42949,44110,48525,48580,48295,45321,44555,14136,14837,13406,14659,47247,44117,48165,13842,14932,15687,15743,47537,15415,14448,15451,15069,15400,47273,48235,48225,47358,15788,46828,15597,47792,47638,13743,14790,15369,15518,48492,14363,15271,13510,48490,15550,44328,13381,15516,46840,14883,47532,15664,12113,48528,15817,47635,14465,47825,14859,14438,48153,47172,47840,48208,48569,48439,47993,48299,14561,48048,46944,48488,47575,14363,13751,11289,47930,48214,47547,13768,48299,14996,15333,15675,13797,47231,15140,13505,14220,45390,9528,47483,13416,46636,14528,48282,14349,15410,13416,46432,13660,11299,12958,15580,14800,48124,10417,14231,45656,46504,46821,13914,12846,47001,14196,14945,45960,15055,14390,47507,14729,47439,14897,48168,14362,11675,15816,48478,46738,48051,48526,9233,47872,48148,15057,15468,48444,14655,11061,14503,48452,15705,48277,15822,47275,14279,14519,47034,15033,48337,47636,13494,14976,12481,48005,15490,47364,15485,12943,46612,14074,48409,14742,6478,48247,48623,46290,47596,15060,48150,48623,10469,47697,15164,15144,48183,45249,13974,42937,48083,14922,15418,48340,47557,15510,48301,46846,15813,47332,15210,46088,13464,14981,15766,47388,48451,14545,14992,46775,47999,48320,47741,15566,14968,48514,46599,15054,48218,48453,41125,15507,13290,11340,15228,43346,45382,47172,15311,48584,46569,48274,47422,48594,48595,48439,48629,15783,12517,48039,12372,14342,14815,15207,12601,47427,46960,47164,48259,48177,14405,46886,13914,46356,48594,14579,14388,12491,47838,15474,48039,45837,48088,15845,48314,47659,15751,47318,15558,44118,48583,15744,48069,15682,46298,13551,15203,15266,47583,46955,47574,12213,15402,48287,15683,13554,48229,14679,14371,9923,15554,47660,15372,15371,47929,48554,14812,15787,46475,47604,47870,15100,42962,48514,14675,12089,15773,47858,12134,14962,48310,15133,12110,15247,12187,15547,15223,47478,45760,47412,48497,15443,47675,46501,47320,47170,15735,48005,48162,15176,48437,48598,15786,15856,14302,44458,15262,48174,11550,48065,48412,15407,47399,14946,42327,47986,47499,48228,15552,48420,13356,47499,47304,14401,15481,47137,15517,46684,46263,45884,14653,47222,45946,13415,11759,14533,48429,47424,15513,12729,47704,7888,15793,13116,47473,15555,14735,47060,48542,15561,15083,47154,47382,47443,48390,15425,14348,15461,47335,15293,14948,47112,14672,15458,14434,48387,13571,47647,15153,47578,48484,15706,13557,48521,14421,11950,15458,47912,14570,45467,47154,14542,48394,12525,15089,48245,14313,11346,14904,47383,47268,43244,44325,48357,13000,47499,48251,15572,15575,15685,48181,15679,15517,47606,46201,48576,47162,12571,43879,15626,15552,47560,15549,13159,41128,45212,47077,48221,47633,15314,48085,47188,41057,15560,47217,15286,13603,11428,48267,9781,15851,47370,14854,47818,47639,15480,47147,48169,48414,15455,46255,47325,47492,15670,46269,14985,47147,12489,46601,48172,13275,15522,10381,14364,45963,15402,48175,48148,47201,46980,15490,12316,14005,13193,47324,47182,48371,45579,47318,46570,15778,48240,44611,47213,14893,14412,15632,47870,15643,15360,10461,48210,15861,48628,12404,14034,47747,14645,14558,45235,14734,46995,48260,10481,9837,48463,44920,48387,44360,45218,14403,14703,15453,12474,13444,48166,13541,46822,48399,46018,15213,12593,47292,42042,46083,48569,47251,15396,39980,15650,46159,15553,15642,11657,46690,15473,15499,14421,47153,48116,15237,47486,41993,14057,15562,47373,42747,12804,13880],"q8k":[230,216,53,60,199,115,215,27,159,11,17,47,240,158,217,148,114,26,114,120,187,222,23,107,181,33,248,145,18,114,203,38,174,198,17,36,111,76,147,137,7,117,168,64,75,218,3,33,179,193,123,98,182,225,157,204,130,227,33,28,50,115,70,69,97,249,247,85,101,84,178,83,76,196,42,51,218,145,222,217,54,208,169,146,210,253,52,136,156,23,215,63,169,164,208,160,39,99,151,65,201,159,31,220,76,122,181,241,111,70,135,141,250,188,125,179,92,50,255,152,129,48,188,88,94,52,173,250,240,158,7,217,129,247,208,91,13,115,182,84,219,88,132,215,29,255,56,239,220,255,244,75,151,143,164,210,29,157,186,82,50,26,241,215,185,247,147,218,215,242,61,31,77,4,203,120,98,40,215,124,25,176,51,209,247,129,237,106,242,90,73,31,133,195,13,83,15,247,255,138,70,88,165,37,252,185,201,241,22,194,15,49,40,134,119,169,220,24,176,181,241,170,205,80,183,52,116,163,225,235,196,75,218,53,213,234,218,112,155,229,99,163,149,117,147,14,32,77,163,100,6,122,8,50,152,35,142,109,119,108,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,137,202,114,60,160,122,155,220,55,127,73,141,91,54,155,202,119,92,202,206,73,115,130,130,122,66,195,108,195,53,150,242,139,124,143,129,155,214,26,78,240,39,108,164,250,108,148,157,213,176,177,151,4,106,105,129,230,108,111,228,198,226,200,74,131,211,155,161,180,27,136,215,151,96,91,217,69,199,249,60,28,113,69,191,7,97,55,16,220,29,201,206,193,116,206,60,158,164,233,61,184,49,36,38,28,112,184,72,167,32,141,9,241,86,136,43,66,17,33,189,127,113,121,6,108,208,206,184,3,216,252,226,164,180,26,83,58,49,215,195,9,139,17,100,179,192,117,82,112,168,34,202,92,20,145,3,175,78,237,231,131,181,114,46,97,233,242,169,104,96,218,76,144,1,66,188,121,159,244,196,197,164,92,84,209,68,245,3,178,228,26,45,253,7,108,25,244,107,190,143,24,227,42,38,39,220,205,208,251,74,183,34,69,48,49,23,36,133,200,91,146,224,147,236,213,112,28,68,219,193,52,108,11,117,198,181,155,225,253,206,193,108,87,18,2,122,229,197,142,187,205,165,188,3,30,225,30,152,230,19,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,158,31,9,60,171,199,15,23,190,38,33,14,255,221,175,78,236,36,91,202,114,239,145,214,99,30,224,12,231,227,24,214,67,229,16,74,36,115,0,189,196,199,161,192,189,85,77,85,145,82,28,227,151,188,124,184,64,0,117,81,61,142,166,254,81,139,201,178,18,215,18,179,224,187,102,3,48,12,14,133,110,37,47,130,149,219,209,166,83,50,232,123,166,5,60,26,70,136,177,56,127,19,96,157,123,163,142,83,255,48,45,13,174,115,225,83,216,225,163,225,114,176,132,18,189,72,104,177,207,65,104,251,250,95,105,134,169,112,236,157,198,188,207,135,44,245,186,152,236,45,215,181,196,97,99,176,55,65,91,72,121,165,61,186,9,205,28,121,219,57,46,118,152,50,47,163,29,35,222,157,107,153,40,198,120,46,68,176,81,96,245,146,145,100,166,179,21,55,51,11,3,204,105,207,183,230,131,47,104,204,204,174,82,48,217,204,218,211,56,30,244,41,247,119,184,202,175,78,173,144,255,1,163,146,206,221,60,238,148,88,33,191,136,8,217,9,2,191,220,156,152,109,46,219,72,7,131,192,188,62,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"q80":[228,32,245,232,112,254,207,43,3,206,158,212,218,226,134,190,51,190,243,146,193,72,156,84,248,137,255,225,106,42,84,87,134,228,57,33,98,148,7,236,106,69,192,129,200,75,236,163,211,74,94,229,171,16,180,215,25,13,168,63,204,239,200,159,111,68,214,201,173,32,245,225,126,123,239,171,44,254,17,12,54,220,176,225,82,160,107,171,136,49,46,4,10,83,142,111,63,180,78,119,171,67,129,32,84,130,46,155,160,22,151,60,124,212,144,234,191,79,162,98,154,133,132,35,60,244,98,67,1,81,59,211,52,65,175,120,117,31,111,84,230,40,248,58,228,37,60,107,252,134,217,219,178,74,47,148,36,93,79,3,29,120,48,217,184,173,215,241,248,161,119,32,73,215,108,210,98,165,157,171,127,242,228,106,185,173,26,70,15,184,108,45,113,85,62,196,48,222,194,125,249,118,152,238,97,32,161,100,157,80,32,105,143,144,133,22,46,88,75,13,28,6,87,233,154,173,38,155,125,24,179,149,232,201,200,250,222,4,9,35,116,209,155,175,195,145,30,81,82,242,169,141,175,141,205,102,131,33,154,251,136,73,116,216,134,253,138,73,120,140,136,109,78,30,38,134,199,198,144,197,79,216,85,208,148,209,241,162,103,117,141,6,140,153,202,91,125,207,38,246,171,20,54,76,101,148,217,31,195,21,221,234,210,1,244,163,122,29,80,229,47,191,113,152,110,63,229,96,176,222,250,61,6,188,53,109,226,5,36,13,200,29,27,27,221,143,165,254,57,98,57,153,3,100,156,87,99,103,109,211,58,130,125,48,83,254,221,196,103,224,134,197,91,245,45,29,143,115,51,16,8,8,13,143,109,5,255,184,200,70,185,200,169,238,145,147,189,33,31,126,5,93,98,62,182,166,247,116,1,35,50,185,90,101,138,35,236,164,54,226,14,212,98,68,244,176,13,15,99,215,64,81,97,5,192,15,17,171,182,121,228,37,57,32,94,241,254,48,113,201,125,11,37,71,24,69,203,40,135,105,232,206,88,158,16,72,244,182,25,150,224,104,148,123,19,11,28,32,88,215,144,51,21,4,178,200,210,238,219,16,145,39,106,55,32,224,120,219,40,127,249,117,226,216,85,156,40,12,56,71,18,35,70,40,169,148,95,153,211,178,247,216,176,31,19,45,226,7,253,76,94,119,133,121,17,4,126,126,119,33,222,225,85,150,146,34,226,130,118,24,127,213,2,78,122,228,120,196,236,206,220,153,214,151,78,167,109,239,222,228,23,19,16,61,67,187,43,26,26,34,83,225,87,234,179,217,93,9,123,63,103,57,4,99,252,182,109,186,214,88,35,93,23,188,2,198,222,240,28,111,216,193,118,32,142,57,3,241,197,138,28,68,164,69,238,228,36,7,175,80,57,27,2,98,97,62,211,243,208,196,112,201,237,5,118,124,161,35,56,222,93,234,175,175,205,179,198,78,194,125,8,178,176,90,25,77,28,105,129,44,68,179,192,160,191,69,149,76,207,60,135,33,124,59,132,179,129,154,228,232,251,110,189,234,78,250,253,172,136,220,48,150,248,170,242,138,242,134,1,7,147,231,247,189,44,31,5,105,120,248,3,218,203,112,6,35,227,218,66,232,183,65,166,190,83,169,184,112,142,58,129,149,202,162,125,237,196,221,217,30,151,71,107,120,145,222,16,98,22,92,249,89,164,220,135,101,168,11,138,75,227,187,110,20,169,182,231,66,63,94,176,224,224,33,248,98,240,64,50,249,216,81,168,81,5,40,155,30,0,61,9,206,106,2,91,75,255,180,146,210,196,98,44,78,59,221],"y_fp16":[-55.4058189,-39.2211151,-39.1265373],"y_q8k":[-55.9825783,-122.863602,-125.422318],"y_q80":[69.9177322,-47.996788,44.2066994]}, +{"name":"s4_k_offset_random","exact":0,"bits":4,"bias":0,"group":32,"codebook":0,"has_offset":1,"n_in":512,"n_out":3,"codes":[74,213,220,198,49,148,190,242,34,149,175,121,61,104,255,109,233,45,250,197,102,19,102,252,139,202,239,125,115,212,20,79,253,109,27,134,37,53,202,65,72,66,184,63,68,20,143,26,242,141,158,178,186,95,207,71,77,194,140,241,227,98,151,183,228,151,187,15,184,20,154,131,94,211,141,13,104,45,189,63,0,95,194,138,211,192,83,118,193,28,223,18,110,13,75,191,100,121,113,178,185,188,35,49,78,234,10,188,202,49,119,20,3,165,78,46,204,109,170,81,192,90,44,107,168,214,246,243,128,56,176,151,24,250,27,185,54,195,70,49,58,110,7,97,246,67,137,51,241,101,2,88,86,219,160,180,62,190,236,157,118,253,76,42,6,244,15,68,186,20,90,158,60,176,226,11,117,13,90,194,208,75,172,3,37,21,221,207,23,51,91,248,18,210,150,185,212,245,44,242,225,254,75,255,240,193,8,138,84,220,54,138,23,170,124,145,35,105,15,173,244,148,45,1,110,229,144,249,7,142,180,187,41,43,81,175,63,247,83,117,41,138,17,235,101,115,72,29,205,82,150,72,93,218,70,193,63,53,202,64,17,103,123,80,101,190,175,227,155,138,106,245,100,195,113,203,221,203,139,149,213,247,11,233,69,191,247,180,59,193,51,187,10,157,40,137,237,40,123,61,29,62,190,127,147,132,102,196,143,224,146,210,128,231,223,179,134,169,218,199,0,175,122,201,166,52,55,167,78,176,247,59,169,164,108,189,18,95,133,228,206,131,156,150,138,59,135,23,101,255,180,5,77,194,156,207,46,214,245,34,185,201,151,119,206,29,188,197,24,20,238,253,240,111,93,57,211,104,178,112,15,115,216,42,125,35,54,100,218,3,83,60,181,97,154,182,127,168,31,48,219,106,48,85,201,130,172,60,2,124,245,118,44,199,162,241,232,171,149,22,37,193,52,222,32,46,84,236,190,241,132,198,110,6,189,247,0,33,225,216,241,254,253,175,42,106,200,183,226,85,114,211,169,27,49,248,123,226,81,247,210,162,3,191,193,129,18,81,173,216,107,14,224,194,107,218,20,76,93,8,207,240,9,49,51,86,195,84,91,55,4,225,12,230,17,131,255,151,172,165,199,55,41,200,148,83,252,228,169,102,175,209,141,107,19,121,192,40,24,131,253,244,214,225,40,130,93,11,180,196,58,171,169,55,29,231,40,167,129,198,55,36,55,141,90,152,23,102,126,48,17,16,242,120,90,57,152,91,210,143,214,182,38,172,190,5,190,4,42,1,193,148,225,238,25,154,14,76,205,91,125,180,160,58,79,189,39,74,138,166,228,152,249,241,76,169,163,4,149,146,112,89,174,96,100,169,67,53,144,93,173,107,77,189,61,29,95,7,237,140,195,147,4,47,191,22,54,51,35,207,222,234,146,246,255,135,23,43,96,170,181,116,172,17,129,221,66,202,3,66,35,137,179,18,46,124,135,140,206,115,62,54,137,78,49,183,176,82,249,198,36,149,62,106,78,169,10,188,220,28,128,185,181,227,130,144,89,57,235,201,139,35,226,111,89,153,174,211,216,86,196,218,129,37,151,253,136,229,158,69,234,195,128,28,58,121,1,106,22,95,20,104,150,179,114,133,243,219,84,32,83,21,217,23,242,199,126,203,28,74,97,189,49,91,19,201,140,23,154,168,52,55,89,181,117,247,109,44,191,177,129,250,53,60,67,89,13,39],"scales":[0.00440568337,0.0296568815,0.0175618697,0.0346169733,0.00925953127,0.0306548346,0.0132710375,0.0334828906,0.00310439826,0.00254503125,0.0341865234,0.0346761867,0.00351371872,0.0341027379,0.00905946083,0.0346014984,0.00684815506,0.0304719862,0.0115522342,0.00813129172,0.0266348757,0.0174158029,0.0158417076,0.00502272695,0.0254651308,0.0299962759,0.0250909626,0.0106256437,0.0346836336,0.024996547,0.0294395946,0.0116262836,0.00641700393,0.00361038442,0.0342789702,0.0146192666,0.0309451222,0.0290225539,0.00459104404,0.0187386703,0.0371912904,0.0175752379,0.013122391,0.00713514537,0.00600957032,0.016961813,0.00680225249,0.0121299801],"offset":[-0.0170934349,-0.0394665636,-0.0334420279,-0.00315506454,0.0375308767,-0.00988394674,0.0238279048,0.0109604662,-0.0228836518,0.00465676794,0.0010835469,-0.0366700105,0.000311690586,0.0165279992,0.000144565114,0.0288775098,0.0358648375,-0.0319820009,-0.0361972526,0.0164785739,0.0150438314,-0.0254135374,0.00876392145,0.0413342677,0.0394040123,-0.00235178182,0.00892770942,-0.0451882146,0.0161184855,0.0163234416,0.0424174666,-0.0433133058,-0.00981804449,0.0286095329,0.032449048,0.0391803645,-0.0142682856,0.0275897738,0.0220131278,-0.0234763268,0.036254108,-0.0284200907,-0.0457094237,0.0125043755,-0.0102602988,-0.0060833185,-0.043266207,0.0101170484],"x":[48273,12237,48579,14871,47011,46854,13227,15669,47376,13331,13478,15211,48355,45050,47930,47367,47328,15634,45324,44528,12565,46983,48631,46977,13602,14947,15827,48330,47293,48185,43466,13474,47582,46702,15387,15598,45562,13341,15791,46112,14098,12443,12309,47492,48266,8662,14674,48157,15449,15613,47860,15516,44235,10639,11874,48131,14118,47461,48617,15724,47834,48311,45930,48497,14911,15778,14991,46101,15582,14955,15353,15291,12462,15817,12606,14779,15251,14883,15611,48273,48549,45568,12521,48430,48266,15054,15227,48267,15082,48364,15835,48425,48493,46660,48434,14558,47420,48474,14499,46509,15848,15524,47608,45432,15095,15756,47409,47854,47736,14802,14683,47787,48378,13843,48571,13400,48171,48343,14135,48310,13443,48355,47465,13779,48386,15439,48225,48229,15803,15344,46869,48167,48094,14447,46124,47525,47516,46785,47821,14572,11462,14747,15590,14849,48379,15074,44716,46900,13806,14178,15384,13925,48500,48165,47110,48427,15029,15532,8541,48323,46629,48012,14809,13590,13806,48092,47559,46466,48365,15613,48213,13929,47255,43484,46113,15571,14626,47629,15210,44209,45029,15530,15843,47822,13393,48152,11432,15861,45498,12700,48591,15120,47870,46555,14453,45135,14138,47365,13638,44130,47392,47869,48394,48557,15794,14722,48386,12415,47346,48424,43757,46705,46733,47795,14587,14400,45879,15604,47770,45284,47516,15001,47527,47048,47800,12758,15441,48604,13108,13408,48450,48205,46548,46394,48381,15578,15485,14125,15110,48450,48236,15631,47880,45088,48564,14131,46261,12249,15434,15253,48502,47059,11365,12061,44471,46928,47169,15519,15556,14941,15225,48324,13791,15571,47508,15741,47293,47810,45320,47591,48639,15361,48506,14663,15513,13331,14732,46788,48231,15869,14829,47157,14093,15457,15399,13562,48419,48428,45649,48541,13534,15140,47987,44811,15082,48241,48449,15045,48153,12471,15076,15602,14464,47940,14200,12295,47277,14593,15669,47818,47529,14674,47038,48444,44578,48580,48199,15254,47814,47614,15664,14677,48639,47758,48497,15634,14145,46639,15393,47405,14767,48375,13471,47766,15526,46055,15719,15166,13986,47344,15156,45935,48272,47243,47897,15425,13655,15340,48317,48358,48412,15838,14679,48393,14234,14706,47954,15857,13764,47163,15399,15229,48462,45391,48208,15869,47702,14957,12460,15459,15475,15209,45093,40325,46305,47454,12411,15816,47610,45889,48596,43161,14665,12790,15725,14583,12043,13958,13400,47180,44666,48186,48292,47880,12464,44300,10634,47603,46263,15334,46908,48548,15522,12550,46288,45224,15113,48143,15118,14589,48322,15385,46723,14753,46861,14094,13543,15465,14423,47585,46830,14557,14205,15310,45139,48639,15005,44403,45844,48518,15079,15405,46284,48007,46978,44919,45480,48225,48413,15093,47959,47147,15371,48285,14555,47465,13925,47031,10820,45378,48276,14196,15024,47142,47246,15752,47039,15146,46162,48371,14503,47384,12388,47330,47509,11217,47736,12311,13411,14486,15355,13391,48518,15853,14892,47070,47961,48347,47830,48565,15454,48307,13389,14164,15857,15386,13850,14569,46801,48129,46217,14481,14902,15624,14760,14822,14380,47707,14840,47584,45896,45548,48172,12969,48000,48284,14385,47943,12943,11237,15427,46129],"q8k":[156,62,54,60,56,192,28,83,173,0,11,172,25,232,143,124,67,120,112,23,85,1,65,126,74,148,124,109,186,33,78,210,154,57,21,240,19,236,114,147,185,77,174,219,152,20,66,108,255,0,158,212,12,207,132,3,78,214,124,92,234,68,46,222,20,208,60,236,240,154,154,37,248,215,217,25,177,223,34,78,175,93,231,177,124,130,240,124,51,173,144,60,174,52,32,67,251,242,214,14,221,159,117,1,33,127,40,114,35,126,80,121,74,68,158,35,255,93,185,163,196,189,107,34,136,191,107,187,113,149,202,236,146,96,110,147,121,194,223,85,168,248,66,170,104,180,96,2,185,88,126,245,222,16,208,251,84,62,93,129,26,233,189,64,96,185,225,84,31,54,159,147,78,112,138,215,199,160,191,213,116,138,47,47,158,80,252,75,41,158,126,64,250,105,200,146,170,193,110,10,149,117,41,88,4,3,50,46,113,39,234,10,195,215,169,138,156,63,247,198,141,152,87,254,249,39,98,195,121,185,186,70,247,47,149,228,253,45,92,9,156,124,152,102,189,4,20,43,38,211,60,188,168,30,91,221,124,105,204,85,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,76,90,79,60,52,58,18,166,179,126,185,43,47,83,100,28,231,194,154,151,208,46,233,252,127,224,21,43,201,220,225,157,163,240,209,242,244,49,104,25,192,181,131,230,231,1,217,253,55,13,51,196,123,185,210,16,18,123,221,79,177,156,14,131,3,132,147,70,68,17,7,180,99,227,247,252,11,94,72,220,124,41,148,81,199,199,208,46,84,116,38,21,44,115,226,156,22,100,25,0,217,176,124,220,237,245,18,17,15,23,0,130,61,0,173,213,56,62,62,106,139,235,139,158,64,250,86,138,68,180,97,59,1,251,119,227,12,60,62,48,225,30,253,185,35,102,10,172,87,114,180,61,114,18,102,215,245,240,103,230,57,12,175,244,56,10,173,229,26,93,179,245,67,18,33,91,146,86,92,91,116,125,231,100,206,15,15,156,115,216,243,87,235,179,112,214,235,107,2,27,38,185,174,59,133,4,170,31,110,171,169,193,113,77,185,47,63,0,60,234,210,175,32,189,26,191,55,94,68,225,164,114,248,28,239,239,15,245,142,20,17,192,127,97,48,9,152,187,204,227,156,178,25,96,146,56,15,75,35,118,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"q80":[113,30,170,151,253,137,65,232,43,93,144,137,243,52,131,17,90,180,130,174,78,152,36,39,17,230,205,249,226,160,129,181,27,153,62,29,231,6,230,224,248,48,209,199,113,131,140,90,164,131,75,61,195,120,197,69,178,186,39,249,3,236,125,121,226,82,159,86,213,32,80,59,244,171,252,0,26,102,253,206,212,198,184,67,162,224,0,164,234,79,171,133,99,102,52,72,227,237,216,164,208,157,103,29,157,74,255,170,46,16,50,83,53,215,82,130,92,138,122,157,77,63,206,132,183,210,102,103,105,159,57,32,238,164,219,211,73,34,45,240,58,96,79,222,205,222,71,160,188,42,105,190,153,203,40,200,167,184,108,159,18,94,124,122,166,151,118,11,203,192,254,33,57,209,39,252,234,32,79,2,78,66,217,41,41,135,98,125,113,196,157,5,129,240,60,102,225,52,46,84,84,204,249,70,55,29,21,179,31,203,105,228,97,155,60,84,94,241,218,57,186,77,206,176,252,138,230,253,17,229,132,106,40,154,52,191,203,47,120,35,246,50,247,168,211,150,103,178,194,22,169,145,187,89,26,108,74,210,148,226,235,190,135,168,107,82,25,127,198,115,247,246,172,32,155,5,42,168,123,174,33,250,11,11,66,198,0,12,172,245,204,177,248,91,13,127,55,90,56,103,173,121,102,15,4,19,174,34,73,112,173,112,1,164,159,220,141,92,20,250,25,13,220,43,147,252,224,68,203,201,40,188,5,195,175,136,63,15,21,112,13,31,72,232,1,89,204,213,152,187,177,3,140,254,43,173,3,187,56,254,165,159,130,185,204,12,41,91,86,247,127,193,69,56,40,31,161,222,153,220,76,186,245,150,99,183,6,158,194,104,69,184,161,239,248,166,187,230,206,245,12,83,196,43,236,29,47,222,112,33,248,191,58,35,180,106,47,239,139,154,215,139,231,140,204,255,251,62,105,37,167,71,92,130,251,44,58,53,52,55,236,35,76,33,180,20,32,239,7,91,239,130,135,119,160,129,147,200,110,123,34,185,107,13,107,217,169,51,27,247,160,69,157,3,76,117,192,31,144,157,90,116,29,97,229,181,157,14,46,62,78,201,222,83,79,82,64,130,53,5,127,222,246,142,134,250,171,250,60,49,246,32,159,6,164,220,115,57,230,44,150,114,34,5,92,158,3,211,119,96,105,239,36,42,47,72,187,165,63,138,251,65,51,71],"y_fp16":[-6.8061409,-4.91714191,-3.75249958],"y_q8k":[4.92353535,5.31176281,-0.442772388],"y_q80":[-2.42747259,-1.24617803,-1.4989177]} +]} diff --git a/crates/cuda-driver/oracle/pure_modules.txt b/crates/cuda-driver/oracle/pure_modules.txt index fb9dd76c..8add1f35 100644 --- a/crates/cuda-driver/oracle/pure_modules.txt +++ b/crates/cuda-driver/oracle/pure_modules.txt @@ -5,3 +5,4 @@ runtime:backend_original_prompt mmq_original_prefill_misc device_arena native_flash_attn +s_gemv_legacy diff --git a/crates/cuda-driver/oracle/run_shim_oracles.sh b/crates/cuda-driver/oracle/run_shim_oracles.sh index 0b7acd31..f17ae91f 100755 --- a/crates/cuda-driver/oracle/run_shim_oracles.sh +++ b/crates/cuda-driver/oracle/run_shim_oracles.sh @@ -21,4 +21,5 @@ run qsa_prompt_attn_variants qsa_prompt_attn_variants.cpp "$src/qsa_prompt_atten SHIM_CXXFLAGS=-DTEST_V2 run qsa_prompt_attn_v2 qsa_prompt_attn_variants.cpp "$src/qsa_prompt_attention.cu" run misc_kernels misc_kernels.cpp "$src/mmq_original_prefill_misc.cu" "$src/device_poison.cu" "$src/mmq_original_group_swiglu.cu" > /dev/null run native_flash_attn flash_attn.cpp "$src/native_flash_attn.cu" +run s_gemv_legacy s_gemv_legacy.cpp "$src/s_gemv_legacy.cu" > /dev/null exit $failed diff --git a/crates/cuda-driver/oracle/shim/cuda_host_shim.h b/crates/cuda-driver/oracle/shim/cuda_host_shim.h index 2e00a223..cc7b0924 100644 --- a/crates/cuda-driver/oracle/shim/cuda_host_shim.h +++ b/crates/cuda-driver/oracle/shim/cuda_host_shim.h @@ -67,6 +67,8 @@ inline float __half2float(__half h) { return h.to_float(); } inline __half __ushort_as_half(unsigned short v) { __half h; h.bits = v; return h; } inline __half2 __floats2half2_rn(float a, float b) { return {__float2half_rn(a), __float2half_rn(b)}; } inline __half2 __halves2half2(__half a, __half b) { return {a, b}; } +inline float __low2float(__half2 h) { return __half2float(h.x); } +inline float __high2float(__half2 h) { return __half2float(h.y); } inline float2 __half22float2(__half2 h) { return {__half2float(h.x), __half2float(h.y)}; } inline __half2 __hsub2(__half2 a, __half2 b) { return {__float2half_rn(__half2float(a.x) - __half2float(b.x)), __float2half_rn(__half2float(a.y) - __half2float(b.y))}; @@ -135,6 +137,9 @@ template inline T shim_shfl(T v, int src_of_lane_fn_dummy, int mode if (mode == 0) { // xor src = lane ^ arg; if ((src & ~(width - 1)) != (lane & ~(width - 1))) src = lane; + } else if (mode == 2) { // down: lane + delta, out of the width segment -> own value + src = lane + arg; + if (src >= (lane & ~(width - 1)) + width) src = lane; } else { // idx src = (lane & ~(width - 1)) + (arg & (width - 1)); } @@ -144,6 +149,7 @@ template inline T shim_shfl(T v, int src_of_lane_fn_dummy, int mode } template inline T __shfl_xor_sync(unsigned, T v, int lane_mask, int width = 32) { return shim_shfl(v, 0, 0, lane_mask, width); } template inline T __shfl_sync(unsigned, T v, int src_lane, int width = 32) { return shim_shfl(v, 0, 1, src_lane, width); } +template inline T __shfl_down_sync(unsigned, T v, unsigned delta, int width = 32) { return shim_shfl(v, 0, 2, (int) delta, width); } // ---- launch: grid blocks run one after another, each CUDA thread on its own std::thread template diff --git a/crates/cuda-driver/src/lib.rs b/crates/cuda-driver/src/lib.rs index b8bfe6c3..99100894 100644 --- a/crates/cuda-driver/src/lib.rs +++ b/crates/cuda-driver/src/lib.rs @@ -83,6 +83,7 @@ pub mod mmq_original_prefill_misc; pub mod device_arena; // Pure half of the Original native_flash_attn.cu (admission, geometry, step rule, FP64 specification). pub mod native_flash_attn; +pub mod s_gemv_legacy; #[cfg(all(test, feature = "cuda-driver", target_os = "linux", target_pointer_width = "64"))] mod original_prefill_schedule_crosscheck; #[cfg(all( diff --git a/crates/cuda-driver/src/s_gemv_legacy.cu b/crates/cuda-driver/src/s_gemv_legacy.cu new file mode 100644 index 00000000..3e2f9924 --- /dev/null +++ b/crates/cuda-driver/src/s_gemv_legacy.cu @@ -0,0 +1,951 @@ +// Literal Original legacy S-form GEMV kernels (src/kernels/cuda/s_gemv.cu, s2_gemv.cu, s2_gemv_quads.cu, s2_gemv_q8.cu, +// s2_gemv_fast.cu), for the S2/S4/S8 canonical-form packs (docs/pack-format.md: value = cb[code] * scale + offset). +// Everything inside the Original's anonymous namespaces is carried verbatim except: +// * every `__global__` kernel template/function becomes a `__device__ __forceinline__` body of the same name, called by the +// `extern "C" __global__ leleka_*` entries at the end (one entry per template instantiation the host launchers use); +// * `Codebook::Iq4Nl` (s_gemv.hpp) is `kCodebookIq4Nl`; `extern __shared__ float partial[]/smem[]` is a view of `smem_raw`; +// * the S2 files' repeated `QK`/`QK_S2` constants are declared once; +// * `s2_gemv_fast`'s `__constant__ float c_codes[256][4]` was filled by a host `cudaMemcpyToSymbol` (ensure_lut); here it is +// statically initialised with the same values ((byte >> 2k & 3) - 1), generated, so no host step is needed. +// The host launchers (argument checks, geometry, cudaDeviceSynchronize) are not carried: see s_gemv_legacy.rs. +// Original build flags: default (none of these files is in CMakeLists' fast-math list). STATUS: NOT COMPILED OR RUN +// (no nvcc here); executed on the host through oracle/shim (drivers/s_gemv_legacy.cpp). +// Original sources (Strata upstream tree as imported under engine/strata): +// s_gemv.cu sha256 46c946cff34b50f252a5d4a6b2f99e0d30b78a6ad741919417566ccb43017e40 +// s2_gemv.cu sha256 4d21a4e14e166b02652719c095b25fa7f346c3d0b3dd1009e050e793b2167b9b +// s2_gemv_quads.cu sha256 3633ad7b7006fdfc96e7b1aa06733168e2292a5b700c49ece60e121c3db4280c +// s2_gemv_q8.cu sha256 c5123b1a7373acad6e62c701bac3ed04f90c1ed5702104008892125de052733d +// s2_gemv_fast.cu sha256 ff3ab5c5b0da5b25aa73f7da3a86894c93960efef3b8c71463d501c1e8adaafe +#include +#include +#include + +enum { kCodebookAffine = 0, kCodebookIq4Nl = 1 }; + + +// `kvalues_iq4nl`: the non-linear codebook, verbatim from ggml-common.h / ggml-quants.c. +// +// **IT WAS IN `__constant__` FOR THE WRONG REASON, AND IT COST 2.12x.** The old comment justified constant +// memory with "every thread reads it and it is the same 16 values for the whole launch". That reasoning is +// exactly backwards: constant memory is fast when the access is UNIFORM - every lane reading the SAME address - +// and a NON-LINEAR codebook means every lane reads a DIFFERENT index, which is the pattern it handles worst. +// +// Measured on `attn_qkv` (IQ4_XS, 10240 x 2560), forcing the Affine codebook so that the memory traffic and the +// instruction count stay identical and only the table read is removed: +// +// codebook probe: IQ4NL 0.2032 ms Affine 0.0960 ms ratio 2.12x +// +// `decode_tbl` below reads a SHARED copy instead: 16 bytes loaded once per block, then an index into shared +// memory that costs at most a four-way bank conflict. The `__constant__` array stays as the source of it. +__constant__ signed char kIq4Nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, + 1, 13, 25, 38, 53, 69, 89, 113}; + +template +__device__ __forceinline__ float decode(int code, int bias, int codebook) { + if (codebook == kCodebookIq4Nl) return (float) kIq4Nl[code & 0x0F]; + return (float) (code + bias); // the bias is applied to the CODE, in the integer domain +} + +/// The same decode with the codebook in SHARED memory, which is what the hot kernels use - see the measurement +/// above. A divergent read of a 16-entry table is the one access pattern constant memory is worst at. +template +__device__ __forceinline__ float decode_tbl(int code, int bias, int codebook, const signed char* tbl) { + if (codebook == kCodebookIq4Nl) return (float) tbl[code & 0x0F]; + return (float) (code + bias); +} + +// ---- Q8_K ACTIVATIONS ---------------------------------------------------------------------------------- +// +// 292 bytes per 256 elements: `{ f32 d ; int8_t qs[256] ; int16_t bsums[16] }`. `bsums` is not read - it +// exists for ggml's AVX2 dot product - but the STRIDE includes it, so a buffer from `quantize_q8_K` passes +// straight in. +// +// The block's `d` sits at a 4-byte-aligned offset from the buffer base as long as the base is aligned, which +// `cudaMalloc` guarantees. +constexpr int Q8K_BLOCK_BYTES = 292; +constexpr int Q8K_BLOCK_ELEMS = 256; + +__device__ __forceinline__ float q8k_at(const uint8_t* __restrict__ x, long long i) { + const uint8_t* blk = x + (i / Q8K_BLOCK_ELEMS) * Q8K_BLOCK_BYTES; + const float d = __ldg((const float*) blk); + const int8_t q = ((const int8_t*) (blk + 4))[i % Q8K_BLOCK_ELEMS]; + return d * (float) q; +} + +/// **THE Q8_0 ACTIVATION**, which is what the LEGACY block formats want: `block_q8_0` is `{fp16 d; int8 qs[32]}`, +/// 34 bytes per 32 elements, produced by `quantize_q8_0`. +/// +/// THE ENGINE HAD NO GEMV FOR THIS, and it was the last activation-contract gap (LEDGER L54). It was described +/// as "Q8_K is not implemented", which is not the situation: `ffn_down_shexp` is IQ4_NL/Q4_0/Q5_0/Q8_0 in EVERY +/// layer, whose `vec_dot_type` is Q8_0 - and its `n_in` is 640, which is not a multiple of 256, so **Q8_K is +/// STRUCTURALLY IMPOSSIBLE for it rather than merely absent**. Every other legacy tensor in the pack is Q2_0, +/// whose attributes happen to be S2's, which is how the S2-only kernel covered them and the hole stayed hidden. +constexpr int Q8_0_BLOCK_BYTES = 34; +constexpr int Q8_0_BLOCK_ELEMS = 32; + +__device__ __forceinline__ float q8_0_at(const uint8_t* __restrict__ x, long long i) { + const uint8_t* blk = x + (i / Q8_0_BLOCK_ELEMS) * Q8_0_BLOCK_BYTES; + // a `uint16_t` needs 2-byte alignment and the stride is 34, so every block's `d` is aligned + const float d = __half2float(__ushort_as_half(*(const uint16_t*) blk)); + const int8_t q = ((const int8_t*) (blk + 2))[i % Q8_0_BLOCK_ELEMS]; + return d * (float) q; +} + +/// The Q8_K path, with the SAME weight decode as `s_gemv_kernel` and a different activation loader. The two +/// are separate kernels rather than one templated on the activation kind because the activation POINTER TYPE +/// differs (`uint16_t*` vs `uint8_t*`) and a templated pointer would need a cast at every call site - which is +/// where a wrong stride would hide. +template +__device__ __forceinline__ void s_gemv_q8k_kernel(const uint8_t* __restrict__ x, const uint8_t* __restrict__ codes, + const float* __restrict__ scales, const float* __restrict__ offset, + float* __restrict__ y, long long n_in, long long n_out, int bias, + int codebook, int group_elems, int has_offset) { + // THE CODEBOOK, IN SHARED MEMORY - 16 bytes, loaded once per block. See the note on `kIq4Nl`. + // THE LOAD AND THE BARRIER COME BEFORE THE EARLY RETURN: `o` is per-THREAD in this kernel, so a thread + // that returned first would leave the others waiting at a barrier it never reaches. + __shared__ signed char s_iq4nl[16]; + if (threadIdx.x < 16) s_iq4nl[threadIdx.x] = kIq4Nl[threadIdx.x]; + __syncthreads(); + const long long o = (long long) blockIdx.x * blockDim.x + threadIdx.x; + if (o >= n_out) return; + + constexpr int PER_BYTE = 8 / CODE_BITS; + const long long n_groups = n_in / group_elems; + const long long codes_per_row = n_in / PER_BYTE; + const uint8_t* c = codes + o * codes_per_row; + const float* s = scales + o * n_groups; + const float* off = has_offset ? offset + o * n_groups : nullptr; + + float acc = 0.0f; + for (long long g = 0; g < n_groups; ++g) { + const float d = s[g]; + const float b = off ? off[g] : 0.0f; + const long long base = g * (long long) group_elems; + for (int j = 0; j < group_elems; ++j) { + const long long i = base + j; + const int code = (c[i / PER_BYTE] >> ((int) (i % PER_BYTE) * CODE_BITS)) & ((1 << CODE_BITS) - 1); + // The offset belongs to the WEIGHT and is applied before the activation multiply - see the note in + // `s_gemv_kernel`, where writing it the other way rounded differently and only the Q4_K case could + // tell. + const float w = decode_tbl(code, bias, codebook, s_iq4nl) * d + b; + acc += w * q8k_at(x, i); + } + } + y[o] = acc; +} + +/// One warp per output row, lanes striding the reduction axis: for a fixed `i` consecutive lanes read +/// consecutive activations, which is the coalescing that matters once the output width is small. +/// +/// **`group_shift`, NOT `group_elems`, AND IT COSTS A DIVISION PER ELEMENT IF YOU GET IT WRONG.** The +/// sibling fp16 kernel was given this treatment ("a 64-bit integer division on this hardware is a long +/// instruction sequence - one per element, in the hottest loop in the engine") and THIS one was not: it kept +/// `i / group_elems` with `group_elems` a RUNTIME value, once per element, in the kernel that carries most of +/// the model's weight bytes. Every group size the format defines is a power of two, so the divisor is passed +/// as its logarithm and the division becomes a shift; the host refuses a non-power-of-two group rather than +/// computing a wrong index. +/// ONE KERNEL FOR BOTH QUANTIZED ACTIVATIONS, templated on which block format `x` holds. A second copy with a +/// different loader would be a second thing to get wrong, and the only difference IS the loader - the weight +/// decode, the quad loop, the shared codebook and the reduction are identical. +template +__device__ __forceinline__ void s_gemv_q8_split_kernel(const uint8_t* __restrict__ x, const uint8_t* __restrict__ codes, + const float* __restrict__ scales, const float* __restrict__ offset, + float* __restrict__ y, long long n_in, long long n_out, int bias, + int codebook, int group_shift, int has_offset) { + constexpr int PER_BYTE = 8 / CODE_BITS; + const int warps_per_block = (int) (blockDim.x >> 5); + const long long o = (long long) blockIdx.x * warps_per_block + (threadIdx.x >> 5); + const int lane = threadIdx.x & 31; + + // THE CODEBOOK, IN SHARED MEMORY. 16 bytes, loaded once per block by the first 16 threads, instead of a + // divergent constant read per element - measured at 2.12x of this whole kernel (see the note on `kIq4Nl`). + // + // THE LOAD AND THE BARRIER COME BEFORE THE EARLY RETURN, as in `s_gemv_q8k_kernel`. The return used to + // sit above them, so whenever `n_out % 8 != 0` the last block's surplus warps left before a barrier the + // others still waited at - undefined behaviour that every shape in the pack (a multiple of 8) happened to + // avoid. `o` is per-WARP, so the return below keeps whole warps together and the shuffle reduction's + // full mask stays valid. + __shared__ signed char s_iq4nl[16]; + if (threadIdx.x < 16) s_iq4nl[threadIdx.x] = kIq4Nl[threadIdx.x]; + __syncthreads(); + if (o >= n_out) return; + + const long long n_groups = n_in >> group_shift; + const long long codes_per_row = n_in / PER_BYTE; + const uint8_t* c = codes + o * codes_per_row; + const float* s = scales + o * n_groups; + const float* off = has_offset ? offset + o * n_groups : nullptr; + + float acc0 = 0.0f, acc1 = 0.0f, acc2 = 0.0f, acc3 = 0.0f; + float acc4 = 0.0f, acc5 = 0.0f, acc6 = 0.0f, acc7 = 0.0f; + float acc8 = 0.0f, acc9 = 0.0f, acc10 = 0.0f, acc11 = 0.0f; + float acc12 = 0.0f, acc13 = 0.0f, acc14 = 0.0f, acc15 = 0.0f; + // ---- EIGHT CONSECUTIVE ELEMENTS PER LANE, from TWO code loads and ONE scale. + // + // **THIS IS THE EXPERIMENT THE `#pragma unroll` NOTE BELOW LEFT OPEN**, and the note said what it would + // be: "Widening QE to 8 or 16 changes the LOADS THEMSELVES - two or four independent 16-bit words per + // lane - which an unroll cannot do because the extraction code is written for exactly four codes." So a + // lane now takes a double quad: two code words issued before either is consumed, which is what raises the + // bytes in flight. The scale and offset are still ONE load, because eight consecutive elements lie in one + // group whenever `group_elems % 8 == 0` - the launcher asserts that, and every group size this format + // defines (16, 32, 64) satisfies it. + // + // The loop used to take `i = lane, lane+32, ...` - a STRIDE, chosen so consecutive lanes read consecutive + // activations - and paid a code load, a shift-and-mask, and a scale load FOR EVERY ELEMENT. But four + // consecutive elements share one code word (1, 2 or 4 bytes depending on width) and lie in one group, + // because every group size this format defines is a multiple of four. So a lane takes a QUAD: + // + // one code load instead of four + // one scale load instead of four + // one shift per element instead of a divide, a modulo and a shift + // + // THE BIT ORDER IS THE SAME ONE, verified for all three widths: element `i+k` is bits + // `[k*CODE_BITS, (k+1)*CODE_BITS)` of the little-endian word at byte `i/PER_BYTE`, which is what the old + // `(c[e/PER_BYTE] >> ((e%PER_BYTE)*CODE_BITS)) & MASK` computed one element at a time. + // + // THE SUMMATION ORDER CHANGES, which is why `s_gemv_parity` and `s_gemv_q8k_parity` carry tolerances and + // compare against the naive kernel - they are the guard for this, and they are the reason it is safe to do. + constexpr int QE = 16; + // BYTES PER FOUR-CODE WORD, not per octet. The two are easy to confuse and the first version of this + // did: it branched on the octet's total width and so read a uint32 for S4 (whose four codes are a + // uint16) and single bytes for S8 (whose four codes are a uint32). `s_gemv_q8k_parity` failed on + // exactly that, which is what it is for. + constexpr int QW = 4 * CODE_BITS / 8; // 1 for S2, 2 for S4, 4 for S8 + constexpr unsigned MASK = (1u << CODE_BITS) - 1u; + long long i = (long long) lane * QE; + // **`#pragma unroll 4` WAS TRIED HERE AND DID NOTHING.** Measured: 184.6 -> 183.9 GB/s at n_out 10240, + // with `s_gemv_parity` and `s_gemv_q8k_parity` both still clean. The hypothesis it was testing is L93 - + // that this kernel's flat ~6 ns per output row is a bytes-in-flight limit, one code load and one scale + // load per lane-iteration with only 20 iterations. A 4x unroll issues four of each before the first is + // consumed, so either the compiler was already doing it or **the limit is not instruction scheduling.** + // Removed rather than kept, because a hint that does not change the timing is a claim that is not true. + // + // What it does NOT rule out is the structural version of the same idea: QE is 4, so a lane consumes ONE + // 2-byte code word per iteration. Widening QE to 8 or 16 changes the LOADS THEMSELVES - two or four + // independent 16-bit words per lane - which an unroll cannot do because the extraction code is written for + // exactly four codes. That is the experiment that would settle it, and it is not this one. + for (; i + QE <= n_in; i += 32 * QE) { + const long long g = i >> group_shift; // the whole octet is in one group: group_elems % 8 == 0 + const float d = s[g]; + const float b = off ? off[g] : 0.0f; + const uint8_t* cp = c + i / PER_BYTE; + unsigned v, v2, v3, v4; + if (QW == 1) { v = cp[0]; v2 = cp[1]; v3 = cp[2]; v4 = cp[3]; } + else if (QW == 2) { v = *(const uint16_t*) cp; v2 = *(const uint16_t*) (cp + 2); + v3 = *(const uint16_t*) (cp + 4); v4 = *(const uint16_t*) (cp + 6); } + else { v = *(const uint32_t*) cp; v2 = *(const uint32_t*) (cp + 4); + v3 = *(const uint32_t*) (cp + 8); v4 = *(const uint32_t*) (cp + 12); } + // ---- THE ACTIVATION BLOCK IS HOISTED OUT OF THE SIXTEEN ELEMENTS, AND THAT WAS THE KERNEL'S COST. + // + // `q8k_at(x, i + k)` computes `(i + k) / 256` and `(i + k) % 256` on EVERY call: a 64-bit integer + // division AND modulo per element, sixteen times per lane-iteration. A 64-bit division on a GPU is a + // software routine of roughly a hundred cycles, and the compiler cannot strength-reduce it because it + // cannot prove `(i + k) / 256 == i / 256` without knowing `i % 256`. + // + // But `QE` is 16, `Q8K_BLOCK_ELEMS` is 256 and `Q8_0_BLOCK_ELEMS` is 32, and the loop advances `i` by + // `32 * QE`. So `i % 256` and `i % 32` are ALWAYS multiples of 16 - which means all QE elements of one + // lane-iteration lie inside ONE block, for either activation kind. One division and one modulo per + // iteration replace sixteen of each, and the sixteen activations become sixteen consecutive int8 loads + // from a pointer computed once. + // + // This is exact, not an approximation: `xi + QE <= blk_elems` holds for every reachable `xi`, so no + // lane-iteration can straddle a block boundary. The tail loop below still uses the accessors because + // its elements do not share a block, and it runs at most once per lane. + const int blk_elems = Q8K ? Q8K_BLOCK_ELEMS : Q8_0_BLOCK_ELEMS; + const uint8_t* xb = x + (i / blk_elems) * (Q8K ? Q8K_BLOCK_BYTES : Q8_0_BLOCK_BYTES); + const int xi = (int) (i % blk_elems); + const float xd = Q8K ? __ldg((const float*) xb) + : __half2float(__ushort_as_half(*(const uint16_t*) xb)); + const int8_t* xq = (const int8_t*) (xb + (Q8K ? 4 : 2)) + xi; + + const float w0 = decode_tbl((int) (v & MASK), bias, codebook, s_iq4nl) * d + b; + const float w1 = decode_tbl((int) ((v >> CODE_BITS) & MASK), bias, codebook, s_iq4nl) * d + b; + const float w2 = decode_tbl((int) ((v >> (2 * CODE_BITS)) & MASK), bias, codebook, s_iq4nl) * d + b; + const float w3 = decode_tbl((int) ((v >> (3 * CODE_BITS)) & MASK), bias, codebook, s_iq4nl) * d + b; + const float w4 = decode_tbl((int) (v2 & MASK), bias, codebook, s_iq4nl) * d + b; + const float w5 = decode_tbl((int) ((v2 >> CODE_BITS) & MASK), bias, codebook, s_iq4nl) * d + b; + const float w6 = decode_tbl((int) ((v2 >> (2 * CODE_BITS)) & MASK), bias, codebook, s_iq4nl) * d + b; + const float w7 = decode_tbl((int) ((v2 >> (3 * CODE_BITS)) & MASK), bias, codebook, s_iq4nl) * d + b; + acc0 += w0 * (xd * (float) xq[0]); + acc1 += w1 * (xd * (float) xq[1]); + acc2 += w2 * (xd * (float) xq[2]); + acc3 += w3 * (xd * (float) xq[3]); + acc4 += w4 * (xd * (float) xq[4]); + acc5 += w5 * (xd * (float) xq[5]); + acc6 += w6 * (xd * (float) xq[6]); + acc7 += w7 * (xd * (float) xq[7]); + const float w8 = decode_tbl((int) (v3 & MASK), bias, codebook, s_iq4nl) * d + b; + const float w9 = decode_tbl((int) ((v3 >> CODE_BITS) & MASK), bias, codebook, s_iq4nl) * d + b; + const float w10 = decode_tbl((int) ((v3 >> (2 * CODE_BITS)) & MASK), bias, codebook, s_iq4nl) * d + b; + const float w11 = decode_tbl((int) ((v3 >> (3 * CODE_BITS)) & MASK), bias, codebook, s_iq4nl) * d + b; + const float w12 = decode_tbl((int) (v4 & MASK), bias, codebook, s_iq4nl) * d + b; + const float w13 = decode_tbl((int) ((v4 >> CODE_BITS) & MASK), bias, codebook, s_iq4nl) * d + b; + const float w14 = decode_tbl((int) ((v4 >> (2 * CODE_BITS)) & MASK), bias, codebook, s_iq4nl) * d + b; + const float w15 = decode_tbl((int) ((v4 >> (3 * CODE_BITS)) & MASK), bias, codebook, s_iq4nl) * d + b; + acc8 += w8 * (xd * (float) xq[8]); + acc9 += w9 * (xd * (float) xq[9]); + acc10 += w10 * (xd * (float) xq[10]); + acc11 += w11 * (xd * (float) xq[11]); + acc12 += w12 * (xd * (float) xq[12]); + acc13 += w13 * (xd * (float) xq[13]); + acc14 += w14 * (xd * (float) xq[14]); + acc15 += w15 * (xd * (float) xq[15]); + } + // the last, partial quad - at most one per lane + for (; i < n_in; i += 32 * QE) { + for (int k = 0; k < QE && i + k < n_in; ++k) { + const long long e = i + k; + const long long g = e >> group_shift; + const int code = (c[e / PER_BYTE] >> ((int) (e % PER_BYTE) * CODE_BITS)) & MASK; + acc0 += (decode_tbl(code, bias, codebook, s_iq4nl) * s[g] + (off ? off[g] : 0.0f)) * (Q8K ? q8k_at(x, e) : q8_0_at(x, e)); + } + } + float acc = (((acc0 + acc1) + (acc2 + acc3)) + ((acc4 + acc5) + (acc6 + acc7))) + + (((acc8 + acc9) + (acc10 + acc11)) + ((acc12 + acc13) + (acc14 + acc15))); + for (int step = 16; step > 0; step >>= 1) acc += __shfl_down_sync(0xFFFFFFFFu, acc, step); + if (lane == 0) y[o] = acc; +} + +template +__device__ __forceinline__ void s_gemv_kernel(const uint16_t* __restrict__ x, const uint8_t* __restrict__ codes, + const float* __restrict__ scales, const float* __restrict__ offset, + float* __restrict__ y, long long n_in, long long n_out, int bias, int codebook, + int group_elems, int has_offset) { + // THE CODEBOOK, IN SHARED MEMORY - 16 bytes, loaded once per block. See the note on `kIq4Nl`. + // THE LOAD AND THE BARRIER COME BEFORE THE EARLY RETURN: `o` is per-THREAD in this kernel, so a thread + // that returned first would leave the others waiting at a barrier it never reaches. + __shared__ signed char s_iq4nl[16]; + if (threadIdx.x < 16) s_iq4nl[threadIdx.x] = kIq4Nl[threadIdx.x]; + __syncthreads(); + const long long o = (long long) blockIdx.x * blockDim.x + threadIdx.x; + if (o >= n_out) return; + + constexpr int PER_BYTE = 8 / CODE_BITS; + const long long n_groups = n_in / group_elems; + const long long codes_per_row = n_in / PER_BYTE; + const uint8_t* c = codes + o * codes_per_row; + const float* s = scales + o * n_groups; + const float* off = has_offset ? offset + o * n_groups : nullptr; + + float acc = 0.0f; + for (long long g = 0; g < n_groups; ++g) { + const float d = s[g]; + const float b = off ? off[g] : 0.0f; + const long long base = g * (long long) group_elems; + for (int j = 0; j < group_elems; ++j) { + const long long i = base + j; + const int code = (c[i / PER_BYTE] >> ((int) (i % PER_BYTE) * CODE_BITS)) & ((1 << CODE_BITS) - 1); + // The offset belongs to the WEIGHT, so it is applied to the decoded value BEFORE the activation + // multiply: `w = code*scale + offset; acc += w * x`. Writing `acc += code*scale*x + offset*x` + // computes a mathematically equal expression that ROUNDS DIFFERENTLY, because it performs two + // multiplications and an addition where this performs one of each. The no-offset types cannot + // tell the difference - which is why this was wrong until the Q4_K case was added. + const float w = decode_tbl(code, bias, codebook, s_iq4nl) * d + b; + acc += w * __half2float(__ushort_as_half(x[i])); + } + } + y[o] = acc; +} + + +// ---- s_gemv.cu, second anonymous namespace: the row-split fp16 kernel ------------------------------------------- + +// One BLOCK per output row, `threads_per_row` threads splitting it. Parallelism becomes +// n_out * threads_per_row instead of n_out, which is the whole point: gate/up is [2560 x 640] and 640 threads +// cannot fill 48 SMs. +// +// The reduction is a fixed tree over shared memory rather than atomics, so the result is DETERMINISTIC for a +// given threads_per_row - a race would make the parity test flaky rather than wrong, which is the worst kind +// of failing test. +// NOTE ON `g = e >> group_shift`. The group index was `e / group_elems` with `group_elems` a RUNTIME value, +// and a 64-bit integer division on this hardware is a long instruction sequence - one per element, in the +// hottest loop in the engine. Every group size this format defines is a POWER OF TWO (16, 32, 64), so the +// divisor is passed as its logarithm and the division becomes a shift. The host wrapper refuses a +// non-power-of-two group rather than silently computing a wrong index. +template +__device__ __forceinline__ void s_gemv_split_kernel(const uint16_t* __restrict__ x, const uint8_t* __restrict__ codes, + const float* __restrict__ scales, const float* __restrict__ offset, + float* __restrict__ y, long long n_in, long long n_out, int bias, + int codebook, int group_elems, int group_shift, int has_offset, + int threads_per_row) { + extern __shared__ __align__(16) unsigned char smem_raw[]; + float* partial = reinterpret_cast(smem_raw); + // THE CODEBOOK, IN SHARED MEMORY - 16 bytes, loaded once per block. See the note on `kIq4Nl`. + // THE LOAD AND THE BARRIER COME BEFORE THE EARLY RETURN: `o` is per-THREAD in this kernel, so a thread + // that returned first would leave the others waiting at a barrier it never reaches. + __shared__ signed char s_iq4nl[16]; + if (threadIdx.x < 16) s_iq4nl[threadIdx.x] = kIq4Nl[threadIdx.x]; + __syncthreads(); + const long long o = blockIdx.x; + if (o >= n_out) return; + const int tid = threadIdx.x; + + constexpr int PER_BYTE = 8 / CODE_BITS; + const long long n_groups = n_in / group_elems; + const uint8_t* c = codes + o * (n_in / PER_BYTE); + const float* s = scales + o * n_groups; + const float* off = has_offset ? offset + o * n_groups : nullptr; + + // FOUR ACCUMULATORS, NOT ONE. L2 measured this kernel at 0.4% of FP32 peak and 2.1% of bandwidth, so + // neither limit is the constraint - the constraint is the DEPENDENCY CHAIN: a single `acc += ...` serialises + // every FMA behind the previous one's latency (4-6 cycles each, ~1 per 4 cycles achieved), so the SM sits + // idle. Four independent accumulators let the scheduler keep four FMAs in flight, and the loop is + // unrolled by four so the four chains are interleaved. The summation ORDER changes, which is why the + // parity test carries a tolerance and why this kernel's output is checked against the naive one. + // FOUR CONSECUTIVE ELEMENTS PER THREAD, from ONE code load and ONE scale - the same transformation the + // Q8_K kernel got, for the same reason and with the same guard (`s_gemv_parity`). See the long note there + // for the bit order, which is unchanged, and for why the group size is passed as a shift. + // + // THIS KERNEL IS THE ONE `attn_output` AND `shared_expert` USE, so the fix has to exist in both or half the + // model keeps the old instruction count. + constexpr int QE = 4; + constexpr int QB = QE * CODE_BITS / 8; // 1 for S2, 2 for S4, 4 for S8 + constexpr unsigned MASK = (1u << CODE_BITS) - 1u; + float acc0 = 0.0f, acc1 = 0.0f, acc2 = 0.0f, acc3 = 0.0f; + long long i = (long long) tid * QE; + for (; i + QE <= n_in; i += (long long) threads_per_row * QE) { + const long long g = i >> group_shift; + const float d = s[g]; + const float b = off ? off[g] : 0.0f; + const uint8_t* cp = c + i / PER_BYTE; + unsigned v; + if (QB == 1) v = cp[0]; + else if (QB == 2) v = *(const uint16_t*) cp; + else v = *(const uint32_t*) cp; + // four halves in ONE 8-byte load; `i` is a multiple of four, so `x + i` is 8-byte aligned + const float2 f01 = __half22float2(*reinterpret_cast(x + i)); + const float2 f23 = __half22float2(*reinterpret_cast(x + i + 2)); + acc0 += (decode_tbl((int) (v & MASK), bias, codebook, s_iq4nl) * d + b) * f01.x; + acc1 += (decode_tbl((int) ((v >> CODE_BITS) & MASK), bias, codebook, s_iq4nl) * d + b) * f01.y; + acc2 += (decode_tbl((int) ((v >> (2 * CODE_BITS)) & MASK), bias, codebook, s_iq4nl) * d + b) * f23.x; + acc3 += (decode_tbl((int) ((v >> (3 * CODE_BITS)) & MASK), bias, codebook, s_iq4nl) * d + b) * f23.y; + } + // the last, partial quad - at most one per thread + for (; i < n_in; i += (long long) threads_per_row * QE) { + for (int k = 0; k < QE && i + k < n_in; ++k) { + const long long e = i + k; + const long long g = e >> group_shift; + const int code = (c[e / PER_BYTE] >> ((int) (e % PER_BYTE) * CODE_BITS)) & MASK; + acc0 += (decode_tbl(code, bias, codebook, s_iq4nl) * s[g] + (off ? off[g] : 0.0f)) * + __half2float(__ushort_as_half(x[e])); + } + } + partial[tid] = (acc0 + acc1) + (acc2 + acc3); + __syncthreads(); + for (int step = threads_per_row / 2; step > 0; step >>= 1) { + if (tid < step) partial[tid] += partial[tid + step]; + __syncthreads(); + } + if (tid == 0) y[o] = partial[0]; +} + + +// ---- s2_gemv.cu / s2_gemv_quads.cu / s2_gemv_q8.cu / s2_gemv_fast.cu -------------------------------------------- +constexpr int QK = 64; +constexpr int QK_S2 = 64; +constexpr int QK8_0 = 32; +constexpr int MAX_SHARED_HALVES = 4096; // 8 KB of shared for x; n_embd 2560 fits with room + +__device__ __forceinline__ void s2_gemv_kernel(const uint16_t* __restrict__ x, const uint8_t* __restrict__ codes, + const float* __restrict__ scales, float* __restrict__ y, long long n_in, + long long n_out) { + const long long o = (long long) blockIdx.x * blockDim.x + threadIdx.x; + if (o >= n_out) return; + + const long long nb = n_in / QK; + const uint8_t* c = codes + o * nb * (QK / 4); + const float* s = scales + o * nb; + + float acc = 0.0f; + for (long long b = 0; b < nb; ++b) { + const float d = s[b]; + const uint8_t* cb = c + b * (QK / 4); + const uint16_t* xb = x + b * QK; + for (int j = 0; j < QK; ++j) { + // (code - 1) in the INTEGER domain, then the group scale, then the activation; the CPU reference + // below is written in the same order so the two differ only by floating-point contraction. + const int code = (cb[j >> 2] >> ((j & 3) * 2)) & 0x03; + acc += (float) (code - 1) * d * __half2float(__ushort_as_half(xb[j])); + } + } + y[o] = acc; +} + + +__device__ __forceinline__ void s2_gemv_quads_kernel(const uint16_t* __restrict__ x, const uint8_t* __restrict__ codes, + const float* __restrict__ scales, float* __restrict__ y, long long n_in, + long long n_out, int threads_per_row) { + extern __shared__ __align__(16) unsigned char smem_raw[]; + float* partial = reinterpret_cast(smem_raw); + const long long o = blockIdx.x; + if (o >= n_out) return; + const int tid = threadIdx.x; + + const long long n_quads = n_in / 4; + const uint8_t* c = codes + o * n_quads; // exactly one code byte per quad + const float* s = scales + o * (n_in / QK_S2); + + float a0 = 0.0f, a1 = 0.0f, a2 = 0.0f, a3 = 0.0f; + for (long long q = tid; q < n_quads; q += threads_per_row) { + const uint8_t byte = c[q]; // ONE load for four codes + const float d = s[q >> 4]; // (q*4) >> 6, the group index as a shift + // ONE 64-bit load for four halves. `x` is 256-byte aligned and four halves are eight bytes, so + // this is an aligned uint2 - if it were not, the misaligned access would fault rather than be slow. + const uint2 xw = *reinterpret_cast(x + q * 4); + const __half2 h01 = *reinterpret_cast(&xw.x); + const __half2 h23 = *reinterpret_cast(&xw.y); + // the four codes, each carrying the -1 bias in the INTEGER domain; the scale is applied once per + // element here rather than once per group, which is the same expression the generic kernel uses + const float w0 = (float) ((int) (byte & 3) - 1) * d; + const float w1 = (float) ((int) ((byte >> 2) & 3) - 1) * d; + const float w2 = (float) ((int) ((byte >> 4) & 3) - 1) * d; + const float w3 = (float) ((int) ((byte >> 6) & 3) - 1) * d; + a0 += w0 * __low2float(h01); + a1 += w1 * __high2float(h01); + a2 += w2 * __low2float(h23); + a3 += w3 * __high2float(h23); + } + partial[tid] = (a0 + a1) + (a2 + a3); + __syncthreads(); + for (int step = threads_per_row / 2; step > 0; step >>= 1) { + if (tid < step) partial[tid] += partial[tid + step]; + __syncthreads(); + } + if (tid == 0) y[o] = partial[0]; +} + + +__device__ __forceinline__ void s2_gemv_q8_kernel(const uint8_t* __restrict__ act, const uint8_t* __restrict__ codes, + const float* __restrict__ scales, float* __restrict__ y, long long n_in, + long long n_out, int threads_per_row) { + extern __shared__ __align__(16) unsigned char smem_raw[]; + float* partial = reinterpret_cast(smem_raw); + const long long o = blockIdx.x; + if (o >= n_out) return; + const int tid = threadIdx.x; + + const long long n_quads = n_in / 4; + const uint8_t* c = codes + o * n_quads; // one code byte per quad + const float* s = scales + o * (n_in / QK_S2); + + float a0 = 0.0f, a1 = 0.0f, a2 = 0.0f, a3 = 0.0f; + for (long long q = tid; q < n_quads; q += threads_per_row) { + const uint8_t byte = c[q]; + const float d = s[q >> 4]; // (q*4) >> 6, the S2 group index as a shift + // the activation quad: four int8 in one 32-element block, so one block scale + const long long ablk = (q * 4) / QK8_0; + const uint8_t* blk = act + ablk * 34; + const uint16_t dbits = (uint16_t) (blk[0] | (blk[1] << 8)); + const float dx = __half2float(__ushort_as_half(dbits)); + const int8_t* xq = reinterpret_cast(blk + 2); + const int off = (int) ((q * 4) % QK8_0); + + const float w0 = (float) ((int) (byte & 3) - 1) * d; + const float w1 = (float) ((int) ((byte >> 2) & 3) - 1) * d; + const float w2 = (float) ((int) ((byte >> 4) & 3) - 1) * d; + const float w3 = (float) ((int) ((byte >> 6) & 3) - 1) * d; + a0 += w0 * ((float) xq[off + 0] * dx); + a1 += w1 * ((float) xq[off + 1] * dx); + a2 += w2 * ((float) xq[off + 2] * dx); + a3 += w3 * ((float) xq[off + 3] * dx); + } + partial[tid] = (a0 + a1) + (a2 + a3); + __syncthreads(); + for (int step = threads_per_row / 2; step > 0; step >>= 1) { + if (tid < step) partial[tid] += partial[tid + step]; + __syncthreads(); + } + if (tid == 0) y[o] = partial[0]; +} + + +// byte -> the four code values with the -1 bias already applied, in element order (bits 0,2,4,6). Generated. +__constant__ float c_codes[256][4] = { + {-1.0f, -1.0f, -1.0f, -1.0f}, + {0.0f, -1.0f, -1.0f, -1.0f}, + {1.0f, -1.0f, -1.0f, -1.0f}, + {2.0f, -1.0f, -1.0f, -1.0f}, + {-1.0f, 0.0f, -1.0f, -1.0f}, + {0.0f, 0.0f, -1.0f, -1.0f}, + {1.0f, 0.0f, -1.0f, -1.0f}, + {2.0f, 0.0f, -1.0f, -1.0f}, + {-1.0f, 1.0f, -1.0f, -1.0f}, + {0.0f, 1.0f, -1.0f, -1.0f}, + {1.0f, 1.0f, -1.0f, -1.0f}, + {2.0f, 1.0f, -1.0f, -1.0f}, + {-1.0f, 2.0f, -1.0f, -1.0f}, + {0.0f, 2.0f, -1.0f, -1.0f}, + {1.0f, 2.0f, -1.0f, -1.0f}, + {2.0f, 2.0f, -1.0f, -1.0f}, + {-1.0f, -1.0f, 0.0f, -1.0f}, + {0.0f, -1.0f, 0.0f, -1.0f}, + {1.0f, -1.0f, 0.0f, -1.0f}, + {2.0f, -1.0f, 0.0f, -1.0f}, + {-1.0f, 0.0f, 0.0f, -1.0f}, + {0.0f, 0.0f, 0.0f, -1.0f}, + {1.0f, 0.0f, 0.0f, -1.0f}, + {2.0f, 0.0f, 0.0f, -1.0f}, + {-1.0f, 1.0f, 0.0f, -1.0f}, + {0.0f, 1.0f, 0.0f, -1.0f}, + {1.0f, 1.0f, 0.0f, -1.0f}, + {2.0f, 1.0f, 0.0f, -1.0f}, + {-1.0f, 2.0f, 0.0f, -1.0f}, + {0.0f, 2.0f, 0.0f, -1.0f}, + {1.0f, 2.0f, 0.0f, -1.0f}, + {2.0f, 2.0f, 0.0f, -1.0f}, + {-1.0f, -1.0f, 1.0f, -1.0f}, + {0.0f, -1.0f, 1.0f, -1.0f}, + {1.0f, -1.0f, 1.0f, -1.0f}, + {2.0f, -1.0f, 1.0f, -1.0f}, + {-1.0f, 0.0f, 1.0f, -1.0f}, + {0.0f, 0.0f, 1.0f, -1.0f}, + {1.0f, 0.0f, 1.0f, -1.0f}, + {2.0f, 0.0f, 1.0f, -1.0f}, + {-1.0f, 1.0f, 1.0f, -1.0f}, + {0.0f, 1.0f, 1.0f, -1.0f}, + {1.0f, 1.0f, 1.0f, -1.0f}, + {2.0f, 1.0f, 1.0f, -1.0f}, + {-1.0f, 2.0f, 1.0f, -1.0f}, + {0.0f, 2.0f, 1.0f, -1.0f}, + {1.0f, 2.0f, 1.0f, -1.0f}, + {2.0f, 2.0f, 1.0f, -1.0f}, + {-1.0f, -1.0f, 2.0f, -1.0f}, + {0.0f, -1.0f, 2.0f, -1.0f}, + {1.0f, -1.0f, 2.0f, -1.0f}, + {2.0f, -1.0f, 2.0f, -1.0f}, + {-1.0f, 0.0f, 2.0f, -1.0f}, + {0.0f, 0.0f, 2.0f, -1.0f}, + {1.0f, 0.0f, 2.0f, -1.0f}, + {2.0f, 0.0f, 2.0f, -1.0f}, + {-1.0f, 1.0f, 2.0f, -1.0f}, + {0.0f, 1.0f, 2.0f, -1.0f}, + {1.0f, 1.0f, 2.0f, -1.0f}, + {2.0f, 1.0f, 2.0f, -1.0f}, + {-1.0f, 2.0f, 2.0f, -1.0f}, + {0.0f, 2.0f, 2.0f, -1.0f}, + {1.0f, 2.0f, 2.0f, -1.0f}, + {2.0f, 2.0f, 2.0f, -1.0f}, + {-1.0f, -1.0f, -1.0f, 0.0f}, + {0.0f, -1.0f, -1.0f, 0.0f}, + {1.0f, -1.0f, -1.0f, 0.0f}, + {2.0f, -1.0f, -1.0f, 0.0f}, + {-1.0f, 0.0f, -1.0f, 0.0f}, + {0.0f, 0.0f, -1.0f, 0.0f}, + {1.0f, 0.0f, -1.0f, 0.0f}, + {2.0f, 0.0f, -1.0f, 0.0f}, + {-1.0f, 1.0f, -1.0f, 0.0f}, + {0.0f, 1.0f, -1.0f, 0.0f}, + {1.0f, 1.0f, -1.0f, 0.0f}, + {2.0f, 1.0f, -1.0f, 0.0f}, + {-1.0f, 2.0f, -1.0f, 0.0f}, + {0.0f, 2.0f, -1.0f, 0.0f}, + {1.0f, 2.0f, -1.0f, 0.0f}, + {2.0f, 2.0f, -1.0f, 0.0f}, + {-1.0f, -1.0f, 0.0f, 0.0f}, + {0.0f, -1.0f, 0.0f, 0.0f}, + {1.0f, -1.0f, 0.0f, 0.0f}, + {2.0f, -1.0f, 0.0f, 0.0f}, + {-1.0f, 0.0f, 0.0f, 0.0f}, + {0.0f, 0.0f, 0.0f, 0.0f}, + {1.0f, 0.0f, 0.0f, 0.0f}, + {2.0f, 0.0f, 0.0f, 0.0f}, + {-1.0f, 1.0f, 0.0f, 0.0f}, + {0.0f, 1.0f, 0.0f, 0.0f}, + {1.0f, 1.0f, 0.0f, 0.0f}, + {2.0f, 1.0f, 0.0f, 0.0f}, + {-1.0f, 2.0f, 0.0f, 0.0f}, + {0.0f, 2.0f, 0.0f, 0.0f}, + {1.0f, 2.0f, 0.0f, 0.0f}, + {2.0f, 2.0f, 0.0f, 0.0f}, + {-1.0f, -1.0f, 1.0f, 0.0f}, + {0.0f, -1.0f, 1.0f, 0.0f}, + {1.0f, -1.0f, 1.0f, 0.0f}, + {2.0f, -1.0f, 1.0f, 0.0f}, + {-1.0f, 0.0f, 1.0f, 0.0f}, + {0.0f, 0.0f, 1.0f, 0.0f}, + {1.0f, 0.0f, 1.0f, 0.0f}, + {2.0f, 0.0f, 1.0f, 0.0f}, + {-1.0f, 1.0f, 1.0f, 0.0f}, + {0.0f, 1.0f, 1.0f, 0.0f}, + {1.0f, 1.0f, 1.0f, 0.0f}, + {2.0f, 1.0f, 1.0f, 0.0f}, + {-1.0f, 2.0f, 1.0f, 0.0f}, + {0.0f, 2.0f, 1.0f, 0.0f}, + {1.0f, 2.0f, 1.0f, 0.0f}, + {2.0f, 2.0f, 1.0f, 0.0f}, + {-1.0f, -1.0f, 2.0f, 0.0f}, + {0.0f, -1.0f, 2.0f, 0.0f}, + {1.0f, -1.0f, 2.0f, 0.0f}, + {2.0f, -1.0f, 2.0f, 0.0f}, + {-1.0f, 0.0f, 2.0f, 0.0f}, + {0.0f, 0.0f, 2.0f, 0.0f}, + {1.0f, 0.0f, 2.0f, 0.0f}, + {2.0f, 0.0f, 2.0f, 0.0f}, + {-1.0f, 1.0f, 2.0f, 0.0f}, + {0.0f, 1.0f, 2.0f, 0.0f}, + {1.0f, 1.0f, 2.0f, 0.0f}, + {2.0f, 1.0f, 2.0f, 0.0f}, + {-1.0f, 2.0f, 2.0f, 0.0f}, + {0.0f, 2.0f, 2.0f, 0.0f}, + {1.0f, 2.0f, 2.0f, 0.0f}, + {2.0f, 2.0f, 2.0f, 0.0f}, + {-1.0f, -1.0f, -1.0f, 1.0f}, + {0.0f, -1.0f, -1.0f, 1.0f}, + {1.0f, -1.0f, -1.0f, 1.0f}, + {2.0f, -1.0f, -1.0f, 1.0f}, + {-1.0f, 0.0f, -1.0f, 1.0f}, + {0.0f, 0.0f, -1.0f, 1.0f}, + {1.0f, 0.0f, -1.0f, 1.0f}, + {2.0f, 0.0f, -1.0f, 1.0f}, + {-1.0f, 1.0f, -1.0f, 1.0f}, + {0.0f, 1.0f, -1.0f, 1.0f}, + {1.0f, 1.0f, -1.0f, 1.0f}, + {2.0f, 1.0f, -1.0f, 1.0f}, + {-1.0f, 2.0f, -1.0f, 1.0f}, + {0.0f, 2.0f, -1.0f, 1.0f}, + {1.0f, 2.0f, -1.0f, 1.0f}, + {2.0f, 2.0f, -1.0f, 1.0f}, + {-1.0f, -1.0f, 0.0f, 1.0f}, + {0.0f, -1.0f, 0.0f, 1.0f}, + {1.0f, -1.0f, 0.0f, 1.0f}, + {2.0f, -1.0f, 0.0f, 1.0f}, + {-1.0f, 0.0f, 0.0f, 1.0f}, + {0.0f, 0.0f, 0.0f, 1.0f}, + {1.0f, 0.0f, 0.0f, 1.0f}, + {2.0f, 0.0f, 0.0f, 1.0f}, + {-1.0f, 1.0f, 0.0f, 1.0f}, + {0.0f, 1.0f, 0.0f, 1.0f}, + {1.0f, 1.0f, 0.0f, 1.0f}, + {2.0f, 1.0f, 0.0f, 1.0f}, + {-1.0f, 2.0f, 0.0f, 1.0f}, + {0.0f, 2.0f, 0.0f, 1.0f}, + {1.0f, 2.0f, 0.0f, 1.0f}, + {2.0f, 2.0f, 0.0f, 1.0f}, + {-1.0f, -1.0f, 1.0f, 1.0f}, + {0.0f, -1.0f, 1.0f, 1.0f}, + {1.0f, -1.0f, 1.0f, 1.0f}, + {2.0f, -1.0f, 1.0f, 1.0f}, + {-1.0f, 0.0f, 1.0f, 1.0f}, + {0.0f, 0.0f, 1.0f, 1.0f}, + {1.0f, 0.0f, 1.0f, 1.0f}, + {2.0f, 0.0f, 1.0f, 1.0f}, + {-1.0f, 1.0f, 1.0f, 1.0f}, + {0.0f, 1.0f, 1.0f, 1.0f}, + {1.0f, 1.0f, 1.0f, 1.0f}, + {2.0f, 1.0f, 1.0f, 1.0f}, + {-1.0f, 2.0f, 1.0f, 1.0f}, + {0.0f, 2.0f, 1.0f, 1.0f}, + {1.0f, 2.0f, 1.0f, 1.0f}, + {2.0f, 2.0f, 1.0f, 1.0f}, + {-1.0f, -1.0f, 2.0f, 1.0f}, + {0.0f, -1.0f, 2.0f, 1.0f}, + {1.0f, -1.0f, 2.0f, 1.0f}, + {2.0f, -1.0f, 2.0f, 1.0f}, + {-1.0f, 0.0f, 2.0f, 1.0f}, + {0.0f, 0.0f, 2.0f, 1.0f}, + {1.0f, 0.0f, 2.0f, 1.0f}, + {2.0f, 0.0f, 2.0f, 1.0f}, + {-1.0f, 1.0f, 2.0f, 1.0f}, + {0.0f, 1.0f, 2.0f, 1.0f}, + {1.0f, 1.0f, 2.0f, 1.0f}, + {2.0f, 1.0f, 2.0f, 1.0f}, + {-1.0f, 2.0f, 2.0f, 1.0f}, + {0.0f, 2.0f, 2.0f, 1.0f}, + {1.0f, 2.0f, 2.0f, 1.0f}, + {2.0f, 2.0f, 2.0f, 1.0f}, + {-1.0f, -1.0f, -1.0f, 2.0f}, + {0.0f, -1.0f, -1.0f, 2.0f}, + {1.0f, -1.0f, -1.0f, 2.0f}, + {2.0f, -1.0f, -1.0f, 2.0f}, + {-1.0f, 0.0f, -1.0f, 2.0f}, + {0.0f, 0.0f, -1.0f, 2.0f}, + {1.0f, 0.0f, -1.0f, 2.0f}, + {2.0f, 0.0f, -1.0f, 2.0f}, + {-1.0f, 1.0f, -1.0f, 2.0f}, + {0.0f, 1.0f, -1.0f, 2.0f}, + {1.0f, 1.0f, -1.0f, 2.0f}, + {2.0f, 1.0f, -1.0f, 2.0f}, + {-1.0f, 2.0f, -1.0f, 2.0f}, + {0.0f, 2.0f, -1.0f, 2.0f}, + {1.0f, 2.0f, -1.0f, 2.0f}, + {2.0f, 2.0f, -1.0f, 2.0f}, + {-1.0f, -1.0f, 0.0f, 2.0f}, + {0.0f, -1.0f, 0.0f, 2.0f}, + {1.0f, -1.0f, 0.0f, 2.0f}, + {2.0f, -1.0f, 0.0f, 2.0f}, + {-1.0f, 0.0f, 0.0f, 2.0f}, + {0.0f, 0.0f, 0.0f, 2.0f}, + {1.0f, 0.0f, 0.0f, 2.0f}, + {2.0f, 0.0f, 0.0f, 2.0f}, + {-1.0f, 1.0f, 0.0f, 2.0f}, + {0.0f, 1.0f, 0.0f, 2.0f}, + {1.0f, 1.0f, 0.0f, 2.0f}, + {2.0f, 1.0f, 0.0f, 2.0f}, + {-1.0f, 2.0f, 0.0f, 2.0f}, + {0.0f, 2.0f, 0.0f, 2.0f}, + {1.0f, 2.0f, 0.0f, 2.0f}, + {2.0f, 2.0f, 0.0f, 2.0f}, + {-1.0f, -1.0f, 1.0f, 2.0f}, + {0.0f, -1.0f, 1.0f, 2.0f}, + {1.0f, -1.0f, 1.0f, 2.0f}, + {2.0f, -1.0f, 1.0f, 2.0f}, + {-1.0f, 0.0f, 1.0f, 2.0f}, + {0.0f, 0.0f, 1.0f, 2.0f}, + {1.0f, 0.0f, 1.0f, 2.0f}, + {2.0f, 0.0f, 1.0f, 2.0f}, + {-1.0f, 1.0f, 1.0f, 2.0f}, + {0.0f, 1.0f, 1.0f, 2.0f}, + {1.0f, 1.0f, 1.0f, 2.0f}, + {2.0f, 1.0f, 1.0f, 2.0f}, + {-1.0f, 2.0f, 1.0f, 2.0f}, + {0.0f, 2.0f, 1.0f, 2.0f}, + {1.0f, 2.0f, 1.0f, 2.0f}, + {2.0f, 2.0f, 1.0f, 2.0f}, + {-1.0f, -1.0f, 2.0f, 2.0f}, + {0.0f, -1.0f, 2.0f, 2.0f}, + {1.0f, -1.0f, 2.0f, 2.0f}, + {2.0f, -1.0f, 2.0f, 2.0f}, + {-1.0f, 0.0f, 2.0f, 2.0f}, + {0.0f, 0.0f, 2.0f, 2.0f}, + {1.0f, 0.0f, 2.0f, 2.0f}, + {2.0f, 0.0f, 2.0f, 2.0f}, + {-1.0f, 1.0f, 2.0f, 2.0f}, + {0.0f, 1.0f, 2.0f, 2.0f}, + {1.0f, 1.0f, 2.0f, 2.0f}, + {2.0f, 1.0f, 2.0f, 2.0f}, + {-1.0f, 2.0f, 2.0f, 2.0f}, + {0.0f, 2.0f, 2.0f, 2.0f}, + {1.0f, 2.0f, 2.0f, 2.0f}, + {2.0f, 2.0f, 2.0f, 2.0f} +}; + +// One block per output row. `staged` says whether x was copied to shared, so the SAME kernel covers both +// configurations and the bench can measure them against each other with nothing else changed. +template +__device__ __forceinline__ void s2_gemv_fast_kernel(const uint16_t* __restrict__ x, const uint8_t* __restrict__ codes, + const float* __restrict__ scales, float* __restrict__ y, long long n_in, + long long n_out, int threads_per_row) { + extern __shared__ __align__(16) unsigned char smem_raw[]; + float* smem = reinterpret_cast(smem_raw); + __half* sx = reinterpret_cast<__half*>(smem); // [threads_per_row, ...] partials live after + float* partial = smem + (STAGE_X ? (MAX_SHARED_HALVES / 2) : 0); + + const long long o = blockIdx.x; + if (o >= n_out) return; + const int tid = threadIdx.x; + + if (STAGE_X) { + const __half* xh = reinterpret_cast(x); + for (long long i = tid; i < n_in; i += threads_per_row) sx[i] = xh[i]; + __syncthreads(); + } + + const long long n_quads = n_in / 4; + const uint8_t* c = codes + o * n_quads; + const float* s = scales + o * (n_in / QK_S2); + + float a0 = 0.0f, a1 = 0.0f, a2 = 0.0f, a3 = 0.0f; + for (long long q = tid; q < n_quads; q += threads_per_row) { + const uint8_t byte = c[q]; // ONE load for four codes + const float d = s[q >> 4]; // (q*4) >> 6 + const float4 cv = *reinterpret_cast(&c_codes[byte][0]); // one broadcast load + if (STAGE_X) { + const __half2 h01 = *reinterpret_cast(&sx[q * 4]); + const __half2 h23 = *reinterpret_cast(&sx[q * 4 + 2]); + a0 += cv.x * d * __low2float(h01); + a1 += cv.y * d * __high2float(h01); + a2 += cv.z * d * __low2float(h23); + a3 += cv.w * d * __high2float(h23); + } else { + const uint2 xw2 = *reinterpret_cast(x + q * 4); + const __half2 h01 = *reinterpret_cast(&xw2.x); + const __half2 h23 = *reinterpret_cast(&xw2.y); + a0 += cv.x * d * __low2float(h01); + a1 += cv.y * d * __high2float(h01); + a2 += cv.z * d * __low2float(h23); + a3 += cv.w * d * __high2float(h23); + } + } + partial[tid] = (a0 + a1) + (a2 + a3); + __syncthreads(); + for (int step = threads_per_row / 2; step > 0; step >>= 1) { + if (tid < step) partial[tid] += partial[tid + step]; + __syncthreads(); + } + if (tid == 0) y[o] = partial[0]; +} + + +// ---- entries ----------------------------------------------------------------------------------------------------- +// Argument order is the Original kernel's. Launch geometry (see s_gemv_legacy.rs): +// leleka_s_gemv_*, leleka_s_gemv_q8k_* : 128 threads, ceil(n_out / 128) blocks, no dynamic shared +// leleka_s_gemv_split_* : threads_per_row threads, n_out blocks, threads_per_row * 4 B dynamic shared +// leleka_s_gemv_q8k_split_* / q8_0_split_* : 256 threads (8 warps), ceil(n_out / 8) blocks, no dynamic shared +// leleka_s2_gemv : 128 threads, ceil(n_out / 128) blocks +// leleka_s2_gemv_quads / _q8 : threads_per_row threads, n_out blocks, threads_per_row * 4 B dynamic shared +// leleka_s2_gemv_fast_{staged,unstaged} : threads_per_row threads, n_out blocks, [8192 B +] threads_per_row * 4 B dynamic shared +#define LELEKA_S_FP16_ARGS const uint16_t* __restrict__ x, const uint8_t* __restrict__ codes, const float* __restrict__ scales, \ + const float* __restrict__ offset, float* __restrict__ y, long long n_in, long long n_out, int bias, int codebook, int group_elems, \ + int has_offset +#define LELEKA_S_Q8_ARGS const uint8_t* __restrict__ x, const uint8_t* __restrict__ codes, const float* __restrict__ scales, \ + const float* __restrict__ offset, float* __restrict__ y, long long n_in, long long n_out, int bias, int codebook, int group_shift, \ + int has_offset + +#define LELEKA_S_GEMV(B) extern "C" __global__ void leleka_s_gemv_##B(LELEKA_S_FP16_ARGS) { \ + s_gemv_kernel(x, codes, scales, offset, y, n_in, n_out, bias, codebook, group_elems, has_offset); } +LELEKA_S_GEMV(2) +LELEKA_S_GEMV(4) +LELEKA_S_GEMV(8) + +#define LELEKA_S_GEMV_SPLIT(B) extern "C" __global__ void leleka_s_gemv_split_##B( \ + const uint16_t* __restrict__ x, const uint8_t* __restrict__ codes, const float* __restrict__ scales, \ + const float* __restrict__ offset, float* __restrict__ y, long long n_in, long long n_out, int bias, int codebook, \ + int group_elems, int group_shift, int has_offset, int threads_per_row) { \ + s_gemv_split_kernel(x, codes, scales, offset, y, n_in, n_out, bias, codebook, group_elems, group_shift, has_offset, \ + threads_per_row); } +LELEKA_S_GEMV_SPLIT(2) +LELEKA_S_GEMV_SPLIT(4) +LELEKA_S_GEMV_SPLIT(8) + +// Q8_K activation, thread-per-row kernel (s_gemv_q8k) +#define LELEKA_S_GEMV_Q8K(B) extern "C" __global__ void leleka_s_gemv_q8k_##B( \ + const uint8_t* __restrict__ x, const uint8_t* __restrict__ codes, const float* __restrict__ scales, \ + const float* __restrict__ offset, float* __restrict__ y, long long n_in, long long n_out, int bias, int codebook, \ + int group_elems, int has_offset) { \ + s_gemv_q8k_kernel(x, codes, scales, offset, y, n_in, n_out, bias, codebook, group_elems, has_offset); } +LELEKA_S_GEMV_Q8K(4) +LELEKA_S_GEMV_Q8K(8) + +// warp-per-row kernels: Q8_K (s_gemv_q8k_split) and Q8_0 (s_gemv_q8_0_split) activations +#define LELEKA_S_GEMV_Q8_SPLIT(NAME, B, IS_Q8K) extern "C" __global__ void NAME(LELEKA_S_Q8_ARGS) { \ + s_gemv_q8_split_kernel(x, codes, scales, offset, y, n_in, n_out, bias, codebook, group_shift, has_offset); } +LELEKA_S_GEMV_Q8_SPLIT(leleka_s_gemv_q8k_split_4, 4, true) +LELEKA_S_GEMV_Q8_SPLIT(leleka_s_gemv_q8k_split_8, 8, true) +LELEKA_S_GEMV_Q8_SPLIT(leleka_s_gemv_q8_0_split_4, 4, false) +LELEKA_S_GEMV_Q8_SPLIT(leleka_s_gemv_q8_0_split_8, 8, false) + +extern "C" __global__ void leleka_s2_gemv(const uint16_t* __restrict__ x, const uint8_t* __restrict__ codes, + const float* __restrict__ scales, float* __restrict__ y, long long n_in, + long long n_out) { + s2_gemv_kernel(x, codes, scales, y, n_in, n_out); +} +extern "C" __global__ void leleka_s2_gemv_quads(const uint16_t* __restrict__ x, const uint8_t* __restrict__ codes, + const float* __restrict__ scales, float* __restrict__ y, long long n_in, + long long n_out, int threads_per_row) { + s2_gemv_quads_kernel(x, codes, scales, y, n_in, n_out, threads_per_row); +} +extern "C" __global__ void leleka_s2_gemv_q8(const uint8_t* __restrict__ act, const uint8_t* __restrict__ codes, + const float* __restrict__ scales, float* __restrict__ y, long long n_in, + long long n_out, int threads_per_row) { + s2_gemv_q8_kernel(act, codes, scales, y, n_in, n_out, threads_per_row); +} +extern "C" __global__ void leleka_s2_gemv_fast_staged(const uint16_t* __restrict__ x, const uint8_t* __restrict__ codes, + const float* __restrict__ scales, float* __restrict__ y, + long long n_in, long long n_out, int threads_per_row) { + s2_gemv_fast_kernel(x, codes, scales, y, n_in, n_out, threads_per_row); +} +extern "C" __global__ void leleka_s2_gemv_fast_unstaged(const uint16_t* __restrict__ x, const uint8_t* __restrict__ codes, + const float* __restrict__ scales, float* __restrict__ y, + long long n_in, long long n_out, int threads_per_row) { + s2_gemv_fast_kernel(x, codes, scales, y, n_in, n_out, threads_per_row); +} diff --git a/crates/cuda-driver/src/s_gemv_legacy.ptx b/crates/cuda-driver/src/s_gemv_legacy.ptx new file mode 100644 index 0000000000000000000000000000000000000000..26364e52a8ee38210fc33045298063cfa980cf35 GIT binary patch literal 114 zcmdPbR|xeBc6Ib|b+J+?&Mz%WPSwrN%c)fGFUm~M%uCEsD2`80%`MYQE>+N1C^iCf zv_QO^)bzyUN`+wEwEUu61$S59uwrY4 Self { + Self { code_bits: 2, code_bias: -1, group_elems: 64, codebook: Codebook::Affine, has_offset: false, act_kind: 0 } + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Launch { + pub grid: [u32; 3], + pub block: [u32; 3], + pub dynamic_shared: u32, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Kernel { + SGemv(u8), + SGemvSplit(u8), + SGemvQ8k(u8), + SGemvQ8kSplit(u8), + SGemvQ80Split(u8), + S2Gemv, + S2GemvQuads, + S2GemvQ8, + S2GemvFastStaged, + S2GemvFastUnstaged, +} +impl Kernel { + /// Export name in `s_gemv_legacy.cu` / its PTX. + pub fn entry(self) -> &'static str { + match self { + Self::SGemv(2) => "leleka_s_gemv_2", + Self::SGemv(4) => "leleka_s_gemv_4", + Self::SGemv(8) => "leleka_s_gemv_8", + Self::SGemvSplit(2) => "leleka_s_gemv_split_2", + Self::SGemvSplit(4) => "leleka_s_gemv_split_4", + Self::SGemvSplit(8) => "leleka_s_gemv_split_8", + Self::SGemvQ8k(4) => "leleka_s_gemv_q8k_4", + Self::SGemvQ8k(8) => "leleka_s_gemv_q8k_8", + Self::SGemvQ8kSplit(4) => "leleka_s_gemv_q8k_split_4", + Self::SGemvQ8kSplit(8) => "leleka_s_gemv_q8k_split_8", + Self::SGemvQ80Split(4) => "leleka_s_gemv_q8_0_split_4", + Self::SGemvQ80Split(8) => "leleka_s_gemv_q8_0_split_8", + Self::S2Gemv => "leleka_s2_gemv", + Self::S2GemvQuads => "leleka_s2_gemv_quads", + Self::S2GemvQ8 => "leleka_s2_gemv_q8", + Self::S2GemvFastStaged => "leleka_s2_gemv_fast_staged", + Self::S2GemvFastUnstaged => "leleka_s2_gemv_fast_unstaged", + _ => unreachable!("planners only construct widths the entry table has"), + } + } + /// Every entry the module exports. + pub const ALL_ENTRIES: [&'static str; 17] = [ + "leleka_s_gemv_2", + "leleka_s_gemv_4", + "leleka_s_gemv_8", + "leleka_s_gemv_split_2", + "leleka_s_gemv_split_4", + "leleka_s_gemv_split_8", + "leleka_s_gemv_q8k_4", + "leleka_s_gemv_q8k_8", + "leleka_s_gemv_q8k_split_4", + "leleka_s_gemv_q8k_split_8", + "leleka_s_gemv_q8_0_split_4", + "leleka_s_gemv_q8_0_split_8", + "leleka_s2_gemv", + "leleka_s2_gemv_quads", + "leleka_s2_gemv_q8", + "leleka_s2_gemv_fast_staged", + "leleka_s2_gemv_fast_unstaged", + ]; +} + +/// Everything a caller needs to launch: the entry, the geometry and the `group_shift` argument (log2 of the group; 0 where +/// the kernel takes `group_elems` instead). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Plan { + pub kernel: Kernel, + pub launch: Launch, + pub group_shift: i32, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum Error { + /// ": n_in is not a multiple of group_elems " + NInNotGroupMultiple { who: &'static str, n_in: i64, group: i32 }, + /// "s_gemv: form says has_offset but offset is null" + OffsetMissing { who: &'static str }, + /// "s_gemv: unsupported code_bits " + UnsupportedCodeBits { who: &'static str, bits: i32 }, + /// "s_gemv_q8k: code_bits has no Q8_K contract (S2 uses Q8_0; see docs/activation-contract.md)" and the Q8_0 twin + NoActivationContract { who: &'static str, bits: i32 }, + /// ": n_in is not a multiple of the Q8_K block 256" (or of 32 for Q8_0 / the S2 launchers' 64 / 4) + NInNotBlockMultiple { who: &'static str, n_in: i64, block: i64 }, + /// ": group_elems must be a power of two, got " + GroupNotPowerOfTwo { who: &'static str, group: i32 }, + /// ": group_elems is not a multiple of " (4 for the fp16 split kernel, 16 for the Q8 kernels) + GroupNotMultiple { who: &'static str, group: i32, multiple: i32 }, + /// "s_gemv_split: threads_per_row must be a power of two in 1..1024, got " (also stricter-than-Original for s2_*) + ThreadsPerRow { who: &'static str, threads: i32 }, + /// "s2_gemv_fast: n_in exceeds the 4096-half shared staging limit" + StagingLimit { n_in: i64 }, + /// `(unsigned) n_out` would truncate (the grid is one block per row, or ceil(n_out / rows-per-block) blocks). + GridTooLarge { who: &'static str, n_out: i64 }, +} +impl Error { + pub fn message(&self) -> String { + match self { + Self::NInNotGroupMultiple { who, n_in, group } => format!("{who}: n_in {n_in} is not a multiple of group_elems {group}"), + Self::OffsetMissing { who } => format!("{who}: form says has_offset but offset is null"), + Self::UnsupportedCodeBits { who, bits } => format!("{who}: unsupported code_bits {bits}"), + Self::NoActivationContract { who, bits } => format!("{who}: code_bits {bits} has no activation contract for this kernel"), + Self::NInNotBlockMultiple { who, n_in, block } => format!("{who}: n_in {n_in} is not a multiple of {block}"), + Self::GroupNotPowerOfTwo { who, group } => format!("{who}: group_elems must be a power of two, got {group}"), + Self::GroupNotMultiple { who, group, multiple } => format!("{who}: group_elems {group} is not a multiple of {multiple}"), + Self::ThreadsPerRow { who, threads } => format!("{who}: threads_per_row must be a power of two in 1..1024, got {threads}"), + Self::StagingLimit { n_in } => { + format!("s2_gemv_fast: n_in {n_in} exceeds the {S2_FAST_MAX_SHARED_HALVES}-half shared staging limit") + } + Self::GridTooLarge { who, n_out } => format!("{who}: n_out {n_out} does not fit a CUDA grid dimension"), + } + } +} + +fn grid_for(who: &'static str, blocks: i64) -> Result { + u32::try_from(blocks).map_err(|_| Error::GridTooLarge { who, n_out: blocks }) +} +fn ceil_div(a: i64, b: i64) -> i64 { + (a + b - 1) / b +} +fn group_shift_of(group: i32) -> i32 { + let mut s = 0; + while (1i32 << s) < group { + s += 1; + } + s +} +fn check_tpr(who: &'static str, tpr: i32) -> Result<(), Error> { + if tpr < 1 || tpr > 1024 || (tpr & (tpr - 1)) != 0 { Err(Error::ThreadsPerRow { who, threads: tpr }) } else { Ok(()) } +} +fn bits_u8(who: &'static str, bits: i32, allowed: &[i32], contract: bool) -> Result { + if allowed.contains(&bits) { + Ok(bits as u8) + } else if contract { + Err(Error::NoActivationContract { who, bits }) + } else { + Err(Error::UnsupportedCodeBits { who, bits }) + } +} + +/// `s_gemv`: one thread per output row, 128 threads, `ceil(n_out / 128)` blocks. `offset_present` is `offset != nullptr`. +pub fn plan_s_gemv(n_in: i64, n_out: i64, form: &SForm, offset_present: bool) -> Result, Error> { + const WHO: &str = "s_gemv"; + if n_in <= 0 || n_out <= 0 { + return Ok(None); + } + if form.group_elems <= 0 || n_in % form.group_elems as i64 != 0 { + return Err(Error::NInNotGroupMultiple { who: WHO, n_in, group: form.group_elems }); + } + if form.has_offset && !offset_present { + return Err(Error::OffsetMissing { who: WHO }); + } + let bits = bits_u8(WHO, form.code_bits, &[2, 4, 8], false)?; + let grid = grid_for(WHO, ceil_div(n_out, 128))?; + Ok(Some(Plan { kernel: Kernel::SGemv(bits), launch: Launch { grid: [grid, 1, 1], block: [128, 1, 1], dynamic_shared: 0 }, group_shift: 0 })) +} + +/// `s_gemv_split` / `s_gemv_split_async`: one block of `threads_per_row` threads per row, `threads_per_row * 4` bytes of +/// dynamic shared memory for the reduction tree. +pub fn plan_s_gemv_split(n_in: i64, n_out: i64, form: &SForm, threads_per_row: i32, offset_present: bool) -> Result, Error> { + const WHO: &str = "s_gemv_split"; + if n_in <= 0 || n_out <= 0 { + return Ok(None); + } + check_tpr(WHO, threads_per_row)?; + if form.group_elems <= 0 || (form.group_elems & (form.group_elems - 1)) != 0 { + return Err(Error::GroupNotPowerOfTwo { who: WHO, group: form.group_elems }); + } + if form.group_elems % 4 != 0 { + return Err(Error::GroupNotMultiple { who: WHO, group: form.group_elems, multiple: 4 }); + } + if n_in % form.group_elems as i64 != 0 { + return Err(Error::NInNotGroupMultiple { who: WHO, n_in, group: form.group_elems }); + } + if form.has_offset && !offset_present { + return Err(Error::OffsetMissing { who: WHO }); + } + let bits = bits_u8(WHO, form.code_bits, &[2, 4, 8], false)?; + let grid = grid_for(WHO, n_out)?; + Ok(Some(Plan { + kernel: Kernel::SGemvSplit(bits), + launch: Launch { grid: [grid, 1, 1], block: [threads_per_row as u32, 1, 1], dynamic_shared: threads_per_row as u32 * 4 }, + group_shift: group_shift_of(form.group_elems), + })) +} + +fn q8_form_ok(who: &'static str, form: &SForm, n_in: i64) -> Result<(), Error> { + if form.group_elems <= 0 || n_in % form.group_elems as i64 != 0 { + return Err(Error::NInNotGroupMultiple { who, n_in, group: form.group_elems }); + } + if n_in % Q8K_BLOCK_ELEMS as i64 != 0 { + return Err(Error::NInNotBlockMultiple { who, n_in, block: Q8K_BLOCK_ELEMS as i64 }); + } + Ok(()) +} + +/// `s_gemv_q8k`: thread per row, Q8_K activation, 4- and 8-bit forms only (S2 uses Q8_0). 128 threads. +pub fn plan_s_gemv_q8k(n_in: i64, n_out: i64, form: &SForm) -> Result, Error> { + const WHO: &str = "s_gemv_q8k"; + if n_in <= 0 || n_out <= 0 { + return Ok(None); + } + q8_form_ok(WHO, form, n_in)?; + let bits = bits_u8(WHO, form.code_bits, &[4, 8], true)?; + let grid = grid_for(WHO, ceil_div(n_out, 128))?; + Ok(Some(Plan { kernel: Kernel::SGemvQ8k(bits), launch: Launch { grid: [grid, 1, 1], block: [128, 1, 1], dynamic_shared: 0 }, group_shift: 0 })) +} + +/// `s_gemv_q8k_split`: one warp per row, 256 threads (8 rows per block), Q8_K activation. +pub fn plan_s_gemv_q8k_split(n_in: i64, n_out: i64, form: &SForm) -> Result, Error> { + const WHO: &str = "s_gemv_q8k_split"; + if n_in <= 0 || n_out <= 0 { + return Ok(None); + } + q8_form_ok(WHO, form, n_in)?; + let shift = group_shift_of(form.group_elems); + if (1i32 << shift) != form.group_elems { + return Err(Error::GroupNotPowerOfTwo { who: WHO, group: form.group_elems }); + } + if form.group_elems % 16 != 0 { + return Err(Error::GroupNotMultiple { who: WHO, group: form.group_elems, multiple: 16 }); + } + let bits = bits_u8(WHO, form.code_bits, &[4, 8], true)?; + let grid = grid_for(WHO, ceil_div(n_out, 8))?; + Ok(Some(Plan { kernel: Kernel::SGemvQ8kSplit(bits), launch: Launch { grid: [grid, 1, 1], block: [256, 1, 1], dynamic_shared: 0 }, group_shift: shift })) +} + +/// `s_gemv_q8_0_split`: the same warp-per-row kernel over `block_q8_0` activations (`n_in % 32 == 0` only). +pub fn plan_s_gemv_q8_0_split(n_in: i64, n_out: i64, form: &SForm) -> Result, Error> { + const WHO: &str = "s_gemv_q8_0_split"; + if n_in <= 0 || n_out <= 0 { + return Ok(None); + } + if n_in % Q8_0_BLOCK_ELEMS as i64 != 0 { + return Err(Error::NInNotBlockMultiple { who: WHO, n_in, block: Q8_0_BLOCK_ELEMS as i64 }); + } + if form.group_elems <= 0 || (form.group_elems & (form.group_elems - 1)) != 0 { + return Err(Error::GroupNotPowerOfTwo { who: WHO, group: form.group_elems }); + } + if form.group_elems % 16 != 0 { + return Err(Error::GroupNotMultiple { who: WHO, group: form.group_elems, multiple: 16 }); + } + let bits = bits_u8(WHO, form.code_bits, &[4, 8], true)?; + let grid = grid_for(WHO, ceil_div(n_out, 8))?; + Ok(Some(Plan { + kernel: Kernel::SGemvQ80Split(bits), + launch: Launch { grid: [grid, 1, 1], block: [256, 1, 1], dynamic_shared: 0 }, + group_shift: group_shift_of(form.group_elems), + })) +} + +/// `s2_gemv`: the naive S2 kernel, 128 threads per block. +pub fn plan_s2_gemv(n_in: i64, n_out: i64) -> Result, Error> { + const WHO: &str = "s2_gemv"; + if n_in <= 0 || n_out <= 0 { + return Ok(None); + } + if n_in % S2_QK != 0 { + return Err(Error::NInNotBlockMultiple { who: WHO, n_in, block: S2_QK }); + } + let grid = grid_for(WHO, ceil_div(n_out, 128))?; + Ok(Some(Plan { kernel: Kernel::S2Gemv, launch: Launch { grid: [grid, 1, 1], block: [128, 1, 1], dynamic_shared: 0 }, group_shift: 0 })) +} + +/// `s2_gemv_quads`: one block per row; the Original only checks `n_in % 4`. +pub fn plan_s2_gemv_quads(n_in: i64, n_out: i64, threads_per_row: i32) -> Result, Error> { + const WHO: &str = "s2_gemv_quads"; + if n_in <= 0 || n_out <= 0 { + return Ok(None); + } + if n_in % 4 != 0 { + return Err(Error::NInNotBlockMultiple { who: WHO, n_in, block: 4 }); + } + check_tpr(WHO, threads_per_row)?; + let grid = grid_for(WHO, n_out)?; + Ok(Some(Plan { + kernel: Kernel::S2GemvQuads, + launch: Launch { grid: [grid, 1, 1], block: [threads_per_row as u32, 1, 1], dynamic_shared: threads_per_row as u32 * 4 }, + group_shift: 0, + })) +} + +/// `s2_gemv_q8`: S2 weights, `block_q8_0` activations; `n_in` a multiple of 64 (hence of 32). +pub fn plan_s2_gemv_q8(n_in: i64, n_out: i64, threads_per_row: i32) -> Result, Error> { + const WHO: &str = "s2_gemv_q8"; + if n_in <= 0 || n_out <= 0 { + return Ok(None); + } + if n_in % 32 != 0 || n_in % S2_QK != 0 { + return Err(Error::NInNotBlockMultiple { who: WHO, n_in, block: S2_QK }); + } + check_tpr(WHO, threads_per_row)?; + let grid = grid_for(WHO, n_out)?; + Ok(Some(Plan { + kernel: Kernel::S2GemvQ8, + launch: Launch { grid: [grid, 1, 1], block: [threads_per_row as u32, 1, 1], dynamic_shared: threads_per_row as u32 * 4 }, + group_shift: 0, + })) +} + +/// `s2_gemv_fast` (measured negative in the Original; kept as the experiment record, not a fast path). The constant code +/// table is static in the PTX, so there is no `ensure_lut` step. Dynamic shared memory: `threads_per_row * 4` plus, when +/// `x` is staged, `4096 * 2` bytes in front. +pub fn plan_s2_gemv_fast(n_in: i64, n_out: i64, threads_per_row: i32, stage_x: bool) -> Result, Error> { + const WHO: &str = "s2_gemv_fast"; + if n_in <= 0 || n_out <= 0 { + return Ok(None); + } + if n_in % 4 != 0 || n_in % S2_QK != 0 { + return Err(Error::NInNotBlockMultiple { who: WHO, n_in, block: S2_QK }); + } + check_tpr(WHO, threads_per_row)?; + if stage_x && n_in > S2_FAST_MAX_SHARED_HALVES { + return Err(Error::StagingLimit { n_in }); + } + let grid = grid_for(WHO, n_out)?; + let smem = if stage_x { S2_FAST_MAX_SHARED_HALVES as u32 * 2 } else { 0 } + threads_per_row as u32 * 4; + Ok(Some(Plan { + kernel: if stage_x { Kernel::S2GemvFastStaged } else { Kernel::S2GemvFastUnstaged }, + launch: Launch { grid: [grid, 1, 1], block: [threads_per_row as u32, 1, 1], dynamic_shared: smem }, + group_shift: 0, + })) +} + +// ---- CPU models ------------------------------------------------------------------------------------------------------- + +/// binary16 bits to f32 (exact). +pub fn f16_to_f32(bits: u16) -> f32 { + let sign = if bits & 0x8000 != 0 { -1.0f32 } else { 1.0 }; + let exp = ((bits >> 10) & 0x1f) as i32; + let man = (bits & 0x3ff) as f32; + match exp { + 0 => sign * man * 2f32.powi(-24), + 31 => { + if man == 0.0 { + sign * f32::INFINITY + } else { + f32::NAN + } + } + _ => sign * (1.0 + man / 1024.0) * 2f32.powi(exp - 15), + } +} + +/// Code of element `e` of a row: `(c[e / per] >> ((e % per) * bits)) & mask`. +pub fn code_at(row_codes: &[u8], bits: u32, e: usize) -> u32 { + let per = (8 / bits) as usize; + ((row_codes[e / per] as u32) >> ((e % per) as u32 * bits)) & ((1u32 << bits) - 1) +} +/// Pack raw codes (each `< 2^bits`) little-endian within each byte. +pub fn pack_codes(raw: &[u8], bits: u32) -> Vec { + let per = (8 / bits) as usize; + let mut out = vec![0u8; raw.len() / per]; + for (e, &c) in raw.iter().enumerate() { + out[e / per] |= c << ((e % per) as u32 * bits); + } + out +} +/// `decode_tbl`: the integer code mapped to a weight before scale/offset. +pub fn decode(code: u32, bias: i32, codebook: Codebook) -> f32 { + match codebook { + Codebook::Iq4Nl => IQ4NL[(code & 0x0f) as usize] as f32, + Codebook::Affine => (code as i32 + bias) as f32, + } +} + +/// `block_q8_K` activation to f32 values (`d * q`, the single f32 multiply the kernels perform). +pub fn q8k_dequant(buf: &[u8], n: usize) -> Vec { + (0..n) + .map(|i| { + let blk = &buf[(i / Q8K_BLOCK_ELEMS) * Q8K_BLOCK_BYTES..]; + let d = f32::from_le_bytes([blk[0], blk[1], blk[2], blk[3]]); + d * (blk[4 + i % Q8K_BLOCK_ELEMS] as i8) as f32 + }) + .collect() +} +/// `block_q8_0` activation to f32 values. +pub fn q8_0_dequant(buf: &[u8], n: usize) -> Vec { + (0..n) + .map(|i| { + let blk = &buf[(i / Q8_0_BLOCK_ELEMS) * Q8_0_BLOCK_BYTES..]; + let d = f16_to_f32(u16::from_le_bytes([blk[0], blk[1]])); + d * (blk[2 + i % Q8_0_BLOCK_ELEMS] as i8) as f32 + }) + .collect() +} + +/// A weight matrix in S-form with its row geometry. +pub struct Weights<'a> { + pub codes: &'a [u8], + pub scales: &'a [f32], + pub offset: Option<&'a [f32]>, + pub form: SForm, + pub n_in: usize, + pub n_out: usize, +} +impl Weights<'_> { + fn per(&self) -> usize { + (8 / self.form.code_bits) as usize + } + fn row_codes(&self, o: usize) -> &[u8] { + let n = self.n_in / self.per(); + &self.codes[o * n..(o + 1) * n] + } + /// `decode(code) * d + b` for element `e` of row `o`, exactly the kernels' expression (two roundings). + fn weight(&self, o: usize, e: usize) -> f32 { + let g = o * (self.n_in / self.form.group_elems as usize) + e / self.form.group_elems as usize; + let d = self.scales[g]; + let b = self.offset.map_or(0.0, |off| off[g]); + decode(code_at(self.row_codes(o), self.form.code_bits as u32, e), self.form.code_bias, self.form.codebook) * d + b + } +} + +/// FP64 specification: `y[o] = sum_i w[o][i] * x[i]`. +pub fn reference_f64(w: &Weights, x: &[f32]) -> Vec { + (0..w.n_out).map(|o| (0..w.n_in).map(|e| w.weight(o, e) as f64 * x[e] as f64).sum()).collect() +} +/// Literal model of `s_gemv_kernel` / `s2_gemv_kernel`: one accumulator, group by group. +pub fn naive_f32(w: &Weights, x: &[f32]) -> Vec { + (0..w.n_out) + .map(|o| { + let mut acc = 0.0f32; + for e in 0..w.n_in { + acc += w.weight(o, e) * x[e]; + } + acc + }) + .collect() +} +/// Literal model of `s_gemv_split_kernel`: per thread a stride of QE=4 consecutive elements into four accumulators, +/// `(a0+a1)+(a2+a3)`, then the shared-memory tree `step = tpr/2 .. 1`. +pub fn split_f32(w: &Weights, x: &[f32], tpr: usize) -> Vec { + const QE: usize = 4; + (0..w.n_out) + .map(|o| { + let mut partial = vec![0.0f32; tpr]; + for (tid, slot) in partial.iter_mut().enumerate() { + let mut a = [0.0f32; QE]; + let mut i = tid * QE; + while i + QE <= w.n_in { + for k in 0..QE { + a[k] += w.weight(o, i + k) * x[i + k]; + } + i += tpr * QE; + } + while i < w.n_in { + let mut k = 0; + while k < QE && i + k < w.n_in { + a[0] += w.weight(o, i + k) * x[i + k]; + k += 1; + } + i += tpr * QE; + } + *slot = (a[0] + a[1]) + (a[2] + a[3]); + } + let mut step = tpr / 2; + while step > 0 { + for tid in 0..step { + partial[tid] += partial[tid + step]; + } + step >>= 1; + } + partial[0] + }) + .collect() +} +/// Literal model of `s_gemv_q8_split_kernel` (warp per row): lane `l` takes 16 consecutive elements at `l*16 + 512*k` into +/// sixteen accumulators; `acc = (((a0+a1)+(a2+a3))+((a4+a5)+(a6+a7))) + (((a8+a9)+(a10+a11))+((a12+a13)+(a14+a15)))`; lane 0 +/// then reads the `__shfl_down_sync` tree with steps 16, 8, 4, 2, 1. `act` holds the dequantised activations (`d * q`). +pub fn q8_split_f32(w: &Weights, act: &[f32]) -> Vec { + const QE: usize = 16; + (0..w.n_out) + .map(|o| { + let mut lanes = [0.0f32; 32]; + for (lane, slot) in lanes.iter_mut().enumerate() { + let mut a = [0.0f32; QE]; + let mut i = lane * QE; + while i + QE <= w.n_in { + for k in 0..QE { + a[k] += w.weight(o, i + k) * act[i + k]; + } + i += 32 * QE; + } + while i < w.n_in { + let mut k = 0; + while k < QE && i + k < w.n_in { + a[0] += w.weight(o, i + k) * act[i + k]; + k += 1; + } + i += 32 * QE; + } + *slot = (((a[0] + a[1]) + (a[2] + a[3])) + ((a[4] + a[5]) + (a[6] + a[7]))) + + (((a[8] + a[9]) + (a[10] + a[11])) + ((a[12] + a[13]) + (a[14] + a[15]))); + } + for step in [16usize, 8, 4, 2, 1] { + let old = lanes; + for l in 0..32 { + if l + step < 32 { + lanes[l] = old[l] + old[l + step]; + } + } + } + lanes[0] + }) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::Value; + + fn golden() -> Value { + serde_json::from_str(include_str!("../oracle/fixtures/s_gemv_legacy_golden.json")).unwrap() + } + fn u8s(v: &Value) -> Vec { + v.as_array().unwrap().iter().map(|x| x.as_u64().unwrap() as u8).collect() + } + fn f32s(v: &Value) -> Vec { + v.as_array().unwrap().iter().map(|x| x.as_f64().unwrap() as f32).collect() + } + fn form(c: &Value) -> SForm { + SForm { + code_bits: c["bits"].as_i64().unwrap() as i32, + code_bias: c["bias"].as_i64().unwrap() as i32, + group_elems: c["group"].as_i64().unwrap() as i32, + codebook: if c["codebook"].as_i64().unwrap() == 1 { Codebook::Iq4Nl } else { Codebook::Affine }, + has_offset: c["has_offset"].as_i64().unwrap() == 1, + act_kind: 0, + } + } + fn bits_eq(a: &[f32], b: &[f32], what: &str) { + assert_eq!(a.len(), b.len(), "{what}"); + for (i, (x, y)) in a.iter().zip(b).enumerate() { + assert_eq!(x.to_bits(), y.to_bits(), "{what}[{i}]: model {x} vs shim-kernel {y}"); + } + } + + #[test] + fn golden_cases_match_the_shim_executed_kernels_bit_for_bit() { + let g = golden(); + let mut checked = 0; + for c in g["cases"].as_array().unwrap() { + let name = c["name"].as_str().unwrap(); + let f = form(c); + let (n_in, n_out) = (c["n_in"].as_u64().unwrap() as usize, c["n_out"].as_u64().unwrap() as usize); + let codes = u8s(&c["codes"]); + let scales = f32s(&c["scales"]); + let offset = if f.has_offset { Some(f32s(&c["offset"])) } else { None }; + let w = Weights { codes: &codes, scales: &scales, offset: offset.as_deref(), form: f, n_in, n_out }; + let exact = c["exact"].as_i64().unwrap() == 1; + // fp16 activations + let x: Vec = c["x"].as_array().unwrap().iter().map(|v| f16_to_f32(v.as_u64().unwrap() as u16)).collect(); + let y = f32s(&c["y_fp16"]); + bits_eq(&split_f32(&w, &x, 64), &y, &format!("{name} split64")); + if exact { + // every order is exact: the naive kernel, the FP64 spec and the split kernel coincide + bits_eq(&naive_f32(&w, &x), &y, &format!("{name} naive")); + let r: Vec = reference_f64(&w, &x).iter().map(|&v| v as f32).collect(); + bits_eq(&r, &y, &format!("{name} f64")); + } + // Q8_K activations (4- and 8-bit forms with n_in % 256 == 0) + let q8k = u8s(&c["q8k"]); + if f.code_bits != 2 && !q8k.is_empty() { + let a = q8k_dequant(&q8k, n_in); + bits_eq(&q8_split_f32(&w, &a), &f32s(&c["y_q8k"]), &format!("{name} q8k split")); + } + // Q8_0 activations (the S2 entry is s2_gemv_q8, whose tree is the quad kernel's) + let q80 = u8s(&c["q80"]); + if !q80.is_empty() { + let a = q8_0_dequant(&q80, n_in); + let y80 = f32s(&c["y_q80"]); + if f.code_bits != 2 { + bits_eq(&q8_split_f32(&w, &a), &y80, &format!("{name} q8_0 split")); + } else { + assert!(exact); + let r: Vec = reference_f64(&w, &a).iter().map(|&v| v as f32).collect(); + bits_eq(&r, &y80, &format!("{name} s2_gemv_q8")); + } + } + checked += 1; + } + assert_eq!(checked, 8); + } + + #[test] + fn pack_unpack_roundtrip_all_widths() { + for bits in [2u32, 4, 8] { + let raw: Vec = (0..64u32).map(|i| ((i * 37 + 5) & ((1 << bits) - 1)) as u8).collect(); + let packed = pack_codes(&raw, bits); + assert_eq!(packed.len(), 64 * bits as usize / 8); + for (e, &r) in raw.iter().enumerate() { + assert_eq!(code_at(&packed, bits, e), r as u32); + } + } + } + + #[test] + fn f16_conversion_covers_normal_subnormal_and_special() { + assert_eq!(f16_to_f32(0x3c00), 1.0); + assert_eq!(f16_to_f32(0xc000), -2.0); + assert_eq!(f16_to_f32(0x0001), 2f32.powi(-24)); + assert_eq!(f16_to_f32(0x7bff), 65504.0); + assert!(f16_to_f32(0x7c00).is_infinite()); + assert!(f16_to_f32(0x7e00).is_nan()); + } + + #[test] + fn cuda_source_carries_the_codebook_and_every_entry() { + let src = include_str!("s_gemv_legacy.cu"); + let table = IQ4NL.iter().map(|v| v.to_string()).collect::>().join(", "); + let squashed: String = src.split_whitespace().collect::>().join(" "); + assert!(squashed.contains(&format!("kIq4Nl[16] = {{{table}}};")), "codebook drifted from s_gemv.cu"); + for b in [2, 4, 8] { + assert!(src.contains(&format!("LELEKA_S_GEMV({b})")) && src.contains(&format!("LELEKA_S_GEMV_SPLIT({b})"))); + } + for b in [4, 8] { + assert!(src.contains(&format!("LELEKA_S_GEMV_Q8K({b})"))); + } + for e in Kernel::ALL_ENTRIES { + // macro-expanded entries are checked above through their invocations; the explicit ones by name + let direct = src.contains(&format!("void {e}(")); + let via_macro = e.starts_with("leleka_s_gemv_") && !e.contains("q8k_split") && !e.contains("q8_0_split"); + let split = (e.contains("q8k_split") || e.contains("q8_0_split")) && src.contains(&format!("LELEKA_S_GEMV_Q8_SPLIT({e},")); + assert!(direct || via_macro || split, "entry {e} not found in s_gemv_legacy.cu"); + } + // the s2_gemv_fast code table was generated: row b is [(b>>2k & 3) - 1 for k in 0..4] + assert!(src.contains("{-1.0f, -1.0f, -1.0f, -1.0f},")); + assert!(src.contains("{2.0f, 2.0f, 2.0f, 2.0f}\n};")); + assert!(src.contains("{0.0f, -1.0f, -1.0f, -1.0f},")); + } + + #[test] + fn entry_names_are_distinct_and_match_kernel_enum() { + let mut all: Vec<&str> = Kernel::ALL_ENTRIES.to_vec(); + all.sort(); + all.dedup(); + assert_eq!(all.len(), 17); + assert_eq!(Kernel::SGemv(8).entry(), "leleka_s_gemv_8"); + assert_eq!(Kernel::SGemvQ80Split(4).entry(), "leleka_s_gemv_q8_0_split_4"); + assert_eq!(Kernel::S2GemvFastUnstaged.entry(), "leleka_s2_gemv_fast_unstaged"); + } + + fn s4() -> SForm { + SForm { code_bits: 4, code_bias: -8, group_elems: 32, codebook: Codebook::Affine, has_offset: false, act_kind: 0 } + } + + #[test] + fn plan_s_gemv_geometry_and_refusals() { + let p = plan_s_gemv(2560, 10240, &s4(), false).unwrap().unwrap(); + assert_eq!(p.kernel, Kernel::SGemv(4)); + assert_eq!(p.launch, Launch { grid: [80, 1, 1], block: [128, 1, 1], dynamic_shared: 0 }); + assert_eq!(plan_s_gemv(0, 5, &s4(), false), Ok(None)); + assert_eq!(plan_s_gemv(2560, 0, &s4(), false), Ok(None)); + assert_eq!(plan_s_gemv(100, 8, &s4(), false), Err(Error::NInNotGroupMultiple { who: "s_gemv", n_in: 100, group: 32 })); + let mut f = s4(); + f.has_offset = true; + assert_eq!(plan_s_gemv(64, 8, &f, false), Err(Error::OffsetMissing { who: "s_gemv" })); + assert!(plan_s_gemv(64, 8, &f, true).is_ok()); + f.code_bits = 3; + assert_eq!(plan_s_gemv(64, 8, &f, true), Err(Error::UnsupportedCodeBits { who: "s_gemv", bits: 3 })); + assert_eq!(Error::NInNotGroupMultiple { who: "s_gemv", n_in: 100, group: 32 }.message(), "s_gemv: n_in 100 is not a multiple of group_elems 32"); + } + + #[test] + fn plan_split_uses_a_row_per_block_and_shift_group() { + let f = SForm { code_bits: 2, code_bias: -1, group_elems: 64, ..SForm::default() }; + let p = plan_s_gemv_split(2560, 640, &f, 64, false).unwrap().unwrap(); + assert_eq!(p.kernel, Kernel::SGemvSplit(2)); + assert_eq!(p.group_shift, 6); + assert_eq!(p.launch, Launch { grid: [640, 1, 1], block: [64, 1, 1], dynamic_shared: 256 }); + for t in [0, 3, 2048, -1] { + assert_eq!(plan_s_gemv_split(2560, 640, &f, t, false), Err(Error::ThreadsPerRow { who: "s_gemv_split", threads: t })); + } + let odd = SForm { group_elems: 48, ..f }; + assert_eq!(plan_s_gemv_split(2560, 8, &odd, 32, false), Err(Error::GroupNotPowerOfTwo { who: "s_gemv_split", group: 48 })); + let two = SForm { group_elems: 2, ..f }; + assert_eq!(plan_s_gemv_split(64, 8, &two, 32, false), Err(Error::GroupNotMultiple { who: "s_gemv_split", group: 2, multiple: 4 })); + // stricter than the Original: n_in must be a group multiple + assert!(plan_s_gemv_split(2562, 8, &f, 32, false).is_err()); + } + + #[test] + fn plan_q8_kernels_enforce_the_activation_contract() { + let f = SForm { code_bits: 4, code_bias: 0, group_elems: 32, codebook: Codebook::Iq4Nl, has_offset: false, act_kind: 1 }; + let p = plan_s_gemv_q8k_split(2560, 10240, &f).unwrap().unwrap(); + assert_eq!(p.launch, Launch { grid: [1280, 1, 1], block: [256, 1, 1], dynamic_shared: 0 }); + assert_eq!(p.group_shift, 5); + assert_eq!(plan_s_gemv_q8k(2560, 10240, &f).unwrap().unwrap().launch.grid, [80, 1, 1]); + // 640 is a multiple of 32 but not of 256: only the Q8_0 kernel serves ffn_down_shexp + assert_eq!(plan_s_gemv_q8k_split(640, 2560, &f), Err(Error::NInNotBlockMultiple { who: "s_gemv_q8k_split", n_in: 640, block: 256 })); + let p0 = plan_s_gemv_q8_0_split(640, 2560, &f).unwrap().unwrap(); + assert_eq!(p0.kernel, Kernel::SGemvQ80Split(4)); + assert_eq!(p0.launch.grid, [320, 1, 1]); + assert_eq!(plan_s_gemv_q8_0_split(100, 8, &f), Err(Error::NInNotBlockMultiple { who: "s_gemv_q8_0_split", n_in: 100, block: 32 })); + let g8 = SForm { group_elems: 8, ..f }; + assert_eq!(plan_s_gemv_q8_0_split(640, 8, &g8), Err(Error::GroupNotMultiple { who: "s_gemv_q8_0_split", group: 8, multiple: 16 })); + let s2 = SForm { code_bits: 2, ..f }; + assert!(matches!(plan_s_gemv_q8k(256, 8, &s2), Err(Error::NoActivationContract { bits: 2, .. }))); + assert!(matches!(plan_s_gemv_q8k_split(256, 8, &s2), Err(Error::NoActivationContract { bits: 2, .. }))); + assert!(matches!(plan_s_gemv_q8_0_split(256, 8, &s2), Err(Error::NoActivationContract { bits: 2, .. }))); + } + + #[test] + fn plan_s2_kernels() { + assert_eq!(plan_s2_gemv(2560, 300).unwrap().unwrap().launch, Launch { grid: [3, 1, 1], block: [128, 1, 1], dynamic_shared: 0 }); + assert!(plan_s2_gemv(100, 8).is_err()); + let q = plan_s2_gemv_quads(2560, 640, 32).unwrap().unwrap(); + assert_eq!(q.launch, Launch { grid: [640, 1, 1], block: [32, 1, 1], dynamic_shared: 128 }); + assert_eq!(plan_s2_gemv_quads(6, 8, 32), Err(Error::NInNotBlockMultiple { who: "s2_gemv_quads", n_in: 6, block: 4 })); + assert!(plan_s2_gemv_quads(64, 8, 24).is_err()); + assert_eq!(plan_s2_gemv_q8(640, 8, 64).unwrap().unwrap().launch.dynamic_shared, 256); + assert!(plan_s2_gemv_q8(96, 8, 64).is_err()); + let st = plan_s2_gemv_fast(2560, 640, 64, true).unwrap().unwrap(); + assert_eq!(st.kernel, Kernel::S2GemvFastStaged); + assert_eq!(st.launch.dynamic_shared, 8192 + 256); + let un = plan_s2_gemv_fast(2560, 640, 64, false).unwrap().unwrap(); + assert_eq!((un.kernel, un.launch.dynamic_shared), (Kernel::S2GemvFastUnstaged, 256)); + assert_eq!(plan_s2_gemv_fast(8192, 8, 64, true), Err(Error::StagingLimit { n_in: 8192 })); + assert!(plan_s2_gemv_fast(8192, 8, 64, false).is_ok()); + } + + #[test] + fn grid_overflow_is_refused_not_truncated() { + let f = SForm { code_bits: 2, ..SForm::default() }; + let big = (u32::MAX as i64) + 1; + assert!(matches!(plan_s_gemv_split(64, big, &f, 32, false), Err(Error::GridTooLarge { .. }))); + } +}