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: read Q8_0 and Q5_1 n-gram tables (#651)
Anon74945954fe79
diff --git a/CMakeLists.txt b/CMakeLists.txtindex 4b34aebaf..ae36e40e9 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -1030,6 +1030,12 @@ if(STRATA_NATIVE_EXPERTS)endif()target_link_libraries(strata_kernels_cpu PUBLIC ggml-cpu ggml-base)target_compile_definitions(strata_kernels_cpu PUBLIC STRATA_NATIVE_EXPERTS=1)+ if(STRATA_BUILD_TESTS)+ # the Q5_1 block dequantizer (a Q5_1 PLE table) against ggml's, synthetic blocks: no model, no GPU+ add_executable(dequant_q5_1_test tests/core/dequant_q5_1_test.cpp)+ target_link_libraries(dequant_q5_1_test PRIVATE strata_artifact ggml-base)+ add_test(NAME dequant_q5_1_test COMMAND dequant_q5_1_test)+ endif()# the router lookahead's dot on each rung this CPU has (AVX2, the older CPUs' AVX1) against a double referenceadd_executable(router_dot_parity src/kernels/cpu/router_dot_parity.cpp)target_link_libraries(router_dot_parity PRIVATE strata_kernels_cpu)diff --git a/include/strata/artifact/dequant.hpp b/include/strata/artifact/dequant.hppindex d5a314ce2..b177c8ec0 100644--- a/include/strata/artifact/dequant.hpp+++ b/include/strata/artifact/dequant.hpp@@ -109,6 +109,22 @@ inline void dequantize_q5_0(const uint8_t* block, float* out) {out[j + 16] = (float)(x1 - 16) * d;}}+// ---- Q5_1: 32 elements from a 24-byte block: fp16 d, fp16 m, then Q5_0's qh[4] and qs[16]. The 5-bit codes are+// unsigned and the block's minimum m is added instead of Q5_0's fixed -16 offset (dequantize_row_q5_1).+inline void dequantize_q5_1(const uint8_t* block, float* out) {+ const float d = fp16_to_fp32(read_u16(block));+ const float m = fp16_to_fp32(read_u16(block + 2));+ const uint8_t* qh = block + 4;+ const uint8_t* qs = block + 8;+ const uint32_t h =+ (uint32_t)qh[0] | ((uint32_t)qh[1] << 8) | ((uint32_t)qh[2] << 16) | ((uint32_t)qh[3] << 24);+ for (int j = 0; j < 16; ++j) {+ const int x0 = (int)(qs[j] & 0x0F) | (int)(((h >> j) & 1u) << 4);+ const int x1 = (int)(qs[j] >> 4) | (int)(((h >> (j + 16)) & 1u) << 4);+ out[j] = (float)x0 * d + m;+ out[j + 16] = (float)x1 * d + m;+ }+}// ---- IQ4_NL: 32 elements from an 18-byte block. Non-linear 4-bit codebook, NOT a linear grid, so// the table must be exact. Copied verbatim from ggml-common.h `kvalues_iq4nl` at 3cf03257:// -127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113diff --git a/include/strata/kernels/ngram.hpp b/include/strata/kernels/ngram.hppindex 6cd1d8551..c0c858584 100644--- a/include/strata/kernels/ngram.hpp+++ b/include/strata/kernels/ngram.hpp@@ -46,13 +46,15 @@ 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; // the widest row any format in `ple_formats()` has+inline constexpr int PLE_ROW_BYTES_Q5_1 = (PLE_HEAD_DIM / 32) * 24; // 120: a Q5_1 row (a Q5_K_M finetune)+inline constexpr int PLE_ROW_BYTES_Q8_0 = (PLE_HEAD_DIM / 32) * 34; // 170: a Q8_0 row (UD-Q6_K_XL, Swift-1.5 Q4_K_L)+inline constexpr int PLE_ROW_BYTES_MAX = PLE_ROW_BYTES_Q8_0; // 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 };+enum class PleFormat : uint8_t { IQ4_NL, Q5_0, F8_E4M3, Q5_1, Q8_0 };struct PleFormatInfo {PleFormat id;diff --git a/src/kernels/dequant_bf16_test.cpp b/src/kernels/dequant_bf16_test.cppindex 80d3d53e6..28a75ba1b 100644--- a/src/kernels/dequant_bf16_test.cpp+++ b/src/kernels/dequant_bf16_test.cpp@@ -25,6 +25,7 @@ bool cpu_block(int type, const uint8_t* b, float* out) {switch (type) {case 2: dequantize_q4_0(b, out); return true;case 6: dequantize_q5_0(b, out); return true;+ case 7: dequantize_q5_1(b, out); return true;case 8: dequantize_q8_0(b, out); return true;case 11: dequantize_q3_K(b, out); return true;case 12: dequantize_q4_K(b, out); return true;diff --git a/src/kernels/ngram.cpp b/src/kernels/ngram.cppindex 3728207b2..8a705639f 100644--- a/src/kernels/ngram.cpp+++ b/src/kernels/ngram.cpp@@ -148,6 +148,9 @@ 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},+ // Ordinary GGUFs ship the table in other types: Unsloth's UD-Q6_K_XL and Swift-1.5 Q4_K_L as Q8_0, a Q5_K_M finetune as Q5_1+ {PleFormat::Q5_1, "Q5_1", "Q5_1", PLE_ROW_BYTES_Q5_1, false, dequant_blocks<strata::dequantize_q5_1, 24>},+ {PleFormat::Q8_0, "Q8_0", "Q8_0", PLE_ROW_BYTES_Q8_0, false, dequant_blocks<strata::dequantize_q8_0, 34>},};constexpr int kPleFormatCount = (int) (sizeof kPleFormats / sizeof kPleFormats[0]);} // namespacediff --git a/src/kernels/ple_q5_parity.cpp b/src/kernels/ple_q5_parity.cppindex cd387a8e4..29dc47e9c 100644--- a/src/kernels/ple_q5_parity.cpp+++ b/src/kernels/ple_q5_parity.cpp@@ -1,4 +1,5 @@-// Real-artifact Q5_0 PLE rows against ggml's reference dequantizer.+// Real-artifact Q5_0 / Q5_1 / Q8_0 PLE rows against ggml's reference dequantizer, through both readers (the mapped+// one and the direct SSD reader).#define NOMINMAX#include "strata/artifact/gguf_reader.hpp"#include "strata/kernels/ngram.hpp"@@ -10,33 +11,28 @@#include <string>#include <vector>-int main(int argc, char** argv) {- if (argc != 2) {- std::fprintf(stderr, "usage: ple_q5_parity <gguf-containing-ple>\n");- return 2;- }- strata::GgufFile gguf(argv[1]);- const auto* tensor = gguf.find("per_layer_token_embd.weight");- if (!tensor || tensor->type != 6 || tensor->shape.size() != 2 || tensor->shape[0] != 160) {- std::fprintf(stderr, "expected Q5_0 PLE [160, N]\n");- return 2;- }+namespace {++// Largest absolute difference between the table's rows and ggml's dequantization of the same bytes, single rows+// and one 16-row batch; -1 when the table cannot be opened or read.+double check(const char* path, strata::kernels::PleIo mode, const uint8_t* bytes, size_t row_bytes,+ const ggml_type_traits* traits, std::string& format, size_t& n_rows) {strata::kernels::PleIoOptions options;- options.mode = strata::kernels::PleIo::Mmap;+ options.mode = mode;strata::kernels::PleTable table;std::string err;- if (!table.open(argv[1], err, options)) {- std::fprintf(stderr, "%s\n", err.c_str());- return 1;+ if (!table.open(path, err, options)) {+ std::fprintf(stderr, "open: %s\n", err.c_str());+ return -1.0;}- const auto* traits = ggml_get_type_traits(GGML_TYPE_Q5_0);- const uint8_t* bytes = gguf.tensor_data(*tensor);+ format = table.format();+ n_rows = (size_t) table.rows();const uint32_t probes[] = {0, 1, 12345, 20000003, (uint32_t) (table.rows() - 1)};double max_abs = 0.0;for (uint32_t row : probes) {float got[160], want[160];table.read_row(row, got);- traits->to_float(bytes + (size_t) row * 110, want, 160);+ traits->to_float(bytes + (size_t) row * row_bytes, want, 160);for (int i = 0; i < 160; ++i)max_abs = std::max(max_abs, (double) std::fabs(got[i] - want[i]));}@@ -45,15 +41,46 @@ int main(int argc, char** argv) {float batch[16 * 160];if (!table.issue(rows) || !table.collect(batch, err)) {std::fprintf(stderr, "collect: %s\n", err.c_str());- return 1;+ return -1.0;}for (int h = 0; h < 16; ++h) {float want[160];- traits->to_float(bytes + (size_t) rows[h] * 110, want, 160);+ traits->to_float(bytes + (size_t) rows[h] * row_bytes, want, 160);for (int i = 0; i < 160; ++i)max_abs = std::max(max_abs, (double) std::fabs(batch[h * 160 + i] - want[i]));}- std::printf("Q5_0 PLE: %zu rows, max_abs %.3e %s\n", (size_t) table.rows(), max_abs,- max_abs <= 1e-6 ? "PASS" : "FAIL");- return max_abs <= 1e-6 ? 0 : 1;+ return max_abs;+}++} // namespace++int main(int argc, char** argv) {+ if (argc != 2) {+ std::fprintf(stderr, "usage: ple_q5_parity <gguf-containing-ple>\n");+ return 2;+ }+ strata::GgufFile gguf(argv[1]);+ const auto* tensor = gguf.find("per_layer_token_embd.weight");+ // Q5_0 (#296: OrcaRouter's Q4_K_S), Q5_1 (a Q5_K_M GGUF), Q8_0 (UD-Q6_K_XL, Swift-1.5 Q4_K_L): 5 blocks of+ // 32 per row+ if (!tensor || (tensor->type != GGML_TYPE_Q5_0 && tensor->type != GGML_TYPE_Q5_1 && tensor->type != GGML_TYPE_Q8_0) ||+ tensor->shape.size() != 2 || tensor->shape[0] != 160) {+ std::fprintf(stderr, "expected a Q5_0, Q5_1 or Q8_0 PLE [160, N]\n");+ return 2;+ }+ const auto type = (ggml_type) tensor->type;+ const size_t row_bytes = type == GGML_TYPE_Q5_0 ? 110 : type == GGML_TYPE_Q5_1 ? 120 : 170;+ const auto* traits = ggml_get_type_traits(type);+ const uint8_t* bytes = gguf.tensor_data(*tensor);+ bool ok = true;+ for (auto mode : {strata::kernels::PleIo::Mmap, strata::kernels::PleIo::Direct}) {+ std::string format;+ size_t n_rows = 0;+ const double max_abs = check(argv[1], mode, bytes, row_bytes, traits, format, n_rows);+ const bool pass = max_abs >= 0.0 && max_abs <= 1e-6;+ std::printf("%s PLE, %s reader: %zu rows, max_abs %.3e %s\n", format.c_str(),+ mode == strata::kernels::PleIo::Mmap ? "mapped" : "direct", n_rows, max_abs, pass ? "PASS" : "FAIL");+ ok = ok && pass;+ }+ return ok ? 0 : 1;}diff --git a/src/ngram/ple_reader_test.cpp b/src/ngram/ple_reader_test.cppindex db9469194..6baaf1eeb 100644--- a/src/ngram/ple_reader_test.cpp+++ b/src/ngram/ple_reader_test.cpp@@ -177,6 +177,11 @@ void all_formats_round_trip(const std::string& dir) {format_round_trip(dir, f, 2000);}CHECK(k::ple_format_for_type("Q6_K") == nullptr, "Q6_K must not be a PLE format");+ if (g_fail == 0) {+ std::printf("ple formats, both readers vs their dequantizers:");+ for (int i = 0; i < k::ple_format_count(); ++i) std::printf(" %s", k::ple_formats()[i].name);+ std::printf(": OK\n");+ }}// row_bytes: ng::ROW_BYTES (90, IQ4_NL) is the production default; 110 (#296, OrcaRouter's Q5_0 PLE rows) isdiff --git a/tests/core/dequant_q5_1_test.cpp b/tests/core/dequant_q5_1_test.cppnew file mode 100644index 000000000..6cd17f766--- /dev/null+++ b/tests/core/dequant_q5_1_test.cpp@@ -0,0 +1,40 @@+// dequantize_q5_1 (strata/artifact/dequant.hpp: the PLE table of a Q5_1 GGUF) against ggml's dequantize_row_q5_1,+// on synthetic blocks: random codes, high bits and scales, plus the edge codes 0 and 31 and a negative minimum.+// No model, no GPU.+#include "strata/artifact/dequant.hpp"+#include "ggml.h"++#include <cmath>+#include <cstdint>+#include <cstdio>+#include <cstring>+#include <random>+#include <vector>++int main() {+ constexpr int kBlocks = 4096;+ constexpr int kBlockBytes = 24;+ std::mt19937 rng(20261002);+ std::uniform_int_distribution<int> byte(0, 255);+ std::uniform_real_distribution<float> scale(-0.05f, 0.05f);+ std::vector<uint8_t> blocks((size_t) kBlocks * kBlockBytes);+ for (int b = 0; b < kBlocks; ++b) {+ uint8_t* blk = blocks.data() + (size_t) b * kBlockBytes;+ const ggml_fp16_t d = ggml_fp32_to_fp16(scale(rng));+ const ggml_fp16_t m = ggml_fp32_to_fp16(b == 1 ? -1.5f : scale(rng) * 20.0f);+ std::memcpy(blk, &d, 2);+ std::memcpy(blk + 2, &m, 2);+ for (int i = 4; i < kBlockBytes; ++i) blk[i] = (uint8_t) byte(rng);+ if (b == 0) std::memset(blk + 4, 0x00, 20); // every code 0: the value is m+ if (b == 1) std::memset(blk + 4, 0xFF, 20); // every code 31 (all high bits set)+ }+ const auto* traits = ggml_get_type_traits(GGML_TYPE_Q5_1);+ std::vector<float> want((size_t) kBlocks * 32), got((size_t) kBlocks * 32);+ traits->to_float(blocks.data(), want.data(), (int64_t) want.size());+ for (int b = 0; b < kBlocks; ++b) strata::dequantize_q5_1(blocks.data() + (size_t) b * kBlockBytes, got.data() + b * 32);+ double max_abs = 0.0;+ for (size_t i = 0; i < want.size(); ++i) max_abs = std::fmax(max_abs, std::fabs((double) got[i] - want[i]));+ const bool ok = max_abs <= 1e-6;+ std::printf("Q5_1 dequant: %d blocks, max_abs %.3e %s\n", kBlocks, max_abs, ok ? "PASS" : "FAIL");+ return ok ? 0 : 1;+}