|
| 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 | +//===---------------------------------------------------------------------===// |
| 30 | +/// |
| 31 | +/// \file |
| 32 | +/// This file defines kernels for elementwise evaluation of ISNAN(x) |
| 33 | +/// function that tests whether a tensor element is a NaN. |
| 34 | +//===---------------------------------------------------------------------===// |
| 35 | + |
| 36 | +#pragma once |
| 37 | +#include <complex> |
| 38 | +#include <cstddef> |
| 39 | +#include <cstdint> |
| 40 | +#include <type_traits> |
| 41 | +#include <vector> |
| 42 | + |
| 43 | +#include <sycl/sycl.hpp> |
| 44 | + |
| 45 | +#include "vec_size_util.hpp" |
| 46 | + |
| 47 | +#include "kernels/dpctl_tensor_types.hpp" |
| 48 | +#include "kernels/elementwise_functions/common.hpp" |
| 49 | + |
| 50 | +#include "utils/offset_utils.hpp" |
| 51 | +#include "utils/type_dispatch_building.hpp" |
| 52 | +#include "utils/type_utils.hpp" |
| 53 | + |
| 54 | +namespace dpctl::tensor::kernels::isnan |
| 55 | +{ |
| 56 | + |
| 57 | +using dpctl::tensor::ssize_t; |
| 58 | +namespace td_ns = dpctl::tensor::type_dispatch; |
| 59 | + |
| 60 | +using dpctl::tensor::type_utils::is_complex; |
| 61 | +using dpctl::tensor::type_utils::vec_cast; |
| 62 | + |
| 63 | +template <typename argT, typename resT> |
| 64 | +struct IsNanFunctor |
| 65 | +{ |
| 66 | + static_assert(std::is_same_v<resT, bool>); |
| 67 | + |
| 68 | + /* |
| 69 | + std::is_same<argT, bool>::value || |
| 70 | + std::is_integral<argT>::value |
| 71 | + */ |
| 72 | + using is_constant = typename std::disjunction<std::is_same<argT, bool>, |
| 73 | + std::is_integral<argT>>; |
| 74 | + static constexpr resT constant_value = false; |
| 75 | + using supports_vec = typename std::true_type; |
| 76 | + using supports_sg_loadstore = typename std::negation< |
| 77 | + std::disjunction<is_complex<resT>, is_complex<argT>>>; |
| 78 | + |
| 79 | + resT operator()(const argT &in) const |
| 80 | + { |
| 81 | + if constexpr (is_complex<argT>::value) { |
| 82 | + const bool real_isnan = sycl::isnan(std::real(in)); |
| 83 | + const bool imag_isnan = sycl::isnan(std::imag(in)); |
| 84 | + return (real_isnan || imag_isnan); |
| 85 | + } |
| 86 | + else if constexpr (std::is_same<argT, bool>::value || |
| 87 | + std::is_integral<argT>::value) |
| 88 | + { |
| 89 | + return constant_value; |
| 90 | + } |
| 91 | + else { |
| 92 | + return sycl::isnan(in); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + template <int vec_sz> |
| 97 | + sycl::vec<resT, vec_sz> operator()(const sycl::vec<argT, vec_sz> &in) const |
| 98 | + { |
| 99 | + auto const &res_vec = sycl::isnan(in); |
| 100 | + |
| 101 | + using deducedT = typename std::remove_cv_t< |
| 102 | + std::remove_reference_t<decltype(res_vec)>>::element_type; |
| 103 | + |
| 104 | + return vec_cast<bool, deducedT, vec_sz>(res_vec); |
| 105 | + } |
| 106 | +}; |
| 107 | + |
| 108 | +template <typename argT, |
| 109 | + typename resT = bool, |
| 110 | + std::uint8_t vec_sz = 4u, |
| 111 | + std::uint8_t n_vecs = 2u, |
| 112 | + bool enable_sg_loadstore = true> |
| 113 | +using IsNanContigFunctor = |
| 114 | + elementwise_common::UnaryContigFunctor<argT, |
| 115 | + resT, |
| 116 | + IsNanFunctor<argT, resT>, |
| 117 | + vec_sz, |
| 118 | + n_vecs, |
| 119 | + enable_sg_loadstore>; |
| 120 | + |
| 121 | +template <typename argTy, typename resTy, typename IndexerT> |
| 122 | +using IsNanStridedFunctor = elementwise_common:: |
| 123 | + UnaryStridedFunctor<argTy, resTy, IndexerT, IsNanFunctor<argTy, resTy>>; |
| 124 | + |
| 125 | +template <typename argTy> |
| 126 | +struct IsNanOutputType |
| 127 | +{ |
| 128 | + using value_type = bool; |
| 129 | +}; |
| 130 | + |
| 131 | +namespace hyperparam_detail |
| 132 | +{ |
| 133 | + |
| 134 | +namespace vsu_ns = dpctl::tensor::kernels::vec_size_utils; |
| 135 | + |
| 136 | +using vsu_ns::ContigHyperparameterSetDefault; |
| 137 | +using vsu_ns::UnaryContigHyperparameterSetEntry; |
| 138 | + |
| 139 | +template <typename argTy> |
| 140 | +struct IsNanContigHyperparameterSet |
| 141 | +{ |
| 142 | + using value_type = |
| 143 | + typename std::disjunction<ContigHyperparameterSetDefault<4u, 2u>>; |
| 144 | + |
| 145 | + constexpr static auto vec_sz = value_type::vec_sz; |
| 146 | + constexpr static auto n_vecs = value_type::n_vecs; |
| 147 | +}; |
| 148 | + |
| 149 | +} // end of namespace hyperparam_detail |
| 150 | + |
| 151 | +template <typename T1, typename T2, std::uint8_t vec_sz, std::uint8_t n_vecs> |
| 152 | +class isnan_contig_kernel; |
| 153 | + |
| 154 | +template <typename argTy> |
| 155 | +sycl::event isnan_contig_impl(sycl::queue &exec_q, |
| 156 | + std::size_t nelems, |
| 157 | + const char *arg_p, |
| 158 | + char *res_p, |
| 159 | + const std::vector<sycl::event> &depends = {}) |
| 160 | +{ |
| 161 | + using IsNanHS = hyperparam_detail::IsNanContigHyperparameterSet<argTy>; |
| 162 | + static constexpr std::uint8_t vec_sz = IsNanHS::vec_sz; |
| 163 | + static constexpr std::uint8_t n_vecs = IsNanHS::n_vecs; |
| 164 | + |
| 165 | + return elementwise_common::unary_contig_impl< |
| 166 | + argTy, IsNanOutputType, IsNanContigFunctor, isnan_contig_kernel, vec_sz, |
| 167 | + n_vecs>(exec_q, nelems, arg_p, res_p, depends); |
| 168 | +} |
| 169 | + |
| 170 | +template <typename fnT, typename T> |
| 171 | +struct IsNanContigFactory |
| 172 | +{ |
| 173 | + fnT get() |
| 174 | + { |
| 175 | + fnT fn = isnan_contig_impl<T>; |
| 176 | + return fn; |
| 177 | + } |
| 178 | +}; |
| 179 | + |
| 180 | +template <typename fnT, typename T> |
| 181 | +struct IsNanTypeMapFactory |
| 182 | +{ |
| 183 | + /*! @brief get typeid for output type of sycl::isnan(T x) */ |
| 184 | + std::enable_if_t<std::is_same<fnT, int>::value, int> get() |
| 185 | + { |
| 186 | + using rT = typename IsNanOutputType<T>::value_type; |
| 187 | + return td_ns::GetTypeid<rT>{}.get(); |
| 188 | + } |
| 189 | +}; |
| 190 | + |
| 191 | +template <typename T1, typename T2, typename T3> |
| 192 | +class isnan_strided_kernel; |
| 193 | + |
| 194 | +template <typename argTy> |
| 195 | +sycl::event |
| 196 | + isnan_strided_impl(sycl::queue &exec_q, |
| 197 | + std::size_t nelems, |
| 198 | + int nd, |
| 199 | + const ssize_t *shape_and_strides, |
| 200 | + const char *arg_p, |
| 201 | + ssize_t arg_offset, |
| 202 | + char *res_p, |
| 203 | + ssize_t res_offset, |
| 204 | + const std::vector<sycl::event> &depends, |
| 205 | + const std::vector<sycl::event> &additional_depends) |
| 206 | +{ |
| 207 | + return elementwise_common::unary_strided_impl< |
| 208 | + argTy, IsNanOutputType, IsNanStridedFunctor, isnan_strided_kernel>( |
| 209 | + exec_q, nelems, nd, shape_and_strides, arg_p, arg_offset, res_p, |
| 210 | + res_offset, depends, additional_depends); |
| 211 | +} |
| 212 | + |
| 213 | +template <typename fnT, typename T> |
| 214 | +struct IsNanStridedFactory |
| 215 | +{ |
| 216 | + fnT get() |
| 217 | + { |
| 218 | + fnT fn = isnan_strided_impl<T>; |
| 219 | + return fn; |
| 220 | + } |
| 221 | +}; |
| 222 | + |
| 223 | +} // namespace dpctl::tensor::kernels::isnan |
0 commit comments