diff --git a/CMakeLists.txt b/CMakeLists.txt index c3bd402f..07c2f0f3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -912,6 +912,9 @@ if(STRATA_NATIVE_EXPERTS) add_executable(q5_projection_parity src/kernels/q5_projection_parity.cpp) target_include_directories(q5_projection_parity PRIVATE ${_strata_gpu_include_directories}) target_link_libraries(q5_projection_parity PRIVATE strata_kernels strata_artifact ggml-base ${_strata_gpu_runtime_target}) + # #291: the FP8 PLE table (tools/ple_fp8_pack.py) through PleTable vs the checkpoint's own values + add_executable(ple_fp8_parity src/kernels/ple_fp8_parity.cpp) + target_link_libraries(ple_fp8_parity PRIVATE strata_kernels ${_strata_gpu_runtime_target}) endif() endif() diff --git a/include/strata/artifact/gguf_reader.hpp b/include/strata/artifact/gguf_reader.hpp index 42e0665e..298dc3bc 100644 --- a/include/strata/artifact/gguf_reader.hpp +++ b/include/strata/artifact/gguf_reader.hpp @@ -96,6 +96,8 @@ inline const char* ggml_type_name(uint32_t t) { return "IQ2_S"; case 23: return "IQ4_XS"; + case 24: + return "I8"; case 30: return "BF16"; case 34: @@ -204,6 +206,10 @@ inline bool block_geometry(uint32_t t, int& elems, int& bytes) { elems = 256; bytes = 56; return true; + case 24: // I8: raw bytes (the FP8 PLE table of tools/ple_fp8_pack.py) + elems = 1; + bytes = 1; + return true; case 42: elems = 64; bytes = 18; diff --git a/include/strata/kernels/ngram.hpp b/include/strata/kernels/ngram.hpp index 33f3eb02..3c6f8fab 100644 --- a/include/strata/kernels/ngram.hpp +++ b/include/strata/kernels/ngram.hpp @@ -44,7 +44,9 @@ inline constexpr float NG_RMS_EPS = 1e-6f; // The table: [160, 320001536] IQ4_NL. ne0 = 160 is the FAST axis, so one row is 160 contiguous elements = // 5 blocks of 32 at 18 bytes = 90 bytes. The head-slowest flatten then makes 16 rows exactly n_embd = 2560. inline constexpr uint64_t PLE_TABLE_ROWS = 320001536ull; -inline constexpr int PLE_ROW_BYTES = (PLE_HEAD_DIM / 32) * 18; // 90 +inline constexpr int PLE_ROW_BYTES = (PLE_HEAD_DIM / 32) * 18; // 90: an IQ4_NL row +inline constexpr int PLE_ROW_BYTES_FP8 = PLE_HEAD_DIM; // 160: an F8_E4M3 row, one byte a value +inline constexpr int PLE_ROW_BYTES_MAX = PLE_ROW_BYTES_FP8; /// The artifact's own hash constants, transcribed from `docs/gguf-dump-shard1.txt`: /// @@ -96,6 +98,11 @@ int iq4nl_code(int code); /// a perfectly plausible embedding of the wrong 160 values. void iq4nl_dequant_row(const uint8_t* row, float* out160); +/// One FP8 row -> 160 floats: each byte an E4M3 value (the "fn" variant: no infinities, 0x7F/0xFF are NaN), times the +/// table's one scale. This is the table as Qwen3.8-Flash-Next ships it (`...ngram_embedding.shard_k`, F8_E4M3, and +/// `weight_scale`), kept byte for byte by tools/ple_fp8_pack.py; IQ4_NL is 8% off it per row. +void fp8_e4m3_dequant_row(const uint8_t* row, float scale, float* out160); + /// How the table's rows are read (plan v0.3 P2). `Direct` is the default: unbuffered 4 KiB reads from the SSD, /// so the table never occupies RAM or the OS file cache. `Mmap` is the earlier memory-mapped path, kept as the /// A/B arm; it returns the same bytes. @@ -155,6 +162,8 @@ public: /// True when `PleIoOptions::lock` was asked for and mlock succeeded (false: pages only pre-touched). bool locked() const; uint64_t rows() const; + /// "IQ4_NL" or "F8_E4M3" (a GGUF from tools/ple_fp8_pack.py: type I8, strata.ple.format = f8_e4m3). + const char* format() const; /// 16 row indices -> 2560 floats. The gathered rows are flattened HEAD-SLOWEST: row h's 160 values /// occupy `out[h*160, (h+1)*160)`, which is what `ggml_get_rows` does and what makes the result a plain diff --git a/include/strata/ngram/ple_reader.hpp b/include/strata/ngram/ple_reader.hpp index 721645c0..2884817e 100644 --- a/include/strata/ngram/ple_reader.hpp +++ b/include/strata/ngram/ple_reader.hpp @@ -1,6 +1,6 @@ // include/strata/ngram/ple_reader.hpp - plan v0.3 P2: the n-gram table read straight from the SSD. // -// The table is 320,001,536 rows of 90 bytes (26.8 GiB) and is never held in RAM: every row comes from an +// The table is 320,001,536 rows of 90 bytes (IQ4_NL, 26.8 GiB) or 160 (FP8 E4M3, 51.2 GB) and is never held in RAM: every row comes from an // unbuffered 4 KiB read (platform::DirectFile). A token needs 16 rows on 16 different pages, and all 16 depend // on the token itself, so the only time to hide them is the embedding plus layer 0. Hence the split API: // @@ -24,7 +24,7 @@ namespace strata::ngram { -inline constexpr uint32_t ROW_BYTES = 90; +inline constexpr uint32_t ROW_BYTES = 90; ///< an IQ4_NL row; `open` takes the table's own size inline constexpr uint32_t PAGE = 4096; struct ReaderStats { @@ -58,13 +58,15 @@ public: /// validated GGUF parse (PleTable::open checks the table exactly fills the file from there). /// `io_thread` (default): a worker thread submits and reaps reads, so `issue` costs the caller no ReadFile /// calls. false: the caller's thread does it (A/B arm). + /// `row_bytes`: one row's size in the file (90 IQ4_NL, 160 FP8), at most one page. bool open(const std::string& path, uint64_t table_offset, uint64_t n_rows, uint32_t max_inflight, - uint64_t cache_rows, std::string& err, bool io_thread = true); + uint64_t cache_rows, std::string& err, bool io_thread = true, uint32_t row_bytes = ROW_BYTES); + uint32_t row_bytes() const; void close(); bool is_open() const; - /// Start fetching `n` rows; row i's 90 raw bytes land at `out_raw + 90 * i`. `out_raw` must stay valid - /// until `collect` returns. Out-of-range rows produce 90 zero bytes (the mmap path's behaviour). + /// Start fetching `n` rows; row i's raw bytes land at `out_raw + row_bytes * i`. `out_raw` must stay valid + /// until `collect` returns. Out-of-range rows produce zero bytes (the mmap path's behaviour). Ticket issue(const uint32_t* rows, size_t n, uint8_t* out_raw); /// Block until every row of the ticket is in `out_raw`. Returns false on an I/O error (message in `err`). diff --git a/src/kernels/ngram.cpp b/src/kernels/ngram.cpp index bd366c50..1a1552bf 100644 --- a/src/kernels/ngram.cpp +++ b/src/kernels/ngram.cpp @@ -8,6 +8,7 @@ #include "strata/ngram/ple_reader.hpp" #include +#include #include #include #if !defined(_WIN32) @@ -112,13 +113,33 @@ void iq4nl_dequant_row(const uint8_t* row, float* out160) { } } +namespace { +struct Fp8Table { + float v[256]; + Fp8Table() { + for (int x = 0; x < 256; ++x) { + const int s = x >> 7, e = (x >> 3) & 15, m = x & 7; + float f; + if (e == 15 && m == 7) f = 0.0f; // NaN in e4m3fn; never in a real table, read as 0 + else if (e == 0) f = std::ldexp((float) m / 8.0f, -6); + else f = std::ldexp(1.0f + (float) m / 8.0f, e - 7); + v[x] = s ? -f : f; + } + } +}; +const Fp8Table kFp8; +} // namespace + +void fp8_e4m3_dequant_row(const uint8_t* row, float scale, float* out160) { + for (int j = 0; j < PLE_HEAD_DIM; ++j) out160[j] = kFp8.v[row[j]] * scale; +} + // --------------------------------------------------------------------------------------------------- struct PleTable::Impl { GgufFile* file = nullptr; const uint8_t* data = nullptr; uint64_t n_rows = 0; - uint32_t row_bytes = PLE_ROW_BYTES; - bool q5_0 = false; + bool q5_0 = false; // #296: Q5_0 rows (110 B), the mapped reader only mutable uint64_t bytes_read = 0; // Direct mode (plan v0.3 P2): the mapping above is released after the header parse and every row comes // from an unbuffered SSD read into `raw`. @@ -128,7 +149,16 @@ struct PleTable::Impl { bool pending = false; bool locked = false; uint32_t rows[PLE_N_HEADS] = {}; - uint8_t raw[PLE_N_HEADS * PLE_ROW_BYTES] = {}; + uint8_t raw[PLE_N_HEADS * PLE_ROW_BYTES_MAX] = {}; + bool fp8 = false; // F8_E4M3 rows (tools/ple_fp8_pack.py); else IQ4_NL + float scale = 1.0f; // the FP8 table's one scale + uint32_t rb = PLE_ROW_BYTES; // bytes per row + void decode(const uint8_t* row, float* out160) const { + if (fp8) fp8_e4m3_dequant_row(row, scale, out160); + else if (q5_0) + for (int b = 0; b < PLE_HEAD_DIM / 32; ++b) strata::dequantize_q5_0(row + (size_t) b * 22, out160 + b * 32); + else iq4nl_dequant_row(row, out160); + } }; PleTable::PleTable() : impl_(new Impl) {} @@ -158,9 +188,24 @@ bool PleTable::open(const std::string& gguf_path, std::string& err, const PleIoO close(); return false; } + // IQ4_NL (ISTA-DASLab's shard 2, the original's own GGUF), Q5_0 (#296: OrcaRouter's GGUF), or the FP8 table as + // shipped: I8 bytes marked strata.ple.format = f8_e4m3 with strata.ple.scale (tools/ple_fp8_pack.py) + impl_->fp8 = false; impl_->q5_0 = std::strcmp(t->type_name(), "Q5_0") == 0; - if (!impl_->q5_0 && std::strcmp(t->type_name(), "IQ4_NL") != 0) { - err = std::string("per_layer_token_embd.weight is ") + t->type_name() + ", expected IQ4_NL or Q5_0"; + impl_->rb = impl_->q5_0 ? (PLE_HEAD_DIM / 32) * 22 : PLE_ROW_BYTES; + if (std::strcmp(t->type_name(), "I8") == 0) { + const MetaValue* f = impl_->file->get("strata.ple.format"); + const MetaValue* s = impl_->file->get("strata.ple.scale"); + if (f == nullptr || f->s != "f8_e4m3" || s == nullptr || !(s->num() > 0.0)) { + err = "per_layer_token_embd.weight is I8 without strata.ple.format = f8_e4m3 and a positive strata.ple.scale"; + close(); + return false; + } + impl_->fp8 = true; + impl_->scale = (float) s->num(); + impl_->rb = PLE_ROW_BYTES_FP8; + } else if (!impl_->q5_0 && std::strcmp(t->type_name(), "IQ4_NL") != 0) { + err = std::string("per_layer_token_embd.weight is ") + t->type_name() + ", not IQ4_NL, Q5_0 or FP8 (I8)"; close(); return false; } @@ -171,7 +216,6 @@ bool PleTable::open(const std::string& gguf_path, std::string& err, const PleIoO close(); return false; } - impl_->row_bytes = impl_->q5_0 ? (PLE_HEAD_DIM / 32) * 22 : PLE_ROW_BYTES; impl_->n_rows = t->shape[1]; impl_->data = impl_->file->tensor_data(*t); @@ -183,7 +227,7 @@ bool PleTable::open(const std::string& gguf_path, std::string& err, const PleIoO // assumed. A wrong data offset would leave a different remainder. // A shard may hold other tensors too (Swift 1.5's shard 1 holds layers 0-12 and the table): the table must // then fit inside the file at its own offset; alone in its shard (the original's shard 2) it fills it exactly. - const uint64_t need = impl_->n_rows * (uint64_t) impl_->row_bytes; + const uint64_t need = impl_->n_rows * (uint64_t) impl_->rb; const uint64_t have = impl_->file->file_size() - impl_->file->data_start(); const bool alone = impl_->file->tensors().size() == 1; if (alone ? need != have : t->offset + need > have) { @@ -191,7 +235,7 @@ bool PleTable::open(const std::string& gguf_path, std::string& err, const PleIoO std::snprintf(buf, sizeof buf, "PLE table size mismatch: %llu rows x %d B = %llu at offset %llu, but the file holds %llu from " "data_start %llu", - (unsigned long long) impl_->n_rows, (int) impl_->row_bytes, (unsigned long long) need, + (unsigned long long) impl_->n_rows, (int) impl_->rb, (unsigned long long) need, (unsigned long long) t->offset, (unsigned long long) have, (unsigned long long) impl_->file->data_start()); err = buf; @@ -206,7 +250,8 @@ bool PleTable::open(const std::string& gguf_path, std::string& err, const PleIoO delete impl_->file; impl_->file = nullptr; impl_->data = nullptr; - if (!impl_->reader.open(gguf_path, table_offset, n_rows, io.max_inflight, io.cache_rows, err, io.io_thread)) { + if (!impl_->reader.open(gguf_path, table_offset, n_rows, io.max_inflight, io.cache_rows, err, io.io_thread, + impl_->rb)) { close(); return false; } @@ -243,41 +288,37 @@ void PleTable::close() { impl_->file = nullptr; impl_->data = nullptr; impl_->n_rows = 0; - impl_->row_bytes = PLE_ROW_BYTES; + impl_->rb = PLE_ROW_BYTES; impl_->q5_0 = false; + impl_->fp8 = false; } bool PleTable::is_open() const { return impl_->data != nullptr || impl_->reader.is_open(); } bool PleTable::locked() const { return impl_->locked; } +const char* PleTable::format() const { return impl_->fp8 ? "F8_E4M3" : impl_->q5_0 ? "Q5_0" : "IQ4_NL"; } PleIo PleTable::mode() const { return impl_->mode; } uint64_t PleTable::rows() const { return impl_->n_rows; } uint64_t PleTable::bytes_read() const { return impl_->bytes_read; } void PleTable::read_row(uint32_t row, float* out160) const { if (impl_->mode == PleIo::Direct && impl_->reader.is_open()) { - uint8_t raw[PLE_ROW_BYTES]; + uint8_t raw[PLE_ROW_BYTES_MAX]; std::string err; const auto t = impl_->reader.issue(&row, 1, raw); if (!impl_->reader.collect(t, err)) { std::memset(out160, 0, (size_t) PLE_HEAD_DIM * sizeof(float)); return; } - iq4nl_dequant_row(raw, out160); - impl_->bytes_read += PLE_ROW_BYTES; + impl_->decode(raw, out160); + impl_->bytes_read += impl_->rb; return; } if (impl_->data == nullptr || row >= impl_->n_rows) { std::memset(out160, 0, (size_t) PLE_HEAD_DIM * sizeof(float)); return; } - const uint8_t* raw = impl_->data + (size_t) row * impl_->row_bytes; - if (impl_->q5_0) { - for (int b = 0; b < PLE_HEAD_DIM / 32; ++b) - strata::dequantize_q5_0(raw + (size_t) b * 22, out160 + b * 32); - } else { - iq4nl_dequant_row(raw, out160); - } - impl_->bytes_read += impl_->row_bytes; + impl_->decode(impl_->data + (size_t) row * impl_->rb, out160); + impl_->bytes_read += impl_->rb; } bool PleTable::issue(const uint32_t* rows16) { @@ -294,8 +335,8 @@ bool PleTable::issue(const uint32_t* rows16) { ULONG_PTR n = 0; for (int h = 0; h < PLE_N_HEADS; ++h) { if (rows16[h] >= impl_->n_rows) continue; - ranges[n].VirtualAddress = (PVOID) (impl_->data + (size_t) rows16[h] * impl_->row_bytes); - ranges[n].NumberOfBytes = impl_->row_bytes; + ranges[n].VirtualAddress = (PVOID) (impl_->data + (size_t) rows16[h] * impl_->rb); + ranges[n].NumberOfBytes = impl_->rb; ++n; } if (n > 0) (void) PrefetchVirtualMemory(GetCurrentProcess(), n, ranges, 0); @@ -311,8 +352,8 @@ bool PleTable::collect(float* out2560, std::string& err) { if (impl_->mode == PleIo::Direct) { if (!impl_->reader.collect(impl_->ticket, err)) return false; for (int h = 0; h < PLE_N_HEADS; ++h) - iq4nl_dequant_row(impl_->raw + (size_t) h * PLE_ROW_BYTES, out2560 + (size_t) h * PLE_HEAD_DIM); - impl_->bytes_read += (uint64_t) PLE_N_HEADS * PLE_ROW_BYTES; + impl_->decode(impl_->raw + (size_t) h * impl_->rb, out2560 + (size_t) h * PLE_HEAD_DIM); + impl_->bytes_read += (uint64_t) PLE_N_HEADS * impl_->rb; return true; } for (int h = 0; h < PLE_N_HEADS; ++h) read_row(impl_->rows[h], out2560 + (size_t) h * PLE_HEAD_DIM); @@ -323,11 +364,11 @@ bool PleTable::gather_batch(const uint32_t* rows, size_t n_tokens, float* out, s if (impl_->pending) { err = "PleTable::gather_batch while a token is in flight"; return false; } const size_t n = n_tokens * (size_t) PLE_N_HEADS; if (impl_->mode == PleIo::Direct) { - std::vector raw(n * PLE_ROW_BYTES); + std::vector raw(n * impl_->rb); const auto ticket = impl_->reader.issue(rows, n, raw.data()); if (!impl_->reader.collect(ticket, err)) return false; - for (size_t i = 0; i < n; ++i) iq4nl_dequant_row(raw.data() + i * PLE_ROW_BYTES, out + i * PLE_HEAD_DIM); - impl_->bytes_read += (uint64_t) n * PLE_ROW_BYTES; + for (size_t i = 0; i < n; ++i) impl_->decode(raw.data() + i * impl_->rb, out + i * PLE_HEAD_DIM); + impl_->bytes_read += (uint64_t) n * impl_->rb; return true; } for (size_t i = 0; i < n; ++i) read_row(rows[i], out + i * PLE_HEAD_DIM); @@ -384,8 +425,8 @@ void PleTable::gather(const uint32_t* rows16, float* out2560) const { for (int h = 0; h < PLE_N_HEADS; ++h) { // Out-of-range rows are handled by `read_row` as zeros and have no address to prefetch. if (rows16[h] >= impl_->n_rows) continue; - ranges[n].VirtualAddress = (PVOID) (impl_->data + (size_t) rows16[h] * impl_->row_bytes); - ranges[n].NumberOfBytes = impl_->row_bytes; + ranges[n].VirtualAddress = (PVOID) (impl_->data + (size_t) rows16[h] * impl_->rb); + ranges[n].NumberOfBytes = impl_->rb; ++n; } if (n > 0) (void) PrefetchVirtualMemory(GetCurrentProcess(), n, ranges, 0); diff --git a/src/kernels/ple_fp8_parity.cpp b/src/kernels/ple_fp8_parity.cpp new file mode 100644 index 00000000..09dd199a --- /dev/null +++ b/src/kernels/ple_fp8_parity.cpp @@ -0,0 +1,84 @@ +// src/kernels/ple_fp8_parity.cpp - the FP8 PLE table (tools/ple_fp8_pack.py) read through PleTable, against values +// decoded from the checkpoint itself, and against the IQ4_NL table it replaces. +// +// ple_fp8_parity [] +// +// reference.bin: int32 n, uint32 rows[n], float32 values[n][160] - the rows as torch decodes the checkpoint's +// F8_E4M3 bytes, times its weight_scale (n a multiple of 16 for the gather_batch check). Every row must match BIT FOR BIT through +// both I/O modes (Direct and Mmap) and through the prompt path's gather_batch. With the IQ4_NL shard the same rows +// are compared too: same table, so a correlation near 0.997 - a shifted row order would be ~0. +#include "strata/kernels/ngram.hpp" + +#include +#include +#include +#include +#include +#include + +namespace k = strata::kernels; + +int main(int argc, char** argv) { + if (argc < 3) { + std::fprintf(stderr, "usage: ple_fp8_parity []\n"); + return 2; + } + std::ifstream f(argv[2], std::ios::binary); + int32_t n = 0; + f.read((char*) &n, 4); + std::vector rows((size_t) n); + std::vector ref((size_t) n * k::PLE_HEAD_DIM); + f.read((char*) rows.data(), (std::streamsize) rows.size() * 4); + f.read((char*) ref.data(), (std::streamsize) ref.size() * 4); + if (!f || n <= 0) { std::fprintf(stderr, "cannot read %s\n", argv[2]); return 2; } + + int bad = 0; + for (const k::PleIo mode : {k::PleIo::Direct, k::PleIo::Mmap}) { + k::PleTable t; + std::string err; + k::PleIoOptions io; + io.mode = mode; + if (!t.open(argv[1], err, io)) { std::fprintf(stderr, "open: %s\n", err.c_str()); return 1; } + std::vector got((size_t) n * k::PLE_HEAD_DIM); + for (int i = 0; i < n; ++i) t.read_row(rows[(size_t) i], got.data() + (size_t) i * k::PLE_HEAD_DIM); + int diff = 0; + for (size_t j = 0; j < got.size(); ++j) diff += std::memcmp(&got[j], &ref[j], 4) != 0; + std::vector batch((size_t) n * k::PLE_HEAD_DIM); // the prompt path: all rows as one request + const bool ok = t.gather_batch(rows.data(), (size_t) n / k::PLE_N_HEADS, batch.data(), err); + int bdiff = 0; + const size_t nb = (size_t) n / k::PLE_N_HEADS * k::PLE_N_HEADS * k::PLE_HEAD_DIM; + for (size_t j = 0; ok && j < nb; ++j) bdiff += std::memcmp(&batch[j], &ref[j], 4) != 0; + std::printf("%-6s %s, %llu rows: read_row %d/%zu values differ, gather_batch %s %d differ\n", + mode == k::PleIo::Direct ? "Direct" : "Mmap", t.format(), (unsigned long long) t.rows(), diff, + got.size(), ok ? "ok," : "FAILED,", bdiff); + bad += diff + bdiff + (ok ? 0 : 1) + (std::strcmp(t.format(), "F8_E4M3") != 0); + } + if (argc > 3) { + k::PleTable q; + std::string err; + if (!q.open(argv[3], err)) { std::fprintf(stderr, "open %s: %s\n", argv[3], err.c_str()); return 1; } + double cmin = 1, rsum = 0; + int counted = 0; + std::vector v(k::PLE_HEAD_DIM); + for (int i = 0; i < n; ++i) { + q.read_row(rows[(size_t) i], v.data()); + const float* r = ref.data() + (size_t) i * k::PLE_HEAD_DIM; + double sa = 0, sb = 0, sab = 0, sa2 = 0, sb2 = 0, e2 = 0, r2 = 0; + for (int j = 0; j < k::PLE_HEAD_DIM; ++j) { + sa += r[j]; sb += v[j]; sab += (double) r[j] * v[j]; sa2 += (double) r[j] * r[j]; sb2 += (double) v[j] * v[j]; + e2 += ((double) v[j] - r[j]) * ((double) v[j] - r[j]); r2 += (double) r[j] * r[j]; + } + if (r2 == 0) continue; // an all-zero row (the table has some) + const double m = k::PLE_HEAD_DIM; + const double c = (sab - sa * sb / m) / std::sqrt((sa2 - sa * sa / m) * (sb2 - sb * sb / m)); + cmin = std::fmin(cmin, c); + rsum += std::sqrt(e2 / r2); + ++counted; + } + std::printf("IQ4_NL table, same rows: correlation min %.4f, error vs FP8 %.1f%% mean (%d non-zero rows)\n", + cmin, 100 * rsum / (counted ? counted : 1), counted); + if (cmin < 0.98) ++bad; + } + std::printf("RESULT: %s\n", bad ? "MISMATCH" : "FP8 rows exact"); + return bad ? 1 : 0; +} diff --git a/src/ngram/ple_reader.cpp b/src/ngram/ple_reader.cpp index e7a3101f..a03e4c04 100644 --- a/src/ngram/ple_reader.cpp +++ b/src/ngram/ple_reader.cpp @@ -34,15 +34,17 @@ constexpr uint32_t EMPTY = 0xFFFFFFFFu; /// Set-associative row cache: 8 ways per set, round-robin replacement inside a set. Bounded by construction. struct RowCache { uint64_t sets = 0; + uint32_t rb = ROW_BYTES; // bytes per row std::vector keys; // sets * WAYS - std::vector data; // sets * WAYS * ROW_BYTES + std::vector data; // sets * WAYS * rb std::vector next; // per-set replacement pointer uint64_t used = 0; - void init(uint64_t rows) { + void init(uint64_t rows, uint32_t row_bytes = ROW_BYTES) { sets = rows / WAYS; + rb = row_bytes; keys.assign(sets * WAYS, EMPTY); - data.assign(sets * WAYS * ROW_BYTES, 0); + data.assign(sets * WAYS * rb, 0); next.assign(sets, 0); used = 0; } @@ -54,7 +56,7 @@ struct RowCache { if (sets == 0) return nullptr; const uint64_t s = mix(row) % sets; for (uint32_t w = 0; w < WAYS; ++w) - if (keys[s * WAYS + w] == row) return &data[(s * WAYS + w) * ROW_BYTES]; + if (keys[s * WAYS + w] == row) return &data[(s * WAYS + w) * rb]; return nullptr; } void insert(uint32_t row, const uint8_t* bytes) { @@ -64,7 +66,7 @@ struct RowCache { next[s] = (uint8_t) ((w + 1) % WAYS); if (keys[s * WAYS + w] == EMPTY) ++used; keys[s * WAYS + w] = row; - std::memcpy(&data[(s * WAYS + w) * ROW_BYTES], bytes, ROW_BYTES); + std::memcpy(&data[(s * WAYS + w) * rb], bytes, rb); } }; @@ -97,6 +99,7 @@ struct PleReader::Impl { DirectFile file; uint64_t table_offset = 0; uint64_t n_rows = 0; + uint32_t row_bytes = ROW_BYTES; uint32_t max_inflight = 0; uint8_t* slab = nullptr; // max_inflight slots of 2 pages std::vector free_slots; @@ -185,7 +188,7 @@ struct PleReader::Impl { rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17; - const uint64_t first = table_offset / PAGE, end = (table_offset + n_rows * (uint64_t) ROW_BYTES) / PAGE; + const uint64_t first = table_offset / PAGE, end = (table_offset + n_rows * (uint64_t) row_bytes) / PAGE; Job j; j.offset = (first + (end > first ? rng % (end - first) : 0)) * PAGE; j.length = PAGE; @@ -225,7 +228,7 @@ struct PleReader::Impl { stats.bytes += c.bytes; const uint8_t* buf = slot_buf(s); for (const Use& u : j.uses) { - if (u.in_page + ROW_BYTES > c.bytes) { + if (u.in_page + row_bytes > c.bytes) { error = "PleReader: short read inside the table"; cancel_queued(); j.uses.clear(); @@ -236,7 +239,7 @@ struct PleReader::Impl { } } for (const Use& u : j.uses) { - std::memcpy(u.dst, buf + u.in_page, ROW_BYTES); + std::memcpy(u.dst, buf + u.in_page, row_bytes); cache.insert(u.row, buf + u.in_page); } auto it = tickets.find(j.ticket); @@ -330,11 +333,13 @@ PleReader::~PleReader() { } bool PleReader::open(const std::string& path, uint64_t table_offset, uint64_t n_rows, uint32_t max_inflight, - uint64_t cache_rows, std::string& err, bool io_thread) { + uint64_t cache_rows, std::string& err, bool io_thread, uint32_t row_bytes) { close(); if (max_inflight == 0 || max_inflight > 1024) { err = "PleReader: max_inflight must be 1..1024"; return false; } + if (row_bytes == 0 || row_bytes > PAGE) { err = "PleReader: row_bytes must be 1..4096"; return false; } if (!impl_->file.open(path, err)) return false; - if (table_offset + n_rows * (uint64_t) ROW_BYTES > impl_->file.size()) { + impl_->row_bytes = row_bytes; + if (table_offset + n_rows * (uint64_t) row_bytes > impl_->file.size()) { err = "PleReader: the table extends past the end of " + path; close(); return false; @@ -347,7 +352,7 @@ bool PleReader::open(const std::string& path, uint64_t table_offset, uint64_t n_ impl_->inflight.assign(max_inflight, Job{}); impl_->free_slots.clear(); for (uint32_t s = max_inflight; s-- > 0;) impl_->free_slots.push_back(s); - impl_->cache.init(cache_rows); + impl_->cache.init(cache_rows, row_bytes); impl_->error.clear(); impl_->keep_us = 0; impl_->last_issue_us = impl_->last_read_us = 0; @@ -411,6 +416,7 @@ void PleReader::close() { } bool PleReader::is_open() const { return impl_->file.is_open(); } +uint32_t PleReader::row_bytes() const { return impl_->row_bytes; } PleReader::Ticket PleReader::issue(const uint32_t* rows, size_t n, uint8_t* out_raw) { Impl& m = *impl_; @@ -425,20 +431,21 @@ PleReader::Ticket PleReader::issue(const uint32_t* rows, size_t n, uint8_t* out_ std::unordered_map by_page; // aligned offset -> index in `jobs` std::vector jobs; for (size_t i = 0; i < n; ++i) { - uint8_t* dst = out_raw + i * ROW_BYTES; + const uint32_t rb = m.row_bytes; + uint8_t* dst = out_raw + i * rb; ++m.stats.requests; if (rows[i] >= m.n_rows) { - std::memset(dst, 0, ROW_BYTES); + std::memset(dst, 0, rb); continue; } if (const uint8_t* hit = m.cache.find(rows[i])) { - std::memcpy(dst, hit, ROW_BYTES); + std::memcpy(dst, hit, rb); ++m.stats.cache_hits; continue; } - const uint64_t at = m.table_offset + (uint64_t) rows[i] * ROW_BYTES; + const uint64_t at = m.table_offset + (uint64_t) rows[i] * rb; const uint64_t first = at / PAGE * PAGE; - const uint32_t length = (uint32_t) ((at + ROW_BYTES - 1) / PAGE * PAGE - first + PAGE); + const uint32_t length = (uint32_t) ((at + rb - 1) / PAGE * PAGE - first + PAGE); auto f = by_page.find(first); if (f != by_page.end()) { Job& j = jobs[f->second]; diff --git a/src/program/generate.cpp b/src/program/generate.cpp index cdf6e04d..d36942f5 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -1468,7 +1468,14 @@ int main(int argc, char** argv) { // every shard of the model (-0000N-of-0000M.gguf beside --native), then the PLE shard: a split // may put any layer in any shard (Swift's GGUFs: layers 13-47 in shard 2, the PLE table in shard 1) o.native_dense_gguf = o.native_shards; - if (std::find(o.native_dense_gguf.begin(), o.native_dense_gguf.end(), o.ple_gguf) == o.native_dense_gguf.end()) + // a PLE-only table (tools/ple_fp8_pack.py: architecture strata-ple) holds no projections + bool ple_only = false; + try { + strata::GgufFile pg(o.ple_gguf); + if (const strata::MetaValue* v = pg.get("general.architecture")) ple_only = v->s == "strata-ple"; + } catch (const std::exception&) {} + if (!ple_only && + std::find(o.native_dense_gguf.begin(), o.native_dense_gguf.end(), o.ple_gguf) == o.native_dense_gguf.end()) o.native_dense_gguf.push_back(o.ple_gguf); } // Plan v0.3 (24 Sep): the CPU experts stay on the VNNI kernel. The llama.cpp-CPU-exact q8_0 contract diff --git a/tools/ple_fp8_pack.py b/tools/ple_fp8_pack.py new file mode 100644 index 00000000..63132731 --- /dev/null +++ b/tools/ple_fp8_pack.py @@ -0,0 +1,123 @@ +"""tools/ple_fp8_pack.py - the PLE n-gram table exactly as Qwen shipped it (FP8 E4M3), as a GGUF the engine reads. + + python tools/ple_fp8_pack.py --model --out + +Qwen3.8-Flash-Next's checkpoint stores the table as 128 shards `...ngram_embedding.shard_{k}.weight` of +[2500012, 160] F8_E4M3 with one BF16 `weight_scale`: 320,001,536 rows of 160 values, 51.2 GB. The engine used to take +it from ISTA-DASLab's GGUF as IQ4_NL (90 B/row), which is 8% off these values per row; this keeps them bit for bit. + +The output is a one-tensor GGUF: `per_layer_token_embd.weight`, ne = [160, 320001536], type I8 holding the FP8 bytes +(GGUF has no FP8 type), with `strata.ple.format` = "f8_e4m3" and `strata.ple.scale` (F32). The bytes are copied +straight out of the safetensors files in shard order - nothing is decoded or rounded - so memory stays flat. +""" +import argparse +import json +import pathlib +import struct +import sys + +GGUF_TYPE_STRING, GGUF_TYPE_F32 = 8, 6 +GGML_TYPE_I8 = 24 +ALIGN = 32 +CHUNK = 64 << 20 + + +def safetensors_header(path): + with open(path, "rb") as f: + n = struct.unpack("