New pure crate with the arithmetic of f16_bits.hpp, kv_q4.cu and kv_q8.cu, the cell layouts and byte sizes of fp16/INT8/rotated INT8/Q4_0/K8V4/keyless pools (identical per-cell figures to the memory planner: 2048/1056/576/816/528), and the generate.cpp format-selection rules. Golden fixtures are produced by the Original's own host code extracted verbatim with awk (FWHT, Q4_0 quantizer under separate and fused multiply-add, INT8 K, kv_pool_bytes, kv_block_bytes). Experimental formats are explicit; nothing is enabled by default. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
167 lines
7.7 KiB
C++
167 lines
7.7 KiB
C++
// Golden fixtures for the Q4_0 / INT8 / FWHT arithmetic and the KV layout sizes, produced by the Original's own
|
|
// host code: the functions below are included VERBATIM (extracted by gen_kv_golden.sh with awk, never retyped)
|
|
// from engine/strata/src/kernels/kv_q4_parity.cpp (fwht256_host_reference, quantize_block_q4_0_host),
|
|
// kv_hybrid_parity.cpp (the INT8 K host reference) and the layer.cpp / kv_stream.cu size functions.
|
|
// usage: gen_kv_golden <separate|fused> (the script builds it twice: -ffp-contract=off and =fast)
|
|
#include "strata/kernels/f16_bits.hpp"
|
|
#include "strata/kernels/kv_q4.hpp"
|
|
#include "strata/kernels/kv_q8.hpp"
|
|
#include "strata/kernels/kv_stream.hpp"
|
|
#include "strata/kernels/qsa.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace k = strata::kernels;
|
|
using namespace strata::kernels;
|
|
|
|
namespace {
|
|
bool g_without_key = false, g_kv_q4 = false;
|
|
#include "fragment_fwht_q4.inc"
|
|
#include "fragment_pool_bytes.inc"
|
|
} // namespace
|
|
namespace strata::kernels {
|
|
#include "fragment_block_bytes.inc"
|
|
}
|
|
|
|
namespace {
|
|
uint64_t lcg_state = 0x9e3779b97f4a7c15ull;
|
|
uint32_t next32() {
|
|
lcg_state = lcg_state * 6364136223846793005ull + 1442695040888963407ull;
|
|
return (uint32_t) (lcg_state >> 32);
|
|
}
|
|
float uniform() { return (float) (next32() >> 8) / 16777216.0f; } // [0, 1)
|
|
float gauss() { // sum of 12 uniforms - 6
|
|
float s = 0.0f;
|
|
for (int i = 0; i < 12; ++i) s += uniform();
|
|
return s - 6.0f;
|
|
}
|
|
uint32_t bits(float f) { uint32_t u; std::memcpy(&u, &f, 4); return u; }
|
|
void print_u32(FILE* o, const float* v, int n) {
|
|
std::fprintf(o, "[");
|
|
for (int i = 0; i < n; ++i) std::fprintf(o, "%s%u", i ? "," : "", bits(v[i]));
|
|
std::fprintf(o, "]");
|
|
}
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv) {
|
|
const std::string mode = argc > 1 ? argv[1] : "separate";
|
|
// The fused build only needs the Q4_0 section (the one place a fused multiply-add changes a result); the
|
|
// other sections are generated identically in both builds so the LCG sequence stays aligned.
|
|
const bool full = mode == "separate";
|
|
FILE* null_out = std::fopen("/dev/null", "w");
|
|
FILE* o = full ? stdout : null_out;
|
|
std::fprintf(o, "{\"mode\":\"%s\",\n", mode.c_str());
|
|
|
|
// ---- FWHT rows (the Original's host reference, butterflies then 1/16)
|
|
std::fprintf(o, "\"fwht\":[");
|
|
for (int r = 0; r < 24; ++r) {
|
|
float in[256], out[256];
|
|
const float scale = r < 8 ? 1.0f : (r < 16 ? 37.5f : 0.003f);
|
|
for (int i = 0; i < 256; ++i) in[i] = (r == 23 ? (i == 5 ? 1.0f : 0.0f) : gauss() * scale);
|
|
std::memcpy(out, in, sizeof in);
|
|
fwht256_host_reference(out);
|
|
std::fprintf(o, "%s{\"in\":", r ? ",\n" : "\n");
|
|
print_u32(o, in, 256);
|
|
std::fprintf(o, ",\"out\":");
|
|
print_u32(o, out, 256);
|
|
std::fprintf(o, "}");
|
|
}
|
|
std::fprintf(o, "],\n");
|
|
|
|
// ---- Q4_0 groups (quantize_block_q4_0_host on separate or fused arithmetic, whatever this build does)
|
|
o = stdout;
|
|
if (!full) std::fprintf(o, "{\"mode\":\"%s\",\n", mode.c_str());
|
|
std::fprintf(o, "\"q4\":[");
|
|
const int groups = 480 + 96; // the last 96 sit on the code boundaries where fused and separate evaluation differ
|
|
for (int g = 0; g < groups; ++g) {
|
|
float x[32];
|
|
const int cls = g >= 480 ? 8 : g % 8;
|
|
const float M = 0.05f + uniform() * 9.0f;
|
|
for (int i = 0; i < 32; ++i) {
|
|
float v = gauss();
|
|
switch (cls) {
|
|
case 1: v *= 0.01f; break;
|
|
case 2: v *= 300.0f; break;
|
|
case 3: v = (i % 2 ? 1.0f : -1.0f) * (float) (i / 2) * 0.25f; break; // exact multiples
|
|
case 4: v = (float) ((int) (next32() % 17) - 8) * 0.5f; break; // ties and boundaries
|
|
case 5: v = i == 7 ? 1e4f : v * 1e-3f; break; // one outlier
|
|
case 6: v = 0.0f; break;
|
|
case 7: v = v * 1e-8f; break; // fp16 subnormal scales
|
|
case 8: { // x on the half-multiples of d, +-1 ulp
|
|
v = i == 0 ? M : (float) ((double) M * ((double) (i % 8) + 0.5) / 8.0);
|
|
if (i > 0 && (next32() & 1)) v = nextafterf(v, 0.0f);
|
|
if (i > 0 && (next32() & 2)) v = nextafterf(v, 100.0f);
|
|
break;
|
|
}
|
|
default: break;
|
|
}
|
|
x[i] = v;
|
|
}
|
|
if (cls == 0 && g % 16 == 0) { x[3] = 2.0f; x[20] = -2.0f; } // tied magnitudes, opposite sign
|
|
k::block_q4_0 blk;
|
|
quantize_block_q4_0_host(x, blk);
|
|
unsigned char raw[18];
|
|
std::memcpy(raw, &blk, 18);
|
|
std::fprintf(o, "%s{\"x\":", g ? ",\n" : "\n");
|
|
print_u32(o, x, 32);
|
|
std::fprintf(o, ",\"block\":\"");
|
|
for (int i = 0; i < 18; ++i) std::fprintf(o, "%02x", raw[i]);
|
|
std::fprintf(o, "\"}");
|
|
}
|
|
std::fprintf(o, "]");
|
|
|
|
// ---- INT8 K groups (kv_hybrid_parity.cpp's host reference for K)
|
|
if (!full) { std::fprintf(o, "}\n"); return 0; }
|
|
std::fprintf(o, ",\n");
|
|
std::fprintf(o, "\"q8\":[");
|
|
{
|
|
const int cells = 12, H = 2, D = 256, G = D / k::KV_Q8_GROUP;
|
|
std::vector<float> K((size_t) cells * H * D);
|
|
for (size_t i = 0; i < K.size(); ++i) K[i] = gauss() * ((i / 64) % 5 == 0 ? 0.0f : ((i / 64) % 5) * 0.4f);
|
|
std::vector<int8_t> rk((size_t) cells * H * D);
|
|
std::vector<uint16_t> rk_s((size_t) cells * H * G);
|
|
#include "fragment_q8_k.inc"
|
|
for (int c = 0; c < cells; ++c)
|
|
for (int h = 0; h < H; ++h)
|
|
for (int g = 0; g < G; ++g) {
|
|
const size_t base = ((size_t) c * H + h) * D + (size_t) g * k::KV_Q8_GROUP;
|
|
std::fprintf(o, "%s{\"x\":", (c || h || g) ? ",\n" : "\n");
|
|
print_u32(o, &K[base], 64);
|
|
std::fprintf(o, ",\"scale\":%u,\"codes\":[", (unsigned) rk_s[((size_t) c * H + h) * G + g]);
|
|
for (int t = 0; t < 64; ++t) std::fprintf(o, "%s%d", t ? "," : "", (int) rk[base + t]);
|
|
std::fprintf(o, "]}");
|
|
}
|
|
}
|
|
std::fprintf(o, "],\n");
|
|
|
|
// ---- layout sizes: the Original's own functions
|
|
const k::QsaShapes s = k::qsa_real_shapes();
|
|
std::fprintf(o, "\"per_cell\":{\"q8\":%llu,\"q4\":%llu},\n", (unsigned long long) k::kv_q8_bytes_per_cell(s),
|
|
(unsigned long long) k::kv_q4_bytes_per_cell(s));
|
|
std::fprintf(o, "\"block_bytes\":{\"f16\":%llu,\"int8\":%llu,\"q4\":%llu,\"value_int8\":%llu},\n",
|
|
(unsigned long long) k::kv_block_bytes(s, k::kKvF16), (unsigned long long) k::kv_block_bytes(s, k::kKvInt8),
|
|
(unsigned long long) k::kv_block_bytes(s, k::kKvQ4), (unsigned long long) k::kv_block_bytes(s, k::kKvValueInt8));
|
|
std::fprintf(o, "\"pool_bytes\":[");
|
|
bool first = true;
|
|
for (int64_t pages : {1, 5, 64, 5120, 131072}) {
|
|
struct Case { const char* name; bool without_key, q4, hybrid, int8; } cases[] = {
|
|
{"fp16", false, false, false, false}, {"int8", false, false, false, true}, {"q4_0", false, true, false, true},
|
|
{"k8v4", false, false, true, true}, {"int8-keyless", true, false, false, true}};
|
|
for (const Case& c : cases) {
|
|
g_without_key = c.without_key;
|
|
g_kv_q4 = c.q4;
|
|
std::fprintf(o, "%s{\"format\":\"%s\",\"pages\":%lld,\"bytes\":%llu}", first ? "\n" : ",\n", c.name, (long long) pages,
|
|
(unsigned long long) kv_pool_bytes(s, pages, c.hybrid, c.int8));
|
|
first = false;
|
|
}
|
|
}
|
|
std::fprintf(o, "]}\n");
|
|
return 0;
|
|
}
|