Skip to content

std/var functions raise TypeError when due to dtype casting #2306

Open
@antonwolfy

Description

@antonwolfy

The below example behaves differently in numpy and dpnp:

import dpnp, numpy

dpnp.__version__
# Out: '0.17.0dev6+6.g5b3beb35ee4'

numpy.__version__
# Out: '2.2.2'

a = dpnp.array([[1, 2], [3, 4]])

dpnp.var(a, dtype='i2')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[3], line 1
----> 1 dpnp.var(a, dtype='i2')

File /localdisk/work/antonvol/code/dpnp/dpnp/dpnp_iface_statistics.py:1758, in var(a, axis, dtype, out, ddof, keepdims, where, mean, correction)
   1755     result = dpnp.get_result_array(usm_res, out)
   1757 if out is None and dtype is not None:
-> 1758     result = result.astype(dtype, casting="same_kind")
   1759 return result

File /localdisk/work/antonvol/code/dpnp/dpnp/dpnp_array.py:853, in dpnp_array.astype(self, dtype, order, casting, subok, copy, device)
    848 if subok is not True:
    849     raise NotImplementedError(
    850         f"subok={subok} is currently not supported"
    851     )
--> 853 return dpnp.astype(
    854     self, dtype, order=order, casting=casting, copy=copy, device=device
    855 )

File /localdisk/work/antonvol/code/dpnp/dpnp/dpnp_iface.py:266, in astype(x1, dtype, order, casting, copy, device)
    263     order = "K"
    265 x1_obj = dpnp.get_usm_ndarray(x1)
--> 266 array_obj = dpt.astype(
    267     x1_obj, dtype, order=order, casting=casting, copy=copy, device=device
    268 )
    270 if array_obj is x1_obj and isinstance(x1, dpnp_array):
    271     # return x1 if dpctl returns a zero copy of x1_obj
    272     return x1

File /localdisk/work/antonvol/soft/miniforge3/envs/dpnp_dev_numpy20/lib/python3.12/site-packages/dpctl/tensor/_copy_utils.py:667, in astype(usm_ary, newdtype, order, casting, copy, device)
    664     target_dtype = _get_dtype(newdtype, usm_ary.sycl_queue)
    666 if not dpt.can_cast(ary_dtype, target_dtype, casting=casting):
--> 667     raise TypeError(
    668         f"Can not cast from {ary_dtype} to {newdtype} "
    669         f"according to rule {casting}."
    670     )
    671 c_contig = usm_ary.flags.c_contiguous
    672 f_contig = usm_ary.flags.f_contiguous

# but works with numpy:
na = a.asnumpy()

numpy.var(na, dtype='i2')
# Out: np.int16(1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions