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: 1 addition & 2 deletions .github/RELEASE-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ cuda-bindings floor" in `cuda_core/AGENTS.md`. Check the floors instead:
`cuda-bindings==<floor>`, and no "predates ... headers" line. Standard
CI does not install the CUDA 12 floor: its rows use the 12.9.x backport
build.
- The release notes list every floor move since the last release under
"Breaking Changes".
- The release notes list every floor move since the last release.

Update the `cuda-core` pin in `cuda_python/setup.py` to the new minor
series (`cuda-core~=X.Y.0`), and plan a `cuda-python` release right after
Expand Down
6 changes: 3 additions & 3 deletions cuda_core/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ bump:
pre-commit hook `check-cuda-core-bindings-floor`
(`toolshed/check_cuda_core_bindings_floor.py`) checks the pins. It also
checks that no documentation page spells a floor out by hand.
3. Add to the release notes a "Breaking Changes" entry that names the new
floors. The support-policy table in `docs/source/support.rst` reads the
floors and the release version at docs-build time. It needs no edit.
3. Add to the release notes an entry that names the new floors. The
support-policy table in `docs/source/support.rst` reads the floors and the
release version at docs-build time. It needs no edit.
4. If the pins moved past what the pixi lock files resolve, refresh the lock
files.
5. Pin `cuda-bindings` to match in the conda-forge `cuda-core` feedstock. The
Expand Down
4 changes: 3 additions & 1 deletion cuda_core/cuda/core/_cpp/rt/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ DevicePtrHandle deviceptr_create_mapped_graphics(

// Import a device pointer from IPC via cuMemPoolImportPointer.
// When the last reference is released, cuMemFreeAsync is called on the stored stream.
// Note: Does not yet implement reference counting for nvbug 5570902.
// Repeat imports of one descriptor share one handle, because the driver's first
// free of an imported pointer unmaps every import of it (nvbug 5570902). An
// import that races the release of the last handle waits until the free is done.
// On error, returns empty handle and sets thread-local error (use get_last_error()).
DevicePtrHandle deviceptr_import_ipc(
const MemoryPoolHandle &h_pool, const void *export_data, const StreamHandle &h_stream
Expand Down
43 changes: 36 additions & 7 deletions cuda_core/cuda/core/_cpp/rt/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
#include "error.hpp"
#include "internal.hpp"
#include <atomic>
#include <condition_variable>
#include <cstddef>
#include <cstring>
#include <memory>
#include <mutex>
#include <stdexcept>
#include <unordered_map>
#include <utility>
#ifndef _WIN32
#include <unistd.h>
Expand Down Expand Up @@ -519,10 +521,21 @@ struct ExportDataKeyHash {
}
};

// The handle imported for one descriptor, and the box it aliases.
struct IpcCacheEntry {
std::weak_ptr<const CUdeviceptr> handle;
const DevicePtrBox *box;
};

} // namespace

static HandleRegistry<ExportDataKey, DevicePtrHandle, ExportDataKeyHash> ipc_ptr_cache;
// Guarded by ipc_import_mutex. An entry outlives its handle's last reference:
// only that handle's deleter erases it, under the mutex, together with the
// free. An importer that finds an expired entry therefore knows a release of
// the same mapping is in flight and waits on ipc_release_cv until it is done.
static std::unordered_map<ExportDataKey, IpcCacheEntry, ExportDataKeyHash> ipc_ptr_cache;
static std::mutex ipc_import_mutex;
static std::condition_variable ipc_release_cv;

