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

tuning tests: a Python stub engine (READY/PP/T/DONE) started as [sys.executable, stub.py] replaces the .sh stubs on every OS

Niko1221f7e12d23ceca
diff --git a/strata_control/tune_engine.py b/strata_control/tune_engine.py
index 5de9b74be..e7c8830e3 100644
--- a/strata_control/tune_engine.py
+++ b/strata_control/tune_engine.py
@@ -333,7 +333,8 @@ class EngineHarness:
self._context: int | None = None
def _argv(self) -> list[str]:
- argv = [str(self.engine), "--serve"]
+ from strata_control.tuning import engine_command
+ argv = [*engine_command(self.engine), "--serve"]
for flag, value in self.args.items():
argv.append(flag)
if value not in ("1", "0", "true", "false", ""):
diff --git a/strata_control/tuning.py b/strata_control/tuning.py
index 5a5c3949d..fdc44668f 100644
--- a/strata_control/tuning.py
+++ b/strata_control/tuning.py
@@ -158,6 +158,18 @@ def validate_dimensions(backend: str,
return offered, excluded
+def engine_command(engine: Path | str) -> list[str]:
+ """The argv prefix that runs an engine: the binary itself, or ``[sys.executable, script.py]``.
+
+ A ``.py`` file stands in for the engine in tests and demos (the same READY/PP/T/DONE protocol,
+ no weights). Running it through the current interpreter is what works on every OS: a ``#!`` line
+ is ignored by Windows, which answers WinError 193.
+ """
+ import sys
+ path = str(engine)
+ return [sys.executable, path] if path.lower().endswith(".py") else [path]
+
+
def engine_flag_inventory(engine: Path) -> list[str] | None:
"""Read the flags an engine binary documents, or None when it cannot be run here.
@@ -168,7 +180,7 @@ def engine_flag_inventory(engine: Path) -> list[str] | None:
at all.
"""
try:
- proc = subprocess.run([str(engine), "--help"], capture_output=True, text=True, timeout=20)
+ proc = subprocess.run([*engine_command(engine), "--help"], capture_output=True, text=True, timeout=20)
except (OSError, subprocess.SubprocessError):
return None
import re
diff --git a/tests/tuning/stub_engine.py b/tests/tuning/stub_engine.py
new file mode 100644
index 000000000..3321cbc42
--- /dev/null
+++ b/tests/tuning/stub_engine.py
@@ -0,0 +1,52 @@
+#!/usr/bin/env python3
+"""A stub engine for exercising `strata tune run` end to end, on every OS.
+
+Run it as ``[sys.executable, "stub_engine.py", "--serve", ...]`` (``strata_control.tuning.engine_command``
+does that for any ``.py`` engine). It speaks the real serve protocol (READY / PP / T / DONE) and
+makes its decode speed depend on one flag, so a sweep has something real to find. It is NOT a model
+and its numbers mean nothing about any hardware -- it exists so the *sweep machinery* can be tested
+without weights.
+
+ --kv int8 decodes at 200 tok/s
+ anything else (including --kv fp16) decodes at 100 tok/s
+
+The prompt's length comes from the GEN line's token list, so the workload is genuinely the one the
+caller asked for.
+"""
+import sys
+
+
+def main(argv):
+ kv = "unknown"
+ for index, arg in enumerate(argv):
+ if arg == "--kv" and index + 1 < len(argv):
+ kv = argv[index + 1]
+ if "--help" in argv:
+ sys.stderr.write("usage:\n --kv KV\n --ple-io MODE\n")
+ return 0
+ out = sys.stdout
+ out.write("READY 32768 stop\n")
+ out.flush()
+ for raw in sys.stdin:
+ line = raw.strip()
+ if line == "QUIT" or not line:
+ return 0
+ if not line.startswith("GEN"):
+ continue
+ fields = line.split()
+ count = len(fields[-1].split(","))
+ try:
+ max_new = int(fields[1])
+ except (IndexError, ValueError):
+ max_new = 8
+ decode_ms = 50.0 if kv == "int8" else 100.0
+ out.write(f"PP {count} {count} 200.0 2560.0\n")
+ for i in range(max_new): # one T per token: the harness cross-checks the stream against DONE
+ out.write(f"T {i}\n")
+ out.write(f"DONE {max_new} {count} 200.0 {decode_ms} stop 0 0\n")
+ out.flush()
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main(sys.argv[1:]))
diff --git a/tests/tuning/stub_engine.sh b/tests/tuning/stub_engine.sh
deleted file mode 100755
index e9339e860..000000000
--- a/tests/tuning/stub_engine.sh
+++ /dev/null
@@ -1,39 +0,0 @@
-#!/bin/sh
-# A stub engine for exercising `strata tune run` end to end.
-#
-# It speaks the real serve protocol (READY / PP / T / DONE) and makes its decode speed depend on one
-# flag, so a sweep has something real to find. It is NOT a model and its numbers mean nothing about
-# any hardware -- it exists so the *sweep machinery* can be tested without weights.
-#
-# --kv int8 decodes at 200 tok/s
-# anything else (including --kv fp16) decodes at 100 tok/s
-#
-# The prompt's length comes from the GEN line's token list, so the workload is genuinely the one the
-# caller asked for.
-KV=unknown
-for arg in "$@"; do
- if [ "$prev" = "--kv" ]; then KV="$arg"; fi
- prev="$arg"
-done
-
-echo "READY 32768 stop"
-while read -r line; do
- case "$line" in
- QUIT|"") exit 0 ;;
- GEN*)
- ids=${line##* }
- count=$(printf '%s' "$ids" | tr ',' '\n' | wc -l | tr -d ' ')
- maxnew=$(printf '%s' "$line" | awk '{print $2}')
- [ -n "$maxnew" ] || maxnew=8
- printf 'PP %s %s 200.0 2560.0\n' "$count" "$count"
- if [ "$KV" = "int8" ]; then decode_ms=50.0; else decode_ms=100.0; fi
- # One T per token: the harness cross-checks the stream against DONE.
- i=0
- while [ "$i" -lt "$maxnew" ]; do
- echo "T $i"
- i=$((i + 1))
- done
- printf 'DONE %s %s 200.0 %s stop 0 0\n' "$maxnew" "$count" "$decode_ms"
- ;;
- esac
-done
diff --git a/tests/tuning/test_tune_engine.py b/tests/tuning/test_tune_engine.py
index 1863a3d5a..342a46bb3 100644
--- a/tests/tuning/test_tune_engine.py
+++ b/tests/tuning/test_tune_engine.py
@@ -131,6 +131,12 @@ class TestMeasurements(unittest.TestCase):
"an unmeasurable prompt rate must be absent, not zero")
+STUB_PRELUDE = (
+ "import sys\n"
+ "def say(text):\n print(text, flush=True)\n"
+ "def wait():\n sys.stdin.readline()\n")
+
+
class TestEngineHarnessWithAStub(unittest.TestCase):
"""The harness against a stub that speaks the real protocol.
@@ -149,24 +155,24 @@ class TestEngineHarnessWithAStub(unittest.TestCase):
def stub(self, body: str) -> Path:
"""Write a stub engine that speaks the serve protocol.
- Each body is explicit about how it blocks: `read line` waits for the harness to send
- something, which keeps the process (and therefore the stdout pipe) alive without leaving a
- child behind. A shell `sleep` child would hold the pipe open after the shell itself exited,
- so the harness would block on a process it had already killed.
+ The stub is a Python script (the harness starts it as ``[sys.executable, stub.py]``, so it
+ runs the same on every OS; a ``.sh`` stub is WinError 193 on Windows). ``body`` is Python
+ using ``say(text)`` (print a line and flush) and ``wait()`` (block until the harness sends
+ a line). Each body is explicit about how it blocks: ``wait()`` keeps the process, and so the
+ stdout pipe, alive without leaving a child behind.
"""
- p = self.tmp / "stub-engine"
- p.write_text("#!/bin/sh\n" + body)
- p.chmod(p.stat().st_mode | stat.S_IEXEC)
+ p = self.tmp / "stub-engine.py"
+ p.write_text(STUB_PRELUDE + body)
return p
def test_a_good_response_becomes_a_measurement(self):
stub = self.stub(
- 'echo "READY 4096 stop"\n'
- 'read line\n'
- 'echo "PP 512 512 200.0 2560.0"\n'
- 'echo "T 1"\necho "T 2"\necho "T 3"\n'
- 'echo "DONE 3 512 200.0 30.0 stop 0 0"\n'
- 'read line\n')
+ 'say("READY 4096 stop")\n'
+ 'wait()\n'
+ 'say("PP 512 512 200.0 2560.0")\n'
+ 'say("T 1"); say("T 2"); say("T 3")\n'
+ 'say("DONE 3 512 200.0 30.0 stop 0 0")\n'
+ 'wait()\n')
h = te.EngineHarness(stub, {}, workload={"prompt_tokens": 8, "max_new": 3})
try:
h.start()
@@ -182,7 +188,7 @@ class TestEngineHarnessWithAStub(unittest.TestCase):
# The stub must block *in its own process* with stdout open. A shell `sleep` child would
# hold the pipe open after the shell exits, so the harness would keep waiting on a process
# that is already gone -- which made this test hang rather than fail.
- stub = self.stub('read line\n')
+ stub = self.stub('wait()\n')
h = te.EngineHarness(stub, {}, boot_timeout_s=1.0)
try:
with self.assertRaises(te.MeasurementError) as ctx:
@@ -194,7 +200,7 @@ class TestEngineHarnessWithAStub(unittest.TestCase):
def test_an_engine_that_exits_without_ready_is_reported_as_an_early_exit(self):
# It prints something and *then* dies without READY, which is the realistic shape: the
# engine got far enough to log, then failed.
- stub = self.stub('echo "loading weights"\nexit 0\n')
+ stub = self.stub('say("loading weights")\nsys.exit(0)\n')
h = te.EngineHarness(stub, {}, boot_timeout_s=10.0)
try:
with self.assertRaises(te.MeasurementError) as ctx:
@@ -204,7 +210,7 @@ class TestEngineHarnessWithAStub(unittest.TestCase):
h.close()
def test_an_engine_that_exits_immediately_reports_the_code(self):
- stub = self.stub('echo "starting up"\nexit 7\n')
+ stub = self.stub('say("starting up")\nsys.exit(7)\n')
h = te.EngineHarness(stub, {})
try:
with self.assertRaises(te.MeasurementError) as ctx:
@@ -214,7 +220,7 @@ class TestEngineHarnessWithAStub(unittest.TestCase):
h.close()
def test_an_error_line_from_the_engine_is_raised_verbatim(self):
- stub = self.stub('echo "READY 4096 stop"\nread line\necho "ERR out of memory"\nread line\n')
+ stub = self.stub('say("READY 4096 stop")\nwait()\nsay("ERR out of memory")\nwait()\n')
h = te.EngineHarness(stub, {}, workload={"prompt_tokens": 4, "max_new": 1},
request_timeout_s=3.0)
try:
@@ -225,6 +231,27 @@ class TestEngineHarnessWithAStub(unittest.TestCase):
finally:
h.close()
+ def test_the_shared_stub_engine_speaks_the_protocol_and_reacts_to_kv(self):
+ """tests/tuning/stub_engine.py is the engine every sweep test uses, on every OS."""
+ stub_path = Path(__file__).with_name("stub_engine.py")
+ speeds = {}
+ for kv in ("int8", "fp16"):
+ h = te.EngineHarness(stub_path, {"--kv": kv}, workload={"prompt_tokens": 8, "max_new": 4})
+ try:
+ h.start()
+ m = h.measure()
+ self.assertEqual(m.produced, 4)
+ speeds[kv] = m.decode_tokens_per_s
+ finally:
+ h.close()
+ self.assertAlmostEqual(speeds["int8"], 80.0) # 4 tokens in 50 ms
+ self.assertAlmostEqual(speeds["fp16"], 40.0) # 4 tokens in 100 ms
+
+ def test_a_py_engine_runs_through_the_current_interpreter(self):
+ from strata_control import tuning
+ self.assertEqual(tuning.engine_command(Path("x") / "stub.py"), [sys.executable, str(Path("x") / "stub.py")])
+ self.assertEqual(tuning.engine_command(Path("x") / "strata"), [str(Path("x") / "strata")])
+
def test_a_missing_binary_is_an_error_before_any_process_starts(self):
h = te.EngineHarness(self.tmp / "nope", {})
with self.assertRaises(te.MeasurementError) as ctx:
@@ -232,7 +259,7 @@ class TestEngineHarnessWithAStub(unittest.TestCase):
self.assertIn("not at", str(ctx.exception))
def test_the_declared_flags_are_passed_to_the_engine(self):
- stub = self.stub('echo "READY 4096 stop"\nread line\necho "DONE 1 1 1.0 1.0 stop 0 0"\nread line\n')
+ stub = self.stub('say("READY 4096 stop")\nwait()\nsay("DONE 1 1 1.0 1.0 stop 0 0")\nwait()\n')
h = te.EngineHarness(stub, {"--kv": "int8", "--kv-grow": "1"})
argv = h._argv()
self.assertIn("--kv", argv)
@@ -245,7 +272,7 @@ class TestEngineHarnessWithAStub(unittest.TestCase):
h.close()
def test_make_measure_returns_the_metric_dict_the_job_expects(self):
- stub = self.stub('echo "READY 4096 stop"\nread line\necho "T 1"\necho "DONE 1 16 8.0 4.0 stop 0 0"\nread line\n')
+ stub = self.stub('say("READY 4096 stop")\nwait()\nsay("T 1")\nsay("DONE 1 16 8.0 4.0 stop 0 0")\nwait()\n')
measure = te.make_measure(stub, workload={"prompt_tokens": 4, "max_new": 1})
class C:
diff --git a/tests/tuning/test_tuning.py b/tests/tuning/test_tuning.py
index 629a07ede..583fe466f 100644
--- a/tests/tuning/test_tuning.py
+++ b/tests/tuning/test_tuning.py
@@ -94,18 +94,16 @@ class TestDimensionValidation(TuningCase):
then excluded the sweep silently searches nothing — safe, but useless and very hard to
notice. This test uses a stand-in that prints usage to stderr only.
"""
- fake = self.tmp / "fake-engine"
- fake.write_text("#!/bin/sh\necho 'usage:' >&2\necho ' --kv KV' >&2\necho ' --ple-io MODE' >&2\n")
- fake.chmod(0o755)
+ fake = self.tmp / "fake-engine.py" # run as [sys.executable, fake]: works on every OS
+ fake.write_text("import sys\nfor line in ('usage:', ' --kv KV', ' --ple-io MODE'):\n print(line, file=sys.stderr)\n")
flags = tuning.engine_flag_inventory(fake)
self.assertIsNotNone(flags, "usage on stderr must still be read")
self.assertIn("--kv", flags)
self.assertIn("--ple-io", flags)
def test_flag_inventory_reads_stdout_too(self):
- fake = self.tmp / "fake-engine-stdout"
- fake.write_text("#!/bin/sh\necho ' --vram-reserve-mib MIB'\n")
- fake.chmod(0o755)
+ fake = self.tmp / "fake-engine-stdout.py"
+ fake.write_text("print(' --vram-reserve-mib MIB')\n")
self.assertIn("--vram-reserve-mib", tuning.engine_flag_inventory(fake))
def test_a_backend_with_dimensions_that_all_need_missing_flags_searches_nothing(self):