Skip to content

Change dpnp.asfarray to run on Iris Xe #1305

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

Merged
merged 5 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions dpnp/dpnp_iface_manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# distutils: language = c++
# -*- coding: utf-8 -*-
# *****************************************************************************
# Copyright (c) 2016-2022, Intel Corporation
# Copyright (c) 2016-2023, Intel Corporation
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -73,7 +73,7 @@
]


def asfarray(x1, dtype=numpy.float64):
def asfarray(x1, dtype=None):
"""
Return an array converted to a float type.

Expand All @@ -82,14 +82,15 @@ def asfarray(x1, dtype=numpy.float64):
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.

"""

x1_desc = dpnp.get_dpnp_descriptor(x1, copy_when_nondefault_queue=False)
if x1_desc:
# behavior of original function: int types replaced with float64
if numpy.issubdtype(dtype, numpy.integer):
dtype = numpy.float64
if dtype is None or not numpy.issubdtype(dtype, numpy.inexact):
dtype = dpnp.default_float_type(sycl_queue=x1.sycl_queue)

# if type is the same then same object should be returned
if x1_desc.dtype == dtype:
Expand Down
30 changes: 14 additions & 16 deletions tests/test_arraymanipulation.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
import pytest
from .helper import get_all_dtypes

import dpnp
import numpy


@pytest.mark.usefixtures("allow_fall_back_on_numpy")
@pytest.mark.parametrize("dtype",
[numpy.float64, numpy.float32, numpy.int64, numpy.int32],
ids=["float64", "float32", "int64", "int32"])
@pytest.mark.parametrize("data",
[[1, 2, 3], [1., 2., 3.]],
ids=["[1, 2, 3]", "[1., 2., 3.]"])
@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.]"]
)
def test_asfarray(dtype, data):
expected = numpy.asfarray(data, dtype)
result = dpnp.asfarray(data, dtype)

numpy.testing.assert_array_equal(result, expected)


@pytest.mark.parametrize("dtype",
[numpy.float64, numpy.float32, numpy.int64, numpy.int32],
ids=["float64", "float32", "int64", "int32"])
@pytest.mark.parametrize("data",
[[1, 2, 3], [1., 2., 3.]],
ids=["[1, 2, 3]", "[1., 2., 3.]"])
def test_asfarray2(dtype, data):
expected = numpy.asfarray(numpy.array(data), dtype)
result = dpnp.asfarray(dpnp.array(data), dtype)
@pytest.mark.parametrize("dtype", get_all_dtypes())
@pytest.mark.parametrize("data", [[1.0, 2.0, 3.0]], ids=["[1., 2., 3.]"])
@pytest.mark.parametrize("data_dtype", get_all_dtypes(no_none=True))
def test_asfarray2(dtype, data, data_dtype):
expected = numpy.asfarray(numpy.array(data, dtype=data_dtype), dtype)
result = dpnp.asfarray(dpnp.array(data, dtype=data_dtype), dtype)

numpy.testing.assert_array_equal(result, expected)

Expand Down Expand Up @@ -59,7 +55,9 @@ def test_concatenate(self):
numpy.testing.assert_array_equal(dpnp.concatenate((r4, r3)), r4 + r3)
# Mixed sequence types
numpy.testing.assert_array_equal(dpnp.concatenate((tuple(r4), r3)), r4 + r3)
numpy.testing.assert_array_equal(dpnp.concatenate((dpnp.array(r4), r3)), r4 + r3)
numpy.testing.assert_array_equal(
dpnp.concatenate((dpnp.array(r4), r3)), r4 + r3
)
# Explicit axis specification
numpy.testing.assert_array_equal(dpnp.concatenate((r4, r3), 0), r4 + r3)
# Including negative
Expand Down