Skip to content
Merged
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='float16')
array([2., 3.], dtype=float16)
>>> np.asfarray([2, 3], dtype='int32')
Comment thread
npolina4 marked this conversation as resolved.
Outdated
array([2., 3.])

Comment thread
npolina4 marked this conversation as resolved.
"""
Comment thread
npolina4 marked this conversation as resolved.
Comment thread
npolina4 marked this conversation as 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.asarray(
a, dtype=dtype, usm_type=usm_type, sycl_queue=_sycl_queue
)
Comment thread
npolina4 marked this conversation as resolved.


def atleast_1d(*arys):
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
134 changes: 134 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,134 @@
import unittest

import numpy
import pytest

import dpnp as cupy
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)
assert a_cpu.dtype == a_gpu.dtype
Comment thread
npolina4 marked this conversation as resolved.
Outdated

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