Skip to content

impl_minimum_maximum_elementwise_funcs #1316

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

Merged
merged 2 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions dpctl/tensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@
logical_not,
logical_or,
logical_xor,
maximum,
minimum,
multiply,
negative,
not_equal,
Expand Down Expand Up @@ -274,6 +276,8 @@
"log1p",
"log2",
"log10",
"maximum",
"minimum",
"multiply",
"negative",
"not_equal",
Expand Down
66 changes: 66 additions & 0 deletions dpctl/tensor/_elementwise_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,66 @@
_logical_xor_docstring_,
)

# B??: ==== MAXIMUM (x1, x2)
_maximum_docstring_ = """
maximum(x1, x2, out=None, order='K')

Compares two input arrays `x1` and `x2` and returns
a new array containing the element-wise maxima.

Args:
x1 (usm_ndarray):
First input array, expected to have numeric data type.
x2 (usm_ndarray):
Second input array, also expected to have numeric data type.
out ({None, usm_ndarray}, optional):
Output array to populate.
Array have the correct shape and the expected data type.
order ("C","F","A","K", optional):
Memory layout of the newly output array, if parameter `out` is `None`.
Default: "K".
Returns:
usm_narray:
An array containing the element-wise maxima. The data type of
the returned array is determined by the Type Promotion Rules.
"""
maximum = BinaryElementwiseFunc(
"maximum",
ti._maximum_result_type,
ti._maximum,
_maximum_docstring_,
)

# B??: ==== MINIMUM (x1, x2)
_minimum_docstring_ = """
minimum(x1, x2, out=None, order='K')

Compares two input arrays `x1` and `x2` and returns
a new array containing the element-wise minima.

Args:
x1 (usm_ndarray):
First input array, expected to have numeric data type.
x2 (usm_ndarray):
Second input array, also expected to have numeric data type.
out ({None, usm_ndarray}, optional):
Output array to populate.
Array have the correct shape and the expected data type.
order ("C","F","A","K", optional):
Memory layout of the newly output array, if parameter `out` is `None`.
Default: "K".
Returns:
usm_narray:
An array containing the element-wise minima. The data type of
the returned array is determined by the Type Promotion Rules.
"""
minimum = BinaryElementwiseFunc(
"minimum",
ti._minimum_result_type,
ti._minimum,
_minimum_docstring_,
)

