dpctl icon indicating copy to clipboard operation
dpctl copied to clipboard

`dpt.from_dlpack` doesn't preserve flag values of an input array

Open antonwolfy opened this issue 1 year ago • 1 comments

The below example:

import dpnp, dpctl, dpctl.tensor as dpt

dpctl.__version__
# Out: '0.18.0dev0+158.g7450558d25'

a = dpnp.ones(10)
a.flags
# Out:
#   C_CONTIGUOUS : True
#   F_CONTIGUOUS : True
#   WRITABLE : True

a.flags["W"] = False
a.flags
# Out:
#   C_CONTIGUOUS : True
#   F_CONTIGUOUS : True
#   WRITABLE : False

b = dpt.from_dlpack(a)
b.flags
# Out:
#   C_CONTIGUOUS : True
#   F_CONTIGUOUS : True
#   WRITABLE : True

demonstrates that writable flag set to False for dpnp array a was not preserved after dpt.from_dlpack(a) call.

antonwolfy avatar Jul 24 '24 13:07 antonwolfy

@antonwolfy This is because dpnp,ndarray does not yet support max_version keyword in its __dlpack__ method:

(dev_dpctl) opavlyk@sprpvcbenchlin01:/localdisk/work/opavlyk/repos/dpctl$ ipython
Python 3.12.4 | packaged by conda-forge | (main, Jun 17 2024, 10:23:07) [GCC 12.3.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.26.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import dpnp, dpctl, dpctl.tensor as dpt

In [2]: x = dpt.linspace(100, 0, num=101, dtype="f8")

In [3]: x.flags["W"] = False

In [4]: y = dpnp.linspace(100, 0, num=101, dtype="f8")

In [5]: y.flags["W"] = False

In [6]: x.__dlpack__(max_version=(1, 0))
Out[6]: <capsule object "dltensor_versioned" at 0x7f71dcefa8e0>

In [7]: y.__dlpack__(max_version=(1, 0))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[7], line 1
----> 1 y.__dlpack__(max_version=(1, 0))

TypeError: dpnp_array.__dlpack__() got an unexpected keyword argument 'max_version'

Once added, from_dlpack would preserve the flag as it does for dpctl.tensor.

oleksandr-pavlyk avatar Jul 24 '24 13:07 oleksandr-pavlyk

@antonwolfy Since the fix must be done on the dpnp side, perhaps this ticket should be closed and a new one be opened in the dpnp project.

oleksandr-pavlyk avatar Dec 15 '24 23:12 oleksandr-pavlyk

@oleksandr-pavlyk, the issue has been already resolved in dpnp:

import dpnp, dpctl, dpctl.tensor as dpt

dpnp.__version__
# Out: '0.17.0dev3+25.g4f81237c708'

a = dpnp.ones(10)
a.flags
# Out:
#  C_CONTIGUOUS : True
#  F_CONTIGUOUS : True
#  WRITABLE : True

a.flags["W"] = False
a.flags
# Out:
#  C_CONTIGUOUS : True
#  F_CONTIGUOUS : True
#  WRITABLE : False

b = dpt.from_dlpack(a)
b.flags
# Out:
#  C_CONTIGUOUS : True
#  F_CONTIGUOUS : True
#  WRITABLE : False

So I'm closing that ticket, thank you!

antonwolfy avatar Dec 16 '24 11:12 antonwolfy