diff --git a/cuda_core/tests/memory_ipc/test_ipc_pool_kind.py b/cuda_core/tests/memory_ipc/test_ipc_pool_kind.py index b7edc019226..ea3d9c987aa 100644 --- a/cuda_core/tests/memory_ipc/test_ipc_pool_kind.py +++ b/cuda_core/tests/memory_ipc/test_ipc_pool_kind.py @@ -46,19 +46,32 @@ pytestmark = pytest.mark.parallel_threads_limit(2) -def _nonzero_numa_node_with_memory(): - """The first host NUMA node other than 0 that has memory, or None.""" +NUMA_SYSFS = "/sys/devices/system/node" + + +def _parse_node_list(text): + """The node IDs in a sysfs node list such as ``0,2-3``.""" + nodes = set() + for node_range in text.strip().split(","): + if node_range: + first, _, last = node_range.partition("-") + nodes.update(range(int(first), int(last or first) + 1)) + return nodes + + +def _nonzero_host_numa_node(sysfs=NUMA_SYSFS): + """The first NUMA node other than 0 that has host memory, or None. + + A node with memory but no CPUs may be GPU memory, as on Grace systems, and + cuMemPoolCreate rejects it as a host NUMA location (#3650). + """ try: - with open("/sys/devices/system/node/has_memory") as f: - node_list = f.read().strip() + with open(f"{sysfs}/has_memory") as f_memory, open(f"{sysfs}/has_cpu") as f_cpu: + with_memory = _parse_node_list(f_memory.read()) + with_cpu = _parse_node_list(f_cpu.read()) except FileNotFoundError: return None - for node_range in node_list.split(","): - first, _, last = node_range.partition("-") - for node in range(int(first), int(last or first) + 1): - if node > 0: - return node - return None + return min((node for node in with_memory & with_cpu if node > 0), default=None) def _other_class(mr_type): @@ -251,9 +264,9 @@ def test_rejected_import_keeps_existing_import(self, pool_resource, exported_buf def test_imported_pinned_resource_reports_numa_id(self, ipc_device): skip_if_host_numa_pinned_memory_unsupported(ipc_device) # An imported resource used to report 0, so node 0 cannot tell the two apart. - numa_id = _nonzero_numa_node_with_memory() + numa_id = _nonzero_host_numa_node() if numa_id is None: - pytest.skip("needs a host NUMA node other than 0 with memory") + pytest.skip("needs a host NUMA node other than 0 with memory and CPUs") ipc_device.set_current() mr = PinnedMemoryResource(PinnedMemoryResourceOptions(max_size=POOL_SIZE, ipc_enabled=True, numa_id=numa_id)) try: @@ -261,3 +274,25 @@ def test_imported_pinned_resource_reports_numa_id(self, ipc_device): run_child(child_imported_numa_id, mr.allocation_handle, numa_id) finally: mr.close() + + +@pytest.mark.agent_authored(model="claude-sonnet-5-5") +@pytest.mark.parametrize( + ("has_memory", "has_cpu", "expected"), + [ + ("0-1", "0", None), # GH200: CPU memory at node 0, GPU memory at node 1 + ("0-9", "0", None), # GH200 with MIG nodes + ("0-1", "0-1", 1), # two-socket host + ("0,2-3", "0,3", 3), # the CPU-less node 2 is skipped + ("0", "0", None), + ], +) +def test_nonzero_host_numa_node(tmp_path, has_memory, has_cpu, expected): + (tmp_path / "has_memory").write_text(has_memory + "\n") + (tmp_path / "has_cpu").write_text(has_cpu + "\n") + assert _nonzero_host_numa_node(str(tmp_path)) == expected + + +@pytest.mark.agent_authored(model="claude-sonnet-5-5") +def test_nonzero_host_numa_node_without_sysfs(tmp_path): + assert _nonzero_host_numa_node(str(tmp_path)) is None