# B19: ==== MULTIPLY (x1, x2)
_multiply_docstring_ = """
multiply(x1, x2, out=None, order='K')
Expand Down Expand Up @@ -1369,6 +1429,12 @@
First input array, expected to have a real-valued data type.
x2 (usm_ndarray):
Second input array, also expected to have a real-valued data type.
out ({None, usm_ndarray}, optional):
Output array to populate.
Array have the correct shape and the expected data type.
order ("C","F","A","K", optional):
Memory layout of the newly output array, if parameter `out` is `None`.
Default: "K".
Returns:
usm_ndarray:
an array containing the element-wise remainders. The data type of
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
//=== maximum.hpp - Binary function MAXIMUM ------ *-C++-*--/===//
//
// Data Parallel Control (dpctl)
//
// Copyright 2020-2023 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//===---------------------------------------------------------------------===//
///
/// \file
/// This file defines kernels for elementwise evaluation of MAXIMUM(x1, x2)
/// function.
//===---------------------------------------------------------------------===//

#pragma once
#include <CL/sycl.hpp>
#include <cstddef>
#include <cstdint>
#include <type_traits>

#include "utils/offset_utils.hpp"
#include "utils/type_dispatch.hpp"
#include "utils/type_utils.hpp"

#include "kernels/elementwise_functions/common.hpp"
#include <pybind11/pybind11.h>

namespace dpctl
{
namespace tensor
{
namespace kernels
{
namespace maximum
{

namespace py = pybind11;
namespace td_ns = dpctl::tensor::type_dispatch;
namespace tu_ns = dpctl::tensor::type_utils;

template <typename argT1, typename argT2, typename resT> struct MaximumFunctor
{

using supports_sg_loadstore = std::negation<
std::disjunction<tu_ns::is_complex<argT1>, tu_ns::is_complex<argT2>>>;
using supports_vec = std::conjunction<
std::is_same<argT1, argT2>,
std::negation<std::disjunction<tu_ns::is_complex<argT1>,
tu_ns::is_complex<argT2>>>>;

resT operator()(const argT1 &in1, const argT2 &in2)
{
if constexpr (tu_ns::is_complex<argT1>::value ||
tu_ns::is_complex<argT2>::value)
{
static_assert(std::is_same_v<argT1, argT2>);
using realT = typename argT1::value_type;
realT real1 = std::real(in1);
realT real2 = std::real(in2);
realT imag1 = std::imag(in1);
realT imag2 = std::imag(in2);

bool gt = (real1 == real2) ? (imag1 > imag2)
: (real1 > real2 && !std::isnan(imag1) &&
!std::isnan(imag2));
return (std::isnan(real1) || std::isnan(imag1) || gt) ? in1 : in2;
}
else if constexpr (std::is_floating_point_v<argT1> ||
std::is_same_v<argT1, sycl::half>)
return (std::isnan(in1) || in1 > in2) ? in1 : in2;
else
return (in1 > in2) ? in1 : in2;
}

template <int vec_sz>
sycl::vec<resT, vec_sz> operator()(const sycl::vec<argT1, vec_sz> &in1,
const sycl::vec<argT2, vec_sz> &in2)
{
sycl::vec<resT, vec_sz> res;
#pragma unroll
for (int i = 0; i < vec_sz; ++i) {
if constexpr (std::is_floating_point_v<argT1>)
res[i] =
(sycl::isnan(in1[i]) || in1[i] > in2[i]) ? in1[i] : in2[i];
else
res[i] = (in1[i] > in2[i]) ? in1[i] : in2[i];
}
return res;
}
};

template <typename argT1,
typename argT2,
typename resT,
unsigned int vec_sz = 4,
unsigned int n_vecs = 2>
using MaximumContigFunctor =
elementwise_common::BinaryContigFunctor<argT1,
argT2,
resT,
MaximumFunctor<argT1, argT2, resT>,
vec_sz,
n_vecs>;

template <typename argT1, typename argT2, typename resT, typename IndexerT>
using MaximumStridedFunctor = elementwise_common::BinaryStridedFunctor<
argT1,
argT2,
resT,
IndexerT,
MaximumFunctor<argT1, argT2, resT>>;

template <typename T1, typename T2> struct MaximumOutputType
{
using value_type = typename std::disjunction< // disjunction is C++17
// feature, supported by DPC++
td_ns::BinaryTypeMapResultEntry<T1, bool, T2, bool, bool>,
td_ns::BinaryTypeMapResultEntry<T1,
std::uint8_t,
T2,
std::uint8_t,
std::uint8_t>,
td_ns::BinaryTypeMapResultEntry<T1,
std::int8_t,
T2,
std::int8_t,
std::int8_t>,
td_ns::BinaryTypeMapResultEntry<T1,
std::uint16_t,
T2,
std::uint16_t,
std::uint16_t>,
td_ns::BinaryTypeMapResultEntry<T1,
std::int16_t,
T2,
std::int16_t,
std::int16_t>,
td_ns::BinaryTypeMapResultEntry<T1,
std::uint32_t,
T2,
std::uint32_t,
std::uint32_t>,
td_ns::BinaryTypeMapResultEntry<T1,
std::int32_t,
T2,
std::int32_t,
std::int32_t>,
td_ns::BinaryTypeMapResultEntry<T1,
std::uint64_t,
T2,
std::uint64_t,
std::uint64_t>,
td_ns::BinaryTypeMapResultEntry<T1,
std::int64_t,
T2,
std::int64_t,
std::int64_t>,
td_ns::BinaryTypeMapResultEntry<T1,
sycl::half,
T2,
sycl::half,
sycl::half>,
td_ns::BinaryTypeMapResultEntry<T1, float, T2, float, float>,
td_ns::BinaryTypeMapResultEntry<T1, double, T2, double, double>,
td_ns::BinaryTypeMapResultEntry<T1,
std::complex<float>,
T2,
std::complex<float>,
std::complex<float>>,
td_ns::BinaryTypeMapResultEntry<T1,
std::complex<double>,
T2,
std::complex<double>,
std::complex<double>>,
td_ns::DefaultResultEntry<void>>::result_type;
};

template <typename argT1,
typename argT2,
typename resT,
unsigned int vec_sz,
unsigned int n_vecs>
class maximum_contig_kernel;

template <typename argTy1, typename argTy2>
sycl::event maximum_contig_impl(sycl::queue exec_q,
size_t nelems,
const char *arg1_p,
py::ssize_t arg1_offset,
const char *arg2_p,
py::ssize_t arg2_offset,
char *res_p,
py::ssize_t res_offset,
const std::vector<sycl::event> &depends = {})
{
return elementwise_common::binary_contig_impl<
argTy1, argTy2, MaximumOutputType, MaximumContigFunctor,
maximum_contig_kernel>(exec_q, nelems, arg1_p, arg1_offset, arg2_p,
arg2_offset, res_p, res_offset, depends);
}

template <typename fnT, typename T1, typename T2> struct MaximumContigFactory
{
fnT get()
{
if constexpr (std::is_same_v<
typename MaximumOutputType<T1, T2>::value_type, void>)
{
fnT fn = nullptr;
return fn;
}
else {
fnT fn = maximum_contig_impl<T1, T2>;
return fn;
}
}
};

template <typename fnT, typename T1, typename T2> struct MaximumTypeMapFactory
{
/*! @brief get typeid for output type of maximum(T1 x, T2 y) */
std::enable_if_t<std::is_same<fnT, int>::value, int> get()
{
using rT = typename MaximumOutputType<T1, T2>::value_type;
;
return td_ns::GetTypeid<rT>{}.get();
}
};

template <typename T1, typename T2, typename resT, typename IndexerT>
class maximum_strided_kernel;

template <typename argTy1, typename argTy2>
sycl::event
maximum_strided_impl(sycl::queue exec_q,
size_t nelems,
int nd,
const py::ssize_t *shape_and_strides,
const char *arg1_p,
py::ssize_t arg1_offset,
const char *arg2_p,
py::ssize_t arg2_offset,
char *res_p,
py::ssize_t res_offset,
const std::vector<sycl::event> &depends,
const std::vector<sycl::event> &additional_depends)
{
return elementwise_common::binary_strided_impl<
argTy1, argTy2, MaximumOutputType, MaximumStridedFunctor,
maximum_strided_kernel>(exec_q, nelems, nd, shape_and_strides, arg1_p,
arg1_offset, arg2_p, arg2_offset, res_p,
res_offset, depends, additional_depends);
}

template <typename fnT, typename T1, typename T2> struct MaximumStridedFactory
{
fnT get()
{
if constexpr (std::is_same_v<
typename MaximumOutputType<T1, T2>::value_type, void>)
{
fnT fn = nullptr;
return fn;
}
else {
fnT fn = maximum_strided_impl<T1, T2>;
return fn;
}
}
};

} // namespace maximum
} // namespace kernels
} // namespace tensor
} // namespace dpctl
Loading