Skip to content
Open
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
24 changes: 22 additions & 2 deletions cuda_core/cuda/core/_memoryview.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ cdef object _tensor_bridge = None
# Cache: type(obj) -> True/False for the torch tensor check.
# Once a type is seen, we never re-check.
cdef dict _torch_type_cache = {}
cdef object _torch_tensor_type = None
# Tri-state: None = not checked, True/False = result of version check
cdef object _torch_version_ok = None

Expand Down Expand Up @@ -82,14 +83,33 @@ cdef inline bint _torch_version_check():
return <bint>_torch_version_ok


cdef object _get_torch_tensor_type():
global _torch_tensor_type
with cython.critical_section(_torch_type_cache):
if _torch_tensor_type is None:
torch = sys.modules.get("torch")
tensor_type = getattr(torch, "Tensor", None)
if tensor_type is None:
# Torch may be imported after an earlier protocol-only call.
return None
_torch_tensor_type = tensor_type
if not _torch_version_check():
return None
return _torch_tensor_type


cdef inline bint _is_torch_tensor(object obj):
cdef type tp = type(obj)
cdef object cached = _torch_type_cache.get(tp)
if cached is not None:
return <bint>cached
cdef str mod = tp.__module__ or ""
cdef bint result = mod.startswith("torch") and hasattr(obj, "data_ptr") \
and _torch_version_check()
cdef object tensor_type
cdef bint result = mod.startswith("torch") and hasattr(obj, "data_ptr")
if result:
tensor_type = _get_torch_tensor_type()
# The bridge requires a real Tensor, not just Tensor-like attributes.
result = tensor_type is not None and issubclass(tp, tensor_type)
_torch_type_cache[tp] = result # setdefault not needed for bools
return result

Expand Down
5 changes: 5 additions & 0 deletions cuda_core/docs/source/release/1.4.0-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
Fixes and enhancements
----------------------

- ``StridedMemoryView`` now verifies the actual Tensor type before using the
PyTorch native bridge. Protocol adapters with a ``torch``-prefixed module
name and a ``data_ptr`` attribute now use their declared data exchange
protocol instead of failing or being passed to the native Tensor interface.

- Kernels, :meth:`Buffer.fill` and asynchronous copies now work on
:class:`PinnedMemoryResource` buffers on every device that supports the
pool's kind of host memory pool, as on ``cudaMallocHost`` memory. In
Expand Down
291 changes: 291 additions & 0 deletions cuda_core/tests/test_utils_dlpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import numpy as np
import pytest

from cuda.bindings import driver
from cuda.core._dlpack import DLDeviceType
from cuda.core.utils import StridedMemoryView

Expand Down Expand Up @@ -355,6 +356,296 @@ def test_from_dlpack_typeerror_fallback_unversioned_import():
del view # exercise __dealloc__ on the imported (used) unversioned capsule


@pytest.fixture(scope="module")
def torch_module():
# Module scope checks Torch before function-scoped CUDA setup.
return pytest.importorskip("torch", minversion="2.3")


@pytest.mark.parametrize("entry", ["from_dlpack", "from_any_interface", "from_array_interface"])
@pytest.mark.parametrize("module", ["application_adapter", "torch_adapter", "torch.adapters", "torchvision.adapters"])
@pytest.mark.parametrize("has_data_ptr", [False, True])
@pytest.mark.agent_authored(model="gpt-6")
def test_protocol_adapter_uses_protocol_on_first_and_cached_import(entry, module, has_data_ptr, torch_module):
"""Non-Tensor adapters retain their protocol routing on repeated imports."""

def dlpack(self, **kwargs):
self.calls["dlpack"] += 1
return self.array.__dlpack__(**kwargs)

def array_interface(self):
self.calls["array"] += 1
return self.array.__array_interface__

namespace = {
"__module__": module,
"__dlpack_device__": lambda self: self.array.__dlpack_device__(),
"__dlpack__": dlpack,
"__array_interface__": property(array_interface),
}
if has_data_ptr:
namespace["data_ptr"] = lambda self: self.array.ctypes.data
adapter_type = type("ProtocolAdapter", (), namespace)
for _ in range(2):
adapter = adapter_type()
adapter.array = np.arange(4, dtype=np.int32)
adapter.calls = {"dlpack": 0, "array": 0}
kwargs = {} if entry == "from_array_interface" else {"stream_ptr": -1}
view = getattr(StridedMemoryView, entry)(adapter, **kwargs)
assert view.ptr == adapter.array.ctypes.data
np.testing.assert_array_equal(np.from_dlpack(view), adapter.array)
assert adapter.calls["array" if entry == "from_array_interface" else "dlpack"] > 0
assert adapter.calls["dlpack" if entry == "from_array_interface" else "array"] == 0


