IntelPython / dpctl

Python SYCL bindings and SYCL-based Python Array API library
https://intelpython.github.io/dpctl/
Apache License 2.0
99 stars 29 forks source link

dpctl does not raise exception from `usm_ndarray` constructor for incompatible strides with shape #1766

Closed antonwolfy closed 1 month ago

antonwolfy commented 1 month ago

In case when strides passed in usm_ndarray constructor is incompatible with shape keyword, there is no validation exception raised by dpctl. Please check below example for more details:

import numpy, dpctl, dpctl.tensor as dpt

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

dpt.usm_ndarray((2, 3), numpy.float32, strides=(5, 1)).strides
# Out: (5, 1)

dpt.usm_ndarray((2, 3), numpy.float32, strides=(7, 2)).strides
# Out: (7, 2)

# while numpy raises an exception for both use cases:
numpy.ndarray((2, 3), numpy.float32, strides=(20, 4))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[10], line 1
----> 1 numpy.ndarray((2, 3), numpy.float32, strides=(20, 4))

ValueError: strides is incompatible with shape of requested array and size of buffer

numpy.ndarray((2, 3), numpy.float32, strides=(28, 8))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[15], line 1
----> 1 numpy.ndarray((2, 3), numpy.float32, strides=(28, 8))

ValueError: strides is incompatible with shape of requested array and size of buffer
oleksandr-pavlyk commented 1 month ago

The input dpt.usm_ndarray((2, 3), numpy.float32, strides=(5, 1)) result in allocation of larger memory blob than np.float32.itemsize * 2 * 3 = 24 bytes. dpctl.tensor would allocate dpt.float32.itemsize * ( 5 * (2-1) + 1 * (3-1) ) = 28 bytes, although not all bytes are going to be addressed by the array indices.

The dpt.usm_ndarray is not intended for use of end-user. The user is advised to use Python Array API constructor functions.

This behavior is not going to change. Feel free to close this ticket.

antonwolfy commented 1 month ago

@oleksandr-pavlyk, I see, thank you for the detailed explanation.