diff --git a/serve/server.py b/serve/server.py index ee6fd69f..329bc7ea 100644 --- a/serve/server.py +++ b/serve/server.py @@ -3667,6 +3667,40 @@ def deepseek_slot_env(env: dict, cfg: dict) -> None: env["STRATA_DS4_SLOT_DIR"] = str(Path(log).parent / "ds4-slots") +# 0.2.9: the exact parallel-request stack, on whenever the engine runs batch slots (--batch / "parallel" 2 or more). Every item +# was qualified bit-exact against the plain slots (QUAL-PAR: greedy identity at c=1, first-token KL 0): the checkpoint pool and the +# slot copies keep a request's running state without a stall, and the BATCH_* / QSA_* switches run all slots of a window in one +# launch. Together +16% sustained throughput at c=8 on two A6000 and +40..65% on one big card, no solo cost. An explicit +# environment variable (or the config's "env") still wins: =0 turns one off. +PARALLEL_STACK_ENV = { + "STRATA_CKPT_POOL": "1", "STRATA_CKPT_POOL_READY": "4", "STRATA_SLOT_CKPT_MOVE": "1", "STRATA_SLOT_COPY_DIRECT": "1", + "STRATA_SLOT_COPY_BATCHSYNC": "1", "STRATA_BATCH_GDN_GROUPS": "1", "STRATA_BATCH_ATTN_ROWS": "1", + "STRATA_BATCH_SELF_COMMIT": "1", "STRATA_QSA_SLOTS": "1", +} + + +def parallel_slots(cfg: dict) -> int: + """How many batch slots the config asks for (0 = none): the args' own --batch/--slots, else "parallel".""" + args = list(cfg.get("args") or []) + for k in ("--batch", "--slots"): + if k in args: + try: + return int(args[args.index(k) + 1]) + except (IndexError, ValueError): + return 0 + n = cfg.get("parallel") + return n if isinstance(n, int) and not isinstance(n, bool) and n > 1 else 0 + + +def parallel_stack_env(env: dict, cfg: dict) -> None: + """Switch the exact parallel stack on (setdefault: a value the user or the config set stays) when the config runs batch + slots on a CUDA or HIP Qwen engine. Other engines (DeepSeek, SYCL, Metal) do not read these.""" + if parallel_slots(cfg) < 2 or (cfg.get("arch") or "") == "deepseek41" or cfg.get("backend") in ("sycl", "metal", "vulkan"): + return + for k, v in PARALLEL_STACK_ENV.items(): + env.setdefault(k, v) + + def child_env(cfg: dict, *, offline: bool = False) -> dict: """The engine's environment: the CUDA libraries setup installed (pip's nvidia packages, or the toolkit that compiled it) first on the library search path.""" @@ -3683,6 +3717,7 @@ def child_env(cfg: dict, *, offline: bool = False) -> dict: env[str(k)] = str(v) if (cfg.get("arch") or "") == "deepseek41": # (only this engine reads these; Qwen's environment is untouched) deepseek_slot_env(env, cfg) + parallel_stack_env(env, cfg) # Strict offline reaches children through the environment, because a tool subprocess or a model # loader is code this server does not own. Applied last so a profile's own `env` cannot remove # the policy by naming the same variable. diff --git a/serve/test_parallel.py b/serve/test_parallel.py index 627cdc98..1bd2fdae 100644 --- a/serve/test_parallel.py +++ b/serve/test_parallel.py @@ -16,6 +16,7 @@ from pathlib import Path from unittest import mock from serve.frontend import ChatTemplate +from serve import server from serve.server import ByteTokenizer, Service, StrataEngine, engine_args, parallel_args, serve from strata_cli import bench from strata_cli.client import Endpoint @@ -808,5 +809,37 @@ class ParallelService(FakeEngineCase): self.assertTrue(all(a <= 1 for a in active), active) + +class ParallelStackDefaults(unittest.TestCase): + """The exact parallel stack is the default with batch slots; an explicit value wins.""" + def env_for(self, cfg, base=None): + import os + from unittest import mock + with mock.patch.dict(os.environ, base or {}, clear=False): + for k in list(os.environ): + if k in server.PARALLEL_STACK_ENV and not (base and k in base): + del os.environ[k] + return server.child_env(cfg) + + def test_on_with_parallel(self): + env = self.env_for({"parallel": 8, "args": []}) + for k, v in server.PARALLEL_STACK_ENV.items(): + self.assertEqual(env.get(k), v, k) + self.assertEqual(self.env_for({"args": ["--batch", "4"]}).get("STRATA_BATCH_SELF_COMMIT"), "1") + + def test_off_without_slots(self): + self.assertNotIn("STRATA_CKPT_POOL", self.env_for({"args": []})) + self.assertNotIn("STRATA_CKPT_POOL", self.env_for({"parallel": 1, "args": []})) + + def test_explicit_wins(self): + env = self.env_for({"parallel": 8, "args": [], "env": {"STRATA_BATCH_GDN_GROUPS": "0"}}, {"STRATA_CKPT_POOL": "0"}) + self.assertEqual(env["STRATA_BATCH_GDN_GROUPS"], "0") + self.assertEqual(env["STRATA_CKPT_POOL"], "0") + self.assertEqual(env["STRATA_QSA_SLOTS"], "1") + + def test_other_engines_untouched(self): + self.assertNotIn("STRATA_CKPT_POOL", self.env_for({"parallel": 8, "args": [], "arch": "deepseek41"})) + self.assertNotIn("STRATA_CKPT_POOL", self.env_for({"parallel": 8, "args": [], "backend": "sycl"})) + if __name__ == "__main__": unittest.main()