wkcn commented on a change in pull request #12047: [MXNET-779]Add DLPack 
Transformation API
URL: https://github.com/apache/incubator-mxnet/pull/12047#discussion_r209416184
 
 

 ##########
 File path: python/mxnet/_ctypes/ndarray.py
 ##########
 @@ -31,21 +31,24 @@
 
 class NDArrayBase(object):
     """Base data structure for ndarray"""
-    __slots__ = ["handle", "writable"]
+    __slots__ = ["handle", "writable", "dlpack"]
     # pylint: disable= no-member
 
-    def __init__(self, handle, writable=True):
+    def __init__(self, handle, writable=True, dlpack=None):
 
 Review comment:
   Thanks.
   
   In the other case,
   ```python
   from torch.utils import dlpack
   a = torch.array([1,2,3])
   pack = dlpack.to_dlpack(a)
   b = mx.nd.from_dlpack(pack)
   del a, pack
   ```
   When `dlpack.to_dlpack` is called, PyTorch will allocate `ATenDLMTensor` 
which increases the refcount of Torch Tensor 
[code](https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/DLConvertor.cpp#L129).
   After the variables `a` and `pack` are released, `ATenDLMTensor` still 
exists.
   I think the deleter should be called by the new NDArray `b` when the NDArray 
`b` releases. Refer to [PyTorch 
FromDLPack](https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/DLConvertor.cpp#L170).
 However, NDArray doesn't have explicit deleter parameter.
   
   In my PR, `from_dlpack` will copy the dlpack object.
   When the old dlpack `pack` releases, it doesn't call the deleter.
   The new dlpack `b.dlpack` will be a member of the new NDArray `b` as 
`NDArray(handle=handle, dlpack=dlpack_copy)`.
   When the new NDArray `b` releases, the new dlpack `b.dlpack` will be 
released, then call the deleter by the new dlpack `b.dlpack`. And the deleter 
will release `NDArrayDLManager` or `ATenDLMTensor`. The refcount of the old 
NDArray `a` will decrease 1.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to