Skip to content

Commit e33a82b

Browse files
authored
Rework implementation of dpnp.deg2rad and dpnp.radians functions (#1943)
* Add implementation of dpnp.radians() and dpnp.deg2rad() * Add more tests * Applied comments to fix typo in comment
1 parent 8acbe64 commit e33a82b

File tree

21 files changed

+513
-270
lines changed

21 files changed

+513
-270
lines changed

doc/reference/ufunc.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ Math operations
5454

5555
Trigonometric functions
5656
~~~~~~~~~~~~~~~~~~~~~~~
57+
All trigonometric functions use radians when an angle is called for.
58+
The ratio of degrees to radians is :math:`180^{\circ}/\pi.`
5759

5860
.. autosummary::
5961
:toctree: generated/
@@ -73,6 +75,8 @@ Trigonometric functions
7375
dpnp.arcsinh
7476
dpnp.arccosh
7577
dpnp.arctanh
78+
dpnp.degrees
79+
dpnp.radians
7680
dpnp.deg2rad
7781
dpnp.rad2deg
7882

dpnp/backend/extensions/ufunc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ set(_elementwise_sources
2929
${CMAKE_CURRENT_SOURCE_DIR}/elementwise_functions/fmax.cpp
3030
${CMAKE_CURRENT_SOURCE_DIR}/elementwise_functions/fmin.cpp
3131
${CMAKE_CURRENT_SOURCE_DIR}/elementwise_functions/fmod.cpp
32+
${CMAKE_CURRENT_SOURCE_DIR}/elementwise_functions/radians.cpp
3233
)
3334

3435
set(python_module_name _ufunc_impl)

dpnp/backend/extensions/ufunc/elementwise_functions/common.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "fmax.hpp"
3030
#include "fmin.hpp"
3131
#include "fmod.hpp"
32+
#include "radians.hpp"
3233

3334
namespace py = pybind11;
3435

@@ -43,5 +44,6 @@ void init_elementwise_functions(py::module_ m)
4344
init_fmax(m);
4445
init_fmin(m);
4546
init_fmod(m);
47+
init_radians(m);
4648
}
4749
} // namespace dpnp::extensions::ufunc

dpnp/backend/extensions/ufunc/elementwise_functions/fabs.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,16 @@
4040
#include "kernels/elementwise_functions/common.hpp"
4141
#include "utils/type_dispatch.hpp"
4242

