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

core: the model's geometry is read from its GGUF metadata and checked against the shape the kernels are compiled for

Shawn Jacksonf812e9a16249
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0f6426a45..7723a0d23 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -257,6 +257,10 @@ if(STRATA_BUILD_TESTS)
add_executable(batch_read_test tests/program/batch_read_test.cpp)
target_include_directories(batch_read_test PRIVATE include)
add_test(NAME batch_read_test COMMAND batch_read_test)
+ # the model's geometry from its metadata, and the check against the kernels' compiled shape (synthetic)
+ add_executable(gguf_geometry_test tests/core/gguf_geometry_test.cpp)
+ target_link_libraries(gguf_geometry_test PRIVATE strata_artifact)
+ add_test(NAME gguf_geometry_test COMMAND gguf_geometry_test)
# the hipBLASLt tuning header's architecture gate: RDNA4 siblings share solution IDs, RDNA3 siblings share
# theirs, cross-family pairs and version mismatches stay rejected (synthetic tables: no model, no GPU)
diff --git a/include/strata/artifact/gguf_reader.hpp b/include/strata/artifact/gguf_reader.hpp
index 1be014326..4b5baef0d 100644
--- a/include/strata/artifact/gguf_reader.hpp
+++ b/include/strata/artifact/gguf_reader.hpp
@@ -20,6 +20,7 @@
//
// Build: scripts/build_artifact.bat Run: gguf_reader.exe <file.gguf> [--check]
+#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
@@ -33,6 +34,7 @@
#include <algorithm>
#include "strata/artifact/gguf_split.hpp"
+#include "strata/core/geometry.hpp"
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
@@ -577,37 +579,175 @@ private:
std::map<std::string, std::pair<size_t, const TensorInfo*>> index_;
};
-// ---- architecture guard (P1.S2). The engine is specialised to ONE model; anything else must be
-// refused with a precise error rather than silently mis-run.
-struct Qwen4ExpGuard {
- uint32_t block_count = 48, hidden = 2560, experts = 0, experts_used = 0, head_count = 24,
- head_count_kv = 2; // 0 = presence-only: pruned variants (GSQ-RCO Coder) legitimately ship
- // fewer experts than the canonical 512; the graph reads the true value
-};
+// ---- the model's geometry from its metadata, and the architecture guard (P1.S2). The engine runs ONE
+// architecture, qwen4exp. Its dimensions are read from the file (llama.cpp's key names under the arch prefix)
+// and then checked against the shape the kernels are compiled for (core/geometry.hpp), so a file of another
+// shape is refused with the key that differs rather than silently mis-run.
-inline std::string check_architecture(const GgufFile& g, const Qwen4ExpGuard& want = {}) {
- const MetaValue* arch = g.get("general.architecture");
- if (!arch) return "missing general.architecture";
- if (arch->s != "qwen4exp") return "architecture is '" + arch->s + "', this engine requires 'qwen4exp'";
- struct Req {
+/// Fills `g` from the metadata shard `f`. Returns "" or the first problem. The six keys every qwen4exp file
+/// has carried since the first guard are required; any other key the file does not carry keeps the compiled
+/// value. The keys that describe one thing twice (the interval and `compress_ratios`, the key and value head
+/// widths, the routed and shared expert widths) must agree.
+inline std::string read_geometry(const GgufFile& f, core::ModelGeometry& g) {
+ g = core::kCompiledGeometry;
+ const std::string p = "qwen4exp.";
+ std::string err;
+ // an integer key into `out`: present and positive, or absent and not `required`
+ auto read_int = [&](const char* key, int64_t& out, bool required) {
+ const MetaValue* v = f.get(p + key);
+ if (!v) {
+ if (required) err = "missing " + p + key;
+ return !required;
+ }
+ if (!v->is_num() || v->type == MetaType::F32 || v->type == MetaType::F64 || v->type == MetaType::BOOL) {
+ err = p + key + " is not an integer";
+ return false;
+ }
+ if ((int64_t) v->u <= 0) {
+ err = p + key + " = " + std::to_string((int64_t) v->u) + ", it must be positive";
+ return false;
+ }
+ out = (int64_t) v->u;
+ return true;
+ };
+ struct Key {
+ const char* key;
+ int64_t core::ModelGeometry::*field;
+ bool required;
+ };
+ const Key keys[] = {
+ {"block_count", &core::ModelGeometry::n_layers, true},
+ {"embedding_length", &core::ModelGeometry::n_embd, true},
+ {"expert_count", &core::ModelGeometry::n_expert, true},
+ {"expert_used_count", &core::ModelGeometry::n_expert_used, true},
+ {"attention.head_count", &core::ModelGeometry::n_head, true},
+ {"attention.head_count_kv", &core::ModelGeometry::n_head_kv, true},
+ {"full_attention_interval", &core::ModelGeometry::qsa_interval, false},
+ {"ssm.state_size", &core::ModelGeometry::ssm_state_size, false},
+ {"ssm.group_count", &core::ModelGeometry::ssm_k_heads, false},
+ {"ssm.time_step_rank", &core::ModelGeometry::ssm_v_heads, false},
+ {"ssm.conv_kernel", &core::ModelGeometry::ssm_d_conv, false},
+ {"ssm.inner_size", &core::ModelGeometry::ssm_value_dim, false},
+ {"attention.key_length", &core::ModelGeometry::head_dim, false},
+ {"rope.dimension_count", &core::ModelGeometry::n_rot, false},
+ {"attention.indexer.head_count", &core::ModelGeometry::idx_q_heads, false},
+ {"attention.indexer.key_length", &core::ModelGeometry::idx_key_dim, false},
+ {"attention.indexer.top_k", &core::ModelGeometry::idx_top_k, false},
+ {"hyper_connection.count", &core::ModelGeometry::hc, false},
+ {"hyper_connection.low_rank", &core::ModelGeometry::hc_lr, false},
+ {"expert_feed_forward_length", &core::ModelGeometry::n_ff, false},
+ };
+ for (const Key& k : keys)
+ if (!read_int(k.key, g.*k.field, k.required)) return err;
+
+ int64_t value_length = g.head_dim, ff_shexp = g.n_ff;
+ if (!read_int("attention.value_length", value_length, false) ||
+ !read_int("expert_shared_feed_forward_length", ff_shexp, false))
+ return err;
+ if (value_length != g.head_dim)
+ return p + "attention.value_length = " + std::to_string(value_length) + " and attention.key_length = " +
+ std::to_string(g.head_dim) + ": the attention kernels use one head width for both";
+ if (ff_shexp != g.n_ff)
+ return p + "expert_shared_feed_forward_length = " + std::to_string(ff_shexp) +
+ " and expert_feed_forward_length = " + std::to_string(g.n_ff) +
+ ": the shared expert is read with the routed experts' width";
+ if (const MetaValue* v = f.get(p + "attention.layer_norm_rms_epsilon")) {
+ if (!v->is_num() || !(v->num() > 0)) return p + "attention.layer_norm_rms_epsilon must be a positive number";
+ g.rms_eps = v->num();
+ }
+ if (const MetaValue* v = f.get("tokenizer.ggml.tokens")) {
+ if (v->type != MetaType::ARRAY || v->count == 0) return "tokenizer.ggml.tokens is not a list of tokens";
+ g.n_vocab = (int64_t) v->count;
+ }
+
+ if (g.n_head % g.n_head_kv != 0)
+ return p + "attention.head_count = " + std::to_string(g.n_head) + " is not a multiple of head_count_kv = " +
+ std::to_string(g.n_head_kv);
+ // GDN's value heads are as wide as its key heads (the state is square)
+ if (g.ssm_value_dim != g.ssm_state_size * g.ssm_v_heads)
+ return p + "ssm.inner_size = " + std::to_string(g.ssm_value_dim) + ", but ssm.state_size x ssm.time_step_rank = " +
+ std::to_string(g.ssm_state_size * g.ssm_v_heads) + ": the GDN kernels take square heads";
+ g.ssm_conv_channels = 2 * g.ssm_state_size * g.ssm_k_heads + g.ssm_value_dim;
+ if (g.n_expert_used > g.n_expert || g.n_expert_used > 64)
+ return p + "expert_used_count = " + std::to_string(g.n_expert_used) + ", the router takes 1.." +
+ std::to_string(std::min<int64_t>(g.n_expert, 64)) + " of " + std::to_string(g.n_expert) + " experts";
+ if (g.n_rot > g.head_dim || g.n_rot > g.idx_key_dim || g.n_rot % 2)
+ return p + "rope.dimension_count = " + std::to_string(g.n_rot) + " is not an even slice of the head widths";
+
+ // The layer split, twice over: the interval, and a ratio per layer that is non-zero (the indexer's pooling
+ // block) exactly on the full-attention layers.
+ if (const MetaValue* v = f.get(p + "attention.compress_ratios")) {
+ if (v->type != MetaType::ARRAY || v->count != (uint64_t) g.n_layers || v->items.size() != v->count)
+ return p + "attention.compress_ratios must list one entry for each of the " + std::to_string(g.n_layers) +
+ " layers";
+ int64_t block = 0;
+ for (int64_t l = 0; l < g.n_layers; ++l) {
+ const int64_t r = (int64_t) v->items[(size_t) l].u;
+ if ((r != 0) != core::is_qsa_layer(g, l) || r < 0 || (block && r && r != block))
+ return p + "attention.compress_ratios[" + std::to_string(l) + "] = " + std::to_string(r) +
+ ", but full_attention_interval = " + std::to_string(g.qsa_interval) + " makes layer " +
+ std::to_string(l) + (core::is_qsa_layer(g, l) ? " full attention" : " linear attention") +
+ (block && r ? " (and the other full-attention layers pool " + std::to_string(block) + ")" : "");
+ if (r) block = r;
+ }
+ if (block) g.idx_block = block;
+ }
+ return {};
+}
+
+/// Returns "" when the compiled kernels can run `g`, else the first metadata key whose value they are not
+/// compiled for. Only the expert count and the experts per token are free (the router and the expert paths
+/// take them at run time); the vocabulary is free because the head and the embedding take their tensors' shapes.
+inline std::string geometry_unsupported(const core::ModelGeometry& g) {
+ const core::ModelGeometry& c = core::kCompiledGeometry;
+ struct Pin {
const char* key;
- uint64_t want;
+ int64_t core::ModelGeometry::*field;
};
- const Req reqs[] = {
- {"qwen4exp.block_count", want.block_count},
- {"qwen4exp.embedding_length", want.hidden},
- {"qwen4exp.expert_count", want.experts},
- {"qwen4exp.expert_used_count", want.experts_used},
- {"qwen4exp.attention.head_count", want.head_count},
- {"qwen4exp.attention.head_count_kv", want.head_count_kv},
+ const Pin pins[] = {
+ {"block_count", &core::ModelGeometry::n_layers},
+ {"embedding_length", &core::ModelGeometry::n_embd},
+ {"full_attention_interval", &core::ModelGeometry::qsa_interval},
+ {"ssm.state_size", &core::ModelGeometry::ssm_state_size},
+ {"ssm.group_count", &core::ModelGeometry::ssm_k_heads},
+ {"ssm.time_step_rank", &core::ModelGeometry::ssm_v_heads},
+ {"ssm.conv_kernel", &core::ModelGeometry::ssm_d_conv},
+ {"ssm.inner_size", &core::ModelGeometry::ssm_value_dim},
+ {"attention.head_count", &core::ModelGeometry::n_head},
+ {"attention.head_count_kv", &core::ModelGeometry::n_head_kv},
+ {"attention.key_length", &core::ModelGeometry::head_dim},
+ {"rope.dimension_count", &core::ModelGeometry::n_rot},
+ {"attention.indexer.head_count", &core::ModelGeometry::idx_q_heads},
+ {"attention.indexer.key_length", &core::ModelGeometry::idx_key_dim},
+ {"attention.compress_ratios", &core::ModelGeometry::idx_block},
+ {"attention.indexer.top_k", &core::ModelGeometry::idx_top_k},
+ {"hyper_connection.count", &core::ModelGeometry::hc},
+ {"hyper_connection.low_rank", &core::ModelGeometry::hc_lr},
+ {"expert_feed_forward_length", &core::ModelGeometry::n_ff},
};
- for (const auto& r : reqs) {
- const MetaValue* v = g.get(r.key);
- if (!v) return std::string("missing ") + r.key;
- if (r.want && v->u != r.want)
- return std::string(r.key) + " = " + std::to_string(v->u) + ", expected " + std::to_string(r.want);
+ for (const Pin& pin : pins)
+ if (g.*pin.field != c.*pin.field)
+ return std::string("qwen4exp.") + pin.key + " = " + std::to_string(g.*pin.field) +
+ ", the kernels are compiled for " + std::to_string(c.*pin.field);
+ // one constant in the norm kernels; the files carry it as an f32, so compare at f32 precision
+ if (std::abs(g.rms_eps - c.rms_eps) > 1e-6 * c.rms_eps) {
+ char buf[160];
+ std::snprintf(buf, sizeof buf, "qwen4exp.attention.layer_norm_rms_epsilon = %g, the kernels are compiled for %g",
+ g.rms_eps, c.rms_eps);
+ return buf;
}
- return {}; // empty == ok
+ return {};
+}
+
+/// The architecture guard: "" when the engine can run the model whose metadata shard is `f`.
+inline std::string check_architecture(const GgufFile& f) {
+ const MetaValue* arch = f.get("general.architecture");
+ if (!arch) return "missing general.architecture";
+ if (arch->s != "qwen4exp") return "architecture is '" + arch->s + "', this engine requires 'qwen4exp'";
+ core::ModelGeometry g;
+ std::string err = read_geometry(f, g);
+ if (err.empty()) err = geometry_unsupported(g);
+ return err;
}
} // namespace strata
diff --git a/include/strata/core/geometry.hpp b/include/strata/core/geometry.hpp
new file mode 100644
index 000000000..d3916d6ed
--- /dev/null
+++ b/include/strata/core/geometry.hpp
@@ -0,0 +1,70 @@
+// include/strata/core/geometry.hpp - the model's geometry: one struct for the shape a model file declares and
+// the shape the kernels are compiled for.
+//
+// A model file's geometry is read from its own metadata (`strata::read_geometry`, gguf_reader.hpp), and the
+// engine passes that struct everywhere it needs a dimension. The CUDA/HIP kernels still fix most of these
+// numbers at compile time (`constexpr int N = 2560` and the like); `kCompiledGeometry` is that shape, written
+// once, and each kernel that pins a number `static_assert`s it against this struct. So a kernel and this file
+// cannot drift apart without the build failing, and a model file of another shape is refused at load with the
+// metadata key that differs (`strata::geometry_unsupported`) instead of mis-running.
+//
+// Dependency-free on purpose: the kernels include it.
+#pragma once
+
+#include <cstdint>
+
+namespace strata::core {
+
+/// Every field is a number a kernel depends on, so a change to the compiled values is a change to a kernel
+/// contract and not a tuning knob. The defaults are Qwen3.8-Flash-Next's (qwen4exp), the shape the kernels
+/// are compiled for; the GGUF key each field is read from is named beside it.
+struct ModelGeometry {
+ int64_t n_embd = 2560; ///< embedding_length
+ int64_t n_layers = 48; ///< block_count
+ int64_t qsa_interval = 4; ///< full_attention_interval: every 4th layer is full attention, 3, 7, ... 47
+ int64_t n_vocab = 248320; ///< the length of tokenizer.ggml.tokens
+ double rms_eps = 1e-6; ///< attention.layer_norm_rms_epsilon
+
+ // GDN (36 layers)
+ int64_t ssm_state_size = 128; ///< ssm.state_size: the key head width, and the value head width
+ int64_t ssm_k_heads = 16; ///< ssm.group_count
+ int64_t ssm_v_heads = 48; ///< ssm.time_step_rank
+ int64_t ssm_d_conv = 4; ///< ssm.conv_kernel
+ int64_t ssm_conv_channels = 10240; ///< 2*128*16 + 128*48, derived
+ int64_t ssm_value_dim = 6144; ///< ssm.inner_size, 128 * 48
+
+ // QSA (12 layers)
+ int64_t n_head = 24; ///< attention.head_count
+ int64_t n_head_kv = 2; ///< attention.head_count_kv
+ int64_t head_dim = 256; ///< attention.key_length (= attention.value_length)
+ int64_t n_rot = 64; ///< rope.dimension_count: the rotated slice of head_dim and of idx_key_dim
+ int64_t idx_q_heads = 4; ///< attention.indexer.head_count
+ int64_t idx_key_dim = 128; ///< attention.indexer.key_length
+ int64_t idx_block = 4; ///< the non-zero attention.compress_ratios entry: one pooled key per 4 cells
+ int64_t idx_top_k = 2048; ///< attention.indexer.top_k: cells of selection budget
+
+ // gated residual, on every layer
+ int64_t hc = 4; ///< hyper_connection.count
+ int64_t hc_lr = 320; ///< hyper_connection.low_rank
+
+ // MoE, on every layer
+ int64_t n_expert = 512; ///< expert_count; pruned variants (GSQ-RCO Coder) ship fewer
+ int64_t n_expert_used = 10; ///< expert_used_count
+ int64_t n_ff = 640; ///< expert_feed_forward_length (= expert_shared_feed_forward_length)
+
+ constexpr int64_t hc_dim() const { return hc * n_embd; }
+ /// `layer % qsa_interval == qsa_interval - 1` is full attention. Derived, not a second list.
+ constexpr int64_t n_qsa_layers() const { return n_layers / qsa_interval; }
+ constexpr int64_t n_gdn_layers() const { return n_layers - n_qsa_layers(); }
+};
+
+/// The shape the kernels are compiled for. A kernel that fixes a dimension at compile time asserts it here.
+inline constexpr ModelGeometry kCompiledGeometry{};
+
+/// True for the full-attention layers. `docs/semantics.md` gives this twice over - `full_attention_interval
+/// = 4` and an explicit `attention.compress_ratios` array - and `read_geometry` checks that the two agree.
+inline bool is_qsa_layer(const ModelGeometry& g, int64_t layer) {
+ return layer % g.qsa_interval == g.qsa_interval - 1;
+}
+
+} // namespace strata::core
diff --git a/include/strata/core/layout.hpp b/include/strata/core/layout.hpp
index 7b27fec95..e9a91f9b5 100644
--- a/include/strata/core/layout.hpp
+++ b/include/strata/core/layout.hpp
@@ -15,53 +15,28 @@
// at LOAD time - not as a wrong number inside a GEMV at token 4000.
#pragma once
+#include "strata/core/geometry.hpp"
#include "strata/core/weights.hpp"
+#include "strata/kernels/qsa.hpp"
#include <cstdint>
#include <string>
namespace strata::core {
-/// The model's geometry, taken from `docs/semantics.md` and the artifact's own metadata. Every field here
-/// is a number a kernel depends on, so a change is a change to a kernel contract and not a tuning knob.
-struct ModelGeometry {
- int64_t n_embd = 2560;
- int64_t n_layers = 48;
- int64_t qsa_interval = 4; ///< every 4th layer is full attention: layers 3, 7, ... 47
-
- // GDN (36 layers)
- int64_t ssm_state_size = 128;
- int64_t ssm_k_heads = 16;
- int64_t ssm_v_heads = 48;
- int64_t ssm_d_conv = 4;
- int64_t ssm_conv_channels = 10240; ///< 2*128*16 + 128*48
- int64_t ssm_value_dim = 6144; ///< 128 * 48
-
- // QSA (12 layers)
- int64_t n_head = 24;
- int64_t n_head_kv = 2;
- int64_t head_dim = 256;
- int64_t idx_q_heads = 4;
- int64_t idx_key_dim = 128;
-
- // gated residual, on every layer
- int64_t hc = 4;
- int64_t hc_lr = 320;
-
- // MoE, on every layer
- int64_t n_expert = 512;
- int64_t n_ff = 640;
-
- int64_t hc_dim() const { return hc * n_embd; }
- /// `layer % qsa_interval == qsa_interval - 1` is full attention. Derived, not a second list.
- int64_t n_qsa_layers() const { return n_layers / qsa_interval; }
- int64_t n_gdn_layers() const { return n_layers - n_qsa_layers(); }
-};
-
-/// True for the full-attention layers. `docs/semantics.md` gives this twice over - `full_attention_interval
-/// = 4` and an explicit `attention.compress_ratios` array - and this is the first of the two.
-inline bool is_qsa_layer(const ModelGeometry& g, int64_t layer) {
- return layer % g.qsa_interval == g.qsa_interval - 1;
+/// The shapes the QSA kernels want, every one from the model's geometry; only the KV page size is the
+/// kernels' own choice (`qsa_real_shapes`).
+inline strata::kernels::QsaShapes qsa_shapes(const ModelGeometry& g) {
+ strata::kernels::QsaShapes s = strata::kernels::qsa_real_shapes();
+ s.n_head = g.n_head;
+ s.n_head_kv = g.n_head_kv;
+ s.head_dim = g.head_dim;
+ s.n_rot = g.n_rot;
+ s.idx_n_head = g.idx_q_heads;
+ s.idx_dim = g.idx_key_dim;
+ s.idx_block = g.idx_block;
+ s.idx_top_k = g.idx_top_k;
+ return s;
}
/// One layer's tensors, resolved by NAME. `get("attn_qkv.weight")` looks up `blk.<layer>.attn_qkv.weight`
diff --git a/include/strata/kernels/cpu/expert.hpp b/include/strata/kernels/cpu/expert.hpp
index 441dfac0a..16dcabd51 100644
--- a/include/strata/kernels/cpu/expert.hpp
+++ b/include/strata/kernels/cpu/expert.hpp
@@ -25,6 +25,8 @@
// per weight block is the natural-looking mistake.
#pragma once
+#include "strata/core/geometry.hpp"
+
#include <cstdint>
#include <cstddef>
@@ -34,6 +36,8 @@ namespace strata::kernels::cpu {
inline constexpr int H = 2560; // n_embd
inline constexpr int FF = 640; // expert intermediate width
inline constexpr int NE = 512; // routed experts per layer
+static_assert(H == strata::core::kCompiledGeometry.n_embd && FF == strata::core::kCompiledGeometry.n_ff,
+ "the CPU expert path is compiled for core/geometry.hpp");
inline constexpr int QK = 64; // QK2_0: weights per fp16 scale
inline constexpr int QKA = 32; // QK8_1/QK8_0: ACTIVATION elements per scale
inline constexpr int BB = 18; // bytes per 64-weight Q2_0 block: 2 fp16-scale + 16 codes
diff --git a/src/core/layer.cpp b/src/core/layer.cpp
index 45f19c081..1e6b3b930 100644
--- a/src/core/layer.cpp
+++ b/src/core/layer.cpp
@@ -503,9 +503,7 @@ bool moe_layer(const WeightTable& tables, const ModelGeometry& g, int64_t layer,
if (!moe_route(tables, g, layer, k, b, x, stream, err, db)) return false; return moe_finish(tables, g, layer, k, b, x, parts, out, stream, err);}
// ================================ the QSA mixer ================================
namespace {using strata::kernels::QsaIndexerBuffers;using strata::kernels::QsaShapes;
-/// The geometry the QSA kernels want, from the one place that defines it. `ModelGeometry` carries the widths
-/// the LAYOUT needs; `QsaShapes` adds `n_rot`, `idx_block` and `idx_top_k`, which are kernel contracts.
-QsaShapes qsa_shapes(const ModelGeometry& g) { QsaShapes s = strata::kernels::qsa_real_shapes(); s.n_head = g.n_head; s.n_head_kv = g.n_head_kv; s.head_dim = g.head_dim; s.idx_n_head = g.idx_q_heads; s.idx_dim = g.idx_key_dim; return s;}
+// `qsa_shapes(g)` (layout.hpp): the geometry the QSA kernels want, every field from the model's geometry
uint64_t align_up16(uint64_t n) { return (n + 15) & ~15ull; }
/// One cursor over an arena, so every region is 16-byte aligned without a list of hand-added offsets.
struct Cursor { uint8_t* p; uint64_t used = 0; template <typename T> T* take(uint64_t count) { T* r = (T*) (p + used); used = align_up16(used + count * sizeof(T)); return r; } uint8_t* take_bytes(uint64_t n) { uint8_t* r = p + used; used = align_up16(used + n); return r; }};
diff --git a/src/core/mtp.cpp b/src/core/mtp.cpp
index 0ca8cd189..dbf608820 100644
--- a/src/core/mtp.cpp
+++ b/src/core/mtp.cpp
@@ -85,15 +85,7 @@ bool mapped(size_t bytes, void** h, void** d) {
return cudaHostGetDevicePointer(d, *h, 0) == cudaSuccess;
}
-strata::kernels::QsaShapes shapes_of(const ModelGeometry& g) {
- strata::kernels::QsaShapes s = strata::kernels::qsa_real_shapes();
- s.n_head = g.n_head;
- s.n_head_kv = g.n_head_kv;
- s.head_dim = g.head_dim;
- s.idx_n_head = g.idx_q_heads;
- s.idx_dim = g.idx_key_dim;
- return s;
-}
+strata::kernels::QsaShapes shapes_of(const ModelGeometry& g) { return qsa_shapes(g); }
// 64-bit seek/tell on a `FILE*`: `fseek`/`ftell` take a 32-bit `long` on Windows and would wrap past 2 GiB.
#if defined(_WIN32)
diff --git a/src/core/session.cpp b/src/core/session.cpp
index 8d1fad916..55317c78a 100644
--- a/src/core/session.cpp
+++ b/src/core/session.cpp
@@ -176,12 +176,7 @@ void gdn_point_at(const ModelGeometry& g, int64_t layer, SessionState& s) {
/// The per-token staging every QSA layer's captured H2D reads FROM. Must run before each replay: the graphs
/// captured the SOURCE POINTER, not the value, and that is exactly why the buffers are pinned and fixed.
void stage_token(const ModelGeometry& g, int64_t pos, int32_t pos_base, SessionState& s) {
- strata::kernels::QsaShapes sh = strata::kernels::qsa_real_shapes();
- sh.n_head = g.n_head;
- sh.n_head_kv = g.n_head_kv;
- sh.head_dim = g.head_dim;
- sh.idx_n_head = g.idx_q_heads;
- sh.idx_dim = g.idx_key_dim;
+ const strata::kernels::QsaShapes sh = qsa_shapes(g);
for (int64_t j = 0; j < s.qsa_alloc; ++j) {
QsaState& q = s.qsa_states[s.qsa_ord0 + j];
qsa_step_fill(q.host_step, pos, sh);
diff --git a/src/core/verify.cpp b/src/core/verify.cpp
index 3995ae9dd..bb108338a 100644
--- a/src/core/verify.cpp
+++ b/src/core/verify.cpp
@@ -305,15 +305,7 @@ bool mapped(size_t bytes, void** h, void** d) {
return cudaHostGetDevicePointer(d, *h, 0) == cudaSuccess;
}
-strata::kernels::QsaShapes shapes_of(const ModelGeometry& g) {
- strata::kernels::QsaShapes s = strata::kernels::qsa_real_shapes();
- s.n_head = g.n_head;
- s.n_head_kv = g.n_head_kv;
- s.head_dim = g.head_dim;
- s.idx_n_head = g.idx_q_heads;
- s.idx_dim = g.idx_key_dim;
- return s;
-}
+strata::kernels::QsaShapes shapes_of(const ModelGeometry& g) { return qsa_shapes(g); }
const WeightRef* need(const LayerView& v, const char* suffix, std::string& err) {
const WeightRef* r = v.get(suffix);
diff --git a/src/kernels/cuda/fused_gdn.cu b/src/kernels/cuda/fused_gdn.cu
index 5d39cd2d0..d3311c255 100644
--- a/src/kernels/cuda/fused_gdn.cu
+++ b/src/kernels/cuda/fused_gdn.cu
@@ -1,5 +1,6 @@
// src/kernels/cuda/fused_gdn.cu - see include/strata/kernels/fused_gdn.hpp.
#include "strata/kernels/fused_gdn.hpp"
+#include "strata/core/geometry.hpp"
#include <cuda_runtime.h>
@@ -10,6 +11,7 @@ namespace strata::kernels {
namespace {
constexpr int S = 128; // state size (rows = cols = 128)
+static_assert(S == strata::core::kCompiledGeometry.ssm_state_size, "fused_gdn is compiled for core/geometry.hpp");
constexpr int RG = 4; // row groups
constexpr int RPG = S / RG; // 32 rows per thread
diff --git a/src/kernels/cuda/fused_gr.cu b/src/kernels/cuda/fused_gr.cu
index ab9842406..c3c2fc8d9 100644
--- a/src/kernels/cuda/fused_gr.cu
+++ b/src/kernels/cuda/fused_gr.cu
@@ -1,6 +1,7 @@
// src/kernels/cuda/fused_gr.cu - see include/strata/kernels/fused_gr.hpp.
#include "strata/core/emulate.hpp"
#include "strata/kernels/q8_1_finite.hpp" // #606: q8_1_ds
+#include "strata/core/geometry.hpp"
#include "strata/kernels/fused_gr.hpp"
#include "strata/kernels/bf16_bits.hpp"
#include "strata/kernels/verify_kernels.hpp"
@@ -31,6 +32,7 @@ constexpr int LR = 320; // hc_lr
#else
#define STRATA_GR_FAST_BUILD 0
#endif
+static_assert(N == strata::core::kCompiledGeometry.n_embd && HC == strata::core::kCompiledGeometry.hc && LR == strata::core::kCompiledGeometry.hc_lr, "fused_gr is compiled for core/geometry.hpp");
constexpr int THREADS = 256;
constexpr int WARPS = THREADS / 32;
constexpr int DOWN_BLOCKS = LR / WARPS; // 40 blocks of 8 rows; one more for the inject rows
diff --git a/src/kernels/cuda/native_gdn.cu b/src/kernels/cuda/native_gdn.cu
index 6661d50f4..7dae8c8c4 100644
--- a/src/kernels/cuda/native_gdn.cu
+++ b/src/kernels/cuda/native_gdn.cu
@@ -24,6 +24,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "strata/kernels/native_gdn.hpp"
+#include "strata/core/geometry.hpp"
#include <cuda_runtime.h>
#include <atomic>
#include <cmath>
@@ -35,6 +36,7 @@ namespace strata::kernels {
namespace {
std::atomic<bool> enabled{false};
constexpr int S = 128;
+static_assert(S == strata::core::kCompiledGeometry.ssm_state_size, "native_gdn is compiled for core/geometry.hpp");
__device__ __forceinline__ float warp_sum(float value) {
#pragma unroll
diff --git a/src/kernels/cuda/native_ple_postops.cu b/src/kernels/cuda/native_ple_postops.cu
index d9aae46b4..4f2db9403 100644
--- a/src/kernels/cuda/native_ple_postops.cu
+++ b/src/kernels/cuda/native_ple_postops.cu
@@ -19,6 +19,7 @@
// SOFTWARE.
#include "strata/kernels/native_ple_postops.hpp"
+#include "strata/core/geometry.hpp"
#include "strata/kernels/native_gr_norm.hpp"
#include "strata/kernels/ngram.hpp"
#include <cuda_runtime.h>
@@ -33,6 +34,7 @@
namespace strata::kernels {
namespace {
constexpr int N = 2560, H = 4, D = N * H, HISTORY = 9;
+static_assert(N == strata::core::kCompiledGeometry.n_embd && H == strata::core::kCompiledGeometry.hc, "native_ple_postops is compiled for core/geometry.hpp");
__device__ float warp_sum(float x) {
for (int offset = 16; offset; offset >>= 1) x += __shfl_xor_sync(0xffffffffu, x, offset);
return x;
diff --git a/src/kernels/cuda/native_qsa_indexer.cu b/src/kernels/cuda/native_qsa_indexer.cu
index f919e8ca4..5d0dad423 100644
--- a/src/kernels/cuda/native_qsa_indexer.cu
+++ b/src/kernels/cuda/native_qsa_indexer.cu
@@ -19,6 +19,7 @@
// SOFTWARE.
#include "strata/kernels/native_qsa_indexer.hpp"
+#include "strata/core/geometry.hpp"
#include "strata/kernels/mrope.hpp"
#include <cuda_runtime.h>
#include <cuda_fp16.h>
@@ -33,6 +34,8 @@ namespace strata::kernels {
namespace {
std::atomic<bool> enabled{false};
constexpr int D = 128, R = 4, ROT = 64, THREADS = 256;
+static_assert(D == strata::core::kCompiledGeometry.idx_key_dim && R == strata::core::kCompiledGeometry.idx_block && ROT == strata::core::kCompiledGeometry.n_rot,
+ "native_qsa_indexer is compiled for core/geometry.hpp");
__device__ float warp_sum(float x) {
#pragma unroll
for (int offset = 16; offset; offset >>= 1)
diff --git a/src/kernels/cuda/native_qsa_score.cu b/src/kernels/cuda/native_qsa_score.cu
index cdcfeadd3..4d10757e9 100644
--- a/src/kernels/cuda/native_qsa_score.cu
+++ b/src/kernels/cuda/native_qsa_score.cu
@@ -19,6 +19,7 @@
// SOFTWARE.
#include "strata/kernels/native_qsa_score.hpp"
+#include "strata/core/geometry.hpp"
#include <cuda_runtime.h>
#include <atomic>
#include <cstddef>
@@ -29,6 +30,8 @@ namespace strata::kernels {
namespace {
std::atomic<bool> enabled{false};
constexpr int D=128, HEADS=4, R=4, ROWS=32, WARPS=2, STRIDE=36, COMBINE=68;
+static_assert(D == strata::core::kCompiledGeometry.idx_key_dim && HEADS == strata::core::kCompiledGeometry.idx_q_heads && R == strata::core::kCompiledGeometry.idx_block,
+ "native_qsa_score is compiled for core/geometry.hpp");
struct TileA { uint32_t x[4]; };
struct TileB { uint32_t x[2]; };
struct TileC { float x[4]={0.0f,0.0f,0.0f,0.0f}; };
diff --git a/src/kernels/cuda/qsa_decode_attn.cu b/src/kernels/cuda/qsa_decode_attn.cu
index 624036c61..bff0e4200 100644
--- a/src/kernels/cuda/qsa_decode_attn.cu
+++ b/src/kernels/cuda/qsa_decode_attn.cu
@@ -1,5 +1,6 @@
// src/kernels/cuda/qsa_decode_attn.cu - see include/strata/kernels/qsa_decode_attn.hpp.
#include "strata/kernels/qsa_decode_attn.hpp"
+#include "strata/core/geometry.hpp"
#include "strata/kernels/kv_q8.hpp"
#include "strata/kernels/kv_q4.hpp"
#if defined(STRATA_HIP_GFX906)
@@ -19,6 +20,7 @@ namespace {
constexpr int HD = 256; // head_dim
constexpr int G = 12; // query heads per KV head (24 / 2)
+static_assert(HD == strata::core::kCompiledGeometry.head_dim && G == strata::core::kCompiledGeometry.n_head / strata::core::kCompiledGeometry.n_head_kv, "qsa_decode_attn is compiled for core/geometry.hpp");
constexpr int CHUNK = 64; // cells per block
constexpr int THREADS = 256;
constexpr int WARPS = THREADS / 32;
diff --git a/src/kernels/cuda/qsa_prompt_attn.cu b/src/kernels/cuda/qsa_prompt_attn.cu
index 3a0a8e38a..bd99609db 100644
--- a/src/kernels/cuda/qsa_prompt_attn.cu
+++ b/src/kernels/cuda/qsa_prompt_attn.cu
@@ -1,5 +1,6 @@
// src/kernels/cuda/qsa_prompt_attn.cu - see include/strata/kernels/qsa_prompt_attn.hpp.
#include "strata/core/emulate.hpp"
+#include "strata/core/geometry.hpp"
#include <cstdlib>
#include <cstring>
#include "strata/kernels/qsa_prompt_attn.hpp"
@@ -21,6 +22,7 @@ namespace {
constexpr int HD = 256; // head_dim
constexpr int G = 12; // query heads per KV head
+static_assert(HD == strata::core::kCompiledGeometry.head_dim && G == strata::core::kCompiledGeometry.n_head / strata::core::kCompiledGeometry.n_head_kv, "qsa_prompt_attn is compiled for core/geometry.hpp");
#ifndef D1_CH
#define D1_CH 32
#endif
diff --git a/src/kernels/cuda/qsa_select.cu b/src/kernels/cuda/qsa_select.cu
index d880fc6e9..cb06396a5 100644
--- a/src/kernels/cuda/qsa_select.cu
+++ b/src/kernels/cuda/qsa_select.cu
@@ -1,5 +1,6 @@
// src/kernels/cuda/qsa_select.cu - see include/strata/kernels/qsa_select.hpp.
#include "strata/core/emulate.hpp"
+#include "strata/core/geometry.hpp"
#include <cstdlib>
#include <cstring>
#include "strata/kernels/qsa_select.hpp"
@@ -16,6 +17,8 @@ namespace strata::kernels {
namespace {
constexpr int IDX_DIM = 128, IDX_HEADS = 4, R = 4;
+static_assert(IDX_DIM == strata::core::kCompiledGeometry.idx_key_dim && IDX_HEADS == strata::core::kCompiledGeometry.idx_q_heads && R == strata::core::kCompiledGeometry.idx_block,
+ "qsa_select is compiled for core/geometry.hpp");
constexpr int SCORE_WARPS = 8;
constexpr int TOPK_T = 256;
diff --git a/src/kernels/cuda/s2_expert_grouped.cu b/src/kernels/cuda/s2_expert_grouped.cu
index bb8890299..77bd5d27c 100644
--- a/src/kernels/cuda/s2_expert_grouped.cu
+++ b/src/kernels/cuda/s2_expert_grouped.cu
@@ -14,6 +14,7 @@
// The two agree to float rounding and not to the bit, which is the same contract `s_gemv_parity` carries for
// the same reason. `bench/micro/moe_hit_parity.cu` is the check.
#include "strata/kernels/s2_expert_grouped.hpp"
+#include "strata/core/geometry.hpp"
#include "strata/kernels/dp4a.hpp"
#include "strata/kernels/f16_bits.hpp"
@@ -37,6 +38,7 @@ namespace {
// are what the CPU kernel's indexing computes, and `moe_hit_parity` compares the two end to end.
constexpr int H = 2560;
constexpr int FF = 640;
+static_assert(H == strata::core::kCompiledGeometry.n_embd && FF == strata::core::kCompiledGeometry.n_ff, "s2_expert_grouped is compiled for core/geometry.hpp");
constexpr int QK = 64; // Q2_0's group: one fp16 scale per 64 weights
constexpr int ROW_GU = H / 4; // 640 B of codes per gate/up row (2 bits per element)
constexpr int ROW_D = FF / 4; // 160 B per down row
diff --git a/src/kernels/cuda/verify_kernels.cu b/src/kernels/cuda/verify_kernels.cu
index ac2855d78..32ce40a1a 100644
--- a/src/kernels/cuda/verify_kernels.cu
+++ b/src/kernels/cuda/verify_kernels.cu
@@ -3,6 +3,7 @@
// The per-token arithmetic of every kernel here is transcribed from its single-token original (fused_gdn.cu,
// elementwise.cu) with the same operation order, so a verify window reproduces plain decode bit for bit.
#include "strata/kernels/verify_kernels.hpp"
+#include "strata/core/geometry.hpp"
#include "strata/core/emulate.hpp"
#include "strata/kernels/q8_1_finite.hpp" // #606: q8_1_ds
#include "strata/kernels/dp4a.hpp"
@@ -24,6 +25,7 @@ namespace strata::kernels {
namespace {
constexpr int S = 128; // GDN state size
+static_assert(S == strata::core::kCompiledGeometry.ssm_state_size, "verify_kernels is compiled for core/geometry.hpp");
constexpr int RG = 4;
constexpr int RPG = S / RG;
diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp
index d13fdd197..3f537abdd 100644
--- a/src/prefill/prefill.cpp
+++ b/src/prefill/prefill.cpp
@@ -1228,9 +1228,7 @@ bool Prefill::carve(size_t T, void* alloc) {
if (bf16x2_hc(m.f16_io)) { m.xn16_lo = o.take<uint16_t>(T * D, ok); m.lo16_lo = o.take<uint16_t>(T * LR, ok); }
if (bf16x2(m.f16_io)) m.mixed_bf_lo = o.take<uint16_t>(T * N, ok);
m.steps_dev = o.take<int32_t>(T * strata::kernels::kStepCount, ok);
- strata::kernels::QsaShapes s = strata::kernels::qsa_real_shapes();
- s.n_head = g.n_head; s.n_head_kv = g.n_head_kv; s.head_dim = g.head_dim; s.idx_n_head = g.idx_q_heads;
- s.idx_dim = g.idx_key_dim;
+ strata::kernels::QsaShapes s = core::qsa_shapes(g);
m.cap = strata::kernels::qsa_selection_width(strata::kernels::kTopkMaxCells, s);
m.max_blocks = ss.qsa_states[ss.qsa_primary()].max_cells / s.idx_block + 2;
{
@@ -1427,9 +1425,7 @@ bool Prefill::draft_kv(core::MtpDrafter& mtp, const float* R_rows, const int32_t
if (!mtp.idle(err)) return false; // the drafter's own stream (its graph uploads) before this writes its K/V
const double ms_idle = ms_since(ti0);
const auto tl0 = Clock::now();
- strata::kernels::QsaShapes s = strata::kernels::qsa_real_shapes();
- s.n_head = g.n_head; s.n_head_kv = g.n_head_kv; s.head_dim = g.head_dim; s.idx_n_head = g.idx_q_heads;
- s.idx_dim = g.idx_key_dim;
+ strata::kernels::QsaShapes s = core::qsa_shapes(g);
std::vector<int32_t> tk((size_t) B);
if (q8) {
if (!m.mmq_ctx) m.mmq_ctx = std::make_unique<mmq::Context>();
@@ -1864,9 +1860,7 @@ uint64_t Prefill::bytes_needed_impl(const core::ModelGeometry& g, const core::Se
if (bf16x2_hc(f16_io)) { o.take<uint16_t>(T * D, ok); o.take<uint16_t>(T * LR, ok); }
if (bf16x2(f16_io)) o.take<uint16_t>(T * N, ok);
o.take<int32_t>(T * strata::kernels::kStepCount, ok);
- strata::kernels::QsaShapes s = strata::kernels::qsa_real_shapes();
- s.n_head = g.n_head; s.n_head_kv = g.n_head_kv; s.head_dim = g.head_dim; s.idx_n_head = g.idx_q_heads;
- s.idx_dim = g.idx_key_dim;
+ strata::kernels::QsaShapes s = core::qsa_shapes(g);
const int64_t cap = strata::kernels::qsa_selection_width(strata::kernels::kTopkMaxCells, s);
const int64_t max_blocks = ss.qsa_states[ss.qsa_primary()].max_cells / s.idx_block + 2;
o.take<uint8_t>((size_t) std::max({gdn_set_bytes(T), qsa_set_bytes(T, cap, max_blocks, 256, 32, s),
@@ -2055,9 +2049,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
// The direct successor's future lives on the Prefill object. Intermediate
// stages therefore do not drain the complete remaining GPU chain here.
double host_sync_ms = 0, host_chunk_ms = 0, host_setup_ms = 0; // STRATA_PREFILL_TIMING: the host's share
- strata::kernels::QsaShapes s = strata::kernels::qsa_real_shapes();
- s.n_head = g.n_head; s.n_head_kv = g.n_head_kv; s.head_dim = g.head_dim; s.idx_n_head = g.idx_q_heads;
- s.idx_dim = g.idx_key_dim;
+ strata::kernels::QsaShapes s = core::qsa_shapes(g);
// read once (STRATA_KV_PREFETCH=1/0: the next streamed layer's KV prefix is staged on its own stream)
static const bool kv_prefetch_on = [] {
const char* e = std::getenv("STRATA_KV_PREFETCH");
diff --git a/src/program/generate.cpp b/src/program/generate.cpp
index 2cd885c92..734837fc7 100644
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@ -2831,8 +2831,10 @@ int main(int argc, char** argv) {
? "AVX1 128-bit (iq_avx1, the older-CPU build)"
: "ggml-cpu vec_dot (no AVX2: the older-CPU build)")
: std::getenv("STRATA_NO_IQ256") == nullptr ? "AVX-2" : "ggml-cpu vec_dot (STRATA_NO_IQ256 set)");
- strata::core::ModelGeometry g; // canonical defaults; the model file overrides the MoE shape below
- int64_t K = 10;
+ // the compiled shape; a native model's file replaces it below with its own metadata (read_geometry), which a
+ // pruned variant (GSQ-RCO Coder, fewer than 512 experts) differs from
+ strata::core::ModelGeometry g;
+ int64_t K = g.n_expert_used;
// THE ROPE CONFIG RESOLVES HERE, BEFORE ANY WEIGHT MOVES - the CLI and the model file have both spoken,
// and `session_init` below builds the rope table from it and captures the kernels reading its constants
// (rope_scaling.hpp); the only hard constraint is "set before that", and dying on a bad rope key beats
@@ -2845,19 +2847,24 @@ int main(int argc, char** argv) {
std::string gguf_rope_type;
double gguf_rope_base = 0, gguf_rope_factor = 0, gguf_rope_orig_ctx = 0;
if (!o.native_preset.empty()) {
- // a pruned variant (GSQ-RCO Coder) ships fewer experts than the canonical 512x10; the model file
- // is the authority on its own MoE shape - everything else in the geometry is unchanged
+ // the model file is the authority on its own geometry; the kernels' compiled shape is checked against
+ // it before any weight moves, naming the key that differs
try {
strata::GgufFile model_gguf(o.native_shards.front()); // the metadata shard
- if (const strata::MetaValue* v = model_gguf.get("qwen4exp.expert_count")) g.n_expert = (int64_t) v->u;
- if (const strata::MetaValue* v = model_gguf.get("qwen4exp.expert_used_count")) K = (int64_t) v->u;
+ std::string gerr = strata::read_geometry(model_gguf, g);
+ if (gerr.empty()) gerr = strata::geometry_unsupported(g);
+ if (!gerr.empty()) {
+ std::fprintf(stderr, "strata generate: %s: %s\n", o.native_shards.front().c_str(), gerr.c_str());
+ return 1;
+ }
+ K = g.n_expert_used;
if (const strata::MetaValue* v = model_gguf.get("qwen4exp.rope.freq_base")) gguf_rope_base = v->num();
if (const strata::MetaValue* v = model_gguf.get("qwen4exp.rope.scaling.type")) gguf_rope_type = v->s;
if (const strata::MetaValue* v = model_gguf.get("qwen4exp.rope.scaling.factor")) gguf_rope_factor = v->num();
if (const strata::MetaValue* v = model_gguf.get("qwen4exp.rope.scaling.original_context_length"))
gguf_rope_orig_ctx = v->num();
} catch (const std::exception& e) {
- std::fprintf(stderr, "strata generate: reading the model's expert shape from %s: %s\n",
+ std::fprintf(stderr, "strata generate: reading the model's geometry from %s: %s\n",
o.native_preset.c_str(), e.what());
return 1;
}
@@ -2942,10 +2949,9 @@ int main(int argc, char** argv) {
"and --prefill CHUNK\n", o.pack.c_str());
return 2;
}
- const strata::core::ModelGeometry g0;
const auto embed_t0 = std::chrono::steady_clock::now();
- if (!native_embed.load(o.embd_gguf.empty() ? o.native_shards : std::vector<std::string>{o.embd_gguf}, g0.n_embd,
- 248320, err)) {
+ if (!native_embed.load(o.embd_gguf.empty() ? o.native_shards : std::vector<std::string>{o.embd_gguf}, g.n_embd,
+ g.n_vocab, err)) {
std::fprintf(stderr, "strata generate: %s\n", err.c_str());
return 1;
}
diff --git a/sycl/src/core/layer.cpp b/sycl/src/core/layer.cpp
index 5cb981f88..78adeb545 100644
--- a/sycl/src/core/layer.cpp
+++ b/sycl/src/core/layer.cpp
@@ -563,9 +563,7 @@ bool moe_layer(const WeightTable& tables, const ModelGeometry& g, int64_t layer,
if (!moe_route(tables, g, layer, k, b, x, stream, err, db)) return false; return moe_finish(tables, g, layer, k, b, x, parts, out, stream, err);}
// ================================ the QSA mixer ================================
namespace {using strata::kernels::QsaIndexerBuffers;using strata::kernels::QsaShapes;
-/// The geometry the QSA kernels want, from the one place that defines it. `ModelGeometry` carries the widths
-/// the LAYOUT needs; `QsaShapes` adds `n_rot`, `idx_block` and `idx_top_k`, which are kernel contracts.
-QsaShapes qsa_shapes(const ModelGeometry& g) { QsaShapes s = strata::kernels::qsa_real_shapes(); s.n_head = g.n_head; s.n_head_kv = g.n_head_kv; s.head_dim = g.head_dim; s.idx_n_head = g.idx_q_heads; s.idx_dim = g.idx_key_dim; return s;}
+// `qsa_shapes(g)` (layout.hpp): the geometry the QSA kernels want, every field from the model's geometry
uint64_t align_up16(uint64_t n) { return (n + 15) & ~15ull; }
/// One cursor over an arena, so every region is 16-byte aligned without a list of hand-added offsets.
struct Cursor { uint8_t* p; uint64_t used = 0; template <typename T> T* take(uint64_t count) { T* r = (T*) (p + used); used = align_up16(used + count * sizeof(T)); return r; } uint8_t* take_bytes(uint64_t n) { uint8_t* r = p + used; used = align_up16(used + n); return r; }};
diff --git a/sycl/src/core/mtp.cpp b/sycl/src/core/mtp.cpp
index 2d97cc3c4..773f54f57 100644
--- a/sycl/src/core/mtp.cpp
+++ b/sycl/src/core/mtp.cpp
@@ -99,15 +99,7 @@ catch (sycl::exception const &exc) {
std::exit(1);
}
-strata::kernels::QsaShapes shapes_of(const ModelGeometry& g) {
- strata::kernels::QsaShapes s = strata::kernels::qsa_real_shapes();
- s.n_head = g.n_head;
- s.n_head_kv = g.n_head_kv;
- s.head_dim = g.head_dim;
- s.idx_n_head = g.idx_q_heads;
- s.idx_dim = g.idx_key_dim;
- return s;
-}
+strata::kernels::QsaShapes shapes_of(const ModelGeometry& g) { return qsa_shapes(g); }
// 64-bit seek/tell on a `FILE*`: `fseek`/`ftell` take a 32-bit `long` on Windows and would wrap past 2 GiB.
#if defined(_WIN32)
diff --git a/sycl/src/core/session.cpp b/sycl/src/core/session.cpp
index 9edd8082e..933468cf3 100644
--- a/sycl/src/core/session.cpp
+++ b/sycl/src/core/session.cpp
@@ -183,12 +183,7 @@ void gdn_point_at(const ModelGeometry& g, int64_t layer, SessionState& s) {
/// The per-token staging every QSA layer's captured H2D reads FROM. Must run before each replay: the graphs
/// captured the SOURCE POINTER, not the value, and that is exactly why the buffers are pinned and fixed.
void stage_token(const ModelGeometry& g, int64_t pos, int32_t pos_base, SessionState& s) {
- strata::kernels::QsaShapes sh = strata::kernels::qsa_real_shapes();
- sh.n_head = g.n_head;
- sh.n_head_kv = g.n_head_kv;
- sh.head_dim = g.head_dim;
- sh.idx_n_head = g.idx_q_heads;
- sh.idx_dim = g.idx_key_dim;
+ const strata::kernels::QsaShapes sh = qsa_shapes(g);
for (int64_t j = 0; j < s.qsa_alloc; ++j) {
QsaState& q = s.qsa_states[s.qsa_ord0 + j];
qsa_step_fill(q.host_step, pos, sh);
diff --git a/sycl/src/core/verify.cpp b/sycl/src/core/verify.cpp
index 71f8cce9e..937280044 100644
--- a/sycl/src/core/verify.cpp
+++ b/sycl/src/core/verify.cpp
@@ -246,15 +246,7 @@ catch (sycl::exception const &exc) {
std::exit(1);
}
-strata::kernels::QsaShapes shapes_of(const ModelGeometry& g) {
- strata::kernels::QsaShapes s = strata::kernels::qsa_real_shapes();
- s.n_head = g.n_head;
- s.n_head_kv = g.n_head_kv;
- s.head_dim = g.head_dim;
- s.idx_n_head = g.idx_q_heads;
- s.idx_dim = g.idx_key_dim;
- return s;
-}
+strata::kernels::QsaShapes shapes_of(const ModelGeometry& g) { return qsa_shapes(g); }
const WeightRef* need(const LayerView& v, const char* suffix, std::string& err) {
const WeightRef* r = v.get(suffix);
diff --git a/sycl/src/kernels/cuda/fused_gdn.dp.cpp b/sycl/src/kernels/cuda/fused_gdn.dp.cpp
index ba5499529..8ce9ff3bf 100644
--- a/sycl/src/kernels/cuda/fused_gdn.dp.cpp
+++ b/sycl/src/kernels/cuda/fused_gdn.dp.cpp
@@ -1,6 +1,7 @@
// src/kernels/cuda/fused_gdn.cu - see include/strata/kernels/fused_gdn.hpp.
#define DPCT_PROFILING_ENABLED
#include <sycl/sycl.hpp>
+#include "strata/core/geometry.hpp"
#include <dpct/dpct.hpp>
#include "strata/sycl_queue.hpp"
#include "strata/kernels/fused_gdn.hpp"
@@ -12,6 +13,7 @@ namespace strata::kernels {
namespace {
constexpr int S = 128; // state size (rows = cols = 128)
+static_assert(S == strata::core::kCompiledGeometry.ssm_state_size, "fused_gdn is compiled for core/geometry.hpp");
constexpr int RG = 4; // row groups
constexpr int RPG = S / RG; // 32 rows per thread
diff --git a/sycl/src/kernels/cuda/fused_gr.dp.cpp b/sycl/src/kernels/cuda/fused_gr.dp.cpp
index 2c8d12469..ef018e2d4 100644
--- a/sycl/src/kernels/cuda/fused_gr.dp.cpp
+++ b/sycl/src/kernels/cuda/fused_gr.dp.cpp
@@ -1,6 +1,7 @@
// src/kernels/cuda/fused_gr.cu - see include/strata/kernels/fused_gr.hpp.
#define DPCT_PROFILING_ENABLED
#include <mutex>
+#include "strata/core/geometry.hpp"
#include <unordered_map>
#include <sycl/sycl.hpp>
#include <dpct/dpct.hpp>
@@ -25,6 +26,7 @@ constexpr int N = 2560; // n_embd
constexpr int HC = 4; // streams
constexpr int D = N * HC; // 10240
constexpr int LR = 320; // hc_lr
+static_assert(N == strata::core::kCompiledGeometry.n_embd && HC == strata::core::kCompiledGeometry.hc && LR == strata::core::kCompiledGeometry.hc_lr, "fused_gr is compiled for core/geometry.hpp");
constexpr int THREADS = 256;
constexpr int WARPS = THREADS / 32;
#if defined(__HIPCC__)
diff --git a/sycl/src/kernels/cuda/native_gdn.dp.cpp b/sycl/src/kernels/cuda/native_gdn.dp.cpp
index 4faf25a36..4099841a0 100644
--- a/sycl/src/kernels/cuda/native_gdn.dp.cpp
+++ b/sycl/src/kernels/cuda/native_gdn.dp.cpp
@@ -25,6 +25,7 @@
// SOFTWARE.
#define DPCT_PROFILING_ENABLED
#include <sycl/sycl.hpp>
+#include "strata/core/geometry.hpp"
#include <dpct/dpct.hpp>
#include "strata/sycl_queue.hpp"
#include "strata/kernels/native_gdn.hpp"
@@ -38,6 +39,7 @@ namespace strata::kernels {
namespace {
std::atomic<bool> enabled{false};
constexpr int S = 128;
+static_assert(S == strata::core::kCompiledGeometry.ssm_state_size, "native_gdn is compiled for core/geometry.hpp");
__dpct_inline__ float warp_sum(float value) {
#pragma unroll
diff --git a/sycl/src/kernels/cuda/native_ple_postops.dp.cpp b/sycl/src/kernels/cuda/native_ple_postops.dp.cpp
index a55748bd8..121015268 100644
--- a/sycl/src/kernels/cuda/native_ple_postops.dp.cpp
+++ b/sycl/src/kernels/cuda/native_ple_postops.dp.cpp
@@ -20,6 +20,7 @@
#define DPCT_PROFILING_ENABLED
#include <sycl/sycl.hpp>
+#include "strata/core/geometry.hpp"
#include <dpct/dpct.hpp>
#include "strata/sycl_queue.hpp"
#include "strata/kernels/native_ple_postops.hpp"
@@ -35,6 +36,7 @@
namespace strata::kernels {
namespace {
constexpr int N = 2560, H = 4, D = N * H, HISTORY = 9;
+static_assert(N == strata::core::kCompiledGeometry.n_embd && H == strata::core::kCompiledGeometry.hc, "native_ple_postops is compiled for core/geometry.hpp");
inline float warp_sum(float x) {
/*
DPCT1108: '__shfl_xor_sync' was migrated with the experimental feature masked
diff --git a/sycl/src/kernels/cuda/native_qsa_indexer.dp.cpp b/sycl/src/kernels/cuda/native_qsa_indexer.dp.cpp
index c1d89635e..c63948f64 100644
--- a/sycl/src/kernels/cuda/native_qsa_indexer.dp.cpp
+++ b/sycl/src/kernels/cuda/native_qsa_indexer.dp.cpp
@@ -20,6 +20,7 @@
#define DPCT_PROFILING_ENABLED
#include <sycl/sycl.hpp>
+#include "strata/core/geometry.hpp"
#include <dpct/dpct.hpp>
#include "strata/sycl_queue.hpp"
#include "strata/kernels/native_qsa_indexer.hpp"
@@ -35,6 +36,8 @@ namespace strata::kernels {
namespace {
std::atomic<bool> enabled{false};
constexpr int D = 128, R = 4, ROT = 64, THREADS = 256;
+static_assert(D == strata::core::kCompiledGeometry.idx_key_dim && R == strata::core::kCompiledGeometry.idx_block && ROT == strata::core::kCompiledGeometry.n_rot,
+ "native_qsa_indexer is compiled for core/geometry.hpp");
inline float warp_sum(float x) {
#pragma unroll
for (int offset = 16; offset; offset >>= 1)
diff --git a/sycl/src/kernels/cuda/native_qsa_score.dp.cpp b/sycl/src/kernels/cuda/native_qsa_score.dp.cpp
index fe729bc0e..d15b3ed76 100644
--- a/sycl/src/kernels/cuda/native_qsa_score.dp.cpp
+++ b/sycl/src/kernels/cuda/native_qsa_score.dp.cpp
@@ -20,6 +20,7 @@
#define DPCT_PROFILING_ENABLED
#include <sycl/sycl.hpp>
+#include "strata/core/geometry.hpp"
#include <dpct/dpct.hpp>
#include "strata/sycl_queue.hpp"
#include "strata/kernels/native_qsa_score.hpp"
@@ -32,6 +33,8 @@ namespace strata::kernels {
namespace {
std::atomic<bool> enabled{false};
constexpr int D=128, HEADS=4, R=4, ROWS=32, WARPS=2, STRIDE=36, COMBINE=68;
+static_assert(D == strata::core::kCompiledGeometry.idx_key_dim && HEADS == strata::core::kCompiledGeometry.idx_q_heads && R == strata::core::kCompiledGeometry.idx_block,
+ "native_qsa_score is compiled for core/geometry.hpp");
struct TileA { uint32_t x[4]; };
struct TileB { uint32_t x[2]; };
struct TileC { float x[4]={0.0f,0.0f,0.0f,0.0f}; };
diff --git a/sycl/src/kernels/cuda/qsa_decode_attn.dp.cpp b/sycl/src/kernels/cuda/qsa_decode_attn.dp.cpp
index 552c4a7db..c4c3d8c64 100644
--- a/sycl/src/kernels/cuda/qsa_decode_attn.dp.cpp
+++ b/sycl/src/kernels/cuda/qsa_decode_attn.dp.cpp
@@ -1,6 +1,7 @@
// src/kernels/cuda/qsa_decode_attn.cu - see include/strata/kernels/qsa_decode_attn.hpp.
#define DPCT_PROFILING_ENABLED
#include <sycl/sycl.hpp>
+#include "strata/core/geometry.hpp"
#include <dpct/dpct.hpp>
#include "strata/sycl_queue.hpp"
#include "strata/kernels/qsa_decode_attn.hpp"
@@ -17,6 +18,7 @@ namespace {
constexpr int HD = 256; // head_dim
constexpr int G = 12; // query heads per KV head (24 / 2)
+static_assert(HD == strata::core::kCompiledGeometry.head_dim && G == strata::core::kCompiledGeometry.n_head / strata::core::kCompiledGeometry.n_head_kv, "qsa_decode_attn is compiled for core/geometry.hpp");
constexpr int CHUNK = 64; // cells per block
constexpr int THREADS = 256;
constexpr int WARPS = THREADS / 32;
diff --git a/sycl/src/kernels/cuda/qsa_prompt_attn.dp.cpp b/sycl/src/kernels/cuda/qsa_prompt_attn.dp.cpp
index f4a6533be..e459919a6 100644
--- a/sycl/src/kernels/cuda/qsa_prompt_attn.dp.cpp
+++ b/sycl/src/kernels/cuda/qsa_prompt_attn.dp.cpp
@@ -1,6 +1,7 @@
// src/kernels/cuda/qsa_prompt_attn.cu - see include/strata/kernels/qsa_prompt_attn.hpp.
#define DPCT_PROFILING_ENABLED
#include <sycl/sycl.hpp>
+#include "strata/core/geometry.hpp"
#include <dpct/dpct.hpp>
#include "strata/sycl_queue.hpp"
#include "strata/core/emulate.hpp"
@@ -24,6 +25,7 @@ namespace {
constexpr int HD = 256; // head_dim
constexpr int G = 12; // query heads per KV head
+static_assert(HD == strata::core::kCompiledGeometry.head_dim && G == strata::core::kCompiledGeometry.n_head / strata::core::kCompiledGeometry.n_head_kv, "qsa_prompt_attn is compiled for core/geometry.hpp");
#ifndef D1_CH
#define D1_CH 32
#endif
diff --git a/sycl/src/kernels/cuda/qsa_prompt_attn_xmx.dp.cpp b/sycl/src/kernels/cuda/qsa_prompt_attn_xmx.dp.cpp
index 15516ee72..1aa9d2d37 100644
--- a/sycl/src/kernels/cuda/qsa_prompt_attn_xmx.dp.cpp
+++ b/sycl/src/kernels/cuda/qsa_prompt_attn_xmx.dp.cpp
@@ -13,6 +13,7 @@
// K^T packed as 4-byte pairs with INT8 converted on the load, hi and lo halves of Q accumulated into one accumulator per
// scale group, and one accumulator update per chunk after the whole P.V product has run in registers.
#include "strata/kernels/qsa_prompt_attn_xmx.hpp"
+#include "strata/core/geometry.hpp"
#include "strata/kernels/qsa_decode_attn.hpp"
#include "strata/kernels/qsa.hpp"
#include "strata/kernels/kv_q8.hpp"
@@ -29,6 +30,8 @@ namespace strata::kernels {
namespace {
namespace jm = sycl::ext::oneapi::experimental::matrix;
constexpr int HD = 256, G = 12, SG = 16, NSG = 8, THREADS = SG * NSG, QS = HD + 8;
+static_assert(HD == strata::core::kCompiledGeometry.head_dim && G == strata::core::kCompiledGeometry.n_head / strata::core::kCompiledGeometry.n_head_kv,
+ "qsa_prompt_attn_xmx is compiled for core/geometry.hpp");
constexpr int QK4_0 = 32;
struct q4_0_block { uint16_t d; uint8_t qs[QK4_0 / 2]; };
diff --git a/sycl/src/kernels/cuda/qsa_select.dp.cpp b/sycl/src/kernels/cuda/qsa_select.dp.cpp
index 481963bfe..adafc8986 100644
--- a/sycl/src/kernels/cuda/qsa_select.dp.cpp
+++ b/sycl/src/kernels/cuda/qsa_select.dp.cpp
@@ -1,6 +1,7 @@
// src/kernels/cuda/qsa_select.cu - see include/strata/kernels/qsa_select.hpp.
#define DPCT_PROFILING_ENABLED
#include <sycl/sycl.hpp>
+#include "strata/core/geometry.hpp"
#include <dpct/dpct.hpp>
#include "strata/sycl_queue.hpp"
#include "strata/core/emulate.hpp"
@@ -18,6 +19,8 @@ namespace strata::kernels {
namespace {
constexpr int IDX_DIM = 128, IDX_HEADS = 4, R = 4;
+static_assert(IDX_DIM == strata::core::kCompiledGeometry.idx_key_dim && IDX_HEADS == strata::core::kCompiledGeometry.idx_q_heads && R == strata::core::kCompiledGeometry.idx_block,
+ "qsa_select is compiled for core/geometry.hpp");
constexpr int SCORE_WARPS = 8;
constexpr int TOPK_T = 256;
diff --git a/sycl/src/kernels/cuda/s2_expert_grouped.dp.cpp b/sycl/src/kernels/cuda/s2_expert_grouped.dp.cpp
index 2da553b67..ec445df91 100644
--- a/sycl/src/kernels/cuda/s2_expert_grouped.dp.cpp
+++ b/sycl/src/kernels/cuda/s2_expert_grouped.dp.cpp
@@ -15,6 +15,7 @@
// the same reason. `bench/micro/moe_hit_parity.cu` is the check.
#define DPCT_PROFILING_ENABLED
#include <sycl/sycl.hpp>
+#include "strata/core/geometry.hpp"
#include <dpct/dpct.hpp>
#include "strata/sycl_math.hpp"
#include "strata/sycl_queue.hpp"
@@ -39,6 +40,7 @@ namespace {
// are what the CPU kernel's indexing computes, and `moe_hit_parity` compares the two end to end.
constexpr int H = 2560;
constexpr int FF = 640;
+static_assert(H == strata::core::kCompiledGeometry.n_embd && FF == strata::core::kCompiledGeometry.n_ff, "s2_expert_grouped is compiled for core/geometry.hpp");
constexpr int QK = 64; // Q2_0's group: one fp16 scale per 64 weights
constexpr int ROW_GU = H / 4; // 640 B of codes per gate/up row (2 bits per element)
constexpr int ROW_D = FF / 4; // 160 B per down row
diff --git a/sycl/src/kernels/cuda/verify_kernels.dp.cpp b/sycl/src/kernels/cuda/verify_kernels.dp.cpp
index 558477f9d..a268cd692 100644
--- a/sycl/src/kernels/cuda/verify_kernels.dp.cpp
+++ b/sycl/src/kernels/cuda/verify_kernels.dp.cpp
@@ -4,6 +4,7 @@
// elementwise.cu) with the same operation order, so a verify window reproduces plain decode bit for bit.
#define DPCT_PROFILING_ENABLED
#include <sycl/sycl.hpp>
+#include "strata/core/geometry.hpp"
#include <dpct/dpct.hpp>
#include "strata/sycl_queue.hpp"
#include "strata/sycl_doorbell.hpp"
@@ -30,6 +31,7 @@ namespace strata::kernels {
namespace {
constexpr int S = 128; // GDN state size
+static_assert(S == strata::core::kCompiledGeometry.ssm_state_size, "verify_kernels is compiled for core/geometry.hpp");
constexpr int RG = 4;
constexpr int RPG = S / RG;
diff --git a/sycl/src/prefill/prefill.cpp b/sycl/src/prefill/prefill.cpp
index 93bde7dd2..bebb039f3 100644
--- a/sycl/src/prefill/prefill.cpp
+++ b/sycl/src/prefill/prefill.cpp
@@ -1164,9 +1164,7 @@ bool Prefill::carve(size_t T, void* alloc) {
if (bf16x2_hc(m.f16_io)) { m.xn16_lo = o.take<uint16_t>(T * D, ok); m.lo16_lo = o.take<uint16_t>(T * LR, ok); }
if (bf16x2(m.f16_io)) m.mixed_bf_lo = o.take<uint16_t>(T * N, ok);
m.steps_dev = o.take<int32_t>(T * strata::kernels::kStepCount, ok);
- strata::kernels::QsaShapes s = strata::kernels::qsa_real_shapes();
- s.n_head = g.n_head; s.n_head_kv = g.n_head_kv; s.head_dim = g.head_dim; s.idx_n_head = g.idx_q_heads;
- s.idx_dim = g.idx_key_dim;
+ strata::kernels::QsaShapes s = core::qsa_shapes(g);
m.cap = strata::kernels::qsa_selection_width(strata::kernels::kTopkMaxCells, s);
m.max_blocks = ss.qsa_states[ss.qsa_primary()].max_cells / s.idx_block + 2;
{
@@ -1384,9 +1382,7 @@ bool Prefill::draft_kv(core::MtpDrafter& mtp, const float* R_rows, const int32_t
if (!mtp.idle(err)) return false; // the drafter's own stream (its graph uploads) before this writes its K/V
const double ms_idle = ms_since(ti0);
const auto tl0 = Clock::now();
- strata::kernels::QsaShapes s = strata::kernels::qsa_real_shapes();
- s.n_head = g.n_head; s.n_head_kv = g.n_head_kv; s.head_dim = g.head_dim; s.idx_n_head = g.idx_q_heads;
- s.idx_dim = g.idx_key_dim;
+ strata::kernels::QsaShapes s = core::qsa_shapes(g);
std::vector<int32_t> tk((size_t) B);
if (q8) {
if (!m.mmq_ctx) m.mmq_ctx = std::make_unique<mmq::Context>();
@@ -1888,9 +1884,7 @@ uint64_t Prefill::bytes_needed_impl(const core::ModelGeometry& g, const core::Se
if (bf16x2_hc(f16_io)) { o.take<uint16_t>(T * D, ok); o.take<uint16_t>(T * LR, ok); }
if (bf16x2(f16_io)) o.take<uint16_t>(T * N, ok);
o.take<int32_t>(T * strata::kernels::kStepCount, ok);
- strata::kernels::QsaShapes s = strata::kernels::qsa_real_shapes();
- s.n_head = g.n_head; s.n_head_kv = g.n_head_kv; s.head_dim = g.head_dim; s.idx_n_head = g.idx_q_heads;
- s.idx_dim = g.idx_key_dim;
+ strata::kernels::QsaShapes s = core::qsa_shapes(g);
const int64_t cap = strata::kernels::qsa_selection_width(strata::kernels::kTopkMaxCells, s);
const int64_t max_blocks = ss.qsa_states[ss.qsa_primary()].max_cells / s.idx_block + 2;
o.take<uint8_t>((size_t) std::max({gdn_set_bytes(T), qsa_set_bytes(T, cap, max_blocks, 256, 32, s),
@@ -2111,9 +2105,7 @@ bool Prefill::run_impl(const int64_t *tokens, int64_t n, int64_t pos0,
// stages therefore do not drain the complete remaining GPU chain here.
double host_sync_ms = 0, host_chunk_ms = 0, host_setup_ms = 0; // STRATA_PREFILL_TIMING: the host's share
double grp_wait_ms = 0, grp_cpu_ms = 0; // the per-layer grouping: the drain wait, the host's loops
- strata::kernels::QsaShapes s = strata::kernels::qsa_real_shapes();
- s.n_head = g.n_head; s.n_head_kv = g.n_head_kv; s.head_dim = g.head_dim; s.idx_n_head = g.idx_q_heads;
- s.idx_dim = g.idx_key_dim;
+ strata::kernels::QsaShapes s = core::qsa_shapes(g);
// read once (STRATA_KV_PREFETCH=1/0: the next streamed layer's KV prefix is staged on its own stream)
static const bool kv_prefetch_on = [] {
const char* e = std::getenv("STRATA_KV_PREFETCH");
diff --git a/sycl/src/program/generate.cpp b/sycl/src/program/generate.cpp
index 666a35c4c..9f1d37830 100644
--- a/sycl/src/program/generate.cpp
+++ b/sycl/src/program/generate.cpp
@@ -2606,8 +2606,10 @@ int main(int argc, char **argv) try {
"(multi-token for the i-quant gate/up rows)\n",
!strata::kernels::cpu::cpu_avx2_ok() ? "ggml-cpu vec_dot (no AVX2: the older-CPU build)"
: std::getenv("STRATA_NO_IQ256") == nullptr ? "AVX-2" : "ggml-cpu vec_dot (STRATA_NO_IQ256 set)");
- strata::core::ModelGeometry g; // canonical defaults; the model file overrides the MoE shape below
- int64_t K = 10;
+ // the compiled shape; a native model's file replaces it below with its own metadata (read_geometry), which a
+ // pruned variant (GSQ-RCO Coder, fewer than 512 experts) differs from
+ strata::core::ModelGeometry g;
+ int64_t K = g.n_expert_used;
// THE ROPE CONFIG RESOLVES HERE, BEFORE ANY WEIGHT MOVES - the CLI and the model file have both spoken,
// and `session_init` below builds the rope table from it and captures the kernels reading its constants
// (rope_scaling.hpp); the only hard constraint is "set before that", and dying on a bad rope key beats
@@ -2620,19 +2622,24 @@ int main(int argc, char **argv) try {
std::string gguf_rope_type;
double gguf_rope_base = 0, gguf_rope_factor = 0, gguf_rope_orig_ctx = 0;
if (!o.native_preset.empty()) {
- // a pruned variant (GSQ-RCO Coder) ships fewer experts than the canonical 512x10; the model file
- // is the authority on its own MoE shape - everything else in the geometry is unchanged
+ // the model file is the authority on its own geometry; the kernels' compiled shape is checked against
+ // it before any weight moves, naming the key that differs
try {
strata::GgufFile model_gguf(o.native_shards.front()); // the metadata shard
- if (const strata::MetaValue* v = model_gguf.get("qwen4exp.expert_count")) g.n_expert = (int64_t) v->u;
- if (const strata::MetaValue* v = model_gguf.get("qwen4exp.expert_used_count")) K = (int64_t) v->u;
+ std::string gerr = strata::read_geometry(model_gguf, g);
+ if (gerr.empty()) gerr = strata::geometry_unsupported(g);
+ if (!gerr.empty()) {
+ std::fprintf(stderr, "strata generate: %s: %s\n", o.native_shards.front().c_str(), gerr.c_str());
+ return 1;
+ }
+ K = g.n_expert_used;
if (const strata::MetaValue* v = model_gguf.get("qwen4exp.rope.freq_base")) gguf_rope_base = v->num();
if (const strata::MetaValue* v = model_gguf.get("qwen4exp.rope.scaling.type")) gguf_rope_type = v->s;
if (const strata::MetaValue* v = model_gguf.get("qwen4exp.rope.scaling.factor")) gguf_rope_factor = v->num();
if (const strata::MetaValue* v = model_gguf.get("qwen4exp.rope.scaling.original_context_length"))
gguf_rope_orig_ctx = v->num();
} catch (const std::exception& e) {
- std::fprintf(stderr, "strata generate: reading the model's expert shape from %s: %s\n",
+ std::fprintf(stderr, "strata generate: reading the model's geometry from %s: %s\n",
o.native_preset.c_str(), e.what());
return 1;
}
@@ -2717,10 +2724,9 @@ int main(int argc, char **argv) try {
"and --prefill CHUNK\n", o.pack.c_str());
return 2;
}
- const strata::core::ModelGeometry g0;
const auto embed_t0 = std::chrono::steady_clock::now();
- if (!native_embed.load(o.embd_gguf.empty() ? o.native_shards : std::vector<std::string>{o.embd_gguf}, g0.n_embd,
- 248320, err)) {
+ if (!native_embed.load(o.embd_gguf.empty() ? o.native_shards : std::vector<std::string>{o.embd_gguf}, g.n_embd,
+ g.n_vocab, err)) {
std::fprintf(stderr, "strata generate: %s\n", err.c_str());
return 1;
}
diff --git a/tests/core/gguf_fixture.hpp b/tests/core/gguf_fixture.hpp
index 677a5cd85..30c67bbfc 100644
--- a/tests/core/gguf_fixture.hpp
+++ b/tests/core/gguf_fixture.hpp
@@ -1,6 +1,6 @@
// tests/core/gguf_fixture.hpp - synthetic GGUF v3 files for the loader tests (no gguf-py, no model, no GPU).
//
-// A file is a header, metadata (strings and integers only), a tensor directory whose offsets are 32-byte aligned
+// A file is a header, metadata (strings, integers, f32, and arrays of u32 or strings), a tensor directory whose offsets are 32-byte aligned
// in the data section, and the tensors' payloads, each filled with a pattern the test can recognise.
#pragma once
@@ -18,14 +18,26 @@ namespace fixture {
struct Kv {
std::string key;
- uint32_t type = 8; ///< GGUF value type: 8 string, 2 u16, 4 u32, 5 i32
+ uint32_t type = 8; ///< GGUF value type: 8 string, 2 u16, 4 u32, 5 i32, 6 f32, 9 array
std::string s;
uint64_t u = 0;
+ float f = 0;
+ uint32_t elem = 4; ///< arrays: 4 u32 (`arr`) or 8 string (`sarr`)
+ std::vector<uint64_t> arr;
+ std::vector<std::string> sarr;
};
inline Kv str(const std::string& k, const std::string& v) { return Kv{k, 8, v, 0}; }
inline Kv u16(const std::string& k, uint64_t v) { return Kv{k, 2, {}, v}; }
inline Kv i32(const std::string& k, uint64_t v) { return Kv{k, 5, {}, v}; }
inline Kv u32(const std::string& k, uint64_t v) { return Kv{k, 4, {}, v}; }
+inline Kv f32(const std::string& k, float v) { Kv r{k, 6}; r.f = v; return r; }
+inline Kv u32s(const std::string& k, std::vector<uint64_t> v) { Kv r{k, 9}; r.arr = std::move(v); return r; }
+inline Kv strs(const std::string& k, std::vector<std::string> v) {
+ Kv r{k, 9};
+ r.elem = 8;
+ r.sarr = std::move(v);
+ return r;
+}
struct Tensor {
std::string name;
@@ -65,7 +77,16 @@ inline Written write(const std::filesystem::path& path, const std::vector<Kv>& k
if (k.type == 8) puts(k.s);
else if (k.type == 2) { const uint16_t v = (uint16_t) k.u; put(&v, 2); }
else if (k.type == 4 || k.type == 5) { const uint32_t v = (uint32_t) k.u; put(&v, 4); }
- else throw std::runtime_error("fixture: unsupported metadata type");
+ else if (k.type == 6) put(&k.f, 4);
+ else if (k.type == 9 && k.elem == 4) {
+ put32(4);
+ put64(k.arr.size());
+ for (uint64_t x : k.arr) put32((uint32_t) x);
+ } else if (k.type == 9 && k.elem == 8) {
+ put32(8);
+ put64(k.sarr.size());
+ for (const auto& x : k.sarr) puts(x);
+ } else throw std::runtime_error("fixture: unsupported metadata type");
}
Written w;
std::vector<uint64_t> bytes;
diff --git a/tests/core/gguf_geometry_test.cpp b/tests/core/gguf_geometry_test.cpp
new file mode 100644
index 000000000..564a46a74
--- /dev/null
+++ b/tests/core/gguf_geometry_test.cpp
@@ -0,0 +1,258 @@
+// tests/core/gguf_geometry_test.cpp - the model's geometry from its GGUF metadata (strata::read_geometry) and the
+// check against the shape the kernels are compiled for (strata::geometry_unsupported, check_architecture), on
+// synthetic GGUFs: no model, no GPU.
+//
+// The full key set is the one Unsloth's Qwen3.8-Flash-Next UD-IQ4_XS carries (read from its first shard). Then
+// the cases: a file with only the six keys the guard has always required, a pruned expert count, keys that
+// describe one thing twice and disagree, and a shape the kernels are not compiled for.
+//
+// `gguf_geometry_test --real SHARD1`: the geometry a real model's metadata shard declares, and the verdict.
+#include "gguf_fixture.hpp"
+
+#include <chrono>
+#include <cstdio>
+#include <filesystem>
+#include <functional>
+#include <string>
+#include <vector>
+
+namespace fs = std::filesystem;
+using strata::core::ModelGeometry;
+
+namespace {
+int g_fail = 0;
+void check(bool ok, const std::string& what) {
+ std::printf(" %-90s %s\n", what.c_str(), ok ? "ok" : "FAIL");
+ if (!ok) ++g_fail;
+}
+
+struct TempDir {
+ fs::path path;
+ TempDir() {
+ path = fs::temp_directory_path() /
+ ("strata-gguf-geometry-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);
+ }
+};
+
+std::vector<uint64_t> ratios(int64_t layers, int64_t interval, uint64_t block) {
+ std::vector<uint64_t> r((size_t) layers, 0);
+ for (int64_t l = 0; l < layers; ++l)
+ if (l % interval == interval - 1) r[(size_t) l] = block;
+ return r;
+}
+
+/// Qwen3.8-Flash-Next's metadata as its GGUF carries it (the PLE and rope-section keys the reader does not use
+/// are left out), with a vocabulary of `vocab` tokens.
+std::vector<fixture::Kv> full_keys(size_t vocab = 1000) {
+ using namespace fixture;
+ const std::string p = "qwen4exp.";
+ std::vector<std::string> tokens(vocab);
+ for (size_t i = 0; i < vocab; ++i) tokens[i] = "t" + std::to_string(i);
+ return {str("general.architecture", "qwen4exp"),
+ u32(p + "block_count", 48),
+ u32(p + "context_length", 262144),
+ u32(p + "embedding_length", 2560),
+ u32(p + "attention.head_count", 24),
+ u32(p + "attention.head_count_kv", 2),
+ f32(p + "rope.freq_base", 1e7f),
+ f32(p + "attention.layer_norm_rms_epsilon", 1e-6f),
+ u32(p + "expert_count", 512),
+ u32(p + "expert_used_count", 10),
+ u32(p + "attention.key_length", 256),
+ u32(p + "attention.value_length", 256),
+ u32(p + "expert_feed_forward_length", 640),
+ u32(p + "expert_shared_feed_forward_length", 640),
+ u32(p + "ssm.conv_kernel", 4),
+ u32(p + "ssm.state_size", 128),
+ u32(p + "ssm.group_count", 16),
+ u32(p + "ssm.time_step_rank", 48),
+ u32(p + "ssm.inner_size", 6144),
+ u32(p + "full_attention_interval", 4),
+ u32(p + "rope.dimension_count", 64),
+ u32(p + "hyper_connection.count", 4),
+ u32(p + "hyper_connection.low_rank", 320),
+ u32(p + "attention.indexer.head_count", 4),
+ u32(p + "attention.indexer.key_length", 128),
+ u32(p + "attention.indexer.top_k", 2048),
+ u32s(p + "attention.compress_ratios", ratios(48, 4, 4)),
+ str("tokenizer.ggml.model", "gpt2"),
+ strs("tokenizer.ggml.tokens", tokens)};
+}
+
+/// `kv` with `key` replaced by `v` (or removed, when `v` is null).
+std::vector<fixture::Kv> with(std::vector<fixture::Kv> kv, const std::string& key, const fixture::Kv* v) {
+ for (auto it = kv.begin(); it != kv.end(); ++it)
+ if (it->key == key) {
+ if (v) *it = *v;
+ else kv.erase(it);
+ return kv;
+ }
+ if (v) kv.push_back(*v);
+ return kv;
+}
+
+struct Result {
+ std::string read_err, unsupported, guard;
+ ModelGeometry g;
+};
+Result run(const fs::path& dir, const std::vector<fixture::Kv>& kv) {
+ static int n = 0;
+ const fs::path p = dir / ("m" + std::to_string(n++) + ".gguf");
+ fixture::write(p, kv, {});
+ Result r;
+ const strata::GgufFile f(p.string());
+ r.read_err = strata::read_geometry(f, r.g);
+ if (r.read_err.empty()) r.unsupported = strata::geometry_unsupported(r.g);
+ r.guard = strata::check_architecture(f);
+ return r;
+}
+bool has(const std::string& s, const char* part) { return s.find(part) != std::string::npos; }
+
+bool same_shape(const ModelGeometry& a, const ModelGeometry& b) {
+ return a.n_embd == b.n_embd && a.n_layers == b.n_layers && a.qsa_interval == b.qsa_interval &&
+ a.n_vocab == b.n_vocab && a.ssm_state_size == b.ssm_state_size && a.ssm_k_heads == b.ssm_k_heads &&
+ a.ssm_v_heads == b.ssm_v_heads && a.ssm_d_conv == b.ssm_d_conv &&
+ a.ssm_conv_channels == b.ssm_conv_channels && a.ssm_value_dim == b.ssm_value_dim && a.n_head == b.n_head &&
+ a.n_head_kv == b.n_head_kv && a.head_dim == b.head_dim && a.n_rot == b.n_rot &&
+ a.idx_q_heads == b.idx_q_heads && a.idx_key_dim == b.idx_key_dim && a.idx_block == b.idx_block &&
+ a.idx_top_k == b.idx_top_k && a.hc == b.hc && a.hc_lr == b.hc_lr && a.n_expert == b.n_expert &&
+ a.n_expert_used == b.n_expert_used && a.n_ff == b.n_ff;
+}
+
+int real_model(const std::string& shard) {
+ try {
+ const strata::GgufFile f(shard);
+ ModelGeometry g;
+ const std::string err = strata::read_geometry(f, g);
+ if (!err.empty()) {
+ std::printf(" refused: %s\n", err.c_str());
+ return 1;
+ }
+ std::printf(" n_embd %lld, layers %lld (attention every %lld), vocab %lld, rms_eps %g\n", (long long) g.n_embd,
+ (long long) g.n_layers, (long long) g.qsa_interval, (long long) g.n_vocab, g.rms_eps);
+ std::printf(" GDN: state %lld, k heads %lld, v heads %lld, conv %lld (%lld channels, values %lld)\n",
+ (long long) g.ssm_state_size, (long long) g.ssm_k_heads, (long long) g.ssm_v_heads,
+ (long long) g.ssm_d_conv, (long long) g.ssm_conv_channels, (long long) g.ssm_value_dim);
+ std::printf(" QSA: heads %lld/%lld x %lld, rope %lld, indexer %lld x %lld, block %lld, top-k %lld\n",
+ (long long) g.n_head, (long long) g.n_head_kv, (long long) g.head_dim, (long long) g.n_rot,
+ (long long) g.idx_q_heads, (long long) g.idx_key_dim, (long long) g.idx_block,
+ (long long) g.idx_top_k);
+ std::printf(" HC %lld (rank %lld); MoE %lld experts, %lld per token, ff %lld\n", (long long) g.hc,
+ (long long) g.hc_lr, (long long) g.n_expert, (long long) g.n_expert_used, (long long) g.n_ff);
+ const std::string why = strata::geometry_unsupported(g);
+ std::printf(" the compiled kernels: %s\n", why.empty() ? "can run it" : why.c_str());
+ return why.empty() ? 0 : 1;
+ } catch (const std::exception& e) {
+ std::printf(" refused: %s\n", e.what());
+ return 1;
+ }
+}
+} // namespace
+
+int main(int argc, char** argv) {
+ if (argc == 3 && std::string(argv[1]) == "--real") return real_model(argv[2]);
+ std::printf("gguf_geometry_test\n");
+ TempDir d;
+ const std::string p = "qwen4exp.";
+ {
+ const Result r = run(d.path, full_keys());
+ ModelGeometry want = strata::core::kCompiledGeometry;
+ want.n_vocab = 1000;
+ check(r.read_err.empty(), "the full key set reads" + (r.read_err.empty() ? "" : ": " + r.read_err));
+ check(same_shape(r.g, want), " and gives the compiled shape (vocabulary from tokenizer.ggml.tokens)");
+ check(r.g.ssm_conv_channels == 10240, " conv channels derived: 2 x 128 x 16 + 6144 = 10240");
+ check(r.unsupported.empty() && r.guard.empty(), " which the kernels run, and the guard passes");
+ }
+ {
+ // the six keys tests/core/native_dense_ple_key_test.cpp writes: everything else is the compiled value
+ const std::vector<fixture::Kv> six = {
+ fixture::str("general.architecture", "qwen4exp"), fixture::u32(p + "block_count", 48),
+ fixture::u32(p + "embedding_length", 2560), fixture::u32(p + "expert_count", 512),
+ fixture::u32(p + "expert_used_count", 10), fixture::u32(p + "attention.head_count", 24),
+ fixture::u32(p + "attention.head_count_kv", 2)};
+ const Result r = run(d.path, six);
+ check(r.read_err.empty() && same_shape(r.g, strata::core::kCompiledGeometry) && r.guard.empty(),
+ "only the six required keys: the rest keeps the compiled values");
+ for (const char* key : {"block_count", "embedding_length", "expert_count", "expert_used_count",
+ "attention.head_count", "attention.head_count_kv"}) {
+ const Result m = run(d.path, with(six, p + key, nullptr));
+ check(has(m.read_err, "missing") && has(m.read_err, key) && m.guard == m.read_err,
+ std::string("a file without ") + key + " is refused, naming it");
+ }
+ }
+ {
+ // GSQ-RCO Coder: fewer experts, read from the file and run by the same kernels
+ const fixture::Kv e = fixture::u32(p + "expert_count", 384), k = fixture::u32(p + "expert_used_count", 8);
+ const Result r = run(d.path, with(with(full_keys(), e.key, &e), k.key, &k));
+ check(r.read_err.empty() && r.g.n_expert == 384 && r.g.n_expert_used == 8 && r.guard.empty(),
+ "a pruned variant: 384 experts, 8 per token, read and accepted");
+ const fixture::Kv k65 = fixture::u32(p + "expert_used_count", 65);
+ check(has(run(d.path, with(full_keys(), k65.key, &k65)).read_err, "expert_used_count = 65"),
+ "65 experts per token is refused (the router takes 1..64)");
+ const fixture::Kv k0 = fixture::u32(p + "expert_used_count", 0);
+ check(has(run(d.path, with(full_keys(), k0.key, &k0)).read_err, "must be positive"),
+ "0 experts per token is refused");
+ }
+ {
+ // keys that describe one thing twice must agree
+ const fixture::Kv vl = fixture::u32(p + "attention.value_length", 128);
+ check(has(run(d.path, with(full_keys(), vl.key, &vl)).read_err, "attention.value_length = 128"),
+ "a value head width other than the key head width is refused");
+ const fixture::Kv sh = fixture::u32(p + "expert_shared_feed_forward_length", 1280);
+ check(has(run(d.path, with(full_keys(), sh.key, &sh)).read_err, "expert_shared_feed_forward_length = 1280"),
+ "a shared expert wider than the routed ones is refused");
+ const fixture::Kv in = fixture::u32(p + "ssm.inner_size", 4096);
+ check(has(run(d.path, with(full_keys(), in.key, &in)).read_err, "square heads"),
+ "ssm.inner_size other than state_size x time_step_rank is refused");
+ std::vector<uint64_t> shifted = ratios(48, 4, 4);
+ shifted[3] = 0;
+ shifted[2] = 4;
+ const fixture::Kv cr = fixture::u32s(p + "attention.compress_ratios", shifted);
+ check(has(run(d.path, with(full_keys(), cr.key, &cr)).read_err, "compress_ratios[2] = 4"),
+ "compress_ratios that disagree with full_attention_interval are refused at the first layer");
+ const fixture::Kv short_cr = fixture::u32s(p + "attention.compress_ratios", ratios(40, 4, 4));
+ check(has(run(d.path, with(full_keys(), short_cr.key, &short_cr)).read_err, "one entry for each of the 48"),
+ "compress_ratios of another length are refused");
+ std::vector<uint64_t> mixed = ratios(48, 4, 4);
+ mixed[7] = 8;
+ const fixture::Kv mx = fixture::u32s(p + "attention.compress_ratios", mixed);
+ check(has(run(d.path, with(full_keys(), mx.key, &mx)).read_err, "compress_ratios[7] = 8"),
+ "two pooling blocks in one model are refused");
+ const fixture::Kv fl = fixture::f32(p + "block_count", 48.0f);
+ check(has(run(d.path, with(full_keys(), fl.key, &fl)).read_err, "is not an integer"),
+ "a float where a count belongs is refused");
+ }
+ {
+ // a consistent file of another shape reads, and the kernels refuse it naming the key
+ const fixture::Kv bc = fixture::u32(p + "block_count", 40);
+ const fixture::Kv cr = fixture::u32s(p + "attention.compress_ratios", ratios(40, 4, 4));
+ const Result r = run(d.path, with(with(full_keys(), bc.key, &bc), cr.key, &cr));
+ check(r.read_err.empty() && r.g.n_layers == 40 && r.g.n_qsa_layers() == 10, "40 layers read: 10 QSA, 30 GDN");
+ check(r.unsupported == "qwen4exp.block_count = 40, the kernels are compiled for 48" && r.guard == r.unsupported,
+ " and refused: " + r.unsupported);
+ const fixture::Kv hd = fixture::u32(p + "attention.key_length", 128), vd = fixture::u32(p + "attention.value_length", 128);
+ const Result h = run(d.path, with(with(full_keys(), hd.key, &hd), vd.key, &vd));
+ check(h.read_err.empty() && h.g.head_dim == 128 && has(h.guard, "attention.key_length = 128"),
+ "head width 128 reads and is refused: " + h.guard);
+ const fixture::Kv tk = fixture::u32(p + "attention.indexer.top_k", 1024);
+ check(has(run(d.path, with(full_keys(), tk.key, &tk)).guard, "attention.indexer.top_k = 1024"),
+ "an indexer budget of 1024 is refused");
+ const fixture::Kv eps = fixture::f32(p + "attention.layer_norm_rms_epsilon", 1e-5f);
+ check(has(run(d.path, with(full_keys(), eps.key, &eps)).guard, "layer_norm_rms_epsilon = 1e-05"),
+ "an rms epsilon of 1e-5 is refused");
+ const fixture::Kv vocab = fixture::u32(p + "embedding_length", 2560);
+ check(run(d.path, with(full_keys(151936), vocab.key, &vocab)).guard.empty(),
+ "another vocabulary size is accepted (the head and embedding take their tensors' shapes)");
+ const fixture::Kv arch = fixture::str("general.architecture", "glm5next");
+ check(run(d.path, with(full_keys(), arch.key, &arch)).guard ==
+ "architecture is 'glm5next', this engine requires 'qwen4exp'",
+ "another architecture is refused by name");
+ }
+ std::printf(g_fail ? "gguf_geometry_test: %d FAILED\n" : "gguf_geometry_test: all passed\n", g_fail);
+ return g_fail ? 1 : 0;
+}