Skip to content

Commit 295e335

Browse files
committed
use_dpctl_conj_for_dpnp
1 parent 18da200 commit 295e335

File tree

11 files changed

+224
-42
lines changed

11 files changed

+224
-42
lines changed

dpnp/backend/extensions/vm/conj.hpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//*****************************************************************************
2+
// Copyright (c) 2023, 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 <CL/sycl.hpp>
29+
30+
#include "common.hpp"
31+
#include "types_matrix.hpp"
32+
33+
namespace dpnp
34+
{
35+
namespace backend
36+
{
37+
namespace ext
38+
{
39+
namespace vm
40+
{
41+
template <typename T>
42+
sycl::event conj_contig_impl(sycl::queue exec_q,
43+
const std::int64_t n,
44+
const char *in_a,
45+
char *out_y,
46+
const std::vector<sycl::event> &depends)
47+
{
48+
type_utils::validate_type_for_device<T>(exec_q);
49+
50+
const T *a = reinterpret_cast<const T *>(in_a);
51+
T *y = reinterpret_cast<T *>(out_y);
52+
53+
return mkl_vm::conj(exec_q,
54+
n, // number of elements to be calculated
55+
a, // pointer `a` containing input vector of size n
56+
y, // pointer `y` to the output vector of size n
57+
depends);
58+
}
59+
60+
template <typename fnT, typename T>
61+
struct ConjContigFactory
62+
{
63+
fnT get()
64+
{
65+
if constexpr (std::is_same_v<
66+
typename types::ConjOutputType<T>::value_type, void>)
67+
{
68+
return nullptr;
69+
}
70+
else {
71+
return conj_contig_impl<T>;
72+
}
73+
}
74+
};
75+
} // namespace vm
76+
} // namespace ext
77+
} // namespace backend
78+
} // namespace dpnp

dpnp/backend/extensions/vm/types_matrix.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ struct CeilOutputType
8181
dpctl_td_ns::TypeMapResultEntry<T, double, double>,
8282
dpctl_td_ns::TypeMapResultEntry<T, float, float>,
8383
dpctl_td_ns::DefaultResultEntry<void>>::result_type;
84+
}
85+
86+
/**
87+
* @brief A factory to define pairs of supported types for which
88+
* MKL VM library provides support in oneapi::mkl::vm::conj<T> function.
89+
*
90+
* @tparam T Type of input vector `a` and of result vector `y`.
91+
*/
92+
struct ConjOutputType
93+
{
94+
using value_type = typename std::disjunction<
95+
dpctl_td_ns::
96+
TypeMapResultEntry<T, std::complex<double>, std::complex<double>>,
97+
dpctl_td_ns::
98+
TypeMapResultEntry<T, std::complex<float>, std::complex<float>>,
99+
dpctl_td_ns::DefaultResultEntry<void>>::result_type;
84100
};
85101

