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
{{ message }}
Repository navigation
fix(cuda.core): keep DLPack owner alive for derived StridedMemoryView - #3378
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.
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
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.
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.
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.
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
2 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 7049151 .
StridedMemoryView.view()on a view imported viafrom_dlpack()does not keep theDLPack tensor alive.
get_buffer()makes the derived view'sBufferowned by theproducer 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 aBufferowned by the viewitself. It is not cached, to avoid a reference cycle. This matches
__dlpack__export (
manager_ctx = view) andTensorMapDescriptor, which already keeps theview alive for the same reason.
Note: each
view()call on a DLPack-imported view now creates its ownBuffer, sosibling views no longer share one
exporting_objinstance.Added
test_dlpack_derived_view_keeps_capsule_owned_storage_alive, which failsbefore the fix and passes after.
Checklist