43-
namespace py = pybind11;
44-
4543
namespace dpnp::extensions::ufunc
4644
{
47-
namespace ew_cmn_ns = dpctl::tensor::kernels::elementwise_common;
45+
namespace py = pybind11;
4846
namespace py_int = dpnp::extensions::py_internal;
49-
namespace td_ns = dpctl::tensor::type_dispatch;
50-
51-
using ew_cmn_ns::unary_contig_impl_fn_ptr_t;
52-
using ew_cmn_ns::unary_strided_impl_fn_ptr_t;
5347

5448
namespace impl
5549
{
50+
namespace ew_cmn_ns = dpctl::tensor::kernels::elementwise_common;
51+
namespace td_ns = dpctl::tensor::type_dispatch;
52+
5653
/**
5754
* @brief A factory to define pairs of supported types for which
5855
* sycl::fabs<T> function is available.

dpnp/backend/extensions/ufunc/elementwise_functions/fmax.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,17 @@
4141
#include "kernels/elementwise_functions/maximum.hpp"
4242
#include "utils/type_dispatch.hpp"
4343

44-
namespace py = pybind11;
45-
4644
namespace dpnp::extensions::ufunc
4745
{
48-
namespace ew_cmn_ns = dpctl::tensor::kernels::elementwise_common;
49-
namespace max_ns = dpctl::tensor::kernels::maximum;
46+
namespace py = pybind11;
5047
namespace py_int = dpnp::extensions::py_internal;
5148
namespace td_ns = dpctl::tensor::type_dispatch;
5249

53-
using ew_cmn_ns::unary_contig_impl_fn_ptr_t;
54-
using ew_cmn_ns::unary_strided_impl_fn_ptr_t;
55-
5650
namespace impl
5751
{
52+
namespace ew_cmn_ns = dpctl::tensor::kernels::elementwise_common;
53+
namespace max_ns = dpctl::tensor::kernels::maximum;
54+
5855
// Supports the same types table as for maximum function in dpctl
5956
template <typename T1, typename T2>
6057
using OutputType = max_ns::MaximumOutputType<T1, T2>;

dpnp/backend/extensions/ufunc/elementwise_functions/fmin.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,17 @@
4141
#include "kernels/elementwise_functions/minimum.hpp"
4242
#include "utils/type_dispatch.hpp"
4343

44-
namespace py = pybind11;
45-
4644
namespace dpnp::extensions::ufunc
4745
{
48-
namespace ew_cmn_ns = dpctl::tensor::kernels::elementwise_common;
49-
namespace min_ns = dpctl::tensor::kernels::minimum;
46+
namespace py = pybind11;
5047
namespace py_int = dpnp::extensions::py_internal;
5148
namespace td_ns = dpctl::tensor::type_dispatch;
5249

53-
using ew_cmn_ns::unary_contig_impl_fn_ptr_t;
54-
using ew_cmn_ns::unary_strided_impl_fn_ptr_t;
55-
5650
namespace impl
5751
{
52+
namespace ew_cmn_ns = dpctl::tensor::kernels::elementwise_common;
53+
namespace min_ns = dpctl::tensor::kernels::minimum;
54+
5855
// Supports the same types table as for minimum function in dpctl
5956
template <typename T1, typename T2>
6057
using OutputType = min_ns::MinimumOutputType<T1, T2>;

dpnp/backend/extensions/ufunc/elementwise_functions/fmod.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,16 @@
4040
#include "kernels/elementwise_functions/common.hpp"
4141
#include "utils/type_dispatch.hpp"
4242

43-
namespace py = pybind11;
44-
4543
namespace dpnp::extensions::ufunc
4644
{
47-
namespace ew_cmn_ns = dpctl::tensor::kernels::elementwise_common;
45+
namespace py = pybind11;
4846
namespace py_int = dpnp::extensions::py_internal;
49-
namespace td_ns = dpctl::tensor::type_dispatch;
50-
51-
using ew_cmn_ns::unary_contig_impl_fn_ptr_t;
52-
using ew_cmn_ns::unary_strided_impl_fn_ptr_t;
5347

5448
namespace impl
5549
{
50+
namespace ew_cmn_ns = dpctl::tensor::kernels::elementwise_common;
51+
namespace td_ns = dpctl::tensor::type_dispatch;
52+
5653
/**
5754
* @brief A factory to define pairs of supported types for which
5855
* sycl::fmod<T> function is available.
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
//*****************************************************************************
2+
// Copyright (c) 2024, Intel Corporation
3+
// All rights reserved.
4+
//
5+
// Redistribution and use in source and binary forms, with or without
6+
// modification, are permitted provided that the following conditions are met:
7+
// - Redistributions of source code must retain the above copyright notice,
8+
// this list of conditions and the following disclaimer.
9+
// - Redistributions in binary form must reproduce the above copyright notice,
10+
// this list of conditions and the following disclaimer in the documentation
11+
// and/or other materials provided with the distribution.
12+
//
13+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16+
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17+
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18+
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19+
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21+
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22+
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23+
// THE POSSIBILITY OF SUCH DAMAGE.
24+
//*****************************************************************************
25+
26+
#include <sycl/sycl.hpp>
27+
28+
#include "dpctl4pybind11.hpp"
29+
30+
#include "kernels/elementwise_functions/radians.hpp"
31+
#include "populate.hpp"
32+
#include "radians.hpp"
33+
34+
// include a local copy of elementwise common header from dpctl tensor:
35+
// dpctl/tensor/libtensor/source/elementwise_functions/elementwise_functions.hpp
36+
// TODO: replace by including dpctl header once available
37+
#include "../../elementwise_functions/elementwise_functions.hpp"
38+
39+
// dpctl tensor headers
40+
#include "kernels/elementwise_functions/common.hpp"
41+
#include "utils/type_dispatch.hpp"
42+
43+
namespace dpnp::extensions::ufunc
44+
{
45+
namespace py = pybind11;
46+
namespace py_int = dpnp::extensions::py_internal;
47+
48+
namespace impl
49+
{
50+
namespace ew_cmn_ns = dpctl::tensor::kernels::elementwise_common;
51+
namespace td_ns = dpctl::tensor::type_dispatch;
52+
53+
/**
54+
* @brief A factory to define pairs of supported types for which
55+
* sycl::radians<T> function is available.
56+
*
57+
* @tparam T Type of input vector `a` and of result vector `y`.
58+
*/
59+
template <typename T>
60+
struct OutputType
61+
{
62+
using value_type =
63+
typename std::disjunction<td_ns::TypeMapResultEntry<T, sycl::half>,
64+
td_ns::TypeMapResultEntry<T, float>,
65+
td_ns::TypeMapResultEntry<T, double>,
66+
td_ns::DefaultResultEntry<void>>::result_type;
67+
};
68+
69+
using dpnp::kernels::radians::RadiansFunctor;
70+
71+
template <typename argT,
72+
typename resT = argT,
73+
unsigned int vec_sz = 4,
74+
unsigned int n_vecs = 2,
75+
bool enable_sg_loadstore = true>
76+
using ContigFunctor = ew_cmn_ns::UnaryContigFunctor<argT,
77+
resT,
78+
RadiansFunctor<argT, resT>,
79+
vec_sz,
80+
n_vecs,
81+
enable_sg_loadstore>;
82+
83+
template <typename argTy, typename resTy, typename IndexerT>
84+
using StridedFunctor = ew_cmn_ns::
85+
UnaryStridedFunctor<argTy, resTy, IndexerT, RadiansFunctor<argTy, resTy>>;
86+
87+
using ew_cmn_ns::unary_contig_impl_fn_ptr_t;
88+
using ew_cmn_ns::unary_strided_impl_fn_ptr_t;
89+
90+
static unary_contig_impl_fn_ptr_t
91+
radians_contig_dispatch_vector[td_ns::num_types];
92+
static int radians_output_typeid_vector[td_ns::num_types];
93+
static unary_strided_impl_fn_ptr_t
94+
radians_strided_dispatch_vector[td_ns::num_types];
95+
96+
MACRO_POPULATE_DISPATCH_VECTORS(radians);
97+
} // namespace impl
98+
99+
void init_radians(py::module_ m)
100+
{
101+
using arrayT = dpctl::tensor::usm_ndarray;
102+
using event_vecT = std::vector<sycl::event>;
103+
{
104+
impl::populate_radians_dispatch_vectors();
105+
using impl::radians_contig_dispatch_vector;
106+
using impl::radians_output_typeid_vector;
107+
using impl::radians_strided_dispatch_vector;
108+
109+
auto radians_pyapi = [&](const arrayT &src, const arrayT &dst,
110+
sycl::queue &exec_q,
111+
const event_vecT &depends = {}) {
112+
return py_int::py_unary_ufunc(src, dst, exec_q, depends,
113+
radians_output_typeid_vector,
114+
radians_contig_dispatch_vector,
115+
radians_strided_dispatch_vector);
116+
};
117+
m.def("_radians", radians_pyapi, "", py::arg("src"), py::arg("dst"),
118+
py::arg("sycl_queue"), py::arg("depends") = py::list());
119+
120+
auto radians_result_type_pyapi = [&](const py::dtype &dtype) {
121+
return py_int::py_unary_ufunc_result_type(
122+
dtype, radians_output_typeid_vector);
123+
};
124+
m.def("_radians_result_type", radians_result_type_pyapi);
125+
}
126+
}
127+
} // namespace dpnp::extensions::ufunc
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//*****************************************************************************
2+
// Copyright (c) 2024, Intel Corporation
3+
// All rights reserved.
4+
//
5+
// Redistribution and use in source and binary forms, with or without
6+
// modification, are permitted provided that the following conditions are met:
7+
// - Redistributions of source code must retain the above copyright notice,
8+
// this list of conditions and the following disclaimer.
9+
// - Redistributions in binary form must reproduce the above copyright notice,
10+
// this list of conditions and the following disclaimer in the documentation
11+
// and/or other materials provided with the distribution.
12+
//
13+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16+
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17+
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18+
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19+
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21+
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22+
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23+
// THE POSSIBILITY OF SUCH DAMAGE.
24+
//*****************************************************************************
25+
26+
#pragma once
27+
28+
#include <pybind11/pybind11.h>
29+
30+
namespace py = pybind11;
31+
32+
namespace dpnp::extensions::ufunc
33+
{
34+
void init_radians(py::module_ m);
35+
} // namespace dpnp::extensions::ufunc

