|
| 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 <cstddef> |
| 38 | +#include <type_traits> |
| 39 | +#include <vector> |
| 40 | + |
| 41 | +#include <sycl/sycl.hpp> |
| 42 | + |
| 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/type_dispatch_building.hpp" |
| 49 | +#include "utils/type_utils.hpp" |
| 50 | + |
| 51 | +namespace dpctl::tensor::kernels::logical_not |
| 52 | +{ |
| 53 | + |
| 54 | +using dpctl::tensor::ssize_t; |
| 55 | +namespace td_ns = dpctl::tensor::type_dispatch; |
| 56 | +namespace tu_ns = dpctl::tensor::type_utils; |
| 57 | + |
| 58 | +template <typename argT, typename resT> |
| 59 | +struct LogicalNotFunctor |
| 60 | +{ |
| 61 | + static_assert(std::is_same_v<resT, bool>); |
| 62 | + |
| 63 | + using is_constant = typename std::false_type; |
| 64 | + // constexpr resT constant_value = resT{}; |
| 65 | + using supports_vec = typename std::false_type; |
| 66 | + using supports_sg_loadstore = typename std::negation< |
| 67 | + std::disjunction<tu_ns::is_complex<resT>, tu_ns::is_complex<argT>>>; |
| 68 | + |
| 69 | + resT operator()(const argT &in) const |
| 70 | + { |
| 71 | + using tu_ns::convert_impl; |
| 72 | + return !convert_impl<bool, argT>(in); |
| 73 | + } |
| 74 | +}; |
| 75 | + |
| 76 | +template <typename argT, |
| 77 | + typename resT = bool, |
| 78 | + std::uint8_t vec_sz = 4u, |
| 79 | + std::uint8_t n_vecs = 2u, |
| 80 | + bool enable_sg_loadstore = true> |
| 81 | +using LogicalNotContigFunctor = |
| 82 | + elementwise_common::UnaryContigFunctor<argT, |
| 83 | + resT, |
| 84 | + LogicalNotFunctor<argT, resT>, |
| 85 | + vec_sz, |
| 86 | + n_vecs, |
| 87 | + enable_sg_loadstore>; |
| 88 | + |
| 89 | +template <typename argTy, typename resTy, typename IndexerT> |
| 90 | +using LogicalNotStridedFunctor = |
| 91 | + elementwise_common::UnaryStridedFunctor<argTy, |
| 92 | + resTy, |
| 93 | + IndexerT, |
| 94 | + LogicalNotFunctor<argTy, resTy>>; |
| 95 | + |
| 96 | +template <typename argTy> |
| 97 | +struct LogicalNotOutputType |
| 98 | +{ |
| 99 | + using value_type = bool; |
| 100 | +}; |
| 101 | + |
| 102 | +namespace hyperparam_detail |
| 103 | +{ |
| 104 | + |
| 105 | +namespace vsu_ns = dpctl::tensor::kernels::vec_size_utils; |
| 106 | + |
| 107 | +using vsu_ns::ContigHyperparameterSetDefault; |
| 108 | +using vsu_ns::UnaryContigHyperparameterSetEntry; |
| 109 | + |
| 110 | +template <typename argTy> |
| 111 | +struct LogicalNotContigHyperparameterSet |
| 112 | +{ |
| 113 | + using value_type = |
| 114 | + typename std::disjunction<ContigHyperparameterSetDefault<4u, 2u>>; |
| 115 | + |
| 116 | + constexpr static auto vec_sz = value_type::vec_sz; |
| 117 | + constexpr static auto n_vecs = value_type::n_vecs; |
| 118 | +}; |
| 119 | + |
| 120 | +} // end of namespace hyperparam_detail |
| 121 | + |
| 122 | +template <typename T1, typename T2, std::uint8_t vec_sz, std::uint8_t n_vecs> |
| 123 | +class logical_not_contig_kernel; |
| 124 | + |
| 125 | +template <typename argTy> |
| 126 | +sycl::event |
| 127 | + logical_not_contig_impl(sycl::queue &exec_q, |
| 128 | + std::size_t nelems, |
| 129 | + const char *arg_p, |
| 130 | + char *res_p, |
| 131 | + const std::vector<sycl::event> &depends = {}) |
| 132 | +{ |
| 133 | + using LogicalNotHS = |
| 134 | + hyperparam_detail::LogicalNotContigHyperparameterSet<argTy>; |
| 135 | + static constexpr std::uint8_t vec_sz = LogicalNotHS::vec_sz; |
| 136 | + static constexpr std::uint8_t n_vecs = LogicalNotHS::n_vecs; |
| 137 | + |
| 138 | + return elementwise_common::unary_contig_impl< |
| 139 | + argTy, LogicalNotOutputType, LogicalNotContigFunctor, |
| 140 | + logical_not_contig_kernel, vec_sz, n_vecs>(exec_q, nelems, arg_p, res_p, |
| 141 | + depends); |
| 142 | +} |
| 143 | + |
| 144 | +template <typename fnT, typename T> |
| 145 | +struct LogicalNotContigFactory |
| 146 | +{ |
| 147 | + fnT get() |
| 148 | + { |
| 149 | + fnT fn = logical_not_contig_impl<T>; |
| 150 | + return fn; |
| 151 | + } |
| 152 | +}; |
| 153 | + |
| 154 | +template <typename fnT, typename T> |
| 155 | +struct LogicalNotTypeMapFactory |
| 156 | +{ |
| 157 | + /*! @brief get typeid for output type of sycl::logical_not(T x) */ |
| 158 | + std::enable_if_t<std::is_same<fnT, int>::value, int> get() |
| 159 | + { |
| 160 | + using rT = typename LogicalNotOutputType<T>::value_type; |
| 161 | + return td_ns::GetTypeid<rT>{}.get(); |
| 162 | + } |
| 163 | +}; |
| 164 | + |
| 165 | +template <typename T1, typename T2, typename T3> |
| 166 | +class logical_not_strided_kernel; |
| 167 | + |
| 168 | +template <typename argTy> |
| 169 | +sycl::event |
| 170 | + logical_not_strided_impl(sycl::queue &exec_q, |
| 171 | + std::size_t nelems, |
| 172 | + int nd, |
| 173 | + const ssize_t *shape_and_strides, |
| 174 | + const char *arg_p, |
| 175 | + ssize_t arg_offset, |
| 176 | + char *res_p, |
| 177 | + ssize_t res_offset, |
| 178 | + const std::vector<sycl::event> &depends, |
| 179 | + const std::vector<sycl::event> &additional_depends) |
| 180 | +{ |
| 181 | + return elementwise_common::unary_strided_impl<argTy, LogicalNotOutputType, |
| 182 | + LogicalNotStridedFunctor, |
| 183 | + logical_not_strided_kernel>( |
| 184 | + exec_q, nelems, nd, shape_and_strides, arg_p, arg_offset, res_p, |
| 185 | + res_offset, depends, additional_depends); |
| 186 | +} |
| 187 | + |
| 188 | +template <typename fnT, typename T> |
| 189 | +struct LogicalNotStridedFactory |
| 190 | +{ |
| 191 | + fnT get() |
| 192 | + { |
| 193 | + fnT fn = logical_not_strided_impl<T>; |
| 194 | + return fn; |
| 195 | + } |
| 196 | +}; |
| 197 | + |
| 198 | +} // namespace dpctl::tensor::kernels::logical_not |
0 commit comments