-
Notifications
You must be signed in to change notification settings - Fork 24.3k
DLPack: PyTorch leaks empty tensors #117273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Labels
module: dlpack
triage review
triaged
This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Comments
aorenste
added a commit
that referenced
this issue
Jan 12, 2024
Summary: The deleter is called indirectly by a std::unique_ptr<> whch only fires if there's actually data to delete. Fixes #117273 Test Plan: [ghstack-poisoned]
aorenste
added a commit
that referenced
this issue
Jan 13, 2024
Summary: When using a custom deleter InefficientStdFunctionContext was using a std::unique_ptr<> to store the pointer and call the deleter - but this failed to call the deleter if the pointer was null. Since we have a separate holder class anyway take out the std::unique_ptr<> and call the deleter directly. Fixes #117273 Test Plan: [ghstack-poisoned]
aorenste
added a commit
that referenced
this issue
Jan 13, 2024
Summary: When using a custom deleter InefficientStdFunctionContext was using a std::unique_ptr<> to store the pointer and call the deleter - but this failed to call the deleter if the pointer was null. Since we have a separate holder class anyway take out the std::unique_ptr<> and call the deleter directly. Fixes #117273 Test Plan: [ghstack-poisoned]
aorenste
added a commit
that referenced
this issue
Jan 13, 2024
Summary: When using a custom deleter InefficientStdFunctionContext was using a std::unique_ptr<> to store the pointer and call the deleter - but this failed to call the deleter if the pointer was null. Since we have a separate holder class anyway take out the std::unique_ptr<> and call the deleter directly. Fixes #117273 Test Plan: [ghstack-poisoned]
aorenste
added a commit
that referenced
this issue
Jan 13, 2024
Summary: When using a custom deleter InefficientStdFunctionContext was using a std::unique_ptr<> to store the pointer and call the deleter - but this failed to call the deleter if the pointer was null. Since we have a separate holder class anyway take out the std::unique_ptr<> and call the deleter directly. Fixes #117273 Test Plan: [ghstack-poisoned]
aorenste
added a commit
that referenced
this issue
Jan 13, 2024
Summary: When using a custom deleter InefficientStdFunctionContext was using a std::unique_ptr<> to store the pointer and call the deleter - but this failed to call the deleter if the pointer was null. Since we have a separate holder class anyway take out the std::unique_ptr<> and call the deleter directly. Fixes #117273 Test Plan: ghstack-source-id: d9a2fd5 Pull Request resolved: #117418
aorenste
added a commit
that referenced
this issue
Jan 13, 2024
Summary: When using a custom deleter InefficientStdFunctionContext was using a std::unique_ptr<> to store the pointer and call the deleter - but this failed to call the deleter if the pointer was null. Since we have a separate holder class anyway take out the std::unique_ptr<> and call the deleter directly. Fixes #117273 Test Plan: [ghstack-poisoned]
aorenste
added a commit
that referenced
this issue
Jan 13, 2024
Summary: When using a custom deleter InefficientStdFunctionContext was using a std::unique_ptr<> to store the pointer and call the deleter - but this failed to call the deleter if the pointer was null. Since we have a separate holder class anyway take out the std::unique_ptr<> and call the deleter directly. Fixes #117273 Test Plan: ghstack-source-id: f0b0128 Pull Request resolved: #117418
aorenste
added a commit
that referenced
this issue
Jan 17, 2024
…o-data tensor." Summary: When using a custom deleter InefficientStdFunctionContext was using a std::unique_ptr<> to store the pointer and call the deleter - but this failed to call the deleter if the pointer was null. Since we have a separate holder class anyway take out the std::unique_ptr<> and call the deleter directly. Fixes #117273 Test Plan: [ghstack-poisoned]
aorenste
added a commit
that referenced
this issue
Jan 17, 2024
Summary: When using a custom deleter InefficientStdFunctionContext was using a std::unique_ptr<> to store the pointer and call the deleter - but this failed to call the deleter if the pointer was null. Since we have a separate holder class anyway take out the std::unique_ptr<> and call the deleter directly. Fixes #117273 Test Plan: [ghstack-poisoned]
aorenste
added a commit
that referenced
this issue
Jan 18, 2024
Summary: When using a custom deleter InefficientStdFunctionContext was using a std::unique_ptr<> to store the pointer and call the deleter - but this failed to call the deleter if the pointer was null. Since we have a separate holder class anyway take out the std::unique_ptr<> and call the deleter directly. Fixes #117273 Test Plan: ghstack-source-id: b5f8bf8 Pull Request resolved: #117418
aorenste
added a commit
that referenced
this issue
Jan 18, 2024
…o-data tensor." Summary: When using a custom deleter InefficientStdFunctionContext was using a std::unique_ptr<> to store the pointer and call the deleter - but this failed to call the deleter if the pointer was null. Since we have a separate holder class anyway take out the std::unique_ptr<> and call the deleter directly. Fixes #117273 Test Plan: [ghstack-poisoned]
aorenste
added a commit
that referenced
this issue
Jan 18, 2024
Summary: When using a custom deleter InefficientStdFunctionContext was using a std::unique_ptr<> to store the pointer and call the deleter - but this failed to call the deleter if the pointer was null. Since we have a separate holder class anyway take out the std::unique_ptr<> and call the deleter directly. Fixes #117273 Test Plan: [ghstack-poisoned]
Isn't it the same as #117058 ? |
Yes, that looks like exactly the same issue. |
Closing as a duplicate of #117058 |
aorenste
added a commit
that referenced
this issue
Jan 22, 2024
…o-data tensor." Summary: When using a custom deleter InefficientStdFunctionContext was using a std::unique_ptr<> to store the pointer and call the deleter - but this failed to call the deleter if the pointer was null. Since we have a separate holder class anyway take out the std::unique_ptr<> and call the deleter directly. Fixes #117273 Test Plan: [ghstack-poisoned]
aorenste
added a commit
that referenced
this issue
Jan 22, 2024
Summary: When using a custom deleter InefficientStdFunctionContext was using a std::unique_ptr<> to store the pointer and call the deleter - but this failed to call the deleter if the pointer was null. Since we have a separate holder class anyway take out the std::unique_ptr<> and call the deleter directly. Fixes #117273 Test Plan: ghstack-source-id: 1f3e9d7 Pull Request resolved: #117418
aorenste
added a commit
that referenced
this issue
Jan 22, 2024
Summary: When using a custom deleter InefficientStdFunctionContext was using a std::unique_ptr<> to store the pointer and call the deleter - but this failed to call the deleter if the pointer was null. Since we have a separate holder class anyway take out the std::unique_ptr<> and call the deleter directly. Fixes #117273 Test Plan: [ghstack-poisoned]
pytorchmergebot
pushed a commit
that referenced
this issue
Jan 22, 2024
Summary: When using a custom deleter InefficientStdFunctionContext was using a std::unique_ptr<> to store the pointer and call the deleter - but this failed to call the deleter if the pointer was null. Since we have a separate holder class anyway take out the std::unique_ptr<> and call the deleter directly. Fixes #117273 Test Plan: Pull Request resolved: #117418 Approved by: https://github.com/wjakob, https://github.com/yanboliang
This was referenced Feb 27, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
module: dlpack
triage review
triaged
This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Uh oh!
There was an error while loading. Please reload this page.
🐛 Describe the bug
PyTorch supports the DLPack interface to enable zero-copy data exchange with various other array programming frameworks.
I've been investigating memory leaks that occur when exchanging tensors with PyTorch and pinned them down to an issue related to situations where
torch.utils.dlpack.from_dlpack
is called with an empty tensor. In this case, PyTorch fails to clean up the associated DLPack data structures, and this leads to small memory leaks. The more fundamental issue is that DLPack tensors are often backed by associated Python data structures, and this can cause numerous reference leaks there as well.Here is a self-contained demonstrator which uses
ctypes
to manually create a DLPack tensor. By default, it creates a tensor containing the entries 0..5 and prints them to the screen. The script below produces the following output:We can observe how both the data was correctly handed over to PyTorch, and how it calls the cleanup routine when the tensor is no longer needed.
Now, change the last line to
make_tensor(0)
. In this case, the output isThe deleter is no longer called, presumably because there is nothing to cleanup. But this is not correct. The DLPack specification mandates that a framework which consumes a tensor must eventually call its
delete
member. And we can see that PyTorch consumed this tensor by adding aprint(capsule)
statement at the end of the functionmake_tensor()
.This produces the output
Versions
PyTorch version: 2.3.0.dev20240108
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A
OS: macOS 13.6.2 (arm64)
GCC version: Could not collect
Clang version: 15.0.0 (clang-1500.1.0.2.5)
CMake version: version 3.28.1
Libc version: N/A
Python version: 3.12.1 (main, Dec 8 2023, 18:57:37) [Clang 14.0.3 (clang-1403.0.22.14.1)] (64-bit runtime)
Python platform: macOS-13.6.2-arm64-arm-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True
CPU:
Apple M1 Max
Versions of relevant libraries:
[pip3] numpy==1.26.2
[pip3] torch==2.3.0.dev20240108
[conda] Could not collect
The text was updated successfully, but these errors were encountered: