From a424e41ae48e2f5cddce74a0b7cb2cd41e48d6eb Mon Sep 17 00:00:00 2001 From: Alexander Kalistratov Date: Thu, 28 Nov 2024 14:29:34 +0100 Subject: [PATCH] Applying review comments --- .../extensions/statistics/bincount.hpp | 3 +- dpnp/backend/extensions/statistics/common.hpp | 14 +++--- .../extensions/statistics/histogram.hpp | 2 + .../extensions/statistics/histogramdd.cpp | 4 +- .../extensions/statistics/histogramdd.hpp | 4 +- dpnp/dpnp_iface_histograms.py | 50 ++++++------------- dpnp/tests/test_histogram.py | 7 ++- 7 files changed, 34 insertions(+), 50 deletions(-) diff --git a/dpnp/backend/extensions/statistics/bincount.hpp b/dpnp/backend/extensions/statistics/bincount.hpp index 70a17431383..0f3b4fe8009 100644 --- a/dpnp/backend/extensions/statistics/bincount.hpp +++ b/dpnp/backend/extensions/statistics/bincount.hpp @@ -25,10 +25,11 @@ #pragma once -#include +#include #include #include "dispatch_table.hpp" +#include "dpctl4pybind11.hpp" namespace dpctl_td_ns = dpctl::tensor::type_dispatch; diff --git a/dpnp/backend/extensions/statistics/common.hpp b/dpnp/backend/extensions/statistics/common.hpp index 2ea2983f87e..5545893fe67 100644 --- a/dpnp/backend/extensions/statistics/common.hpp +++ b/dpnp/backend/extensions/statistics/common.hpp @@ -115,18 +115,16 @@ struct IsNan static bool isnan(const T &v) { if constexpr (type_utils::is_complex_v) { - const auto real1 = std::real(v); - const auto imag1 = std::imag(v); - using vT = typename T::value_type; + const vT real1 = std::real(v); + const vT imag1 = std::imag(v); + return IsNan::isnan(real1) || IsNan::isnan(imag1); } - else { - if constexpr (std::is_floating_point_v || - std::is_same_v) { - return sycl::isnan(v); - } + else if constexpr (std::is_floating_point_v || + std::is_same_v) { + return sycl::isnan(v); } return false; diff --git a/dpnp/backend/extensions/statistics/histogram.hpp b/dpnp/backend/extensions/statistics/histogram.hpp index e89737c1294..c35c8b4a439 100644 --- a/dpnp/backend/extensions/statistics/histogram.hpp +++ b/dpnp/backend/extensions/statistics/histogram.hpp @@ -25,9 +25,11 @@ #pragma once +#include #include #include "dispatch_table.hpp" +#include "dpctl4pybind11.hpp" namespace dpctl_td_ns = dpctl::tensor::type_dispatch; diff --git a/dpnp/backend/extensions/statistics/histogramdd.cpp b/dpnp/backend/extensions/statistics/histogramdd.cpp index 283dbb25b11..6641c853b07 100644 --- a/dpnp/backend/extensions/statistics/histogramdd.cpp +++ b/dpnp/backend/extensions/statistics/histogramdd.cpp @@ -245,7 +245,7 @@ struct HistogramddF }; template -using HistogramddF2 = HistogramddF; +using HistogramddF_ = HistogramddF; using SupportedTypes = std::tuple, @@ -268,7 +268,7 @@ using SupportedTypes = Histogramdd::Histogramdd() : dispatch_table("sample", "histogram") { - dispatch_table.populate_dispatch_table(); + dispatch_table.populate_dispatch_table(); } std::tuple Histogramdd::call( diff --git a/dpnp/backend/extensions/statistics/histogramdd.hpp b/dpnp/backend/extensions/statistics/histogramdd.hpp index 7eb72c68672..8951796e085 100644 --- a/dpnp/backend/extensions/statistics/histogramdd.hpp +++ b/dpnp/backend/extensions/statistics/histogramdd.hpp @@ -25,13 +25,13 @@ #pragma once +#include "dispatch_table.hpp" +#include #include // dpctl tensor headers #include "dpctl4pybind11.hpp" -#include "dispatch_table.hpp" - namespace statistics { namespace histogram diff --git a/dpnp/dpnp_iface_histograms.py b/dpnp/dpnp_iface_histograms.py index f30c7f1b9cf..12637d3d434 100644 --- a/dpnp/dpnp_iface_histograms.py +++ b/dpnp/dpnp_iface_histograms.py @@ -235,32 +235,6 @@ def _get_bin_edges(a, bins, range, usm_type): return bin_edges, None -def _normalize_array(a, dtype, usm_type=None): - if usm_type is None: - usm_type = a.usm_type - - try: - return dpnp.asarray( - a, - dtype=dtype, - usm_type=usm_type, - sycl_queue=a.sycl_queue, - order="C", - copy=False, - ) - except ValueError: - pass - - return dpnp.asarray( - a, - dtype=dtype, - usm_type=usm_type, - sycl_queue=a.sycl_queue, - order="C", - copy=True, - ) - - def _bincount_validate(x, weights, minlength): if x.ndim > 1: raise ValueError("object too deep for desired array") @@ -426,16 +400,16 @@ def bincount(x, weights=None, minlength=None): "supported types" ) - x_casted = _normalize_array(x, dtype=x_casted_dtype) + x_casted = dpnp.asarray(x, dtype=x_casted_dtype, order="C") if weights is not None: - weights_casted = _normalize_array(weights, dtype=ntype_casted) + weights_casted = dpnp.asarray(weights, dtype=ntype_casted, order="C") n_casted = _bincount_run_native( x_casted, weights_casted, minlength, ntype_casted, usm_type ) - n = _normalize_array(n_casted, dtype=ntype, usm_type=usm_type) + n = dpnp.asarray(n_casted, dtype=ntype, usm_type=usm_type, order="C") return n @@ -643,10 +617,12 @@ def histogram(a, bins=10, range=None, density=None, weights=None): "supported types" ) - a_casted = _normalize_array(a, a_bin_dtype) - bin_edges_casted = _normalize_array(bin_edges, a_bin_dtype) + a_casted = dpnp.asarray(a, dtype=a_bin_dtype, order="C") + bin_edges_casted = dpnp.asarray(bin_edges, dtype=a_bin_dtype, order="C") weights_casted = ( - _normalize_array(weights, hist_dtype) if weights is not None else None + dpnp.asarray(weights, dtype=hist_dtype, order="C") + if weights is not None + else None ) # histogram implementation uses atomics, but atomics doesn't work with @@ -681,7 +657,7 @@ def histogram(a, bins=10, range=None, density=None, weights=None): ) _manager.add_event_pair(mem_ev, ht_ev) - n = _normalize_array(n_casted, dtype=ntype, usm_type=usm_type) + n = dpnp.asarray(n_casted, dtype=ntype, usm_type=usm_type, order="C") if density: db = dpnp.astype( @@ -1055,9 +1031,11 @@ def histogramdd(sample, bins=10, range=None, weights=None, density=False): _histdd_check_monotonicity(bin_edges_view_list) - sample_ = _normalize_array(sample, sample_dtype) + sample_ = dpnp.asarray(sample, dtype=sample_dtype, order="C") weights_ = ( - _normalize_array(weights, hist_dtype) if weights is not None else None + dpnp.asarray(weights, dtype=hist_dtype, order="C") + if weights is not None + else None ) n = _histdd_run_native( sample_, @@ -1069,7 +1047,7 @@ def histogramdd(sample, bins=10, range=None, weights=None, density=False): ) expexted_hist_dtype = _histdd_hist_dtype(queue, weights) - n = _normalize_array(n, expexted_hist_dtype, usm_type) + n = dpnp.asarray(n, dtype=expexted_hist_dtype, usm_type=usm_type, order="C") if density: # calculate the probability density function diff --git a/dpnp/tests/test_histogram.py b/dpnp/tests/test_histogram.py index 925196c5129..f956e817d94 100644 --- a/dpnp/tests/test_histogram.py +++ b/dpnp/tests/test_histogram.py @@ -642,10 +642,15 @@ def test_linspace_data(self, dtype): assert_array_equal(result_hist, expected_hist) @pytest.mark.parametrize("xp", [numpy, dpnp]) - def test_invalid_bin(self, xp): + def test_invalid_bin_float(self, xp): a = xp.array([[1, 2]]) assert_raises(ValueError, xp.histogramdd, a, bins=0.1) + @pytest.mark.parametrize("xp", [numpy, dpnp]) + def test_invalid_bin_2d_array(self, xp): + a = xp.array([[1, 2]]) + assert_raises(ValueError, xp.histogramdd, a, bins=[[[10]]]) + @pytest.mark.parametrize( "bins", [