Skip to content

Update test_statistic.py to run on Iris Xe #1494

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 7 commits into from
Jul 24, 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
16 changes: 8 additions & 8 deletions dpnp/backend/kernels/dpnp_krnl_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1167,17 +1167,17 @@ void func_map_init_linalg(func_map_t &fmap)
eft_DBL, (void *)dpnp_eig_default_c<double, double>};

fmap[DPNPFuncName::DPNP_FN_EIG_EXT][eft_INT][eft_INT] = {
get_default_floating_type<>(),
get_default_floating_type(),
(void *)dpnp_eig_ext_c<
int32_t, func_type_map_t::find_type<get_default_floating_type<>()>>,
int32_t, func_type_map_t::find_type<get_default_floating_type()>>,
get_default_floating_type<std::false_type>(),
(void *)dpnp_eig_ext_c<
int32_t, func_type_map_t::find_type<
get_default_floating_type<std::false_type>()>>};
fmap[DPNPFuncName::DPNP_FN_EIG_EXT][eft_LNG][eft_LNG] = {
get_default_floating_type<>(),
get_default_floating_type(),
(void *)dpnp_eig_ext_c<
int64_t, func_type_map_t::find_type<get_default_floating_type<>()>>,
int64_t, func_type_map_t::find_type<get_default_floating_type()>>,
get_default_floating_type<std::false_type>(),
(void *)dpnp_eig_ext_c<
int64_t, func_type_map_t::find_type<
Expand All @@ -1197,17 +1197,17 @@ void func_map_init_linalg(func_map_t &fmap)
eft_DBL, (void *)dpnp_eigvals_default_c<double, double>};

fmap[DPNPFuncName::DPNP_FN_EIGVALS_EXT][eft_INT][eft_INT] = {
get_default_floating_type<>(),
get_default_floating_type(),
(void *)dpnp_eigvals_ext_c<
int32_t, func_type_map_t::find_type<get_default_floating_type<>()>>,
int32_t, func_type_map_t::find_type<get_default_floating_type()>>,
get_default_floating_type<std::false_type>(),
(void *)dpnp_eigvals_ext_c<
int32_t, func_type_map_t::find_type<
get_default_floating_type<std::false_type>()>>};
fmap[DPNPFuncName::DPNP_FN_EIGVALS_EXT][eft_LNG][eft_LNG] = {
get_default_floating_type<>(),
get_default_floating_type(),
(void *)dpnp_eigvals_ext_c<
int64_t, func_type_map_t::find_type<get_default_floating_type<>()>>,
int64_t, func_type_map_t::find_type<get_default_floating_type()>>,
get_default_floating_type<std::false_type>(),
(void *)dpnp_eigvals_ext_c<
int64_t, func_type_map_t::find_type<
Expand Down
28 changes: 14 additions & 14 deletions dpnp/backend/kernels/dpnp_krnl_linalg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -879,17 +879,17 @@ void func_map_init_linalg_func(func_map_t &fmap)
eft_DBL, (void *)dpnp_inv_default_c<double, double>};