dpnp/backend/include/dpnp_gen_1arg_2type_tbl.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,6 @@ MACRO_1ARG_2TYPES_OP(dpnp_copyto_c, input_elem, q.submit(kernel_func))
8989
MACRO_1ARG_2TYPES_OP(dpnp_degrees_c,
9090
sycl::degrees(input_elem),
9191
q.submit(kernel_func))
92-
MACRO_1ARG_2TYPES_OP(dpnp_radians_c,
93-
sycl::radians(input_elem),
94-
q.submit(kernel_func))
9592
MACRO_1ARG_2TYPES_OP(dpnp_sqrt_c,
9693
sycl::sqrt(input_elem),
9794
oneapi::mkl::vm::sqrt(q, input1_size, input1_data, result))

dpnp/backend/include/dpnp_iface_fptr.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,6 @@ enum class DPNPFuncName : size_t
116116
DPNP_FN_PARTITION_EXT, /**< Used in numpy.partition() impl, requires extra
117117
parameters */
118118
DPNP_FN_PROD, /**< Used in numpy.prod() impl */
119-
DPNP_FN_RADIANS, /**< Used in numpy.radians() impl */
120-
DPNP_FN_RADIANS_EXT, /**< Used in numpy.radians() impl, requires extra
121-
parameters */
122119
DPNP_FN_RNG_BETA, /**< Used in numpy.random.beta() impl */
123120
DPNP_FN_RNG_BETA_EXT, /**< Used in numpy.random.beta() impl, requires extra
124121
parameters */

