mirror of
https://github.com/sous-labs/songe.git
synced 2026-12-01 12:47:14 -10:00
Until the Deep Dream panel lets people choose, nothing changes for anyone who does not ask: one GPU trained on and watched, as before. The release check takes SONGE_CHECK_MODEL / _MINUTES / _MEMORY_FRACTION for manual multi-GPU runs, installs flash-linear-attention for them, and logs its result. Co-Authored-By: Claude Opus 5.5 <[email protected]>
108 lines
3.9 KiB
Python
108 lines
3.9 KiB
Python
"""Training on several GPUs: which ones, and the guard watching each."""
|
|
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from songe.memory import TrainingPlatform, chosen_gpus, snapshot
|
|
from songe.platform import Accel, Memory
|
|
|
|
UUIDS = ["GPU-aaaa-1111", "GPU-bbbb-2222", "GPU-cccc-3333"]
|
|
|
|
|
|
def uuid_of(i):
|
|
return UUIDS[i].removeprefix("GPU-") # torch writes UUIDs without the prefix
|
|
|
|
|
|
def test_all_is_every_visible_gpu_in_order_and_first_the_default():
|
|
assert chosen_gpus("all", 3, uuid_of) == [0, 1, 2]
|
|
assert chosen_gpus("first", 3, uuid_of) == [0]
|
|
assert chosen_gpus(None, 2, uuid_of) == [0]
|
|
|
|
|
|
def test_positions_and_uuids_name_the_same_gpus():
|
|
assert chosen_gpus([2, "0"], 3, uuid_of) == [2, 0]
|
|
assert chosen_gpus(["GPU-bbbb-2222"], 3, uuid_of) == [1]
|
|
assert chosen_gpus(["CCCC-3333", 2], 3, uuid_of) == [2] # once, any spelling
|
|
|
|
|
|
@pytest.mark.parametrize("gpus", [[3], ["GPU-dddd"], []])
|
|
def test_a_gpu_that_is_not_there_is_refused(gpus):
|
|
with pytest.raises(OSError):
|
|
chosen_gpus(gpus, 3, uuid_of)
|
|
|
|
|
|
@pytest.fixture
|
|
def three_cards(monkeypatch):
|
|
monkeypatch.delenv("CUDA_VISIBLE_DEVICES", raising=False)
|
|
devices = [Accel("cuda", "RTX 5090", 1000, 900 - 100 * i, UUIDS[i]) for i in range(3)]
|
|
platform = SimpleNamespace(
|
|
name="linux", accelerators=lambda: devices, memory=lambda: Memory(1000, 900)
|
|
)
|
|
return devices, platform
|
|
|
|
|
|
def test_by_default_one_gpu_is_trained_on_and_watched(config, three_cards):
|
|
devices, platform = three_cards
|
|
assert snapshot(TrainingPlatform(platform, config)).vram == (devices[0],)
|
|
|
|
|
|
def test_all_trains_on_and_watches_every_gpu(config, three_cards):
|
|
devices, platform = three_cards
|
|
config.torch_options = {"gpus": "all"}
|
|
chosen = TrainingPlatform(platform, config)
|
|
assert chosen.indices == [0, 1, 2]
|
|
assert snapshot(chosen).vram == tuple(devices)
|
|
|
|
|
|
def test_the_chosen_gpus_are_the_ones_watched(config, three_cards):
|
|
devices, platform = three_cards
|
|
config.torch_options = {"gpus": ["GPU-cccc-3333", 0]}
|
|
chosen = TrainingPlatform(platform, config)
|
|
assert chosen.indices == [2, 0]
|
|
assert snapshot(chosen).vram == (devices[2], devices[0])
|
|
devices.pop() # a watched GPU's readings disappear: stop, never guess
|
|
with pytest.raises(OSError, match="telemetry"):
|
|
snapshot(chosen)
|
|
|
|
|
|
def test_an_explicit_device_is_still_one_gpu(config, three_cards):
|
|
devices, platform = three_cards
|
|
config.torch_options = {"device": "cuda:1"}
|
|
assert snapshot(TrainingPlatform(platform, config)).vram == (devices[1],)
|
|
|
|
|
|
def test_positions_follow_the_visible_list(config, three_cards, monkeypatch):
|
|
"""With CUDA_VISIBLE_DEVICES torch numbers only the listed cards."""
|
|
devices, platform = three_cards
|
|
monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "2,GPU-aaaa")
|
|
config.torch_options = {"gpus": "all"}
|
|
chosen = TrainingPlatform(platform, config)
|
|
assert chosen.indices == [2, 0]
|
|
config.torch_options = {"gpus": [1]}
|
|
assert TrainingPlatform(platform, config).indices == [0]
|
|
|
|
|
|
def test_select_devices_resolves_like_the_guard():
|
|
from songe.backends.torch import TorchOptions, select_devices
|
|
|
|
class Device(SimpleNamespace):
|
|
pass
|
|
|
|
cuda = SimpleNamespace(
|
|
is_available=lambda: True,
|
|
device_count=lambda: 3,
|
|
get_device_properties=lambda i: SimpleNamespace(uuid=uuid_of(i)),
|
|
)
|
|
torch = SimpleNamespace(
|
|
cuda=cuda,
|
|
device=lambda kind, index=None: Device(type=kind, index=index),
|
|
backends=SimpleNamespace(mps=SimpleNamespace(is_available=lambda: False)),
|
|
)
|
|
devices = select_devices(torch, TorchOptions(gpus=["GPU-cccc-3333", 0]))
|
|
assert [(d.type, d.index) for d in devices] == [("cuda", 2), ("cuda", 0)]
|
|
assert len(select_devices(torch, TorchOptions(gpus="all"))) == 3
|
|
assert [d.index for d in select_devices(torch, TorchOptions())] == [0]
|
|
one = select_devices(torch, TorchOptions(device="cuda:1"))
|
|
assert [d.type for d in one] == ["cuda:1"] # passed through as asked
|