DevicePtrHandle
deviceptr_import_ipc(const MemoryPoolHandle &h_pool, const void *export_data, const StreamHandle &h_stream)
Expand Down Expand Up @@ -552,10 +565,17 @@ deviceptr_import_ipc(const MemoryPoolHandle &h_pool, const void *export_data, co
CUresult discard_status = CUDA_SUCCESS;
CUdeviceptr discarded = 0;
{
std::scoped_lock lock(ipc_import_mutex);
std::unique_lock lock(ipc_import_mutex);

if (auto h = ipc_ptr_cache.lookup(key)) {
return h;
for (auto it = ipc_ptr_cache.find(key); it != ipc_ptr_cache.end(); it = ipc_ptr_cache.find(key)) {
if (DevicePtrHandle h = it->second.handle.lock()) {
return h;
}
// Expired but not erased: the last reference is gone and its
// deleter has not freed the mapping yet. A re-import now would
// share that mapping, and the deleter's cuMemFreeAsync would
// unmap it under us (nvbug 5570902). Wait for the release.
ipc_release_cv.wait(lock);
}

CUdeviceptr ptr;
Expand All @@ -579,20 +599,29 @@ deviceptr_import_ipc(const MemoryPoolHandle &h_pool, const void *export_data, co
GILReleaseGuard gil;
const DeallocationStream& stream = b->deallocation;
ContextHandle h_dealloc = deallocation_context(stream);
// Explicit template argument: MSVC cannot deduce it here (C2955).
std::unique_lock<std::mutex> lock(ipc_import_mutex);
ipc_ptr_cache.unregister_handle(key);
// Erase only this box's entry (importers wait while it
// exists, so no other box can own the key yet).
auto entry = ipc_ptr_cache.find(key);
if (entry != ipc_ptr_cache.end() && entry->second.box == b) {
ipc_ptr_cache.erase(entry);
}
cleanup_in_context(
h_dealloc, "cuMemFreeAsync", handle_bits(b->resource),
[&]() noexcept {
return p_cuMemFreeAsync(b->resource, as_cu(stream.h_stream)); // raw: under ipc_import_mutex
},
[&]() noexcept { lock.release()->unlock(); });
[&]() noexcept {
ipc_release_cv.notify_all();
lock.release()->unlock();
});
delete b;
}
);
// clang-format on
DevicePtrHandle h(box, &box->resource);
ipc_ptr_cache.register_handle(key, h);
ipc_ptr_cache[key] = IpcCacheEntry{h, box.get()};
return h;
}

