Skip to content
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

Update dpnp.power using dpctl and OneMKL implementations #1476

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
96b9759
Reuse dpctl.tensor.pow for dpnp.power
vlad-perevezentsev Jul 13, 2023
a3d04ba
Add pow call from OneMKL by pybind11 extension
vlad-perevezentsev Jul 13, 2023
fdddcee
Update all tests for dpnp.power
vlad-perevezentsev Jul 13, 2023
01fbe4c
Fix return type in all docstrings
vlad-perevezentsev Jul 14, 2023
d7e874e
Remove _check_nd_call in dpnp_iface_mathematical
vlad-perevezentsev Jul 14, 2023
206ae7e
Fix remarks
vlad-perevezentsev Jul 14, 2023
ba6f7d2
Fix tests for dpnp.power
vlad-perevezentsev Jul 14, 2023
302e7dc
Update examples for dpnp.power
vlad-perevezentsev Jul 14, 2023
715a858
Fix test_power with complex128 on CPU
vlad-perevezentsev Jul 14, 2023
f6b843c
Merge master into reuse_dpctl_pow
vlad-perevezentsev Jul 24, 2023
09df135
Fix test_out_dtypes for windows
vlad-perevezentsev Jul 25, 2023
4e8d275
Merge master into reuse_dpctl_pow
vlad-perevezentsev Jul 25, 2023
0097431
Merge master into reuse_dpctl_pow
vlad-perevezentsev Aug 23, 2023
aa33e23
Update dpnp_power and use OneMKL only on Linux for it
vlad-perevezentsev Aug 23, 2023
e9d3ba3
Restore deleted funcs in test_arithmetic
vlad-perevezentsev Aug 23, 2023
e46dd0b
Update tests for dpnp.power
vlad-perevezentsev Aug 23, 2023
816bd93
Update tests for dpnp.power
vlad-perevezentsev Aug 25, 2023
5590ceb
Merge master into reuse_dpctl_pow
vlad-perevezentsev Aug 25, 2023
a728e80
Skip test_out_2in_1out-power on gpu
vlad-perevezentsev Aug 25, 2023
374fc96
apply review remarks
vlad-perevezentsev Aug 28, 2023
281fc21
Remove dpnp_init_val
vlad-perevezentsev Aug 28, 2023
fbacbd6
Merge master into reuse_dpctl_pow
vlad-perevezentsev Aug 28, 2023
4baca36
Merge branch 'master' into reuse_dpctl_pow
antonwolfy Aug 29, 2023
14b9ba9
A small change for test_power
vlad-perevezentsev Aug 29, 2023
9f12282
Skip test_copy
vlad-perevezentsev Aug 29, 2023
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
81 changes: 81 additions & 0 deletions dpnp/backend/extensions/vm/pow.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//*****************************************************************************
// Copyright (c) 2023, 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 <CL/sycl.hpp>

#include "common.hpp"
#include "types_matrix.hpp"

namespace dpnp
{
namespace backend
{
namespace ext
{
namespace vm
{
template <typename T>
sycl::event pow_contig_impl(sycl::queue exec_q,
const std::int64_t n,
const char *in_a,
const char *in_b,
char *out_y,
const std::vector<sycl::event> &depends)
{
type_utils::validate_type_for_device<T>(exec_q);

const T *a = reinterpret_cast<const T *>(in_a);
const T *b = reinterpret_cast<const T *>(in_b);
T *y = reinterpret_cast<T *>(out_y);

return mkl_vm::pow(exec_q,
n, // number of elements to be calculated
a, // pointer `a` containing 1st input vector of size n
b, // pointer `b` containing 2nd input vector of size n
y, // pointer `y` to the output vector of size n
depends);
}

template <typename fnT, typename T>
struct PowContigFactory
{
fnT get()
{
if constexpr (std::is_same_v<
typename types::PowOutputType<T>::value_type, void>)
{
return nullptr;
}
else {
return pow_contig_impl<T>;
}
}
};
} // namespace vm
} // namespace ext
} // namespace backend
} // namespace dpnp
25 changes: 25 additions & 0 deletions dpnp/backend/extensions/vm/types_matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,31 @@ struct LnOutputType
dpctl_td_ns::DefaultResultEntry<void>>::result_type;
};

