diff --git a/setup.py b/setup.py index 8dcbab1d..c6450333 100644 --- a/setup.py +++ b/setup.py @@ -671,6 +671,41 @@ def gpu_drives_display(g) -> bool: return text.strip().lower() == "enabled" +def pcie_link(index: int) -> dict | None: + """The NVIDIA card's PCIe link: {"gen": the generation card and board both run (an idle card drops to a lower + one, so the current generation is not asked), "gpu_gen", "host_gen", "width", "max_width"}; None when nvidia-smi + does not say.""" + s = out(["nvidia-smi", "-i", str(index), "--query-gpu=pcie.link.gen.max,pcie.link.gen.gpumax,pcie.link.gen.hostmax," + "pcie.link.width.current,pcie.link.width.max", "--format=csv,noheader,nounits"]) + try: + gen, gpu_gen, host_gen, width, max_width = (int(x.strip()) for x in s.strip().splitlines()[0].split(",")) + except (ValueError, IndexError): + return None + return {"gen": gen, "gpu_gen": gpu_gen, "host_gen": host_gen, "width": width, "max_width": max_width} + + +PCIE_GBPS = {1: 0.25, 2: 0.5, 3: 0.985, 4: 1.97, 5: 3.94} # GB/s per lane, each direction + + +def pcie_lines(link: dict) -> tuple[str, str | None]: + """(the ok line, a warning or None) for step 1. The copy rate the engine measures is about 75% of the link's.""" + lim = [] + if link["gpu_gen"] > link["gen"]: + lim.append(f"the card supports {link['gpu_gen']}.0, the board {link['host_gen']}.0") + line = f"PCIe: {link['gen']}.0 x{link['width']}" + (f" ({'; '.join(lim)})" if lim else "") + rate = PCIE_GBPS.get(link["gen"], 0) * link["width"] + if 0 < rate < 12: + line += (f" - up to ~{rate:.0f} GB/s to the GPU: long prompts are read slower than on a PCIe 4.0 x16 PC (their " + "experts are copied over it); the engine measures the link at start for the output speed") + warn_line = None + if link["width"] < link["max_width"]: + # (#912) the width is read now, and some cards and laptops narrow the link while idle: a hint, not a verdict + warn_line = (f"the GPU's PCIe link reads {link['width']} of its {link['max_width']} lanes right now (some cards " + "narrow it when idle); if it stays narrow under load: is it in the right slot (the one wired " + "x16), fully seated, and not sharing lanes with an M.2 drive? (the BIOS can say)") + return line, warn_line + + GPU_PICK = None # --gpu N (issue #51); None: the card with the most VRAM SPLIT_MIN_VRAM_GB = 8 # a card sharing a model holds the dense weights and its own # prompt buffers too (docs/MULTI_GPU.md) @@ -4354,6 +4389,12 @@ def main() -> int: "the model may not start or may use less VRAM. Set it to \"System managed\": System > About > " "Advanced system settings > Performance > Advanced > Virtual memory") ok(f"CPU: {cpu} ({'AVX-512' if avx512 else 'AVX2' if avx2 else 'no AVX2'})") + link = None if hip else pcie_link(int(gpu.get("index", 0))) + if link is not None: + line, problem = pcie_lines(link) + ok(line) + if problem: + warn(problem) floor = cpu_floor(avx2) if floor == "unsupported": fail("this CPU has neither AVX2 nor SSE4.2; Strata needs at least SSE4.2 (Intel Nehalem, 2008, or newer)") diff --git a/tools/test_setup_pcie.py b/tools/test_setup_pcie.py new file mode 100644 index 00000000..a412fab3 --- /dev/null +++ b/tools/test_setup_pcie.py @@ -0,0 +1,44 @@ +"""Step 1's PCIe line: the generation card and board both run, the lanes, and a warning when the card runs on fewer +lanes than it has. No GPU needed: nvidia-smi's answer is given. + + python -m unittest tools.test_setup_pcie +""" +from __future__ import annotations + +import sys +import unittest +from pathlib import Path +from unittest import mock + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT)) +import setup # noqa: E402 + + +class Link(unittest.TestCase): + def test_parse(self): + with mock.patch.object(setup, "out", return_value="3, 4, 3, 8, 8\n"): + self.assertEqual(setup.pcie_link(0), {"gen": 3, "gpu_gen": 4, "host_gen": 3, "width": 8, "max_width": 8}) + for bad in ("", "[N/A], 4, 3, 8, 8\n", "garbage"): + with mock.patch.object(setup, "out", return_value=bad): + self.assertIsNone(setup.pcie_link(0), bad) + + def test_board_limit_and_slow_link(self): # llmserver: RTX 4060 Ti (4.0 x8) on an X99 board (3.0) + line, warn = setup.pcie_lines({"gen": 3, "gpu_gen": 4, "host_gen": 3, "width": 8, "max_width": 8}) + self.assertIn("PCIe: 3.0 x8 (the card supports 4.0, the board 3.0)", line) + self.assertIn("~8 GB/s", line) + self.assertIsNone(warn) + + def test_fast_link_says_nothing_more(self): + line, warn = setup.pcie_lines({"gen": 4, "gpu_gen": 4, "host_gen": 5, "width": 16, "max_width": 16}) + self.assertEqual(line, "PCIe: 4.0 x16") + self.assertIsNone(warn) + + def test_fewer_lanes_warns(self): + _, warn = setup.pcie_lines({"gen": 3, "gpu_gen": 3, "host_gen": 3, "width": 4, "max_width": 16}) + self.assertIn("reads 4 of its 16 lanes right now", warn) + self.assertIn("narrow it when idle", warn) # the width is read at idle: a hint, not a verdict + + +if __name__ == "__main__": + unittest.main()