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_r209225184
 
 

 ##########
 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:
   Sorry. I don't know which object manages the actual memory after from_dlpack 
is called.
   In the following code, 
   ```
   a = mx.nd.array([1,2,3]) # Denote TBlob x to store the data
   pack = a.to_dlpack_for_write()
   b = mx.nd.from_dlpack(pack)
   del a, pack
   ```
   The original NDArray `a` and dlpack are released. However, the Manager 
`NDArrayDLManager` should be kept.
   The new NDArray `b` get the data pointer from DLPack.
   Which object will call the deleter?
   
   In PyTorch, the Tensor class has the deleter structrual parameter. 
[Code1](https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/DLConvertor.cpp#L170)
 
[Code2](https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/templates/Type.h#L88)
   When the refcount of Tensor is zero, the Tensor Object will call the deleter 
to release `ATenDLMTensor`.
   MXNet doesn't have the explicit deleter structual parameter. 
   So I set `dlpack` as the member of NDArray (Python) class.
   When the refcount of NDArray is zero, the refcount of dlpack in NDArray is 
zero too.
   The dlpack will be released then call the deleter to delete 
`NDArrayDLManager`.

----------------------------------------------------------------
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