From 963679b35e4fc7f692a6f59a64ec8d6db211f3bb Mon Sep 17 00:00:00 2001 From: Jakub Luwierski Date: Mon, 28 Sep 2026 20:56:15 +0200 Subject: [PATCH] engine: --rope-scaling/--rope-scale/--yarn-* flags; rope keys as GGUF defaults Nine flags, llama.cpp's names: --rope-scaling none|linear|yarn, --rope-scale, --rope-freq-base, --rope-freq-scale, --yarn-orig-ctx, --yarn-ext-factor, --yarn-attn-factor, --yarn-beta-fast, --yarn-beta-slow. Ranges die in the validation block at second zero; the config itself resolves after the model file has had its say and before session_init builds the table or captures a graph (rope_scaling.hpp's one-writer rule). Precedence: CLI over the model file's qwen4exp.rope.* keys over the struct defaults - today's artifact ships no rope keys, so that channel is a no-op defaults path for future fine-tunes. A scaled run prints its resolved summary (type, factor, freq_scale, base, mscale, context vs trained) to stderr, which is what the bench logs quote, and warns when scaling is on with the context still inside the trained range - YaRN's magnitude correction applies everywhere, so that run is not the stock model. --- src/program/generate.cpp | 111 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 60700870..cb0f9d50 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -129,6 +129,19 @@ struct Options { bool native_flash_attn_short = false; // diagnostic pinned attention, context <=256 bool native_qsa_indexer = false; // pinned F16 key cache and F32 pooling bool native_qsa = false; // pinned F32 QSA norms and gate + /// THE CONTEXT EXTENSION (rope scaling, rope_scaling.hpp). These knobs resolve to ONE process config, + /// set once before `session_init` builds the rope table and the graphs capture the kernels. There is + /// deliberately no per-request form: K sits in the cache POST-RoPE, so one cache must never mix two + /// scalings. Precedence: CLI over the model file's rope keys over the struct defaults. + std::string rope_scaling = "none"; ///< --rope-scaling none|linear|yarn (llama.cpp's names) + double rope_scale = 1.0; ///< --rope-scale F: the extension factor; 1 = off + double rope_freq_base = 0; ///< --rope-freq-base N: 0 = the model's (1e7) + double rope_freq_scale = 0; ///< --rope-freq-scale F: the raw ggml knob; 0 = 1/--rope-scale + double yarn_orig_ctx = 0; ///< --yarn-orig-ctx N: 0 = the model's, else 262144 + double yarn_ext_factor = -1.0; ///< --yarn-ext-factor F: <0 = auto (1 for yarn, 0 otherwise) + double yarn_attn_factor = 1.0; ///< --yarn-attn-factor F + double yarn_beta_fast = 32.0; ///< --yarn-beta-fast F + double yarn_beta_slow = 1.0; ///< --yarn-beta-slow F bool native_rope = false; // pinned text-only CUDA rotary arithmetic bool native_ple_postops = false; // pinned PLE postprojection arithmetic bool native_router = false; // pinned fused 512-expert top-10 router @@ -360,6 +373,15 @@ void usage() { " --expert-cache-cpu-order experimental GPU expert reduction matching CPU order\n" " --max-new N tokens to generate (default 16)\n" " --max-context N KV/state capacity (default 4096)\n" + " --rope-scaling T extend the context past the trained one: none (default), linear\n" + " (position interpolation) or yarn - llama.cpp's types and names.\n" + " Fixed at startup: K in the cache is post-RoPE, so one run one scaling\n" + " --rope-scale F the extension factor for linear/yarn (default 1 = off)\n" + " --rope-freq-base N the raw ggml knobs: the frequency base (0 = the model's 1e7) and\n" + " --rope-freq-scale F the angle shrink (0 = 1/--rope-scale)\n" + " --yarn-orig-ctx N the trained context the correction targets (0 = 262144)\n" + " --yarn-ext-factor F --yarn-attn-factor F --yarn-beta-fast F --yarn-beta-slow F\n" + " YaRN's knobs; defaults: -1 (auto: 1 for yarn), 1, 32, 1\n" " --greedy argmax (the default)\n" " --seed S enable sampling with this Philox seed\n" " --top-k N --top-p F --temperature F\n" @@ -930,6 +952,15 @@ int main(int argc, char** argv) { } else if (a == "--max-new") o.max_new = std::atoll(next("--max-new")); else if (a == "--max-context") o.max_context = std::atoll(next("--max-context")); + else if (a == "--rope-scaling") o.rope_scaling = next("--rope-scaling"); + else if (a == "--rope-scale") o.rope_scale = std::atof(next("--rope-scale")); + else if (a == "--rope-freq-base") o.rope_freq_base = std::atof(next("--rope-freq-base")); + else if (a == "--rope-freq-scale") o.rope_freq_scale = std::atof(next("--rope-freq-scale")); + else if (a == "--yarn-orig-ctx") o.yarn_orig_ctx = std::atof(next("--yarn-orig-ctx")); + else if (a == "--yarn-ext-factor") o.yarn_ext_factor = std::atof(next("--yarn-ext-factor")); + else if (a == "--yarn-attn-factor") o.yarn_attn_factor = std::atof(next("--yarn-attn-factor")); + else if (a == "--yarn-beta-fast") o.yarn_beta_fast = std::atof(next("--yarn-beta-fast")); + else if (a == "--yarn-beta-slow") o.yarn_beta_slow = std::atof(next("--yarn-beta-slow")); else if (a == "--greedy") o.greedy = true; else if (a == "--seed") { o.seed = (uint64_t) std::atoll(next("--seed")); o.greedy = false; } else if (a == "--top-k") o.top_k = std::atoi(next("--top-k")); @@ -1279,6 +1310,36 @@ int main(int argc, char** argv) { std::fprintf(stderr, "strata generate: positive --max-new and --max-context must fit the prompt and generation\n"); return 2; } + // THE ROPE KNOBS (rope_scaling.hpp). Anything invalid dies here, at second zero, rather than becoming a + // NaN angle inside one of the twelve QSA layers. Only the RANGES are checked - the config itself is + // resolved after the model file has had its say, right before session_init. + strata::kernels::RopeScaling rope_cfg; // type filled here; the rest at the resolution below + { + using RST = strata::kernels::RopeScalingType; + if (o.rope_scaling == "none") rope_cfg.type = RST::None; + else if (o.rope_scaling == "linear") rope_cfg.type = RST::Linear; + else if (o.rope_scaling == "yarn") rope_cfg.type = RST::YaRN; + else { + std::fprintf(stderr, "strata generate: --rope-scaling must be none, linear or yarn (got '%s')\n", + o.rope_scaling.c_str()); + return 2; + } + if (o.rope_scale < 1.0) { + std::fprintf(stderr, "strata generate: --rope-scale %g must be >= 1 (it extends the context, not shrinks it)\n", + o.rope_scale); + return 2; + } + if (o.rope_freq_base != 0 && o.rope_freq_base <= 1.0) { + std::fprintf(stderr, "strata generate: --rope-freq-base must be a base above 1 (0 = the model's)\n"); + return 2; + } + if (o.rope_freq_scale < 0 || o.yarn_orig_ctx < 0 || o.yarn_ext_factor < -1.0 || o.yarn_attn_factor <= 0 || + o.yarn_beta_fast <= 0 || o.yarn_beta_slow <= 0) { + std::fprintf(stderr, "strata generate: invalid rope scaling knob (see usage: --yarn-ext-factor <0 = auto, " + "--yarn-orig-ctx 0 = default, the rest positive)\n"); + return 2; + } + } if (!std::isfinite(o.temperature) || o.temperature < 0 || !std::isfinite(o.top_p) || o.top_p <= 0 || o.top_p > 1 || o.top_k < 0 || o.expert_cache < -1 || o.pool_workers < 0 || std::any_of(o.expert_cache_remote.begin(), o.expert_cache_remote.end(), @@ -1477,6 +1538,10 @@ int main(int argc, char** argv) { strata::kernels::ple_set_native_postops(o.native_ple_postops); strata::core::ModelGeometry g; // canonical defaults; the model file overrides the MoE shape below int64_t K = 10; + // The model file's rope keys (llama.cpp's names under the arch prefix), when it carries any - the + // artifact today ships none, so this is a no-op defaults channel for future fine-tunes. + 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 @@ -1484,12 +1549,58 @@ int main(int argc, char** argv) { strata::GgufFile model_gguf(o.native_preset); 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; + 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", o.native_preset.c_str(), e.what()); return 1; } } + // THE ROPE CONFIG RESOLVES HERE - 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). + // Precedence: CLI over model file over the struct defaults. An explicit `--rope-scaling none` and the + // untouched default are the same choice: the CLI opt-out wins over the model file too. + { + using RST = strata::kernels::RopeScalingType; + if (rope_cfg.type == RST::None && !gguf_rope_type.empty()) { + if (gguf_rope_type == "linear") rope_cfg.type = RST::Linear; + else if (gguf_rope_type == "yarn") rope_cfg.type = RST::YaRN; + else if (gguf_rope_type != "none") { + std::fprintf(stderr, "strata generate: %s carries rope.scaling.type '%s' - none, linear or yarn only\n", + o.native_preset.c_str(), gguf_rope_type.c_str()); + return 2; + } + } + rope_cfg.factor = (o.rope_scale == 1.0 && gguf_rope_factor > 1.0) ? gguf_rope_factor : o.rope_scale; + if (o.rope_freq_base > 0) rope_cfg.freq_base = o.rope_freq_base; + else if (gguf_rope_base > 1.0) rope_cfg.freq_base = gguf_rope_base; + if (o.yarn_orig_ctx > 0) rope_cfg.orig_ctx = o.yarn_orig_ctx; + else if (gguf_rope_orig_ctx >= 1) rope_cfg.orig_ctx = gguf_rope_orig_ctx; + rope_cfg.freq_scale_in = o.rope_freq_scale; + rope_cfg.ext_factor = o.yarn_ext_factor >= 0 ? o.yarn_ext_factor + : (rope_cfg.type == RST::YaRN ? 1.0 : 0.0); + rope_cfg.attn_factor = o.yarn_attn_factor; + rope_cfg.beta_fast = o.yarn_beta_fast; + rope_cfg.beta_slow = o.yarn_beta_slow; + strata::kernels::rope_scaling_set(rope_cfg); + if (rope_cfg.type != RST::None) { + const char* tn = rope_cfg.type == RST::YaRN ? "yarn" : "linear"; + std::fprintf(stderr, + "strata generate: rope scaling %s, factor %.6g (freq_scale %.6g, base %.6g, mscale %.6f), " + "--max-context %lld against a trained context of %.0f\n", + tn, rope_cfg.factor, rope_cfg.freq_scale(), rope_cfg.freq_base, rope_cfg.mscale(), + (long long) o.max_context, rope_cfg.orig_ctx); + if ((double) o.max_context <= rope_cfg.orig_ctx) + std::fprintf(stderr, + "strata generate: note: the context is within the trained %.0f - the angles barely move, " + "but YaRN's magnitude correction applies everywhere\n", + rope_cfg.orig_ctx); + } + } // before session_init: every graph captured from here on has the vector's kernels where it applies std::string cvec_summary = "0"; if (!o.cvec_files.empty()) {