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

serve: card order on AMD/Linux too (KFD topology: SIMDs x clock)

Niko1221786b3ca01641
diff --git a/serve/server.py b/serve/server.py
index c67d5ef97..89eeaa0d5 100644
--- a/serve/server.py
+++ b/serve/server.py
@@ -2151,7 +2151,7 @@ def hip_visible(cfg: dict) -> list[int]:
return [int(str(ordinal).strip())]
except ValueError:
pass
- return gpu_list(cfg)
+ return ordered_gpus(cfg)
def gpu_speed_scores(indices: list[int]) -> dict[int, float] | None:
@@ -2185,6 +2185,26 @@ def gpu_speed_scores(indices: list[int]) -> dict[int, float] | None:
return got if all(i in got and got[i] > 0 for i in indices) else None
+def hip_speed_scores(indices: list[int], root: str = "/sys/class/kfd/kfd/topology/nodes") -> dict[int, float] | None:
+ """AMD on Linux: SIMDs x max engine clock of each card, from the KFD topology (the order setup and the engine use
+ for "gpu" there: the GPU nodes in node order). None when the files are not there (Windows, no ROCm driver)."""
+ try:
+ nodes = sorted((int(n) for n in os.listdir(root) if n.isdigit()))
+ except OSError:
+ return None
+ cards = []
+ for n in nodes:
+ try:
+ props = dict(line.split() for line in open(os.path.join(root, str(n), "properties")) if len(line.split()) == 2)
+ simd, clk = int(props.get("simd_count", 0)), int(props.get("max_engine_clk_fcompute", 0))
+ except (OSError, ValueError):
+ continue
+ if simd > 0:
+ cards.append(float(simd) * float(clk or 1))
+ got = dict(enumerate(cards))
+ return got if all(i in got for i in indices) else None
+
+
def ordered_gpus(cfg: dict, scores=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
@@ -2194,12 +2214,13 @@ def ordered_gpus(cfg: dict, scores=None) -> list[int]:
manual "layer_split" ("24") also keeps it: the user placed the layers. scores: {index: score} (tests); None asks
the driver."""
gl = gpu_list(cfg)
- if len(gl) < 2 or cfg.get("gpu_order") == "as_given" or cfg.get("backend") == "hip":
+ if len(gl) < 2 or cfg.get("gpu_order") == "as_given":
return gl
args = cfg.get("args") or []
if "--peer-device" in args or "--layer-split" in args or layer_split_value(cfg) != "auto":
return gl
- sc = scores if scores is not None else gpu_speed_scores(gl)
+ hip = cfg.get("backend") == "hip"
+ sc = scores if scores is not None else (hip_speed_scores(gl) if hip else gpu_speed_scores(gl))
if not sc or any(i not in sc for i in gl):
return gl
out = sorted(gl, key=lambda i: sc[i])
diff --git a/serve/test_server.py b/serve/test_server.py
index f415baccc..362fbf6ad 100644
--- a/serve/test_server.py
+++ b/serve/test_server.py
@@ -1598,7 +1598,7 @@ class GpuChoice(unittest.TestCase):
def test_card_order(self):
"""#1352: with an auto layer split the faster card (SMs x clock) goes last; ties, a manual split, a
measurement we lack, HIP and "gpu_order": "as_given" keep the config's order."""
- from serve.server import child_env, ordered_gpus
+ from serve.server import child_env, hip_speed_scores, ordered_gpus
fast_first = {0: 8448.0 * 2610, 2: 4608.0 * 2575} # 4070 Ti SUPER (66 SMs) vs 5060 Ti (36 SMs), shaped
base = {"gpu": [0, 2], "args": []}
self.assertEqual(ordered_gpus(base, fast_first), [2, 0])
@@ -1607,12 +1607,30 @@ class GpuChoice(unittest.TestCase):
self.assertEqual(ordered_gpus({**base, "gpu_order": "as_given"}, fast_first), [0, 2])
self.assertEqual(ordered_gpus({**base, "layer_split": "24"}, fast_first), [0, 2])
self.assertEqual(ordered_gpus({**base, "args": ["--layer-split", "24"]}, fast_first), [0, 2])
- self.assertEqual(ordered_gpus({**base, "backend": "hip"}, fast_first), [0, 2])
+ self.assertEqual(ordered_gpus({**base, "backend": "hip"}, fast_first), [2, 0]) # AMD: same rule
+ self.assertEqual(ordered_gpus({**base, "backend": "hip"}, {0: 7.0, 2: 7.0}), [0, 2]) # identical cards (the R9700s)
self.assertEqual(ordered_gpus(base, {0: 1.0}), [0, 2]) # a card unmeasured
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])
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"))
+
+ def test_kfd_scores(self):
+ """AMD on Linux: the GPU nodes of the KFD topology, SIMDs x clock, node order = the config's index."""
+ import tempfile
+ from serve.server import hip_speed_scores
+ with tempfile.TemporaryDirectory() as d:
+ for n, simd, clk in ((0, 0, 0), (1, 128, 2350), (2, 64, 2000), (3, 128, 2350)):
+ os.makedirs(os.path.join(d, str(n)))
+ with open(os.path.join(d, str(n), "properties"), "w") as f:
+ f.write(f"simd_count {simd}
+max_engine_clk_fcompute {clk}
+gfx_target_version 120001
+")
+ sc = hip_speed_scores([0, 1, 2], root=d)
+ self.assertEqual(sc, {0: 128 * 2350.0, 1: 64 * 2000.0, 2: 128 * 2350.0})
+ self.assertIsNone(hip_speed_scores([0, 1, 2, 3], root=d)) # a card the topology does not have
def test_vision_device(self):
# #408: the image encoder on its own card; the engine's environment stays as it was