Skip to content

impl_elementwise_func_round #1299

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
Jul 26, 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
2 changes: 2 additions & 0 deletions dpctl/tensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
pow,
proj,
real,
round,
sin,
sqrt,
square,
Expand Down Expand Up @@ -243,6 +244,7 @@
"pow",
"proj",
"real",
"round",
"sin",
"sqrt",
"square",
Expand Down
25 changes: 24 additions & 1 deletion dpctl/tensor/_elementwise_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,30 @@
# FIXME: implement B22

# U28: ==== ROUND (x)
# FIXME: implement U28
_round_docstring = """
round(x, out=None, order='K')

Rounds each element `x_i` of the input array `x` to
the nearest integer-valued number.

Args:
x (usm_ndarray):
Input array, 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 rounded value. The data type
of the returned array is determined by the Type Promotion Rules.
"""

round = UnaryElementwiseFunc(
"round", ti._round_result_type, ti._round, _round_docstring
)

# U29: ==== SIGN (x)
# FIXME: implement U29
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
//=== round.hpp - Unary function ROUND ------ *-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 ROUND(x) function.
//===---------------------------------------------------------------------===//

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

#include "kernels/elementwise_functions/common.hpp"

#include "utils/offset_utils.hpp"
#include "utils/type_dispatch.hpp"
#include "utils/type_utils.hpp"
#include <pybind11/pybind11.h>