@pytest.mark.parametrize("entry", ["from_dlpack", "from_any_interface"])
@pytest.mark.parametrize("legacy_dlpack", [False, True])
@pytest.mark.agent_authored(model="gpt-6")
def test_class_proxy_is_not_a_native_tensor(entry, legacy_dlpack, torch_module):
"""A proxy advertising Tensor through __class__ still uses its DLPack protocol."""
from cuda_python_test_helpers.subprocess_runner import run_python_snippet
from helpers.child_processes import child_timeout_sec

code = f"""import sys,ctypes,numpy as np,torch
from cuda.core.utils import StridedMemoryView
if sys.platform == "win32":
ctypes.windll.kernel32.SetErrorMode(3)
class Proxy:
__module__ = "torch_adapter"

@property
def __class__(self):
return torch.Tensor

def data_ptr(self):
return self.array.ctypes.data

def __dlpack_device__(self):
return self.array.__dlpack_device__()

def __dlpack__(self, **kwargs):
self.calls += 1
if {legacy_dlpack!r} and "max_version" in kwargs:
raise TypeError("max_version is not supported")
return self.array.__dlpack__(**kwargs)

adapter = Proxy()
adapter.array = np.arange(4, dtype=np.int32)
adapter.calls = 0
assert isinstance(adapter, torch.Tensor)
assert not issubclass(type(adapter), torch.Tensor)
view = getattr(StridedMemoryView, {entry!r})(adapter, stream_ptr=-1)
np.testing.assert_array_equal(np.from_dlpack(view), adapter.array)
assert adapter.calls > 0
"""
run_python_snippet(code, timeout=child_timeout_sec(), extra_env={"PYTHONFAULTHANDLER": "1"})


@pytest.mark.parametrize("entry", ["from_dlpack", "from_any_interface"])
@pytest.mark.parametrize(
("module", "legacy_dlpack"),
[(None, False), ("torch_custom", False), ("application_tensor", False), ("application_tensor", True)],
)
@pytest.mark.agent_authored(model="gpt-6")
def test_real_tensor_and_subclasses_keep_existing_routes(entry, module, legacy_dlpack, torch_module):
"""Real Tensors and subclasses preserve their data and existing import routes."""
torch = torch_module
calls = []

def dlpack(self, **kwargs):
calls.append(kwargs)
if legacy_dlpack and "max_version" in kwargs:
raise TypeError("max_version is not supported")
return torch.Tensor.__dlpack__(self, **kwargs)

array = torch.arange(4, dtype=torch.int32)
if module is not None:
tensor_type = type("TensorSubclass", (torch.Tensor,), {"__module__": module, "__dlpack__": dlpack})
array = array.as_subclass(tensor_type)
for _ in range(2):
calls_before = len(calls)
view = getattr(StridedMemoryView, entry)(array, stream_ptr=-1)
assert view.ptr == array.data_ptr()
np.testing.assert_array_equal(np.from_dlpack(view), np.arange(4, dtype=np.int32))
if module == "application_tensor":
assert len(calls) > calls_before
else:
assert len(calls) == calls_before


@pytest.mark.parametrize("entry", ["from_cuda_array_interface", "from_any_interface"])
@pytest.mark.parametrize("module", ["application_cuda", "torch_cuda_adapter", "torchvision.adapters"])
@pytest.mark.agent_authored(model="gpt-6")
def test_cuda_adapter_uses_real_device_storage(init_cuda, entry, module, torch_module):
"""Torch-looking CAI adapters import real device storage through their protocol."""
source = np.arange(4, dtype=np.int32)
buffer = init_cuda.allocate(source.nbytes, stream=init_cuda.default_stream)
try:
init_cuda.sync()
ptr = int(buffer.handle)
(status,) = driver.cuMemcpyHtoD(ptr, source.ctypes.data, source.nbytes)
assert int(status) == 0
calls = []

def interface(self):
calls.append(True)
return {"shape": (4,), "strides": None, "typestr": source.dtype.str, "data": (ptr, False), "version": 3}

adapter_type = type(
"CudaAdapter",
(),
{"__module__": module, "data_ptr": lambda _self: ptr, "__cuda_array_interface__": property(interface)},
)
for _ in range(2):
view = getattr(StridedMemoryView, entry)(adapter_type(), stream_ptr=-1)
assert view.ptr == ptr
actual = np.empty_like(source)
(status,) = driver.cuMemcpyDtoH(actual.ctypes.data, view.ptr, actual.nbytes)
assert int(status) == 0
np.testing.assert_array_equal(actual, source)
assert len(calls) > 0
finally:
buffer.close()


@pytest.mark.agent_authored(model="gpt-6")
def test_protocol_import_does_not_load_torch():
"""A Torch-looking protocol adapter imports successfully without importing Torch."""
from cuda_python_test_helpers.subprocess_runner import run_python_snippet
from helpers.child_processes import child_timeout_sec

