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++

native packs: a BF16 (--compat-bf16) PLE key stays in the arena and is not replaced by the GGUF key (#326)

Niko1221abfb05cfbe27
diff --git a/CMakeLists.txt b/CMakeLists.txt
index a47c32e6d..b3aed4e17 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -343,6 +343,11 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP)
target_link_libraries(expert_layout_test PRIVATE strata_engine)
add_test(NAME expert_layout_test COMMAND expert_layout_test ${CMAKE_CURRENT_SOURCE_DIR}/tests/data/native_experts)
set_tests_properties(expert_layout_test PROPERTIES TIMEOUT 60)
+ # #326: a native pack's BF16 (--compat-bf16) PLE key stays in the arena; a quantized key row loads as before
+ add_executable(native_dense_ple_key_test tests/core/native_dense_ple_key_test.cpp)
+ target_link_libraries(native_dense_ple_key_test PRIVATE strata_engine)
+ add_test(NAME native_dense_ple_key_test COMMAND native_dense_ple_key_test)
+ set_tests_properties(native_dense_ple_key_test PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60)
endif()
if(STRATA_BUILD_CONVERSATION_TESTS)
add_executable(conversation_snapshot_test src/core/conversation_snapshot_test.cpp)
diff --git a/include/strata/core/native_dense.hpp b/include/strata/core/native_dense.hpp
index 1d41216d2..8330fac55 100644
--- a/include/strata/core/native_dense.hpp
+++ b/include/strata/core/native_dense.hpp
@@ -25,6 +25,10 @@ public:
/// supported type, 2-D), read from the GGUF headers only - so the canonical arena can skip them.
static bool served_names(const std::vector<std::string>& shards, bool include_ple_key,
std::set<std::string>& out, std::string& err);
+ /// #326: a native pack whose `blk.1.ple_key.weight` row is unquantized (iq_pack --compat-bf16 of a GGUF key
+ /// the native kernel also reads, e.g. OrcaRouter's IQ3_XXS) serves the PLE from that row, so it is taken out
+ /// of `skip` and `load` does not upload the GGUF key over it. A quantized row leaves `skip` unchanged.
+ static bool keep_unquantized_ple_key(const std::string& pack_dir, std::set<std::string>& skip, std::string& err);
uint64_t weight_bytes() const { return bytes_; }
size_t tensor_count() const { return weights_.size(); }
diff --git a/include/strata/core/weights.hpp b/include/strata/core/weights.hpp
index 8d8e1ec59..5c6414f8c 100644
--- a/include/strata/core/weights.hpp
+++ b/include/strata/core/weights.hpp
@@ -113,6 +113,12 @@ public:
static bool pool_bytes(const std::string& pack_dir, uint64_t& out, std::string& err,
const std::set<std::string>* skip = nullptr);
+ /// The `code_bits` field of one row of `<pack_dir>/index.txt`, readable WITHOUT loading anything (0 = the
+ /// pack stores the tensor unquantized, e.g. a --compat-bf16 key; -1 = no such row). #326: the loader has
+ /// to know this before it builds the skip set.
+ static bool index_code_bits(const std::string& pack_dir, const std::string& name, int& code_bits,
+ std::string& err);
+
/// Load every tensor in `<pack_dir>/index.txt` into `arena_base`.
///
/// `arena_bytes` MUST be at least `pool_bytes`; the loader checks rather than trusting the caller,
diff --git a/src/core/native_dense.cpp b/src/core/native_dense.cpp
index 6fa6896f9..4a86d48c2 100644
--- a/src/core/native_dense.cpp
+++ b/src/core/native_dense.cpp
@@ -53,6 +53,16 @@ bool NativeDense::served_names(const std::vector<std::string>& shards, bool incl
}
}
+bool NativeDense::keep_unquantized_ple_key(const std::string& pack_dir, std::set<std::string>& skip,
+ std::string& err) {
+ const std::string key = "blk.1.ple_key.weight";
+ if (!skip.count(key)) return true;
+ int code_bits = -1;
+ if (!WeightTable::index_code_bits(pack_dir, key, code_bits, err)) return false;
+ if (code_bits == 0) skip.erase(key);
+ return true;
+}
+
NativeDense::~NativeDense() {
if (scratch_) cudaFree(scratch_);
for (void* p : weights_) cudaFree(p);
@@ -139,6 +149,8 @@ bool NativeDense::load(const std::vector<std::string>& shards, WeightTable& tabl
auto& ref = found->second;
if (ref.native_data) { err = "native dense: override already attached"; return false; }
if (!strata::kernels::native_mmvq_supported(tensor.type)) continue;
+ // #326: the pack keeps an unquantized (--compat-bf16) key, which the PLE reads from the arena
+ if (tensor.name == "blk.1.ple_key.weight" && !ref.quantized()) continue;
if (!ref.quantized() || tensor.shape.size() != 2 ||
ref.ne0 <= 0 || ref.ne0 > INT_MAX || ref.ne1 <= 0 || ref.ne1 > INT_MAX ||
tensor.shape[0] != (uint64_t) ref.ne0 || tensor.shape[1] != (uint64_t) ref.ne1) {
diff --git a/src/core/weights.cpp b/src/core/weights.cpp
index a77f3c648..8b42f84f3 100644
--- a/src/core/weights.cpp
+++ b/src/core/weights.cpp
@@ -118,6 +118,29 @@ bool WeightTable::pool_bytes(const std::string& pack_dir, uint64_t& out, std::st
return true;
}
+bool WeightTable::index_code_bits(const std::string& pack_dir, const std::string& name, int& code_bits,
+ std::string& err) {
+ const std::string path = pack_dir + "/index.txt";
+ std::FILE* f = std::fopen(path.c_str(), "rb");
+ if (!f) { err = "cannot open " + path; return false; }
+ char line[1024];
+ code_bits = -1;
+ while (std::fgets(line, sizeof line, f)) {
+ if (line[0] == '#') continue;
+ char row[256] = {0};
+ unsigned long long dummy = 0;
+ long long ne = 0;
+ int i1 = 0, i2 = 0, bits = 0;
+ // the first ten fields of the row `load` parses: name file kind src_off src_bytes dst_off dst_bytes ne0 ne1 code_bits
+ if (std::sscanf(line, "%255s %d %d %llu %llu %llu %llu %lld %lld %d", row, &i1, &i2, &dummy, &dummy, &dummy,
+ &dummy, &ne, &ne, &bits) != 10)
+ continue;
+ if (name == row) { code_bits = bits; break; }
+ }
+ std::fclose(f);
+ return true;
+}
+
bool WeightTable::load(const std::string& pack_dir, void* arena_base, uint64_t arena_bytes, std::string& err,
const std::set<std::string>* skip) {
const std::string path = pack_dir + "/index.txt";
diff --git a/src/program/generate.cpp b/src/program/generate.cpp
index caf47d6bc..30c1a13e3 100644
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@ -1764,6 +1764,11 @@ int main(int argc, char** argv) {
if (!o.native_head_gguf.empty()) skip.insert("output.weight");
// the PLE module validates its canonical key at construction (8 MB); a native pack has none to load
if (!native_pack) skip.erase("blk.1.ple_key.weight");
+ // #326: a --compat-bf16 pack (OrcaRouter IQ3_XXS) keeps its BF16 key in the arena
+ if (native_pack && !strata::core::NativeDense::keep_unquantized_ple_key(o.pack, skip, err)) {
+ std::fprintf(stderr, "strata generate: %s\n", err.c_str());
+ return 1;
+ }
if (native_pack) skip.insert("token_embd.weight");
}
uint64_t pool_bytes = 0;
diff --git a/tests/core/native_dense_ple_key_test.cpp b/tests/core/native_dense_ple_key_test.cpp
new file mode 100644
index 000000000..e69d83851
--- /dev/null
+++ b/tests/core/native_dense_ple_key_test.cpp
@@ -0,0 +1,169 @@
+// tests/core/native_dense_ple_key_test.cpp - #326: a native pack whose PLE key row is BF16 (iq_pack --compat-bf16 of
+// OrcaRouter's IQ3_XXS key) loads with that row in the arena, and NativeDense does not upload the GGUF key over it.
+//
+// 1. BF16 key row + IQ3_XXS GGUF key: served_names lists the key, keep_unquantized_ple_key takes it out of the
+// skip set, the arena holds the BF16 bytes, and NativeDense::load attaches no native key (0.1.25-0.1.31 refused
+// the load with "incompatible matrix blk.1.ple_key.weight"). The other matrix still gets its native upload.
+// 2. A quantized key row (the Q2_0 / UD-Q4_K_XL packs: a shape-only row) + a Q8_0 GGUF key: unchanged - the key
+// stays skipped and is served natively.
+// 3. index_code_bits on a missing row and a missing index.
+// Synthetic files at small dimensions; needs a CUDA device for the arena (exits 77 without one).
+#include "gguf_fixture.hpp"
+
+#include "strata/core/native_dense.hpp"
+#include "strata/core/weights.hpp"
+
+#include <cuda_runtime.h>
+
+#include <chrono>
+#include <cstdio>
+#include <filesystem>
+#include <fstream>
+#include <set>
+#include <string>
+#include <vector>
+
+namespace fs = std::filesystem;
+using strata::core::NativeDense;
+using strata::core::WeightTable;
+
+namespace {
+int g_fail = 0;
+void check(bool ok, const std::string& what) {
+ std::printf(" %-84s %s\n", what.c_str(), ok ? "ok" : "FAIL");
+ if (!ok) ++g_fail;
+}
+
+struct TempDir {
+ fs::path path;
+ TempDir() {
+ path = fs::temp_directory_path() /
+ ("strata-ple-key-test-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
+ fs::create_directories(path);
+ }
+ ~TempDir() {
+ std::error_code ignored;
+ fs::remove_all(path, ignored);
+ }
+};
+
+const char* KEY = "blk.1.ple_key.weight";
+const char* QKV = "blk.0.attn_qkv.weight";
+constexpr uint64_t NE0 = 256, NE1 = 4;
+
+std::vector<fixture::Kv> arch_keys() {
+ return {fixture::str("general.architecture", "qwen4exp"), fixture::u32("qwen4exp.block_count", 48),
+ fixture::u32("qwen4exp.embedding_length", 2560), fixture::u32("qwen4exp.expert_count", 512),
+ fixture::u32("qwen4exp.expert_used_count", 10), fixture::u32("qwen4exp.attention.head_count", 24),
+ fixture::u32("qwen4exp.attention.head_count_kv", 2)};
+}
+
+// A shape-only quantized row (what iq_pack writes for a tensor the GGUF serves) and, with `bf16_key`, the key as a
+// raw BF16 row (index kind 4) in dense.bin.
+void write_pack(const fs::path& dir, bool bf16_key) {
+ fs::create_directories(dir);
+ const uint64_t key_bytes = NE0 * NE1 * 2;
+ std::string idx = "# align 256 pool " + std::to_string(bf16_key ? 2048 + 256 : 256) + " tensors 2\n";
+ idx += std::string(QKV) + " 0 0 0 0 0 0 256 4 8 0 32 0 0 0 0 0 0 0\n";
+ if (bf16_key)
+ idx += std::string(KEY) + " 0 4 0 " + std::to_string(key_bytes) + " 256 " + std::to_string(key_bytes) +
+ " 256 4 0 0 1 0 0 0 0 0 0 0\n";
+ else
+ idx += std::string(KEY) + " 0 0 0 0 256 0 256 4 8 0 32 0 0 0 0 0 0 0\n";
+ std::ofstream(dir / "index.txt", std::ios::binary) << idx;
+ std::vector<char> bytes(bf16_key ? key_bytes : 16);
+ for (size_t i = 0; i < bytes.size(); ++i) bytes[i] = (char) fixture::pattern(9, i);
+ std::ofstream(dir / "dense.bin", std::ios::binary).write(bytes.data(), (std::streamsize) bytes.size());
+}
+
+struct Loaded {
+ bool ok = false;
+ std::string err;
+ std::set<std::string> skip;
+ WeightTable wt;
+ NativeDense dense;
+ void* arena = nullptr;
+ ~Loaded() { if (arena) cudaFree(arena); }
+};
+
+// generate.cpp's order: served_names, keep_unquantized_ple_key (native packs), pool_bytes, load, NativeDense::load.
+void load(const fs::path& pack, const std::string& shard, Loaded& l) {
+ const std::vector<std::string> shards{shard};
+ if (!NativeDense::served_names(shards, true, l.skip, l.err)) return;
+ if (!NativeDense::keep_unquantized_ple_key(pack.string(), l.skip, l.err)) return;
+ uint64_t pool = 0;
+ if (!WeightTable::pool_bytes(pack.string(), pool, l.err, &l.skip)) return;
+ if (cudaMalloc(&l.arena, pool ? pool : 256) != cudaSuccess) { l.err = "cudaMalloc"; return; }
+ if (!l.wt.load(pack.string(), l.arena, pool ? pool : 256, l.err, &l.skip)) return;
+ l.ok = l.dense.load(shards, l.wt, l.err, true);
+}
+} // namespace
+
+int main() {
+ int devices = 0;
+ if (cudaGetDeviceCount(&devices) != cudaSuccess || devices == 0) {
+ std::printf("native_dense_ple_key_test: no CUDA device, skipped\n");
+ return 77;
+ }
+ TempDir tmp;
+
+ std::printf("1. BF16 key row (--compat-bf16), IQ3_XXS GGUF key\n");
+ {
+ const fs::path pack = tmp.path / "orca";
+ write_pack(pack, true);
+ const std::string shard = (tmp.path / "orca.gguf").string();
+ fixture::write(shard, arch_keys(), {{QKV, {NE0, NE1}, 8, 1}, {KEY, {NE0, NE1}, 18, 2}});
+ int bits = 99;
+ std::string err;
+ check(WeightTable::index_code_bits(pack.string(), KEY, bits, err) && bits == 0, "index_code_bits: key row is 0");
+ check(WeightTable::index_code_bits(pack.string(), QKV, bits, err) && bits == 8, "index_code_bits: qkv row is 8");
+ Loaded l;
+ load(pack, shard, l);
+ check(l.ok, "the pack loads (was: incompatible matrix) " + l.err);
+ check(!l.skip.count(KEY) && l.skip.count(QKV), "the key is not skipped, qkv is");
+ const auto* k = l.wt.find(KEY);
+ check(k && k->resident && k->data && !k->quantized(), "the key is resident and BF16");
+ check(k && !k->native_data, "no native key attached");
+ if (k && k->data) {
+ std::vector<unsigned char> back(NE0 * NE1 * 2);
+ cudaMemcpy(back.data(), k->data, back.size(), cudaMemcpyDeviceToHost);
+ bool same = true;
+ for (size_t i = 0; i < back.size(); ++i) same = same && back[i] == fixture::pattern(9, i);
+ check(same, "the arena holds the pack's BF16 bytes");
+ }
+ const auto* q = l.wt.find(QKV);
+ check(q && q->native_data && q->native_type == 8, "qkv still served natively (Q8_0)");
+ check(l.dense.tensor_count() == 1, "one native matrix");
+ }
+
+ std::printf("2. quantized key row (Q2_0 / UD-Q4_K_XL packs), Q8_0 GGUF key: unchanged\n");
+ {
+ const fs::path pack = tmp.path / "q8";
+ write_pack(pack, false);
+ const std::string shard = (tmp.path / "q8.gguf").string();
+ fixture::write(shard, arch_keys(), {{QKV, {NE0, NE1}, 8, 1}, {KEY, {NE0, NE1}, 8, 2}});
+ Loaded l;
+ load(pack, shard, l);
+ check(l.ok, "the pack loads " + l.err);
+ check(l.skip.count(KEY) == 1, "the key stays skipped");
+ const auto* k = l.wt.find(KEY);
+ check(k && !k->resident && k->native_data && k->native_type == 8, "the key is served natively");
+ check(l.dense.tensor_count() == 2, "two native matrices");
+ }
+
+ std::printf("3. index_code_bits edge cases\n");
+ {
+ int bits = 0;
+ std::string err;
+ check(WeightTable::index_code_bits((tmp.path / "orca").string(), "blk.9.nothing.weight", bits, err) &&
+ bits == -1, "a missing row is -1");
+ check(!WeightTable::index_code_bits((tmp.path / "none").string(), KEY, bits, err) && !err.empty(),
+ "a missing index is an error");
+ std::set<std::string> skip{QKV};
+ check(NativeDense::keep_unquantized_ple_key((tmp.path / "none").string(), skip, err) && skip.size() == 1,
+ "no key in the skip set: the index is not read");
+ }
+
+ std::printf(g_fail ? "native_dense_ple_key_test: %d FAILED\n" : "native_dense_ple_key_test: all passed\n", g_fail);
+ return g_fail ? 1 : 0;
+}