Archived
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:
1 parent
ee5fcf1024
commit
6fa7707dff
9 files changed
+457
-1
No files matched your search
@@ -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
|
||||
|
||||
@@ -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
@@ -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;
|
||||
|
||||
@@ -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 */
|
||||
@@ -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.
|
||||
|
||||
@@ -984,6 +984,7 @@ one line naming each one the command line moved.
|
||||
| `--max-num-batched-tokens N` | 8192 | largest token count one step may carry |
|
||||
| `--max-num-seqs N` | 256 | largest number of sequences one step may carry |
|
||||
| `--max-model-len N` | 0 | context bound; 0 takes the model's training context |
|
||||
| `--rope-scaling SPEC` | — | `yarn:FACTOR[:ORIG]`: static YaRN over the container's own rotary keys, no reconvert; `none` takes a checkpoint's YaRN off ([YARN.md](YARN.md)) |
|
||||
|
||||
**Memory**
|
||||
|
||||
|
||||
@@ -82,6 +82,34 @@ successful actual long requests must be checked; the command is not a promise
|
||||
of concurrency or accuracy. Keep the unmodified model and serving setup until
|
||||
long-context quality and speed meet the deployment's requirements.
|
||||
|
||||
## Serve an existing container: `--rope-scaling`
|
||||
|
||||
The same metadata can be written when the container is loaded instead of converted in:
|
||||
|
||||
```sh
|
||||
radiance --model /models/qwen3.8-next-flash-fp8.rad \
|
||||
--rope-scaling yarn:2 --max-model-len 409600
|
||||
```
|
||||
|
||||
`yarn:FACTOR[:ORIG]` sets `rope_type = yarn`, `factor` and `original_max_position_embeddings`
|
||||
(`ORIG`, default the container's own trained length) in the rotary object the reader above
|
||||
takes first -- for a Qwen3.8 container that is `text_config.rope_parameters`, which already says
|
||||
`default`; YaRN written under plain `rope_parameters` would sit behind it and never be read. A
|
||||
DFlash2 drafter gets the same factor over its own trained length. Over a checkpoint's own YaRN
|
||||
the factor is replaced, its trained length kept unless `ORIG` is given, and an explicit
|
||||
`attention_factor` (or `mscale` variant) dropped so the magnitude is derived for the new factor.
|
||||
`none` takes a checkpoint's YaRN off. A linear or other explicit rotary type is refused rather
|
||||
than stacked over. The result is resolved and checked exactly as a converted container's is
|
||||
(`core/format/rope_override.h`, held to `yarn_from` in `tests/yarn_test.cpp`).
|
||||
|
||||
Startup says what the rotary is (`rope: YaRN factor 2 over 262144 trained positions -- the rotary
|
||||
reaches 524288`) and warns when `--max-model-len` is past what it covers -- also with no scaling
|
||||
at all, past the trained length. A persisted prefix cache (`--prefix-cache-dir`) written under a
|
||||
different `--rope-scaling` is not reused: every cached key was rotated differently.
|
||||
|
||||
The flag is for trying a factor, or serving a container whose source checkpoints are not at hand;
|
||||
a deployment that settles on one can convert it in as above and drop the flag.
|
||||
|
||||
## Kernel and test contract
|
||||
|
||||
`rope` and `rope_table` take optional resolved coefficients `yarn_factor`,
|
||||
|
||||
@@ -825,3 +825,21 @@ TEST(usage_names_every_flag_it_documents) {
|
||||
}
|
||||
|
||||
RAD_TEST_MAIN()
|
||||
|
||||
/* --rope-scaling is checked at the command line: a typo is a refusal, not a model served with its
|
||||
* trained rotary at a length it cannot reach. */
|
||||
TEST(rope_scaling_takes_yarn_with_a_factor_above_one_or_none) {
|
||||
for (const char* ok : { "yarn:4", "yarn:2.5", "yarn:4:262144", "none" }) {
|
||||
Config c;
|
||||
CHECK_EQ(parse({ "--rope-scaling", ok }, &c), RAD_OK);
|
||||
CHECK_EQ(c.rope_scaling, std::string(ok));
|
||||
}
|
||||
for (const char* bad : { "yarn:1", "yarn:0.5", "yarn:4x", "yarn:", "yarn:4:", "yarn:4:0",
|
||||
"yarn:4:12k", "linear:2", "yarn" }) {
|
||||
Config c;
|
||||
CHECK(parse({ "--rope-scaling", bad }, &c) < 0);
|
||||
}
|
||||
Config d;
|
||||
CHECK_EQ(parse({}, &d), RAD_OK);
|
||||
CHECK(d.rope_scaling.empty());
|
||||
}
|
||||
@@ -1,5 +1,10 @@
|
||||
#include "rad_test.h"
|
||||
#include "../arch/common/rad_arch.h"
|
||||
#include "format/rope_override.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using rad::arch::Geom;
|
||||
using rad::arch::yarn_from;
|
||||
@@ -80,4 +85,149 @@ TEST(yarn_legacy_explicit_magnitude_and_fractional_bounds) {
|
||||
CHECK(yarn_from(g, &m, "", 64) != RAD_OK);
|
||||
}
|
||||
|
||||
/* --rope-scaling (core/format/rope_override.h) rewrites a container's keys at load; yarn_from then
|
||||
* reads the result as it reads a converted container. These hold the two together. */
|
||||
namespace {
|
||||
struct Meta {
|
||||
std::vector<std::string> k, v;
|
||||
std::vector<const char*> kp, vp;
|
||||
RadModelMeta m{};
|
||||
Meta(std::initializer_list<std::pair<const char*, const char*>> kvs) {
|
||||
for (const auto& p : kvs) { k.push_back(p.first); v.push_back(p.second); }
|
||||
}
|
||||
const RadModelMeta* meta() {
|
||||
kp.clear(); vp.clear();
|
||||
for (size_t i = 0; i < k.size(); ++i) { kp.push_back(k[i].c_str()); vp.push_back(v[i].c_str()); }
|
||||
m = RadModelMeta{};
|
||||
m.n_kv = (int)k.size(); m.kv_key = kp.data(); m.kv_val = vp.data();
|
||||
return &m;
|
||||
}
|
||||
const std::string* get(const std::string& key) const {
|
||||
for (size_t i = 0; i < k.size(); ++i) if (k[i] == key) return &v[i];
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
/* What a Qwen3.8-Flash-Next container says about its rotary (rad-info --meta). */
|
||||
Meta qfn_container() {
|
||||
return Meta{ { "max_position_embeddings", "262144" },
|
||||
{ "rope_parameters.mrope_section", "11 11 10" },
|
||||
{ "rope_parameters.partial_rotary_factor", "0.25" },
|
||||
{ "rope_parameters.rope_theta", "10000000" },
|
||||
{ "rope_parameters.rope_type", "default" },
|
||||
{ "text_config.max_position_embeddings", "262144" },
|
||||
{ "text_config.rope_parameters.mrope_section", "11 11 10" },
|
||||
{ "text_config.rope_parameters.partial_rotary_factor", "0.25" },
|
||||
{ "text_config.rope_parameters.rope_theta", "10000000" },
|
||||
{ "text_config.rope_parameters.rope_type", "default" } };
|
||||
}
|
||||
rad::RopeSpec spec(const char* s) {
|
||||
rad::RopeSpec r;
|
||||
CHECK(rad::rope_spec_parse(s, &r));
|
||||
return r;
|
||||
}
|
||||
} /* namespace */
|
||||
|
||||
TEST(rope_spec_parse_takes_yarn_with_a_factor_above_one_or_none) {
|
||||
rad::RopeSpec r;
|
||||
CHECK(rad::rope_spec_parse("yarn:2", &r));
|
||||
CHECK(r.kind == rad::RopeSpec::Yarn); CHECK_EQ(r.factor, 2.0); CHECK_EQ(r.orig, (int64_t)0);
|
||||
CHECK(rad::rope_spec_parse("yarn:4:131072", &r));
|
||||
CHECK_EQ(r.factor, 4.0); CHECK_EQ(r.orig, (int64_t)131072);
|
||||
CHECK(rad::rope_spec_parse("none", &r)); CHECK(r.kind == rad::RopeSpec::None);
|
||||
CHECK(rad::rope_spec_parse("", &r)); CHECK(r.kind == rad::RopeSpec::Keep);
|
||||
for (const char* bad : { "yarn:1", "yarn:0.5", "yarn:4x", "yarn:", "yarn:4:", "yarn:4:0",
|
||||
"yarn:4:12k", "linear:2", "yarn", "yarn:nan", "yarn:inf" })
|
||||
CHECK(!rad::rope_spec_parse(bad, &r));
|
||||
}
|
||||
|
||||
/* THE TRAP THE OVERRIDE EXISTS TO AVOID: the container names its rotary under text_config., and
|
||||
* the reader takes the first object that names one -- so YaRN written beside it is never read. */
|
||||
TEST(rope_override_writes_where_the_reader_looks_on_a_qwen38_container) {
|
||||
{
|
||||
Meta naive = qfn_container();
|
||||
naive.v[4] = "yarn";
|
||||
naive.k.push_back("rope_parameters.factor"); naive.v.push_back("2");
|
||||
naive.k.push_back("rope_parameters.original_max_position_embeddings");
|
||||
naive.v.push_back("262144");
|
||||
Geom g{}; g.theta = 1e7f;
|
||||
CHECK_OK(yarn_from(g, naive.meta(), "", 64));
|
||||
CHECK_EQ(g.yarn.factor, 0.0f); /* text_config's "default" won */
|
||||
}
|
||||
Meta m = qfn_container();
|
||||
std::string why;
|
||||
CHECK(rad::rope_override_group(m.k, m.v, spec("yarn:2"), "", 262144, &why));
|
||||
CHECK_EQ(*m.get("text_config.rope_parameters.rope_type"), std::string("yarn"));
|
||||
CHECK_EQ(*m.get("rope_parameters.rope_type"), std::string("default")); /* untouched */
|
||||
Geom g{}; g.theta = 1e7f;
|
||||
CHECK_OK(yarn_from(g, m.meta(), "", 64));
|
||||
CHECK_EQ(g.yarn.factor, 2.0f);
|
||||
CHECK_EQ(g.yarn.low, 14.0f); CHECK_EQ(g.yarn.high, 22.0f);
|
||||
CHECK_NEAR(g.yarn.magnitude, 1.0 + 0.1 * std::log(2.0), 1e-6);
|
||||
CHECK_EQ(g.rope_scale, 1.0f);
|
||||
const rad::RopeResolved r = rad::rope_resolve(m.k, m.v, "");
|
||||
CHECK_EQ(r.type, std::string("yarn")); CHECK_EQ(r.factor, 2.0); CHECK_EQ(r.orig, 262144.0);
|
||||
/* An explicit ORIG is the one used. */
|
||||
Meta m2 = qfn_container();
|
||||
CHECK(rad::rope_override_group(m2.k, m2.v, spec("yarn:4:131072"), "", 262144, &why));
|
||||
Geom g2{}; g2.theta = 1e7f;
|
||||
CHECK_OK(yarn_from(g2, m2.meta(), "", 64));
|
||||
CHECK_EQ(g2.yarn.factor, 4.0f);
|
||||
CHECK(g2.yarn.low < 14.0f); /* a shorter trained length moves the ramp down */
|
||||
}
|
||||
|
||||
/* A checkpoint's own YaRN: the override's factor replaces it, its trained length stays, and what
|
||||
* belonged to the old factor (an explicit attention_factor) goes so the reader derives it anew.
|
||||
* `none` takes it off. */
|
||||
TEST(rope_override_over_a_checkpoints_own_yarn_and_none_takes_it_off) {
|
||||
auto legacy = [] {
|
||||
return Meta{ { "rope_scaling.type", "yarn" }, { "rope_scaling.factor", "4" },
|
||||
{ "rope_scaling.original_max_position_embeddings", "32768" },
|
||||
{ "rope_scaling.attention_factor", "1.25" } };
|
||||
};
|
||||
Meta m = legacy();
|
||||
std::string why;
|
||||
CHECK(rad::rope_override_group(m.k, m.v, spec("yarn:2"), "", 131072, &why));
|
||||
CHECK_EQ(*m.get("rope_scaling.original_max_position_embeddings"), std::string("32768"));
|
||||
CHECK(!m.get("rope_scaling.attention_factor"));
|
||||
Geom g{}; g.theta = 1e6f;
|
||||
CHECK_OK(yarn_from(g, m.meta(), "", 128));
|
||||
CHECK_EQ(g.yarn.factor, 2.0f);
|
||||
CHECK_NEAR(g.yarn.magnitude, 1.0 + 0.1 * std::log(2.0), 1e-6);
|
||||
Meta off = legacy();
|
||||
CHECK(rad::rope_override_group(off.k, off.v, spec("none"), "", 131072, &why));
|
||||
Geom g3{}; g3.theta = 1e6f; g3.rope_scale = 1.0f;
|
||||
CHECK_OK(yarn_from(g3, off.meta(), "", 128));
|
||||
CHECK_EQ(g3.yarn.factor, 0.0f);
|
||||
CHECK_EQ(g3.rope_scale, 1.0f);
|
||||
/* none on a model without YaRN changes nothing. */
|
||||
Meta q = qfn_container();
|
||||
const std::vector<std::string> before = q.v;
|
||||
CHECK(rad::rope_override_group(q.k, q.v, spec("none"), "", 262144, &why));
|
||||
CHECK(q.v == before);
|
||||
}
|
||||
|
||||
/* Not stacked over another rotary scheme, and not without a trained length to scale from. The
|
||||
* drafter is its own group, over its own trained length. */
|
||||
TEST(rope_override_refuses_linear_and_scales_a_drafter_on_its_own) {
|
||||
Meta lin{ { "rope_parameters.rope_type", "linear" }, { "rope_parameters.factor", "2" } };
|
||||
std::string why;
|
||||
CHECK(!rad::rope_override_group(lin.k, lin.v, spec("yarn:2"), "", 262144, &why));
|
||||
CHECK(why.find("linear") != std::string::npos);
|
||||
Meta bare{ { "rope_theta", "1000000" } };
|
||||
CHECK(!rad::rope_override_group(bare.k, bare.v, spec("yarn:2"), "", 0, &why));
|
||||
CHECK(rad::rope_override_group(bare.k, bare.v, spec("yarn:2:65536"), "", 0, &why));
|
||||
CHECK_EQ(*bare.get("rope_parameters.rope_type"), std::string("yarn"));
|
||||
|
||||
Meta d = qfn_container();
|
||||
d.k.push_back("draft.rope_parameters.rope_type"); d.v.push_back("default");
|
||||
d.k.push_back("draft.max_position_embeddings"); d.v.push_back("262144");
|
||||
CHECK(rad::rope_override_group(d.k, d.v, spec("yarn:2"), "", 262144, &why));
|
||||
CHECK(rad::rope_override_group(d.k, d.v, spec("yarn:2"), "draft.", 262144, &why));
|
||||
Geom t{}, dr{}; t.theta = dr.theta = 1e7f;
|
||||
CHECK_OK(yarn_from(t, d.meta(), "", 64));
|
||||
CHECK_OK(yarn_from(dr, d.meta(), "draft.", 128));
|
||||
CHECK_EQ(t.yarn.factor, 2.0f); CHECK_EQ(dr.yarn.factor, 2.0f);
|
||||
CHECK_EQ(dr.yarn.low, 28.0f); CHECK_EQ(dr.yarn.high, 43.0f);
|
||||
}
|
||||
|
||||
RAD_TEST_MAIN()
|
||||
Reference in new issue
Block a user