serve: --rope-scaling writes static YaRN over a container's own rotary keys

yarn:FACTOR[:ORIG] (or none) is written at load into the rotary object yarn_from takes first --
for a Qwen3.8 container text_config.rope_parameters, which already says default, so YaRN written
beside it would never be read -- and resolved and checked exactly as a converted container's is.
An existing container serves past its trained context with no reconvert; a DFlash2 drafter gets
the same factor over its own trained length; a checkpoint's own YaRN has its factor replaced and
its stale attention_factor/mscale dropped; linear or other explicit types are refused. Startup
names the rotary's reach and warns when --max-model-len is past it, and a persisted prefix cache
written under another --rope-scaling is not reused. core/format/rope_override.h is held to
yarn_from in tests/yarn_test.cpp.
This commit is contained in:
baKon authored and Deadcode committed 2026-10-07 11:13:46 +02:00
1 parent ee5fcf1024
commit 6fa7707dff
9 files changed
+457 -1

No files matched your search

+15
View File
@@ -6,6 +6,7 @@
* (spec §6).
*/
#include "rad_core.h"
#include "format/rope_override.h"
#include "sample/host_ref.h"
#include "server/oai.h"
@@ -45,6 +46,8 @@ static const Flag g_flags[] = {
{ "--max-num-batched-tokens", "N", "the largest token count one step may carry" },
{ "--max-num-seqs", "N", "the largest number of sequences one step may carry" },
{ "--max-model-len", "N", "context bound; 0 takes the model's training context" },
{ "--rope-scaling", "SPEC", "yarn:FACTOR[:ORIG]: static YaRN over the container's own rotary\n"
"keys, no reconvert (docs/YARN.md); none: off" },
{ "--gpu-headroom-mib", "N", "VRAM left unclaimed on every card (default 96)" },
{ "--vram-weights-mib", "N", "the resident weight slab; 0 derives it from the card" },
@@ -380,6 +383,18 @@ int config_parse(int argc, char** argv, Config* c) {
else if (S("--max-num-batched-tokens")){ if (!(v = need(i))) return RAD_E_INVAL; c->max_tok = atoll(v); }
else if (S("--max-num-seqs")) { if (!(v = need(i))) return RAD_E_INVAL; c->max_seqs = atoll(v); }
else if (S("--max-model-len")) { if (!(v = need(i))) return RAD_E_INVAL; c->max_ctx = atoll(v); }
else if (S("--rope-scaling")) {
if (!(v = need(i))) return RAD_E_INVAL;
/* Checked here, so a typo is a refusal at the command line and not a model served
* with its trained rotary at a length it cannot reach. */
RopeSpec rs;
if (!rope_spec_parse(v, &rs) || rs.kind == RopeSpec::Keep) {
RAD_ERR("--rope-scaling takes yarn:FACTOR (above 1) with an optional :ORIG trained "
"length, or none -- got '%s'", v);
return RAD_E_INVAL;
}
c->rope_scaling = v;
}
else if (S("--vram-weights-mib")) { if (!(v = need(i))) return RAD_E_INVAL; c->vram_weights_mib = atoll(v); }
else if (S("--vram-kv-mib")) { if (!(v = need(i))) return RAD_E_INVAL; c->vram_kv_mib = atoll(v); }
/* --gpu-headroom takes the short name too: it is the flag an operator reaches for and
+3
View File
@@ -124,6 +124,9 @@ private:
int load_plugins();
int read_metadata();
int read_checkpoint_metadata();
/* --rope-scaling over the free-form keys, then the startup line and the context checks;
* runs on meta_kv_k_/meta_kv_v_ before their pointer arrays are built. */
int apply_rope_scaling();
int declare();
int plan();
/* --embedding-placement and --ngram-placement, resolved into Config before the card is split
+61 -1
View File
@@ -14,6 +14,7 @@
#include "device/device.h"
#include "device/directio.h"
#include "format/radfile.h"
#include "format/rope_override.h"
#include "format/share.h"
#include "format/table_copy.h"
#include "rad_plugin.h"
@@ -707,6 +708,7 @@ int Engine::read_metadata() {
default: meta_kv_v_.push_back(file_->str(kv.v.s)); break;
}
}
RAD_TRY(apply_rope_scaling());
meta_kv_kp_.clear(); meta_kv_vp_.clear();
for (auto& s : meta_kv_k_) meta_kv_kp_.push_back(s.c_str());
for (auto& s : meta_kv_v_) meta_kv_vp_.push_back(s.c_str());
@@ -741,6 +743,7 @@ int Engine::read_checkpoint_metadata() {
}
meta_kv_k_ = mo.keys;
meta_kv_v_ = mo.vals;
RAD_TRY(apply_rope_scaling());
meta_kv_kp_.clear(); meta_kv_vp_.clear();
for (auto& k : meta_kv_k_) meta_kv_kp_.push_back(k.c_str());
for (auto& v : meta_kv_v_) meta_kv_vp_.push_back(v.c_str());
@@ -754,6 +757,59 @@ int Engine::read_checkpoint_metadata() {
return RAD_OK;
}
/* --rope-scaling (format/rope_override.h): YaRN written into the container's own rotary keys at
* load, where the architecture's reader takes them, so an existing container serves past its
* trained context with no reconvert -- and is checked by the same reader a converted one is. The
* drafter, when there is one, gets the same factor over its own trained length. Then one line says
* what the rotary is, and a context past what it covers is named. */
int Engine::apply_rope_scaling() {
RopeSpec spec;
if (!rope_spec_parse(cfg_.rope_scaling.c_str(), &spec)) {
RAD_ERR("--rope-scaling '%s': yarn:FACTOR (above 1) with an optional :ORIG, or none",
cfg_.rope_scaling.c_str());
return RAD_E_INVAL;
}
if (spec.kind != RopeSpec::Keep) {
const RopeResolved before = rope_resolve(meta_kv_k_, meta_kv_v_, "");
std::string why;
if (!rope_override_group(meta_kv_k_, meta_kv_v_, spec, "", meta_.n_ctx_train, &why)) {
RAD_ERR("--rope-scaling %s: %s", cfg_.rope_scaling.c_str(), why.c_str());
return RAD_E_INVAL;
}
RopeKeys kv{ meta_kv_k_, meta_kv_v_ };
if (kv.any_prefix("draft.")) {
int64_t dtrain = meta_.n_ctx_train;
if (const std::string* t = kv.get("draft.max_position_embeddings"))
dtrain = std::atoll(t->c_str());
if (!rope_override_group(meta_kv_k_, meta_kv_v_, spec, "draft.", dtrain, &why)) {
RAD_ERR("--rope-scaling %s: %s", cfg_.rope_scaling.c_str(), why.c_str());
return RAD_E_INVAL;
}
}
/* A converter that read a YaRN factor as the linear rope_scale left it there; with YaRN
* taken off it would stay behind as a position divisor. */
if (spec.kind == RopeSpec::None && before.type == "yarn") meta_.rope_scale = 1.0f;
}
const RopeResolved r = rope_resolve(meta_kv_k_, meta_kv_v_, "");
const long long want = cfg_.max_ctx > 0 ? (long long)cfg_.max_ctx : (long long)meta_.n_ctx_train;
if (r.type == "yarn" && r.factor > 1.0) {
const long long reach = (long long)(r.orig * r.factor);
RAD_INFO("rope: YaRN factor %g over %lld trained positions -- the rotary reaches %lld; "
"--max-model-len %lld%s", r.factor, (long long)r.orig, reach, want,
spec.kind == RopeSpec::Yarn ? " (--rope-scaling)" : " (the container's own)");
if (want > reach)
RAD_WARN("--max-model-len %lld is past the %lld positions YaRN factor %g covers; "
"positions beyond them are extrapolated", want, reach, r.factor);
} else if (meta_.n_ctx_train > 0 && want > meta_.n_ctx_train && !(meta_.rope_scale > 1.0f)) {
RAD_WARN("--max-model-len %lld is past the %lld positions the model was trained on and "
"nothing scales its rotary, so positions beyond them are extrapolated; a model "
"card's YaRN extension is --rope-scaling yarn:FACTOR (docs/YARN.md)", want,
(long long)meta_.n_ctx_train);
}
return RAD_OK;
}
/* ================================================================== 3 + 4. plugins, declare */
int Engine::load_plugins() {
int s = rad_tools_load_plugins(cfg_.radiance_home, cfg_.kernel_hierarchy,
@@ -2830,7 +2886,11 @@ int Engine::configure_kv() {
std::to_string(ranks_[0]->prefix.block_size()) + "|" +
std::to_string(slot_bytes) + "|" + std::to_string(ck_slot_bytes) + "|" +
cfg_.kv_cache_dtype + "|" +
std::to_string((long long)ranks_.size()) + "|" + RAD_VERSION;
std::to_string((long long)ranks_.size()) + "|" + RAD_VERSION +
/* --rope-scaling rotates every cached key differently. Appended only
* when given, so a store written without it keeps its fingerprint. */
(cfg_.rope_scaling.empty() ? std::string()
: "|rope=" + cfg_.rope_scaling);
uint64_t h = 1469598103934665603ull;
for (char c : id) { h ^= (unsigned char)c; h *= 1099511628211ull; }
sc.fingerprint = h;
+177
View File
@@ -0,0 +1,177 @@
/* --rope-scaling, over a container's free-form metadata.
*
* Static YaRN is read by the architecture (arch/common/rad_arch.h, yarn_from) out of the model's
* own keys: `text_config.rope_parameters.`, `rope_parameters.`, `text_config.rope_scaling.`,
* `rope_scaling.`, in that order, the FIRST of them that names a rotary type being the one that
* counts; a DFlash2 drafter reads `draft.rope_parameters.` / `draft.rope_scaling.` on its own. A
* container converted without YaRN therefore needs a reconvert to get it -- or these keys rewritten
* when it is loaded, which is all this does. The architecture then resolves the result exactly as
* it would a converted container, so there is one reader and one set of checks.
*
* The rewrite goes into the object the reader would take, not beside it. A Qwen3.8 container says
* `text_config.rope_parameters.rope_type = default`; a YaRN written under `rope_parameters.` would
* sit behind that and never be read.
*
* Pure functions over two parallel string vectors, so they are tested without an engine
* (tests/yarn_test.cpp holds them to yarn_from). */
#pragma once
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
namespace rad {
struct RopeSpec {
enum Kind { Keep, None, Yarn } kind = Keep;
double factor = 0.0;
int64_t orig = 0; /* 0: the container's own trained length */
};
/* "yarn:FACTOR[:ORIG]" with FACTOR > 1 and ORIG > 0, or "none"; empty is Keep. False for anything
* else, so a typo is refused where it was typed. */
inline bool rope_spec_parse(const char* s, RopeSpec* out) {
RopeSpec r;
if (!s || !*s) { *out = r; return true; }
if (!std::strcmp(s, "none")) { r.kind = RopeSpec::None; *out = r; return true; }
if (std::strncmp(s, "yarn:", 5)) return false;
char* e = nullptr;
r.factor = std::strtod(s + 5, &e);
if (e == s + 5 || !(r.factor > 1.0) || r.factor > 1e6) return false;
if (*e == ':') {
char* e2 = nullptr;
const long long o = std::strtoll(e + 1, &e2, 10);
if (e2 == e + 1 || *e2 || o <= 0) return false;
r.orig = o;
} else if (*e) {
return false;
}
r.kind = RopeSpec::Yarn;
*out = r;
return true;
}
struct RopeKeys {
std::vector<std::string>& k;
std::vector<std::string>& v;
int find(const std::string& key) const {
for (size_t i = 0; i < k.size(); ++i)
if (k[i] == key) return (int)i;
return -1;
}
const std::string* get(const std::string& key) const {
const int i = find(key);
return i < 0 ? nullptr : &v[(size_t)i];
}
void set(const std::string& key, const std::string& val) {
const int i = find(key);
if (i >= 0) { v[(size_t)i] = val; return; }
k.push_back(key);
v.push_back(val);
}
void erase(const std::string& key) {
const int i = find(key);
if (i < 0) return;
k.erase(k.begin() + i);
v.erase(v.begin() + i);
}
bool any_prefix(const char* pre) const {
const size_t n = std::strlen(pre);
for (const auto& s : k)
if (!s.compare(0, n, pre)) return true;
return false;
}
};
/* The reader's order -- yarn_from's, kept in step with it. */
inline std::vector<std::string> rope_homes(const char* group) {
if (!*group)
return { "text_config.rope_parameters.", "rope_parameters.", "text_config.rope_scaling.",
"rope_scaling." };
return { std::string(group) + "rope_parameters.", std::string(group) + "rope_scaling." };
}
/* The object the reader takes and the key that names its type, or empty strings when no object
* names one (the model's rotary is then the default). */
inline void rope_authoritative(const RopeKeys& kv, const char* group, std::string* home,
std::string* type_key) {
home->clear();
type_key->clear();
for (const auto& h : rope_homes(group))
for (const char* t : { "rope_type", "type" })
if (kv.get(h + t)) { *home = h; *type_key = h + t; return; }
}
struct RopeResolved {
std::string type; /* "" when nothing names one */
double factor = 1.0;
double orig = 0.0;
};
inline RopeResolved rope_resolve(std::vector<std::string>& k, std::vector<std::string>& v,
const char* group) {
RopeKeys kv{ k, v };
RopeResolved r;
std::string home, tk;
rope_authoritative(kv, group, &home, &tk);
if (home.empty()) return r;
r.type = *kv.get(tk);
if (const std::string* f = kv.get(home + "factor")) r.factor = std::strtod(f->c_str(), nullptr);
if (const std::string* o = kv.get(home + "original_max_position_embeddings"))
r.orig = std::strtod(o->c_str(), nullptr);
return r;
}
/* Apply SPEC to one rotary group ("" the target, "draft." a DFlash2 drafter). `trained` is the
* group's own trained length, the default ORIG. Returns false, with *why set, when the group's
* rotary is one this flag does not stack YaRN over (linear position interpolation, say). */
inline bool rope_override_group(std::vector<std::string>& k, std::vector<std::string>& v,
const RopeSpec& spec, const char* group, int64_t trained,
std::string* why) {
if (spec.kind == RopeSpec::Keep) return true;
RopeKeys kv{ k, v };
std::string home, tk;
rope_authoritative(kv, group, &home, &tk);
const std::string type = home.empty() ? std::string() : *kv.get(tk);
if (spec.kind == RopeSpec::None) {
if (type == "yarn") kv.set(tk, "default");
return true;
}
if (!type.empty() && type != "default" && type != "yarn") {
*why = "the " + std::string(*group ? "drafter's" : "model's") + " rotary is '" + type +
"' (" + tk + "); YaRN over it is not --rope-scaling's to decide";
return false;
}
if (home.empty()) {
home = *group ? std::string(group) + "rope_parameters."
: kv.any_prefix("text_config.") ? "text_config.rope_parameters."
: "rope_parameters.";
tk = home + "rope_type";
}
/* A checkpoint's own YaRN keeps its trained length unless one is given; anything that was
* tied to its old factor goes, so the reader derives it for the new one. */
int64_t orig = spec.orig;
if (!orig && type == "yarn")
if (const std::string* o = kv.get(home + "original_max_position_embeddings"))
orig = std::atoll(o->c_str());
if (!orig) orig = trained;
if (orig <= 0) {
*why = "no trained length to scale from; give one: yarn:FACTOR:ORIG";
return false;
}
char buf[64];
kv.set(tk, "yarn");
std::snprintf(buf, sizeof buf, "%.17g", spec.factor);
kv.set(home + "factor", buf);
std::snprintf(buf, sizeof buf, "%lld", (long long)orig);
kv.set(home + "original_max_position_embeddings", buf);
for (const char* stale : { "attention_factor", "mscale", "mscale_all_dim" })
kv.erase(home + stale);
return true;
}
} /* namespace rad */
+4
View File
@@ -623,6 +623,10 @@ struct Config {
int64_t max_tok = 8192; /* --max-num-batched-tokens */
int64_t max_seqs = 256;
int64_t max_ctx = 0; /* 0 = the model's training context */
/* --rope-scaling: "yarn:FACTOR[:ORIG]" writes YaRN over the checkpoint's own rotary keys (the
* model card's extension past its trained context, no reconvert); "none" takes a checkpoint's
* YaRN off; empty serves what the checkpoint says. Engine::apply_rope_scaling. */
std::string rope_scaling;
/* THE OPERATOR STATES A BUDGET OR THE ENGINE DERIVES ONE FROM THE CARD, AND EITHER WAY IT IS
* STATED IN FULL BEFORE ANYTHING IS ALLOCATED.