Skip to content

Commit 607e248

Browse files
committed
Add implementation of dpnp.degrees() and dpnp.rad2deg()
1 parent e33a82b commit 607e248

File tree

15 files changed

+398
-155
lines changed

15 files changed

+398
-155
lines changed

dpnp/backend/extensions/ufunc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
set(_elementwise_sources
2727
${CMAKE_CURRENT_SOURCE_DIR}/elementwise_functions/common.cpp
28+
${CMAKE_CURRENT_SOURCE_DIR}/elementwise_functions/degrees.cpp
2829
${CMAKE_CURRENT_SOURCE_DIR}/elementwise_functions/fabs.cpp
2930
${CMAKE_CURRENT_SOURCE_DIR}/elementwise_functions/fmax.cpp
3031
${CMAKE_CURRENT_SOURCE_DIR}/elementwise_functions/fmin.cpp

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <pybind11/pybind11.h>
2727

28+
#include "degrees.hpp"
2829
#include "fabs.hpp"
2930
#include "fmax.hpp"
3031
#include "fmin.hpp"
@@ -40,6 +41,7 @@ namespace dpnp::extensions::ufunc
4041
*/
4142
void init_elementwise_functions(py::module_ m)
4243
{
44+
init_degrees(m);
4345
init_fabs(m);
4446
init_fmax(m);
4547
init_fmin(m);
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 "degrees.hpp"
31+
#include "kernels/elementwise_functions/degrees.hpp"
32+
#include "populate.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::degrees<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::degrees::DegreesFunctor;
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+
DegreesFunctor<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, DegreesFunctor<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+
degrees_contig_dispatch_vector[td_ns::num_types];
92+
static int degrees_output_typeid_vector[td_ns::num_types];
93+
static unary_strided_impl_fn_ptr_t
94+
degrees_strided_dispatch_vector[td_ns::num_types];
95+
96+
MACRO_POPULATE_DISPATCH_VECTORS(degrees);
97+
} // namespace impl
98+
99+
void init_degrees(py::module_ m)
100+
{
101+
using arrayT = dpctl::tensor::usm_ndarray;
102+
using event_vecT = std::vector<sycl::event>;
103+
{
104+
impl::populate_degrees_dispatch_vectors();
105+
using impl::degrees_contig_dispatch_vector;
106+
using impl::degrees_output_typeid_vector;
107+
using impl::degrees_strided_dispatch_vector;
108+
109+
auto degrees_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+
degrees_output_typeid_vector,
114+
degrees_contig_dispatch_vector,
115+
degrees_strided_dispatch_vector);
116+
};
117+
m.def("_degrees", degrees_pyapi, "", py::arg("src"), py::arg("dst"),
118+
py::arg("sycl_queue"), py::arg("depends") = py::list());
119+
120+
auto degrees_result_type_pyapi = [&](const py::dtype &dtype) {
121+
return py_int::py_unary_ufunc_result_type(
122+
dtype, degrees_output_typeid_vector);
123+
};
124+
m.def("_degrees_result_type", degrees_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_degrees(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
@@ -86,9 +86,6 @@
8686
#endif
8787

8888
MACRO_1ARG_2TYPES_OP(dpnp_copyto_c, input_elem, q.submit(kernel_func))
89-
MACRO_1ARG_2TYPES_OP(dpnp_degrees_c,
90-
sycl::degrees(input_elem),
91-
q.submit(kernel_func))
9289
MACRO_1ARG_2TYPES_OP(dpnp_sqrt_c,
9390
sycl::sqrt(input_elem),
9491
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
@@ -78,9 +78,6 @@ enum class DPNPFuncName : size_t
7878
parameters */
7979
DPNP_FN_COUNT_NONZERO, /**< Used in numpy.count_nonzero() impl */
8080
DPNP_FN_COV, /**< Used in numpy.cov() impl */
81-
DPNP_FN_DEGREES, /**< Used in numpy.degrees() impl */
82-
DPNP_FN_DEGREES_EXT, /**< Used in numpy.degrees() impl, requires extra
83-
parameters */
8481
DPNP_FN_DOT, /**< Used in numpy.dot() impl */
8582
DPNP_FN_DOT_EXT, /**< Used in numpy.dot() impl, requires extra parameters */
8683
DPNP_FN_EDIFF1D, /**< Used in numpy.ediff1d() impl */

dpnp/backend/kernels/dpnp_krnl_elemwise.cpp

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -307,36 +307,6 @@ static void func_map_init_elemwise_1arg_2type(func_map_t &fmap)
307307
fmap[DPNPFuncName::DPNP_FN_COPYTO_EXT][eft_FLT][eft_FLT] = {
308308
eft_FLT, (void *)dpnp_copyto_c_ext<float, float>};
309309

310-
fmap[DPNPFuncName::DPNP_FN_DEGREES][eft_INT][eft_INT] = {
311-
eft_DBL, (void *)dpnp_degrees_c_default<int32_t, double>};
312-
fmap[DPNPFuncName::DPNP_FN_DEGREES][eft_LNG][eft_LNG] = {
313-
eft_DBL, (void *)dpnp_degrees_c_default<int64_t, double>};
314-
fmap[DPNPFuncName::DPNP_FN_DEGREES][eft_FLT][eft_FLT] = {
315-
eft_FLT, (void *)dpnp_degrees_c_default<float, float>};
316-
fmap[DPNPFuncName::DPNP_FN_DEGREES][eft_DBL][eft_DBL] = {
317-
eft_DBL, (void *)dpnp_degrees_c_default<double, double>};
318-
319-
fmap[DPNPFuncName::DPNP_FN_DEGREES_EXT][eft_INT][eft_INT] = {
320-
get_default_floating_type(),
321-
(void *)dpnp_degrees_c_ext<
322-
int32_t, func_type_map_t::find_type<get_default_floating_type()>>,
323-
get_default_floating_type<std::false_type>(),
324-
(void *)dpnp_degrees_c_ext<
325-
int32_t, func_type_map_t::find_type<
326-
get_default_floating_type<std::false_type>()>>};
327-
fmap[DPNPFuncName::DPNP_FN_DEGREES_EXT][eft_LNG][eft_LNG] = {
328-
get_default_floating_type(),
329-
(void *)dpnp_degrees_c_ext<
330-
int64_t, func_type_map_t::find_type<get_default_floating_type()>>,
331-
get_default_floating_type<std::false_type>(),
332-
(void *)dpnp_degrees_c_ext<
333-
int64_t, func_type_map_t::find_type<
334-
get_default_floating_type<std::false_type>()>>};
335-
fmap[DPNPFuncName::DPNP_FN_DEGREES_EXT][eft_FLT][eft_FLT] = {
336-
eft_FLT, (void *)dpnp_degrees_c_ext<float, float>};
337-
fmap[DPNPFuncName::DPNP_FN_DEGREES_EXT][eft_DBL][eft_DBL] = {
338-
eft_DBL, (void *)dpnp_degrees_c_ext<double, double>};
339-
340310
fmap[DPNPFuncName::DPNP_FN_SQRT][eft_INT][eft_INT] = {
341311
eft_DBL, (void *)dpnp_sqrt_c_default<int32_t, double>};
342312
fmap[DPNPFuncName::DPNP_FN_SQRT][eft_LNG][eft_LNG] = {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 <sycl/sycl.hpp>
29+
30+
namespace dpnp::kernels::degrees
31+
{
32+
template <typename argT, typename resT>
33+
struct DegreesFunctor
34+
{
35+
// is function constant for given argT
36+
using is_constant = typename std::false_type;
37+
// constant value, if constant
38+
// constexpr resT constant_value = resT{};
39+
// is function defined for sycl::vec
40+
using supports_vec = typename std::true_type;
41+
// do both argT and resT support subgroup store/load operation
42+
using supports_sg_loadstore = typename std::true_type;
43+
44+
resT operator()(const argT &x) const
45+
{
46+
return sycl::degrees(x);
47+
}
48+
49+
template <int vec_sz>
50+
sycl::vec<resT, vec_sz> operator()(const sycl::vec<argT, vec_sz> &x) const
51+
{
52+
return sycl::degrees(x);
53+
}
54+
};
55+
} // namespace dpnp::kernels::degrees

dpnp/dpnp_algo/dpnp_algo.pxd

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
3636
DPNP_FN_ALLCLOSE_EXT
3737
DPNP_FN_CHOOSE_EXT
3838
DPNP_FN_CORRELATE_EXT
39-
DPNP_FN_DEGREES_EXT
4039
DPNP_FN_EDIFF1D_EXT
4140
DPNP_FN_ERF_EXT
4241
DPNP_FN_FFT_FFT_EXT
@@ -165,9 +164,3 @@ Logic functions
165164
"""
166165
cpdef dpnp_descriptor dpnp_isclose(dpnp_descriptor input1, dpnp_descriptor input2,
167166
double rtol=*, double atol=*, cpp_bool equal_nan=*)
168-
169-
170-
"""
171-
Trigonometric functions
172-
"""
173-
cpdef dpnp_descriptor dpnp_degrees(dpnp_descriptor array1)

dpnp/dpnp_algo/dpnp_algo_trigonometric.pxi

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,10 @@ and the rest of the library
3636
# NO IMPORTs here. All imports must be placed into main "dpnp_algo.pyx" file
3737

3838
__all__ += [
39-
'dpnp_degrees',
4039
'dpnp_unwrap'
4140
]
4241

4342

44-
cpdef utils.dpnp_descriptor dpnp_degrees(utils.dpnp_descriptor x1):
45-
return call_fptr_1in_1out_strides(DPNP_FN_DEGREES_EXT, x1)
46-
47-
4843
cpdef utils.dpnp_descriptor dpnp_unwrap(utils.dpnp_descriptor array1):
4944

5045
result_type = dpnp.float64

0 commit comments

Comments
 (0)