namespace dpctl
{
namespace tensor
{
namespace kernels
{
namespace round
{

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

using dpctl::tensor::type_utils::is_complex;

template <typename argT, typename resT> struct RoundFunctor
{

// is function constant for given argT
using is_constant = typename std::false_type;
// constant value, if constant
// constexpr resT constant_value = resT{};
// is function defined for sycl::vec
using supports_vec = typename std::false_type;
// do both argTy and resTy support sugroup store/load operation
using supports_sg_loadstore = typename std::negation<
std::disjunction<is_complex<resT>, is_complex<argT>>>;

resT operator()(const argT &in)
{

if constexpr (std::is_integral_v<argT>) {
return in;
}
else if constexpr (is_complex<argT>::value) {
using realT = typename argT::value_type;
return resT{round_func<realT>(std::real(in)),
round_func<realT>(std::imag(in))};
}
else {
return round_func<argT>(in);
}
}

private:
template <typename T> T round_func(const T &input) const
{
return std::rint(input);
}
};

template <typename argTy,
typename resTy = argTy,
unsigned int vec_sz = 4,
unsigned int n_vecs = 2>
using RoundContigFunctor =
elementwise_common::UnaryContigFunctor<argTy,
resTy,
RoundFunctor<argTy, resTy>,
vec_sz,
n_vecs>;

template <typename argTy, typename resTy, typename IndexerT>
using RoundStridedFunctor = elementwise_common::
UnaryStridedFunctor<argTy, resTy, IndexerT, RoundFunctor<argTy, resTy>>;

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

template <typename T1, typename T2, unsigned int vec_sz, unsigned int n_vecs>
class round_contig_kernel;

template <typename argTy>
sycl::event round_contig_impl(sycl::queue exec_q,
size_t nelems,
const char *arg_p,
char *res_p,
const std::vector<sycl::event> &depends = {})
{
return elementwise_common::unary_contig_impl<
argTy, RoundOutputType, RoundContigFunctor, round_contig_kernel>(
exec_q, nelems, arg_p, res_p, depends);
}

template <typename fnT, typename T> struct RoundContigFactory
{
fnT get()
{
if constexpr (std::is_same_v<typename RoundOutputType<T>::value_type,
void>) {
fnT fn = nullptr;
return fn;
}
else {
fnT fn = round_contig_impl<T>;
return fn;
}
}
};

template <typename fnT, typename T> struct RoundTypeMapFactory
{
/*! @brief get typeid for output type of sycl::round(T x) */
std::enable_if_t<std::is_same<fnT, int>::value, int> get()
{
using rT = typename RoundOutputType<T>::value_type;
return td_ns::GetTypeid<rT>{}.get();
}
};

template <typename T1, typename T2, typename T3> class round_strided_kernel;

template <typename argTy>
sycl::event
round_strided_impl(sycl::queue exec_q,
size_t nelems,
int nd,
const py::ssize_t *shape_and_strides,
const char *arg_p,
py::ssize_t arg_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::unary_strided_impl<
argTy, RoundOutputType, RoundStridedFunctor, round_strided_kernel>(
exec_q, nelems, nd, shape_and_strides, arg_p, arg_offset, res_p,
res_offset, depends, additional_depends);
}

template <typename fnT, typename T> struct RoundStridedFactory
{
fnT get()
{
if constexpr (std::is_same_v<typename RoundOutputType<T>::value_type,
void>) {
fnT fn = nullptr;
return fn;
}
else {
fnT fn = round_strided_impl<T>;
return fn;
}
}
};

} // namespace round
} // namespace kernels
} // namespace tensor
} // namespace dpctl
55 changes: 53 additions & 2 deletions dpctl/tensor/libtensor/source/elementwise_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
#include "kernels/elementwise_functions/pow.hpp"
#include "kernels/elementwise_functions/proj.hpp"
#include "kernels/elementwise_functions/real.hpp"
#include "kernels/elementwise_functions/round.hpp"
#include "kernels/elementwise_functions/sin.hpp"
#include "kernels/elementwise_functions/sqrt.hpp"
#include "kernels/elementwise_functions/square.hpp"
Expand Down Expand Up @@ -1627,7 +1628,37 @@ namespace impl
// U28: ==== ROUND (x)
namespace impl
{
// FIXME: add code for U28

namespace round_fn_ns = dpctl::tensor::kernels::round;

static unary_contig_impl_fn_ptr_t
round_contig_dispatch_vector[td_ns::num_types];
static int round_output_typeid_vector[td_ns::num_types];
static unary_strided_impl_fn_ptr_t
round_strided_dispatch_vector[td_ns::num_types];

void populate_round_dispatch_vectors(void)
{
using namespace td_ns;
namespace fn_ns = round_fn_ns;

using fn_ns::RoundContigFactory;
DispatchVectorBuilder<unary_contig_impl_fn_ptr_t, RoundContigFactory,
num_types>
dvb1;
dvb1.populate_dispatch_vector(round_contig_dispatch_vector);

using fn_ns::RoundStridedFactory;
DispatchVectorBuilder<unary_strided_impl_fn_ptr_t, RoundStridedFactory,
num_types>
dvb2;
dvb2.populate_dispatch_vector(round_strided_dispatch_vector);

using fn_ns::RoundTypeMapFactory;
DispatchVectorBuilder<int, RoundTypeMapFactory, num_types> dvb3;
dvb3.populate_dispatch_vector(round_output_typeid_vector);
}

} // namespace impl

// U29: ==== SIGN (x)
Expand Down Expand Up @@ -3029,7 +3060,27 @@ void init_elementwise_functions(py::module_ m)
// FIXME:

// U28: ==== ROUND (x)
// FIXME:
{
impl::populate_round_dispatch_vectors();
using impl::round_contig_dispatch_vector;
using impl::round_output_typeid_vector;
using impl::round_strided_dispatch_vector;

auto round_pyapi = [&](arrayT src, arrayT dst, sycl::queue exec_q,
const event_vecT &depends = {}) {
return py_unary_ufunc(
src, dst, exec_q, depends, round_output_typeid_vector,
round_contig_dispatch_vector, round_strided_dispatch_vector);
};
m.def("_round", round_pyapi, "", py::arg("src"), py::arg("dst"),
py::arg("sycl_queue"), py::arg("depends") = py::list());

auto round_result_type_pyapi = [&](py::dtype dtype) {
return py_unary_ufunc_result_type(dtype,
round_output_typeid_vector);
};
m.def("_round_result_type", round_result_type_pyapi);
}

// U29: ==== SIGN (x)
// FIXME:
Expand Down
Loading