Skip to content

Commit 7cb3949

Browse files
npolina4antonwolfy
andauthored
Get rid of fallback on numpy in dpnp.asfarray (#1542)
* SAT-6118 Get rid of fallback on numpy in dpnp.asfarray * Added nore test for dpnp.asfarray() function. * Fixed example for asfarray function. * Update dpnp/dpnp_iface_manipulation.py Co-authored-by: Anton <[email protected]> * Update tests/third_party/cupy/manipulation_tests/test_kind.py Co-authored-by: Anton <[email protected]> * Update test_kind.py --------- Co-authored-by: Anton <[email protected]>
1 parent 89fd914 commit 7cb3949

File tree

5 files changed

+186
-18
lines changed

5 files changed

+186
-18
lines changed

dpnp/dpnp_iface_manipulation.py

+27-15
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import dpnp
4848
from dpnp.dpnp_algo import *
4949
from dpnp.dpnp_array import dpnp_array
50-
from dpnp.dpnp_iface_arraycreation import array
5150
from dpnp.dpnp_utils import *
5251

5352
__all__ = [
@@ -80,32 +79,45 @@
8079
]
8180

8281

83-
def asfarray(a, dtype=None):
82+
def asfarray(a, dtype=None, *, device=None, usm_type=None, sycl_queue=None):
8483
"""
8584
Return an array converted to a float type.
8685
8786
For full documentation refer to :obj:`numpy.asfarray`.
8887
8988
Notes
9089
-----
91-
This function works exactly the same as :obj:`dpnp.array`.
92-
If dtype is `None`, `bool` or one of the `int` dtypes, it is replaced with
93-
the default floating type in DPNP depending on device capabilities.
90+
If `dtype` is ``None``, :obj:`dpnp.bool` or one of the `int` dtypes,
91+
it is replaced with the default floating type (:obj:`dpnp.float64`
92+
if a device supports it, or :obj:`dpnp.float32` type otherwise).
9493
95-
"""
94+
Returns
95+
-------
96+
y : dpnp.ndarray
97+
The input a as a float ndarray.
9698
97-
a_desc = dpnp.get_dpnp_descriptor(a, copy_when_nondefault_queue=False)
98-
if a_desc:
99-
if dtype is None or not numpy.issubdtype(dtype, dpnp.inexact):
100-
dtype = dpnp.default_float_type(sycl_queue=a.sycl_queue)
99+
Examples
100+
--------
101+
>>> import dpnp as np
102+
>>> np.asfarray([2, 3])
103+
array([2., 3.])
104+
>>> np.asfarray([2, 3], dtype=dpnp.float32)
105+
array([2., 3.], dtype=float32)
106+
>>> np.asfarray([2, 3], dtype=dpnp.int32)
107+
array([2., 3.])
108+
109+
"""
101110

102-
# if type is the same then same object should be returned
103-
if a_desc.dtype == dtype:
104-
return a
111+
_sycl_queue = dpnp.get_normalized_queue_device(
112+
a, sycl_queue=sycl_queue, device=device
113+
)
105114

106-
return array(a, dtype=dtype)
115+
if dtype is None or not numpy.issubdtype(dtype, dpnp.inexact):
116+
dtype = dpnp.default_float_type(sycl_queue=_sycl_queue)
107117

108-
return call_origin(numpy.asfarray, a, dtype)
118+
return dpnp.asarray(
119+
a, dtype=dtype, usm_type=usm_type, sycl_queue=_sycl_queue
120+
)
109121

110122

111123
def atleast_1d(*arys):

tests/test_arraymanipulation.py

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from .helper import get_all_dtypes, get_float_complex_dtypes
1414

1515

16-
@pytest.mark.usefixtures("allow_fall_back_on_numpy")
1716
@pytest.mark.parametrize("dtype", get_all_dtypes())
1817
@pytest.mark.parametrize(
1918
"data", [[1, 2, 3], [1.0, 2.0, 3.0]], ids=["[1, 2, 3]", "[1., 2., 3.]"]

tests/test_sycl_queue.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,14 @@ def test_to_device(device_from, device_to):
10201020
)
10211021
@pytest.mark.parametrize(
10221022
"func",
1023-
["array", "asarray", "asanyarray", "ascontiguousarray", "asfortranarray"],
1023+
[
1024+
"array",
1025+
"asarray",
1026+
"asanyarray",
1027+
"ascontiguousarray",
1028+
"asfarray",
1029+
"asfortranarray",
1030+
],
10241031
)
10251032
@pytest.mark.parametrize(
10261033
"device_param", ["", "None", "sycl_device"], ids=["Empty", "None", "device"]

tests/test_usm_type.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,14 @@ def test_array_creation(func, args, usm_type_x, usm_type_y):
159159

160160
@pytest.mark.parametrize(
161161
"func",
162-
["array", "asarray", "asanyarray", "ascontiguousarray", "asfortranarray"],
162+
[
163+
"array",
164+
"asarray",
165+
"asanyarray",
166+
"ascontiguousarray",
167+
"asfarray",
168+
"asfortranarray",
169+
],
163170
)
164171
@pytest.mark.parametrize("usm_type_x", list_of_usm_types, ids=list_of_usm_types)
165172
@pytest.mark.parametrize("usm_type_y", list_of_usm_types, ids=list_of_usm_types)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import unittest
2+
3+
import numpy
4+
import pytest
5+
6+
import dpnp as cupy
7+
from tests.helper import has_support_aspect64
8+
from tests.third_party.cupy import testing
9+
10+
11+
class TestKind(unittest.TestCase):
12+
@pytest.mark.skip("dpnp.asarray_chkfinite() is not implemented yet")
13+
@testing.for_orders("CFAK")
14+
@testing.for_all_dtypes()
15+
@testing.numpy_cupy_array_equal()
16+
def test_asarray_chkfinite(self, xp, dtype, order):
17+
a = [0, 4, 0, 5]
18+
return xp.asarray_chkfinite(a, dtype=dtype, order=order)
19+
20+
@pytest.mark.skip("dpnp.asarray_chkfinite() is not implemented yet")
21+
@testing.for_orders("CFAK")
22+
@testing.for_all_dtypes(no_bool=True)
23+
def test_asarray_chkfinite_non_finite_vals(self, dtype, order):
24+
a = [-numpy.inf, 0.0, numpy.inf, numpy.nan]
25+
for xp in (numpy, cupy):
26+
if xp.issubdtype(dtype, xp.integer):
27+
error = OverflowError
28+
else:
29+
error = ValueError
30+
with pytest.raises(error):
31+
xp.asarray_chkfinite(a, dtype=dtype, order=order)
32+
33+
@testing.for_all_dtypes()
34+
def test_asfarray(self, dtype):
35+
a = cupy.asarray([1, 2, 3])
36+
a_gpu = cupy.asfarray(a, dtype)
37+
a_cpu = numpy.asfarray(a, dtype)
38+
if (
39+
has_support_aspect64()
40+
or cupy.issubdtype(dtype, cupy.complexfloating)
41+
or cupy.issubdtype(dtype, cupy.floating)
42+
):
43+
assert a_cpu.dtype == a_gpu.dtype
44+
else:
45+
assert a_cpu.dtype == cupy.float64
46+
assert a_gpu.dtype == cupy.float32
47+
48+
@testing.for_all_dtypes()
49+
def test_asfortranarray1(self, dtype):
50+
def func(xp):
51+
x = xp.zeros((2, 3), dtype=dtype)
52+
ret = xp.asfortranarray(x)
53+
assert x.flags.c_contiguous
54+
assert ret.flags.f_contiguous
55+
56+
assert func(numpy) == func(cupy)
57+
58+
@testing.for_all_dtypes()
59+
def test_asfortranarray2(self, dtype):
60+
def func(xp):
61+
x = xp.zeros((2, 3, 4), dtype=dtype)
62+
ret = xp.asfortranarray(x)
63+
assert x.flags.c_contiguous
64+
assert ret.flags.f_contiguous
65+
66+
assert func(numpy) == func(cupy)
67+
68+
@testing.for_all_dtypes()
69+
def test_asfortranarray3(self, dtype):
70+
def func(xp):
71+
x = xp.zeros((2, 3, 4), dtype=dtype)
72+
ret = xp.asfortranarray(xp.asfortranarray(x))
73+
assert x.flags.c_contiguous
74+
assert ret.flags.f_contiguous
75+
76+
assert func(numpy) == func(cupy)
77+
78+
@testing.for_all_dtypes()
79+
def test_asfortranarray4(self, dtype):
80+
def func(xp):
81+
x = xp.zeros((2, 3), dtype=dtype)
82+
x = xp.transpose(x, (1, 0))
83+
ret = xp.asfortranarray(x)
84+
assert ret.flags.f_contiguous
85+
86+
assert func(numpy) == func(cupy)
87+
88+
@testing.for_all_dtypes()
89+
def test_asfortranarray5(self, dtype):
90+
def func(xp):
91+
x = testing.shaped_arange((2, 3), xp, dtype)
92+
ret = xp.asfortranarray(x)
93+
assert x.flags.c_contiguous
94+
assert ret.flags.f_contiguous
95+
96+
assert func(numpy) == func(cupy)
97+
98+
@pytest.mark.skip("dpnp.require() is not implemented yet")
99+
@testing.for_all_dtypes()
100+
def test_require_flag_check(self, dtype):
101+
possible_flags = [["C_CONTIGUOUS"], ["F_CONTIGUOUS"]]
102+
x = cupy.zeros((2, 3, 4), dtype=dtype)
103+
for flags in possible_flags:
104+
arr = cupy.require(x, dtype, flags)
105+
for parameter in flags:
106+
assert arr.flags[parameter]
107+
assert arr.dtype == dtype
108+
109+
@pytest.mark.skip("dpnp.require() is not implemented yet")
110+
@testing.for_all_dtypes()
111+
def test_require_owndata(self, dtype):
112+
x = cupy.zeros((2, 3, 4), dtype=dtype)
113+
arr = x.view()
114+
arr = cupy.require(arr, dtype, ["O"])
115+
assert arr.flags["OWNDATA"]
116+
117+
@pytest.mark.skip("dpnp.require() is not implemented yet")
118+
@testing.for_all_dtypes()
119+
def test_require_C_and_F_flags(self, dtype):
120+
x = cupy.zeros((2, 3, 4), dtype=dtype)
121+
with pytest.raises(ValueError):
122+
cupy.require(x, dtype, ["C", "F"])
123+
124+
@pytest.mark.skip("dpnp.require() is not implemented yet")
125+
@testing.for_all_dtypes()
126+
def test_require_incorrect_requirments(self, dtype):
127+
x = cupy.zeros((2, 3, 4), dtype=dtype)
128+
with pytest.raises(ValueError):
129+
cupy.require(x, dtype, ["W"])
130+
131+
@pytest.mark.skip("dpnp.require() is not implemented yet")
132+
@testing.for_all_dtypes()
133+
def test_require_incorrect_dtype(self, dtype):
134+
x = cupy.zeros((2, 3, 4), dtype=dtype)
135+
with pytest.raises(ValueError):
136+
cupy.require(x, "random", "C")
137+
138+
@pytest.mark.skip("dpnp.require() is not implemented yet")
139+
@testing.for_all_dtypes()
140+
def test_require_empty_requirements(self, dtype):
141+
x = cupy.zeros((2, 3, 4), dtype=dtype)
142+
x = cupy.require(x, dtype, [])
143+
assert x.flags["C_CONTIGUOUS"]

0 commit comments

Comments
 (0)