dpnp/backend/kernels/dpnp_krnl_elemwise.cpp

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -337,36 +337,6 @@ static void func_map_init_elemwise_1arg_2type(func_map_t &fmap)
337337
fmap[DPNPFuncName::DPNP_FN_DEGREES_EXT][eft_DBL][eft_DBL] = {
338338
eft_DBL, (void *)dpnp_degrees_c_ext<double, double>};
339339

340-
fmap[DPNPFuncName::DPNP_FN_RADIANS][eft_INT][eft_INT] = {
341-
eft_DBL, (void *)dpnp_radians_c_default<int32_t, double>};
342-
fmap[DPNPFuncName::DPNP_FN_RADIANS][eft_LNG][eft_LNG] = {
343-
eft_DBL, (void *)dpnp_radians_c_default<int64_t, double>};
344-
fmap[DPNPFuncName::DPNP_FN_RADIANS][eft_FLT][eft_FLT] = {
345-
eft_FLT, (void *)dpnp_radians_c_default<float, float>};
346-
fmap[DPNPFuncName::DPNP_FN_RADIANS][eft_DBL][eft_DBL] = {
347-
eft_DBL, (void *)dpnp_radians_c_default<double, double>};
348-
349-
fmap[DPNPFuncName::DPNP_FN_RADIANS_EXT][eft_INT][eft_INT] = {
350-
get_default_floating_type(),
351-
(void *)dpnp_radians_c_ext<
352-
int32_t, func_type_map_t::find_type<get_default_floating_type()>>,
353-
get_default_floating_type<std::false_type>(),
354-
(void *)dpnp_radians_c_ext<
355-
int32_t, func_type_map_t::find_type<
356-
get_default_floating_type<std::false_type>()>>};
357-
fmap[DPNPFuncName::DPNP_FN_RADIANS_EXT][eft_LNG][eft_LNG] = {
358-
get_default_floating_type(),
359-
(void *)dpnp_radians_c_ext<
360-
int64_t, func_type_map_t::find_type<get_default_floating_type()>>,
361-
get_default_floating_type<std::false_type>(),
362-
(void *)dpnp_radians_c_ext<
363-
int64_t, func_type_map_t::find_type<
364-
get_default_floating_type<std::false_type>()>>};
365-
fmap[DPNPFuncName::DPNP_FN_RADIANS_EXT][eft_FLT][eft_FLT] = {
366-
eft_FLT, (void *)dpnp_radians_c_ext<float, float>};
367-
fmap[DPNPFuncName::DPNP_FN_RADIANS_EXT][eft_DBL][eft_DBL] = {
368-
eft_DBL, (void *)dpnp_radians_c_ext<double, double>};
369-
370340
fmap[DPNPFuncName::DPNP_FN_SQRT][eft_INT][eft_INT] = {
371341
eft_DBL, (void *)dpnp_sqrt_c_default<int32_t, double>};
372342
fmap[DPNPFuncName::DPNP_FN_SQRT][eft_LNG][eft_LNG] = {

dpnp/backend/kernels/elementwise_functions/fabs.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct FabsFunctor
3838
// constexpr resT constant_value = resT{};
3939
// is function defined for sycl::vec
4040
using supports_vec = typename std::false_type;
41-
// do both argT and resT support sugroup store/load operation
41+
// do both argT and resT support subgroup store/load operation
4242
using supports_sg_loadstore = typename std::true_type;
4343

4444
resT operator()(const argT &x) const

0 commit comments

Comments
 (0)