Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 41 additions & 27 deletions cuda_core/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,50 +529,64 @@ def _close_memory_resource(mr):
close()


@pytest.fixture(
params=[
pytest.param("device", id="DeviceMR"),
pytest.param("pinned", id="PinnedMR"),
pytest.param("vmm", id="VirtualMR"),
]
)
def ipc_memory_resource(request, ipc_device):
"""Provides an IPC-enabled memory resource: a device pool, a pinned pool, or a VirtualMemoryResource.
_DEVICE_MR = pytest.param("device", id="DeviceMR")
_PINNED_MR = pytest.param("pinned", id="PinnedMR")
_VIRTUAL_MR = pytest.param("vmm", id="VirtualMR")

The VMM parameter needs VMM support and POSIX file descriptor handles on
top of what :func:`ipc_device` requires; it skips otherwise. Tests that
use the pool-only half of the IPC protocol (``allocation_handle``,
``from_allocation_handle``, the registry, ``uuid``, ``is_mapped`` on the
resource) guard or skip for it.
"""
mr = _make_ipc_memory_resource(request.param, ipc_device)

def _ipc_memory_resource(kind, device):
mr = _make_ipc_memory_resource(kind, device)
assert mr.is_ipc_enabled
yield mr
_close_memory_resource(mr)
# TODO(seberg): Make sure the `mr` and it's buffers are fully torn down.
# May be unnecessary as `mr.close()` is not parallel with other work.
ipc_device.sync()
device.sync()


@pytest.fixture(
params=[
pytest.param("device", id="DeviceMR"),
pytest.param("vmm", id="VirtualMR"),
]
)
def ipc_memory_resource_factory(request, ipc_device):
"""Provides a callable that creates fresh IPC-enabled resources of one kind; all are closed at teardown."""
def _ipc_memory_resource_factory(kind, device):
created = []

def make():
mr = _make_ipc_memory_resource(request.param, ipc_device)
mr = _make_ipc_memory_resource(kind, device)
created.append(mr)
return mr

yield make
for mr in created:
_close_memory_resource(mr)
ipc_device.sync()
device.sync()


@pytest.fixture(params=[_DEVICE_MR, _PINNED_MR, _VIRTUAL_MR])
def ipc_memory_resource(request, ipc_device):
"""Provides an IPC-enabled memory resource: a device pool, a pinned pool, or a VirtualMemoryResource.

The VMM parameter needs VMM support and POSIX file descriptor handles on
top of what :func:`ipc_device` requires; it skips otherwise. Tests that
use the pool-only half of the IPC protocol (``allocation_handle``,
``from_allocation_handle``, the registry, ``uuid``, ``is_mapped`` on the
resource) use :func:`ipc_pool_memory_resource` instead.
"""
yield from _ipc_memory_resource(request.param, ipc_device)


@pytest.fixture(params=[_DEVICE_MR, _PINNED_MR])
def ipc_pool_memory_resource(request, ipc_device):
"""Provides an IPC-enabled memory pool: a device pool or a pinned pool."""
yield from _ipc_memory_resource(request.param, ipc_device)


@pytest.fixture(params=[_DEVICE_MR, _VIRTUAL_MR])
def ipc_memory_resource_factory(request, ipc_device):
"""Provides a callable that creates fresh IPC-enabled resources of one kind; all are closed at teardown."""
yield from _ipc_memory_resource_factory(request.param, ipc_device)


@pytest.fixture(params=[_DEVICE_MR])
def ipc_pool_memory_resource_factory(request, ipc_device):
"""Like :func:`ipc_memory_resource_factory`, for tests that use the pool-only half of the IPC protocol."""
yield from _ipc_memory_resource_factory(request.param, ipc_device)


@pytest.fixture
Expand Down
36 changes: 12 additions & 24 deletions cuda_core/tests/memory_ipc/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,16 @@ def test_registry_rejects_a_key_that_is_not_a_uuid(mempool_device, key):
DeviceMemoryResource.from_registry(key)


