From efe3bfcf873d3c5c0be230d6d1f7aaa8cca01214 Mon Sep 17 00:00:00 2001 From: nekomario28 <206304251+nekomario28@users.noreply.github.com> Date: Fri, 9 Oct 2026 01:23:30 +0900 Subject: [PATCH] serve: keep the given card order when the reorder starves the last stage (#1576) #1352 orders an auto layer split faster-card-last because the last stage runs the head, the draft layer and the verify. On a VRAM-asymmetric pair that rule can put the heaviest stage on the card least able to hold it: the reporter's 12 GB 4070 Ti ended up with 31 layers + head + verify and a 3 GiB expert cache (decode ~58 -> ~44 tok/s, the 'card this small' warning on). Skip the reorder when it would put the last stage on a card with less total VRAM than the one the config placed there (NVML / amdgpu sysfs; unreadable cards keep the reorder). scores/vrams stay injectable for tests. --- serve/server.py | 32 +++++++++++++++++++++++++++++--- serve/test_server.py | 10 ++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/serve/server.py b/serve/server.py index 553f440c..cd5eee62 100644 --- a/serve/server.py +++ b/serve/server.py @@ -2324,14 +2324,34 @@ def hip_speed_scores(indices: list[int], root: str = "/sys/class/kfd/kfd/topolog return got if all(i in got for i in indices) else None -def ordered_gpus(cfg: dict, scores=None) -> list[int]: +def gpu_vram_mib(indices: list[int], hip: bool = False) -> dict[int, int] | None: + """Total VRAM of each card in MiB (NVML / amdgpu sysfs); None when any of them cannot be read.""" + try: + from serve.telemetry import gpu_reader + except ImportError: + return None + out = {} + for i in indices: + g = gpu_reader(i, amd=hip) + if not g.ok(): + return None + total = g.read().get("mem_total") + if total is None: + return None + out[i] = int(total) >> 20 + return out + + +def ordered_gpus(cfg: dict, scores=None, vrams=None) -> list[int]: """#1352: the cards of a layer split in the order the engine stages them. With "layer_split": "auto" (the default) the faster card goes LAST - the last stage runs the head, the draft layer and the verify, and a prompt chunk waits on it (the reporter's 4070 Ti SUPER + 5060 Ti: a 6K prompt took 50 s one way round and 15 s the other); "faster" is multiprocessors x max clock. Equal cards keep the config's order (the sort is stable), and so does anything we cannot measure. "gpu_order": "as_given" keeps the config's order whatever the cards. A - manual "layer_split" ("24") also keeps it: the user placed the layers. scores: {index: score} (tests); None asks - the driver.""" + manual "layer_split" ("24") also keeps it: the user placed the layers. #1576: the reorder is skipped when it + would land the last stage on a card with less total VRAM than the one the config put there - that stage's head, + draft and verify have a floor the smaller card cannot afford. scores/vrams: {index: value} (tests); None asks + the driver - injected scores without injected vrams means no VRAM check (a test seam is a test seam).""" gl = gpu_list(cfg) if len(gl) < 2 or cfg.get("gpu_order") == "as_given": return gl @@ -2344,6 +2364,12 @@ def ordered_gpus(cfg: dict, scores=None) -> list[int]: return gl out = sorted(gl, key=lambda i: sc[i]) if out != gl: + vram = vrams if vrams is not None else (None if scores is not None else gpu_vram_mib(gl, hip)) + if vram is not None and all(i in vram for i in gl) and vram[out[-1]] < vram[gl[-1]]: + print(f"[strata] layer split: keeping card order {','.join(map(str, gl))} - the faster card has " + f"less VRAM than the one the split ends on ({vram[out[-1]]} vs {vram[gl[-1]]} MiB, #1576)", + flush=True) + return gl print(f"[strata] layer split: card order {','.join(map(str, out))} (the faster card last; " f'"gpu_order": "as_given" keeps {",".join(map(str, gl))}, #1352)', flush=True) return out diff --git a/serve/test_server.py b/serve/test_server.py index 60e909e8..eb55dac7 100644 --- a/serve/test_server.py +++ b/serve/test_server.py @@ -1632,6 +1632,16 @@ class GpuChoice(unittest.TestCase): self.assertEqual(ordered_gpus({"gpu": 1, "args": []}, {1: 1.0}), [1]) self.assertEqual(ordered_gpus({"gpu": [0, 1, 2], "args": []}, {0: 3.0, 1: 9.0, 2: 6.0}), [0, 2, 1]) self.assertEqual(ordered_gpus({"gpu": [0, 1, 2], "args": []}, {0: 3.0, 1: 3.0, 2: 3.0}), [0, 1, 2]) + # #1576: the reorder is skipped when it would land the last stage (head + draft + verify + cache) on a card + # with less VRAM than the config's own last card - the reporter's fast 12 GB / slow 22 GB pair + fast_small = {"gpu": [0, 1], "args": []} + speeds = {0: 8448.0 * 2610, 1: 4352.0 * 1665} # 4070 Ti vs 2080 Ti + self.assertEqual(ordered_gpus(fast_small, speeds, {0: 12288, 1: 22528}), [0, 1]) # keep: tail would shrink + self.assertEqual(ordered_gpus(fast_small, speeds, {0: 24576, 1: 12288}), [1, 0]) # faster AND bigger: reorder + self.assertEqual(ordered_gpus(fast_small, speeds, {0: 16384, 1: 16384}), [1, 0]) # equal VRAM: reorder + self.assertEqual(ordered_gpus(fast_small, speeds, {0: 12288}), [1, 0]) # a card unmeasured: reorder + self.assertEqual(ordered_gpus({"gpu": [0, 1, 2], "args": []}, {0: 3.0, 1: 9.0, 2: 6.0}, + {0: 8192, 1: 12288, 2: 24576}), [0, 1, 2]) # tail 1 (12G) < given tail 2 (24G) self.assertEqual(child_env({"gpu": [0, 2], "args": [], "gpu_order": "as_given"})["CUDA_VISIBLE_DEVICES"], "0,2") self.assertIsNone(hip_speed_scores([0], root="/nonexistent/kfd"))