Skip to content

Implement dpnp.erf through pybind11 extension #2551

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

Draft
wants to merge 31 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ec2a3f0
Add erf functor
antonwolfy Aug 1, 2025
ebd7bd0
Add erf implementation to ufunc extension
antonwolfy Aug 1, 2025
fcc3f4e
Add dpnp.special submodule
antonwolfy Aug 1, 2025
70f1483
Remove erf implementation from the legacy backend
antonwolfy Aug 1, 2025
237e5d7
Clean up backend utils
antonwolfy Aug 4, 2025
1ab2725
Remove erf implementation from cython backend
antonwolfy Aug 4, 2025
b34580b
Clean up algo namespace in cython backend
antonwolfy Aug 4, 2025
8c6e64a
Exclude the removed files from the cmake build
antonwolfy Aug 4, 2025
4547843
Clean up algo utils in cython backend
antonwolfy Aug 4, 2025
3cf1ec1
Add missing include to backend utils
antonwolfy Aug 4, 2025
a54950f
Add missing imports to algo namespace in cython backend
antonwolfy Aug 4, 2025
719dbe7
Remove obsolete libmath i/f
antonwolfy Aug 4, 2025
6cf7ec1
Implement erf function as ufunc extension
antonwolfy Aug 4, 2025
2268f9f
Reuse VM implementation of erf from oneMKL
antonwolfy Aug 4, 2025
8959e5a
Add missing includes to VM functions
antonwolfy Aug 4, 2025
33bc76b
Add missing includes to ufunc functions
antonwolfy Aug 4, 2025
dc75465
Enable VM implementation of erf from oneMKL
antonwolfy Aug 4, 2025
8c9ce62
Remove the dedicated erf kernel for half dtype due to accuracy of the…
antonwolfy Aug 4, 2025
ad690ac
Add scipy as a test dependency
antonwolfy Aug 4, 2025
52d5558
Add third party tests for erf function
antonwolfy Aug 4, 2025
a8daafd
Add PR to the changelog
antonwolfy Aug 4, 2025
018b098
Add testing for more use cases
antonwolfy Aug 4, 2025
db96676
Add CFD tests
antonwolfy Aug 4, 2025
e6cc664
Add test with strides
antonwolfy Aug 4, 2025
1046811
Enable erf tests for NVIDIA GPU
antonwolfy Aug 4, 2025
b90a8cd
Update rendered page for special submodule
antonwolfy Aug 4, 2025
0f2cce4
Enable pylint for dpnp.special submodule
antonwolfy Aug 4, 2025
6ef574a
Mute false-positive pylint warnings
antonwolfy Aug 4, 2025
8aa0176
Add special dtype check in tests for scipy>=0.16
antonwolfy Aug 4, 2025
607c87f
Add third party tests for scipy into the package
antonwolfy Aug 4, 2025
c261a83
Skip erf tests when no scipy installed
antonwolfy Aug 5, 2025
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ repos:
"--disable=redefined-builtin",
"--disable=unused-wildcard-import"
]
files: '^dpnp/(dpnp_iface.*|fft|linalg|dpnp_array)'
files: '^dpnp/(dpnp_iface.*|fft|linalg|special|dpnp_array)'
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: v2.15.0
hooks:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Replaced `ci` section in `.pre-commit-config.yaml` with a new GitHub workflow with scheduled run to autoupdate the `pre-commit` configuration [#2542](https://github.com/IntelPython/dpnp/pull/2542)
* FFT module is updated to perform in-place FFT in intermediate steps of ND FFT [#2543](https://github.com/IntelPython/dpnp/pull/2543)
* Reused dpctl tensor include to enable experimental SYCL namespace for complex types [#2546](https://github.com/IntelPython/dpnp/pull/2546)
* Redesigned `dpnp.erf` function through pybind11 extension of OneMKL call or dedicated kernel in `ufunc` namespace [#2551](https://github.com/IntelPython/dpnp/pull/2551)

### Deprecated

Expand Down
1 change: 1 addition & 0 deletions conda-recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ test:
requires:
- pytest
- setuptools
- scipy

about:
home: https://github.com/IntelPython/dpnp
Expand Down
23 changes: 13 additions & 10 deletions doc/reference/special.rst
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
Special Functions
=================
.. currentmodule:: dpnp.special

.. https://docs.scipy.org/doc/scipy/reference/special.html
Special functions (:mod:`dpnp.special`)
=======================================

Error Function
--------------
.. Hint:: `SciPy API Reference: Special functions (scipy.special) <https://docs.scipy.org/doc/scipy/reference/special.html>`_

Error function and Fresnel integrals
------------------------------------

.. autosummary::
:toctree: generated/
:nosignatures:

dpnp.erf
dpnp.erfc
dpnp.erfcx
dpnp.erfinv
dpnp.erfcinv
erf
erfc
erfcx
erfi
erfinv
erfcinv
1 change: 0 additions & 1 deletion dpnp/backend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
set(DPNP_SRC
kernels/dpnp_krnl_arraycreation.cpp
kernels/dpnp_krnl_common.cpp
kernels/dpnp_krnl_elemwise.cpp
kernels/dpnp_krnl_mathematical.cpp
kernels/dpnp_krnl_random.cpp
kernels/dpnp_krnl_sorting.cpp
Expand Down
1 change: 1 addition & 0 deletions dpnp/backend/extensions/ufunc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ set(_elementwise_sources
${CMAKE_CURRENT_SOURCE_DIR}/elementwise_functions/bitwise_count.cpp
${CMAKE_CURRENT_SOURCE_DIR}/elementwise_functions/common.cpp
${CMAKE_CURRENT_SOURCE_DIR}/elementwise_functions/degrees.cpp
${CMAKE_CURRENT_SOURCE_DIR}/elementwise_functions/erf.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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
// THE POSSIBILITY OF SUCH DAMAGE.
//*****************************************************************************

#include <type_traits>
#include <vector>

#include <sycl/sycl.hpp>

#include "dpctl4pybind11.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "bitwise_count.hpp"
#include "degrees.hpp"
#include "erf.hpp"
#include "fabs.hpp"
#include "fix.hpp"
#include "float_power.hpp"
Expand Down Expand Up @@ -56,6 +57,7 @@ void init_elementwise_functions(py::module_ m)
{
init_bitwise_count(m);
init_degrees(m);
init_erf(m);
init_fabs(m);
init_fix(m);
init_float_power(m);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
// THE POSSIBILITY OF SUCH DAMAGE.
//*****************************************************************************

#include <type_traits>
#include <vector>

#include <sycl/sycl.hpp>

#include "dpctl4pybind11.hpp"
Expand Down
127 changes: 127 additions & 0 deletions dpnp/backend/extensions/ufunc/elementwise_functions/erf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
//*****************************************************************************
// Copyright (c) 2025, 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.
//
// 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 <type_traits>
#include <vector>

#include <sycl/sycl.hpp>

#include "dpctl4pybind11.hpp"

#include "erf.hpp"
#include "kernels/elementwise_functions/erf.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::erf<T> function is available.
*
* @tparam T Type of input vector `a` and of result vector `y`.
*/
template <typename T>
struct OutputType
{
using value_type =
typename std::disjunction<td_ns::TypeMapResultEntry<T, float>,
td_ns::TypeMapResultEntry<T, double>,
td_ns::DefaultResultEntry<void>>::result_type;
};

using dpnp::kernels::erf::ErfFunctor;

template <typename argT,
typename resT = argT,
unsigned int vec_sz = 4,
unsigned int n_vecs = 2,
bool enable_sg_loadstore = true>
using ContigFunctor = ew_cmn_ns::UnaryContigFunctor<argT,
resT,
ErfFunctor<argT, resT>,
vec_sz,
n_vecs,
enable_sg_loadstore>;

template <typename argTy, typename resTy, typename IndexerT>
using StridedFunctor = ew_cmn_ns::
UnaryStridedFunctor<argTy, resTy, IndexerT, ErfFunctor<argTy, resTy>>;

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 erf_contig_dispatch_vector[td_ns::num_types];
static int erf_output_typeid_vector[td_ns::num_types];
static unary_strided_impl_fn_ptr_t
erf_strided_dispatch_vector[td_ns::num_types];

MACRO_POPULATE_DISPATCH_VECTORS(erf);
} // namespace impl

void init_erf(py::module_ m)
{
using arrayT = dpctl::tensor::usm_ndarray;
using event_vecT = std::vector<sycl::event>;
{
impl::populate_erf_dispatch_vectors();
using impl::erf_contig_dispatch_vector;
using impl::erf_output_typeid_vector;
using impl::erf_strided_dispatch_vector;

auto erf_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, erf_output_typeid_vector,
erf_contig_dispatch_vector, erf_strided_dispatch_vector);
};
m.def("_erf", erf_pyapi, "", py::arg("src"), py::arg("dst"),
py::arg("sycl_queue"), py::arg("depends") = py::list());

auto erf_result_type_pyapi = [&](const py::dtype &dtype) {
return py_int::py_unary_ufunc_result_type(dtype,
erf_output_typeid_vector);
};
m.def("_erf_result_type", erf_result_type_pyapi);
}
}
} // namespace dpnp::extensions::ufunc
35 changes: 35 additions & 0 deletions dpnp/backend/extensions/ufunc/elementwise_functions/erf.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//*****************************************************************************
// Copyright (c) 2025, 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.
//
// 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 <pybind11/pybind11.h>

namespace py = pybind11;

namespace dpnp::extensions::ufunc
{
void init_erf(py::module_ m);
} // namespace dpnp::extensions::ufunc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
// THE POSSIBILITY OF SUCH DAMAGE.
//*****************************************************************************

#include <type_traits>
#include <vector>

#include <sycl/sycl.hpp>

#include "dpctl4pybind11.hpp"
Expand Down
3 changes: 3 additions & 0 deletions dpnp/backend/extensions/ufunc/elementwise_functions/fix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
// THE POSSIBILITY OF SUCH DAMAGE.
//*****************************************************************************

#include <type_traits>
#include <vector>

#include <sycl/sycl.hpp>

#include "dpctl4pybind11.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
// THE POSSIBILITY OF SUCH DAMAGE.
//*****************************************************************************

#include <complex>
#include <type_traits>
#include <vector>

#include <sycl/sycl.hpp>

#include "dpctl4pybind11.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
// THE POSSIBILITY OF SUCH DAMAGE.
//*****************************************************************************

#include <vector>

#include <sycl/sycl.hpp>

#include "dpctl4pybind11.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
// THE POSSIBILITY OF SUCH DAMAGE.
//*****************************************************************************

#include <vector>

#include <sycl/sycl.hpp>

#include "dpctl4pybind11.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
// THE POSSIBILITY OF SUCH DAMAGE.
//*****************************************************************************

#include <type_traits>
#include <vector>

#include <sycl/sycl.hpp>

#include "dpctl4pybind11.hpp"
Expand Down
3 changes: 3 additions & 0 deletions dpnp/backend/extensions/ufunc/elementwise_functions/gcd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
// THE POSSIBILITY OF SUCH DAMAGE.
//*****************************************************************************

#include <type_traits>
#include <vector>

#include <sycl/sycl.hpp>

#include "dpctl4pybind11.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
// THE POSSIBILITY OF SUCH DAMAGE.
//*****************************************************************************

#include <type_traits>
#include <vector>

#include <sycl/sycl.hpp>

#include "dpctl4pybind11.hpp"
Expand Down
3 changes: 3 additions & 0 deletions dpnp/backend/extensions/ufunc/elementwise_functions/i0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
// THE POSSIBILITY OF SUCH DAMAGE.
//*****************************************************************************

#include <type_traits>
#include <vector>

#include <sycl/sycl.hpp>

#include "dpctl4pybind11.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@
//*****************************************************************************

#include <complex>
#include <optional>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

#include <sycl/sycl.hpp>

#include "dpctl4pybind11.hpp"
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
Expand Down
3 changes: 3 additions & 0 deletions dpnp/backend/extensions/ufunc/elementwise_functions/lcm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
// THE POSSIBILITY OF SUCH DAMAGE.
//*****************************************************************************

#include <type_traits>
#include <vector>

#include <sycl/sycl.hpp>

#include "dpctl4pybind11.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
// THE POSSIBILITY OF SUCH DAMAGE.
//*****************************************************************************

#include <type_traits>
#include <vector>

#include <sycl/sycl.hpp>

#include "dpctl4pybind11.hpp"
Expand Down
Loading
Loading