Skip to content

Commit 351f50b

Browse files
Change dpnp.asfarray to run on Iris Xe (#1305)
* Change asfarray func and fix tests for it * Change dtype check in dpnp.asfarray and fix remarks * Small fix * Update copyright --------- Co-authored-by: Anton <[email protected]>
1 parent f85a362 commit 351f50b

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

dpnp/dpnp_iface_manipulation.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# distutils: language = c++
33
# -*- coding: utf-8 -*-
44
# *****************************************************************************
5-
# Copyright (c) 2016-2022, Intel Corporation
5+
# Copyright (c) 2016-2023, Intel Corporation
66
# All rights reserved.
77
#
88
# Redistribution and use in source and binary forms, with or without
@@ -73,7 +73,7 @@
7373
]
7474

7575

76-
def asfarray(x1, dtype=numpy.float64):
76+
def asfarray(x1, dtype=None):
7777
"""
7878
Return an array converted to a float type.
7979
@@ -82,14 +82,15 @@ def asfarray(x1, dtype=numpy.float64):
8282
Notes
8383
-----
8484
This function works exactly the same as :obj:`dpnp.array`.
85+
If dtype is `None`, `bool` or one of the `int` dtypes, it is replaced with
86+
the default floating type in DPNP depending on device capabilities.
8587
8688
"""
8789

8890
x1_desc = dpnp.get_dpnp_descriptor(x1, copy_when_nondefault_queue=False)
8991
if x1_desc:
90-
# behavior of original function: int types replaced with float64
91-
if numpy.issubdtype(dtype, numpy.integer):
92-
dtype = numpy.float64
92+
if dtype is None or not numpy.issubdtype(dtype, numpy.inexact):
93+
dtype = dpnp.default_float_type(sycl_queue=x1.sycl_queue)
9394

9495
# if type is the same then same object should be returned
9596
if x1_desc.dtype == dtype:

tests/test_arraymanipulation.py

+14-16
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
import pytest
2+
from .helper import get_all_dtypes
23

34
import dpnp
45
import numpy
56

67

78
@pytest.mark.usefixtures("allow_fall_back_on_numpy")
8-
@pytest.mark.parametrize("dtype",
9-
[numpy.float64, numpy.float32, numpy.int64, numpy.int32],
10-
ids=["float64", "float32", "int64", "int32"])
11-
@pytest.mark.parametrize("data",
12-
[[1, 2, 3], [1., 2., 3.]],
13-
ids=["[1, 2, 3]", "[1., 2., 3.]"])
9+
@pytest.mark.parametrize("dtype", get_all_dtypes())
10+
@pytest.mark.parametrize(
11+
"data", [[1, 2, 3], [1.0, 2.0, 3.0]], ids=["[1, 2, 3]", "[1., 2., 3.]"]
12+
)
1413
def test_asfarray(dtype, data):
1514
expected = numpy.asfarray(data, dtype)
1615
result = dpnp.asfarray(data, dtype)
1716

1817
numpy.testing.assert_array_equal(result, expected)
1918

2019

21-
@pytest.mark.parametrize("dtype",
22-
[numpy.float64, numpy.float32, numpy.int64, numpy.int32],
23-
ids=["float64", "float32", "int64", "int32"])
24-
@pytest.mark.parametrize("data",
25-
[[1, 2, 3], [1., 2., 3.]],
26-
ids=["[1, 2, 3]", "[1., 2., 3.]"])
27-
def test_asfarray2(dtype, data):
28-
expected = numpy.asfarray(numpy.array(data), dtype)
29-
result = dpnp.asfarray(dpnp.array(data), dtype)
20+
@pytest.mark.parametrize("dtype", get_all_dtypes())
21+
@pytest.mark.parametrize("data", [[1.0, 2.0, 3.0]], ids=["[1., 2., 3.]"])
22+
@pytest.mark.parametrize("data_dtype", get_all_dtypes(no_none=True))
23+
def test_asfarray2(dtype, data, data_dtype):
24+
expected = numpy.asfarray(numpy.array(data, dtype=data_dtype), dtype)
25+
result = dpnp.asfarray(dpnp.array(data, dtype=data_dtype), dtype)
3026

3127
numpy.testing.assert_array_equal(result, expected)
3228

@@ -59,7 +55,9 @@ def test_concatenate(self):
5955
numpy.testing.assert_array_equal(dpnp.concatenate((r4, r3)), r4 + r3)
6056
# Mixed sequence types
6157
numpy.testing.assert_array_equal(dpnp.concatenate((tuple(r4), r3)), r4 + r3)
62-
numpy.testing.assert_array_equal(dpnp.concatenate((dpnp.array(r4), r3)), r4 + r3)
58+
numpy.testing.assert_array_equal(
59+
dpnp.concatenate((dpnp.array(r4), r3)), r4 + r3
60+
)
6361
# Explicit axis specification
6462
numpy.testing.assert_array_equal(dpnp.concatenate((r4, r3), 0), r4 + r3)
6563
# Including negative

0 commit comments

Comments
 (0)