# For the VirtualMR parameter case:
# This test is not currently run in any CI job
@pytest.mark.human_authored
def test_register_rejects_mismatched_uuid(ipc_memory_resource):
def test_register_rejects_mismatched_uuid(ipc_pool_memory_resource):
"""register() rejects a UUID that does not match the resource's own.

The check is an explicit ValueError (not a bare ``assert``) so it remains
enforced even when CPython runs with ``-O``, where assertions are removed
(see #2697). Registering under a foreign key would otherwise rewrite the
resource's UUID and silently corrupt the IPC registry.
"""
mr = ipc_memory_resource
if not is_pool_resource(mr):
pytest.skip("allocation handles and the registry are pool-only")
mr = ipc_pool_memory_resource
assert mr.is_ipc_enabled

# The resource must already carry its own UUID (assigned on creation).
Expand All @@ -136,7 +132,7 @@ def test_register_rejects_mismatched_uuid(ipc_memory_resource):


@pytest.mark.agent_authored(model="claude-fable-5-2")
def test_register_keeps_the_uuid_object_the_resource_already_has(ipc_memory_resource_factory):
def test_register_keeps_the_uuid_object_the_resource_already_has(ipc_pool_memory_resource_factory):
"""register() under an equal UUID keeps the UUID object the resource already has.

The UUID of an allocation handle is read without a lock. Replacing the
Expand All @@ -145,9 +141,7 @@ def test_register_keeps_the_uuid_object_the_resource_already_has(ipc_memory_reso
"""
# A resource of its own: pytest-run-parallel shares fixtures between its
# threads, and another thread could register a shared resource first.
mr = ipc_memory_resource_factory()
if not is_pool_resource(mr):
pytest.skip("allocation handles and the registry are pool-only")
mr = ipc_pool_memory_resource_factory()

original = mr.uuid
assert original is not None
Expand Down Expand Up @@ -180,13 +174,9 @@ def test_ipc_allocation_handle_state_tracks_close():
os.close(write_fd)


