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

Merge f-qwen36 into f-144-rel (geometry: #2179's read_geometry guard kept for qwen4exp, Qwen3.6's family guard on top)

Niko12214cfeaa0f4fb8
diff --cc include/strata/artifact/gguf_reader.hpp
index 8f46837f1,3b960c1a3..7ce5c60cb
--- a/include/strata/artifact/gguf_reader.hpp
+++ b/include/strata/artifact/gguf_reader.hpp
@@@ -34,7 -33,7 +34,8 @@@
#include <algorithm>
#include "strata/artifact/gguf_split.hpp"
+#include "strata/core/geometry.hpp"
+ #include "strata/artifact/model_family.hpp"
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
@@@ -212,10 -211,10 +213,14 @@@ inline bool block_geometry(uint32_t t,
elems = 1;
bytes = 1;
return true;
+ case 40: // NVFP4: 64 values = 4 UE4M3 sub-block scales + 32 bytes of E2M1 codes
+ elems = 64;
+ bytes = 36;
+ return true;
+ case 39: // MXFP4: one E8M0 scale + 16 bytes of 4-bit values (Qwen3.6's MXFP4_MOE experts)
+ elems = 32;
+ bytes = 17;
+ return true;
case 42:
elems = 64;
bytes = 18;
@@@ -583,176 -582,81 +588,223 @@@ private
std::map<std::string, std::pair<size_t, const TensorInfo*>> index_;
};
-// ---- architecture guard (P1.S2). The engine is specialised to known model families (model_family.hpp);
-// anything else must be refused with a precise error rather than silently mis-run. `check_architecture` accepts
-// every family Strata knows (qwen4exp, qwen35moe); `check_engine_architecture` below also wants one this engine
-// runs.
-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 family of the model whose metadata shard is `g` (Unknown when it has no or another architecture).
+ inline ModelFamily model_family(const GgufFile& g) {
+ const MetaValue* arch = g.get("general.architecture");
+ return arch ? model_family_from_arch(arch->s) : ModelFamily::Unknown;
+ }
+
+ /// Qwen3.6-35B-A3B's guard: every key the geometry is read from must be present (core::geometry_from_gguf),
+ /// and the few that the layer pattern depends on must have the values the model has.
+ inline std::string check_architecture_qwen35moe(const GgufFile& g) {
+ static const char* const keys[] = {
+ "qwen35moe.block_count", "qwen35moe.embedding_length", "qwen35moe.attention.head_count",
+ "qwen35moe.attention.head_count_kv", "qwen35moe.attention.key_length", "qwen35moe.attention.value_length",
+ "qwen35moe.expert_count", "qwen35moe.expert_used_count", "qwen35moe.expert_feed_forward_length",
+ "qwen35moe.expert_shared_feed_forward_length", "qwen35moe.ssm.conv_kernel", "qwen35moe.ssm.state_size",
+ "qwen35moe.ssm.group_count", "qwen35moe.ssm.time_step_rank", "qwen35moe.ssm.inner_size",
+ "qwen35moe.full_attention_interval", "qwen35moe.nextn_predict_layers",
+ };
+ for (const char* k : keys) {
+ const MetaValue* v = g.get(k);
+ if (!v) return std::string("missing ") + k;
+ if (!v->is_num()) return std::string(k) + " is not a number";
+ }
+ const MetaValue* nl = g.get("qwen35moe.block_count");
+ const MetaValue* mtp = g.get("qwen35moe.nextn_predict_layers");
+ if (mtp->u >= nl->u) return "qwen35moe.nextn_predict_layers is not smaller than qwen35moe.block_count";
+ if (g.get("qwen35moe.full_attention_interval")->u < 2)
+ return "qwen35moe.full_attention_interval must be at least 2 (the GDN / attention pattern)";
+ return {};
+ }
+
-inline std::string check_architecture(const GgufFile& g, const Qwen4ExpGuard& want = {}) {
- const MetaValue* arch = g.get("general.architecture");
+// ---- 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.
+
+/// Fills `g` from the metadata shard `f`. Returns "" or the first problem. Another architecture is refused by
+/// name first: its keys carry another prefix, and "missing qwen4exp.block_count" would hide what is wrong. 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 MetaValue* arch = f.get("general.architecture");
if (!arch) return "missing general.architecture";
- const ModelFamily family = model_family_from_arch(arch->s);
- if (family == ModelFamily::Qwen35Moe) return check_architecture_qwen35moe(g);
- if (family != ModelFamily::Qwen4Exp)
- return "architecture is '" + arch->s + "', Strata reads 'qwen4exp' (Qwen3.8-Flash-Next) and 'qwen35moe' "
- "(Qwen3.6-35B-A3B)";
- struct Req {
+ if (arch->s != "qwen4exp") return "architecture is '" + arch->s + "', this engine requires 'qwen4exp'";
+ 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;
- uint64_t want;
+ int64_t core::ModelGeometry::*field;
+ bool required;
};
- 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 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 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 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();
}
- return {}; // empty == ok
+ 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;
+ int64_t core::ModelGeometry::*field;
+ };
+ 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 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 {};
+}
+
- /// The architecture guard: "" when the engine can run the model whose metadata shard is `f`.
++/// The architecture guard: "" when the engine can read the model whose metadata shard is `f`. Qwen3.8-Flash-Next
++/// (qwen4exp) is read and checked against the shape the kernels are compiled for; Qwen3.6-35B-A3B (qwen35moe) must
++/// carry every key its geometry is read from (core::geometry_from_gguf).
+inline std::string check_architecture(const GgufFile& f) {
++ const MetaValue* arch = f.get("general.architecture");
++ if (!arch) return "missing general.architecture";
++ const ModelFamily family = model_family_from_arch(arch->s);
++ if (family == ModelFamily::Qwen35Moe) return check_architecture_qwen35moe(f);
++ if (family != ModelFamily::Qwen4Exp)
++ return "architecture is '" + arch->s + "', Strata reads 'qwen4exp' (Qwen3.8-Flash-Next) and 'qwen35moe' "
++ "(Qwen3.6-35B-A3B)";
+ core::ModelGeometry g;
+ std::string err = read_geometry(f, g);
+ if (err.empty()) err = geometry_unsupported(g);
+ return err;
}
+ /// The engine's guard: `check_architecture`, then a family this engine has a path for. A recognised family
-/// it does not run yet (Qwen3.6) is refused with a message that says so.
-inline std::string check_engine_architecture(const GgufFile& g, const Qwen4ExpGuard& want = {}) {
- std::string err = check_architecture(g, want);
++/// it does not run yet (Qwen3.6 on SYCL) is refused with a message that says so.
++inline std::string check_engine_architecture(const GgufFile& g) {
++ std::string err = check_architecture(g);
+ if (!err.empty()) return err;
+ return model_family_not_supported(model_family(g));
+ }
+
} // namespace strata
diff --cc include/strata/core/family_layout.hpp
index 000000000,726e006a9..554350d37
mode 000000,100644..100644
--- a/include/strata/core/family_layout.hpp
+++ b/include/strata/core/family_layout.hpp
@@@ -1,0 -1,284 +1,285 @@@
+ // include/strata/core/family_layout.hpp - per model family: the geometry read from a GGUF's metadata, and the
+ // tensor layout table (every tensor of a layer, by name and GGUF shape) checked against a model file's directory.
+ //
+ // Header-only and GPU-free: the engine, the tools and the unit tests (tests/core/model_family_test.cpp) share it.
+ //
+ // * geometry_from_gguf: fills core::ModelGeometry from "<arch>.*" keys. For qwen4exp the keys a file does not
+ // carry keep Flash-Next's values (the struct defaults, which every published qwen4exp file matches); for
+ // qwen35moe every key is required (check_architecture_qwen35moe).
+ // * compiled_geometry_mismatch: the first field where a geometry differs from the shapes this engine's kernels
+ // are built for (the expert count and top-k are run-time values and are not compared).
+ // * layer_tensors / global_tensors: the layout tables. Names are GGUF names (blk.N.<suffix>), shapes are in
+ // GGUF order (dim 0 contiguous), as tools/gguf_reader.py prints them.
+ // * check_gguf_layout: every table entry present with its shape, inside its file, of a type with a known block
+ // size; and, for a family whose table is complete (qwen35moe), no blk.* tensor the table does not name.
+ #pragma once
+
+ #include "strata/artifact/gguf_reader.hpp"
+ #include "strata/artifact/model_family.hpp"
+ #include "strata/core/geometry.hpp"
+
+ #include <cstdint>
+ #include <cstdio>
+ #include <set>
+ #include <string>
+ #include <vector>
+
+ namespace strata::core {
+
+ /// Fills `g` (and `top_k`, the experts per token) from the metadata shard `meta`. False with `err` when the
+ /// architecture guard refuses the file or a key has an impossible value.
+ inline bool geometry_from_gguf(const strata::GgufFile& meta, ModelGeometry& g, int64_t& top_k, std::string& err) {
+ err = strata::check_architecture(meta);
+ if (!err.empty()) return false;
+ const strata::ModelFamily family = strata::model_family(meta);
+ const std::string p = std::string(strata::model_family_arch(family)) + ".";
+ g = ModelGeometry{};
+ g.family = family;
+ // a key the file carries replaces the default; `required` keys were checked present by the guard
+ auto num = [&](const char* key, int64_t& dst) {
+ if (const strata::MetaValue* v = meta.get(p + key))
+ if (v->is_num()) dst = (int64_t) v->u;
+ };
+ int64_t block_count = g.n_layers, key_length = g.head_dim, value_length = -1;
+ num("block_count", block_count);
+ num("embedding_length", g.n_embd);
+ num("attention.head_count", g.n_head);
+ num("attention.head_count_kv", g.n_head_kv);
+ num("attention.key_length", key_length);
+ num("attention.value_length", value_length);
+ num("expert_count", g.n_expert);
+ top_k = 10;
+ num("expert_used_count", top_k);
++ g.n_expert_used = top_k;
+ num("expert_feed_forward_length", g.n_ff);
+ num("expert_shared_feed_forward_length", g.n_ff_shexp);
+ num("ssm.conv_kernel", g.ssm_d_conv);
+ num("ssm.state_size", g.ssm_state_size);
+ num("ssm.group_count", g.ssm_k_heads);
+ num("ssm.time_step_rank", g.ssm_v_heads);
+ num("ssm.inner_size", g.ssm_value_dim);
+ num("full_attention_interval", g.qsa_interval);
+ g.head_dim = key_length;
+ if (family == strata::ModelFamily::Qwen35Moe) {
+ num("nextn_predict_layers", g.n_mtp_layers);
+ g.hc = g.hc_lr = 0; // a plain pre-norm residual
+ g.idx_q_heads = g.idx_key_dim = 0; // dense attention, no sparse indexer
+ } else {
+ num("hyper_connection.count", g.hc);
+ num("hyper_connection.low_rank", g.hc_lr);
+ num("attention.indexer.head_count", g.idx_q_heads);
+ num("attention.indexer.key_length", g.idx_key_dim);
+ }
+ g.n_layers = block_count - g.n_mtp_layers;
+ g.ssm_conv_channels = 2 * g.ssm_state_size * g.ssm_k_heads + g.ssm_value_dim;
+ if (const strata::MetaValue* v = meta.get("tokenizer.ggml.tokens"))
+ if (v->type == strata::MetaType::ARRAY && v->count > 0) g.n_vocab = (int64_t) v->count;
+
+ char buf[256];
+ auto bad = [&](const char* what) {
+ std::snprintf(buf, sizeof buf, "%s: %s", meta.path().c_str(), what);
+ err = buf;
+ return false;
+ };
+ if (g.n_layers <= 0 || g.n_embd <= 0 || g.n_head <= 0 || g.n_head_kv <= 0 || g.head_dim <= 0 || g.n_expert <= 0 ||
+ top_k <= 0 || top_k > g.n_expert || g.n_ff <= 0 || g.ssm_state_size <= 0 || g.ssm_k_heads <= 0 ||
+ g.ssm_v_heads <= 0 || g.ssm_d_conv <= 0 || g.qsa_interval < 2)
+ return bad("a geometry key is zero or out of range");
+ if (value_length >= 0 && value_length != key_length) return bad("attention.key_length != attention.value_length");
+ if (g.ssm_value_dim != g.ssm_state_size * g.ssm_v_heads) return bad("ssm.inner_size != ssm.state_size * ssm.time_step_rank");
+ if (g.ssm_v_heads % g.ssm_k_heads) return bad("ssm.time_step_rank is not a multiple of ssm.group_count");
+ if (g.n_head % g.n_head_kv) return bad("attention.head_count is not a multiple of attention.head_count_kv");
+ if (g.n_layers % g.qsa_interval) return bad("the layer count is not a multiple of full_attention_interval");
+ return true;
+ }
+
+ /// The first geometry field that differs from the shapes this engine's kernels are built for (ModelGeometry's
+ /// defaults, Flash-Next's), as "<what> is <got>, this engine's kernels are built for <want>"; empty when none.
+ /// n_expert and top-k are run-time sizes (the Coder ships 256 experts) and n_vocab is the embedding's business.
+ inline std::string compiled_geometry_mismatch(const ModelGeometry& g) {
+ const ModelGeometry c;
+ if (g.family != c.family)
+ return std::string("the model is ") + strata::model_family_title(g.family) + ", this engine's kernels are built "
+ "for " + strata::model_family_title(c.family);
+ struct F {
+ const char* what;
+ int64_t got, want;
+ };
+ const F fields[] = {
+ {"embedding_length", g.n_embd, c.n_embd},
+ {"the layer count", g.n_layers, c.n_layers},
+ {"full_attention_interval", g.qsa_interval, c.qsa_interval},
+ {"ssm.state_size", g.ssm_state_size, c.ssm_state_size},
+ {"ssm.group_count", g.ssm_k_heads, c.ssm_k_heads},
+ {"ssm.time_step_rank", g.ssm_v_heads, c.ssm_v_heads},
+ {"ssm.conv_kernel", g.ssm_d_conv, c.ssm_d_conv},
+ {"the GDN conv channels", g.ssm_conv_channels, c.ssm_conv_channels},
+ {"ssm.inner_size", g.ssm_value_dim, c.ssm_value_dim},
+ {"attention.head_count", g.n_head, c.n_head},
+ {"attention.head_count_kv", g.n_head_kv, c.n_head_kv},
+ {"attention.key_length", g.head_dim, c.head_dim},
+ {"attention.indexer.head_count", g.idx_q_heads, c.idx_q_heads},
+ {"attention.indexer.key_length", g.idx_key_dim, c.idx_key_dim},
+ {"hyper_connection.count", g.hc, c.hc},
+ {"hyper_connection.low_rank", g.hc_lr, c.hc_lr},
+ {"expert_feed_forward_length", g.n_ff, c.n_ff},
+ {"expert_shared_feed_forward_length", g.n_ff_shexp, c.n_ff_shexp},
+ {"the in-file MTP layers", g.n_mtp_layers, c.n_mtp_layers},
+ };
+ for (const F& f : fields)
+ if (f.got != f.want) {
+ char buf[256];
+ std::snprintf(buf, sizeof buf, "%s is %lld, this engine's kernels are built for %lld", f.what,
+ (long long) f.got, (long long) f.want);
+ return buf;
+ }
+ return {};
+ }
+
+ enum class LayerKind { Gdn, Attention, Mtp };
+
+ inline LayerKind layer_kind(const ModelGeometry& g, int64_t layer) {
+ if (layer >= g.n_layers) return LayerKind::Mtp;
+ return is_qsa_layer(g, layer) ? LayerKind::Attention : LayerKind::Gdn;
+ }
+
+ /// One tensor of the layout table: its name (a blk.N. suffix for a layer's, the full name for a global one) and
+ /// its GGUF shape.
+ struct TensorWant {
+ std::string name;
+ std::vector<int64_t> shape;
+ };
+
+ /// Every tensor of layer `layer` (kind from `layer_kind`), by family.
+ inline std::vector<TensorWant> layer_tensors(const ModelGeometry& g, int64_t layer) {
+ const LayerKind kind = layer_kind(g, layer);
+ const int64_t N = g.n_embd, NE = g.n_expert, FF = g.n_ff, FS = g.n_ff_shexp;
+ std::vector<TensorWant> t;
+ auto add = [&](const char* s, std::vector<int64_t> shape) { t.push_back({s, std::move(shape)}); };
+ // MoE, every layer (the MTP block's too)
+ add("ffn_gate_inp.weight", {N, NE});
+ add("ffn_gate_inp_shexp.weight", {N});
+ add("ffn_gate_shexp.weight", {N, FS});
+ add("ffn_up_shexp.weight", {N, FS});
+ add("ffn_down_shexp.weight", {FS, N});
+ add("ffn_gate_exps.weight", {N, FF, NE});
+ add("ffn_up_exps.weight", {N, FF, NE});
+ add("ffn_down_exps.weight", {FF, N, NE});
+ if (g.has_hyper_connections()) {
+ // gated residual (Flash-Next): `gr_read` transposes, so the file's orientation is [hc_dim, hc_lr] / [hc_lr, hc_dim]
+ const int64_t H = g.hc_dim();
+ add("hc_attn_down.weight", {H, g.hc_lr});
+ add("hc_attn_up.weight", {g.hc_lr, H});
+ add("hc_attn_inject.weight", {H, g.hc});
+ add("hc_attn_norm.weight", {H});
+ add("hc_ffn_down.weight", {H, g.hc_lr});
+ add("hc_ffn_up.weight", {g.hc_lr, H});
+ add("hc_ffn_inject.weight", {H, g.hc});
+ add("hc_ffn_norm.weight", {H});
+ } else {
+ // plain pre-norm residual (qwen35moe): x += mixer(rms(x)); x += moe(rms(x))
+ add("attn_norm.weight", {N});
+ add("post_attention_norm.weight", {N});
+ }
+ if (kind == LayerKind::Gdn) {
+ add("attn_qkv.weight", {N, g.ssm_conv_channels});
+ add("attn_gate.weight", {N, g.ssm_value_dim});
+ add("ssm_out.weight", {g.ssm_value_dim, N});
+ add("ssm_conv1d.weight", {g.ssm_d_conv, g.ssm_conv_channels});
+ add("ssm_alpha.weight", {N, g.ssm_v_heads});
+ add("ssm_beta.weight", {N, g.ssm_v_heads});
+ add("ssm_a", {g.ssm_v_heads});
+ add("ssm_dt.bias", {g.ssm_v_heads});
+ add("ssm_norm.weight", {g.ssm_state_size});
+ } else {
+ // attention (query + output gate in attn_q), also the MTP block's
+ add("attn_q.weight", {N, 2 * g.n_head * g.head_dim});
+ add("attn_k.weight", {N, g.n_head_kv * g.head_dim});
+ add("attn_v.weight", {N, g.n_head_kv * g.head_dim});
+ add("attn_output.weight", {g.n_head * g.head_dim, N});
+ add("attn_q_norm.weight", {g.head_dim});
+ add("attn_k_norm.weight", {g.head_dim});
+ if (g.has_indexer() && kind == LayerKind::Attention) {
+ // `q_proj` is the QUERY count, `k_proj` the KEY width (round 194: they are different numbers)
+ add("indexer.q_proj.weight", {N, g.idx_q_heads * g.idx_key_dim});
+ add("indexer.k_proj.weight", {N, g.idx_key_dim});
+ add("indexer.q_norm.weight", {g.idx_key_dim});
+ add("indexer.k_norm.weight", {g.idx_key_dim});
+ }
+ }
+ if (kind == LayerKind::Mtp) {
+ // the GGUF-native MTP block: eh_proj over concat(enorm(embedding), hnorm(hidden))
+ add("nextn.eh_proj.weight", {2 * N, N});
+ add("nextn.enorm.weight", {N});
+ add("nextn.hnorm.weight", {N});
+ add("nextn.shared_head_norm.weight", {N});
+ }
+ return t;
+ }
+
+ /// The tensors outside the layers, by family. (Flash-Next's output_norm, PLE and n-gram tables are not listed:
+ /// the published files place them differently, and their loaders check them.)
+ inline std::vector<TensorWant> global_tensors(const ModelGeometry& g) {
+ std::vector<TensorWant> t;
+ t.push_back({"token_embd.weight", {g.n_embd, g.n_vocab}});
+ t.push_back({"output.weight", {g.n_embd, g.n_vocab}});
+ if (g.has_hyper_connections()) {
+ t.push_back({"output_hc_down.weight", {g.hc_dim(), g.hc_lr}});
+ t.push_back({"output_hc_up.weight", {g.hc_lr, g.hc_dim()}});
+ t.push_back({"output_hc_norm.weight", {g.hc_dim()}});
+ } else {
+ t.push_back({"output_norm.weight", {g.n_embd}});
+ }
+ return t;
+ }
+
+ /// Whether the family's table names every blk.* tensor its files carry (then an unnamed one is an error).
+ inline bool layout_table_complete(const ModelGeometry& g) { return g.family == strata::ModelFamily::Qwen35Moe; }
+
+ /// Checks the model `m` (all shards) against the layout table of `g`. `checked` counts the tensors found.
+ inline bool check_gguf_layout(const strata::GgufModel& m, const ModelGeometry& g, std::string& err,
+ size_t* checked = nullptr) {
+ std::set<std::string> named;
+ size_t n = 0;
+ auto check = [&](const std::string& name, const std::vector<int64_t>& shape) {
+ named.insert(name);
+ size_t s = 0;
+ const strata::TensorInfo* t = m.find(name, &s);
+ if (!t) {
+ err = "missing " + name;
+ return false;
+ }
+ bool same = t->shape.size() == shape.size();
+ for (size_t i = 0; same && i < shape.size(); ++i) same = t->shape[i] == (uint64_t) shape[i];
+ if (!same) {
+ std::string got, want;
+ for (uint64_t d : t->shape) got += (got.empty() ? "" : ", ") + std::to_string(d);
+ for (int64_t d : shape) want += (want.empty() ? "" : ", ") + std::to_string(d);
+ err = name + " is [" + got + "], the " + strata::model_family_title(g.family) + " layout wants [" + want + "]";
+ return false;
+ }
+ if (!m.in_bounds(*t, s)) {
+ err = name + " (" + t->type_name() + ") has no known size inside " + m.shard(s).path();
+ return false;
+ }
+ ++n;
+ return true;
+ };
+ for (const TensorWant& w : global_tensors(g))
+ if (!check(w.name, w.shape)) return false;
+ for (int64_t l = 0; l < g.n_layers + g.n_mtp_layers; ++l)
+ for (const TensorWant& w : layer_tensors(g, l))
+ if (!check("blk." + std::to_string(l) + "." + w.name, w.shape)) return false;
+ if (layout_table_complete(g)) {
+ for (size_t s = 0; s < m.size(); ++s)
+ for (const strata::TensorInfo& t : m.shard(s).tensors())
+ if (!named.count(t.name)) {
+ err = t.name + " is not in the " + std::string(strata::model_family_title(g.family)) + " layout";
+ return false;
+ }
+ }
+ if (checked) *checked = n;
+ return true;
+ }
+
+ } // namespace strata::core
diff --cc include/strata/core/geometry.hpp
index d3916d6ed,5ffc3dd64..0a73ca4fa
--- a/include/strata/core/geometry.hpp
+++ b/include/strata/core/geometry.hpp
@@@ -1,68 -1,60 +1,77 @@@
-// include/strata/core/geometry.hpp - the model's geometry (moved out of layout.hpp so that tools and tests can use
-// it without the weight table). The defaults are Qwen3.8-Flash-Next's: they are the shapes this engine's kernels
-// are built for. core::geometry_from_gguf (family_layout.hpp) fills one from a model file's metadata.
+// 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 "strata/artifact/model_family.hpp"
+
#include <cstdint>
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.
+/// 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;
- int64_t n_layers = 48; ///< the trunk's layers (an in-file MTP block is counted in n_mtp_layers)
- int64_t qsa_interval = 4; ///< every 4th layer is full attention: layers 3, 7, ... 47
+ int64_t n_embd = 2560; ///< embedding_length
- int64_t n_layers = 48; ///< block_count
++ int64_t n_layers = 48; ///< block_count (the trunk; an in-file MTP block is counted in n_mtp_layers)
+ 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;
- 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
+ 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;
- int64_t n_head_kv = 2;
- int64_t head_dim = 256;
- int64_t idx_q_heads = 4; ///< 0: no sparse indexer (dense attention only: qwen35moe)
- int64_t idx_key_dim = 128;
+ 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; ///< 0: no hyper-connections (a plain pre-norm residual: qwen35moe)
- int64_t hc_lr = 320;
+ 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;
- int64_t n_ff = 640;
- int64_t n_ff_shexp = 640; ///< the shared expert's FFN width
+ 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)
- // appended for the model families (NEW-MODELS-PLAN.md T0); the defaults keep Flash-Next's values
++ // appended for the model families (Qwen3.6 = qwen35moe); the defaults keep Flash-Next's values
++ int64_t n_ff_shexp = 640; ///< the shared expert's FFN width (qwen4exp: equal to n_ff, read_geometry checks)
+ ModelFamily family = ModelFamily::Qwen4Exp;
- int64_t n_mtp_layers = 0; ///< MTP blocks inside the model file, after the trunk (qwen35moe: blk.40)
- int64_t n_vocab = 248320;
++ int64_t n_mtp_layers = 0; ///< MTP blocks inside the model file, after the trunk (qwen35moe: blk.40)
+
- int64_t hc_dim() const { return hc * n_embd; }
++ constexpr bool has_indexer() const { return idx_q_heads > 0; } ///< false: dense attention only
++ constexpr bool has_hyper_connections() const { return hc > 0; } ///< false: plain pre-norm residual
+ constexpr 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(); }
- bool has_indexer() const { return idx_q_heads > 0; }
- bool has_hyper_connections() const { return hc > 0; }
+ 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 this is the first of the two.
+/// = 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;
}
diff --cc setup.py
index 7d8cfe367,84c4b2dc4..5e0388f1f
--- a/setup.py
+++ b/setup.py
@@@ -413,15 -356,41 +414,48 @@@ FAMILIES =
"mmproj": "mmproj-Qwen3.8-Flash-Next-BF16.gguf", "name": "qwen3.8-flash-next-unsloth",
"vision": False, "pack_args": ["--compat-bf16"],
"sha256": {**UNSLOTH_SHARDS, **UNSLOTH_IQ4_XS_SHARDS, **UNSLOTH_BIG_SHARDS}},
+ # MODEL-UPDATE Phase 4: the original model with NVFP4 experts, as ready-made packs (NVFP4_REPOS): the files come
+ # as the engine reads them (no GGUF to pack), each size has its own repository ("repo" in MODELS)
+ "nvfp4": {"title": "Qwen3.8-Flash-Next (NVFP4)", "by": "NVFP4 packs by sergqwer (strata-nvfp4)",
+ "about": "4-bit NVFP4 experts, NVIDIA cards; a 128 GB download, ~80 GB of RAM to keep every expert in RAM",
+ "tag": "nvfp4-", "name": "qwen3.8-flash-next-nvfp4", "ready": True,
+ "mmproj_hf": hf("ISTA-DASLab/Qwen3.8-Flash-Next-GSQ-RCO-GGUF"), "mmproj": "mmproj-Qwen3.8-Flash-Next-BF16.gguf",
+ "mmproj_sha256": (907543008, "b1a82259702816a5330d7bd7607cd9676b11780e79ff7348c21103ff3ce49bd0")},
}
MMPROJ = "mmproj-Qwen3.8-Flash-Next-BF16.gguf"
+ # Qwen3.6-35B-A3B (GGUF architecture qwen35moe), Unsloth's GGUFs with the MTP block: a family with an engine path of its
+ # own (src/qwen35; `strata` switches to it from the file's architecture). No pack, no expert profile, no separate MTP
+ # draft layer: the engine reads the GGUF as it is, keeps every expert it can in VRAM and computes the rest on the CPU
+ # (the hybrid mode), and drafts with the file's own MTP block. Not in FAMILIES (whose entries are Flash-Next files with
+ # packs): OWN_PATH_FAMILIES, offered in the same menu and installed by install_own_path. NVIDIA (CUDA) and AMD (HIP)
+ # only: the Intel Arc (SYCL) engine refuses it.
+ QWEN36_FILES = {
+ "Qwen3.6-35B-A3B-UD-Q4_K_XL.gguf": (22853663008, "55983c5a75a1ab969824077b3bb3de4146e82a9234072b48ad4e8f92ad3fe9f1"),
+ "Qwen3.6-35B-A3B-UD-Q6_K_XL.gguf": (32611711264, "35fce994cd36104a7dc1bd8a4bdf13778145664c00fdef6773aebc9246e5019c"),
+ "Qwen3.6-35B-A3B-Q8_0.gguf": (37801097504, "c1283d8b80c3e38b2735ddbc9766d3b3126f44d6c484be419d4e101d09a76131"),
+ "mmproj-F16.gguf": (899283584, "71f3cbc1f7cc0f30d09d41cfa924c0060827ebc33bf15ace7e86661e856f0160"),
+ }
+ OWN_PATH_FAMILIES = {
+ "qwen36": {"title": "Qwen3.6-35B-A3B", "by": "Qwen; GGUFs with the MTP layer by Unsloth", "arch": "qwen35moe",
+ "about": "35B mixture of experts with 3B active (256 experts, top-8): a smaller cousin of "
+ "Qwen3.8-Flash-Next with the same tokenizer",
+ "hf": hf("unsloth/Qwen3.6-35B-A3B-MTP-GGUF"), "file": "Qwen3.6-35B-A3B-{q}.gguf", "tag": "qwen36-",
+ "mmproj_hf": hf("unsloth/Qwen3.6-35B-A3B-MTP-GGUF"), "mmproj": "mmproj-F16.gguf",
+ "name": "qwen3.6-35b-a3b", "license": "Apache 2.0: https://huggingface.co/Qwen/Qwen3.6-35B-A3B",
+ # RAM: the engine keeps a copy of every expert in RAM when they do not all fit in VRAM (the hybrid mode),
+ # so the file's size plus room for the OS and the rest (measured: UD-Q4_K_XL on a 46 GB PC with a 12 GB
+ # card, 18.75 GiB of experts pinned)
+ "models": {"UD-Q4_K_XL": {"about": "4-bit (Unsloth Dynamic), the smallest", "ram_gb": 32},
+ "UD-Q6_K_XL": {"about": "6-bit (Unsloth Dynamic), closer to the full model", "ram_gb": 44},
+ "Q8_0": {"about": "8-bit, the closest to the full model", "ram_gb": 52}},
+ "sha256": QWEN36_FILES,
+ # the engine's KV cache per context token: 10 attention layers + the MTP block, 2 KV heads x 256, K and
+ # V in fp32 (strata-q36)
+ "kv_bytes_per_token": 11 * 2 * 256 * 2 * 4,
+ "mtp": 2, # drafts per step (greedy requests): measured best on the R9700
+ "vision": False}, # the image encoder for this family is not wired up yet
+ }
+ PREVIEW_FAMILIES = OWN_PATH_FAMILIES # the earlier name (tools and tests that import it)
# EXPERIMENTAL, off by default (setup asks): a control vector shipped with the repository, see its README
ESP_VECTOR = ROOT / "data" / "experimental-speed-projection" / "Qwen3.8-Flash-Next-experimental-speed-projection.gguf"
# the image encoder on the GPU (~1.2 GB at 1024 image tokens) warms up before the engine starts, so the engine
@@@ -4511,10 -4404,10 +4562,11 @@@ def update_install(have: list, a) -> in
"numpy, jinja2, regex, pyyaml, tqdm, requests, cmake, ninja, pillow, psutil")
if not a.build:
update_installed_engine(a.prebuilt)
+ mtp_done: set = set()
for cfg_path in have:
cfg = upgrade_config(cfg_path, json.loads(cfg_path.read_text(encoding="utf-8-sig")))
- refresh_shared_draft_vocab(cfg, mtp_done)
- if "--mtp" in cfg["args"][:-1] and not own_path_config(cfg):
- refresh_draft_vocab(Path(cfg["args"][cfg["args"].index("--mtp") + 1]), cfg.get("draft_vocab", "cjk"))
++ if not own_path_config(cfg):
++ refresh_shared_draft_vocab(cfg, mtp_done)
if cfg.get("backend") == "hip" and WIN:
hip_runtime_beside_exe(Path(cfg["exe"]).parent) # #468 #461
ok(f"{cfg.get('model_name', cfg_path.stem)}: up to date")
@@@ -5491,16 -5598,15 +5779,22 @@@ def main() -> int
d = FAMILIES[f]
say(f" {i}) {d['title']:20s} {d['by']} - {d['about']}" + (" [experimental]" if d.get("experimental") else "")
+ (f" (recommended for Strix Halo: {STRIX_HALO_MODEL})" if f == rec_fam and rec_fam != fams[0] else ""))
- family = fams[int(ask("Which model?", [str(i) for i in range(1, len(fams) + 1)],
- str(fams.index(rec_fam) + 1), a.yes)) - 1]
+ for j, f in enumerate(OWN_PATH_FAMILIES, len(fams) + 1): # families with an engine path of their own
+ d = OWN_PATH_FAMILIES[f]
+ say(f" {j}) {d['title']:20s} {d['by']} - {d['about']} [new]")
+ allf = fams + list(OWN_PATH_FAMILIES)
+ family = allf[int(ask("Which model?", [str(i) for i in range(1, len(allf) + 1)],
+ str(allf.index(rec_fam) + 1), a.yes)) - 1]
+ if family in OWN_PATH_FAMILIES: # Qwen3.6: its own engine path, its own steps 2-7
+ return install_own_path(a, family, gpu, hip, amd, cuda_tk, multi, port, ram, adopted)
fam = FAMILIES[family]
+ if fam.get("ready") and hip:
+ # not a risk the user could take: the HIP engine has no NVFP4 expert kernels at all, so it would not start
+ fail(f"{fam['title']} runs on NVIDIA cards only: the AMD engine has no NVFP4 kernels",
+ "UD-IQ4_XS is about the same size and runs on AMD: --family unsloth --model UD-IQ4_XS")
+ if fam.get("ready") and a.gguf_dir:
+ warn(f"--gguf-dir does not apply to {fam['title']}: its files are ready-made packs, not GGUF shards")
+ a.gguf_dir = None
ok(f"model: {fam['title']}")
if fam.get("license"):
say(f" Its license: {fam['license']}")
@@@ -5789,39 -5877,7 +6083,14 @@@
# ---- 4. the engine
step(4, "the Strata engine")
- llama = get_llama_cpp()
- ok(f"llama.cpp {LLAMA_CPP_COMMIT[:7]} (gguf-py, ggml, mtmd)")
- if hip and WIN: # AMD on Windows: the ready-made HIP engine (no compiler)
- eng = None if a.build else get_prebuilt_hip(a.prebuilt, gpu)
- if eng is None:
- fail("no ready-made AMD engine for this Strata version" + (" (--build)" if a.build else ""),
- "compiling it on Windows: tools\\hip\\build_windows.bat makes strata-windows-x64-hip.zip, then run "
- "START-HERE.bat --backend hip --prebuilt <its dist folder> (docs/AMD_HIP.md)")
- gpu = hip_card(eng, gpu, amd)
- a.gpu = gpu["index"] if gpu["count"] > 1 else a.gpu
- else:
- eng = None if a.build or hip else get_prebuilt(a.prebuilt, gpu, vision, **({"toolkit": 12} if cuda_tk == 12
- else {}))
- if eng is not None and not hip and json.loads((eng / "BUILD.json").read_text(encoding="utf-8")).get("source") != "local":
- pip_cuda_libs(cuda_tk)
- if vision != "none" and not (eng / VEXE).exists():
- warn("the ready-made engine has no image encoder: compiling it")
- eng = None
- else:
- vision = prebuilt_vision(json.loads((eng / "BUILD.json").read_text(encoding="utf-8")), gpu, vision)
- if eng is None:
- eng = build_engine_hip(gpu, llama, vision) if hip else build_engine(gpu, vision, a.yes, llama, toolkit=cuda_tk)
- meta = json.loads((eng / "BUILD.json").read_text(encoding="utf-8"))
+ llama, eng, meta, lib_dirs, gpu, vision = engine_step(a, gpu, hip, amd, cuda_tk, vision)
+ if ready and meta.get("source") != "local" and \
+ tuple(int(x) for x in str(meta.get("version", "0")).split(".")[:4] if x.isdigit()) < MODELS[model]["engine"]:
+ # a ready-made engine from before NVFP4 cannot read these experts: one compiled from this source can
+ warn(f"the ready-made engine {meta.get('version')} has no NVFP4 experts yet: compiling the engine from this source")
+ eng = build_engine(gpu, vision, a.yes, llama, toolkit=cuda_tk)
+ meta = json.loads((eng / "BUILD.json").read_text(encoding="utf-8"))
- if hip and WIN: # the ready-made engine's rocm/bin, first on the engine's PATH
- lib_dirs = [str(d) for d in hip_lib_dirs(eng)]
- else:
+ lib_dirs = meta.get("lib_dirs") or meta.get("cuda_dirs") or cuda_lib_dirs(cuda_tk)
engine_ver = tuple(int(x) for x in str(meta.get("version", "0")).split(".")[:4] if x.isdigit())
need_engine = MODELS[model].get("engine", UNSLOTH_ENGINE)
# checked before the 94-188 GB download
diff --cc src/kernels/cuda/iq_kernels.cu
index d54a980c9,ebed142b1..947952a04
--- a/src/kernels/cuda/iq_kernels.cu
+++ b/src/kernels/cuda/iq_kernels.cu
@@@ -641,14 -608,17 +641,19 @@@ template<> struct Fmt<8> { static const
// The formats of each role, one list each so a type cannot be in one switch and missing from another. Every
// entry is a kernel template for each CUDA architecture of the build, hence two lists rather than one.
#ifdef STRATA_Q6K_EXPERTS // opt-in build (-DSTRATA_Q6K_EXPERTS=ON): one more instance per kernel, loaded at start
-#define STRATA_GU_FMTS(X) X(16) X(17) X(18) X(21) X(22) X(23) X(29) X(42) X(12) X(13) X(14) X(6) X(2) X(3) X(8)
+#define STRATA_GU_FMTS(X) X(16) X(17) X(18) X(21) X(22) X(23) X(29) X(42) X(12) X(13) X(14) X(6) X(2) X(3) X(8) X(40)
#else
-#define STRATA_GU_FMTS(X) X(16) X(17) X(18) X(21) X(22) X(23) X(29) X(42) X(12) X(13) X(6) X(2) X(3) X(8)
+#define STRATA_GU_FMTS(X) X(16) X(17) X(18) X(21) X(22) X(23) X(29) X(42) X(12) X(13) X(6) X(2) X(3) X(8) X(40)
#endif
- #define STRATA_D_FMTS(X) X(20) X(23) X(42) X(7) X(6) X(2) X(3) X(8) X(40)
+ // Q5_K / Q6_K downs: Qwen3.6-35B-A3B (FF 512, whole 256-blocks per row); Flash-Next's FF 640 never selects them
+ #ifdef STRATA_Q6K_EXPERTS
-#define STRATA_D_FMTS(X) X(20) X(23) X(42) X(7) X(6) X(2) X(3) X(8) X(13) X(14)
++#define STRATA_D_FMTS(X) X(20) X(23) X(42) X(7) X(6) X(2) X(3) X(8) X(13) X(14) X(40)
+ #else
-#define STRATA_D_FMTS(X) X(20) X(23) X(42) X(7) X(6) X(2) X(3) X(8) X(13)
++#define STRATA_D_FMTS(X) X(20) X(23) X(42) X(7) X(6) X(2) X(3) X(8) X(13) X(40)
+ #endif
-#define STRATA_MMVQ_FMTS(X) X(16) X(17) X(18) X(20) X(21) X(22) X(23) X(29) X(42) X(12) X(13) X(7) X(6) X(2) X(3) X(8)
+#define STRATA_MMVQ_FMTS(X) X(16) X(17) X(18) X(20) X(21) X(22) X(23) X(29) X(42) X(12) X(13) X(7) X(6) X(2) X(3) X(8) X(40)
+template<> struct Fmt<40> { static constexpr int qk = 64, ipb = 2, step = 4; // NVFP4: 2 calls of 32 values
+ __device__ static float dot(const void* v, const block_q8_1* y, int kbx, int iqs) { return vec_dot_nvfp4_q8_1(v, y, kbx, iqs); } };
__device__ __forceinline__ float warp_sum(float v) {
#pragma unroll
diff --cc src/program/generate.cpp
index 16a03e1b3,5ed10cf0d..9cd64fc0a
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@@ -2865,24 -2894,29 +2905,29 @@@ 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()) {
- // 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
+ // The geometry is the model file's (core::geometry_from_gguf: every "<arch>.*" shape key), checked
+ // against the shapes this engine's kernels are built for. The expert count and top-k are run-time
+ // sizes: a pruned variant (GSQ-RCO Coder) ships fewer experts than the canonical 512x10.
try {
strata::GgufFile model_gguf(o.native_shards.front()); // the metadata shard
- 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());
+ std::string gerr;
+ if (!strata::core::geometry_from_gguf(model_gguf, g, K, gerr)) {
+ std::fprintf(stderr, "strata generate: %s\n", gerr.c_str());
+ return 1;
+ }
+ const std::string mismatch = strata::core::compiled_geometry_mismatch(g);
+ if (!mismatch.empty()) {
+ std::fprintf(stderr, "strata generate: %s: %s\n", o.native_preset.c_str(), mismatch.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"))
+ const std::string p = std::string(strata::model_family_arch(g.family)) + ".";
+ if (const strata::MetaValue* v = model_gguf.get(p + "rope.freq_base")) gguf_rope_base = v->num();
+ if (const strata::MetaValue* v = model_gguf.get(p + "rope.scaling.type")) gguf_rope_type = v->s;
+ if (const strata::MetaValue* v = model_gguf.get(p + "rope.scaling.factor")) gguf_rope_factor = v->num();
+ if (const strata::MetaValue* v = model_gguf.get(p + "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;
}
diff --cc sycl/src/program/generate.cpp
index 29a07e66f,04dfafbf3..8f29ed18c
--- a/sycl/src/program/generate.cpp
+++ b/sycl/src/program/generate.cpp
@@@ -2637,24 -2630,29 +2648,29 @@@ 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()) {
- // 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
+ // The geometry is the model file's (core::geometry_from_gguf: every "<arch>.*" shape key), checked
+ // against the shapes this engine's kernels are built for. The expert count and top-k are run-time
+ // sizes: a pruned variant (GSQ-RCO Coder) ships fewer experts than the canonical 512x10.
try {
strata::GgufFile model_gguf(o.native_shards.front()); // the metadata shard
- 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());
+ std::string gerr;
+ if (!strata::core::geometry_from_gguf(model_gguf, g, K, gerr)) {
+ std::fprintf(stderr, "strata generate: %s\n", gerr.c_str());
+ return 1;
+ }
+ const std::string mismatch = strata::core::compiled_geometry_mismatch(g);
+ if (!mismatch.empty()) {
+ std::fprintf(stderr, "strata generate: %s: %s\n", o.native_preset.c_str(), mismatch.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"))
+ const std::string p = std::string(strata::model_family_arch(g.family)) + ".";
+ if (const strata::MetaValue* v = model_gguf.get(p + "rope.freq_base")) gguf_rope_base = v->num();
+ if (const strata::MetaValue* v = model_gguf.get(p + "rope.scaling.type")) gguf_rope_type = v->s;
+ if (const strata::MetaValue* v = model_gguf.get(p + "rope.scaling.factor")) gguf_rope_factor = v->num();
+ if (const strata::MetaValue* v = model_gguf.get(p + "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;
}
diff --cc tests/core/gguf_fixture.hpp
index d3415c5ec,4fcfd94fa..c20fda352
--- a/tests/core/gguf_fixture.hpp
+++ b/tests/core/gguf_fixture.hpp
@@@ -18,33 -18,16 +18,34 @@@ namespace fixture
struct Kv {
std::string key;
- uint32_t type = 8; ///< GGUF value type: 8 string, 2 u16, 4 u32, 5 i32, 9 array of strings
+ 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;
- std::vector<std::string> items; ///< type 9
+ 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 str_array(const std::string& k, std::vector<std::string> v) { return Kv{k, 9, {}, 0, std::move(v)}; }
+// fields set by name: a braced Kv{...} that stops short warns under -Wmissing-field-initializers (clang -Wextra)
+inline Kv make_kv(const std::string& k, uint32_t type) {
+ Kv r;
+ r.key = k;
+ r.type = type;
+ return r;
+}
+inline Kv str(const std::string& k, const std::string& v) { Kv r = make_kv(k, 8); r.s = v; return r; }
+inline Kv u16(const std::string& k, uint64_t v) { Kv r = make_kv(k, 2); r.u = v; return r; }
+inline Kv i32(const std::string& k, uint64_t v) { Kv r = make_kv(k, 5); r.u = v; return r; }
+inline Kv u32(const std::string& k, uint64_t v) { Kv r = make_kv(k, 4); r.u = v; return r; }
+inline Kv f32(const std::string& k, float v) { Kv r = make_kv(k, 6); r.f = v; return r; }
+inline Kv u32s(const std::string& k, std::vector<uint64_t> v) { Kv r = make_kv(k, 9); r.arr = std::move(v); return r; }
+inline Kv strs(const std::string& k, std::vector<std::string> v) {
+ Kv r = make_kv(k, 9);
+ r.elem = 8;
+ r.sarr = std::move(v);
+ return r;
+}
++inline Kv str_array(const std::string& k, std::vector<std::string> v) { return strs(k, std::move(v)); }
struct Tensor {
std::string name;