Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

[MXNET-1406] [BUG] Fix DLManagedTensor deleter #15016

Merged
merged 3 commits into from
May 22, 2019

Conversation

junrushao
Copy link
Member

Description

This PR fixes a bug that causes recent crash in DGL. Please refer to dmlc/dlpack#39 for details.

CC: @zheng-da

Checklist

Essentials

Please feel free to remove inapplicable items for your PR.

  • The PR title starts with [MXNET-$JIRA_ID], where $JIRA_ID refers to the relevant JIRA issue created (except PRs with tiny changes)
  • Changes are complete (i.e. I finished coding on this PR)
  • All changes have test coverage:
  • Unit tests are added for small changes to verify correctness (e.g. adding a new operator)
  • Nightly tests are added for complicated/long-running ones (e.g. changing distributed kvstore)
  • Build tests will be added for build configuration changes (e.g. adding a new build option with NCCL)
  • Code is well-documented:
  • For user-facing API changes, API doc string has been updated.
  • For new C++ functions in header files, their functionalities and arguments are documented.
  • For new examples, README.md is added to explain the what the example does, the source of the dataset, expected performance on test set and reference to the original paper if applicable
  • Check the API doc at http:https://mxnet-ci-doc.s3-accelerate.dualstack.amazonaws.com/PR-$PR_ID/$BUILD_ID/index.html
  • To the my best knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change

Changes

  • Allow MXNDArrayFromDLPack to accept an additional flag indicating whether the handle will be freed before calling deleter.

Comments

None

@junrushao
Copy link
Member Author

Waiting for #15015

@@ -818,10 +818,12 @@ MXNET_DLL int MXNDArrayToDLPack(NDArrayHandle handle,
* The memory is retained until the NDArray went out of scope.
*
* \param dlpack the pointer of the input DLManagedTensor
* \param transient_handle whether the handle will be destructed before calling the deleter
Copy link
Contributor

Choose a reason for hiding this comment

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

before calling?

Copy link
Member Author

Choose a reason for hiding this comment

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

If it is false, the DLManagedTensor handle won't be freed after the call, and deleter will free it.
If it is true, the DLManagedTensor handle will be freed by the caller.

@@ -4156,7 +4156,7 @@ def from_dlpack(dlpack):
assert ctypes.pythonapi.PyCapsule_IsValid(dlpack, _c_str_dltensor), ValueError(
'Invalid DLPack Tensor. DLTensor capsules can be consumed only once.')
dlpack_handle = ctypes.c_void_p(ctypes.pythonapi.PyCapsule_GetPointer(dlpack, _c_str_dltensor))
check_call(_LIB.MXNDArrayFromDLPack(dlpack_handle, ctypes.byref(handle)))
check_call(_LIB.MXNDArrayFromDLPack(dlpack_handle, False, ctypes.byref(handle)))
Copy link
Contributor

Choose a reason for hiding this comment

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

if transient_handle is false, it means the dlpack_handle won't be free'd any time soon? However, how do we control when the dlpack_handle will be free'd?

Copy link
Member Author

Choose a reason for hiding this comment

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

It means deleter will free it

handle = NDArrayHandle()
check_call(_LIB.MXNDArrayFromDLPack(address, ctypes.byref(handle)))
check_call(_LIB.MXNDArrayFromDLPack(ctypes.byref(c_obj), True, ctypes.byref(handle)))
Copy link
Contributor

Choose a reason for hiding this comment

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

why the handle here is transient handle?

Copy link
Member Author

Choose a reason for hiding this comment

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

bc the DLManagedTensor here is freed immediately after this call

@junrushao
Copy link
Member Author

junrushao commented May 21, 2019

The implication here:

  1. transient_handle = True implies that the provided deleter does not delete the handle, so we have to manually delete it in our MXNet's deleter.
  2. transient_handle = False implies that the provided deleter does delete the handle, so we don't have to manually delete it in our MXNet's deleter.

@karan6181
Copy link
Contributor

@mxnet-label-bot add [C API, NDArray]

@zheng-da zheng-da merged commit 90b66b8 into apache:master May 22, 2019
@wkcn
Copy link
Member

wkcn commented May 22, 2019

In the old implementation, the deleter will release the object NDArrayDLManager
https://github.com/apache/incubator-mxnet/blob/master/src/ndarray/ndarray.cc#L353

NDArrayDLManager consists of DLManagerTensor
https://github.com/apache/incubator-mxnet/blob/master/src/ndarray/ndarray.cc#L343

It is not necessary to release DLManagerTensor again.

Edit:
I see. Thank you for the fix!

@junrushao
Copy link
Member Author

Thanks @wkcn for the in-depth discussion!

access2rohit pushed a commit to access2rohit/incubator-mxnet that referenced this pull request May 22, 2019
haohuanw pushed a commit to haohuanw/incubator-mxnet that referenced this pull request Jun 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants