From 4a7e373da3352227d7e55c61847c7a59b074e03f Mon Sep 17 00:00:00 2001 From: sergqwer <38361819+sergqwer@users.noreply.github.com> Date: Mon, 26 Oct 2026 18:25:53 +0100 Subject: [PATCH] engine: --adapt-decay outside 0..1 falls back to 0.7 with a line on stderr (#1284) --adapt-decay took any number. Above 1 the adaptive tier's usage counts grow without end (inf after a few hundred adaptations) and the swap gains turn NaN, which breaks the sort that orders the swaps; a negative value flips the counts. The value is now checked where it is parsed (include/strata/program/adapt_decay.hpp, also in sycl/): a finite value in 0..1 is used exactly as before (0 and 1 included, the range the calibration profile already allows); anything else, NaN and inf too, runs with the default 0.7 and says so on stderr. A warning instead of the PR's exit 2: a risky pick is reported, the engine still starts. adapt_decay_test (25 checks) covers the range, the fallback and finite counts after 100000 adaptations. Ported from PR #1284 (the check; the refusal became a fallback). --- CMakeLists.txt | 4 +++ include/strata/program/adapt_decay.hpp | 23 ++++++++++++ src/program/adapt_decay_test.cpp | 49 ++++++++++++++++++++++++++ src/program/generate.cpp | 3 +- sycl/src/program/generate.cpp | 3 +- 5 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 include/strata/program/adapt_decay.hpp create mode 100644 src/program/adapt_decay_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 3fed5ff6..2d83fad8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1588,6 +1588,10 @@ if(STRATA_BUILD_TESTS OR STRATA_BUILD_CONVERSATION_TESTS) add_executable(conv_cache_test src/program/conv_cache_test.cpp) target_include_directories(conv_cache_test PRIVATE include) add_test(NAME conv_cache_test COMMAND conv_cache_test) + # --adapt-decay outside 0..1 falls back to 0.7 with a line (#1284; header-only, CPU) + add_executable(adapt_decay_test src/program/adapt_decay_test.cpp) + target_include_directories(adapt_decay_test PRIVATE include) + add_test(NAME adapt_decay_test COMMAND adapt_decay_test) # which path computes the draft layer's prompt K/V, and STRATA_SPLIT_MTP_BATCH's parsing (header-only, CPU) add_executable(draft_kv_plan_test src/program/draft_kv_plan_test.cpp) target_include_directories(draft_kv_plan_test PRIVATE include) diff --git a/include/strata/program/adapt_decay.hpp b/include/strata/program/adapt_decay.hpp new file mode 100644 index 00000000..9225f39c --- /dev/null +++ b/include/strata/program/adapt_decay.hpp @@ -0,0 +1,23 @@ +#pragma once +// --adapt-decay: the factor the adaptive tier's usage counts are multiplied by after each adaptation (#1284). +// Inside 0..1 it forgets old routing; above 1 the counts grow without end (inf after a few hundred adaptations) and the +// swap gains turn NaN, which breaks the sort that orders the swaps. An unusable value is not refused: the engine says +// so and runs with the default. +#include +#include + +namespace strata::program { + +constexpr float kAdaptDecayDefault = 0.7f; + +/// `v` itself when it is finite and in 0..1 (0 forgets everything at once, 1 never forgets: both work), else the default +/// with a line on stderr. +inline float adapt_decay_checked(double v, std::FILE* log = stderr) { + if (std::isfinite(v) && v >= 0.0 && v <= 1.0) return (float) v; + if (log != nullptr) + std::fprintf(log, "strata: --adapt-decay %g is not between 0 and 1; using %g (counts above 1 grow without end and the swap gains turn NaN)\n", + v, (double) kAdaptDecayDefault); + return kAdaptDecayDefault; +} + +} // namespace strata::program diff --git a/src/program/adapt_decay_test.cpp b/src/program/adapt_decay_test.cpp new file mode 100644 index 00000000..1220532a --- /dev/null +++ b/src/program/adapt_decay_test.cpp @@ -0,0 +1,49 @@ +// --adapt-decay validation (include/strata/program/adapt_decay.hpp), CPU only, no model (#1284). +#include "strata/program/adapt_decay.hpp" + +#include +#include +#include +#include +#include + +using strata::program::adapt_decay_checked; +using strata::program::kAdaptDecayDefault; + +namespace { +int checks = 0; +void check(bool ok, const char* label) { + ++checks; + if (!ok) { std::fprintf(stderr, "FAIL: %s\n", label); std::exit(1); } +} +} // namespace + +int main() { + // inside the range: the value itself, no line (bit for bit what atof gave before, cast to float) + for (double v : {0.0, 0.25, 0.5, 0.7, 0.9, 0.97, 0.999, 1.0}) { + check(adapt_decay_checked(v, nullptr) == (float) v, "a value in 0..1 is kept"); + } + // every default and tune_knobs value of the engine is unchanged + check(adapt_decay_checked(0.7, nullptr) == kAdaptDecayDefault, "the default stays 0.7"); + // outside: the default + for (double v : {1.0000001, 1.5, 2.0, 1e30, -0.1, -1.0, -1e30}) { + check(adapt_decay_checked(v, nullptr) == kAdaptDecayDefault, "an out-of-range value falls back to the default"); + } + const double inf = std::numeric_limits::infinity(); + const double nan = std::numeric_limits::quiet_NaN(); + check(adapt_decay_checked(inf, nullptr) == kAdaptDecayDefault, "inf falls back"); + check(adapt_decay_checked(-inf, nullptr) == kAdaptDecayDefault, "-inf falls back"); + check(adapt_decay_checked(nan, nullptr) == kAdaptDecayDefault, "NaN falls back"); + // the result is always usable: finite and in 0..1 + for (double v : {-5.0, 0.3, 7.0, nan, inf}) { + const float r = adapt_decay_checked(v, nullptr); + check(std::isfinite(r) && r >= 0.f && r <= 1.f, "the result is finite and in 0..1"); + } + // a counts vector driven by an accepted value stays finite over many adaptations (the bug: >1 overflowed to inf) + float usage = 1.f; + const float d = adapt_decay_checked(1.5, nullptr); + for (int i = 0; i < 100000; ++i) usage = usage * d + 1.f; + check(std::isfinite(usage), "counts stay finite with the value the engine ends up using"); + std::printf("adapt_decay_test: %d checks passed\n", checks); + return 0; +} diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 53a6085c..6452a35c 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -79,6 +79,7 @@ #include "strata/prefill/moe_mmq.hpp" #endif #include "strata/core/native_dense.hpp" +#include "strata/program/adapt_decay.hpp" #include "strata/program/batch_read.hpp" #include "strata/program/logits_selection.hpp" #include "strata/program/conv_cache.hpp" @@ -2648,7 +2649,7 @@ int main(int argc, char** argv) { else if (a == "--mtp-window") o.mtp_window = std::atoll(next("--mtp-window")); else if (a == "--pcie-frac") o.pcie_frac = std::atof(next("--pcie-frac")); else if (a == "--adapt-every") { o.adapt_every = std::atoi(next("--adapt-every")); o.adapt_every_given = true; } - else if (a == "--adapt-decay") o.adapt_decay = (float) std::atof(next("--adapt-decay")); + else if (a == "--adapt-decay") o.adapt_decay = strata::program::adapt_decay_checked(std::atof(next("--adapt-decay"))); // #1284: outside 0..1 -> 0.7, said else if (a == "--adapt-min-gain") o.adapt_min_gain = (float) std::atof(next("--adapt-min-gain")); else if (a == "--adapt-async") { o.adapt_async = std::atoi(next("--adapt-async")) != 0 ? 1 : 0; o.adapt_async_given = o.adapt_async; } else if (a == "--spec-min-p") o.spec_min_p = std::atof(next("--spec-min-p")); diff --git a/sycl/src/program/generate.cpp b/sycl/src/program/generate.cpp index 5a111ea4..442fd167 100644 --- a/sycl/src/program/generate.cpp +++ b/sycl/src/program/generate.cpp @@ -66,6 +66,7 @@ namespace strata::prefill { void set_nonresident_share(double share); } // SYC #include "strata/kernels/native_rope.hpp" #include "strata/kernels/mrope.hpp" #include "strata/kernels/kv_q4.hpp" +#include "strata/program/adapt_decay.hpp" #include "strata/program/split_rules.hpp" #include "strata/kernels/qsa.hpp" #include "strata/core/native_head.hpp" @@ -1922,7 +1923,7 @@ int main(int argc, char **argv) try { else if (a == "--mtp-window") o.mtp_window = std::atoll(next("--mtp-window")); else if (a == "--pcie-frac") o.pcie_frac = std::atof(next("--pcie-frac")); else if (a == "--adapt-every") o.adapt_every = std::atoi(next("--adapt-every")); - else if (a == "--adapt-decay") o.adapt_decay = (float) std::atof(next("--adapt-decay")); + else if (a == "--adapt-decay") o.adapt_decay = strata::program::adapt_decay_checked(std::atof(next("--adapt-decay"))); // #1284: outside 0..1 -> 0.7, said else if (a == "--adapt-async") o.adapt_async = std::atoi(next("--adapt-async")) != 0 ? 1 : 0; else if (a == "--spec-min-p") o.spec_min_p = std::atof(next("--spec-min-p")); else if (a == "--stop-eos") o.stop_eos = true;