diff --git a/dpnp/dpnp_iface_manipulation.py b/dpnp/dpnp_iface_manipulation.py index 840a1c634152..a747719e1c68 100644 --- a/dpnp/dpnp_iface_manipulation.py +++ b/dpnp/dpnp_iface_manipulation.py @@ -47,7 +47,6 @@ import dpnp from dpnp.dpnp_algo import * from dpnp.dpnp_array import dpnp_array -from dpnp.dpnp_iface_arraycreation import array from dpnp.dpnp_utils import * __all__ = [ @@ -80,7 +79,7 @@ ] -def asfarray(a, dtype=None): +def asfarray(a, dtype=None, *, device=None, usm_type=None, sycl_queue=None): """ Return an array converted to a float type. @@ -88,24 +87,37 @@ def asfarray(a, dtype=None): Notes ----- - This function works exactly the same as :obj:`dpnp.array`. - If dtype is `None`, `bool` or one of the `int` dtypes, it is replaced with - the default floating type in DPNP depending on device capabilities. + If `dtype` is ``None``, :obj:`dpnp.bool` or one of the `int` dtypes, + it is replaced with the default floating type (:obj:`dpnp.float64` + if a device supports it, or :obj:`dpnp.float32` type otherwise). - """ + Returns + ------- + y : dpnp.ndarray + The input a as a float ndarray. - a_desc = dpnp.get_dpnp_descriptor(a, copy_when_nondefault_queue=False) - if a_desc: - if dtype is None or not numpy.issubdtype(dtype, dpnp.inexact): - dtype = dpnp.default_float_type(sycl_queue=a.sycl_queue) + Examples + -------- + >>> import dpnp as np + >>> np.asfarray([2, 3]) + array([2., 3.]) + >>> np.asfarray([2, 3], dtype=dpnp.float32) + array([2., 3.], dtype=float32) + >>> np.asfarray([2, 3], dtype=dpnp.int32) + array([2., 3.]) + + """ - # if type is the same then same object should be returned - if a_desc.dtype == dtype: - return a + _sycl_queue = dpnp.get_normalized_queue_device( + a, sycl_queue=sycl_queue, device=device + ) - return array(a, dtype=dtype) + if dtype is None or not numpy.issubdtype(dtype, dpnp.inexact): + dtype = dpnp.default_float_type(sycl_queue=_sycl_queue) - return call_origin(numpy.asfarray, a, dtype) + return dpnp.asarray( + a, dtype=dtype, usm_type=usm_type, sycl_queue=_sycl_queue + ) def atleast_1d(*arys): diff --git a/tests/test_arraymanipulation.py b/tests/test_arraymanipulation.py index 2dc3b861b7ba..510b347ab3b5 100644 --- a/tests/test_arraymanipulation.py +++ b/tests/test_arraymanipulation.py @@ -13,7 +13,6 @@ from .helper import get_all_dtypes, get_float_complex_dtypes -@pytest.mark.usefixtures("allow_fall_back_on_numpy") @pytest.mark.parametrize("dtype", get_all_dtypes()) @pytest.mark.parametrize( "data", [[1, 2, 3], [1.0, 2.0, 3.0]], ids=["[1, 2, 3]", "[1., 2., 3.]"] diff --git a/tests/test_sycl_queue.py b/tests/test_sycl_queue.py index aaa7b3bae8e9..0bb59384cefc 100644 --- a/tests/test_sycl_queue.py +++ b/tests/test_sycl_queue.py @@ -1020,7 +1020,14 @@ def test_to_device(device_from, device_to): ) @pytest.mark.parametrize( "func", - ["array", "asarray", "asanyarray", "ascontiguousarray", "asfortranarray"], + [ + "array", + "asarray", + "asanyarray", + "ascontiguousarray", + "asfarray", + "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 8eb8b1b779d8..7c11538d7318 100644 --- a/tests/test_usm_type.py +++ b/tests/test_usm_type.py @@ -159,7 +159,14 @@ def test_array_creation(func, args, usm_type_x, usm_type_y): @pytest.mark.parametrize( "func", - ["array", "asarray", "asanyarray", "ascontiguousarray", "asfortranarray"], + [ + "array", + "asarray", + "asanyarray", + "ascontiguousarray", + "asfarray", + "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) diff --git a/tests/third_party/cupy/manipulation_tests/test_kind.py b/tests/third_party/cupy/manipulation_tests/test_kind.py new file mode 100644 index 000000000000..1812d77c0af5 --- /dev/null +++ b/tests/third_party/cupy/manipulation_tests/test_kind.py @@ -0,0 +1,143 @@ +import unittest + +import numpy +import pytest + +import dpnp as cupy +from tests.helper import has_support_aspect64 +from tests.third_party.cupy import testing + + +class TestKind(unittest.TestCase): + @pytest.mark.skip("dpnp.asarray_chkfinite() is not implemented yet") + @testing.for_orders("CFAK") + @testing.for_all_dtypes() + @testing.numpy_cupy_array_equal() + def test_asarray_chkfinite(self, xp, dtype, order): + a = [0, 4, 0, 5] + return xp.asarray_chkfinite(a, dtype=dtype, order=order) + + @pytest.mark.skip("dpnp.asarray_chkfinite() is not implemented yet") + @testing.for_orders("CFAK") + @testing.for_all_dtypes(no_bool=True) + def test_asarray_chkfinite_non_finite_vals(self, dtype, order): + a = [-numpy.inf, 0.0, numpy.inf, numpy.nan] + for xp in (numpy, cupy): + if xp.issubdtype(dtype, xp.integer): + error = OverflowError + else: + error = ValueError + with pytest.raises(error): + xp.asarray_chkfinite(a, dtype=dtype, order=order) + + @testing.for_all_dtypes() + def test_asfarray(self, dtype): + a = cupy.asarray([1, 2, 3]) + a_gpu = cupy.asfarray(a, dtype) + a_cpu = numpy.asfarray(a, dtype) + if ( + has_support_aspect64() + or cupy.issubdtype(dtype, cupy.complexfloating) + or cupy.issubdtype(dtype, cupy.floating) + ): + assert a_cpu.dtype == a_gpu.dtype + else: + assert a_cpu.dtype == cupy.float64 + assert a_gpu.dtype == cupy.float32 + + @testing.for_all_dtypes() + def test_asfortranarray1(self, dtype): + def func(xp): + x = xp.zeros((2, 3), dtype=dtype) + ret = xp.asfortranarray(x) + assert x.flags.c_contiguous + assert ret.flags.f_contiguous + + assert func(numpy) == func(cupy) + + @testing.for_all_dtypes() + def test_asfortranarray2(self, dtype): + def func(xp): + x = xp.zeros((2, 3, 4), dtype=dtype) + ret = xp.asfortranarray(x) + assert x.flags.c_contiguous + assert ret.flags.f_contiguous + + assert func(numpy) == func(cupy) + + @testing.for_all_dtypes() + def test_asfortranarray3(self, dtype): + def func(xp): + x = xp.zeros((2, 3, 4), dtype=dtype) + ret = xp.asfortranarray(xp.asfortranarray(x)) + assert x.flags.c_contiguous + assert ret.flags.f_contiguous + + assert func(numpy) == func(cupy) + + @testing.for_all_dtypes() + def test_asfortranarray4(self, dtype): + def func(xp): + x = xp.zeros((2, 3), dtype=dtype) + x = xp.transpose(x, (1, 0)) + ret = xp.asfortranarray(x) + assert ret.flags.f_contiguous + + assert func(numpy) == func(cupy) + + @testing.for_all_dtypes() + def test_asfortranarray5(self, dtype): + def func(xp): + x = testing.shaped_arange((2, 3), xp, dtype) + ret = xp.asfortranarray(x) + assert x.flags.c_contiguous + assert ret.flags.f_contiguous + + assert func(numpy) == func(cupy) + + @pytest.mark.skip("dpnp.require() is not implemented yet") + @testing.for_all_dtypes() + def test_require_flag_check(self, dtype): + possible_flags = [["C_CONTIGUOUS"], ["F_CONTIGUOUS"]] + x = cupy.zeros((2, 3, 4), dtype=dtype) + for flags in possible_flags: + arr = cupy.require(x, dtype, flags) + for parameter in flags: + assert arr.flags[parameter] + assert arr.dtype == dtype + + @pytest.mark.skip("dpnp.require() is not implemented yet") + @testing.for_all_dtypes() + def test_require_owndata(self, dtype): + x = cupy.zeros((2, 3, 4), dtype=dtype) + arr = x.view() + arr = cupy.require(arr, dtype, ["O"]) + assert arr.flags["OWNDATA"] + + @pytest.mark.skip("dpnp.require() is not implemented yet") + @testing.for_all_dtypes() + def test_require_C_and_F_flags(self, dtype): + x = cupy.zeros((2, 3, 4), dtype=dtype) + with pytest.raises(ValueError): + cupy.require(x, dtype, ["C", "F"]) + + @pytest.mark.skip("dpnp.require() is not implemented yet") + @testing.for_all_dtypes() + def test_require_incorrect_requirments(self, dtype): + x = cupy.zeros((2, 3, 4), dtype=dtype) + with pytest.raises(ValueError): + cupy.require(x, dtype, ["W"]) + + @pytest.mark.skip("dpnp.require() is not implemented yet") + @testing.for_all_dtypes() + def test_require_incorrect_dtype(self, dtype): + x = cupy.zeros((2, 3, 4), dtype=dtype) + with pytest.raises(ValueError): + cupy.require(x, "random", "C") + + @pytest.mark.skip("dpnp.require() is not implemented yet") + @testing.for_all_dtypes() + def test_require_empty_requirements(self, dtype): + x = cupy.zeros((2, 3, 4), dtype=dtype) + x = cupy.require(x, dtype, []) + assert x.flags["C_CONTIGUOUS"]