Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of fallback on numpy in dpnp.asfarray #1542

Merged
merged 9 commits into from
Sep 6, 2023
42 changes: 27 additions & 15 deletions dpnp/dpnp_iface_manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,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__ = [
Expand Down Expand Up @@ -78,32 +77,45 @@
]


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.

For full documentation refer to :obj:`numpy.asfarray`.

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='float')
npolina4 marked this conversation as resolved.
Show resolved Hide resolved
array([2., 3.])
>>> np.asfarray([2, 3], dtype='int8')
array([2., 3.])

npolina4 marked this conversation as resolved.
Show resolved Hide resolved
"""
npolina4 marked this conversation as resolved.
Show resolved Hide resolved
npolina4 marked this conversation as resolved.
Show resolved Hide resolved

# 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.array(
a, dtype=dtype, copy=False, usm_type=usm_type, sycl_queue=_sycl_queue
)
npolina4 marked this conversation as resolved.
Show resolved Hide resolved


def atleast_1d(*arys):
Expand Down
9 changes: 9 additions & 0 deletions tests/skipped_tests.tbl
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,15 @@ tests/third_party/cupy/manipulation_tests/test_shape.py::TestRavel::test_ravel
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshape::test_reshape_zerosize
tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshape::test_reshape_zerosize2

tests/third_party/cupy/manipulation_tests/test_kind.py::TestKind::test_asarray_chkfinite
tests/third_party/cupy/manipulation_tests/test_kind.py::TestKind::test_asarray_chkfinite_non_finite_vals
tests/third_party/cupy/manipulation_tests/test_kind.py::TestKind::test_require_C_and_F_flags
npolina4 marked this conversation as resolved.
Show resolved Hide resolved
tests/third_party/cupy/manipulation_tests/test_kind.py::TestKind::test_require_empty_requirements
tests/third_party/cupy/manipulation_tests/test_kind.py::TestKind::test_require_flag_check
tests/third_party/cupy/manipulation_tests/test_kind.py::TestKind::test_require_incorrect_dtype
tests/third_party/cupy/manipulation_tests/test_kind.py::TestKind::test_require_incorrect_requirments
tests/third_party/cupy/manipulation_tests/test_kind.py::TestKind::test_require_owndata

tests/third_party/cupy/manipulation_tests/test_tiling.py::TestRepeatRepeatsNdarray::test_func
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestRepeatRepeatsNdarray::test_method
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTileFailure_param_0_{reps=-1}::test_tile_failure
Expand Down
9 changes: 9 additions & 0 deletions tests/skipped_tests_gpu.tbl
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,15 @@ tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_3_{reps
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_4_{reps=(2, 3)}::test_array_tile
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_5_{reps=(2, 3, 4, 5)}::test_array_tile

tests/third_party/cupy/manipulation_tests/test_kind.py::TestKind::test_asarray_chkfinite
tests/third_party/cupy/manipulation_tests/test_kind.py::TestKind::test_asarray_chkfinite_non_finite_vals
tests/third_party/cupy/manipulation_tests/test_kind.py::TestKind::test_require_C_and_F_flags
tests/third_party/cupy/manipulation_tests/test_kind.py::TestKind::test_require_empty_requirements
tests/third_party/cupy/manipulation_tests/test_kind.py::TestKind::test_require_flag_check
tests/third_party/cupy/manipulation_tests/test_kind.py::TestKind::test_require_incorrect_dtype
tests/third_party/cupy/manipulation_tests/test_kind.py::TestKind::test_require_incorrect_requirments
tests/third_party/cupy/manipulation_tests/test_kind.py::TestKind::test_require_owndata

tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticRaisesWithNumpyInput_param_3_{name='angle', nargs=1}::test_raises_with_numpy_input

tests/third_party/cupy/math_tests/test_explog.py::TestExplog::test_logaddexp
Expand Down
1 change: 0 additions & 1 deletion tests/test_arraymanipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.]"]
Expand Down
9 changes: 8 additions & 1 deletion tests/test_sycl_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
9 changes: 8 additions & 1 deletion tests/test_usm_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
126 changes: 126 additions & 0 deletions tests/third_party/cupy/manipulation_tests/test_kind.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import unittest

import numpy
import pytest

import dpnp as cupy
from tests.third_party.cupy import testing


class TestKind(unittest.TestCase):
@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)

@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)
assert a_cpu.dtype == a_gpu.dtype
npolina4 marked this conversation as resolved.
Show resolved Hide resolved

@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)

@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

@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"]

@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"])

@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"])

@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")

@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"]