Skip to content

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 the n-gram table at full BF16 precision (#586)

Constantincb87a62e8290
diff --git a/include/strata/kernels/ngram.hpp b/include/strata/kernels/ngram.hpp
index da7373896..842914760 100644
--- a/include/strata/kernels/ngram.hpp
+++ b/include/strata/kernels/ngram.hpp
@@ -48,13 +48,14 @@ inline constexpr int PLE_ROW_BYTES = (PLE_HEAD_DIM / 32) * 18; // 90:
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_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
+inline constexpr int PLE_ROW_BYTES_BF16 = PLE_HEAD_DIM * 2; // 320: a BF16 row, two bytes a value
+inline constexpr int PLE_ROW_BYTES_MAX = PLE_ROW_BYTES_BF16; // 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, Q5_1, Q8_0, Q4_0 };
+enum class PleFormat : uint8_t { IQ4_NL, Q5_0, F8_E4M3, Q5_1, Q8_0, Q4_0, BF16 };
struct PleFormatInfo {
PleFormat id;
@@ -130,6 +131,12 @@ void iq4nl_dequant_row(const uint8_t* row, float* out160);
/// `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);
+/// One BF16 row -> 160 floats, EXACTLY: bfloat16 is the top half of a float32, so this is a shift and a copy with no
+/// rounding anywhere. It is the other published form of this same table - the full checkpoint stores
+/// `...ngram_embedding.shard_k` as BF16 (2 B a value, 102.4 GB), the source of record rather than a re-quantization.
+/// There is no scale, because BF16 values are the values.
+void bf16_dequant_row(const uint8_t* row, 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.
@@ -142,7 +149,8 @@ enum class PleIo { Direct, Mmap };
struct PleIoOptions {
PleIo mode = PleIo::Direct;
uint32_t max_inflight = 256; ///< outstanding SSD reads (decode needs 16; a prompt chunk saturates the SSD at 256)
- uint64_t cache_rows = 1u << 20; ///< bounded row cache: 1,048,576 rows x 90 B ~ 95 MB; 0 disables
+ uint64_t cache_rows = 1u << 20; ///< bounded row cache, in ROWS: 1,048,576 x 90 B ~ 95 MB (IQ4_NL), ~160 MB (FP8),
+ ///< ~335 MB (BF16); 0 disables
bool io_thread = true; ///< reads submitted by a worker thread, not the caller
/// Mmap mode only (`--ple-io ram`): lock the whole mapped table in RAM at open, so no SSD read ever sits on
/// the prompt or token path. Needs RAM for the full table. POSIX only (mlock); `locked()` reports the outcome.
diff --git a/src/kernels/ngram.cpp b/src/kernels/ngram.cpp
index 259bfe88d..406213c30 100644
--- a/src/kernels/ngram.cpp
+++ b/src/kernels/ngram.cpp
@@ -138,6 +138,19 @@ 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;
}
+void bf16_dequant_row(const uint8_t* row, float* out160) {
+ // bfloat16 IS the top 16 bits of a float32, so widening is exact for every value, normal or not - no rounding,
+ // no clamp, no scale. Bytes are assembled little-endian explicitly rather than reinterpreted, so this is the
+ // same on a big-endian host.
+ for (int j = 0; j < PLE_HEAD_DIM; ++j) {
+ const uint32_t bits = (uint32_t) row[2 * j] | ((uint32_t) row[2 * j + 1] << 8);
+ const uint32_t wide = bits << 16;
+ float v;
+ std::memcpy(&v, &wide, sizeof v);
+ out160[j] = v;
+ }
+}
+
// ---------------------------------------------------------------------------------------------------
// THE FORMAT TABLE (see PleFormatInfo). One entry per table type; the block formats decode 5 blocks of 32.
namespace {
@@ -147,6 +160,7 @@ void dequant_blocks(const uint8_t* row, float /*scale*/, float* out160) {
}
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); }
+void dequant_bf16(const uint8_t* row, float, float* out160) { bf16_dequant_row(row, out160); }
const PleFormatInfo kPleFormats[] = {
{PleFormat::IQ4_NL, "IQ4_NL", "IQ4_NL", PLE_ROW_BYTES, false, dequant_iq4_nl},
@@ -157,6 +171,9 @@ const PleFormatInfo kPleFormats[] = {
{PleFormat::Q8_0, "Q8_0", "Q8_0", PLE_ROW_BYTES_Q8_0, false, dequant_blocks<strata::dequantize_q8_0, 34>},
// Q4_0 (plain llama-quantize Q4_0 files, #599): 90-byte rows like IQ4_NL (18-byte blocks), a linear 4-bit grid
{PleFormat::Q4_0, "Q4_0", "Q4_0", PLE_ROW_BYTES, false, dequant_blocks<strata::dequantize_q4_0, 18>},
+ // BF16 (#586): the checkpoint's own table at full precision, 320-byte rows; self-describing, so no scale and no
+ // metadata to trust (tools/ple_fp8_pack.py writes the FP8 form of the same table)
+ {PleFormat::BF16, "BF16", "BF16", PLE_ROW_BYTES_BF16, false, dequant_bf16},
};
constexpr int kPleFormatCount = (int) (sizeof kPleFormats / sizeof kPleFormats[0]);
} // namespace
diff --git a/src/ngram/ple_reader_test.cpp b/src/ngram/ple_reader_test.cpp
index e9086d514..920847083 100644
--- a/src/ngram/ple_reader_test.cpp
+++ b/src/ngram/ple_reader_test.cpp
@@ -75,6 +75,30 @@ bool check_rows(ng::PleReader& rd, const std::vector<uint32_t>& rows, uint32_t n
return true;
}
+/// THE BF16 TABLE, and the claim it rests on. A bfloat16 is literally the top half of a float32, so widening
+/// one is a shift: exact for every value, normal or not, with no rounding to hide behind. These patterns are
+/// the ones a lossy path would break on - the subnormals and the NaN/inf encodings a saturating cast to FP8
+/// would collapse - so a table that came back through the FP8 route would not reproduce all of them.
+void bf16_widening_is_exact() {
+ const uint16_t bits[] = {0x0000, 0x8000, 0x3f80, 0xbf80, 0x7f7f, 0xff7f, // 0, -0, 1, -1, max, -max
+ 0x0001, 0x8001, 0x007f, 0x7f80, 0xff80, // smallest subnormals, +-inf
+ 0x7fc0, 0x7f81, 0x4049, 0xc249, 0x3c75}; // NaN, signalling NaN, 3.14, -3.14
+ for (uint16_t b : bits) {
+ uint8_t row[2 * k::PLE_HEAD_DIM];
+ std::memset(row, 0, sizeof row);
+ row[0] = (uint8_t) b; // little-endian: the low byte first
+ row[1] = (uint8_t) (b >> 8);
+ float out[k::PLE_HEAD_DIM];
+ k::bf16_dequant_row(row, out);
+ uint32_t want = (uint32_t) b << 16, got;
+ std::memcpy(&got, &out[0], sizeof got);
+ CHECK(got == want, "bf16 0x%04x widened to 0x%08x, not 0x%08x", b, got, want);
+ bool tail_zero = true; // one element under test must not have shifted the rest
+ for (int j = 1; j < k::PLE_HEAD_DIM; ++j) tail_zero = tail_zero && out[j] == 0.0f;
+ CHECK(tail_zero, "bf16 0x%04x: the rest of the row is not zero", b);
+ }
+}
+
/// 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
@@ -198,6 +222,7 @@ void bad_sizes_are_refused(const std::string& dir) {
}
void all_formats_round_trip(const std::string& dir) {
+ bf16_widening_is_exact(); // BF16's own claim: a shift, exact for every pattern
bad_sizes_are_refused(dir);
CHECK(k::ple_format_count() > 0, "no formats");
for (int i = 0; i < k::ple_format_count(); ++i) {