From 1542aa077fd4a438fea2de4bde4829c829f89dd7 Mon Sep 17 00:00:00 2001 From: Ralf Juengling Date: Mon, 16 Nov 2026 12:01:56 -0800 Subject: [PATCH] fix(cuda.core): run cuMemPoolSetAccess in relaxed mode in PinnedMemoryResource Since #3405, PinnedMemoryResource.__init__ without options calls cuMemPoolSetAccess to grant the current device read/write access to the driver's default pool. The driver refuses that call, and invalidates the capture, while a global-mode capture is active on any thread. Users of PyTorch's torch.cuda.graph (which defaults to global mode) therefore see CUDA_ERROR_STREAM_CAPTURE_UNSUPPORTED on construction and CUDA_ERROR_STREAM_CAPTURE_INVALIDATED on end_building. Fix: introduce MP_set_access in _memory_pool.pyx, wrapping cuMemPoolSetAccess in the existing _enter_relaxed_capture_mode / _exit_relaxed_capture_mode helpers, following the identical pattern used by pool attribute reads and writes and trim() since #2834. Call it from _PMR_init instead of the bare with-nogil block. The with-options path (cuMemPoolCreate) is unaffected; that call is still refused under global capture as documented in the 1.3.0 notes. Add tests for the three capture combinations (global+same_thread, global+other_thread, thread_local+same_thread) and a capture-mode restore check modelled on test_dmr_init_restores_capture_mode. Closes #3451 --- cuda_core/cuda/core/_memory/_memory_pool.pxd | 3 + cuda_core/cuda/core/_memory/_memory_pool.pyx | 18 +++++ .../core/_memory/_pinned_memory_resource.pyi | 3 +- .../core/_memory/_pinned_memory_resource.pyx | 8 +- cuda_core/docs/source/release/1.3.1-notes.rst | 21 +++++ cuda_core/tests/test_memory.py | 78 +++++++++++++++++++ 6 files changed, 126 insertions(+), 5 deletions(-) create mode 100644 cuda_core/docs/source/release/1.3.1-notes.rst diff --git a/cuda_core/cuda/core/_memory/_memory_pool.pxd b/cuda_core/cuda/core/_memory/_memory_pool.pxd index b85e5182e3b..f2117b4034d 100644 --- a/cuda_core/cuda/core/_memory/_memory_pool.pxd +++ b/cuda_core/cuda/core/_memory/_memory_pool.pxd @@ -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: diff --git a/cuda_core/cuda/core/_memory/_memory_pool.pyx b/cuda_core/cuda/core/_memory/_memory_pool.pyx index 7d071f62238..679e25b4f3c 100644 --- a/cuda_core/cuda/core/_memory/_memory_pool.pyx +++ b/cuda_core/cuda/core/_memory/_memory_pool.pyx @@ -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: diff --git a/cuda_core/cuda/core/_memory/_pinned_memory_resource.pyi b/cuda_core/cuda/core/_memory/_pinned_memory_resource.pyi index af909bb095a..29f72ea10c1 100644 --- a/cuda_core/cuda/core/_memory/_pinned_memory_resource.pyi +++ b/cuda_core/cuda/core/_memory/_pinned_memory_resource.pyi @@ -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 diff --git a/cuda_core/cuda/core/_memory/_pinned_memory_resource.pyx b/cuda_core/cuda/core/_memory/_pinned_memory_resource.pyx index cbfaa4f7638..305d69c2809 100644 --- a/cuda_core/cuda/core/_memory/_pinned_memory_resource.pyx +++ b/cuda_core/cuda/core/_memory/_pinned_memory_resource.pyx @@ -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, @@ -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 @@ -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, ...]: diff --git a/cuda_core/docs/source/release/1.3.1-notes.rst b/cuda_core/docs/source/release/1.3.1-notes.rst new file mode 100644 index 00000000000..c9c00443ffc --- /dev/null +++ b/cuda_core/docs/source/release/1.3.1-notes.rst @@ -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 `__) diff --git a/cuda_core/tests/test_memory.py b/cuda_core/tests/test_memory.py index 5d4aab75e42..a9d50eb753c 100644 --- a/cuda_core/tests/test_memory.py +++ b/cuda_core/tests/test_memory.py @@ -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