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

engine: --adapt-decay outside 0..1 falls back to 0.7 with a line on stderr (#1284)

sergqwer4a7e373da335
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3fed5ff6a..2d83fad86 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 000000000..9225f39cd
--- /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 <cmath>
+#include <cstdio>
+
+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 000000000..1220532a6
--- /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 <cmath>
+#include <cstdio>
+#include <cstdlib>
+#include <initializer_list>
+#include <limits>
+
+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<double>::infinity();
+ const double nan = std::numeric_limits<double>::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 53a6085c6..6452a35cc 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 5a111ea47..442fd167c 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;