Skip to content

Array assignment with broadcasting is not working #1330

Closed

Description

@antonwolfy

The code below results in an exception, but is expected to work:

import numpy, dpctl, dpctl.tensor as dpt

x = numpy.ones((3, 1))
y = numpy.empty((2, 3, 4))

# works in numpy:
y[...] = x

a = dpt.ones((3, 1))
b = dpt.empty((2, 3, 4))

# dpctl raises the exception:
b[...] = a
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[5], line 1
----> 1 b[...] = a

File /localdisk/work/antonvol/code/dpctl/dpctl/tensor/_usmarray.pyx:1172, in dpctl.tensor._usmarray.usm_ndarray.__setitem__()
   1170     if Xv.size == 0:
   1171         return
-> 1172     _copy_from_usm_ndarray_to_usm_ndarray(Xv, rhs)
   1173 else:
   1174     if hasattr(rhs, "__sycl_usm_array_interface__"):

File /localdisk/work/antonvol/code/dpctl/dpctl/tensor/_copy_utils.py:283, in _copy_from_usm_ndarray_to_usm_ndarray(dst, src)
    281 if src.ndim < len(common_shape):
    282     new_src_strides = (0,) * (len(common_shape) - src.ndim) + src.strides
--> 283     src_same_shape = dpt.usm_ndarray(
    284         common_shape, dtype=src.dtype, buffer=src, strides=new_src_strides
    285     )
    286 else:
    287     src_same_shape = src

File /localdisk/work/antonvol/code/dpctl/dpctl/tensor/_usmarray.pyx:328, in dpctl.tensor._usmarray.usm_ndarray.__cinit__()
    326    (_offset + ary_max_displacement + 1) * itemsize > _buffer.nbytes):
    327     self._cleanup()
--> 328     raise ValueError(("buffer='{}' can not accomodate "
    329                       "the requested array.").format(buffer))
    330 is_fp64 = (typenum == UAR_DOUBLE or typenum == UAR_CDOUBLE)

ValueError: buffer='[[1.]
 [1.]
 [1.]]' can not accomodate the requested array.

Activity

  1. antonwolfy commented on Aug 9, 2023

    @antonwolfy
    CollaboratorAuthor

    One more similar issue which might have the same root cause:

    import numpy, dpctl, dpctl.tensor as dpt
    
    a = numpy.ones((1, 2, 1, 4))
    b = numpy.empty((2, 3, 4))
    
    # works in numpy:
    b[...] = a
    
    a = dpt.ones((1, 2, 1, 4))
    b = dpt.empty((2, 3, 4))
    
    # dpctl raises the exception:
    b[...] = a
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    Cell In[4], line 1
    ----> 1 b[...] = a
    
    File dpctl/tensor/_usmarray.pyx:1147, in dpctl.tensor._usmarray.usm_ndarray.__setitem__()
    
    File ~/miniconda3/envs/dpnp_dev/lib/python3.9/site-packages/dpctl/tensor/_copy_utils.py:287, in _copy_from_usm_ndarray_to_usm_ndarray(dst, src)
        285 else:
        286     src_same_shape = src
    --> 287     src_same_shape.shape = common_shape
        289 _copy_same_shape(dst, src_same_shape)
    
    File dpctl/tensor/_usmarray.pyx:576, in dpctl.tensor._usmarray.usm_ndarray.shape.__set__()
    
    TypeError: Can not reshape array of size 8 into (2, 3, 4)
  2. antonwolfy commented on Aug 9, 2023

    @antonwolfy
    CollaboratorAuthor

    And one more similar problem:

    a = dpt.zeros((2, 3, 4), dtype=numpy.int64)
    b = numpy.array([1], dtype=numpy.int64)
    c = dpt.asarray(b, device=a.sycl_queue)
    
    a[...] = c
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    Cell In[6], line 1
    ----> 1 a[...] = c
    
    File dpctl/tensor/_usmarray.pyx:1147, in dpctl.tensor._usmarray.usm_ndarray.__setitem__()
    
    File ~/miniconda3/envs/dpnp_dev/lib/python3.9/site-packages/dpctl/tensor/_copy_utils.py:282, in _copy_from_usm_ndarray_to_usm_ndarray(dst, src)
        280 if src.ndim < len(common_shape):
        281     new_src_strides = (0,) * (len(common_shape) - src.ndim) + src.strides
    --> 282     src_same_shape = dpt.usm_ndarray(
        283         common_shape, dtype=src.dtype, buffer=src, strides=new_src_strides
        284     )
        285 else:
        286     src_same_shape = src
    
    File dpctl/tensor/_usmarray.pyx:328, in dpctl.tensor._usmarray.usm_ndarray.__cinit__()
    
    ValueError: buffer='[1]' can not accomodate the requested array.
  3. added a commit that references this issue on Aug 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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