import os
from modelexpress import ucx_utils
def test_nic_access_requires_exposed_uverbs_device(monkeypatch):
monkeypatch.setattr(
ucx_utils.os,
"listdir",
lambda path: ["uverbs4"] if path.endswith("infiniband_verbs") else [],
)
monkeypatch.setattr(
ucx_utils.os.path,
"exists",
lambda path: path == "/dev/infiniband/uverbs4",
)
assert ucx_utils._nic_has_accessible_verbs_device("mlx5_4")
def test_nic_access_rejects_host_only_uverbs_device(monkeypatch):
monkeypatch.setattr(
ucx_utils.os,
"listdir",
lambda path: ["uverbs0"] if path.endswith("infiniband_verbs") else [],
)
monkeypatch.setattr(ucx_utils.os.path, "exists", lambda path: False)
assert not ucx_utils._nic_has_accessible_verbs_device("mlx5_0")
def test_flat_pci_topology_scores_local_phb_above_remote_sys(monkeypatch):
paths = {
"0002:00:01.0": "/sys/devices/pci0002:00/0002:00:01.0",
"0002:00:09.0": "/sys/devices/pci0002:00/0002:00:09.0",
"0003:00:09.0": "/sys/devices/pci0003:00/0003:00:09.0",
}
monkeypatch.setattr(
ucx_utils.os.path,
"realpath",
lambda path: paths[path.rsplit("/", 1)[-1]],
)
gpu_path = ucx_utils._pci_path_components("0002:00:01.0")
local_nic_path = ucx_utils._pci_path_components("0002:00:09.0")
remote_nic_path = ucx_utils._pci_path_components("0003:00:09.0")
assert gpu_path == ["pci0002:00", "0002:00:01.0"]
assert local_nic_path == ["pci0002:00", "0002:00:09.0"]
assert remote_nic_path == ["pci0003:00", "0003:00:09.0"]
assert ucx_utils._pci_common_depth(gpu_path, local_nic_path) == 1
assert ucx_utils._pci_common_depth(gpu_path, remote_nic_path) == 0
def _install_fake_topology(monkeypatch, gpus, nics, visible=None):
host_order = list(gpus) if visible is None else list(visible)
monkeypatch.setattr(
ucx_utils, "_list_compute_ib_nics", lambda min_rate_gbps=None: nics
)
monkeypatch.setattr(ucx_utils.torch.cuda, "device_count", lambda: len(host_order))
monkeypatch.setattr(
ucx_utils,
"_gpu_pci_bdf",
lambda gi: gpus[host_order[gi]][0] if gi < len(host_order) else None,
)
monkeypatch.setattr(
ucx_utils, "_host_gpu_bdfs", lambda: sorted(spec[0] for spec in gpus.values())
)
bdf_to_gpu = {spec[0]: spec for spec in gpus.values()}
monkeypatch.setattr(
ucx_utils,
"_pci_path_components",
lambda bdf: bdf_to_gpu[bdf][2] if bdf in bdf_to_gpu else [],
)
monkeypatch.setattr(
ucx_utils,
"_read_int_file",
lambda path: next(
(spec[1] for spec in bdf_to_gpu.values() if spec[0] in path), None
),
)
def test_flat_topology_selection_uses_root_complex_when_numa_unknown(monkeypatch):
gpu_bdfs = [
"0002:00:01.0",
"0002:00:02.0",
"0002:00:03.0",
"0002:00:04.0",
"0003:00:01.0",
"0003:00:02.0",
"0003:00:03.0",
"0003:00:04.0",
]
nic_bdfs = [
("mlx5_5", "0002:00:09.0"),
("mlx5_6", "0002:00:0a.0"),
("mlx5_7", "0002:00:0b.0"),
("mlx5_8", "0002:00:0c.0"),
("mlx5_9", "0003:00:09.0"),
("mlx5_10", "0003:00:0a.0"),
("mlx5_11", "0003:00:0b.0"),
("mlx5_12", "0003:00:0c.0"),
]
def _flat_realpath(path):
bdf = os.path.basename(path)
domain = bdf.split(":", 1)[0]
return f"/sys/devices/pci{domain}:00/{bdf}"
monkeypatch.setattr(ucx_utils.os.path, "realpath", _flat_realpath)
gpus = {
gpu: (bdf, -1, ucx_utils._pci_path_components(bdf))
for gpu, bdf in enumerate(gpu_bdfs)
}
nics = [
(name, -1, 400.0, ucx_utils._pci_path_components(bdf))
for name, bdf in nic_bdfs
]
_install_fake_topology(monkeypatch, gpus, nics)
chosen = {gpu: ucx_utils.probe_nic_pin_for_device(gpu) for gpu in gpus}
assert chosen == {
0: "mlx5_5:1",
1: "mlx5_6:1",
2: "mlx5_7:1",
3: "mlx5_8:1",
4: "mlx5_10:1",
5: "mlx5_11:1",
6: "mlx5_12:1",
7: "mlx5_9:1",
}
_MISAFFINE_GPUS = {
0: (
"0000:9a:00.0",
1,
["pci0000:97", "0000:97:01.0", "0000:98:00.0", "0000:9a:00.0"],
),
1: (
"0000:aa:00.0",
1,
["pci0000:a7", "0000:a7:01.0", "0000:a8:00.0", "0000:aa:00.0"],
),
2: (
"0000:ba:00.0",
1,
["pci0000:b7", "0000:b7:01.0", "0000:b8:00.0", "0000:ba:00.0"],
),
3: (
"0000:ca:00.0",
1,
["pci0000:c7", "0000:c7:01.0", "0000:c8:00.0", "0000:ca:00.0"],
),
}
_MISAFFINE_NICS = [
("mlx5_0", 0, 400.0, ["pci0000:15", "0000:15:01.0", "0000:19:00.0"]),
("mlx5_1", 0, 400.0, ["pci0000:26", "0000:26:01.0", "0000:2a:00.0"]),
("mlx5_11", 1, 400.0, ["pci0000:c7", "0000:c7:01.0", "0000:cb:00.0"]),
("mlx5_2", 0, 400.0, ["pci0000:37", "0000:37:01.0", "0000:3b:00.0"]),
]
def test_the_fixture_reproduces_the_depth_collapse(monkeypatch):
nic_paths = {name: path for name, _numa, _rate, path in _MISAFFINE_NICS}
for gpu in (0, 1, 2):
gpu_path = _MISAFFINE_GPUS[gpu][2]
depths = {
name: ucx_utils._pci_common_depth(gpu_path, path)
for name, path in nic_paths.items()
}
assert set(depths.values()) == {0}, (
f"GPU{gpu} must tie at depth 0 against every rail, got {depths}"
)
assert (
ucx_utils._pci_common_depth(_MISAFFINE_GPUS[3][2], nic_paths["mlx5_11"]) > 0
), "GPU3 is the one GPU with real PCIe affinity; without it the tie is total"
def test_partial_allocation_spreads_rather_than_sharing_one_rail(monkeypatch):
_install_fake_topology(monkeypatch, _MISAFFINE_GPUS, _MISAFFINE_NICS)
chosen = {gpu: ucx_utils.probe_nic_pin_for_device(gpu) for gpu in _MISAFFINE_GPUS}
assert len(set(chosen.values())) == 4, (
f"every GPU must get its own rail; got {chosen}"
)
assert chosen[3] == "mlx5_11:1", (
"the one GPU with PCIe affinity to the same-socket rail should keep it"
)
def test_rank_local_cuda_visibility_still_spreads(monkeypatch):
chosen = {}
for host_index in _MISAFFINE_GPUS:
_install_fake_topology(
monkeypatch, _MISAFFINE_GPUS, _MISAFFINE_NICS, visible=[host_index]
)
chosen[host_index] = ucx_utils.probe_nic_pin_for_device(0)
assert len(set(chosen.values())) == 4, (
f"each rank-local worker must still get its own rail; got {chosen}"
)
assert chosen[3] == "mlx5_11:1", (
"the affinity-aware placement must survive the masking too, not just "
"the distinctness"
)
def test_rank_local_assignment_matches_the_full_visibility_one(monkeypatch):
_install_fake_topology(monkeypatch, _MISAFFINE_GPUS, _MISAFFINE_NICS)
unmasked = {gpu: ucx_utils.probe_nic_pin_for_device(gpu) for gpu in _MISAFFINE_GPUS}
masked = {}
for host_index in _MISAFFINE_GPUS:
_install_fake_topology(
monkeypatch, _MISAFFINE_GPUS, _MISAFFINE_NICS, visible=[host_index]
)
masked[host_index] = ucx_utils.probe_nic_pin_for_device(0)
assert masked == unmasked, (
f"masked ranks disagree with the full-visibility map: {masked} != {unmasked}"
)
def test_unreadable_pci_listing_falls_back_to_visible_devices(monkeypatch):
_install_fake_topology(monkeypatch, _MISAFFINE_GPUS, _MISAFFINE_NICS)
monkeypatch.setattr(ucx_utils, "_host_gpu_bdfs", list)
assert ucx_utils.probe_nic_pin_for_device(3) == "mlx5_11:1"
def test_a_lone_same_socket_rail_is_not_handed_to_everyone(monkeypatch):
_install_fake_topology(monkeypatch, _MISAFFINE_GPUS, _MISAFFINE_NICS)
on_the_lone_rail = [
gpu
for gpu in _MISAFFINE_GPUS
if ucx_utils.probe_nic_pin_for_device(gpu) == "mlx5_11:1"
]
assert len(on_the_lone_rail) == 1, (
f"only one GPU may pin to the lone same-socket rail, got {on_the_lone_rail}"
)
_AFFINE_GPUS = {
0: ("0000:18:00.0", 0, ["0000:15:01.0", "0000:16:00.0", "0000:18:00.0"]),
1: ("0000:29:00.0", 0, ["0000:26:01.0", "0000:27:00.0", "0000:29:00.0"]),
2: ("0000:3a:00.0", 0, ["0000:37:01.0", "0000:38:00.0", "0000:3a:00.0"]),
3: ("0000:4b:00.0", 0, ["0000:48:01.0", "0000:49:00.0", "0000:4b:00.0"]),
4: ("0000:9a:00.0", 1, ["0000:97:01.0", "0000:98:00.0", "0000:9a:00.0"]),
5: ("0000:aa:00.0", 1, ["0000:a7:01.0", "0000:a8:00.0", "0000:aa:00.0"]),
6: ("0000:ba:00.0", 1, ["0000:b7:01.0", "0000:b8:00.0", "0000:ba:00.0"]),
7: ("0000:ca:00.0", 1, ["0000:c7:01.0", "0000:c8:00.0", "0000:ca:00.0"]),
}
_AFFINE_NICS = [
("mlx5_0", 0, 400.0, ["0000:15:01.0", "0000:19:00.0"]),
("mlx5_1", 0, 400.0, ["0000:26:01.0", "0000:2a:00.0"]),
("mlx5_10", 1, 400.0, ["0000:b7:01.0", "0000:bc:00.0"]),
("mlx5_11", 1, 400.0, ["0000:c7:01.0", "0000:cb:00.0"]),
("mlx5_2", 0, 400.0, ["0000:37:01.0", "0000:3b:00.0"]),
("mlx5_3", 0, 400.0, ["0000:48:01.0", "0000:4c:00.0"]),
("mlx5_4", 1, 400.0, ["0000:97:01.0", "0000:9b:00.0"]),
("mlx5_5", 1, 400.0, ["0000:a7:01.0", "0000:ab:00.0"]),
]
def test_affine_allocation_keeps_one_to_one_mapping(monkeypatch):
_install_fake_topology(monkeypatch, _AFFINE_GPUS, _AFFINE_NICS)
chosen = {gpu: ucx_utils.probe_nic_pin_for_device(gpu) for gpu in _AFFINE_GPUS}
assert chosen == {
0: "mlx5_0:1",
1: "mlx5_1:1",
2: "mlx5_2:1",
3: "mlx5_3:1",
4: "mlx5_4:1",
5: "mlx5_5:1",
6: "mlx5_10:1",
7: "mlx5_11:1",
}
assert len(set(chosen.values())) == len(chosen)
def test_more_gpus_than_rails_balances_reuse(monkeypatch):
nics = [
("mlx5_0", 0, 400.0, ["0000:15:01.0", "0000:19:00.0"]),
("mlx5_11", 1, 400.0, ["0000:c7:01.0", "0000:cb:00.0"]),
]
_install_fake_topology(monkeypatch, _AFFINE_GPUS, nics)
counts: dict[str, int] = {}
for gpu in _AFFINE_GPUS:
pinned = ucx_utils.probe_nic_pin_for_device(gpu)
counts[pinned] = counts.get(pinned, 0) + 1
assert sorted(counts.values()) == [4, 4], f"uneven reuse: {counts}"
def test_unknown_numa_does_not_override_pcie_affinity(monkeypatch):
gpus = {0: ("0000:9a:00.0", 1, ["0000:97:01.0", "0000:9a:00.0"])}
nics = [
("mlx5_4", -1, 400.0, ["0000:97:01.0", "0000:9b:00.0"]),
("mlx5_11", 1, 400.0, ["0000:c7:01.0", "0000:cb:00.0"]),
]
_install_fake_topology(monkeypatch, gpus, nics)
assert ucx_utils.probe_nic_pin_for_device(0) == "mlx5_4:1"
def test_host_gpu_listing_ignores_cuda_visibility(monkeypatch, tmp_path):
devices = tmp_path / "devices"
entries = {
"0000:9a:00.0": ("0x10de", "0x030000"), "0000:aa:00.0": ("0x10de", "0x030200"), "0000:c7:00.0": ("0x15b3", "0x020700"), "0000:05:00.0": ("0x10de", "0x0c0330"), }
for bdf, (vendor, pci_class) in entries.items():
(devices / bdf).mkdir(parents=True)
(devices / bdf / "vendor").write_text(f"{vendor}\n")
(devices / bdf / "class").write_text(f"{pci_class}\n")
real_listdir, real_open = os.listdir, open
monkeypatch.setattr(
ucx_utils.os,
"listdir",
lambda path: (
real_listdir(devices)
if path == "/sys/bus/pci/devices"
else real_listdir(path)
),
)
monkeypatch.setattr(
ucx_utils,
"_read_str_file",
lambda path: (
real_open(str(devices) + path[len("/sys/bus/pci/devices") :]).read().strip()
if path.startswith("/sys/bus/pci/devices")
else None
),
)
assert ucx_utils._host_gpu_bdfs() == ["0000:9a:00.0", "0000:aa:00.0"]
def test_host_gpu_listing_is_empty_when_sysfs_is_unreadable(monkeypatch):
def _boom(_path):
raise OSError("no /sys here")
monkeypatch.setattr(ucx_utils.os, "listdir", _boom)
assert ucx_utils._host_gpu_bdfs() == []