86102
/**

dpnp/backend/extensions/vm/vm_py.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "ceil.hpp"
3434
#include "common.hpp"
35+
#include "conj.hpp"
3536
#include "cos.hpp"
3637
#include "div.hpp"
3738
#include "floor.hpp"
@@ -53,6 +54,7 @@ static binary_impl_fn_ptr_t div_dispatch_vector[dpctl_td_ns::num_types];
5354
static unary_impl_fn_ptr_t ceil_dispatch_vector[dpctl_td_ns::num_types];
5455
static unary_impl_fn_ptr_t cos_dispatch_vector[dpctl_td_ns::num_types];
5556
static unary_impl_fn_ptr_t floor_dispatch_vector[dpctl_td_ns::num_types];
57+
static unary_impl_fn_ptr_t conj_dispatch_vector[dpctl_td_ns::num_types];
5658
static unary_impl_fn_ptr_t ln_dispatch_vector[dpctl_td_ns::num_types];
5759
static unary_impl_fn_ptr_t sin_dispatch_vector[dpctl_td_ns::num_types];
5860
static unary_impl_fn_ptr_t sqr_dispatch_vector[dpctl_td_ns::num_types];
@@ -150,6 +152,7 @@ PYBIND11_MODULE(_vm_impl, m)
150152
py::arg("sycl_queue"), py::arg("src"), py::arg("dst"));
151153
}
152154

155+
<<<<<<< HEAD
153156
// UnaryUfunc: ==== Floor(x) ====
154157
{
155158
vm_ext::init_ufunc_dispatch_vector<unary_impl_fn_ptr_t,
@@ -174,6 +177,32 @@ PYBIND11_MODULE(_vm_impl, m)
174177
};
175178
m.def("_mkl_floor_to_call", floor_need_to_call_pyapi,
176179
"Check input arguments to answer if `floor` function from "
180+
=======
181+
// UnaryUfunc: ==== Conj(x) ====
182+
{
183+
vm_ext::init_ufunc_dispatch_vector<unary_impl_fn_ptr_t,
184+
vm_ext::ConjContigFactory>(
185+
conj_dispatch_vector);
186+
187+
auto conj_pyapi = [&](sycl::queue exec_q, arrayT src, arrayT dst,
188+
const event_vecT &depends = {}) {
189+
return vm_ext::unary_ufunc(exec_q, src, dst, depends,
190+
conj_dispatch_vector);
191+
};
192+
m.def("_conj", conj_pyapi,
193+
"Call `conj` function from OneMKL VM library to compute "
194+
"conjugate of vector elements",
195+
py::arg("sycl_queue"), py::arg("src"), py::arg("dst"),
196+
py::arg("depends") = py::list());
197+
198+
auto conj_need_to_call_pyapi = [&](sycl::queue exec_q, arrayT src,
199+
arrayT dst) {
200+
return vm_ext::need_to_call_unary_ufunc(exec_q, src, dst,
201+
conj_dispatch_vector);
202+
};
203+
m.def("_mkl_conj_to_call", conj_need_to_call_pyapi,
204+
"Check input arguments to answer if `conj` function from "
205+
>>>>>>> use_dpctl_conj_for_dpnp
177206
"OneMKL VM library can be used",
178207
py::arg("sycl_queue"), py::arg("src"), py::arg("dst"));
179208
}

dpnp/backend/include/dpnp_iface_fptr.hpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,13 @@ enum class DPNPFuncName : size_t
114114
*/
115115
DPNP_FN_CEIL, /**< Used in numpy.ceil() impl */
116116
DPNP_FN_CHOLESKY, /**< Used in numpy.linalg.cholesky() impl */
117-
DPNP_FN_CHOLESKY_EXT, /**< Used in numpy.linalg.cholesky() impl, requires
118-
extra parameters */
119-
DPNP_FN_CONJIGUATE, /**< Used in numpy.conjugate() impl */
120-
DPNP_FN_CONJIGUATE_EXT, /**< Used in numpy.conjugate() impl, requires extra
121-
parameters */
122-
DPNP_FN_CHOOSE, /**< Used in numpy.choose() impl */
123-
DPNP_FN_CHOOSE_EXT, /**< Used in numpy.choose() impl, requires extra
124-
parameters */
125-
DPNP_FN_COPY, /**< Used in numpy.copy() impl */
117+
DPNP_FN_CHOLESKY_EXT, /**< Used in numpy.linalg.cholesky() impl, requires
118+
extra parameters */
119+
DPNP_FN_CONJUGATE, /**< Used in numpy.conjugate() impl */
120+
DPNP_FN_CHOOSE, /**< Used in numpy.choose() impl */
121+
DPNP_FN_CHOOSE_EXT, /**< Used in numpy.choose() impl, requires extra
122+
parameters */
123+
DPNP_FN_COPY, /**< Used in numpy.copy() impl */
126124
DPNP_FN_COPY_EXT, /**< Used in numpy.copy() impl, requires extra parameters
127125
*/
128126
DPNP_FN_COPYSIGN, /**< Used in numpy.copysign() impl */

dpnp/backend/kernels/dpnp_krnl_elemwise.cpp

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,28 +1002,17 @@ constexpr auto dispatch_fmod_op(T elem1, T elem2)
10021002