/**
* @brief A factory to define pairs of supported types for which
* MKL VM library provides support in oneapi::mkl::vm::pow<T> function.
*
* @tparam T Type of input vectors `a` and `b` and of result vector `y`.
*/
template <typename T>
struct PowOutputType
{
using value_type = typename std::disjunction<
dpctl_td_ns::BinaryTypeMapResultEntry<T,
std::complex<double>,
T,
std::complex<double>,
std::complex<double>>,
dpctl_td_ns::BinaryTypeMapResultEntry<T,
std::complex<float>,
T,
std::complex<float>,
std::complex<float>>,
dpctl_td_ns::BinaryTypeMapResultEntry<T, double, T, double, double>,
dpctl_td_ns::BinaryTypeMapResultEntry<T, float, T, float, float>,
dpctl_td_ns::DefaultResultEntry<void>>::result_type;
};

/**
* @brief A factory to define pairs of supported types for which
* MKL VM library provides support in oneapi::mkl::vm::sin<T> function.
Expand Down
32 changes: 32 additions & 0 deletions dpnp/backend/extensions/vm/vm_py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "cos.hpp"
#include "div.hpp"
#include "ln.hpp"
#include "pow.hpp"
#include "sin.hpp"
#include "sqr.hpp"
#include "sqrt.hpp"
Expand All @@ -46,6 +47,7 @@ using vm_ext::binary_impl_fn_ptr_t;
using vm_ext::unary_impl_fn_ptr_t;

static binary_impl_fn_ptr_t div_dispatch_vector[dpctl_td_ns::num_types];
static binary_impl_fn_ptr_t pow_dispatch_vector[dpctl_td_ns::num_types];

static unary_impl_fn_ptr_t cos_dispatch_vector[dpctl_td_ns::num_types];
static unary_impl_fn_ptr_t ln_dispatch_vector[dpctl_td_ns::num_types];
Expand Down Expand Up @@ -144,6 +146,36 @@ PYBIND11_MODULE(_vm_impl, m)
py::arg("sycl_queue"), py::arg("src"), py::arg("dst"));
}

// BinaryUfunc: ==== Pow(x1, x2) ====
{
vm_ext::init_ufunc_dispatch_vector<binary_impl_fn_ptr_t,
vm_ext::PowContigFactory>(
pow_dispatch_vector);

auto pow_pyapi = [&](sycl::queue exec_q, arrayT src1, arrayT src2,
arrayT dst, const event_vecT &depends = {}) {
return vm_ext::binary_ufunc(exec_q, src1, src2, dst, depends,
pow_dispatch_vector);
};
m.def("_pow", pow_pyapi,
"Call `pow` function from OneMKL VM library to performs element "
"by element exponentiation of vector `src1` raised to the power "
"of vector `src2` to resulting vector `dst`",
py::arg("sycl_queue"), py::arg("src1"), py::arg("src2"),
py::arg("dst"), py::arg("depends") = py::list());

auto pow_need_to_call_pyapi = [&](sycl::queue exec_q, arrayT src1,
arrayT src2, arrayT dst) {
return vm_ext::need_to_call_binary_ufunc(exec_q, src1, src2, dst,
pow_dispatch_vector);
};
m.def("_mkl_pow_to_call", pow_need_to_call_pyapi,
"Check input arguments to answer if `pow` function from "
"OneMKL VM library can be used",
py::arg("sycl_queue"), py::arg("src1"), py::arg("src2"),
py::arg("dst"));
}

// UnaryUfunc: ==== Sin(x) ====
{
vm_ext::init_ufunc_dispatch_vector<unary_impl_fn_ptr_t,
Expand Down
2 changes: 0 additions & 2 deletions dpnp/backend/include/dpnp_iface_fptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,6 @@ enum class DPNPFuncName : size_t
parameters */
DPNP_FN_PLACE, /**< Used in numpy.place() impl */
DPNP_FN_POWER, /**< Used in numpy.power() impl */
DPNP_FN_POWER_EXT, /**< Used in numpy.power() impl, requires extra
parameters */
DPNP_FN_PROD, /**< Used in numpy.prod() impl */
DPNP_FN_PROD_EXT, /**< Used in numpy.prod() impl, requires extra parameters
*/
Expand Down
7 changes: 0 additions & 7 deletions dpnp/backend/kernels/dpnp_krnl_elemwise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1628,13 +1628,6 @@ static void func_map_elemwise_2arg_3type_core(func_map_t &fmap)
func_type_map_t::find_type<FT1>,
func_type_map_t::find_type<FTs>>}),
...);
((fmap[DPNPFuncName::DPNP_FN_POWER_EXT][FT1][FTs] =
{populate_func_types<FT1, FTs>(),
(void *)dpnp_power_c_ext<
func_type_map_t::find_type<populate_func_types<FT1, FTs>()>,
func_type_map_t::find_type<FT1>,
func_type_map_t::find_type<FTs>>}),
...);
((fmap[DPNPFuncName::DPNP_FN_SUBTRACT_EXT][FT1][FTs] =
{populate_func_types<FT1, FTs>(),
(void *)dpnp_subtract_c_ext<
Expand Down
4 changes: 0 additions & 4 deletions dpnp/dpnp_algo/dpnp_algo.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,6 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
DPNP_FN_PARTITION
DPNP_FN_PARTITION_EXT
DPNP_FN_PLACE
DPNP_FN_POWER
DPNP_FN_POWER_EXT
DPNP_FN_PROD
DPNP_FN_PROD_EXT
DPNP_FN_PTP
Expand Down Expand Up @@ -490,8 +488,6 @@ cpdef dpnp_descriptor dpnp_maximum(dpnp_descriptor x1_obj, dpnp_descriptor x2_ob
cpdef dpnp_descriptor dpnp_minimum(dpnp_descriptor x1_obj, dpnp_descriptor x2_obj, object dtype=*,
dpnp_descriptor out=*, object where=*)
cpdef dpnp_descriptor dpnp_negative(dpnp_descriptor array1)
cpdef dpnp_descriptor dpnp_power(dpnp_descriptor x1_obj, dpnp_descriptor x2_obj, object dtype=*,
vlad-perevezentsev marked this conversation as resolved.
Show resolved Hide resolved
vlad-perevezentsev marked this conversation as resolved.
Show resolved Hide resolved
dpnp_descriptor out=*, object where=*)
cpdef dpnp_descriptor dpnp_remainder(dpnp_descriptor x1_obj, dpnp_descriptor x2_obj, object dtype=*,
dpnp_descriptor out=*, object where=*)

Expand Down
9 changes: 0 additions & 9 deletions dpnp/dpnp_algo/dpnp_algo_mathematical.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ __all__ += [
"dpnp_nanprod",
"dpnp_nansum",
"dpnp_negative",
"dpnp_power",
"dpnp_prod",
"dpnp_remainder",
"dpnp_sign",
Expand Down Expand Up @@ -476,14 +475,6 @@ cpdef utils.dpnp_descriptor dpnp_negative(dpnp_descriptor x1):
return call_fptr_1in_1out_strides(DPNP_FN_NEGATIVE_EXT, x1)


cpdef utils.dpnp_descriptor dpnp_power(utils.dpnp_descriptor x1_obj,
utils.dpnp_descriptor x2_obj,
object dtype=None,
utils.dpnp_descriptor out=None,
object where=True):
return call_fptr_2in_1out_strides(DPNP_FN_POWER_EXT, x1_obj, x2_obj, dtype, out, where, func_name="power")


cpdef utils.dpnp_descriptor dpnp_prod(utils.dpnp_descriptor x1,
object axis=None,
object dtype=None,
Expand Down
63 changes: 59 additions & 4 deletions dpnp/dpnp_algo/dpnp_elementwise_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"dpnp_logical_xor",
"dpnp_multiply",
"dpnp_not_equal",
"dpnp_power",
"dpnp_sin",
"dpnp_sqrt",
"dpnp_square",
Expand Down Expand Up @@ -613,7 +614,7 @@ def _call_log(src, dst, sycl_queue, depends=None):
Memory layout of the newly output array, if parameter `out` is `None`.
Default: "K".
Returns:
usm_narray:
dpnp.ndarray:
An array containing the element-wise logical AND results.
"""

Expand Down Expand Up @@ -649,7 +650,7 @@ def dpnp_logical_and(x1, x2, out=None, order="K"):
output array, if parameter `out` is `None`.
Default: "K".
Return:
usm_ndarray:
dpnp.ndarray:
An array containing the element-wise logical NOT results.
"""

Expand Down Expand Up @@ -689,7 +690,7 @@ def dpnp_logical_not(x, out=None, order="K"):
Memory layout of the newly output array, if parameter `out` is `None`.
Default: "K".
Returns:
usm_narray:
dpnp.ndarray:
An array containing the element-wise logical OR results.
"""

Expand Down Expand Up @@ -730,7 +731,7 @@ def dpnp_logical_or(x1, x2, out=None, order="K"):
Memory layout of the newly output array, if parameter `out` is `None`.
Default: "K".
Returns:
usm_narray:
dpnp.ndarray:
An array containing the element-wise logical XOR results.
"""

Expand Down Expand Up @@ -844,6 +845,60 @@ def dpnp_not_equal(x1, x2, out=None, order="K"):
return dpnp_array._create_from_usm_ndarray(res_usm)


_power_docstring_ = """
vlad-perevezentsev marked this conversation as resolved.
Show resolved Hide resolved
power(x1, x2, out=None, order="K")
Calculates `x1_i` raised to `x2_i` for each element `x1_i` of the input array
`x1` with the respective element `x2_i` of the input array `x2`.
Args:
x1 (dpnp.ndarray):
First input array, expected to have numeric data type.
x2 (dpnp.ndarray):
Second input array, also expected to have numeric data type.
out ({None, dpnp.ndarray}, optional):
Output array to populate. Array must have the correct
shape and the expected data type.
order ("C","F","A","K", None, optional):
Output array, if parameter `out` is `None`.
Default: "K".
Returns:
dpnp.ndarray:
An array containing the result of element-wise of raising each element
to a specified power.
The data type of the returned array is determined by the Type Promotion Rules.
"""


def dpnp_power(x1, x2, out=None, order="K"):
"""
Invokes pow() function from pybind11 extension of OneMKL VM if possible.

Otherwise fully relies on dpctl.tensor implementation for pow() function.

"""

def _call_pow(src1, src2, dst, sycl_queue, depends=None):
"""A callback to register in BinaryElementwiseFunc class of dpctl.tensor"""

if depends is None:
depends = []

if vmi._mkl_pow_to_call(sycl_queue, src1, src2, dst):
# call pybind11 extension for pow() function from OneMKL VM
return vmi._pow(sycl_queue, src1, src2, dst, depends)
return ti._pow(src1, src2, dst, sycl_queue, depends)

# dpctl.tensor only works with usm_ndarray or scalar
vlad-perevezentsev marked this conversation as resolved.
Show resolved Hide resolved
x1_usm_or_scalar = dpnp.get_usm_ndarray_or_scalar(x1)
x2_usm_or_scalar = dpnp.get_usm_ndarray_or_scalar(x2)
out_usm = None if out is None else dpnp.get_usm_ndarray(out)

func = BinaryElementwiseFunc(
"pow", ti._pow_result_type, _call_pow, _power_docstring_
)
res_usm = func(x1_usm_or_scalar, x2_usm_or_scalar, out=out_usm, order=order)
return dpnp_array._create_from_usm_ndarray(res_usm)


_sin_docstring = """
sin(x, out=None, order='K')
Computes sine for each element `x_i` of input array `x`.
Expand Down
Loading