fmap[DPNPFuncName::DPNP_FN_INV_EXT][eft_INT][eft_INT] = {
get_default_floating_type<>(),
get_default_floating_type(),
(void *)dpnp_inv_ext_c<
int32_t, func_type_map_t::find_type<get_default_floating_type<>()>>,
int32_t, func_type_map_t::find_type<get_default_floating_type()>>,
get_default_floating_type<std::false_type>(),
(void *)dpnp_inv_ext_c<
int32_t, func_type_map_t::find_type<
get_default_floating_type<std::false_type>()>>};
fmap[DPNPFuncName::DPNP_FN_INV_EXT][eft_LNG][eft_LNG] = {
get_default_floating_type<>(),
get_default_floating_type(),
(void *)dpnp_inv_ext_c<
int64_t, func_type_map_t::find_type<get_default_floating_type<>()>>,
int64_t, func_type_map_t::find_type<get_default_floating_type()>>,
get_default_floating_type<std::false_type>(),
(void *)dpnp_inv_ext_c<
int64_t, func_type_map_t::find_type<
Expand Down Expand Up @@ -1051,17 +1051,17 @@ void func_map_init_linalg_func(func_map_t &fmap)
// eft_C128, (void*)dpnp_qr_c<std::complex<double>, std::complex<double>>};

fmap[DPNPFuncName::DPNP_FN_QR_EXT][eft_INT][eft_INT] = {
get_default_floating_type<>(),
get_default_floating_type(),
(void *)dpnp_qr_ext_c<
int32_t, func_type_map_t::find_type<get_default_floating_type<>()>>,
int32_t, func_type_map_t::find_type<get_default_floating_type()>>,
get_default_floating_type<std::false_type>(),
(void *)dpnp_qr_ext_c<
int32_t, func_type_map_t::find_type<
get_default_floating_type<std::false_type>()>>};
fmap[DPNPFuncName::DPNP_FN_QR_EXT][eft_LNG][eft_LNG] = {
get_default_floating_type<>(),
get_default_floating_type(),
(void *)dpnp_qr_ext_c<
int64_t, func_type_map_t::find_type<get_default_floating_type<>()>>,
int64_t, func_type_map_t::find_type<get_default_floating_type()>>,
get_default_floating_type<std::false_type>(),
(void *)dpnp_qr_ext_c<
int64_t, func_type_map_t::find_type<
Expand All @@ -1086,10 +1086,10 @@ void func_map_init_linalg_func(func_map_t &fmap)
std::complex<double>, double>};

fmap[DPNPFuncName::DPNP_FN_SVD_EXT][eft_INT][eft_INT] = {
get_default_floating_type<>(),
get_default_floating_type(),
(void *)dpnp_svd_ext_c<
int32_t, func_type_map_t::find_type<get_default_floating_type<>()>,
func_type_map_t::find_type<get_default_floating_type<>()>>,
int32_t, func_type_map_t::find_type<get_default_floating_type()>,
func_type_map_t::find_type<get_default_floating_type()>>,
get_default_floating_type<std::false_type>(),
(void *)
dpnp_svd_ext_c<int32_t,
Expand All @@ -1098,10 +1098,10 @@ void func_map_init_linalg_func(func_map_t &fmap)
func_type_map_t::find_type<
get_default_floating_type<std::false_type>()>>};
fmap[DPNPFuncName::DPNP_FN_SVD_EXT][eft_LNG][eft_LNG] = {
get_default_floating_type<>(),
get_default_floating_type(),
(void *)dpnp_svd_ext_c<
int64_t, func_type_map_t::find_type<get_default_floating_type<>()>,
func_type_map_t::find_type<get_default_floating_type<>()>>,
int64_t, func_type_map_t::find_type<get_default_floating_type()>,
func_type_map_t::find_type<get_default_floating_type()>>,
get_default_floating_type<std::false_type>(),
(void *)
dpnp_svd_ext_c<int64_t,
Expand Down
16 changes: 14 additions & 2 deletions dpnp/backend/kernels/dpnp_krnl_statistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1330,9 +1330,21 @@ void func_map_init_statistics(func_map_t &fmap)
eft_DBL, (void *)dpnp_median_default_c<double, double>};

fmap[DPNPFuncName::DPNP_FN_MEDIAN_EXT][eft_INT][eft_INT] = {
eft_DBL, (void *)dpnp_median_ext_c<int32_t, double>};
get_default_floating_type(),
(void *)dpnp_median_ext_c<
int32_t, func_type_map_t::find_type<get_default_floating_type()>>,
get_default_floating_type<std::false_type>(),
(void *)dpnp_median_ext_c<
int32_t, func_type_map_t::find_type<
get_default_floating_type<std::false_type>()>>};
fmap[DPNPFuncName::DPNP_FN_MEDIAN_EXT][eft_LNG][eft_LNG] = {
eft_DBL, (void *)dpnp_median_ext_c<int64_t, double>};
get_default_floating_type(),
(void *)dpnp_median_ext_c<
int64_t, func_type_map_t::find_type<get_default_floating_type()>>,
get_default_floating_type<std::false_type>(),
(void *)dpnp_median_ext_c<
int64_t, func_type_map_t::find_type<
get_default_floating_type<std::false_type>()>>};
fmap[DPNPFuncName::DPNP_FN_MEDIAN_EXT][eft_FLT][eft_FLT] = {
eft_FLT, (void *)dpnp_median_ext_c<float, float>};
fmap[DPNPFuncName::DPNP_FN_MEDIAN_EXT][eft_DBL][eft_DBL] = {
Expand Down
8 changes: 5 additions & 3 deletions dpnp/dpnp_algo/dpnp_algo_statistics.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,12 @@ cpdef utils.dpnp_descriptor dpnp_median(utils.dpnp_descriptor array1):

array1_obj = array1.get_array()

cdef (DPNPFuncType, void *) ret_type_and_func = utils.get_ret_type_and_func(array1_obj, kernel_data)
cdef DPNPFuncType return_type = ret_type_and_func[0]
cdef custom_statistic_1in_1out_func_ptr_t func = < custom_statistic_1in_1out_func_ptr_t > ret_type_and_func[1]

cdef utils.dpnp_descriptor result = utils.create_output_descriptor((1,),
kernel_data.return_type,
return_type,
None,
device=array1_obj.sycl_device,
usm_type=array1_obj.usm_type,
Expand All @@ -277,8 +281,6 @@ cpdef utils.dpnp_descriptor dpnp_median(utils.dpnp_descriptor array1):
cdef c_dpctl.SyclQueue q = <c_dpctl.SyclQueue> result_sycl_queue
cdef c_dpctl.DPCTLSyclQueueRef q_ref = q.get_queue_ref()

cdef custom_statistic_1in_1out_func_ptr_t func = <custom_statistic_1in_1out_func_ptr_t > kernel_data.ptr

# stub for interface support
cdef shape_type_c axis
cdef Py_ssize_t axis_size = 0
Expand Down
8 changes: 7 additions & 1 deletion dpnp/dpnp_utils/dpnp_algo_utils.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from libcpp cimport bool as cpp_bool

from dpnp.dpnp_algo cimport shape_type_c
from dpnp.dpnp_algo.dpnp_algo cimport DPNPFuncName, DPNPFuncType
from dpnp.dpnp_algo.dpnp_algo cimport DPNPFuncData, DPNPFuncName, DPNPFuncType


cpdef checker_throw_runtime_error(function_name, message)
Expand Down Expand Up @@ -162,3 +162,9 @@ cdef tuple get_common_usm_allocation(dpnp_descriptor x1, dpnp_descriptor x2)
"""
Get common USM allocation in the form of (sycl_device, usm_type, sycl_queue)
"""

cdef (DPNPFuncType, void *) get_ret_type_and_func(x1_obj, DPNPFuncData kernel_data)
"""
Get the corresponding return type and function pointer based on the
capability of the allocated input array device for the integer types.
"""
10 changes: 10 additions & 0 deletions dpnp/dpnp_utils/dpnp_algo_utils.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,16 @@ cdef tuple get_common_usm_allocation(dpnp_descriptor x1, dpnp_descriptor x2):
return (common_sycl_queue.sycl_device, common_usm_type, common_sycl_queue)


cdef (DPNPFuncType, void *) get_ret_type_and_func(x1_obj, DPNPFuncData kernel_data):
if dpnp.issubdtype(x1_obj.dtype, dpnp.integer) and not x1_obj.sycl_device.has_aspect_fp64:
return_type = kernel_data.return_type_no_fp64
func = kernel_data.ptr_no_fp64
else:
return_type = kernel_data.return_type
func = kernel_data.ptr
return return_type, func


cdef class dpnp_descriptor:
def __init__(self, obj, dpnp_descriptor orig_desc=None):
""" Initialze variables """
Expand Down
8 changes: 7 additions & 1 deletion dpnp/dpnp_utils/dpnp_utils_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def _get_2dmin_array(x, dtype):
It casts to another dtype, if the input array differs from requested one.

"""

if x.ndim == 0:
x = x.reshape((1, 1))
elif x.ndim == 1:
Expand All @@ -81,6 +80,13 @@ def _get_2dmin_array(x, dtype):
if y is not None:
dtypes.append(y.dtype)
dtype = dpt.result_type(*dtypes)
# TODO: remove when dpctl.result_type() is fixed
fp64 = queue.sycl_device.has_aspect_fp64
if not fp64:
if dtype == dpnp.float64:
dtype = dpnp.float32
elif dtype == dpnp.complex128:
dtype = dpnp.complex64

X = _get_2dmin_array(m, dtype)
if y is not None:
Expand Down
20 changes: 5 additions & 15 deletions dpnp/linalg/dpnp_algo_linalg.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,6 @@ ctypedef c_dpctl.DPCTLSyclEventRef(*custom_linalg_2in_1out_func_ptr_t)(c_dpctl.D
const c_dpctl.DPCTLEventVectorRef)


cdef (DPNPFuncType, void *) get_ret_type_and_func(x1_obj, DPNPFuncData kernel_data):
if dpnp.issubdtype(x1_obj.dtype, dpnp.integer) and not x1_obj.sycl_device.has_aspect_fp64:
return_type = kernel_data.return_type_no_fp64
func = kernel_data.ptr_no_fp64
else:
return_type = kernel_data.return_type
func = kernel_data.ptr
return return_type, func


cpdef utils.dpnp_descriptor dpnp_cholesky(utils.dpnp_descriptor input_):
size_ = input_.shape[-1]

Expand Down Expand Up @@ -206,7 +196,7 @@ cpdef tuple dpnp_eig(utils.dpnp_descriptor x1):

x1_obj = x1.get_array()

cdef (DPNPFuncType, void *) ret_type_and_func = get_ret_type_and_func(x1_obj, kernel_data)
cdef (DPNPFuncType, void *) ret_type_and_func = utils.get_ret_type_and_func(x1_obj, kernel_data)
cdef DPNPFuncType return_type = ret_type_and_func[0]
cdef custom_linalg_2in_1out_func_ptr_t func = < custom_linalg_2in_1out_func_ptr_t > ret_type_and_func[1]

Expand Down Expand Up @@ -252,7 +242,7 @@ cpdef utils.dpnp_descriptor dpnp_eigvals(utils.dpnp_descriptor input):

input_obj = input.get_array()

cdef (DPNPFuncType, void *) ret_type_and_func = get_ret_type_and_func(input_obj, kernel_data)
cdef (DPNPFuncType, void *) ret_type_and_func = utils.get_ret_type_and_func(input_obj, kernel_data)
cdef DPNPFuncType return_type = ret_type_and_func[0]
cdef custom_linalg_1in_1out_with_size_func_ptr_t_ func = < custom_linalg_1in_1out_with_size_func_ptr_t_ > ret_type_and_func[1]

Expand Down Expand Up @@ -291,7 +281,7 @@ cpdef utils.dpnp_descriptor dpnp_inv(utils.dpnp_descriptor input):

input_obj = input.get_array()

cdef (DPNPFuncType, void *) ret_type_and_func = get_ret_type_and_func(input_obj, kernel_data)
cdef (DPNPFuncType, void *) ret_type_and_func = utils.get_ret_type_and_func(input_obj, kernel_data)
cdef DPNPFuncType return_type = ret_type_and_func[0]
cdef custom_linalg_1in_1out_func_ptr_t func = < custom_linalg_1in_1out_func_ptr_t > ret_type_and_func[1]

Expand Down Expand Up @@ -472,7 +462,7 @@ cpdef tuple dpnp_qr(utils.dpnp_descriptor x1, str mode):

x1_obj = x1.get_array()

cdef (DPNPFuncType, void *) ret_type_and_func = get_ret_type_and_func(x1_obj, kernel_data)
cdef (DPNPFuncType, void *) ret_type_and_func = utils.get_ret_type_and_func(x1_obj, kernel_data)
cdef DPNPFuncType return_type = ret_type_and_func[0]
cdef custom_linalg_1in_3out_shape_t func = < custom_linalg_1in_3out_shape_t > ret_type_and_func[1]

Expand Down Expand Up @@ -525,7 +515,7 @@ cpdef tuple dpnp_svd(utils.dpnp_descriptor x1, cpp_bool full_matrices, cpp_bool

x1_obj = x1.get_array()

cdef (DPNPFuncType, void *) ret_type_and_func = get_ret_type_and_func(x1_obj, kernel_data)
cdef (DPNPFuncType, void *) ret_type_and_func = utils.get_ret_type_and_func(x1_obj, kernel_data)
cdef DPNPFuncType return_type = ret_type_and_func[0]
cdef custom_linalg_1in_3out_shape_t func = < custom_linalg_1in_3out_shape_t > ret_type_and_func[1]

Expand Down
46 changes: 24 additions & 22 deletions tests/test_statistics.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
import numpy
import pytest
from numpy.testing import assert_allclose

import dpnp

from .helper import get_all_dtypes


@pytest.mark.parametrize(
"type",
[numpy.float64, numpy.float32, numpy.int64, numpy.int32],
ids=["float64", "float32", "int64", "int32"],
"dtype", get_all_dtypes(no_none=True, no_bool=True, no_complex=True)
)
@pytest.mark.parametrize("size", [2, 4, 8, 16, 3, 9, 27, 81])
def test_median(type, size):
a = numpy.arange(size, dtype=type)
def test_median(dtype, size):
a = numpy.arange(size, dtype=dtype)
ia = dpnp.array(a)

np_res = numpy.median(a)
dpnp_res = dpnp.median(ia)

numpy.testing.assert_allclose(dpnp_res, np_res)
assert_allclose(dpnp_res, np_res)


@pytest.mark.usefixtures("allow_fall_back_on_numpy")
@pytest.mark.parametrize("axis", [0, 1, -1, 2, -2, (1, 2), (0, -2)])
def test_max(axis):
a = numpy.arange(768, dtype=numpy.float64).reshape((4, 4, 6, 8))
@pytest.mark.parametrize(
"dtype", get_all_dtypes(no_none=True, no_bool=True, no_complex=True)
)
def test_max(axis, dtype):
a = numpy.arange(768, dtype=dtype).reshape((4, 4, 6, 8))
ia = dpnp.array(a)

np_res = numpy.max(a, axis=axis)
dpnp_res = dpnp.max(ia, axis=axis)

numpy.testing.assert_allclose(dpnp_res, np_res)
assert_allclose(dpnp_res, np_res)


@pytest.mark.usefixtures("allow_fall_back_on_numpy")
Expand Down Expand Up @@ -70,17 +72,21 @@ def test_max(axis):
"[[np.nan, np.nan], [np.inf, np.nan]]",
],
)
def test_nanvar(array):
a = numpy.array(array)
@pytest.mark.parametrize(
"dtype", get_all_dtypes(no_none=True, no_bool=True, no_complex=True)
)
def test_nanvar(array, dtype):
dtype = dpnp.default_float_type()
a = numpy.array(array, dtype=dtype)
ia = dpnp.array(a)
for ddof in range(a.ndim):
expected = numpy.nanvar(a, ddof=ddof)
result = dpnp.nanvar(ia, ddof=ddof)
numpy.testing.assert_array_equal(expected, result)
assert_allclose(expected, result, rtol=1e-06)

expected = numpy.nanvar(a, axis=None, ddof=0)
result = dpnp.nanvar(ia, axis=None, ddof=0)
numpy.testing.assert_array_equal(expected, result)
assert_allclose(expected, result, rtol=1e-06)


@pytest.mark.usefixtures("allow_fall_back_on_numpy")
Expand All @@ -99,7 +105,7 @@ def test_bincount_minlength(self, array, minlength):

expected = numpy.bincount(np_a, minlength=minlength)
result = dpnp.bincount(dpnp_a, minlength=minlength)
numpy.testing.assert_array_equal(expected, result)
assert_allclose(expected, result)

@pytest.mark.parametrize(
"array", [[1, 2, 2, 1, 2, 4]], ids=["[1, 2, 2, 1, 2, 4]"]
Expand All @@ -115,7 +121,7 @@ def test_bincount_weights(self, array, weights):

expected = numpy.bincount(np_a, weights=weights)
result = dpnp.bincount(dpnp_a, weights=weights)
numpy.testing.assert_array_equal(expected, result)
assert_allclose(expected, result)


@pytest.mark.parametrize(
Expand All @@ -124,10 +130,8 @@ def test_bincount_weights(self, array, weights):
def test_cov_rowvar(dtype):
a = dpnp.array([[0, 2], [1, 1], [2, 0]], dtype=dtype)
b = numpy.array([[0, 2], [1, 1], [2, 0]], dtype=dtype)
numpy.testing.assert_array_equal(dpnp.cov(a.T), dpnp.cov(a, rowvar=False))
numpy.testing.assert_array_equal(
numpy.cov(b, rowvar=False), dpnp.cov(a, rowvar=False)
)
assert_allclose(dpnp.cov(a.T), dpnp.cov(a, rowvar=False))
assert_allclose(numpy.cov(b, rowvar=False), dpnp.cov(a, rowvar=False))


@pytest.mark.parametrize(
Expand All @@ -136,6 +140,4 @@ def test_cov_rowvar(dtype):
def test_cov_1D_rowvar(dtype):
a = dpnp.array([[0, 1, 2]], dtype=dtype)
b = numpy.array([[0, 1, 2]], dtype=dtype)
numpy.testing.assert_array_equal(
numpy.cov(b, rowvar=False), dpnp.cov(a, rowvar=False)
)
assert_allclose(numpy.cov(b, rowvar=False), dpnp.cov(a, rowvar=False))