Expand Down
4 changes: 4 additions & 0 deletions cuda_core/cuda/core/_memoryview.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,10 @@ cdef inline int _smv_dtype_numpy_to_dlpack(object dtype_obj, DLDataType* out_dty
if bits not in (64, 128):
raise BufferError(f"Unsupported complex dtype: {np_dtype}")
code = <uint8_t>kDLComplex
elif np_dtype.type is bfloat16:
# ml_dtypes bfloat16 has kind "V", so the kind chain cannot reach it;
# the byte-order check above has already run for it.
code = <uint8_t>kDLBfloat
else:
raise BufferError(f"Unsupported dtype for DLPack export: {np_dtype}")

Expand Down
5 changes: 5 additions & 0 deletions cuda_core/docs/source/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ If you upgrade ``cuda-core`` alone, the installed ``cuda-bindings`` can be older
release requires. ``import cuda.core`` then reports the required version and the ``pip`` command
that installs it.

Do not pin ``cuda-bindings`` in your own requirements. Depend on ``cuda-core`` with the ``cu12``
or ``cu13`` extra, and let ``cuda-core`` choose the ``cuda-bindings`` version. A pin of your own
can conflict with the floor of a later ``cuda-core`` release.


Installing from Conda (conda-forge)
-----------------------------------
Expand All @@ -73,6 +77,7 @@ and likewise use ``cuda-version=13`` for CUDA 13.

The conda-forge package depends on ``cuda-bindings`` of the same CUDA major. The
``cuda-bindings`` floor of the release also applies to it. See :ref:`cuda-core-bindings-floor`.
As with ``pip``, do not pin ``cuda-bindings`` yourself. Let ``cuda-core`` choose it.


Development environment
Expand Down
2 changes: 1 addition & 1 deletion cuda_core/docs/source/support.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ all read them from there.
A floor moves only for one of two reasons. A ``cuda-core`` release needs a newer
``cuda-bindings`` API or fix, or its wheels are built against a newer CUDA minor version. A new
``cuda-bindings`` release alone does not move a floor, and a ``cuda-core`` release alone does
not. The :doc:`release notes <release>` list every move under "Breaking Changes".
not. The :doc:`release notes <release>` list every move.

Python Version Support
----------------------
Expand Down
23 changes: 21 additions & 2 deletions cuda_core/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ def init_cuda(request):
# live_at_setup stays None until the setup snapshot is recorded.
device.set_current()
live_at_setup = None
primary_error = None
try:
# Set option to avoid spin-waiting on synchronization.
if int(os.environ.get("CUDA_CORE_TEST_BLOCKING_SYNC", 0)) != 0:
Expand Down Expand Up @@ -275,6 +276,9 @@ def init_cuda(request):
# not counted, so no cached-default bookkeeping is needed.
live_at_setup = _live_resource_counts()
yield device
except BaseException as exc:
primary_error = exc
raise
finally:
# pytest holds fixture values in item.funcargs (a pytest-internal dict)
# until all teardown has run; we deliberately drop them so already
Expand All @@ -298,8 +302,23 @@ def init_cuda(request):
# With pytest-run-parallel this runs after worker join. This also runs
# when setup raises, so an escalated leftover warning cannot leave the
# context current.
driver.cuCtxSynchronize()
_ = _device_unset_current()
# The test wrapper may already have popped the calling thread's
# context; synchronize the fixture device's bound context explicitly.
# Keep the first failure primary; cleanup still runs for interrupts.
try:
device.sync()
except BaseException as exc:
if primary_error is None:
primary_error = exc
raise
primary_error.add_note(f"CUDA fixture synchronization failed: {type(exc).__name__}: {exc}")
finally:
try:
_ = _device_unset_current()
except BaseException as exc:
if primary_error is None:
raise
primary_error.add_note(f"CUDA fixture context pop failed: {type(exc).__name__}: {exc}")
# After the cleanup, so an escalated warning cannot skip it.
if live_after is not None and any(now < snap for now, snap in zip(live_after, live_at_setup)):
released = tuple(s - n for s, n in zip(live_at_setup, live_after))
Expand Down
5 changes: 5 additions & 0 deletions cuda_core/tests/graph/test_graph_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,11 @@ def test_alloc_memory_type_host(init_cuda):
"""HOST graph alloc nodes reconstruct memory_type from the driver, not the Python argument."""
skip_if_mempool_unsupported()
from cuda.core._utils.cuda_utils import CUDAError
from cuda.core._utils.version import driver_version

# CUDA 12 rejects HOST pool properties before graph support is checked.
if driver_version() < (13, 0, 0):
pytest.skip("CUDA drivers before 13.0 reject HOST allocation properties")

g = GraphDefinition()
try:
Expand Down
12 changes: 10 additions & 2 deletions cuda_core/tests/helpers/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,17 @@ def assert_device_operations_use_bound_context(device):
# Live check: query the driver directly, rather than comparing cached
# metadata, to confirm the stream was actually created in the bound
# context rather than whatever was ambient.
driver_stream_ctx = handle_return(driver.cuStreamGetCtx(stream.handle))
if bound_context.is_green:
# The legacy query rejects green streams on CUDA 12.9. In v2,
# pCtx is the primary context; pGreenCtx identifies the green context.
_, driver_green_ctx = handle_return(driver.cuStreamGetCtx_v2(stream.handle))
assert int(driver_green_ctx) != 0, "The stream must be associated with a green context."
driver_stream_ctx = handle_return(driver.cuCtxFromGreenCtx(driver_green_ctx))
else:
# Keep the legacy query for regular contexts on older drivers.
driver_stream_ctx = handle_return(driver.cuStreamGetCtx(stream.handle))
assert int(driver_stream_ctx) == int(bound_context.handle), (
"cuStreamGetCtx reports a context other than the one the stream was created in."
"The driver reports a context other than the one the stream was created in."
)

event = device.create_event()
Expand Down
112 changes: 111 additions & 1 deletion cuda_core/tests/memory/test_init_cuda_collect_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
UnreleasedMemoryResourceWarning,
)

from cuda.core._utils.cuda_utils import driver, handle_return
from cuda.core import Device
from cuda.core._utils.cuda_utils import CUDAError, driver, handle_return


