Skip to content

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

Merged
rparolin merged 3 commits into
NVIDIA:mainfrom
rluo8:fix/torch-tensor-type-guard
Nov 25, 2026
Merged

rparolin merged 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 Resolved
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor
Doc Preview CI
Preview removed because the pull request was closed or merged.

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.

@rparolin
rparolin merged commit ca3fe58 into NVIDIA:main Nov 25, 2026
224 of 228 checks passed
@leofang

leofang commented Nov 25, 2026

Copy link
Copy Markdown
Member

Sorry I did not have a chance to circle back before this got merged. @rluo8 Could you benchmark the SMV creation time before/after this PR?

@rluo8

rluo8 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Hi @leofang , I reran the original PR comparison using pyperf 2.11.0: 76239506a1b (before) vs. ca3fe586ce0 (after).

Environment: B10, AMD EPYC 9124, RTX PRO 4500 Blackwell, Python 3.14.6, PyTorch 2.15.0+cu132, and cuda-bindings 13.5.0.

Each case used 20 measurement worker processes per version, pinned to CPU 2, in a before-after-after-before sequence to reduce timing drift. Timing includes SMV creation and immediate release; input allocation and GPU synchronization were outside timing.

Results are mean ± standard deviation in ns:

Case Before After Change
CPU Tensor / from_dlpack 366.9 ± 9.4 350.6 ± 2.9 -4.45%
GPU Tensor / from_dlpack 371.2 ± 3.4 358.9 ± 9.1 -3.31%
CPU Tensor / from_any_interface 482.6 ± 5.4 484.5 ± 4.9 +0.38%
GPU Tensor / from_any_interface 489.1 ± 6.0 491.8 ± 11.3 +0.56%
NumPy / from_dlpack 360.9 ± 2.2 349.3 ± 3.0 -3.21%
NumPy / from_array_interface 1375.1 ± 20.6 1387.2 ± 11.4 +0.88%
Protocol adapter / from_dlpack 779.8 ± 7.6 773.4 ± 5.2 -0.82%
Protocol adapter / from_any_interface 894.8 ± 5.2 914.8 ± 5.7 +2.24%
New Tensor subclass / first from_dlpack 817.8 ± 57.0 868.4 ± 57.4 +6.19%

pyperf found no statistically significant difference for CPU/GPU Tensor from_any_interface. The adapter's from_any_interface increased by about 20 ns.

The last row is a synthetic cache-miss diagnostic: each call uses a previously unseen Tensor subclass, created outside timing. Its approximately 51 ns increase includes cache growth and does not represent repeated calls with the same type.
Would the additional overhead in the adapter and first-use cases be acceptable?
Thanks!

@leofang

leofang commented Nov 25, 2026

Copy link
Copy Markdown
Member

LGTM, thanks Rui!

github-actions Bot pushed a commit that referenced this pull request Nov 25, 2026
Removed preview folders for the following PRs:
- PR #3499
- PR #3524
- PR #3543
- PR #3544
- PR #3546
- PR #3551
@rluo8
rluo8 deleted the fix/torch-tensor-type-guard branch November 25, 2026 04:51
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