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

array protocol support on memory sharing #18561

@szha

Description

@szha

Description

For arrays that share memory, np.may_share_memory doesn't work for numpy types yet.

import mxnet as mx
import numpy as _np
np_array = _np.ones((3,5))
mx_nd_array = mx.nd.from_numpy(np_array, zero_copy=True)
mx_np_array = mx_nd_array.as_np_ndarray()
mx_np_array[0, 0] = 2

In [42]: mx_np_array
Out[42]:
array([[2., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]], dtype=float64)

In [43]: mx_nd_array
Out[43]:

[[2. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1.]]
<NDArray 3x5 @cpu(0)>

In [44]: np_array
Out[44]:
array([[2., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]])

Then, if we call np.may_share_memory(np_array, mx_np_array) or np.may_share_memory(np_array, mx_nd_array), it returns

TypeError: no implementation found for 'numpy.may_share_memory' on types that implement __array_function__: [<class 'numpy.ndarray'>, <class 'mxnet.numpy.ndarray'>]

It works for _np.may_share_memory(mx_nd_array, mx_np_array) though

True

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions