|
| 1 | +//***************************************************************************** |
| 2 | +// Copyright (c) 2026, 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 | +// - Neither the name of the copyright holder nor the names of its contributors |
| 13 | +// may be used to endorse or promote products derived from this software |
| 14 | +// without specific prior written permission. |
| 15 | +// |
| 16 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 | +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 26 | +// THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | +//***************************************************************************** |
| 28 | +// |
| 29 | +/// \file |
| 30 | +/// This file defines kernels for elementwise evaluation of EXP(x) function. |
| 31 | +//===---------------------------------------------------------------------===// |
| 32 | + |
| 33 | +#pragma once |
| 34 | +#include <cmath> |
| 35 | +#include <cstddef> |
| 36 | +#include <cstdint> |
| 37 | +#include <limits> |
| 38 | +#include <type_traits> |
| 39 | + |
| 40 | +#include <sycl/sycl.hpp> |
| 41 | + |
| 42 | +#include "sycl_complex.hpp" |
| 43 | +#include "vec_size_util.hpp" |
| 44 | + |
| 45 | +#include "kernels/dpctl_tensor_types.hpp" |
| 46 | +#include "kernels/elementwise_functions/common.hpp" |
| 47 | + |
| 48 | +#include "utils/offset_utils.hpp" |
| 49 | +#include "utils/type_dispatch_building.hpp" |
| 50 | +#include "utils/type_utils.hpp" |
| 51 | + |
| 52 | +namespace dpctl::tensor::kernels::exp |
| 53 | +{ |
| 54 | + |
| 55 | +using dpctl::tensor::ssize_t; |
| 56 | +namespace td_ns = dpctl::tensor::type_dispatch; |
| 57 | + |
| 58 | +using dpctl::tensor::type_utils::is_complex; |
| 59 | + |
| 60 | +template <typename argT, typename resT> |
| 61 | +struct ExpFunctor |
| 62 | +{ |
| 63 | + // is function constant for given argT |
| 64 | + using is_constant = typename std::false_type; |
| 65 | + // constant value, if constant |
| 66 | + // constexpr resT constant_value = resT{}; |
| 67 | + // is function defined for sycl::vec |
| 68 | + using supports_vec = typename std::false_type; |
| 69 | + // do both argTy and resTy support sugroup store/load operation |
| 70 | + using supports_sg_loadstore = typename std::negation< |
| 71 | + std::disjunction<is_complex<resT>, is_complex<argT>>>; |
| 72 | + |
| 73 | + resT operator()(const argT &in) const |
| 74 | + { |
| 75 | + if constexpr (is_complex<argT>::value) { |
| 76 | + using realT = typename argT::value_type; |
| 77 | + |
| 78 | + static constexpr realT q_nan = |
| 79 | + std::numeric_limits<realT>::quiet_NaN(); |
| 80 | + |
| 81 | + const realT x = std::real(in); |
| 82 | + const realT y = std::imag(in); |
| 83 | + if (std::isfinite(x)) { |
| 84 | + if (std::isfinite(y)) { |
| 85 | + return exprm_ns::exp( |
| 86 | + exprm_ns::complex<realT>(in)); // exp(in); |
| 87 | + } |
| 88 | + else { |
| 89 | + return resT{q_nan, q_nan}; |
| 90 | + } |
| 91 | + } |
| 92 | + else if (std::isnan(x)) { |
| 93 | + /* x is nan */ |
| 94 | + if (y == realT(0)) { |
| 95 | + return resT{in}; |
| 96 | + } |
| 97 | + else { |
| 98 | + return resT{x, q_nan}; |
| 99 | + } |
| 100 | + } |
| 101 | + else { |
| 102 | + if (!sycl::signbit(x)) { /* x is +inf */ |
| 103 | + if (y == realT(0)) { |
| 104 | + return resT{x, y}; |
| 105 | + } |
| 106 | + else if (std::isfinite(y)) { |
| 107 | + return resT{x * sycl::cos(y), x * sycl::sin(y)}; |
| 108 | + } |
| 109 | + else { |
| 110 | + /* x = +inf, y = +-inf || nan */ |
| 111 | + return resT{x, q_nan}; |
| 112 | + } |
| 113 | + } |
| 114 | + else { /* x is -inf */ |
| 115 | + if (std::isfinite(y)) { |
| 116 | + realT exp_x = sycl::exp(x); |
| 117 | + return resT{exp_x * sycl::cos(y), exp_x * sycl::sin(y)}; |
| 118 | + } |
| 119 | + else { |
| 120 | + /* x = -inf, y = +-inf || nan */ |
| 121 | + return resT{0, 0}; |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + else { |
| 127 | + return sycl::exp(in); |
| 128 | + } |
| 129 | + } |
| 130 | +}; |
| 131 | + |
| 132 | +template <typename argTy, |
| 133 | + typename resTy = argTy, |
| 134 | + std::uint8_t vec_sz = 4u, |
| 135 | + std::uint8_t n_vecs = 2u, |
| 136 | + bool enable_sg_loadstore = true> |
| 137 | +using ExpContigFunctor = |
| 138 | + elementwise_common::UnaryContigFunctor<argTy, |
| 139 | + resTy, |
| 140 | + ExpFunctor<argTy, resTy>, |
| 141 | + vec_sz, |
| 142 | + n_vecs, |
| 143 | + enable_sg_loadstore>; |
| 144 | + |
| 145 | +template <typename argTy, typename resTy, typename IndexerT> |
| 146 | +using ExpStridedFunctor = elementwise_common:: |
| 147 | + UnaryStridedFunctor<argTy, resTy, IndexerT, ExpFunctor<argTy, resTy>>; |
| 148 | + |
| 149 | +template <typename T> |
| 150 | +struct ExpOutputType |
| 151 | +{ |
| 152 | + using value_type = typename std::disjunction< |
| 153 | + td_ns::TypeMapResultEntry<T, sycl::half>, |
| 154 | + td_ns::TypeMapResultEntry<T, float>, |
| 155 | + td_ns::TypeMapResultEntry<T, double>, |
| 156 | + td_ns::TypeMapResultEntry<T, std::complex<float>>, |
| 157 | + td_ns::TypeMapResultEntry<T, std::complex<double>>, |
| 158 | + td_ns::DefaultResultEntry<void>>::result_type; |
| 159 | + |
| 160 | + static constexpr bool is_defined = !std::is_same_v<value_type, void>; |
| 161 | +}; |
| 162 | + |
| 163 | +namespace hyperparam_detail |
| 164 | +{ |
| 165 | + |
| 166 | +namespace vsu_ns = dpctl::tensor::kernels::vec_size_utils; |
| 167 | + |
| 168 | +using vsu_ns::ContigHyperparameterSetDefault; |
| 169 | +using vsu_ns::UnaryContigHyperparameterSetEntry; |
| 170 | + |
| 171 | +template <typename argTy> |
| 172 | +struct ExpContigHyperparameterSet |
| 173 | +{ |
| 174 | + using value_type = |
| 175 | + typename std::disjunction<ContigHyperparameterSetDefault<4u, 2u>>; |
| 176 | + |
| 177 | + constexpr static auto vec_sz = value_type::vec_sz; |
| 178 | + constexpr static auto n_vecs = value_type::n_vecs; |
| 179 | +}; |
| 180 | + |
| 181 | +} // end of namespace hyperparam_detail |
| 182 | + |
| 183 | +template <typename T1, typename T2, std::uint8_t vec_sz, std::uint8_t n_vecs> |
| 184 | +class exp_contig_kernel; |
| 185 | + |
| 186 | +template <typename argTy> |
| 187 | +sycl::event exp_contig_impl(sycl::queue &exec_q, |
| 188 | + std::size_t nelems, |
| 189 | + const char *arg_p, |
| 190 | + char *res_p, |
| 191 | + const std::vector<sycl::event> &depends = {}) |
| 192 | +{ |
| 193 | + using ExpHS = hyperparam_detail::ExpContigHyperparameterSet<argTy>; |
| 194 | + static constexpr std::uint8_t vec_sz = ExpHS::vec_sz; |
| 195 | + static constexpr std::uint8_t n_vecs = ExpHS::n_vecs; |
| 196 | + |
| 197 | + return elementwise_common::unary_contig_impl< |
| 198 | + argTy, ExpOutputType, ExpContigFunctor, exp_contig_kernel, vec_sz, |
| 199 | + n_vecs>(exec_q, nelems, arg_p, res_p, depends); |
| 200 | +} |
| 201 | + |
| 202 | +template <typename fnT, typename T> |
| 203 | +struct ExpContigFactory |
| 204 | +{ |
| 205 | + fnT get() |
| 206 | + { |
| 207 | + if constexpr (!ExpOutputType<T>::is_defined) { |
| 208 | + fnT fn = nullptr; |
| 209 | + return fn; |
| 210 | + } |
| 211 | + else { |
| 212 | + fnT fn = exp_contig_impl<T>; |
| 213 | + return fn; |
| 214 | + } |
| 215 | + } |
| 216 | +}; |
| 217 | + |
| 218 | +template <typename fnT, typename T> |
| 219 | +struct ExpTypeMapFactory |
| 220 | +{ |
| 221 | + /*! @brief get typeid for output type of sycl::exp(T x) */ |
| 222 | + std::enable_if_t<std::is_same<fnT, int>::value, int> get() |
| 223 | + { |
| 224 | + using rT = typename ExpOutputType<T>::value_type; |
| 225 | + return td_ns::GetTypeid<rT>{}.get(); |
| 226 | + } |
| 227 | +}; |
| 228 | + |
| 229 | +template <typename T1, typename T2, typename T3> |
| 230 | +class exp_strided_kernel; |
| 231 | + |
| 232 | +template <typename argTy> |
| 233 | +sycl::event exp_strided_impl(sycl::queue &exec_q, |
| 234 | + std::size_t nelems, |
| 235 | + int nd, |
| 236 | + const ssize_t *shape_and_strides, |
| 237 | + const char *arg_p, |
| 238 | + ssize_t arg_offset, |
| 239 | + char *res_p, |
| 240 | + ssize_t res_offset, |
| 241 | + const std::vector<sycl::event> &depends, |
| 242 | + const std::vector<sycl::event> &additional_depends) |
| 243 | +{ |
| 244 | + return elementwise_common::unary_strided_impl< |
| 245 | + argTy, ExpOutputType, ExpStridedFunctor, exp_strided_kernel>( |
| 246 | + exec_q, nelems, nd, shape_and_strides, arg_p, arg_offset, res_p, |
| 247 | + res_offset, depends, additional_depends); |
| 248 | +} |
| 249 | + |
| 250 | +template <typename fnT, typename T> |
| 251 | +struct ExpStridedFactory |
| 252 | +{ |
| 253 | + fnT get() |
| 254 | + { |
| 255 | + if constexpr (!ExpOutputType<T>::is_defined) { |
| 256 | + fnT fn = nullptr; |
| 257 | + return fn; |
| 258 | + } |
| 259 | + else { |
| 260 | + fnT fn = exp_strided_impl<T>; |
| 261 | + return fn; |
| 262 | + } |
| 263 | + } |
| 264 | +}; |
| 265 | + |
| 266 | +} // namespace dpctl::tensor::kernels::exp |
0 commit comments