Files
Spellcast3d/code/tests/test_graphics_card.py

138 lines
8.1 KiB
Python

"""A PC with several NVIDIA cards: which one the tools use (gpu.pick), how they are shown it (gpu.tools_env, through
paths.clean_env) and the config file's graphics_card line."""
from __future__ import annotations
import pytest
from spellcast import config, gpu, paths
from spellcast.server import config_changed
LAPTOP = [gpu.GpuInfo(0, "NVIDIA GeForce RTX 2060", 7.5, 6144, 0, "621.24", "GPU-2060aaaa"),
gpu.GpuInfo(1, "NVIDIA GeForce RTX 4080", 8.9, 16376, 656, "621.24", "GPU-4080bbbb")]
TWINS = [gpu.GpuInfo(0, "NVIDIA GeForce GTX 1070", 6.1, 8192, 0, "580.1", "GPU-a"),
gpu.GpuInfo(1, "NVIDIA GeForce GTX 1070", 6.1, 8192, 0, "580.1", "GPU-b")]
def _card(index: int, name: str, compute_cap: float, vram_mb: int) -> gpu.GpuInfo:
return gpu.GpuInfo(index, name, compute_cap, vram_mb, 0, "580.1", f"GPU-{index}")
@pytest.fixture
def cards(monkeypatch: pytest.MonkeyPatch):
"""Sets the PC's cards, the config file's wish and the artist's own CUDA_VISIBLE_DEVICES, as at a new start."""
def give(gpus: list[gpu.GpuInfo], wanted: str = gpu.AUTO, own: str | None = None) -> None:
monkeypatch.setattr(gpu, "query_gpus", lambda timeout=10.0: list(gpus))
monkeypatch.setattr(config, "graphics_card", lambda: wanted)
if own is None:
monkeypatch.delenv("CUDA_VISIBLE_DEVICES", raising=False)
else:
monkeypatch.setenv("CUDA_VISIBLE_DEVICES", own)
monkeypatch.delenv(gpu.HANDED_DOWN, raising=False)
monkeypatch.setattr(gpu, "_decided", None)
return give
def test_the_card_with_the_most_video_memory_is_used_and_card_0_among_equals(cards) -> None:
assert gpu.AUTO == config.GRAPHICS_CARD_AUTO
cards(LAPTOP)
assert gpu.primary_gpu() is not None and gpu.primary_gpu().name.endswith("4080")
assert gpu.tools_env() == {"CUDA_VISIBLE_DEVICES": "GPU-4080bbbb", gpu.HANDED_DOWN: "GPU-4080bbbb"}, \
"by UUID: CUDA and nvidia-smi number cards differently"
assert paths.clean_env()["CUDA_VISIBLE_DEVICES"] == "GPU-4080bbbb", "every tool and installer step is shown that card"
assert paths.clean_env(extra={"CUDA_VISIBLE_DEVICES": "-1"})["CUDA_VISIBLE_DEVICES"] == "-1", "a step without a card"
cards(TWINS)
assert gpu.primary_gpu().index == 0 and gpu.tools_env()["CUDA_VISIBLE_DEVICES"] == "GPU-a"
def test_the_best_card_is_not_always_the_one_with_the_most_memory(cards) -> None:
"""A card no tool runs on, however big, comes last; then a card with the 8 GB Spellcast3D is made for; then an
RTX before a GTX 10xx (every stage in fp32 there: several times slower)."""
old_tesla, rtx_3060 = _card(0, "Tesla M40 24GB", 5.2, 24576), _card(1, "NVIDIA GeForce RTX 3060", 8.6, 12288)
assert gpu.pick([old_tesla, rtx_3060]) is rtx_3060
gtx_1080_ti, rtx_4060 = _card(0, "NVIDIA GeForce GTX 1080 Ti", 6.1, 11264), _card(1, "NVIDIA GeForce RTX 4060", 8.9, 8188)
assert gpu.pick([gtx_1080_ti, rtx_4060]) is rtx_4060
rtx_2060 = _card(1, "NVIDIA GeForce RTX 2060", 7.5, 6144)
assert gpu.pick([gtx_1080_ti, rtx_2060]) is gtx_1080_ti, "8 GB or more before an RTX with less"
assert gpu.pick([rtx_2060, _card(2, "NVIDIA GeForce RTX 3050", 8.6, 6144)]).index == 2, "of equals, the newer"
assert gpu.pick([]) is None
def test_a_pc_with_one_card_or_none_is_left_as_it_was(cards) -> None:
cards(LAPTOP[:1])
assert gpu.primary_gpu().index == 0 and gpu.tools_env() == {}
assert "CUDA_VISIBLE_DEVICES" not in paths.clean_env()
cards([])
assert gpu.primary_gpu() is None and gpu.tools_env() == {}
def test_the_config_file_numbers_another_card(cards) -> None:
cards(LAPTOP, wanted="0")
assert gpu.primary_gpu().name.endswith("2060") and gpu.tools_env()["CUDA_VISIBLE_DEVICES"] == "GPU-2060aaaa"
cards(LAPTOP, wanted="5") # a card that is not there: as auto
assert gpu.primary_gpu().name.endswith("4080")
def test_the_card_is_picked_once_a_start(cards, monkeypatch: pytest.MonkeyPatch) -> None:
"""The card a tool was started on, the page's card and the memory rules are one card: the config file saved with
another number while Spellcast3D runs moves nothing until the next start (the page asks for one)."""
cards([]) # nvidia-smi does not answer yet: nothing is decided
assert gpu.tools_env() == {} and gpu.primary_gpu() is None
monkeypatch.setattr(gpu, "query_gpus", lambda timeout=10.0: list(LAPTOP))
assert gpu.primary_gpu().name.endswith("4080")
monkeypatch.setattr(config, "graphics_card", lambda: "0")
assert gpu.primary_gpu().name.endswith("4080") and gpu.tools_env()["CUDA_VISIBLE_DEVICES"] == "GPU-4080bbbb"
monkeypatch.setattr(gpu, "query_gpus", lambda timeout=10.0: LAPTOP[:1]) # the picked card is gone
assert gpu.primary_gpu() is None and gpu.tools_env()["CUDA_VISIBLE_DEVICES"] == "GPU-4080bbbb"
template = paths.CONFIG_TEMPLATE.read_text(encoding="utf-8")
auto, first = config.parse(template), config.parse(template.replace("graphics_card = auto", "graphics_card = 0"))
said = config_changed(auto, first)
assert "now picks another graphics card (graphics_card = 0)" in said and "start it again" in said
bigger = config.parse(template.replace("graphics_card = auto", "graphics_card = 0")
.replace("quality = Q4_K_M", "quality = Q8_0"))
assert "model size Q8_0" in config_changed(auto, bigger), "with another change, that one's words (one new start)"
def test_a_program_the_hub_starts_keeps_the_hubs_card(cards, monkeypatch: pytest.MonkeyPatch) -> None:
"""The installer the hub starts finds the hub's pick in its environment. It is no setting of the artist's own
(the system check must not say so), and it stands whatever the config file says by then."""
cards(LAPTOP)
handed = paths.clean_env()
assert handed["CUDA_VISIBLE_DEVICES"] == handed[gpu.HANDED_DOWN] == "GPU-4080bbbb"
cards(LAPTOP, wanted="0", own=handed["CUDA_VISIBLE_DEVICES"]) # the started program: a process of its own
monkeypatch.setenv(gpu.HANDED_DOWN, handed[gpu.HANDED_DOWN])
assert gpu.own_devices() == "" and gpu.primary_gpu().name.endswith("4080")
assert gpu.tools_env() == {"CUDA_VISIBLE_DEVICES": "GPU-4080bbbb", gpu.HANDED_DOWN: "GPU-4080bbbb"}
# a CUDA_VISIBLE_DEVICES that is not that pick is the artist's (theirs was there before the hub started)
monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "0")
monkeypatch.setattr(gpu, "_decided", None)
assert gpu.own_devices() == "0" and gpu.primary_gpu().name.endswith("2060")
def test_the_artists_own_cuda_visible_devices_stands(cards) -> None:
cards(LAPTOP, own="0")
assert gpu.primary_gpu().name.endswith("2060")
assert gpu.tools_env() == {"CUDA_DEVICE_ORDER": "PCI_BUS_ID"}, "their number is read in nvidia-smi's order"
assert paths.clean_env()["CUDA_VISIBLE_DEVICES"] == "0"
# a UUID as CUDA takes it: any case, or its beginning
for own in ("GPU-2060aaaa", "gpu-2060AAAA", "GPU-2060"):
cards(LAPTOP, own=own)
assert gpu.primary_gpu().name.endswith("2060"), own
# several: the first is the tools' cuda:0
cards(LAPTOP, own="1,0")
assert gpu.primary_gpu().name.endswith("4080") and paths.clean_env()["CUDA_VISIBLE_DEVICES"] == "1,0"
cards(LAPTOP, own="GPU-2060aaaa, GPU-4080bbbb")
assert gpu.primary_gpu().name.endswith("2060")
def test_the_graphics_card_line_of_the_config_file() -> None:
assert config.parse("backend = qwen\n").graphics_card == "auto"
assert config.parse("backend = qwen\ngraphics_card = 1\n").graphics_card == "1"
assert config.parse("backend = qwen\ngraphics_card = AUTO ; the default\n").graphics_card == "auto"
wrong = config.parse("backend = qwen\ngraphics_card = the fast one\n")
assert wrong.graphics_card == "auto" and wrong.backend == "qwen" and "neither auto nor" in wrong.problems[0]
twice = config.parse("backend = qwen\ngraphics_card = 0\ngraphics_card = 1\n")
assert twice.graphics_card == "auto" and "more than one graphics_card line" in twice.problems[0]
assert "graphics_card = auto" in paths.CONFIG_TEMPLATE.read_text(encoding="utf-8")
assert config.parse(paths.CONFIG_TEMPLATE.read_text(encoding="utf-8")).problems == ()