diff --git a/dpnp/dpnp_container.py b/dpnp/dpnp_container.py index bd77c0998c8d..08522ceaafa6 100644 --- a/dpnp/dpnp_container.py +++ b/dpnp/dpnp_container.py @@ -86,8 +86,8 @@ def arange( def asarray( x1, dtype=None, - copy=False, - order="C", + copy=None, + order=None, device=None, usm_type=None, sycl_queue=None, @@ -96,7 +96,7 @@ def asarray( dpu.validate_usm_type(usm_type, allow_none=True) if order is None: - order = "C" + order = "K" """Converts incoming 'x1' object to 'dpnp_array'.""" if isinstance(x1, (list, tuple, range)): @@ -127,6 +127,11 @@ def asarray( usm_type=usm_type, sycl_queue=sycl_queue_normalized, ) + + # return x1 if dpctl returns a zero copy of x1_obj + if array_obj is x1_obj: + return x1 + return dpnp_array(array_obj.shape, buffer=array_obj, order=order) diff --git a/dpnp/dpnp_iface.py b/dpnp/dpnp_iface.py index 760466916220..066b28e8e2cd 100644 --- a/dpnp/dpnp_iface.py +++ b/dpnp/dpnp_iface.py @@ -310,7 +310,7 @@ def get_dpnp_descriptor( if ext_obj.strides != shape_offsets or ext_obj_offset != 0: orig_desc = dpnp_descriptor(ext_obj) - ext_obj = array(ext_obj) + ext_obj = array(ext_obj, order="C") # while dpnp functions are based on DPNP_QUEUE # we need to create a copy on device associated with DPNP_QUEUE diff --git a/dpnp/dpnp_iface_arraycreation.py b/dpnp/dpnp_iface_arraycreation.py index 3be59df4eb3a..549920066ac0 100644 --- a/dpnp/dpnp_iface_arraycreation.py +++ b/dpnp/dpnp_iface_arraycreation.py @@ -56,6 +56,7 @@ "asanyarray", "asarray", "ascontiguousarray", + "asfortranarray", "copy", "diag", "diagflat", @@ -150,10 +151,11 @@ def arange( def array( - x1, + x, dtype=None, + *, copy=True, - order="C", + order="K", subok=False, ndmin=0, like=None, @@ -162,15 +164,16 @@ def array( sycl_queue=None, ): """ - Creates an array. + Create an array. For full documentation refer to :obj:`numpy.array`. Limitations ----------- - Parameter ``subok`` is supported only with default value ``False``. - Parameter ``ndmin`` is supported only with default value ``0``. - Parameter ``like`` is supported only with default value ``None``. + Parameter `subok` is supported only with default value ``False``. + Parameter `ndmin` is supported only with default value ``0``. + Parameter `like` is supported only with default value ``None``. + Otherwise, the function raises `ValueError` exception. See Also -------- @@ -204,35 +207,47 @@ def array( """ if subok is not False: - pass + raise ValueError( + "Keyword argument `subok` is supported only with " + f"default value ``False``, but got {subok}" + ) elif ndmin != 0: - pass + raise ValueError( + "Keyword argument `ndmin` is supported only with " + f"default value ``0``, but got {ndmin}" + ) elif like is not None: - pass - else: - return dpnp_container.asarray( - x1, - dtype=dtype, - copy=copy, - order=order, - device=device, - usm_type=usm_type, - sycl_queue=sycl_queue, + raise ValueError( + "Keyword argument `like` is supported only with " + f"default value ``None``, but got {like}" ) - return call_origin( - numpy.array, - x1, + # `False`` in numpy means exactly the same like `None` in python array API: + # that is to reuse existing memory buffer if possible or to copy otherwise. + if copy is False: + copy = None + + return dpnp_container.asarray( + x, dtype=dtype, copy=copy, order=order, - subok=subok, - ndmin=ndmin, - like=like, + device=device, + usm_type=usm_type, + sycl_queue=sycl_queue, ) -def asanyarray(a, dtype=None, order="C"): +def asanyarray( + x, + dtype=None, + order=None, + *, + like=None, + device=None, + usm_type=None, + sycl_queue=None, +): """ Convert the input to an ndarray, but pass ndarray subclasses through. @@ -240,7 +255,8 @@ def asanyarray(a, dtype=None, order="C"): Limitations ----------- - Parameter ``order`` is supported only with default value ``"C"``. + Parameter `like` is supported only with default value ``None``. + Otherwise, the function raises `ValueError` exception. See Also -------- @@ -258,29 +274,31 @@ def asanyarray(a, dtype=None, order="C"): Examples -------- >>> import dpnp as np - >>> x = np.asanyarray([1, 2, 3]) - >>> [i for i in x] - [1, 2, 3] + >>> np.asanyarray([1, 2, 3]) + array([1, 2, 3]) """ - if not use_origin_backend(a): - # if it is already dpnp.ndarray then same object should be returned - if isinstance(a, dpnp.ndarray): - return a - - if order != "C": - pass - else: - return array(a, dtype=dtype, order=order) + if like is not None: + raise ValueError( + "Keyword argument `like` is supported only with " + f"default value ``None``, but got {like}" + ) - return call_origin(numpy.asanyarray, a, dtype, order) + return asarray( + x, + dtype=dtype, + order=order, + device=device, + usm_type=usm_type, + sycl_queue=sycl_queue, + ) def asarray( - x1, + x, dtype=None, - order="C", + order=None, like=None, device=None, usm_type=None, @@ -293,7 +311,8 @@ def asarray( Limitations ----------- - Parameter ``like`` is supported only with default value ``None``. + Parameter `like` is supported only with default value ``None``. + Otherwise, the function raises `ValueError` exception. See Also -------- @@ -311,64 +330,168 @@ def asarray( Examples -------- >>> import dpnp as np - >>> x = np.asarray([1, 2, 3]) - >>> print(x) - [1 2 3] + >>> np.asarray([1, 2, 3]) + array([1, 2, 3]) """ if like is not None: - pass - else: - return dpnp_container.asarray( - x1, - dtype=dtype, - copy=True, # Converting Python sequence to usm_ndarray requires a copy - order=order, - device=device, - usm_type=usm_type, - sycl_queue=sycl_queue, + raise ValueError( + "Keyword argument `like` is supported only with " + f"default value ``None``, but got {like}" ) - return call_origin(numpy.asarray, x1, dtype=dtype, order=order, like=like) + return dpnp_container.asarray( + x, + dtype=dtype, + order=order, + device=device, + usm_type=usm_type, + sycl_queue=sycl_queue, + ) -def ascontiguousarray(a, dtype=None): +def ascontiguousarray( + x, dtype=None, *, like=None, device=None, usm_type=None, sycl_queue=None +): """ Return a contiguous array (ndim >= 1) in memory (C order). For full documentation refer to :obj:`numpy.ascontiguousarray`. + Limitations + ----------- + Parameter `like` is supported only with default value ``None``. + Otherwise, the function raises `ValueError` exception. + See Also -------- :obj:`dpnp.asfortranarray` : Convert input to an ndarray with column-major memory order. :obj:`dpnp.require` : Return an ndarray that satisfies requirements. + :obj:`dpnp.ndarray.flags` : Information about the memory layout of the array. Examples -------- >>> import dpnp as np - >>> x = np.arange(6).reshape((2, 3)) - >>> out = np.ascontiguousarray(x) - >>> out.shape - (2, 3) - >>> [i for i in out] - [0, 1, 2, 3, 4, 5] + >>> x = np.ones((2, 3), order='F') + >>> x.flags['F_CONTIGUOUS'] + True + + Calling ``ascontiguousarray`` makes a C-contiguous copy: + + >>> y = np.ascontiguousarray(x) + >>> y.flags['F_CONTIGUOUS'] + True + >>> x is y + False + + Now, starting with a C-contiguous array: + + >>> x = np.ones((2, 3), order='C') + >>> x.flags['C_CONTIGUOUS'] + True + + Then, calling ``ascontiguousarray`` returns the same object: + + >>> y = np.ascontiguousarray(x) + >>> x is y + True """ - if not use_origin_backend(a): - # we support only c-contiguous arrays for now - # if type is the same then same object should be returned - if isinstance(a, dpnp.ndarray) and a.dtype == dtype: - return a + if like is not None: + raise ValueError( + "Keyword argument `like` is supported only with " + f"default value ``None``, but got {like}" + ) - return array(a, dtype=dtype) + # at least 1-d array has to be returned + if x.ndim == 0: + x = [x] - return call_origin(numpy.ascontiguousarray, a, dtype) + return asarray( + x, + dtype=dtype, + order="C", + device=device, + usm_type=usm_type, + sycl_queue=sycl_queue, + ) + + +def asfortranarray( + x, dtype=None, *, like=None, device=None, usm_type=None, sycl_queue=None +): + """ + Return an array (ndim >= 1) laid out in Fortran order in memory. + + For full documentation refer to :obj:`numpy.asfortranarray`. + + Limitations + ----------- + Parameter `like` is supported only with default value ``None``. + Otherwise, the function raises `ValueError` exception. + + See Also + -------- + :obj:`dpnp.ascontiguousarray` : Convert input to a contiguous (C order) array. + :obj:`dpnp.asanyarray` : Convert input to an ndarray with either row or column-major memory order. + :obj:`dpnp.require` : Return an ndarray that satisfies requirements. + :obj:`dpnp.ndarray.flags` : Information about the memory layout of the array. + + Examples + -------- + >>> import dpnp as np + + Starting with a C-contiguous array: + + >>> x = np.ones((2, 3), order='C') + >>> x.flags['C_CONTIGUOUS'] + True + + Calling ``asfortranarray`` makes a Fortran-contiguous copy: + + >>> y = np.asfortranarray(x) + >>> y.flags['F_CONTIGUOUS'] + True + >>> x is y + False + + Now, starting with a Fortran-contiguous array: + + >>> x = np.ones((2, 3), order='F') + >>> x.flags['F_CONTIGUOUS'] + True + + Then, calling ``asfortranarray`` returns the same object: + + >>> y = np.asfortranarray(x) + >>> x is y + True + + """ + + if like is not None: + raise ValueError( + "Keyword argument `like` is supported only with " + f"default value ``None``, but got {like}" + ) + + # at least 1-d array has to be returned + if x.ndim == 0: + x = [x] + + return asarray( + x, + dtype=dtype, + order="F", + device=device, + usm_type=usm_type, + sycl_queue=sycl_queue, + ) -# numpy.copy(a, order='K', subok=False) def copy(x1, order="K", subok=False): """ Return an array copy of the given object. diff --git a/tests/skipped_tests.tbl b/tests/skipped_tests.tbl index 2424ade4cee4..de05f8267a1b 100644 --- a/tests/skipped_tests.tbl +++ b/tests/skipped_tests.tbl @@ -264,61 +264,6 @@ tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_full_like_s tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_ones_like_subok tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_zeros_like_subok tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_zeros_strides -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_0_{copy=True, ndmin=0, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_10_{copy=False, ndmin=1, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_11_{copy=False, ndmin=1, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_12_{copy=False, ndmin=2, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_13_{copy=False, ndmin=2, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_14_{copy=False, ndmin=3, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_15_{copy=False, ndmin=3, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_1_{copy=True, ndmin=0, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_2_{copy=True, ndmin=1, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_3_{copy=True, ndmin=1, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_4_{copy=True, ndmin=2, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_5_{copy=True, ndmin=2, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_6_{copy=True, ndmin=3, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_7_{copy=True, ndmin=3, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_8_{copy=False, ndmin=0, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_9_{copy=False, ndmin=0, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_10_{copy=False, ndmin=1, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_11_{copy=False, ndmin=1, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_12_{copy=False, ndmin=2, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_13_{copy=False, ndmin=2, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_14_{copy=False, ndmin=3, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_15_{copy=False, ndmin=3, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_2_{copy=True, ndmin=1, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_3_{copy=True, ndmin=1, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_4_{copy=True, ndmin=2, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_5_{copy=True, ndmin=2, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_6_{copy=True, ndmin=3, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_7_{copy=True, ndmin=3, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_8_{copy=False, ndmin=0, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_9_{copy=False, ndmin=0, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_copy_is_copied -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_copy_list_of_cupy_with_dtype -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_copy_list_of_cupy_with_dtype_char -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_copy_list_of_numpy_with_dtype -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_copy_list_of_numpy_with_dtype_char -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_f_contiguous_input -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_f_contiguous_output -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_from_list_of_cupy -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_from_list_of_cupy_scalar -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_from_list_of_cupy_view -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_from_list_of_numpy -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_from_list_of_numpy_scalar -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_from_list_of_numpy_view -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_from_nested_list_of_cupy -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_from_nested_list_of_numpy -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_from_numpy_scalar -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_no_copy -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_no_copy_ndmin -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_asarray_cuda_array_zero_dim -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_asarray_cuda_array_zero_dim_dtype -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_asarray_is_not_copied -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_asarray_with_order_copy_behavior -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_ascontiguousarray_on_noncontiguous_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_asfortranarray_cuda_array_zero_dim -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_asfortranarray_cuda_array_zero_dim_dtype tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_construction_from_list tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_construction_from_tuple diff --git a/tests/skipped_tests_gpu.tbl b/tests/skipped_tests_gpu.tbl index 85f25b90028f..8723eff71e6f 100644 --- a/tests/skipped_tests_gpu.tbl +++ b/tests/skipped_tests_gpu.tbl @@ -169,12 +169,7 @@ tests/third_party/cupy/core_tests/test_ndarray_math.py::TestRoundHalfway_param_0 tests/third_party/cupy/core_tests/test_ndarray_math.py::TestRoundHalfway_param_1_{decimals=-2}::test_round_halfway_float tests/third_party/cupy/core_tests/test_ndarray_math.py::TestRoundHalfway_param_2_{decimals=-1}::test_round_halfway_float tests/third_party/cupy/core_tests/test_ndarray_math.py::TestRoundHalfway_param_3_{decimals=0}::test_round_halfway_float -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_asanyarray_with_order -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_asarray_from_numpy -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_asarray_preserves_numpy_array_order -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_asarray_with_order -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_ascontiguousarray_on_contiguous_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_copy_order + tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_construction tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_construction_from_list tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_construction_from_tuple @@ -434,61 +429,6 @@ tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_full_like_s tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_ones_like_subok tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_zeros_like_subok tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_zeros_strides -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_0_{copy=True, ndmin=0, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_10_{copy=False, ndmin=1, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_11_{copy=False, ndmin=1, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_12_{copy=False, ndmin=2, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_13_{copy=False, ndmin=2, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_14_{copy=False, ndmin=3, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_15_{copy=False, ndmin=3, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_1_{copy=True, ndmin=0, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_2_{copy=True, ndmin=1, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_3_{copy=True, ndmin=1, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_4_{copy=True, ndmin=2, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_5_{copy=True, ndmin=2, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_6_{copy=True, ndmin=3, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_7_{copy=True, ndmin=3, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_8_{copy=False, ndmin=0, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayCopy_param_9_{copy=False, ndmin=0, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_10_{copy=False, ndmin=1, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_11_{copy=False, ndmin=1, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_12_{copy=False, ndmin=2, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_13_{copy=False, ndmin=2, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_14_{copy=False, ndmin=3, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_15_{copy=False, ndmin=3, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_2_{copy=True, ndmin=1, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_3_{copy=True, ndmin=1, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_4_{copy=True, ndmin=2, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_5_{copy=True, ndmin=2, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_6_{copy=True, ndmin=3, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_7_{copy=True, ndmin=3, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_8_{copy=False, ndmin=0, xp=numpy}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestArrayPreservationOfShape_param_9_{copy=False, ndmin=0, xp=dpnp}::test_cupy_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_copy_is_copied -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_copy_list_of_cupy_with_dtype -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_copy_list_of_cupy_with_dtype_char -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_copy_list_of_numpy_with_dtype -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_copy_list_of_numpy_with_dtype_char -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_f_contiguous_input -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_f_contiguous_output -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_from_list_of_cupy -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_from_list_of_cupy_scalar -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_from_list_of_cupy_view -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_from_list_of_numpy -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_from_list_of_numpy_scalar -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_from_list_of_numpy_view -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_from_nested_list_of_cupy -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_from_nested_list_of_numpy -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_from_numpy_scalar -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_no_copy -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_array_no_copy_ndmin -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_asarray_cuda_array_zero_dim -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_asarray_cuda_array_zero_dim_dtype -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_asarray_is_not_copied -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_asarray_with_order_copy_behavior -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_ascontiguousarray_on_noncontiguous_array -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_asfortranarray_cuda_array_zero_dim -tests/third_party/cupy/creation_tests/test_from_data.py::TestFromData::test_asfortranarray_cuda_array_zero_dim_dtype tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_0_{copy=False, indexing='xy', sparse=False}::test_meshgrid0 tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_0_{copy=False, indexing='xy', sparse=False}::test_meshgrid1 diff --git a/tests/skipped_tests_gpu_no_fp64.tbl b/tests/skipped_tests_gpu_no_fp64.tbl index 2658f178bf8b..46ae7fb4c8f2 100644 --- a/tests/skipped_tests_gpu_no_fp64.tbl +++ b/tests/skipped_tests_gpu_no_fp64.tbl @@ -525,6 +525,8 @@ tests/test_umath.py::TestArctan2::test_invalid_shape[(0,)] tests/test_umath.py::TestArctan2::test_invalid_shape[(15, )] tests/test_umath.py::TestArctan2::test_invalid_shape[(2,2)] +tests/test_umath.py::TestSqrt::test_sqrt_complex[complex64] + tests/third_party/cupy/core_tests/test_ndarray_math.py::TestRound_param_2_{decimals=0}::test_round tests/third_party/cupy/core_tests/test_ndarray_math.py::TestRound_param_0_{decimals=-2}::test_round_out tests/third_party/cupy/core_tests/test_ndarray_math.py::TestRound_param_1_{decimals=-1}::test_round_out @@ -919,35 +921,55 @@ tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticUnary_param_ tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_6_{arg1=array([[1., 2., 3.], [4., 5., 6.]], dtype=float32), arg2=array([[0., 1., 2.], [3., 4., 5.]]), name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_10_{arg1=array([[1., 2., 3.], [4., 5., 6.]], dtype=float32), arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int32), name='power'}::test_binary +tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_10_{arg1=array([[1., 2., 3.], [4., 5., 6.]], dtype=float32), arg2=array([[0, 1, 2], [3, 4, 5]]), name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_14_{arg1=array([[1., 2., 3.], [4., 5., 6.]], dtype=float32), arg2=array([[0, 1, 2], [3, 4, 5]]), name='power'}::test_binary +tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_14_{arg1=array([[1., 2., 3.], [4., 5., 6.]], dtype=float32), arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int64), name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_38_{arg1=array([[1., 2., 3.], [4., 5., 6.]], dtype=float32), arg2=array([[0., 1., 2.], [3., 4., 5.]]), name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_42_{arg1=array([[1., 2., 3.], [4., 5., 6.]], dtype=float32), arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int32), name='power'}::test_binary +tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_42_{arg1=array([[1., 2., 3.], [4., 5., 6.]], dtype=float32), arg2=array([[0, 1, 2], [3, 4, 5]]), name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_46_{arg1=array([[1., 2., 3.], [4., 5., 6.]], dtype=float32), arg2=array([[0, 1, 2], [3, 4, 5]]), name='power'}::test_binary +tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_46_{arg1=array([[1., 2., 3.], [4., 5., 6.]], dtype=float32), arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int64), name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_66_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int32), arg2=array([[0., 1., 2.], [3., 4., 5.]], dtype=float32), name='power'}::test_binary +tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_66_{arg1=array([[1, 2, 3], [4, 5, 6]]), arg2=array([[0., 1., 2.], [3., 4., 5.]], dtype=float32), name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_70_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int32), arg2=array([[0., 1., 2.], [3., 4., 5.]]), name='power'}::test_binary +tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_70_{arg1=array([[1, 2, 3], [4, 5, 6]]), arg2=array([[0., 1., 2.], [3., 4., 5.]]), name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_74_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int32), arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int32), name='power'}::test_binary +tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_74_{arg1=array([[1, 2, 3], [4, 5, 6]]), arg2=array([[0, 1, 2], [3, 4, 5]]), name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_78_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int32), arg2=array([[0, 1, 2], [3, 4, 5]]), name='power'}::test_binary +tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_78_{arg1=array([[1, 2, 3], [4, 5, 6]]), arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int64), name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_82_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int32), arg2=0, name='power'}::test_binary +tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_82_{arg1=array([[1, 2, 3], [4, 5, 6]]), arg2=0, name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_86_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int32), arg2=0.0, name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_90_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int32), arg2=2, name='power'}::test_binary +tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_90_{arg1=array([[1, 2, 3], [4, 5, 6]]), arg2=2, name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_94_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int32), arg2=2.0, name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_98_{arg1=array([[1, 2, 3], [4, 5, 6]]), arg2=array([[0., 1., 2.], [3., 4., 5.]], dtype=float32), name='power'}::test_binary +tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_98_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int64), arg2=array([[0., 1., 2.], [3., 4., 5.]], dtype=float32), name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_102_{arg1=array([[1, 2, 3], [4, 5, 6]]), arg2=array([[0., 1., 2.], [3., 4., 5.]]), name='power'}::test_binary +tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_102_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int64), arg2=array([[0., 1., 2.], [3., 4., 5.]]), name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_106_{arg1=array([[1, 2, 3], [4, 5, 6]]), arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int32), name='power'}::test_binary +tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_106_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int64), arg2=array([[0, 1, 2], [3, 4, 5]]), name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_110_{arg1=array([[1, 2, 3], [4, 5, 6]]), arg2=array([[0, 1, 2], [3, 4, 5]]), name='power'}::test_binary +tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_110_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int64), arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int64), name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_114_{arg1=array([[1, 2, 3], [4, 5, 6]]), arg2=0, name='power'}::test_binary +tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_114_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int64), arg2=0, name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_118_{arg1=array([[1, 2, 3], [4, 5, 6]]), arg2=0.0, name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_122_{arg1=array([[1, 2, 3], [4, 5, 6]]), arg2=2, name='power'}::test_binary +tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_122_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int64), arg2=2, name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_126_{arg1=array([[1, 2, 3], [4, 5, 6]]), arg2=2.0, name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_134_{arg1=0, arg2=array([[0., 1., 2.], [3., 4., 5.]]), name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_138_{arg1=0, arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int32), name='power'}::test_binary +tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_138_{arg1=0, arg2=array([[0, 1, 2], [3, 4, 5]]), name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_142_{arg1=0, arg2=array([[0, 1, 2], [3, 4, 5]]), name='power'}::test_binary +tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_142_{arg1=0, arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int64), name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_166_{arg1=0.0, arg2=array([[0., 1., 2.], [3., 4., 5.]]), name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_170_{arg1=0.0, arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int32), name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_174_{arg1=0.0, arg2=array([[0, 1, 2], [3, 4, 5]]), name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_198_{arg1=2, arg2=array([[0., 1., 2.], [3., 4., 5.]]), name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_202_{arg1=2, arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int32), name='power'}::test_binary +tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_202_{arg1=2, arg2=array([[0, 1, 2], [3, 4, 5]]), name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_206_{arg1=2, arg2=array([[0, 1, 2], [3, 4, 5]]), name='power'}::test_binary +tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_206_{arg1=2, arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int64), name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_230_{arg1=2.0, arg2=array([[0., 1., 2.], [3., 4., 5.]]), name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_234_{arg1=2.0, arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int32), name='power'}::test_binary tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary_param_238_{arg1=2.0, arg2=array([[0, 1, 2], [3, 4, 5]]), name='power'}::test_binary diff --git a/tests/test_arraycreation.py b/tests/test_arraycreation.py index f846a74ef38e..f8f6007c2f82 100644 --- a/tests/test_arraycreation.py +++ b/tests/test_arraycreation.py @@ -1,5 +1,6 @@ import operator import tempfile +from math import prod import dpctl import dpctl.tensor as dpt @@ -20,6 +21,30 @@ ) +@pytest.mark.parametrize( + "func, kwargs", + [ + pytest.param("array", {"subok": True}), + pytest.param("array", {"ndmin": 1}), + pytest.param("array", {"like": dpnp.ones(10)}), + pytest.param("asanyarray", {"like": dpnp.array(7)}), + pytest.param("asarray", {"like": dpnp.array([1, 5])}), + pytest.param("ascontiguousarray", {"like": dpnp.zeros(4)}), + pytest.param("asfortranarray", {"like": dpnp.empty((2, 4))}), + ], +) +def test_array_copy_exception(func, kwargs): + sh = (3, 5) + x = dpnp.arange(1, prod(sh) + 1, 1).reshape(sh) + + with pytest.raises( + ValueError, + match=f"Keyword argument `{list(kwargs.keys())[0]}` is supported " + "only with default value", + ): + getattr(dpnp, func)(x, **kwargs) + + @pytest.mark.parametrize( "start", [0, -5, 10, -2.5, 9.7], ids=["0", "-5", "10", "-2.5", "9.7"] ) diff --git a/tests/test_sycl_queue.py b/tests/test_sycl_queue.py index 6cc3cce50d16..78f98c61581c 100644 --- a/tests/test_sycl_queue.py +++ b/tests/test_sycl_queue.py @@ -983,7 +983,10 @@ def test_to_device(device_from, device_to): valid_devices, ids=[device.filter_string for device in valid_devices], ) -@pytest.mark.parametrize("func", ["array", "asarray"]) +@pytest.mark.parametrize( + "func", + ["array", "asarray", "asanyarray", "ascontiguousarray", "asfortranarray"], +) @pytest.mark.parametrize( "device_param", ["", "None", "sycl_device"], ids=["Empty", "None", "device"] ) diff --git a/tests/test_usm_type.py b/tests/test_usm_type.py index bda39505a1c3..522348e02055 100644 --- a/tests/test_usm_type.py +++ b/tests/test_usm_type.py @@ -1,3 +1,5 @@ +from math import prod + import dpctl.utils as du import pytest @@ -128,6 +130,22 @@ def test_array_creation(func, args, usm_type_x, usm_type_y): assert y.usm_type == usm_type_y +@pytest.mark.parametrize( + "func", + ["array", "asarray", "asanyarray", "ascontiguousarray", "asfortranarray"], +) +@pytest.mark.parametrize("usm_type_x", list_of_usm_types, ids=list_of_usm_types) +@pytest.mark.parametrize("usm_type_y", list_of_usm_types, ids=list_of_usm_types) +def test_array_copy(func, usm_type_x, usm_type_y): + sh = (3, 7, 5) + x = dp.arange(1, prod(sh) + 1, 1, usm_type=usm_type_x).reshape(sh) + + y = getattr(dp, func)(x, usm_type=usm_type_y) + + assert x.usm_type == usm_type_x + assert y.usm_type == usm_type_y + + @pytest.mark.parametrize( "usm_type_start", list_of_usm_types, ids=list_of_usm_types ) diff --git a/tests/third_party/cupy/creation_tests/test_from_data.py b/tests/third_party/cupy/creation_tests/test_from_data.py index 235b5041171a..be606d1840f3 100644 --- a/tests/third_party/cupy/creation_tests/test_from_data.py +++ b/tests/third_party/cupy/creation_tests/test_from_data.py @@ -1,54 +1,56 @@ import tempfile import unittest +import dpctl import numpy import pytest import dpnp as cupy +from tests.helper import has_support_aspect64 from tests.third_party.cupy import testing -@testing.gpu class TestFromData(unittest.TestCase): - # @testing.for_orders('CFAK') - @testing.for_orders("C") + @testing.for_orders("CFAK") @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_array(self, xp, dtype, order): return xp.array([[1, 2, 3], [2, 3, 4]], dtype=dtype, order=order) - # @testing.for_orders('CFAK') - @testing.for_orders("C") + @testing.for_orders("CFAK") @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_array_from_empty_list(self, xp, dtype, order): return xp.array([], dtype=dtype, order=order) - # @testing.for_orders('CFAK') - @testing.for_orders("C") + @testing.for_orders("CFAK") @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_array_from_nested_empty_list(self, xp, dtype, order): return xp.array([[], []], dtype=dtype, order=order) - # @testing.for_orders('CFAK') - @testing.for_orders("C") + @testing.for_orders("CFAK") @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_array_from_numpy(self, xp, dtype, order): a = testing.shaped_arange((2, 3, 4), numpy, dtype) return xp.array(a, order=order) - # @testing.for_orders('CFAK') - @testing.for_orders("C") + @testing.for_orders("CFAK") @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_array_from_numpy_scalar(self, xp, dtype, order): a = numpy.array(2, dtype=dtype) return xp.array(a, order=order) - # @testing.for_orders('CFAK') - @testing.for_orders("C") + @testing.for_orders("CFAK") + @testing.for_all_dtypes() + @testing.numpy_cupy_array_equal() + def test_array_from_numpy_c_and_f(self, xp, dtype, order): + a = numpy.ones((1, 3, 1), dtype=dtype) + return xp.array(a, order=order) + + @testing.for_orders("CFAK") @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_array_from_numpy_broad_cast(self, xp, dtype, order): @@ -59,7 +61,7 @@ def test_array_from_numpy_broad_cast(self, xp, dtype, order): @testing.for_orders("CFAK", name="src_order") @testing.for_orders("CFAK", name="dst_order") @testing.for_all_dtypes() - @testing.numpy_cupy_array_equal(strides_check=True) + @testing.numpy_cupy_array_equal() def test_array_from_list_of_numpy(self, xp, dtype, src_order, dst_order): # compares numpy.array() with # cupy.array() @@ -72,7 +74,7 @@ def test_array_from_list_of_numpy(self, xp, dtype, src_order, dst_order): @testing.for_orders("CFAK", name="src_order") @testing.for_orders("CFAK", name="dst_order") @testing.for_all_dtypes() - @testing.numpy_cupy_array_equal(strides_check=True) + @testing.numpy_cupy_array_equal() def test_array_from_list_of_numpy_view( self, xp, dtype, src_order, dst_order ): @@ -90,7 +92,7 @@ def test_array_from_list_of_numpy_view( @testing.for_orders("CFAK") @testing.for_all_dtypes() - @testing.numpy_cupy_array_equal(strides_check=True) + @testing.numpy_cupy_array_equal() def test_array_from_list_of_numpy_scalar(self, xp, dtype, order): # compares numpy.array() with # cupy.array() @@ -100,7 +102,7 @@ def test_array_from_list_of_numpy_scalar(self, xp, dtype, order): @testing.for_orders("CFAK", name="src_order") @testing.for_orders("CFAK", name="dst_order") @testing.for_all_dtypes() - @testing.numpy_cupy_array_equal(strides_check=True) + @testing.numpy_cupy_array_equal() def test_array_from_nested_list_of_numpy( self, xp, dtype, src_order, dst_order ): @@ -115,7 +117,7 @@ def test_array_from_nested_list_of_numpy( @testing.for_orders("CFAK", name="src_order") @testing.for_orders("CFAK", name="dst_order") @testing.for_all_dtypes_combination(names=("dtype1", "dtype2")) - @testing.numpy_cupy_array_equal(strides_check=True) + @testing.numpy_cupy_array_equal() def test_array_from_list_of_cupy( self, xp, dtype1, dtype2, src_order, dst_order ): @@ -125,12 +127,21 @@ def test_array_from_list_of_cupy( testing.shaped_arange((3, 4), xp, dtype1, src_order), testing.shaped_arange((3, 4), xp, dtype2, src_order), ] + + # need to align with the use case when device doesn't has aspect fp64 + if xp is numpy and not has_support_aspect64(): + dt = numpy.promote_types(dtype1, dtype2) + if dt == numpy.float64: + return numpy.array(a, order=dst_order, dtype=numpy.float32) + elif dt == numpy.complex128: + return numpy.array(a, order=dst_order, dtype=numpy.complex64) + return xp.array(a, order=dst_order) @testing.for_orders("CFAK", name="src_order") @testing.for_orders("CFAK", name="dst_order") @testing.for_all_dtypes() - @testing.numpy_cupy_array_equal(strides_check=True) + @testing.numpy_cupy_array_equal() def test_array_from_list_of_cupy_view( self, xp, dtype, src_order, dst_order ): @@ -149,7 +160,7 @@ def test_array_from_list_of_cupy_view( @testing.for_orders("CFAK", name="src_order") @testing.for_orders("CFAK", name="dst_order") @testing.for_all_dtypes() - @testing.numpy_cupy_array_equal(strides_check=True) + @testing.numpy_cupy_array_equal() def test_array_from_nested_list_of_cupy( self, xp, dtype, src_order, dst_order ): @@ -163,23 +174,44 @@ def test_array_from_nested_list_of_cupy( @testing.for_orders("CFAK") @testing.for_all_dtypes() - @testing.numpy_cupy_array_equal(strides_check=True) + @testing.numpy_cupy_array_equal() def test_array_from_list_of_cupy_scalar(self, xp, dtype, order): # compares numpy.array() with # cupy.array() a = [xp.array(i, dtype=dtype) for i in range(2)] return xp.array(a, order=order) - # @testing.for_orders('CFAK') - @testing.for_orders("C") + @testing.for_orders("CFAK") + @testing.for_all_dtypes() + @testing.numpy_cupy_array_equal() + def test_array_from_nested_list_of_cupy_scalar(self, xp, dtype, order): + # compares numpy.array() with + # cupy.array() + a = [[xp.array(i, dtype=dtype)] for i in range(2)] + return xp.array(a, order=order) + + @testing.for_orders("CFAK") @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_array_copy(self, xp, dtype, order): a = testing.shaped_arange((2, 3, 4), xp, dtype) return xp.array(a, order=order) - # @testing.for_orders('CFAK') - @testing.for_orders("C") + @testing.for_orders("CFAK") + @testing.for_all_dtypes() + @testing.numpy_cupy_array_equal() + def test_array_both_c_and_f_contig_copy(self, xp, dtype, order): + a = testing.shaped_arange((1, 4, 1), xp, dtype, order="C") + return xp.array(a, order=order) + + @testing.for_orders("CFAK") + @testing.for_all_dtypes() + @testing.numpy_cupy_array_equal() + def test_array_both_c_and_f_contig_f_strides_copy(self, xp, dtype, order): + a = testing.shaped_arange((1, 4, 1), xp, dtype, order="F") + return xp.array(a, order=order) + + @testing.for_orders("CFAK") @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_array_copy_is_copied(self, xp, dtype, order): @@ -188,8 +220,7 @@ def test_array_copy_is_copied(self, xp, dtype, order): a.fill(0) return b - # @testing.for_orders('CFAK') - @testing.for_orders("C") + @testing.for_orders("CFAK") @testing.for_all_dtypes(name="dtype1", no_complex=True) @testing.for_all_dtypes(name="dtype2") @testing.numpy_cupy_array_equal() @@ -198,8 +229,7 @@ def test_array_copy_with_dtype(self, xp, dtype1, dtype2, order): a = testing.shaped_arange((2, 3, 4), xp, dtype1) return xp.array(a, dtype=dtype2, order=order) - # @testing.for_orders('CFAK') - @testing.for_orders("C") + @testing.for_orders("CFAK") @testing.for_all_dtypes(name="dtype1", no_complex=True) @testing.for_all_dtypes(name="dtype2") @testing.numpy_cupy_array_equal() @@ -208,8 +238,7 @@ def test_array_copy_with_dtype_char(self, xp, dtype1, dtype2, order): a = testing.shaped_arange((2, 3, 4), xp, dtype1) return xp.array(a, dtype=numpy.dtype(dtype2).char, order=order) - # @testing.for_orders('CFAK') - @testing.for_orders("C") + @testing.for_orders("CFAK") @testing.numpy_cupy_array_equal() def test_array_copy_with_dtype_being_none(self, xp, order): a = testing.shaped_arange((2, 3, 4), xp) @@ -219,7 +248,7 @@ def test_array_copy_with_dtype_being_none(self, xp, order): @testing.for_orders("CFAK", name="dst_order") @testing.for_all_dtypes(name="dtype1", no_complex=True) @testing.for_all_dtypes(name="dtype2") - @testing.numpy_cupy_array_equal(strides_check=True) + @testing.numpy_cupy_array_equal() def test_array_copy_list_of_numpy_with_dtype( self, xp, dtype1, dtype2, src_order, dst_order ): @@ -235,7 +264,7 @@ def test_array_copy_list_of_numpy_with_dtype( @testing.for_orders("CFAK", name="dst_order") @testing.for_all_dtypes(name="dtype1", no_complex=True) @testing.for_all_dtypes(name="dtype2") - @testing.numpy_cupy_array_equal(strides_check=True) + @testing.numpy_cupy_array_equal() def test_array_copy_list_of_numpy_with_dtype_char( self, xp, dtype1, dtype2, src_order, dst_order ): @@ -251,7 +280,7 @@ def test_array_copy_list_of_numpy_with_dtype_char( @testing.for_orders("CFAK", name="dst_order") @testing.for_all_dtypes(name="dtype1", no_complex=True) @testing.for_all_dtypes(name="dtype2") - @testing.numpy_cupy_array_equal(strides_check=True) + @testing.numpy_cupy_array_equal() def test_array_copy_list_of_cupy_with_dtype( self, xp, dtype1, dtype2, src_order, dst_order ): @@ -267,7 +296,7 @@ def test_array_copy_list_of_cupy_with_dtype( @testing.for_orders("CFAK", name="dst_order") @testing.for_all_dtypes(name="dtype1", no_complex=True) @testing.for_all_dtypes(name="dtype2") - @testing.numpy_cupy_array_equal(strides_check=True) + @testing.numpy_cupy_array_equal() def test_array_copy_list_of_cupy_with_dtype_char( self, xp, dtype1, dtype2, src_order, dst_order ): @@ -304,30 +333,31 @@ def test_array_f_contiguous_output(self, xp, dtype): assert b.flags.f_contiguous return b - # @testing.multi_gpu(2) - # def test_array_multi_device(self): - # with cuda.Device(0): - # x = testing.shaped_arange((2, 3, 4), cupy, dtype='f') - # with cuda.Device(1): - # y = cupy.array(x) - # assert isinstance(y, cupy.ndarray) - # assert x is not y # Do copy - # assert int(x.device) == 0 - # assert int(y.device) == 1 - # testing.assert_array_equal(x, y) - - # @testing.multi_gpu(2) - # def test_array_multi_device_zero_size(self): - # with cuda.Device(0): - # x = testing.shaped_arange((0,), cupy, dtype='f') - # with cuda.Device(1): - # y = cupy.array(x) - # assert isinstance(y, cupy.ndarray) - # assert x is not y # Do copy - # assert x.device.id == 0 - # assert y.device.id == 1 - # testing.assert_array_equal(x, y) - + def test_array_multi_device(self): + q1 = dpctl.SyclQueue() + q2 = dpctl.SyclQueue() + + x = cupy.ones((2, 3, 4), dtype=cupy.float32, device=q1) + y = cupy.array(x, device=q2) + assert isinstance(y, cupy.ndarray) + assert x is not y # Do copy + assert x.sycl_queue == q1 + assert y.sycl_queue == q2 + testing.assert_array_equal(x, y) + + def test_array_multi_device_zero_size(self): + q1 = dpctl.SyclQueue() + q2 = dpctl.SyclQueue() + + x = cupy.ones((0,), dtype=cupy.float32, device=q1) + y = cupy.array(x, device=q2) + assert isinstance(y, cupy.ndarray) + assert x is not y # Do copy + assert x.sycl_queue == q1 + assert y.sycl_queue == q2 + testing.assert_array_equal(x, y) + + @pytest.mark.skip("`ndmin` argument isn't supported") @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_array_no_copy_ndmin(self, xp, dtype): @@ -337,6 +367,27 @@ def test_array_no_copy_ndmin(self, xp, dtype): a.fill(0) return b + @pytest.mark.skip("only native byteorder is supported") + @testing.for_all_dtypes() + @testing.numpy_cupy_array_equal() + def test_array_from_big_endian(self, xp, dtype): + dtype = numpy.dtype(dtype).newbyteorder(">") + a = testing.shaped_arange((2, 3, 4), numpy, dtype) + b = xp.array(a) + # Make a computation here as just moving big-endian data back and forth + # happens to work before the change in #5828 + return b + b + + @testing.for_all_dtypes() + @testing.numpy_cupy_array_equal() + def test_array_from_list_of_numpy_big_endian(self, xp, dtype): + dtype = numpy.dtype(dtype).newbyteorder(">") + a = [testing.shaped_arange((3, 4), numpy, dtype) for i in range(2)] + b = xp.array(a) + # Make a computation here as just moving big-endian data back and forth + # happens to work before the change in #5828 + return b + b + @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_asarray(self, xp, dtype): @@ -351,8 +402,7 @@ def test_asarray_is_not_copied(self, xp, dtype): a.fill(0) return b - # @testing.for_CF_orders() - @testing.for_orders("C") + @testing.for_CF_orders() @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_asarray_with_order(self, xp, dtype, order): @@ -386,8 +436,18 @@ def test_asanyarray_with_order(self, xp, dtype, order): assert b.flags.c_contiguous return b - # @testing.for_CF_orders() - @testing.for_orders("C") + @pytest.mark.skip("only native byteorder is supported") + @testing.for_all_dtypes() + @testing.numpy_cupy_array_equal() + def test_asanyarray_from_big_endian(self, xp, dtype): + dtype = numpy.dtype(dtype).newbyteorder(">") + a = testing.shaped_arange((2, 3, 4), numpy, dtype) + b = xp.asanyarray(a) + # Make a computation here as just moving big-endian data back and forth + # happens to work before the change in #5828 + return b + b + + @testing.for_CF_orders() @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_asarray_from_numpy(self, xp, dtype, order): @@ -420,19 +480,29 @@ def test_ascontiguousarray_on_contiguous_array(self): b = cupy.ascontiguousarray(a) assert a is b - @testing.numpy_cupy_array_equal() + @testing.numpy_cupy_array_equal(type_check=has_support_aspect64()) def test_asarray_cuda_array_zero_dim(self, xp): a = xp.ones(()) return xp.ascontiguousarray(a) @testing.numpy_cupy_array_equal() def test_asarray_cuda_array_zero_dim_dtype(self, xp): - a = xp.ones((), dtype=numpy.float64) + a = xp.ones((), dtype=numpy.float32) return xp.ascontiguousarray(a, dtype=numpy.int64) - # @testing.for_CF_orders() + @pytest.mark.skip("only native byteorder is supported") + @testing.for_all_dtypes() + @testing.numpy_cupy_array_equal() + def test_asarray_from_big_endian(self, xp, dtype): + dtype = numpy.dtype(dtype).newbyteorder(">") + a = testing.shaped_arange((2, 3, 4), numpy, dtype) + b = xp.asarray(a) + # Make a computation here as just moving big-endian data back and forth + # happens to work before the change in #5828 + return b + b + @pytest.mark.usefixtures("allow_fall_back_on_numpy") - @testing.for_orders("C") + @testing.for_CF_orders() @testing.for_all_dtypes() @testing.numpy_cupy_array_equal() def test_copy(self, xp, dtype, order): @@ -441,15 +511,15 @@ def test_copy(self, xp, dtype, order): a[1] = 1 return b - # @testing.multi_gpu(2) - # @testing.for_CF_orders() - # @testing.for_all_dtypes() - # def test_copy_multigpu(self, dtype, order): - # with cuda.Device(0): - # src = cupy.random.uniform(-1, 1, (2, 3)).astype(dtype) - # with cuda.Device(1): - # dst = cupy.copy(src, order) - # testing.assert_allclose(src, dst, rtol=0, atol=0) + @pytest.mark.skip("`device` argument isn't supported") + @testing.for_CF_orders() + @testing.for_all_dtypes() + def test_copy_multigpu(self, dtype, order): + with cuda.Device(0): + src = cupy.random.uniform(-1, 1, (2, 3)).astype(dtype) + with cuda.Device(1): + dst = cupy.copy(src, order) + testing.assert_allclose(src, dst, rtol=0, atol=0) @testing.for_CF_orders() @testing.numpy_cupy_equal() @@ -458,7 +528,7 @@ def test_copy_order(self, xp, order): b = xp.copy(a) return (b.flags.c_contiguous, b.flags.f_contiguous) - @testing.numpy_cupy_array_equal() + @testing.numpy_cupy_array_equal(type_check=has_support_aspect64()) def test_asfortranarray_cuda_array_zero_dim(self, xp): a = xp.ones(()) return xp.asfortranarray(a) @@ -480,135 +550,237 @@ def test_fromfile(self, xp): fh.seek(0) return xp.fromfile(fh, dtype="u1") + @pytest.mark.usefixtures("allow_fall_back_on_numpy") + @testing.numpy_cupy_array_equal() + def test_fromfunction(self, xp): + def function(i, j): + return i == j + + return xp.fromfunction(function, shape=(3, 3), dtype=int) + + @pytest.mark.usefixtures("allow_fall_back_on_numpy") + @testing.numpy_cupy_array_equal(type_check=has_support_aspect64()) + def test_fromiter(self, xp): + iterable = (x * x for x in range(5)) + return xp.fromiter(iterable, float) + + @pytest.mark.usefixtures("allow_fall_back_on_numpy") + @testing.numpy_cupy_array_equal() + def test_fromstring(self, xp): + return xp.fromstring("1 2", dtype=int, sep=" ") + + @pytest.mark.usefixtures("allow_fall_back_on_numpy") + @testing.numpy_cupy_array_equal() + def test_frombuffer(self, xp): + return xp.frombuffer(b"\x01\x02", dtype=numpy.uint8) + + @pytest.mark.usefixtures("allow_fall_back_on_numpy") + @testing.numpy_cupy_array_equal() + def test_loadtxt(self, xp): + with tempfile.TemporaryFile() as fh: + fh.write(b"0 1\n2 3") + fh.flush() + fh.seek(0) + return xp.loadtxt(fh, dtype="u1") + + @pytest.mark.skip("`genfromtxt` isn't supported") + @testing.numpy_cupy_array_equal() + def test_genfromtxt(self, xp): + with tempfile.TemporaryFile() as fh: + fh.write(b"0 1\n2 3") + fh.flush() + fh.seek(0) + return xp.genfromtxt(fh, dtype="u1") + + @pytest.mark.skip("only native byteorder is supported") + @testing.numpy_cupy_array_equal() + def test_fromfile_big_endian(self, xp): + with tempfile.TemporaryFile() as fh: + fh.write(b"\x00\x00\x00\x01") + fh.flush() + fh.seek(0) + a = xp.fromfile(fh, dtype=">u4") + # Make a computation here as just moving big-endian data back and + # forth happens to work before the change in #5828 + return a + a + + +max_cuda_array_interface_version = 1 -############################################################################### -# __cuda_array_interface__ tests -############################################################################### - -# max_cuda_array_interface_version = 2 - - -# @testing.gpu -# @testing.parameterize(*testing.product({ -# 'ver': tuple(range(max_cuda_array_interface_version+1)), -# 'strides': (False, None, True), -# })) -# class TestCudaArrayInterface(unittest.TestCase): -# @testing.for_all_dtypes() -# def test_base(self, dtype): -# a = testing.shaped_arange((2, 3, 4), cupy, dtype) -# b = cupy.asarray( -# DummyObjectWithCudaArrayInterface(a, self.ver, self.strides)) -# testing.assert_array_equal(a, b) - -# @testing.for_all_dtypes() -# def test_not_copied(self, dtype): -# a = testing.shaped_arange((2, 3, 4), cupy, dtype) -# b = cupy.asarray( -# DummyObjectWithCudaArrayInterface(a, self.ver, self.strides)) -# a.fill(0) -# testing.assert_array_equal(a, b) - -# @testing.for_all_dtypes() -# def test_order(self, dtype): -# a = testing.shaped_arange((2, 3, 4), cupy, dtype) -# b = cupy.asarray( -# DummyObjectWithCudaArrayInterface(a, self.ver, self.strides), -# order='F') -# assert b.flags.f_contiguous -# testing.assert_array_equal(a, b) - -# @testing.for_all_dtypes() -# def test_with_strides(self, dtype): -# a = testing.shaped_arange((2, 3, 4), cupy, dtype).T -# b = cupy.asarray( -# DummyObjectWithCudaArrayInterface(a, self.ver, self.strides)) -# assert a.strides == b.strides -# assert a.nbytes == b.data.mem.size - -# @testing.for_all_dtypes() -# def test_with_zero_size_array(self, dtype): -# a = testing.shaped_arange((0,), cupy, dtype) -# b = cupy.asarray( -# DummyObjectWithCudaArrayInterface(a, self.ver, self.strides)) -# assert a.strides == b.strides -# assert a.nbytes == b.data.mem.size -# assert a.data.ptr == 0 -# assert a.size == 0 - -# @testing.for_all_dtypes() -# def test_asnumpy(self, dtype): -# a = testing.shaped_arange((2, 3, 4), cupy, dtype) -# b = DummyObjectWithCudaArrayInterface(a, self.ver, self.strides) -# a_cpu = cupy.asnumpy(a) -# b_cpu = cupy.asnumpy(b) -# testing.assert_array_equal(a_cpu, b_cpu) - - -# @testing.gpu -# @testing.parameterize(*testing.product({ -# 'ver': tuple(range(1, max_cuda_array_interface_version+1)), -# 'strides': (False, None, True), -# })) -# class TestCudaArrayInterfaceMaskedArray(unittest.TestCase): -# # TODO(leofang): update this test when masked array is supported -# @testing.for_all_dtypes() -# def test_masked_array(self, dtype): -# a = testing.shaped_arange((2, 3, 4), cupy, dtype) -# mask = testing.shaped_arange((2, 3, 4), cupy, dtype) -# a = DummyObjectWithCudaArrayInterface(a, self.ver, self.strides, mask) -# with pytest.raises(ValueError) as ex: -# b = cupy.asarray(a) -# assert 'does not support' in str(ex.value) - - -# @testing.slow -# @testing.gpu -# class TestCudaArrayInterfaceBigArray(unittest.TestCase): -# def test_with_over_size_array(self): -# # real example from #3009 -# size = 5 * 10**8 -# try: -# a = testing.shaped_random((size,), cupy, cupy.float64) -# b = cupy.asarray(DummyObjectWithCudaArrayInterface(a, 2, None)) -# testing.assert_array_equal(a, b) -# except cupy.cuda.memory.OutOfMemoryError: -# pass -# else: -# del b, a -# finally: -# cupy.get_default_memory_pool().free_all_blocks() - - -# class DummyObjectWithCudaArrayInterface(object): -# def __init__(self, a, ver, include_strides=False, mask=None): -# assert ver in tuple(range(max_cuda_array_interface_version+1)) -# self.a = a -# self.ver = ver -# self.include_strides = include_strides -# self.mask = mask - -# @property -# def __cuda_array_interface__(self): -# desc = { -# 'shape': self.a.shape, -# 'typestr': self.a.dtype.str, -# 'descr': self.a.dtype.descr, -# 'data': (self.a.data.ptr, False), -# 'version': self.ver, -# } -# if self.a.flags.c_contiguous: -# if self.include_strides is True: -# desc['strides'] = self.a.strides -# elif self.include_strides is None: -# desc['strides'] = None -# else: # self.include_strides is False -# pass -# else: # F contiguous or neither -# desc['strides'] = self.a.strides -# if self.mask is not None: -# desc['mask'] = self.mask -# return desc + +@testing.parameterize( + *testing.product( + { + "ver": (max_cuda_array_interface_version,), + "strides": (False, None, True), + } + ) +) +class TestCudaArrayInterface(unittest.TestCase): + @testing.for_all_dtypes() + def test_base(self, dtype): + a = testing.shaped_arange((2, 3, 4), cupy, dtype) + b = cupy.asarray( + DummyObjectWithCudaArrayInterface(a, self.ver, self.strides) + ) + testing.assert_array_equal(a, b) + + @pytest.mark.skip("that isn't supported yet") + @testing.for_all_dtypes() + def test_not_copied(self, dtype): + a = testing.shaped_arange((2, 3, 4), cupy, dtype) + b = cupy.asarray( + DummyObjectWithCudaArrayInterface(a, self.ver, self.strides) + ) + a.fill(0) + testing.assert_array_equal(a, b) + + @testing.for_all_dtypes() + def test_order(self, dtype): + a = testing.shaped_arange((2, 3, 4), cupy, dtype) + b = cupy.asarray( + DummyObjectWithCudaArrayInterface(a, self.ver, self.strides), + order="F", + ) + assert b.flags.f_contiguous + testing.assert_array_equal(a, b) + + @testing.for_all_dtypes() + def test_with_strides(self, dtype): + a = testing.shaped_arange((2, 3, 4), cupy, dtype).T + b = cupy.asarray( + DummyObjectWithCudaArrayInterface(a, self.ver, self.strides) + ) + assert a.strides == b.strides + assert a.nbytes == b.nbytes + + @testing.for_all_dtypes() + def test_with_zero_size_array(self, dtype): + a = testing.shaped_arange((0,), cupy, dtype) + b = cupy.asarray( + DummyObjectWithCudaArrayInterface(a, self.ver, self.strides) + ) + assert a.strides == b.strides + assert a.nbytes == b.nbytes + assert a.size == 0 + + @pytest.mark.skip("that isn't supported yet") + @testing.for_all_dtypes() + def test_asnumpy(self, dtype): + a = testing.shaped_arange((2, 3, 4), cupy, dtype) + b = DummyObjectWithCudaArrayInterface(a, self.ver, self.strides) + a_cpu = cupy.asnumpy(a) + b_cpu = cupy.asnumpy(b) + testing.assert_array_equal(a_cpu, b_cpu) + + @pytest.mark.skip("only native byteorder is supported") + def test_big_endian(self): + a = cupy.array([0x1, 0x0, 0x0, 0x0], dtype=numpy.int8) + dtype = numpy.dtype(">i4") + shape = (1,) + strides = (4,) + data = a.data.ptr + b = DummyObjectWithCudaArrayInterface( + (shape, strides, dtype.str, dtype.descr, data), + self.ver, + self.strides, + ) + with pytest.raises(ValueError): + cupy.asarray(b) + + +@testing.parameterize( + *testing.product( + { + "ver": tuple(range(1, max_cuda_array_interface_version + 1)), + "strides": (False, None, True), + } + ) +) +class TestCudaArrayInterfaceMaskedArray(unittest.TestCase): + # TODO(leofang): update this test when masked array is supported + @pytest.mark.skip("that isn't supported") + @testing.for_all_dtypes() + def test_masked_array(self, dtype): + a = testing.shaped_arange((2, 3, 4), cupy, dtype) + mask = testing.shaped_arange((2, 3, 4), cupy, dtype) + a = DummyObjectWithCudaArrayInterface(a, self.ver, self.strides, mask) + with pytest.raises(ValueError) as ex: + b = cupy.asarray(a) + assert "does not support" in str(ex.value) + + +@pytest.mark.skip() +class TestCudaArrayInterfaceBigArray(unittest.TestCase): + @pytest.mark.skip("that isn't supported") + def test_with_over_size_array(self): + # real example from #3009 + size = 5 * 10**8 + a = testing.shaped_random((size,), cupy, cupy.float64) + b = cupy.asarray(DummyObjectWithCudaArrayInterface(a, 2, None)) + testing.assert_array_equal(a, b) + + +class DummyObjectWithCudaArrayInterface(object): + def __init__(self, a, ver, include_strides=False, mask=None, stream=None): + assert ver in tuple(range(max_cuda_array_interface_version + 1)) + self.a = None + if isinstance(a, cupy.ndarray): + self.a = a + else: + self.shape, self.strides, self.typestr, self.descr, self.data = a + self.ver = ver + self.include_strides = include_strides + self.mask = mask + self.stream = stream + + @property + def __sycl_usm_array_interface__(self): + if self.a is not None: + desc = { + "shape": self.a.shape, + "typestr": self.a.dtype.str, + "data": (self.a.get_array()._pointer, False), + "version": self.a.__sycl_usm_array_interface__["version"], + "syclobj": self.a.sycl_queue, + "offset": self.a.get_array()._element_offset, + } + if self.a.flags.c_contiguous: + if self.include_strides is True: + desc["strides"] = self.a.strides + elif self.include_strides is None: + desc["strides"] = None + else: # self.include_strides is False + pass + else: # F contiguous or neither + desc["strides"] = self.a.strides + else: + desc = { + "shape": self.shape, + "typestr": self.typestr, + "descr": self.descr, + "data": (self.data, False), + "version": self.ver, + } + if self.include_strides is True: + desc["strides"] = self.strides + elif self.include_strides is None: + desc["strides"] = None + else: # self.include_strides is False + pass + if self.mask is not None: + desc["mask"] = self.mask + # The stream field is kept here for compliance. However, since the + # synchronization is done via calling a cpdef function, which cannot + # be mock-tested. + if self.stream is not None: + if self.stream is cuda.Stream.null: + desc["stream"] = cuda.runtime.streamLegacy + elif (not cuda.runtime.is_hip) and self.stream is cuda.Stream.ptds: + desc["stream"] = cuda.runtime.streamPerThread + else: + desc["stream"] = self.stream.ptr + return desc @testing.parameterize( @@ -617,6 +789,7 @@ def test_fromfile(self, xp): ) ) class TestArrayPreservationOfShape(unittest.TestCase): + @pytest.mark.skip("`ndmin` argument isn't supported") @testing.for_all_dtypes() def test_cupy_array(self, dtype): shape = 2, 3 @@ -634,6 +807,7 @@ def test_cupy_array(self, dtype): ) ) class TestArrayCopy(unittest.TestCase): + @pytest.mark.skip("`ndmin` argument isn't supported") @testing.for_all_dtypes() def test_cupy_array(self, dtype): a = testing.shaped_arange((2, 3), self.xp, dtype) @@ -652,6 +826,5 @@ def test_cupy_array(self, dtype): class TestArrayInvalidObject(unittest.TestCase): def test_invalid_type(self): a = numpy.array([1, 2, 3], dtype=object) - # with self.assertRaises(ValueError): with self.assertRaises(TypeError): cupy.array(a)