Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
ple: a table-driven PLE format (enum, row bytes and a dequant per type)
Niko1221ce62f5058fb6
diff --git a/include/strata/kernels/ngram.hpp b/include/strata/kernels/ngram.hppindex a321ea70f..6cd1d8551 100644--- a/include/strata/kernels/ngram.hpp+++ b/include/strata/kernels/ngram.hpp@@ -46,7 +46,32 @@ inline constexpr float NG_RMS_EPS = 1e-6f;inline constexpr uint64_t PLE_TABLE_ROWS = 320001536ull;inline constexpr int PLE_ROW_BYTES = (PLE_HEAD_DIM / 32) * 18; // 90: an IQ4_NL rowinline 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;+inline constexpr int PLE_ROW_BYTES_MAX = PLE_ROW_BYTES_FP8; // the widest row any format in `ple_formats()` has++/// THE FORMATS OF THE TABLE, ONE ROW EACH. A table type is one entry here (its GGUF type name, the bytes of one+/// 160-value row, how to turn a row into floats) and nothing else: `PleTable::open`, `format()`, the reader's row size,+/// the error string and the buffers' size all read this list, so a new format cannot be in one switch and missing from+/// another. A row is always `PLE_HEAD_DIM` = 160 values = 5 blocks of 32 for the block formats.+enum class PleFormat : uint8_t { IQ4_NL, Q5_0, F8_E4M3 };++struct PleFormatInfo {+ PleFormat id;+ const char* name; ///< what `PleTable::format()` returns+ const char* gguf_type; ///< `TensorInfo::type_name()` of the table tensor ("I8" for F8_E4M3: GGUF has no FP8 type)+ uint32_t row_bytes; ///< bytes of one 160-value row in the file+ bool needs_scale; ///< the table has one scale in the GGUF metadata (`strata.ple.scale`), F8_E4M3 only+ void (*dequant)(const uint8_t* row, float scale, float* out160);+};++/// Every format, in the order of `PleFormat`; `ple_format_count()` entries.+const PleFormatInfo* ple_formats();+int ple_format_count();+const PleFormatInfo& ple_format_info(PleFormat f);+/// The format whose table tensor has this GGUF type name, or nullptr. F8_E4M3's name is "I8"; the caller checks the+/// `strata.ple.format` marker and the scale.+const PleFormatInfo* ple_format_for_type(const char* gguf_type_name);+/// "IQ4_NL, Q5_0 or FP8 (I8)": the formats for an error message, built from the list.+std::string ple_format_list();/// The artifact's own hash constants, transcribed from `docs/gguf-dump-shard1.txt`:///diff --git a/src/kernels/ngram.cpp b/src/kernels/ngram.cppindex 7292dd963..3728207b2 100644--- a/src/kernels/ngram.cpp+++ b/src/kernels/ngram.cpp@@ -135,11 +135,48 @@ void fp8_e4m3_dequant_row(const uint8_t* row, float scale, float* out160) {}// ---------------------------------------------------------------------------------------------------+// THE FORMAT TABLE (see PleFormatInfo). One entry per table type; the block formats decode 5 blocks of 32.+namespace {+template <void (*Block)(const uint8_t*, float*), int BlockBytes>+void dequant_blocks(const uint8_t* row, float /*scale*/, float* out160) {+ for (int b = 0; b < PLE_HEAD_DIM / 32; ++b) Block(row + (size_t) b * BlockBytes, out160 + b * 32);+}+void dequant_iq4_nl(const uint8_t* row, float, float* out160) { iq4nl_dequant_row(row, out160); }+void dequant_fp8(const uint8_t* row, float scale, float* out160) { fp8_e4m3_dequant_row(row, scale, out160); }++const PleFormatInfo kPleFormats[] = {+ {PleFormat::IQ4_NL, "IQ4_NL", "IQ4_NL", PLE_ROW_BYTES, false, dequant_iq4_nl},+ {PleFormat::Q5_0, "Q5_0", "Q5_0", (PLE_HEAD_DIM / 32) * 22, false, dequant_blocks<strata::dequantize_q5_0, 22>},+ {PleFormat::F8_E4M3, "F8_E4M3", "I8", PLE_ROW_BYTES_FP8, true, dequant_fp8},+};+constexpr int kPleFormatCount = (int) (sizeof kPleFormats / sizeof kPleFormats[0]);+} // namespace++const PleFormatInfo* ple_formats() { return kPleFormats; }+int ple_format_count() { return kPleFormatCount; }+const PleFormatInfo& ple_format_info(PleFormat f) { return kPleFormats[(int) f]; }+const PleFormatInfo* ple_format_for_type(const char* gguf_type_name) {+ for (const PleFormatInfo& f : kPleFormats)+ if (std::strcmp(f.gguf_type, gguf_type_name) == 0) return &f;+ return nullptr;+}+std::string ple_format_list() {+ std::string out;+ for (int i = 0; i < kPleFormatCount; ++i) {+ const PleFormatInfo& f = kPleFormats[i];+ if (i > 0) out += i + 1 == kPleFormatCount ? " or " : ", ";+ out += std::strcmp(f.gguf_type, "I8") == 0 ? "FP8 (I8)" : f.name;+ }+ return out;+}++static_assert(PLE_ROW_BYTES_MAX >= PLE_ROW_BYTES && PLE_ROW_BYTES_MAX >= PLE_ROW_BYTES_FP8, "row buffers too small");+struct PleTable::Impl {GgufFile* file = nullptr;const uint8_t* data = nullptr;uint64_t n_rows = 0;- bool q5_0 = false; // #296: Q5_0 rows (110 B), the mapped reader only+ const PleFormatInfo* fmt = &ple_format_info(PleFormat::IQ4_NL); // the table's format (#296: Q5_0 is one of them)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`.@@ -155,15 +192,9 @@ struct PleTable::Impl {strata::ngram::PleReader::Ticket prefetch_tickets[kMaxPrefetch] = {};uint32_t prefetch_keys[kMaxPrefetch][PLE_N_HEADS] = {};uint8_t prefetch_raw[kMaxPrefetch][PLE_N_HEADS * PLE_ROW_BYTES_MAX] = {};- bool fp8 = false; // F8_E4M3 rows (tools/ple_fp8_pack.py); else IQ4_NLfloat scale = 1.0f; // the FP8 table's one scaleuint32_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);- }+ void decode(const uint8_t* row, float* out160) const { fmt->dequant(row, scale, out160); }};PleTable::PleTable() : impl_(new Impl) {}@@ -193,12 +224,16 @@ bool PleTable::open(const std::string& gguf_path, std::string& err, const PleIoOclose();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;- impl_->rb = impl_->q5_0 ? (PLE_HEAD_DIM / 32) * 22 : PLE_ROW_BYTES;- if (std::strcmp(t->type_name(), "I8") == 0) {+ // The table's type is looked up in the format list (ple_formats()): 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)+ const PleFormatInfo* fmt = ple_format_for_type(t->type_name());+ if (fmt == nullptr) {+ err = std::string("per_layer_token_embd.weight is ") + t->type_name() + ", not " + ple_format_list();+ close();+ return false;+ }+ if (fmt->needs_scale) {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)) {@@ -206,14 +241,10 @@ bool PleTable::open(const std::string& gguf_path, std::string& err, const PleIoOclose();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;}+ impl_->fmt = fmt;+ impl_->rb = fmt->row_bytes;// PleReader's row_bytes has been a runtime parameter since the FP8 table (160 B rows) needed it; Q5_0's// 110 B rows go through the exact same generic path (ple_reader_test --selftest covers both 90 and 110 B// rows: straddling, caching, in-flight tickets, keep-alive). This refusal was stale.@@ -323,13 +354,12 @@ void PleTable::close() {impl_->data = nullptr;impl_->n_rows = 0;impl_->rb = PLE_ROW_BYTES;- impl_->q5_0 = false;- impl_->fp8 = false;+ impl_->fmt = &ple_format_info(PleFormat::IQ4_NL);}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"; }+const char* PleTable::format() const { return impl_->fmt->name; }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; }diff --git a/src/ngram/ple_reader_test.cpp b/src/ngram/ple_reader_test.cppindex 87270fdc6..db9469194 100644--- a/src/ngram/ple_reader_test.cpp+++ b/src/ngram/ple_reader_test.cpp@@ -75,6 +75,110 @@ bool check_rows(ng::PleReader& rd, const std::vector<uint32_t>& rows, uint32_t nreturn true;}+/// EVERY FORMAT OF THE TABLE, END TO END (table-driven: one entry in `k::ple_formats()` is one run here). A minimal+/// PLE-only GGUF holds `rows` rows of that format's row size, each filled with bytes that encode the row, and both I/O+/// modes must return exactly what the format's own dequantizer makes of those bytes - so the row width, the offset+/// and the type lookup all have to be right for every format, not only for IQ4_NL.+uint32_t gguf_type_id(const char* name) {+ static const struct { const char* name; uint32_t id; } ids[] = {+ {"IQ4_NL", 20}, {"Q4_0", 2}, {"Q4_1", 3}, {"Q5_0", 6}, {"Q5_1", 7}, {"Q8_0", 8}, {"BF16", 30}, {"F8_E4M3", 24}};+ for (const auto& e : ids)+ if (std::strcmp(e.name, name) == 0) return e.id;+ return 0xFFFFFFFFu;+}++std::string write_format_table(const std::string& dir, const k::PleFormatInfo& f, uint32_t rows) {+ const std::string path = dir + "/ple_format_selftest.gguf";+ std::ofstream out(path, std::ios::binary);+ if (!out) { std::fprintf(stderr, "cannot write %s\n", path.c_str()); return {}; }+ const auto le = [](auto value, int bytes) {+ std::string s((size_t) bytes, '\0');+ for (int i = 0; i < bytes; ++i) s[(size_t) i] = (char) ((value >> (8 * i)) & 0xff);+ return s;+ };+ const auto str = [&](const char* s) { return le((uint64_t) std::strlen(s), 8) + std::string(s); };+ const uint64_t n_kv = f.needs_scale ? 3 : 1;+ std::string head = "GGUF" + le(3u, 4) + le(1ull, 8) + le(n_kv, 8);+ head += str("general.architecture") + le(8u, 4) + str("strata-ple");+ if (f.needs_scale) {+ const float scale = 0.75f;+ uint32_t sb;+ std::memcpy(&sb, &scale, 4);+ head += str("strata.ple.format") + le(8u, 4) + str("f8_e4m3");+ head += str("strata.ple.scale") + le(6u, 4) + le(sb, 4); // 6 = FLOAT32+ }+ head += str("per_layer_token_embd.weight") + le(2u, 4);+ head += le((uint64_t) k::PLE_HEAD_DIM, 8) + le((uint64_t) rows, 8);+ head += le(gguf_type_id(f.name), 4) + le(0ull, 8);+ while (head.size() % 32) head += '\0';+ out.write(head.data(), (std::streamsize) head.size());+ std::vector<uint8_t> r(f.row_bytes);+ for (uint32_t i = 0; i < rows; ++i) {+ expected_row(i, f.row_bytes, r.data());+ out.write((const char*) r.data(), (std::streamsize) f.row_bytes);+ }+ out.close();+ return out ? path : std::string();+}++void format_round_trip(const std::string& dir, const k::PleFormatInfo& f, uint32_t rows) {+ CHECK(gguf_type_id(f.name) != 0xFFFFFFFFu, "%s: no GGUF type id in this test", f.name);+ CHECK(f.row_bytes > 0 && f.row_bytes <= (uint32_t) k::PLE_ROW_BYTES_MAX, "%s: row_bytes %u out of range", f.name, f.row_bytes);+ const std::string path = write_format_table(dir, f, rows);+ if (path.empty()) { CHECK(false, "%s: no table was written", f.name); return; }+ std::mt19937 rng(13);+ std::vector<uint32_t> want(rows);+ for (uint32_t i = 0; i < rows; ++i) want[i] = rng() % rows;+ want[0] = 0;+ want[1] = rows - 1;+ for (k::PleIo mode : {k::PleIo::Direct, k::PleIo::Mmap}) {+ const char* name = mode == k::PleIo::Direct ? "direct" : "mmap";+ k::PleTable t;+ std::string err;+ k::PleIoOptions io;+ io.mode = mode;+ io.max_inflight = 8;+ io.cache_rows = 0;+ CHECK(t.open(path, err, io), "%s: open in %s mode: %s", f.name, name, err.c_str());+ if (g_fail) break;+ CHECK(std::strcmp(t.format(), f.name) == 0, "%s: format() says \"%s\"", f.name, t.format());+ CHECK(t.rows() == rows, "%s: rows %llu, not %u", f.name, (unsigned long long) t.rows(), rows);+ std::vector<uint8_t> raw(f.row_bytes);+ float got[k::PLE_HEAD_DIM], ref[k::PLE_HEAD_DIM];+ for (uint32_t r : want) {+ t.read_row(r, got);+ expected_row(r, f.row_bytes, raw.data());+ f.dequant(raw.data(), f.needs_scale ? 0.75f : 1.0f, ref);+ if (std::memcmp(got, ref, sizeof got) != 0) {+ CHECK(false, "%s: %s: row %u differs from the format's own dequantizer", f.name, name, r);+ break;+ }+ }+ // the 16-row path a token takes, one batch+ float tok[16 * k::PLE_HEAD_DIM];+ uint32_t rows16[16];+ for (int h = 0; h < 16; ++h) rows16[h] = want[(size_t) h + 2];+ t.gather(rows16, tok);+ for (int h = 0; h < 16; ++h) {+ expected_row(rows16[h], f.row_bytes, raw.data());+ f.dequant(raw.data(), f.needs_scale ? 0.75f : 1.0f, ref);+ CHECK(std::memcmp(tok + (size_t) h * k::PLE_HEAD_DIM, ref, sizeof ref) == 0, "%s: %s: gather head %d differs", f.name, name, h);+ }+ }+ std::filesystem::remove(path);+}++void all_formats_round_trip(const std::string& dir) {+ CHECK(k::ple_format_count() > 0, "no formats");+ for (int i = 0; i < k::ple_format_count(); ++i) {+ const k::PleFormatInfo& f = k::ple_formats()[i];+ CHECK((int) f.id == i, "%s: the table is not in PleFormat order", f.name);+ CHECK(k::ple_format_for_type(f.gguf_type) == &f, "%s: type lookup", f.name);+ format_round_trip(dir, f, 2000);+ }+ CHECK(k::ple_format_for_type("Q6_K") == nullptr, "Q6_K must not be a PLE format");+}+// row_bytes: ng::ROW_BYTES (90, IQ4_NL) is the production default; 110 (#296, OrcaRouter's Q5_0 PLE rows) is// run too, through the exact same generic row_bytes path -- nothing here is IQ4_NL-specific, so a second row// size run here is the correctness evidence for lifting ngram.cpp's "Q5_0 PLE requires --ple-io mmap" refusal.@@ -288,6 +392,8 @@ int main(int argc, char** argv) {if (self) {// ng::ROW_BYTES (90, IQ4_NL, production default) and 110 (#296, OrcaRouter's Q5_0 PLE rows) through the// same generic row_bytes path -- see the comment on selftest().+ all_formats_round_trip(dir); // every format of k::ple_formats(), both readers, vs its dequantizer+ if (g_fail != 0) return 1;const int r90 = selftest(dir, ng::ROW_BYTES);const int r110 = selftest(dir, 110);return r90 != 0 ? r90 : r110;