diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e067b3783b..2e5cedb4dba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ Also, that release drops support for Python 3.9, making Python 3.10 the minimum * Unified `dpnp` public API exports by consolidating function exports in `__init__.py` and removing wildcard imports [#2665](https://github.com/IntelPython/dpnp/pull/2665) [#2666](https://github.com/IntelPython/dpnp/pull/2666) * Updated tests to reflect the new scalar conversion rules for non-0D `usm_ndarray` [#2694](https://github.com/IntelPython/dpnp/pull/2694) * Compile indexing extension with `-fno-sycl-id-queries-fit-in-int` to support huge arrays [#2721](https://github.com/IntelPython/dpnp/pull/2721) +* Updated `dpnp.fix` to reuse `dpnp.trunc` internally [#2722](https://github.com/IntelPython/dpnp/pull/2722) ### Deprecated diff --git a/dpnp/backend/extensions/ufunc/CMakeLists.txt b/dpnp/backend/extensions/ufunc/CMakeLists.txt index 5609522f58a..b24d5d131cf 100644 --- a/dpnp/backend/extensions/ufunc/CMakeLists.txt +++ b/dpnp/backend/extensions/ufunc/CMakeLists.txt @@ -34,7 +34,6 @@ set(_elementwise_sources ${CMAKE_CURRENT_SOURCE_DIR}/elementwise_functions/divmod.cpp ${CMAKE_CURRENT_SOURCE_DIR}/elementwise_functions/erf_funcs.cpp ${CMAKE_CURRENT_SOURCE_DIR}/elementwise_functions/fabs.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/elementwise_functions/fix.cpp ${CMAKE_CURRENT_SOURCE_DIR}/elementwise_functions/float_power.cpp ${CMAKE_CURRENT_SOURCE_DIR}/elementwise_functions/fmax.cpp ${CMAKE_CURRENT_SOURCE_DIR}/elementwise_functions/fmin.cpp diff --git a/dpnp/backend/extensions/ufunc/elementwise_functions/common.cpp b/dpnp/backend/extensions/ufunc/elementwise_functions/common.cpp index df409464a5c..9254e87e52c 100644 --- a/dpnp/backend/extensions/ufunc/elementwise_functions/common.cpp +++ b/dpnp/backend/extensions/ufunc/elementwise_functions/common.cpp @@ -33,7 +33,6 @@ #include "divmod.hpp" #include "erf_funcs.hpp" #include "fabs.hpp" -#include "fix.hpp" #include "float_power.hpp" #include "fmax.hpp" #include "fmin.hpp" @@ -67,7 +66,6 @@ void init_elementwise_functions(py::module_ m) init_divmod(m); init_erf_funcs(m); init_fabs(m); - init_fix(m); init_float_power(m); init_fmax(m); init_fmin(m); diff --git a/dpnp/backend/extensions/ufunc/elementwise_functions/fix.cpp b/dpnp/backend/extensions/ufunc/elementwise_functions/fix.cpp deleted file mode 100644 index 6b21245489f..00000000000 --- a/dpnp/backend/extensions/ufunc/elementwise_functions/fix.cpp +++ /dev/null @@ -1,131 +0,0 @@ -//***************************************************************************** -// Copyright (c) 2024, Intel Corporation -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// - Redistributions of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// - Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// - Neither the name of the copyright holder nor the names of its contributors -// may be used to endorse or promote products derived from this software -// without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE -// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF -// THE POSSIBILITY OF SUCH DAMAGE. -//***************************************************************************** - -#include -#include - -#include - -#include "dpctl4pybind11.hpp" - -#include "fix.hpp" -#include "kernels/elementwise_functions/fix.hpp" -#include "populate.hpp" - -// include a local copy of elementwise common header from dpctl tensor: -// dpctl/tensor/libtensor/source/elementwise_functions/elementwise_functions.hpp -// TODO: replace by including dpctl header once available -#include "../../elementwise_functions/elementwise_functions.hpp" - -// dpctl tensor headers -#include "kernels/elementwise_functions/common.hpp" -#include "utils/type_dispatch.hpp" - -namespace dpnp::extensions::ufunc -{ -namespace py = pybind11; -namespace py_int = dpnp::extensions::py_internal; - -namespace impl -{ -namespace ew_cmn_ns = dpctl::tensor::kernels::elementwise_common; -namespace td_ns = dpctl::tensor::type_dispatch; - -/** - * @brief A factory to define pairs of supported types for which - * sycl::fix function is available. - * - * @tparam T Type of input vector `a` and of result vector `y`. - */ -template -struct OutputType -{ - using value_type = - typename std::disjunction, - td_ns::TypeMapResultEntry, - td_ns::TypeMapResultEntry, - td_ns::DefaultResultEntry>::result_type; -}; - -using dpnp::kernels::fix::FixFunctor; - -template -using ContigFunctor = ew_cmn_ns::UnaryContigFunctor, - vec_sz, - n_vecs, - enable_sg_loadstore>; - -template -using StridedFunctor = ew_cmn_ns:: - UnaryStridedFunctor>; - -using ew_cmn_ns::unary_contig_impl_fn_ptr_t; -using ew_cmn_ns::unary_strided_impl_fn_ptr_t; - -static unary_contig_impl_fn_ptr_t fix_contig_dispatch_vector[td_ns::num_types]; -static int fix_output_typeid_vector[td_ns::num_types]; -static unary_strided_impl_fn_ptr_t - fix_strided_dispatch_vector[td_ns::num_types]; - -MACRO_POPULATE_DISPATCH_VECTORS(fix); -} // namespace impl - -void init_fix(py::module_ m) -{ - using arrayT = dpctl::tensor::usm_ndarray; - using event_vecT = std::vector; - { - impl::populate_fix_dispatch_vectors(); - using impl::fix_contig_dispatch_vector; - using impl::fix_output_typeid_vector; - using impl::fix_strided_dispatch_vector; - - auto fix_pyapi = [&](const arrayT &src, const arrayT &dst, - sycl::queue &exec_q, - const event_vecT &depends = {}) { - return py_int::py_unary_ufunc( - src, dst, exec_q, depends, fix_output_typeid_vector, - fix_contig_dispatch_vector, fix_strided_dispatch_vector); - }; - m.def("_fix", fix_pyapi, "", py::arg("src"), py::arg("dst"), - py::arg("sycl_queue"), py::arg("depends") = py::list()); - - auto fix_result_type_pyapi = [&](const py::dtype &dtype) { - return py_int::py_unary_ufunc_result_type(dtype, - fix_output_typeid_vector); - }; - m.def("_fix_result_type", fix_result_type_pyapi); - } -} -} // namespace dpnp::extensions::ufunc diff --git a/dpnp/backend/extensions/ufunc/elementwise_functions/fix.hpp b/dpnp/backend/extensions/ufunc/elementwise_functions/fix.hpp deleted file mode 100644 index f9fed62cf81..00000000000 --- a/dpnp/backend/extensions/ufunc/elementwise_functions/fix.hpp +++ /dev/null @@ -1,38 +0,0 @@ -//***************************************************************************** -// Copyright (c) 2024, Intel Corporation -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// - Redistributions of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// - Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// - Neither the name of the copyright holder nor the names of its contributors -// may be used to endorse or promote products derived from this software -// without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE -// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF -// THE POSSIBILITY OF SUCH DAMAGE. -//***************************************************************************** - -#pragma once - -#include - -namespace py = pybind11; - -namespace dpnp::extensions::ufunc -{ -void init_fix(py::module_ m); -} // namespace dpnp::extensions::ufunc diff --git a/dpnp/backend/kernels/elementwise_functions/fix.hpp b/dpnp/backend/kernels/elementwise_functions/fix.hpp deleted file mode 100644 index f53bfc17e56..00000000000 --- a/dpnp/backend/kernels/elementwise_functions/fix.hpp +++ /dev/null @@ -1,52 +0,0 @@ -//***************************************************************************** -// Copyright (c) 2024, Intel Corporation -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// - Redistributions of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// - Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// - Neither the name of the copyright holder nor the names of its contributors -// may be used to endorse or promote products derived from this software -// without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE -// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF -// THE POSSIBILITY OF SUCH DAMAGE. -//***************************************************************************** - -#pragma once - -#include - -namespace dpnp::kernels::fix -{ -template -struct FixFunctor -{ - // is function constant for given argT - using is_constant = typename std::false_type; - // constant value, if constant - // constexpr resT constant_value = resT{}; - // is function defined for sycl::vec - using supports_vec = typename std::false_type; - // do both argT and resT support subgroup store/load operation - using supports_sg_loadstore = typename std::true_type; - - resT operator()(const argT &x) const - { - return (x >= 0.0) ? sycl::floor(x) : sycl::ceil(x); - } -}; -} // namespace dpnp::kernels::fix diff --git a/dpnp/dpnp_algo/dpnp_elementwise_common.py b/dpnp/dpnp_algo/dpnp_elementwise_common.py index 60a55acd1f4..c404d71dfbc 100644 --- a/dpnp/dpnp_algo/dpnp_elementwise_common.py +++ b/dpnp/dpnp_algo/dpnp_elementwise_common.py @@ -60,7 +60,6 @@ "DPNPBinaryFunc", "DPNPBinaryFuncOutKw", "DPNPBinaryTwoOutputsFunc", - "DPNPFix", "DPNPImag", "DPNPReal", "DPNPRound", @@ -1188,55 +1187,6 @@ def __call__(self, x, /, deg=False, *, out=None, order="K"): return res -class DPNPFix(DPNPUnaryFunc): - """Class that implements dpnp.fix unary element-wise functions.""" - - def __init__( - self, - name, - result_type_resolver_fn, - unary_dp_impl_fn, - docs, - ): - super().__init__( - name, - result_type_resolver_fn, - unary_dp_impl_fn, - docs, - ) - - def __call__(self, x, /, out=None, *, order="K"): - if not dpnp.is_supported_array_type(x): - pass # pass to raise error in main implementation - elif dpnp.issubdtype(x.dtype, dpnp.inexact): - pass # for inexact types, pass to calculate in the backend - elif not ( - out is None - or isinstance(out, tuple) - or dpnp.is_supported_array_type(out) - ): - pass # pass to raise error in main implementation - elif not ( - out is None or isinstance(out, tuple) or out.dtype == x.dtype - ): - # passing will raise an error but with incorrect needed dtype - raise ValueError( - f"Output array of type {x.dtype} is needed, got {out.dtype}" - ) - else: - # for exact types, return the input - out = self._unpack_out_kw(out) - if out is None: - return dpnp.copy(x, order=order) - - if isinstance(out, dpt.usm_ndarray): - out = dpnp_array._create_from_usm_ndarray(out) - out[...] = x - return out - - return super().__call__(x, out=out, order=order) - - class DPNPI0(DPNPUnaryFunc): """Class that implements dpnp.i0 unary element-wise functions.""" diff --git a/dpnp/dpnp_iface_mathematical.py b/dpnp/dpnp_iface_mathematical.py index 3dc5ccb8288..63aee599d9b 100644 --- a/dpnp/dpnp_iface_mathematical.py +++ b/dpnp/dpnp_iface_mathematical.py @@ -66,7 +66,6 @@ DPNPBinaryFunc, DPNPBinaryFuncOutKw, DPNPBinaryTwoOutputsFunc, - DPNPFix, DPNPImag, DPNPReal, DPNPRound, @@ -1867,11 +1866,14 @@ def ediff1d(ary, to_end=None, to_begin=None): """ -fix = DPNPFix( +# reuse trunc backend implementation for fix +fix = DPNPUnaryFunc( "fix", - ufi._fix_result_type, - ufi._fix, + ti._trunc_result_type, + ti._trunc, _FIX_DOCSTRING, + mkl_fn_to_call="_mkl_trunc_to_call", + mkl_impl_fn="_trunc", )