diff --git a/engine/include/strata/artifact/gguf_reader.hpp b/engine/include/strata/artifact/gguf_reader.hpp index 23dc0c31d6..3626577a59 100644 --- a/engine/include/strata/artifact/gguf_reader.hpp +++ b/engine/include/strata/artifact/gguf_reader.hpp @@ -911,18 +911,23 @@ inline std::string read_geometry(const GgufFile& f, core::ModelGeometry& g) { // An in-file MTP tail is not a trunk layer. `qwen4exp` files may carry nextn_predict_layers (Qwen4-35B-A3B: // block_count 41 = 40 trunk + blk.40), as qwen35moe already does; the kernels and the layer split are the // trunk's. Read it here so block_count, compress_ratios and the compiled shape are compared on the trunk. - int64_t n_mtp = 0; - if (const MetaValue* v = f.get(p + "nextn_predict_layers")) { - if (!v->is_num() || v->type == MetaType::F32 || v->type == MetaType::F64 || v->type == MetaType::BOOL) - return p + "nextn_predict_layers is not an integer"; - n_mtp = (int64_t) v->u; - if (n_mtp < 0) return p + "nextn_predict_layers = " + std::to_string(n_mtp) + ", it cannot be negative"; + // Only a build whose compiled geometry has an in-file MTP block (q4_lite) reads it; the other shapes keep the + // code they had (no key read, no trunk subtraction), so their binaries do not change. + constexpr bool kMtpTail = core::kCompiledGeometry.n_mtp_layers > 0; + if constexpr (kMtpTail) { + int64_t n_mtp = 0; + if (const MetaValue* v = f.get(p + "nextn_predict_layers")) { + if (!v->is_num() || v->type == MetaType::F32 || v->type == MetaType::F64 || v->type == MetaType::BOOL) + return p + "nextn_predict_layers is not an integer"; + n_mtp = (int64_t) v->u; + if (n_mtp < 0) return p + "nextn_predict_layers = " + std::to_string(n_mtp) + ", it cannot be negative"; + } + if (n_mtp >= g.n_layers) + return p + "nextn_predict_layers = " + std::to_string(n_mtp) + " leaves no trunk in block_count = " + + std::to_string(g.n_layers); + g.n_mtp_layers = n_mtp; + g.n_layers -= n_mtp; } - if (n_mtp >= g.n_layers) - return p + "nextn_predict_layers = " + std::to_string(n_mtp) + " leaves no trunk in block_count = " + - std::to_string(g.n_layers); - g.n_mtp_layers = n_mtp; - g.n_layers -= n_mtp; int64_t value_length = g.head_dim, ff_shexp = g.n_ff; if (!read_int("attention.value_length", value_length, false) || @@ -963,11 +968,12 @@ inline std::string read_geometry(const GgufFile& f, core::ModelGeometry& g) { if (const MetaValue* v = f.get(p + "attention.compress_ratios")) { // One entry per trunk layer; a file with an in-file MTP tail may list the tail too (Qwen4-35B-A3B lists 41 // for 40 trunk layers). Only the trunk entries are checked - the tail is not a trunk layer. - const uint64_t listed = (uint64_t) g.n_layers + (uint64_t) g.n_mtp_layers; + const uint64_t listed = (uint64_t) g.n_layers + (uint64_t) g.n_mtp_layers; // = n_layers when no tail if (v->type != MetaType::ARRAY || (v->count != (uint64_t) g.n_layers && v->count != listed) || 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 (or the " + std::to_string(listed) + " blocks with an in-file MTP tail)"; + (kMtpTail ? " layers (or the " + std::to_string(listed) + " blocks with an in-file MTP tail)" + : std::string(" layers")); // A list of zeros only is a converter's placeholder, not a layer split (#2396: orcarouter's // Qwen3.8-Flash-Next-Uncensored GGUFs carry 48 zeros; 0.1.43 never read the key): it is treated as absent // and the compiled pooling block is used. A list with any non-zero entry is checked in full below. diff --git a/engine/include/strata/core/family_layout.hpp b/engine/include/strata/core/family_layout.hpp index d5cf7e20e7..319dcedd19 100644 --- a/engine/include/strata/core/family_layout.hpp +++ b/engine/include/strata/core/family_layout.hpp @@ -62,8 +62,10 @@ inline bool geometry_from_gguf(const strata::GgufFile& meta, ModelGeometry& g, i g.head_dim = key_length; // An in-file MTP block is not a Qwen3.6-only thing: Qwen4-35B-A3B (`qwen4exp`, released 2026-12-03) carries // its single MTP layer as blk., announced by the same key. Flash-Next files do not carry the - // key, so n_mtp_layers stays 0 there. - num("nextn_predict_layers", g.n_mtp_layers); + // key, so n_mtp_layers stays 0 there. Only a build whose compiled shape has an in-file MTP block (q4_lite) reads + // it for qwen4exp; every other shape keeps the code it had. + if (family == strata::ModelFamily::Qwen35Moe || kCompiledGeometry.n_mtp_layers > 0) + num("nextn_predict_layers", g.n_mtp_layers); if (family == strata::ModelFamily::Qwen35Moe) { // #6522: Ollama/llama.cpp GGUFs may encode attention.head_count_kv (and head_count) as a per-layer array // (e.g. [0,0,0,2] x 10); resolve via the shared helper so we never fall back to entry 0 or defaults. @@ -135,8 +137,6 @@ inline std::string compiled_geometry_mismatch(const ModelGeometry& g) { {"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}, - // n_mtp_layers is file layout, not a kernel shape: a trunk view (no in-file MTP block) and the published - // file (blk.40) are the same 40-layer model and both run on a q4_lite build. }; for (const F& f : fields) if (f.got != f.want) { @@ -145,6 +145,17 @@ inline std::string compiled_geometry_mismatch(const ModelGeometry& g) { (long long) f.got, (long long) f.want); return buf; } + // n_mtp_layers is file layout, not a kernel shape, where the compiled shape has an in-file MTP block (q4_lite): a + // trunk view (no MTP block) and the published file (blk.40) are the same 40-layer model and both run there. + // Every other shape still compares it, as before. + if constexpr (kCompiledGeometry.n_mtp_layers == 0) { + if (g.n_mtp_layers != c.n_mtp_layers) { + char buf[256]; + std::snprintf(buf, sizeof buf, "%s is %lld, this engine's kernels are built for %lld", "the in-file MTP layers", + (long long) g.n_mtp_layers, (long long) c.n_mtp_layers); + return buf; + } + } return {}; } diff --git a/engine/src/kernels/cuda/qsa_pattn.cu b/engine/src/kernels/cuda/qsa_pattn.cu index 4af270e078..bf50c93cfd 100644 --- a/engine/src/kernels/cuda/qsa_pattn.cu +++ b/engine/src/kernels/cuda/qsa_pattn.cu @@ -318,8 +318,16 @@ __global__ void __launch_bounds__(64) ps_value_d4_kernel(QsaAttnPools p, const i const float v0 = (float) (int8_t) (codes & 0xFF) * sc, v1 = (float) (int8_t) ((codes >> 8) & 0xFF) * sc; const float v2 = (float) (int8_t) ((codes >> 16) & 0xFF) * sc, v3 = (float) (int8_t) (codes >> 24) * sc; float ww[G]; + if constexpr (G == 12) { // Flash-Next: the three float4 loads this kernel has always had + const float4* w4 = reinterpret_cast(spt[cl]); + const float4 w0 = w4[0], w1 = w4[1], w2 = w4[2]; + const float t12[12] = {w0.x, w0.y, w0.z, w0.w, w1.x, w1.y, w1.z, w1.w, w2.x, w2.y, w2.z, w2.w}; #pragma unroll - for (int h = 0; h < G; ++h) ww[h] = spt[cl][h]; + for (int h = 0; h < 12; ++h) ww[h] = t12[h]; + } else { // other widths (q4_lite: G = 8): plain loads, any G +#pragma unroll + for (int h = 0; h < G; ++h) ww[h] = spt[cl][h]; + } #pragma unroll for (int h = 0; h < G; ++h) { a[0][h] = fmaf(ww[h], v0, a[0][h]);