class _ScriptedCounts:
Expand Down Expand Up @@ -250,3 +251,112 @@ def test_teardown_warns_on_baseline_release(request, monkeypatch):
pytest.raises(StopIteration),
):
next(gen) # teardown: live_after == (0, 0) -> baseline-release warning


@_THREAD_UNSAFE_MARKER
@pytest.mark.agent_authored(model="gpt-6")
@pytest.mark.parametrize("clear_current", [False, True])
def test_teardown_synchronizes_fixture_context(request, monkeypatch, clear_current):
"""Teardown works even after the test wrapper has popped the current context."""
func = _init_cuda_func(request)
_patch_conftest_global(monkeypatch, func, "_mr_expected_live", None)
_patch_conftest_global(monkeypatch, func, "_live_resource_counts", _ScriptedCounts((0, 0)))

saved = _pop_context_stack()
gen = func(_FakeRequest())
try:
next(gen)
if clear_current:
handle_return(driver.cuCtxPopCurrent())
assert current_context_handle() == 0
_finish_teardown(gen)
assert current_context_handle() == 0
finally:
gen.close()
_push_context_stack(saved)


@_THREAD_UNSAFE_MARKER
@pytest.mark.agent_authored(model="gpt-6")
def test_teardown_sync_failure_still_pops_context(request, monkeypatch):
"""A synchronization error propagates without leaving the context current."""
func = _init_cuda_func(request)
_patch_conftest_global(monkeypatch, func, "_mr_expected_live", None)
_patch_conftest_global(monkeypatch, func, "_live_resource_counts", _ScriptedCounts((0, 0)))
failure = CUDAError("injected synchronization failure")

class FailingSyncDevice:
device_id = 0

def set_current(self):
Device(0).set_current()

def sync(self):
raise failure

_patch_conftest_global(monkeypatch, func, "Device", lambda _: FailingSyncDevice())
saved = _pop_context_stack()
gen = func(_FakeRequest())
try:
next(gen)
with pytest.raises(CUDAError) as excinfo:
next(gen)
assert excinfo.value is failure
assert current_context_handle() == 0
finally:
gen.close()
_push_context_stack(saved)


@_THREAD_UNSAFE_MARKER
@pytest.mark.agent_authored(model="gpt-6")
@pytest.mark.parametrize(
"fail_setup, fail_sync, fail_pop",
[(True, True, True), (True, False, True), (True, True, False), (False, True, True), (False, False, True)],
)
@pytest.mark.parametrize("error_type", [RuntimeError, KeyboardInterrupt])
def test_cleanup_preserves_first_failure(request, monkeypatch, fail_setup, fail_sync, fail_pop, error_type):
"""Later cleanup errors annotate the first failure, and pop is always attempted."""
func = _init_cuda_func(request)
_patch_conftest_global(monkeypatch, func, "_mr_expected_live", None)
monkeypatch.setenv("CUDA_CORE_TEST_BLOCKING_SYNC", "0")
setup_error = error_type("injected setup failure")
sync_error = error_type("injected synchronization failure")
pop_error = error_type("injected context pop failure")
state = {"current": False}

class FakeDevice:
def set_current(self):
state["current"] = True

def sync(self):
if fail_sync:
raise sync_error

def counts():
if fail_setup:
raise setup_error
return (0, 0)

def pop():
state["current"] = False
if fail_pop:
raise pop_error

_patch_conftest_global(monkeypatch, func, "Device", lambda _: FakeDevice())
_patch_conftest_global(monkeypatch, func, "_live_resource_counts", counts)
_patch_conftest_global(monkeypatch, func, "_device_unset_current", pop)
gen = func(_FakeRequest())
if not fail_setup:
next(gen)
with pytest.raises(error_type) as excinfo:
next(gen)
expected = setup_error if fail_setup else sync_error if fail_sync else pop_error
assert excinfo.value is expected
assert not state["current"]
notes = getattr(expected, "__notes__", [])
assert len(notes) == int(fail_setup and fail_sync) + int(fail_pop and (fail_setup or fail_sync))
if fail_setup and fail_sync:
assert any(str(sync_error) in note for note in notes)
if fail_pop and (fail_setup or fail_sync):
assert any(str(pop_error) in note for note in notes)
Loading
Loading