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

rope: one validity rule for the resolved config; none passes the identity constants to every analytic site

Niko1221eb77a8c145a9
diff --git a/include/strata/kernels/rope_scaling.hpp b/include/strata/kernels/rope_scaling.hpp
index ebb052e22..b494ddc75 100644
--- a/include/strata/kernels/rope_scaling.hpp
+++ b/include/strata/kernels/rope_scaling.hpp
@@ -47,6 +47,17 @@ namespace strata::kernels {
enum class RopeScalingType { None, Linear, YaRN };
+/// The float32 constants the ANALYTIC rotation kernels take (`rope_scaled_angle`'s arguments). The defaults are
+/// the identity: freq_scale 1, no correction, magnitude 1 - with them the helper computes `cosf(theta) * 1.0f`
+/// on `1.0f * theta`, which is exactly today's unscaled `cosf(theta)` bit for bit.
+struct RopeKernelArgs {
+ float freq_scale = 1.0f;
+ float corr_low = 0.0f;
+ float corr_high = 0.0f;
+ float ext_factor = 0.0f;
+ float attn_factor = 1.0f;
+};
+
/// The one rope configuration, as the CLI flag family and the (future) GGUF rope keys resolve to.
///
/// Default-constructed this is EXACTLY today's engine: freq_scale 1, mscale 1, no correction. That
@@ -88,6 +99,23 @@ struct RopeScaling {
out[1] = end > (double) (n_rot - 1) ? (double) (n_rot - 1) : end;
}
+ /// The analytic kernels' constants. `none` returns the identity constants WHATEVER the other knobs hold -
+ /// the table builder ignores them for `none` too, so the two rotation forms cannot disagree there, and an
+ /// unscaled run computes exactly what it computed before scaling existed. Linear/YaRN: ggml's `rope_yarn`
+ /// arguments (`attn_factor` is the raw knob; the helper applies the log term when correcting).
+ RopeKernelArgs kernel_args(int n_rot) const {
+ RopeKernelArgs a;
+ if (type == RopeScalingType::None) return a;
+ double cd[2];
+ corr_dims(n_rot, cd);
+ a.freq_scale = (float) freq_scale();
+ a.corr_low = (float) cd[0];
+ a.corr_high = (float) cd[1];
+ a.ext_factor = (float) ext_factor;
+ a.attn_factor = (float) attn_factor;
+ return a;
+ }
+
private:
/// ggml/src/ggml.c: "Apparently solving `n_rot = 2pi * x * base^((2 * max_pos_emb) / n_dims)`
/// for x, we get `corr_dim(n_rot) = n_dims * log(max_pos_emb / (n_rot * 2pi)) / (2 * log(base))`".
@@ -133,6 +161,28 @@ STRATA_ROPE_SCALING_HD inline void rope_scaled_angle(float theta_extrap, float f
sin_out = sinf(theta) * mscale;
}
+/// The ONE validity rule for a resolved configuration, shared by the engine's startup check and every rotation
+/// site that takes the struct, so a bad knob cannot pass one site and trip (or silently NaN) another. Returns
+/// nullptr when the configuration is usable, else what is wrong. Every value is tested with `std::isfinite`
+/// FIRST: a NaN passes every `<`/`<=` range comparison, which is how a `--rope-scale nan` would otherwise slip
+/// through. `none` passes with any knob values - the knobs are inert there (the resolution resets them).
+inline const char* rope_scaling_invalid(const RopeScaling& s) {
+ if (!std::isfinite(s.freq_base) || s.freq_base <= 1.0) return "the frequency base must be a finite value above 1";
+ if (s.type == RopeScalingType::None) return nullptr;
+ if (!std::isfinite(s.factor) || s.factor < 1.0) return "the scaling factor must be a finite value >= 1";
+ if (!std::isfinite(s.freq_scale_in) || s.freq_scale_in < 0.0)
+ return "the frequency scale must be finite and positive (0 = 1/factor)";
+ const double fs = s.freq_scale();
+ if (!std::isfinite(fs) || fs <= 0.0) return "the resolved frequency scale must be a finite positive value";
+ if (!std::isfinite(s.orig_ctx) || s.orig_ctx < 1.0) return "the original (trained) context must be finite and > 0";
+ if (!std::isfinite(s.ext_factor) || s.ext_factor < 0.0) return "the YaRN ext factor must be finite and >= 0";
+ if (!std::isfinite(s.attn_factor) || s.attn_factor <= 0.0) return "the attention factor must be finite and > 0";
+ if (!std::isfinite(s.beta_fast) || s.beta_fast <= 0.0 || !std::isfinite(s.beta_slow) || s.beta_slow <= 0.0)
+ return "the YaRN betas must be finite and > 0";
+ if (!std::isfinite(s.mscale()) || s.mscale() <= 0.0) return "the resolved magnitude correction must be finite and > 0";
+ return nullptr;
+}
+
/// The process's one rope configuration. Set it ONCE at startup, after the CLI and the model file
/// have agreed on the values and BEFORE `session_init` (which builds the rope table and captures the
/// graphs); read it at table build time and when launching the analytic kernels. Plain storage, no
diff --git a/src/kernels/cuda/native_qsa_indexer.cu b/src/kernels/cuda/native_qsa_indexer.cu
index 86d6ae835..299199a93 100644
--- a/src/kernels/cuda/native_qsa_indexer.cu
+++ b/src/kernels/cuda/native_qsa_indexer.cu
@@ -216,17 +216,10 @@ bool native_qsa_indexer_enabled() { return enabled.load(std::memory_order_relaxe
void native_qsa_indexer_append(const float* raw, const int32_t* relative_pos_device, int32_t pos_base,
const float* gamma, float epsilon, const QsaIndexerBuffers& b,
const QsaShapes& s, int64_t max_cells, const RopeScaling& scaling, void* stream) {
- const double fs = scaling.freq_scale();
if (!stream || s.idx_dim != D || s.idx_block != R || s.n_rot != ROT ||
max_cells < 1 || max_cells > INT32_MAX || pos_base < 0 || pos_base % R ||
int64_t(pos_base) + max_cells > INT32_MAX || !std::isfinite(epsilon) || epsilon <= 0.0f ||
- !std::isfinite(scaling.freq_base) || scaling.freq_base <= 1.0 ||
- !std::isfinite(fs) || fs <= 0.0 ||
- !std::isfinite(scaling.ext_factor) || scaling.ext_factor < 0.0 ||
- !std::isfinite(scaling.attn_factor) || scaling.attn_factor <= 0.0 ||
- !std::isfinite(scaling.orig_ctx) || scaling.orig_ctx < 1.0 ||
- !std::isfinite(scaling.beta_fast) || scaling.beta_fast <= 0.0 ||
- !std::isfinite(scaling.beta_slow) || scaling.beta_slow <= 0.0)
+ rope_scaling_invalid(scaling) != nullptr)
throw std::invalid_argument("native QSA indexer requires fixed geometry, aligned position base, positive capacity/epsilon, valid frequency/scaling and explicit stream");
const Span spans[] = {{raw,D*4},{relative_pos_device,4},{gamma,D*4},{b.tail,(R-1)*D*4},
{b.dead,D*4},{b.pooled,std::size_t(max_cells/R+1)*D*4},{b.block_pos,4}};
@@ -234,35 +227,24 @@ void native_qsa_indexer_append(const float* raw, const int32_t* relative_pos_dev
for (int i = 0; i < 7; ++i) for (int j = i + 1; j < 7; ++j)
if (overlaps(spans[i], spans[j])) throw std::invalid_argument("native QSA indexer buffers overlap");
const float theta_scale = powf((float) scaling.freq_base, -2.0f / ROT);
- double corr[2];
- scaling.corr_dims(ROT, corr);
+ const RopeKernelArgs k = scaling.kernel_args(ROT); // none: the identity constants
append<<<1,THREADS,0,static_cast<cudaStream_t>(stream)>>>(raw,relative_pos_device,pos_base,gamma,epsilon,
- b.tail,b.dead,b.pooled,b.block_pos,int(max_cells),theta_scale,(float) fs,
- (float) corr[0],(float) corr[1],(float) scaling.ext_factor,(float) scaling.attn_factor,mrope_table());
+ b.tail,b.dead,b.pooled,b.block_pos,int(max_cells),theta_scale,k.freq_scale,
+ k.corr_low,k.corr_high,k.ext_factor,k.attn_factor,mrope_table());
const auto error = cudaGetLastError();
if (error != cudaSuccess) throw std::runtime_error(cudaGetErrorString(error));
}
void native_qsa_indexer_append_batch(const float* raw, int64_t n, int64_t p0, int32_t pos_base, const float* gamma,
float epsilon, const QsaIndexerBuffers& b, const QsaShapes& s, int64_t max_cells,
const RopeScaling& scaling, void* stream) {
- const double fs = scaling.freq_scale();
if (n <= 0) return;
if (!stream || s.idx_dim != D || s.idx_block != R || s.n_rot != ROT || p0 < 0 || p0 + n > max_cells ||
max_cells > INT32_MAX || pos_base < 0 || pos_base % R || int64_t(pos_base) + max_cells > INT32_MAX ||
- !std::isfinite(epsilon) || epsilon <= 0.0f ||
- !std::isfinite(scaling.freq_base) || scaling.freq_base <= 1.0 ||
- !std::isfinite(fs) || fs <= 0.0 ||
- !std::isfinite(scaling.ext_factor) || scaling.ext_factor < 0.0 ||
- !std::isfinite(scaling.attn_factor) || scaling.attn_factor <= 0.0 ||
- !std::isfinite(scaling.orig_ctx) || scaling.orig_ctx < 1.0 ||
- !std::isfinite(scaling.beta_fast) || scaling.beta_fast <= 0.0 ||
- !std::isfinite(scaling.beta_slow) || scaling.beta_slow <= 0.0)
+ !std::isfinite(epsilon) || epsilon <= 0.0f || rope_scaling_invalid(scaling) != nullptr)
throw std::invalid_argument("native QSA indexer (batch): bad geometry, positions, parameters or scaling");
const float theta_scale = powf((float) scaling.freq_base, -2.0f / ROT);
- double corr[2];
- scaling.corr_dims(ROT, corr);
- const float fsf = (float) fs, cl = (float) corr[0], ch = (float) corr[1];
- const float ef = (float) scaling.ext_factor, ms = (float) scaling.attn_factor;
+ const RopeKernelArgs k = scaling.kernel_args(ROT); // none: the identity constants
+ const float fsf = k.freq_scale, cl = k.corr_low, ch = k.corr_high, ef = k.ext_factor, ms = k.attn_factor;
const cudaStream_t st = static_cast<cudaStream_t>(stream);
const int32_t* mtab = mrope_table();
if (p0 == 0) append_first<<<1, THREADS, 0, st>>>(raw, gamma, epsilon, b.dead, b.pooled, theta_scale,
diff --git a/src/kernels/cuda/native_rope.cu b/src/kernels/cuda/native_rope.cu
index d40c4f6be..463117204 100644
--- a/src/kernels/cuda/native_rope.cu
+++ b/src/kernels/cuda/native_rope.cu
@@ -76,16 +76,9 @@ void native_rope_set_enabled(bool value) { enabled.store(value, std::memory_orde
bool native_rope_enabled() { return enabled.load(std::memory_order_relaxed); }
void native_rope_apply(const float* x, float* out, int rows, int head_dim,
int n_rot, const RopeScaling& scaling, const int* positions, void* stream) {
- const double fs = scaling.freq_scale();
if (!x || !out || !positions || !stream || rows < 1 || rows > 65535 ||
(head_dim != 128 && head_dim != 256) || n_rot != 64 ||
- !std::isfinite(scaling.freq_base) || scaling.freq_base <= 1.0 ||
- !std::isfinite(fs) || fs <= 0.0 ||
- !std::isfinite(scaling.ext_factor) || scaling.ext_factor < 0.0 ||
- !std::isfinite(scaling.attn_factor) || scaling.attn_factor <= 0.0 ||
- !std::isfinite(scaling.orig_ctx) || scaling.orig_ctx < 1.0 ||
- !std::isfinite(scaling.beta_fast) || scaling.beta_fast <= 0.0 ||
- !std::isfinite(scaling.beta_slow) || scaling.beta_slow <= 0.0 ||
+ rope_scaling_invalid(scaling) != nullptr ||
reinterpret_cast<uintptr_t>(x) % 4 || reinterpret_cast<uintptr_t>(out) % 4 ||
reinterpret_cast<uintptr_t>(positions) % 4) {
throw std::invalid_argument("native RoPE requires aligned F32 rows, width 128/256, rotation 64, valid base/scaling and explicit stream");
@@ -98,13 +91,11 @@ void native_rope_apply(const float* x, float* out, int rows, int head_dim,
}
// Match pinned host-side float powf before device fast powf/trigonometry.
const float theta_scale = powf((float) scaling.freq_base, -2.0f / n_rot);
- double corr[2];
- scaling.corr_dims(n_rot, corr);
+ const RopeKernelArgs k = scaling.kernel_args(n_rot); // none: the identity constants
apply<<<dim3((head_dim / 2 + 127) / 128, rows), 128, 0,
static_cast<cudaStream_t>(stream)>>>(x, out, rows, head_dim, n_rot, theta_scale,
- (float) fs, (float) corr[0], (float) corr[1],
- (float) scaling.ext_factor, (float) scaling.attn_factor,
- positions, mrope_table());
+ k.freq_scale, k.corr_low, k.corr_high, k.ext_factor,
+ k.attn_factor, positions, mrope_table());
const auto error = cudaGetLastError();
if (error != cudaSuccess) throw std::runtime_error(cudaGetErrorString(error));
}
diff --git a/src/prefill/kernels.cu b/src/prefill/kernels.cu
index 5e74d6097..25226735c 100644
--- a/src/prefill/kernels.cu
+++ b/src/prefill/kernels.cu
@@ -763,18 +763,17 @@ void rms_rows(float* x, const float* w, int64_t rows, int64_t cols, int64_t ld,
}
void rope(float* x, int64_t T, int64_t heads, int64_t dim, int64_t ld, int64_t pos0,
const strata::kernels::RopeScaling& scaling, void* stream) {
- const double fs = scaling.freq_scale();
- if (!(fs > 0.0) || scaling.freq_base <= 1.0) {
- std::fprintf(stderr, "prefill rope: invalid scaling (freq_base %.6g, freq_scale %.6g)\n",
- scaling.freq_base, fs);
+ // The engine validates the resolved config at startup with the same rule (generate.cpp), so this only
+ // fires for a caller that bypassed it; the prompt path has no error return here, so it stops the process.
+ if (const char* why = strata::kernels::rope_scaling_invalid(scaling)) {
+ std::fprintf(stderr, "prefill rope: invalid rope scaling: %s\n", why);
std::exit(1);
}
const float theta_scale = powf((float) scaling.freq_base, -2.0f / 64.0f);
- double corr[2];
- scaling.corr_dims(64, corr);
+ const strata::kernels::RopeKernelArgs k = scaling.kernel_args(64); // none: the identity constants
rope_kernel<<<(unsigned) (T * heads), 32, 0, (cudaStream_t) stream>>>(
- x, heads, dim, ld, pos0, theta_scale, (float) fs, (float) corr[0], (float) corr[1],
- (float) scaling.ext_factor, (float) scaling.attn_factor, strata::kernels::mrope_table());
+ x, heads, dim, ld, pos0, theta_scale, k.freq_scale, k.corr_low, k.corr_high, k.ext_factor, k.attn_factor,
+ strata::kernels::mrope_table());
check("rope");
}
void split_q(const float* q_full, float* q, int64_t T, void* stream) {
diff --git a/src/program/generate.cpp b/src/program/generate.cpp
index 979dd0545..61d1e8556 100644
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@ -1329,8 +1329,15 @@ int main(int argc, char** argv) {
o.rope_scaling.c_str());
return 2;
}
+ // every knob FINITE first: `atof("nan")` is NaN, and a NaN passes every range comparison below
+ for (const double v : {o.rope_scale, o.rope_freq_base, o.rope_freq_scale, o.yarn_orig_ctx, o.yarn_ext_factor,
+ o.yarn_attn_factor, o.yarn_beta_fast, o.yarn_beta_slow})
+ if (!std::isfinite(v)) {
+ std::fprintf(stderr, "strata generate: a rope scaling knob is not a finite number (%g)\n", v);
+ return 2;
+ }
// 0 is the absent default; an explicit factor must extend, not shrink
- if (o.rope_scale > 0 && o.rope_scale < 1.0) {
+ if (o.rope_scale != 0 && 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;
@@ -1499,6 +1506,29 @@ int main(int argc, char** argv) {
rope_cfg.attn_factor = o.yarn_attn_factor;
rope_cfg.beta_fast = o.yarn_beta_fast;
rope_cfg.beta_slow = o.yarn_beta_slow;
+ if (rope_cfg.type == RST::None) {
+ // none is the trained rotation, exactly: the scaling knobs are inert (the table builder and
+ // `kernel_args` ignore them), and resetting them keeps the logged/queried config honest. Only the
+ // frequency base survives - it is the rotation itself, not a scaling knob.
+ const bool knobs = o.rope_scale > 1.0 || o.rope_freq_scale > 0 || o.yarn_ext_factor > 0 ||
+ o.yarn_attn_factor != 1.0;
+ const double base = rope_cfg.freq_base;
+ rope_cfg = strata::kernels::RopeScaling{};
+ rope_cfg.freq_base = base;
+ if (knobs)
+ std::fprintf(stderr, "strata generate: note: no rope scaling is active (none), so --rope-scale, "
+ "--rope-freq-scale and the --yarn-* knobs have no effect\n");
+ }
+ // THE RESOLVED CONFIG IS VALIDATED AS A WHOLE, with the one rule every rotation site also applies
+ // (rope_scaling.hpp): the CLI ranges above cannot see a model-file value, nor a combination such as a
+ // --rope-freq-scale that turns the resolved factor non-finite.
+ if (const char* why = strata::kernels::rope_scaling_invalid(rope_cfg)) {
+ std::fprintf(stderr, "strata generate: invalid rope scaling configuration: %s (type %s, factor %g, "
+ "freq_scale %g, base %g, original context %g)\n",
+ why, rope_cfg.type == RST::YaRN ? "yarn" : rope_cfg.type == RST::Linear ? "linear" : "none",
+ rope_cfg.factor, rope_cfg.freq_scale(), rope_cfg.freq_base, rope_cfg.orig_ctx);
+ return 2;
+ }
strata::kernels::rope_scaling_set(rope_cfg);
if (rope_cfg.type != RST::None) {
const char* tn = rope_cfg.type == RST::YaRN ? "yarn" : "linear";
@@ -1512,11 +1542,13 @@ int main(int argc, char** argv) {
"strata generate: note: the context is within the trained %.0f - no position needs the "
"extension, and the resolved scaling still applies to every angle\n",
rope_cfg.orig_ctx);
- if (rope_cfg.type == RST::YaRN && rope_cfg.mscale() != 1.0)
+ // only when there IS a magnitude correction: YaRN's log term (ext_factor != 0) or an explicit
+ // --yarn-attn-factor; plain linear (mscale 1) has none, and saying otherwise was TODO 22
+ if (rope_cfg.mscale() != 1.0)
std::fprintf(stderr,
- "strata generate: note: YaRN's magnitude correction scales cos and sin by %.6f "
+ "strata generate: note: the %s magnitude correction scales cos and sin by %.6f "
"at every position\n",
- rope_cfg.mscale());
+ rope_cfg.type == RST::YaRN ? "YaRN" : "--yarn-attn-factor", rope_cfg.mscale());
}
}
strata::core::NativeEmbed native_embed;