Skip to content
Draft
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
3 changes: 3 additions & 0 deletions cuda_core/cuda/core/_memory/_memory_pool.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ cdef int MP_init_current_pool(

cdef int MP_raise_release_threshold(_MemPool self) except? -1

cdef int MP_set_access(
_MemPool self, const cydriver.CUmemAccessDesc* desc, size_t count) except? -1


cdef inline int MP_check_open(_MemPool self) except -1:
if not self._h_pool:
Expand Down
18 changes: 18 additions & 0 deletions cuda_core/cuda/core/_memory/_memory_pool.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,24 @@ cdef int MP_raise_release_threshold(_MemPool self) except? -1:
return 0


cdef int MP_set_access(
_MemPool self, const cydriver.CUmemAccessDesc* desc, size_t count) except? -1:
"""Set device access to the pool.

Runs in relaxed capture mode, like every pool attribute access (see
``_enter_relaxed_capture_mode``): the driver refuses cuMemPoolSetAccess,
and invalidates the capture, while a global-mode capture is active on
any thread or a thread-local one on the calling thread.
"""
cdef cydriver.CUstreamCaptureMode mode
cdef cydriver.CUresult err
with nogil:
_enter_relaxed_capture_mode(&mode)
err = cydriver.cuMemPoolSetAccess(as_cu(self._h_pool), desc, count)
_exit_relaxed_capture_mode(err, &mode)
return 0


# Raise an exception if the given stream is capturing.
# A result of CU_STREAM_CAPTURE_STATUS_INVALIDATED is considered an error.
cdef inline int check_not_capturing(cydriver.CUstream s) except?-1 nogil:
Expand Down
3 changes: 2 additions & 1 deletion cuda_core/cuda/core/_memory/_pinned_memory_resource.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ class PinnedMemoryResource(_MemPool):
default, so the resource grants the device that is current when it is
created read and write access to the pool. Kernels on other devices cannot
access its buffers. Without options, the grant applies to the driver's
current pool, which the whole process shares.
current pool, which the whole process shares. Construction without options
does not affect a CUDA graph capture in progress on any thread.

To create an IPC-Enabled memory resource (MR) that is capable of sharing
allocations between processes, specify ``ipc_enabled=True`` in the initializer
Expand Down
8 changes: 4 additions & 4 deletions cuda_core/cuda/core/_memory/_pinned_memory_resource.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ from cuda.core._memory._memory_pool cimport (
MP_check_open,
MP_init_create_pool,
MP_init_current_pool,
MP_set_access,
)
from cuda.core._memory cimport _ipc
from cuda.core._memory._ipc cimport IPCAllocationHandle
from cuda.core._memory._location cimport cumemlocation_from_id
from cuda.core._rt cimport as_cu
from cuda.core._stream cimport Stream, Stream_accept
from cuda.core._utils.cuda_utils cimport (
check_or_create_options,
Expand Down Expand Up @@ -105,7 +105,8 @@ cdef class PinnedMemoryResource(_MemPool):
default, so the resource grants the device that is current when it is
created read and write access to the pool. Kernels on other devices cannot
access its buffers. Without options, the grant applies to the driver's
current pool, which the whole process shares.
current pool, which the whole process shares. Construction without options
does not affect a CUDA graph capture in progress on any thread.

To create an IPC-Enabled memory resource (MR) that is capable of sharing
allocations between processes, specify ``ipc_enabled=True`` in the initializer
Expand Down Expand Up @@ -328,8 +329,7 @@ cdef inline _PMR_init(PinnedMemoryResource self, options):
access.location = cumemlocation_from_id(
cydriver.CUmemLocationType.CU_MEM_LOCATION_TYPE_DEVICE, dev.device_id)
access.flags = cydriver.CUmemAccess_flags.CU_MEM_ACCESS_FLAGS_PROT_READWRITE
with nogil:
HANDLE_RETURN(cydriver.cuMemPoolSetAccess(as_cu(self._h_pool), &access, 1))
MP_set_access(self, &access, 1)


def _deep_reduce_pinned_memory_resource(mr: object) -> tuple[object, ...]:
Expand Down
21 changes: 21 additions & 0 deletions cuda_core/docs/source/release/1.3.1-notes.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.. SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
.. SPDX-License-Identifier: Apache-2.0

.. currentmodule:: cuda.core

``cuda.core`` 1.3.1 Release Notes
=================================

Fixes and enhancements
-----------------------

- :class:`PinnedMemoryResource` construction without options no longer
invalidates a CUDA graph capture in progress on another thread. ``cuda.core``
1.3.0 introduced a ``cuMemPoolSetAccess`` call in the constructor (#3405);
the driver refuses that call, and invalidates any concurrent global-mode
capture, unless it runs in relaxed capture mode. The fix applies the same
relaxed-mode wrapper already used for pool attribute reads and writes and
:meth:`~_memory._memory_pool._MemPool.trim`. Captures started with the
default ``begin_building()`` mode are unaffected; global mode is the default
in several external libraries (for example ``torch.cuda.graph``).
(`#3451 <https://github.com/NVIDIA/cuda-python/issues/3451>`__)
78 changes: 78 additions & 0 deletions cuda_core/tests/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3065,6 +3065,84 @@ def use_pool():
mr.close()


@pytest.mark.agent_authored(model="claude-sonnet-4-6")
@pytest.mark.thread_unsafe(reason="a global-mode capture affects every thread")
@pytest.mark.parametrize(
("mode", "caller"),
# A thread-local capture does not restrict other threads, so pairing it
# with other_thread would pass without relaxed mode.
[("global", "same_thread"), ("global", "other_thread"), ("thread_local", "same_thread")],
)
def test_pmr_construction_during_capture_keeps_capture_valid(init_cuda, mode, caller):
"""PinnedMemoryResource() without options does not invalidate a capture on another thread.

Since #3405 construction calls cuMemPoolSetAccess, which the driver refuses
while a global-mode capture is active on any thread or a thread-local one on
the calling thread. The fix runs that call in relaxed capture mode (#3451).
"""
device = Device()
skip_if_pinned_memory_unsupported(device)
device.set_current()

mr = None

def construct():
device.set_current()
nonlocal mr
mr = PinnedMemoryResource()

gb = device.create_graph_builder().begin_building(mode=mode)
try:
if caller == "same_thread":
construct()
else:
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:
pool.submit(construct).result()
finally:
# Raises if construction invalidated the capture.
gb.end_building()
gb.close()
if mr is not None:
mr.close()


@pytest.mark.agent_authored(model="claude-sonnet-4-6")
@pytest.mark.thread_unsafe(reason="a global-mode capture affects every thread")
@pytest.mark.parametrize("mode", ["global", "thread_local"])
def test_pmr_construction_restores_capture_mode(init_cuda, mode):
"""After construction the calling thread is back in its previous capture mode.

PinnedMemoryResource() switches the thread to relaxed mode around its
cuMemPoolSetAccess call (#3451) and must not leave it there.
"""

def thread_capture_mode():
# Exchange the mode with relaxed twice to read without changing it.
relaxed = driver.CUstreamCaptureMode.CU_STREAM_CAPTURE_MODE_RELAXED
err, current = driver.cuThreadExchangeStreamCaptureMode(relaxed)
assert err == driver.CUresult.CUDA_SUCCESS
err, _ = driver.cuThreadExchangeStreamCaptureMode(current)
assert err == driver.CUresult.CUDA_SUCCESS
return current

device = Device()
skip_if_pinned_memory_unsupported(device)
device.set_current()

gb = device.create_graph_builder().begin_building(mode=mode)
mr = None
try:
before = thread_capture_mode()
mr = PinnedMemoryResource()
assert thread_capture_mode() == before
finally:
# Raises if construction invalidated the capture.
gb.end_building()
gb.close()
if mr is not None:
mr.close()


def test_mempool_attributes_repr(memory_resource_factory):
"""Test the repr of memory pool attributes for all memory resource types."""
MR, MRops = memory_resource_factory
Expand Down
Loading