# For the VirtualMR parameter case:
# This test is not currently run in any CI job
@pytest.mark.agent_authored(model="gpt-5.6")
def test_closed_ipc_allocation_handle_rejected_before_registry_hit(ipc_device, ipc_memory_resource):
mr = ipc_memory_resource
if not is_pool_resource(mr):
pytest.skip("allocation handles and the registry are pool-only")
def test_closed_ipc_allocation_handle_rejected_before_registry_hit(ipc_device, ipc_pool_memory_resource):
mr = ipc_pool_memory_resource
handle = IPCAllocationHandle._init(os.dup(int(mr.allocation_handle)), mr.uuid)
assert mr.register(mr.uuid) is mr
handle.close()
Expand All @@ -202,9 +192,8 @@ def test_closed_ipc_allocation_handle_rejected_before_registry_hit(ipc_device, i
class ChildErrorHarness:
"""Test harness for checking errors in child processes. Subclasses override
PARENT_ACTION, CHILD_ACTION, and ASSERT (see below for examples). A subclass
that exercises the pool half of the protocol sets POOL_ONLY."""

POOL_ONLY = False
that exercises the pool half of the protocol overrides ``ipc_memory_resource``
with ``ipc_pool_memory_resource``."""

@pytest.mark.thread_unsafe(
reason=(
Expand All @@ -220,8 +209,6 @@ def test_main(self, ipc_device, ipc_memory_resource):
self.device = ipc_device
self.mr = ipc_memory_resource
self._extra_mrs = []
if self.POOL_ONLY and not is_pool_resource(self.mr):
pytest.skip("this scenario needs the pool half of the IPC protocol")
# Clear stale 'buffer' left by a previous flaky-rerun attempt.
# Its backing DeviceMemoryResource may already be closed, which causes
# process.start() to fail when pickling self for spawn-mode IPC.
Expand Down Expand Up @@ -377,15 +364,16 @@ def ASSERT(self, exc_type, exc_msg):
assert exc_msg.startswith("Argument 'ipc_descriptor' has incorrect type")


# For the inherited test_main[VirtualMR] case:
# This test is not currently run in any CI job
class TestDanglingBuffer(ChildErrorHarness):
"""
Error when importing a buffer object without registering its memory
resource.
"""

POOL_ONLY = True # plain pickle of a VMM buffer is refused (fds), and VMM has no registry
@pytest.fixture
def ipc_memory_resource(self, ipc_pool_memory_resource):
# Plain pickle of a VMM buffer is refused (fds), and VMM has no registry.
return ipc_pool_memory_resource

def PARENT_ACTION(self, queue):
options = DeviceMemoryResourceOptions(max_size=POOL_SIZE, ipc_enabled=True)
Expand Down
86 changes: 51 additions & 35 deletions cuda_core/tests/memory_ipc/test_ipc_pool_duplicate_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
drain_on_exit,
enter_imported_pool,
import_pool_resource,
is_pool_resource,
pool_access_flags,
pool_supported_devices,
)
Expand Down Expand Up @@ -606,96 +605,113 @@ def _set_start_method(self):
mp.set_start_method("spawn", force=True)

@pytest.fixture
def pool_resource(self, ipc_memory_resource):
if not is_pool_resource(ipc_memory_resource):
pytest.skip("a VirtualMemoryResource has no memory pool to import")
return ipc_memory_resource

@pytest.fixture
def exported_buffer(self, ipc_device, pool_resource):
def exported_buffer(self, ipc_device, ipc_pool_memory_resource):
ipc_device.set_current()
stream = ipc_device.default_stream
buffer = pool_resource.allocate(NBYTES, stream=stream)
buffer = ipc_pool_memory_resource.allocate(NBYTES, stream=stream)
stream.sync()
yield buffer
buffer.close(stream)

@pytest.mark.agent_authored(model="claude-opus-5-5")
def test_closing_one_import_keeps_the_other_usable(self, pool_resource, exported_buffer):
def test_closing_one_import_keeps_the_other_usable(self, ipc_pool_memory_resource, exported_buffer):
run_child(
child_close_one_import, type(pool_resource), pool_resource.allocation_handle, exported_buffer.ipc_descriptor
child_close_one_import,
type(ipc_pool_memory_resource),
ipc_pool_memory_resource.allocation_handle,
exported_buffer.ipc_descriptor,
)

@pytest.mark.agent_authored(model="claude-opus-5-5")
def test_closing_both_imports_with_an_outstanding_buffer(self, pool_resource, exported_buffer):
def test_closing_both_imports_with_an_outstanding_buffer(self, ipc_pool_memory_resource, exported_buffer):
run_child(
child_close_both_imports_with_outstanding_buffer,
type(pool_resource),
pool_resource.allocation_handle,
type(ipc_pool_memory_resource),
ipc_pool_memory_resource.allocation_handle,
exported_buffer.ipc_descriptor,
)

@pytest.mark.agent_authored(model="claude-opus-5-5")
def test_reimport_racing_the_last_release_keeps_access(self, pool_resource, exported_buffer):
def test_reimport_racing_the_last_release_keeps_access(self, ipc_pool_memory_resource, exported_buffer):
run_child(
child_reimport_races_last_release,
type(pool_resource),
pool_resource.allocation_handle,
type(ipc_pool_memory_resource),
ipc_pool_memory_resource.allocation_handle,
exported_buffer.ipc_descriptor,
)

@pytest.mark.agent_authored(model="claude-opus-5-5")
def test_registration_reentered_from_garbage_collection(self, ipc_device, pool_resource):
def test_registration_reentered_from_garbage_collection(self, ipc_device, ipc_pool_memory_resource):
ipc_device.set_current()
run_child(child_register_reentered_by_gc, type(pool_resource), pool_resource.allocation_handle)
run_child(
child_register_reentered_by_gc, type(ipc_pool_memory_resource), ipc_pool_memory_resource.allocation_handle
)

@pytest.mark.skipif(sys.version_info[:2] != (3, 11), reason="requires CPython 3.11's allocation-triggered GC")
@pytest.mark.agent_authored(model="gpt-6.2-sol")
def test_gc_during_registration_allows_registry_lookup(self, ipc_device, pool_resource):
def test_gc_during_registration_allows_registry_lookup(self, ipc_device, ipc_pool_memory_resource):
ipc_device.set_current()
run_child(
child_gc_during_registration_allows_registry_lookup, type(pool_resource), pool_resource.allocation_handle
child_gc_during_registration_allows_registry_lookup,
type(ipc_pool_memory_resource),
ipc_pool_memory_resource.allocation_handle,
)

@pytest.mark.agent_authored(model="claude-opus-5-5")
@pytest.mark.parametrize("shared_handle", [False, True], ids=["separate_handles", "shared_handle"])
@pytest.mark.parametrize("case", ["keep", "close", "fail"])
def test_registration_interrupted_at_every_use_of_its_key(self, ipc_device, pool_resource, case, shared_handle):
def test_registration_interrupted_at_every_use_of_its_key(
self, ipc_device, ipc_pool_memory_resource, case, shared_handle
):
ipc_device.set_current()
run_child(
child_register_interrupted_at_every_key_use,
type(pool_resource),
pool_resource.allocation_handle,
type(ipc_pool_memory_resource),
ipc_pool_memory_resource.allocation_handle,
case,
shared_handle,
)

@pytest.mark.agent_authored(model="claude-opus-5-5")
def test_registry_entry_of_a_dropped_resource_is_removed(self, ipc_device, pool_resource):
def test_registry_entry_of_a_dropped_resource_is_removed(self, ipc_device, ipc_pool_memory_resource):
ipc_device.set_current()
run_child(child_dead_registry_entry_is_removed, type(pool_resource), pool_resource.allocation_handle)
run_child(
child_dead_registry_entry_is_removed,
type(ipc_pool_memory_resource),
ipc_pool_memory_resource.allocation_handle,
)

@pytest.mark.agent_authored(model="claude-fable-5-2")
def test_shared_handle_keeps_its_uuid_object_after_its_first_owner(self, ipc_device, pool_resource):
def test_shared_handle_keeps_its_uuid_object_after_its_first_owner(self, ipc_device, ipc_pool_memory_resource):
ipc_device.set_current()
run_child(child_shared_handle_keeps_its_uuid_object, type(pool_resource), pool_resource.allocation_handle)
run_child(
child_shared_handle_keeps_its_uuid_object,
type(ipc_pool_memory_resource),
ipc_pool_memory_resource.allocation_handle,
)

@pytest.mark.agent_authored(model="claude-opus-5-5")
def test_lookups_racing_registrations_find_registered_resources(self, ipc_device, pool_resource):
def test_lookups_racing_registrations_find_registered_resources(self, ipc_device, ipc_pool_memory_resource):
ipc_device.set_current()
run_child(child_lookups_race_registrations, type(pool_resource), pool_resource.allocation_handle)
run_child(
child_lookups_race_registrations, type(ipc_pool_memory_resource), ipc_pool_memory_resource.allocation_handle
)

@pytest.mark.agent_authored(model="claude-opus-5-5")
def test_concurrent_imports_of_one_uuid_share_a_resource(self, ipc_device, pool_resource):
def test_concurrent_imports_of_one_uuid_share_a_resource(self, ipc_device, ipc_pool_memory_resource):
ipc_device.set_current()
run_child(child_concurrent_uuid_imports, type(pool_resource), pool_resource.allocation_handle)
run_child(
child_concurrent_uuid_imports, type(ipc_pool_memory_resource), ipc_pool_memory_resource.allocation_handle
)

# Releasing the last import of a pool with unsynchronized frees pending
# leaves the driver holding the pool (#3514); the next import must still
# grant device access.
@pytest.mark.parametrize("import_path", ["single", "raw_handle", "concurrent_uuid"])
@pytest.mark.agent_authored(model="claude-sonnet-5-5")
def test_reimport_after_release_with_pending_free(self, ipc_device, pool_resource, exported_buffer, import_path):
def test_reimport_after_release_with_pending_free(
self, ipc_device, ipc_pool_memory_resource, exported_buffer, import_path
):
# The child holds the free with a LatchKernel. Check what it needs here:
# a skip raised in the child would surface as a child failure.
if helpers.CUDA_INCLUDE_PATH is None:
Expand All @@ -704,8 +720,8 @@ def test_reimport_after_release_with_pending_free(self, ipc_device, pool_resourc
pytest.skip("__nanosleep is only available starting Volta (sm70)")
run_child(
child_reimport_after_release_with_pending_free,
type(pool_resource),
pool_resource.allocation_handle,
type(ipc_pool_memory_resource),
ipc_pool_memory_resource.allocation_handle,
exported_buffer.ipc_descriptor,
import_path,
)
Loading
Loading