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

Fix #3490: tuning stops waiting for memory when none is being freed instead of ten minutes per candidate, and says how much is free and needed

Niko122175d0d0b96654
diff --git a/strata_control/autotune.py b/strata_control/autotune.py
index a6d37c3cd..083f23591 100644
--- a/strata_control/autotune.py
+++ b/strata_control/autotune.py
@@ -37,6 +37,10 @@ EARLY_DROP = 0.05 # a candidate this much slower than its own r
STAGE_GAP_NOISY = 0.04 # an A/A gap above this: the machine is noisy (6.5)
+STAGNANT_WAIT_S = 60.0 # free memory that has not grown by STAGNANT_MIB after this long is not going to
+STAGNANT_MIB = 512
+
+
class Cancelled(Exception):
"""The user stopped the tune; what was measured so far is kept in the result."""
@@ -193,6 +197,7 @@ class Tuner:
self.progress = progress or (lambda p: None)
self.cancel = cancel or threading.Event()
self.history_bad = history_bad or set()
+ self._ram_gave_up: tuple[float, float] | None = None
self.clock = clock
self.sleep = sleep
self.system = system or session.system
@@ -261,8 +266,10 @@ class Tuner:
return None
def _ram_guard(self, delta_mib: int, label: str, wait_s: float | None = None) -> str | None:
- """None when a start fits; otherwise the plain reason it does not."""
+ """None when a start fits; otherwise the plain reason it does not. Waiting ends early when the free memory does not
+ grow (a loaded model keeps it): ten minutes per candidate made a tuning run last for hours (#3490)."""
waited = 0.0
+ first_avail: float | None = None
while True:
mem = self.system().get("memory", {})
avail = mem.get("available_mib")
@@ -276,10 +283,19 @@ class Tuner:
need = max(need, self.o.min_free_ram_mib)
if avail >= need:
return None
- if waited >= (self.o.wait_ram_s if wait_s is None else wait_s):
- return (f"not enough free memory to start it safely ({avail / 1024:.1f} GiB free, "
- f"{need / 1024:.1f} GiB needed)")
- self._say("waiting", f"Waiting for memory to be freed before trying {label}")
+ reason = (f"not enough free memory to start it safely ({avail / 1024:.1f} GiB free, "
+ f"{need / 1024:.1f} GiB needed)")
+ prior = self._ram_gave_up
+ if prior is not None and need >= prior[0] and avail <= prior[1] + STAGNANT_MIB:
+ return reason # an earlier wait found nothing freed and this needs no less
+ if first_avail is None:
+ first_avail = avail
+ stagnant = waited >= STAGNANT_WAIT_S and avail <= first_avail + STAGNANT_MIB
+ if stagnant or waited >= (self.o.wait_ram_s if wait_s is None else wait_s):
+ self._ram_gave_up = (need, avail)
+ return reason
+ self._say("waiting", f"Waiting for memory to be freed before trying {label} "
+ f"({avail / 1024:.1f} GiB free, {need / 1024:.1f} GiB needed)")
self._check_cancel()
self.sleep(10.0)
waited += 10.0
diff --git a/tests/tuning/test_autotune.py b/tests/tuning/test_autotune.py
index 3a99b111d..2efd9556e 100644
--- a/tests/tuning/test_autotune.py
+++ b/tests/tuning/test_autotune.py
@@ -267,6 +267,22 @@ class TunerTest(unittest.TestCase):
self.assertEqual(r.outcome, "default_confirmed")
self.assertTrue(any(a["status"] == "skipped" and "memory" in a["reason"] for a in r.arms))
+ def test_memory_wait_gives_up_when_nothing_is_freed(self):
+ # #3490: a loaded model keeps the memory; ten minutes of waiting per candidate made a tuning last for hours
+ model = speed_with({})
+ clock = FakeClock()
+ session = StubSession(model=model, clock=clock)
+
+ def system():
+ out = session.system()
+ out["memory"]["available_mib"] = 40000 if not session.runs else 3000
+ return out
+ t = autotune.Tuner(session, BASE, {}, make_ctx(), autotune.Options(per_request=False), clock=clock,
+ sleep=clock.sleep, system=system, total_ram_mib=64000, quiet=lambda: None)
+ before = clock()
+ t.run()
+ self.assertLess(clock() - before, 600.0, "waited for the whole ten minutes per candidate")
+
def test_nothing_compared_is_inconclusive_not_default_confirmed(self):
model = speed_with({})
clock = FakeClock()