10031003
static void func_map_init_elemwise_1arg_1type(func_map_t &fmap)
10041004
{
1005-
fmap[DPNPFuncName::DPNP_FN_CONJIGUATE][eft_INT][eft_INT] = {
1005+
fmap[DPNPFuncName::DPNP_FN_CONJUGATE][eft_INT][eft_INT] = {
10061006
eft_INT, (void *)dpnp_copy_c_default<int32_t>};
1007-
fmap[DPNPFuncName::DPNP_FN_CONJIGUATE][eft_LNG][eft_LNG] = {
1007+
fmap[DPNPFuncName::DPNP_FN_CONJUGATE][eft_LNG][eft_LNG] = {
10081008
eft_LNG, (void *)dpnp_copy_c_default<int64_t>};
1009-
fmap[DPNPFuncName::DPNP_FN_CONJIGUATE][eft_FLT][eft_FLT] = {
1009+
fmap[DPNPFuncName::DPNP_FN_CONJUGATE][eft_FLT][eft_FLT] = {
10101010
eft_FLT, (void *)dpnp_copy_c_default<float>};
1011-
fmap[DPNPFuncName::DPNP_FN_CONJIGUATE][eft_DBL][eft_DBL] = {
1011+
fmap[DPNPFuncName::DPNP_FN_CONJUGATE][eft_DBL][eft_DBL] = {
10121012
eft_DBL, (void *)dpnp_copy_c_default<double>};
1013-
fmap[DPNPFuncName::DPNP_FN_CONJIGUATE][eft_C128][eft_C128] = {
1013+
fmap[DPNPFuncName::DPNP_FN_CONJUGATE][eft_C128][eft_C128] = {
10141014
eft_C128, (void *)dpnp_conjugate_c_default<std::complex<double>>};
10151015

1016-
fmap[DPNPFuncName::DPNP_FN_CONJIGUATE_EXT][eft_INT][eft_INT] = {
1017-
eft_INT, (void *)dpnp_copy_c_ext<int32_t>};
1018-
fmap[DPNPFuncName::DPNP_FN_CONJIGUATE_EXT][eft_LNG][eft_LNG] = {
1019-
eft_LNG, (void *)dpnp_copy_c_ext<int64_t>};
1020-
fmap[DPNPFuncName::DPNP_FN_CONJIGUATE_EXT][eft_FLT][eft_FLT] = {
1021-
eft_FLT, (void *)dpnp_copy_c_ext<float>};
1022-
fmap[DPNPFuncName::DPNP_FN_CONJIGUATE_EXT][eft_DBL][eft_DBL] = {
1023-
eft_DBL, (void *)dpnp_copy_c_ext<double>};
1024-
fmap[DPNPFuncName::DPNP_FN_CONJIGUATE_EXT][eft_C128][eft_C128] = {
1025-
eft_C128, (void *)dpnp_conjugate_c_ext<std::complex<double>>};
1026-
10271016
fmap[DPNPFuncName::DPNP_FN_COPY][eft_BLN][eft_BLN] = {
10281017
eft_BLN, (void *)dpnp_copy_c_default<bool>};
10291018
fmap[DPNPFuncName::DPNP_FN_COPY][eft_INT][eft_INT] = {

dpnp/dpnp_algo/dpnp_algo.pxd

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
6868
DPNP_FN_CHOLESKY_EXT
6969
DPNP_FN_CHOOSE
7070
DPNP_FN_CHOOSE_EXT
71-
DPNP_FN_CONJIGUATE
72-
DPNP_FN_CONJIGUATE_EXT
7371
DPNP_FN_COPY
7472
DPNP_FN_COPY_EXT
7573
DPNP_FN_COPYSIGN

dpnp/dpnp_algo/dpnp_algo_mathematical.pxi

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ __all__ += [
3939
"dpnp_absolute",
4040
"dpnp_arctan2",
4141
"dpnp_around",
42-
"dpnp_conjugate",
4342
"dpnp_copysign",
4443
"dpnp_cross",
4544
"dpnp_cumprod",
@@ -155,10 +154,6 @@ cpdef utils.dpnp_descriptor dpnp_around(utils.dpnp_descriptor x1, int decimals):
155154
return result
156155

157156

158-
cpdef utils.dpnp_descriptor dpnp_conjugate(utils.dpnp_descriptor x1):
159-
return call_fptr_1in_1out_strides(DPNP_FN_CONJIGUATE_EXT, x1)
160-
161-
162157
cpdef utils.dpnp_descriptor dpnp_copysign(utils.dpnp_descriptor x1_obj,
163158
utils.dpnp_descriptor x2_obj,
164159
object dtype=None,

dpnp/dpnp_algo/dpnp_elementwise_common.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"dpnp_bitwise_or",
4848
"dpnp_bitwise_xor",
4949
"dpnp_ceil",
50+
"dpnp_conj",
5051
"dpnp_cos",
5152
"dpnp_divide",
5253
"dpnp_equal",
@@ -423,6 +424,58 @@ def _call_cos(src, dst, sycl_queue, depends=None):
423424
return dpnp_array._create_from_usm_ndarray(res_usm)
424425

425426

427+
_conj_docstring = """
428+
conj(x, out=None, order='K')
429+
430+
Computes conjugate for each element `x_i` for input array `x`.
431+
432+
Args:
433+
x (dpnp.ndarray):
434+
Input array, expected to have numeric data type.
435+
out ({None, dpnp.ndarray}, optional):
436+
Output array to populate. Array must have the correct
437+
shape and the expected data type.
438+
order ("C","F","A","K", optional): memory layout of the new
439+
output array, if parameter `out` is `None`.
440+
Default: "K".
441+
Return:
442+
dpnp.ndarray:
443+
An array containing the element-wise conjugate.
444+
The returned array has the same data type as `x`.
445+
"""
446+
447+
448+
def _call_conj(src, dst, sycl_queue, depends=None):
449+
"""A callback to register in UnaryElementwiseFunc class of dpctl.tensor"""
450+
451+
if depends is None:
452+
depends = []
453+
454+
if vmi._mkl_conj_to_call(sycl_queue, src, dst):
455+
# call pybind11 extension for conj() function from OneMKL VM
456+
return vmi._conj(sycl_queue, src, dst, depends)
457+
return ti._conj(src, dst, sycl_queue, depends)
458+
459+
460+
conj_func = UnaryElementwiseFunc(
461+
"conj", ti._conj_result_type, _call_conj, _conj_docstring
462+
)
463+
464+
465+
def dpnp_conj(x, out=None, order="K"):
466+
"""
467+
Invokes conj() function from pybind11 extension of OneMKL VM if possible.
468+
469+
Otherwise fully relies on dpctl.tensor implementation for conj() function.
470+
"""
471+
# dpctl.tensor only works with usm_ndarray
472+
x1_usm = dpnp.get_usm_ndarray(x)
473+
out_usm = None if out is None else dpnp.get_usm_ndarray(out)
474+
475+
res_usm = conj_func(x1_usm, out=out_usm, order=order)
476+
return dpnp_array._create_from_usm_ndarray(res_usm)
477+
478+
426479
_divide_docstring_ = """
427480
divide(x1, x2, out=None, order="K")
428481

dpnp/dpnp_iface_mathematical.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
check_nd_call_func,
5353
dpnp_add,
5454
dpnp_ceil,
55+
dpnp_conj,
5556
dpnp_divide,
5657
dpnp_floor,
5758
dpnp_floor_divide,
@@ -362,7 +363,17 @@ def ceil(
362363
)
363364

364365

365-
def conjugate(x1, **kwargs):
366+
def conjugate(
367+
x,
368+
/,
369+
out=None,
370+
*,
371+
order="K",
372+
where=True,
373+
dtype=None,
374+
subok=True,
375+
**kwargs,
376+
):
366377
"""
367378
Return the complex conjugate, element-wise.
368379
@@ -371,6 +382,18 @@ def conjugate(x1, **kwargs):
371382
372383
For full documentation refer to :obj:`numpy.conjugate`.
373384
385+
Returns
386+
-------
387+
out : dpnp.ndarray
388+
The conjugate of each element of `x`.
389+
390+
Limitations
391+
-----------
392+
Parameters `x` is only supported as either :class:`dpnp.ndarray` or :class:`dpctl.tensor.usm_ndarray`.
393+
Parameters `where`, `dtype` and `subok` are supported with their default values.
394+
Otherwise the function will be executed sequentially on CPU.
395+
Input array data types are limited by supported DPNP :ref:`Data types`.
396+
374397
Examples
375398
--------
376399
>>> import dpnp as np
@@ -384,13 +407,17 @@ def conjugate(x1, **kwargs):
384407
385408
"""
386409

387-
x1_desc = dpnp.get_dpnp_descriptor(
388-
x1, copy_when_strides=False, copy_when_nondefault_queue=False
410+
return check_nd_call_func(
411+
numpy.conjugate,
412+
dpnp_conj,
413+
x,
414+
out=out,
415+
where=where,
416+
order=order,
417+
dtype=dtype,
418+
subok=subok,
419+
**kwargs,
389420
)
390-
if x1_desc and not kwargs:
391-
return dpnp_conjugate(x1_desc).get_pyobj()
392-
393-
return call_origin(numpy.conjugate, x1, **kwargs)
394421

395422

396423
conj = conjugate

tests/skipped_tests_gpu.tbl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ tests/test_random.py::TestPermutationsTestShuffle::test_no_miss_numbers[int64]
1212
tests/test_random.py::TestPermutationsTestShuffle::test_shuffle1[lambda x: dpnp.array([])]
1313
tests/test_random.py::TestPermutationsTestShuffle::test_shuffle1[lambda x: dpnp.astype(dpnp.asarray(x), dpnp.float32)]
1414

15-
tests/test_sycl_queue.py::test_1in_1out[opencl:gpu:0-conjugate-data2]
1615
tests/test_sycl_queue.py::test_1in_1out[opencl:gpu:0-copy-data3]
1716
tests/test_sycl_queue.py::test_1in_1out[opencl:gpu:0-cumprod-data4]
1817
tests/test_sycl_queue.py::test_1in_1out[opencl:gpu:0-cumsum-data5]
1918
tests/test_sycl_queue.py::test_1in_1out[opencl:gpu:0-ediff1d-data7]
2019
tests/test_sycl_queue.py::test_1in_1out[opencl:gpu:0-fabs-data8]
2120

22-
tests/test_sycl_queue.py::test_1in_1out[level_zero:gpu:0-conjugate-data2]
2321
tests/test_sycl_queue.py::test_1in_1out[level_zero:gpu:0-copy-data3]
2422
tests/test_sycl_queue.py::test_1in_1out[level_zero:gpu:0-cumprod-data4]
2523
tests/test_sycl_queue.py::test_1in_1out[level_zero:gpu:0-cumsum-data5]

0 commit comments

Comments
 (0)