Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from 5 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: 14 additions & 2 deletions 16 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<>(),
vlad-perevezentsev marked this conversation as resolved.
Outdated
Show resolved Hide resolved
(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 8 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 8 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 10 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
9 changes: 9 additions & 0 deletions 9 dpnp/dpnp_utils/dpnp_utils_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ def _get_2dmin_array(x, dtype):
It casts to another dtype, if the input array differs from requested one.

"""
# dtype map for devices without double precision type support
dtype_map = {
dpnp.float64: dpnp.float32,
dpnp.complex128: dpnp.complex64,
}
vlad-perevezentsev marked this conversation as resolved.
Outdated
Show resolved Hide resolved

if x.ndim == 0:
x = x.reshape((1, 1))
Expand All @@ -69,6 +74,10 @@ def _get_2dmin_array(x, dtype):
x = x.T

if x.dtype != dtype:
fp64 = x.sycl_device.has_aspect_fp64
# TODO: remove when dpctl.result_type() is fixed
if not fp64:
dtype = dtype_map[dtype.type]
vlad-perevezentsev marked this conversation as resolved.
Outdated
Show resolved Hide resolved
x = dpnp.astype(x, dtype)
return x

Expand Down
20 changes: 5 additions & 15 deletions 20 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
27 changes: 14 additions & 13 deletions 27 tests/test_statistics.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
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))
dtype = dpnp.default_float_type()
vlad-perevezentsev marked this conversation as resolved.
Outdated
Show resolved Hide resolved
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 @@ -71,16 +71,17 @@ def test_max(axis):
],
)
def test_nanvar(array):
a = numpy.array(array)
dtype = dpnp.default_float_type()
vlad-perevezentsev marked this conversation as resolved.
Show resolved Hide resolved
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 +100,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 +116,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 Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.