Skip to content

fix(cuda.core): verify Torch tensor identity - #3499

Open
rluo8 wants to merge 3 commits into
NVIDIA:mainfrom
rluo8:fix/torch-tensor-type-guard
Open

rluo8 wants to merge 3 commits into
NVIDIA:mainfrom
rluo8:fix/torch-tensor-type-guard

Conversation

@rluo8

@rluo8 rluo8 commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Description

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.
  • All applicable pre-commit checks passed.

Signed-off-by: Rui Luo <ruluo@nvidia.com>
@github-actions github-actions Bot added the cuda.core Everything related to the cuda.core module label Nov 19, 2026
@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Explain Change →

Summary

Summary by CodeRabbit

  • Bug Fixes
    • 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.
Protocol import tests
cuda_core/tests/test_utils_dlpack.py
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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review info

Run configuration

Commits

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.

Comment thread cuda_core/tests/test_utils_dlpack.py Resolved
Comment thread cuda_core/cuda/core/_memoryview.pyx Outdated
Comment on lines +94 to +96
torch = sys.modules.get("torch")
tensor_type = getattr(torch, "Tensor", None)
# The bridge requires a real Tensor, not just Tensor-like attributes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This path is performance-sensitive. Can we lazy-init and cache the type (and maybe the version check too)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @leofang for the suggestion. I've updated it to cache the torch.Tensor and reuse existing version check cache.

@github-actions

Copy link
Copy Markdown
Contributor

Signed-off-by: Rui Luo <ruluo@nvidia.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick comments: 1

cuda_core/tests/test_utils_dlpack.py: 1

633-633: Trivial · Maintainability & Code Quality · Quick win

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.”

Source: Coding guidelines

Review info

Run configuration

Commits

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.

@rluo8 rluo8 self-assigned this Nov 19, 2026
Signed-off-by: Rui Luo <ruluo@nvidia.com>
@rluo8 rluo8 added this to the cuda.core 1.4.0 milestone Nov 20, 2026
@rluo8 rluo8 added the bug Something isn't working label Nov 20, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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.

Review info

Run configuration

Commits

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working cuda.core Everything related to the cuda.core module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants