Skip to content

fix(cuda.core): keep DLPack owner alive for derived StridedMemoryView - #3378

Open
rluo8 wants to merge 3 commits into
NVIDIA:mainfrom
rluo8:fix/dlpack-derived-view-owner
Open

rluo8 wants to merge 3 commits into
NVIDIA:mainfrom
rluo8:fix/dlpack-derived-view-owner

Conversation

@rluo8

@rluo8 rluo8 commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Description

This is to fix internal bug 7049151 .

StridedMemoryView.view() on a view imported via from_dlpack() does not keep the
DLPack tensor alive. get_buffer() makes the derived view's Buffer owned by the
producer object, but the capsule (and its deleter) is held by the original view.
When the capsule is the only owner of the storage (DLPack allows this, e.g. a
producer that returns a copy), releasing the original view frees the storage while
the derived view still points into it.

Fix: for DLPack-imported views, get_buffer() returns a Buffer owned by the view
itself. It is not cached, to avoid a reference cycle. This matches __dlpack__
export (manager_ctx = view) and TensorMapDescriptor, which already keeps the
view alive for the same reason.

Note: each view() call on a DLPack-imported view now creates its own Buffer, so
sibling views no longer share one exporting_obj instance.

Added test_dlpack_derived_view_keeps_capsule_owned_storage_alive, which fails
before the fix and passes after.

Checklist

  • I have read the Contributing Guidelines.
  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

@copy-pr-bot

copy-pr-bot Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@github-actions github-actions Bot added the cuda.core Everything related to the cuda.core module label Nov 5, 2026
@rluo8
rluo8 marked this pull request as ready for review November 5, 2026 09:29
@coderabbitai

coderabbitai Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Explain Change →

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Views derived from DLPack-imported data now keep the underlying tensor available and readable for as long as those views exist. The tensor is released when the last derived view is no longer in use, preventing premature release while avoiding unnecessary retention.

DLPack-backed StridedMemoryView objects now receive an uncached Buffer owned by the view. A test checks that a derived view keeps its array alive and releases it when the derived view is deleted. The release notes describe this behavior.

Changes: DLPack view lifetime

Layer / File(s) Summary
View-owned buffer and lifetime test
cuda_core/cuda/core/_memoryview.pyx, cuda_core/tests/test_utils_dlpack.py, cuda_core/docs/source/release/1.3.0-notes.rst
get_buffer creates an uncached Buffer owned by a view holding a DLPack tensor. The test checks that a derived view retains and reads the array, then releases it when deleted. The release notes describe the fix.

Possibly related issues

  • Issue 1989 covers lifetime management in StridedMemoryView.from_dlpack(). This change addresses storage lifetime for views derived from a DLPack import.

Suggested labels: bug

Suggested reviewers: andy-jost, mdboom

Change: Bug fix

Merge Risk: Minimal · up to 24d35

The fix protects ordinary DLPack exporters, but a pre-existing custom Buffer-subclass edge case remains.

- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

@rluo8
rluo8 requested review from Andy-Jost and seberg November 5, 2026 12:57

@Andy-Jost Andy-Jost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. Making the Buffer of a DLPack-imported view owned by the view itself, and not caching it, fixes the lifetime gap without adding a cycle. Every DLPack import path sets dl_tensor and stores the consumed capsule on the view, so the new branch covers all of them. The test checks liveness before it reads the memory, and checks the release without gc.collect().

Optional: a line in the 1.3.0 release notes would help users who hit this bug.

@rluo8 rluo8 added this to the cuda.core 1.3.0 milestone Nov 6, 2026
@rluo8 rluo8 added the bug Something isn't working label Nov 6, 2026
@rluo8 rluo8 self-assigned this Nov 6, 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.

Caution

Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

⚠️ Outside diff range comments (1)

Major · important: Retain the imported view for capsule-owned Buffer exports. · _memoryview.pyx:573-584

cuda_core/cuda/core/_memoryview.pyx:573-584
Major · Stability & Availability · Heavy lift

important: Retain the imported view for capsule-owned Buffer exports.

If a non-owning Buffer subclass exports a capsule that owns its storage, get_buffer() returns the exporter before checking dl_tensor. Deleting the imported view can then run the capsule deleter while a derived view still uses the storage. Preserve the existing Buffer reuse behavior when the Buffer itself owns the storage.

Review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between fd15442 and 24d3556.

Files selected for processing: 2

  • cuda_core/docs/source/release/1.3.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.

@rluo8

rluo8 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @Andy-Jost . Added a 1.3.0 release note in 7952575.

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.

2 participants