code = """import sys,numpy as np
from cuda.core.utils import StridedMemoryView
assert "torch" not in sys.modules
class Adapter:
__module__="torch_adapter"
def __init__(self):self.array=np.arange(4,dtype=np.int32)
def data_ptr(self):return self.array.ctypes.data
def __dlpack_device__(self):return self.array.__dlpack_device__()
def __dlpack__(self,**kwargs):return self.array.__dlpack__(**kwargs)
x=Adapter();v=StridedMemoryView.from_dlpack(x,stream_ptr=-1)
np.testing.assert_array_equal(np.from_dlpack(v),x.array)
assert "torch" not in sys.modules
"""
run_python_snippet(code, timeout=child_timeout_sec(), extra_env={"PYTHONFAULTHANDLER": "1"})


@pytest.mark.parametrize("entry", ["from_dlpack", "from_any_interface", "from_array_interface"])
@pytest.mark.parametrize(
("module", "has_data_ptr"),
[("application_adapter", False), ("application_adapter", True), ("torch_adapter", False)],
)
@pytest.mark.agent_authored(model="gpt-6")
def test_protocol_import_keeps_unrelated_torch_module_lazy(entry, module, has_data_ptr):
"""Protocol adapters outside the Torch candidate filter do not initialize lazy Torch."""
from cuda_python_test_helpers.subprocess_runner import run_python_snippet
from helpers.child_processes import child_timeout_sec

code = f"""import importlib.abc
import importlib.util
import sys
import numpy as np
from cuda.core.utils import StridedMemoryView

class RaisingTorchLoader(importlib.abc.Loader):
def __init__(self):
self.calls = 0
def create_module(self, spec):
return None
def exec_module(self, module):
self.calls += 1
raise AssertionError("Unrelated protocol import initialized Torch")

loader = RaisingTorchLoader()
spec = importlib.util.spec_from_loader("torch", importlib.util.LazyLoader(loader))
torch_module = importlib.util.module_from_spec(spec)
sys.modules["torch"] = torch_module
spec.loader.exec_module(torch_module)

class Adapter:
__module__ = {module!r}
def __init__(self):
self.array = np.arange(4, dtype=np.int32)
def __dlpack_device__(self):
return self.array.__dlpack_device__()
def __dlpack__(self, **kwargs):
return self.array.__dlpack__(**kwargs)
@property
def __array_interface__(self):
return self.array.__array_interface__

if {has_data_ptr!r}:
Adapter.data_ptr = lambda self: self.array.ctypes.data
adapter = Adapter()
if {entry!r} == "from_array_interface":
view = StridedMemoryView.from_array_interface(adapter)
else:
view = getattr(StridedMemoryView, {entry!r})(adapter, stream_ptr=-1)
assert view.ptr == adapter.array.ctypes.data
np.testing.assert_array_equal(np.from_dlpack(view), adapter.array)
assert loader.calls == 0
"""
run_python_snippet(code, timeout=child_timeout_sec(), extra_env={"PYTHONFAULTHANDLER": "1"})


@pytest.mark.agent_authored(model="gpt-6")
def test_torch_tensor_type_lookup_is_cached(torch_module):
"""Distinct candidate types reuse Torch metadata after a protocol-only import."""
from cuda_python_test_helpers.subprocess_runner import run_python_snippet
from helpers.child_processes import child_timeout_sec

code = """import sys
import types
import numpy as np
from cuda.core.utils import StridedMemoryView

class Adapter:
def __init__(self):
self.array = np.arange(4, dtype=np.int32)
def data_ptr(self):
return self.array.ctypes.data
def __dlpack_device__(self):
return self.array.__dlpack_device__()
def __dlpack__(self, **kwargs):
return self.array.__dlpack__(**kwargs)

def import_adapter():
adapter = type("Adapter", (Adapter,), {"__module__": "torch_adapter"})()
view = StridedMemoryView.from_dlpack(adapter, stream_ptr=-1)
np.testing.assert_array_equal(np.from_dlpack(view), adapter.array)

assert "torch" not in sys.modules
import_adapter()
assert "torch" not in sys.modules
import torch

lookups = {"Tensor": 0, "__version__": 0}
class TrackedTorch(types.ModuleType):
def __getattribute__(self, name):
if name in lookups:
lookups[name] += 1
return super().__getattribute__(name)

original_type = type(torch)
torch.__class__ = TrackedTorch
try:
import_adapter()
assert lookups["Tensor"] == 1
first_lookups = lookups.copy()
for _ in range(2):
import_adapter()
assert lookups == first_lookups, lookups
finally:
torch.__class__ = original_type

array = torch.arange(4, dtype=torch.int32)
view = StridedMemoryView.from_dlpack(array, stream_ptr=-1)
assert view.ptr == array.data_ptr()
np.testing.assert_array_equal(np.from_dlpack(view), np.arange(4, dtype=np.int32))
"""
run_python_snippet(code, timeout=child_timeout_sec(), extra_env={"PYTHONFAULTHANDLER": "1"})


# ---------------------------------------------------------------------------
# DLPack C exchange API (`__dlpack_c_exchange_api__`)
#
Expand Down
Loading