You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is to fix internal bug 7118195. StridedMemoryView can mistake a non-Tensor protocol adapter for a PyTorch Tensor when its type has a torch-prefixed module name and a data_ptr attribute. This bypasses the adapter's declared protocol and can cause a native bridge error or, for a __class__ proxy, an access violation.
Check issubclass(type(obj), torch.Tensor) before selecting the native bridge. Preserve the existing candidate filters, type cache, and version gate without eagerly importing Torch.
Add regression coverage in test_utils_dlpack.py for protocol adapters, __class__ proxies, real Tensors and subclasses, cached routing, and lazy Torch loading.
Validation:
52 regression cases passed on Windows and Linux, including Torch 2.15 GPU and Torch 2.3 CPU environments.
CUDA 12.9 headers / bindings 12.9.10: rebuilt the cu12 extension; all 52 cases passed.
Missing or unsupported Torch: 12 passed, 42 skipped without CUDA initialization.
Improved handling of PyTorch tensors and compatible subclasses when exchanging data through the native bridge.
Torch-prefixed adapters that provide a data pointer now use their declared data exchange protocol, helping ensure data is interpreted correctly.
Torch detection can be retried when PyTorch is loaded after an earlier protocol-based check, and unsupported Torch versions are handled safely.
The change caches the resolved torch.Tensor class and checks candidate objects against it before selecting the PyTorch native bridge. Tests cover DLPack and CUDA array-interface routing, Torch tensors and subclasses, and Torch loading and metadata lookup.
Changes: PyTorch Tensor Detection
Layer / File(s)
Summary
Tensor type gate cuda_core/cuda/core/_memoryview.pyx, cuda_core/docs/source/release/1.4.0-notes.rst
The code caches the resolved torch.Tensor class and checks candidate objects for subclass compatibility. The release notes describe this check and protocol-based handling for torch-prefixed adapters with data_ptr.
Tests cover protocol routing across repeated imports, reported-versus-concrete tensor types, real tensors and subclasses, CUDA array interface, lazy Torch loading, and cached Torch metadata lookups.
Possibly related PRs
NVIDIA/cuda-python#3033: Both PRs change bridge-selection logic in StridedMemoryView, so their Torch bridge paths and version gates need to be reconciled.
Suggested labels:bug
Suggested reviewers:leofang, mdboom
Change: Bug fix
Merge Risk: Minimal · up to b9068
This change makes the PyTorch native bridge apply only to real Tensor types. Protocol adapters that merely look like Torch now use their declared protocol. The remaining lazy-module note predates this PR and is low impact, so the change is ready to merge once the PR metadata check passes.
Architecture risk: Low · up to b9068
The change affects 1 system.
Changed systems:cuda_core
Architecture concerns
No architecture-level concerns identified.
Review details
Systems and components
observed — cuda_core (service) was modified; 3 changed files map to changed impact.
Before / after behavior
observed — Modified behavior in cuda_core/cuda/core/_memoryview.pyx: Adds a cached slot for the resolved torch.Tensor class.
observed — Modified behavior in cuda_core/cuda/core/_memoryview.pyx: Adds _get_torch_tensor_type, which looks up torch.Tensor under the type-cache critical section, returns None if Torch or its Tensor class is unavailable or the version check fails, and caches the class once found.
observed — Modified behavior in cuda_core/cuda/core/_memoryview.pyx: _is_torch_tensor no longer includes the Torch version check in its initial module-and-data_ptr test. For candidates, it now resolves the Tensor class and requires the object's type to be its subclass; the result remains cached by type.
observed — Modified behavior in cuda_core/docs/source/release/1.4.0-notes.rst: The release notes add a description of Tensor type verification before using the PyTorch native bridge and protocol-based handling for torch-prefixed adapters with a data_ptr attribute.
Finishing Touches
Fix failing CI checks
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Reviewing files that changed from the base of the PR and between 5d81fc0 and 383b7ba.
Files selected for processing: 3
cuda_core/cuda/core/_memoryview.pyx
cuda_core/docs/source/release/1.3.1-notes.rst
cuda_core/tests/test_utils_dlpack.py
Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.
suggestion: Test routing behavior without exact metadata lookup counts.
Line 633 requires exactly one Tensor attribute read. A valid implementation that performs two initial reads and then caches the class would fail this test. Keep the adapter round-trips and real-Tensor checks, but remove the internal lookup-count assertions.
As per coding guidelines, “Avoid asserting on internal call counts.”
Reviewing files that changed from the base of the PR and between 383b7ba and 357341a.
Files selected for processing: 2
cuda_core/cuda/core/_memoryview.pyx
cuda_core/tests/test_utils_dlpack.py
Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 10 remain after this review.
The reason will be displayed to describe this comment to others. Learn more.
Minor comments: 1
cuda_core/cuda/core/_memoryview.pyx: 1
91-91: Minor · Functional Correctness · Poor tradeoff
suggestion: Keep the Tensor lookup from loading a lazy torch module.
When sys.modules["torch"] is a importlib.util.LazyLoader module, getattr(torch, "Tensor", None) runs the Torch loader. A torch-prefixed object with data_ptr reaches this lookup before the protocol path runs. If the Torch import fails, the exception propagates from this call, because getattr suppresses only AttributeError. The existing _torch_version_check() already reads torch.__version__ for this input, so the load predates this change. The new lookup keeps that behavior.
Do not read attributes of a lazy torch module in this check. Keep native routing when Torch is already loaded.
Add the ("torch_adapter", True) case to test_protocol_import_keeps_unrelated_torch_module_lazy. With the current lookup, that case would fail because the loader runs.
Reviewing files that changed from the base of the PR and between 357341a and b906870.
Files selected for processing: 2
cuda_core/docs/source/release/1.4.0-notes.rst
cuda_core/tests/test_utils_dlpack.py
Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.
This branch has not been deployed
No deployments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bugSomething isn't workingcuda.coreEverything related to the cuda.core module
3 participants
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This is to fix internal bug 7118195.
StridedMemoryViewcan mistake a non-Tensor protocol adapter for a PyTorch Tensor when its type has atorch-prefixed module name and adata_ptrattribute. This bypasses the adapter's declared protocol and can cause a native bridge error or, for a__class__proxy, an access violation.Check
issubclass(type(obj), torch.Tensor)before selecting the native bridge. Preserve the existing candidate filters, type cache, and version gate without eagerly importing Torch.Add regression coverage in
test_utils_dlpack.pyfor protocol adapters,__class__proxies, real Tensors and subclasses, cached routing, and lazy Torch loading.Validation: