Add make_tensor method which takes ownership of DLManagedTensor object#1157
Add make_tensor method which takes ownership of DLManagedTensor object#1157simonbyrne wants to merge 19 commits intomainfrom
Conversation
|
I think this is ready. I also tried to clean up the python interop example, as I thought it was a bit confusing. |
There was a problem hiding this comment.
Pull request overview
This PR adds DLPack ownership-transfer support to MatX tensor import/export so that imported storage lifetimes are safely managed by MatX until the last view is released, with corresponding tests, docs, and Python integration sample updates.
Changes:
- Add pointer-owning
make_tensor(tensor, DLManagedTensor*)andmake_tensor(tensor, DLManagedTensorVersioned*)overloads; deprecate the legacy non-owningmake_tensor(tensor, DLManagedTensor)import. - Refactor MatX DLPack export to support both legacy (
ToDlPack) and versioned (ToDlPackVersioned) managed tensor types via a shared implementation. - Update vendored
dlpack.h, add lifetime tests for owning import, and refresh docs/examples/devcontainer to reflect the new ownership model.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
test/00_tensor/BasicTensorTests.cu |
Adds lifetime tests verifying DLPack deleter is called exactly once after last MatX reference is released. |
include/matx/core/tensor.h |
Refactors DLPack export; adds ToDlPackVersioned() and shared deleter logic. |
include/matx/core/make_tensor.h |
Introduces ownership-taking DLPack import overloads and factors dtype validation/shape-stride extraction into helpers. |
include/matx/core/dlpack.h |
Updates vendored DLPack header to newer definitions (versioned tensors + exchange API). |
examples/python_integration_sample/mypythonlib.py |
Simplifies Python-side printing of received DLPack tensor. |
examples/python_integration_sample/matxutil.cu |
Updates pybind helper to consume capsules and use pointer-owning MatX import. |
examples/python_integration_sample/example_matxutil.py |
Updates sample script to demonstrate single-consumption semantics and error cases. |
docs_input/external.rst |
Adds/expands DLPack interoperability documentation. |
docs_input/api/creation/tensors/make.rst |
Documents new DLPack-related overloads and APIs. |
docs_input/Doxyfile.in / docs_input/CMakeLists.txt |
Excludes examples from Doxygen generation. |
.devcontainer/dev.Dockerfile |
Adds Python/Sphinx deps and CuPy installation for the sample/docs workflow. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Greptile SummaryThis PR adds ownership-taking Confidence Score: 4/5Safe to merge after addressing the DLPackLaneInfo primitive-type mismatch for char/short vector specializations. All previously flagged P0/P1 issues (resource leak on validation throw, byte_offset ignored, null strides crash, kDLFloat-16 accepting matxBf16, read-only flag unhandled) are now addressed. One remaining P2 issue: DLPackLaneInfo specializations use C primitive types (char, short, int, long) whose scalar_type won't satisfy the is_same_v checks against fixed-width types (int8_t, int16_t, int32_t, int64_t) in validate_dlpack_tensor_type, breaking import roundtrips for char/short-based vector types. Low practical impact but a real correctness gap in the new lane infrastructure. include/matx/core/type_utils_both.h — DLPackLaneInfo specializations should use int8_t/uint8_t/int16_t/uint16_t instead of char/unsigned char/short/unsigned short. Important Files Changed
Sequence DiagramsequenceDiagram
participant Producer as Producer (CuPy/PyTorch)
participant Helper as make_tensor_from_capsule
participant MakeTensor as matx::make_tensor
participant Owner as shared_ptr owner
participant Tensor as tensor_t
Producer->>Helper: py::capsule ("dltensor")
Helper->>Helper: PyCapsule_SetName("used_dltensor")
Helper->>MakeTensor: make_tensor(tensor, managed*)
MakeTensor->>Owner: wrap managed* in shared_ptr immediately
MakeTensor->>MakeTensor: validate type, rank, strides, byte_offset
alt validation throws
Owner-->>Producer: shared_ptr dtor calls managed->deleter()
else success
MakeTensor->>Tensor: alias shared_ptr ties DLMgd lifetime to tensor
Tensor-->>Helper: tensor populated
end
Note over Owner,Tensor: DLMgd deleter called when last tensor copy is destroyed
Reviews (9): Last reviewed commit: "revert dockerfile changes" | Re-trigger Greptile |
7c73610 to
5a892da
Compare
…eaks on validation failure
cliffburdick
left a comment
There was a problem hiding this comment.
Lots of comments from the bot
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
I think i addressed all the reviewbot comments. |
|
/build |
1 similar comment
|
/build |
|
I merged in main. |
|
/build |
|
/build |
|
/build |
No description provided.