DPNP C++ backend kernel library 0.20.0dev0
Data Parallel Extension for NumPy*
Loading...
Searching...
No Matches
nan_to_num.hpp
1//*****************************************************************************
2// Copyright (c) 2024, 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#pragma once
30
31#include <complex>
32#include <cstddef>
33#include <vector>
34
35#include <sycl/sycl.hpp>
36// dpctl tensor headers
37#include "kernels/alignment.hpp"
38#include "kernels/dpctl_tensor_types.hpp"
39#include "utils/offset_utils.hpp"
40#include "utils/sycl_utils.hpp"
41#include "utils/type_utils.hpp"
42
43namespace dpnp::kernels::nan_to_num
44{
45
46template <typename T>
47inline T to_num(const T v, const T nan, const T posinf, const T neginf)
48{
49 return (sycl::isnan(v)) ? nan
50 : (sycl::isinf(v)) ? (v > 0) ? posinf : neginf
51 : v;
52}
53
54template <typename T, typename scT, typename InOutIndexerT>
56{
57private:
58 const T *inp_ = nullptr;
59 T *out_ = nullptr;
60 const InOutIndexerT inp_out_indexer_;
61 const scT nan_;
62 const scT posinf_;
63 const scT neginf_;
64
65public:
66 NanToNumFunctor(const T *inp,
67 T *out,
68 const InOutIndexerT &inp_out_indexer,
69 const scT nan,
70 const scT posinf,
71 const scT neginf)
72 : inp_(inp), out_(out), inp_out_indexer_(inp_out_indexer), nan_(nan),
73 posinf_(posinf), neginf_(neginf)
74 {
75 }
76
77 void operator()(sycl::id<1> wid) const
78 {
79 const auto &offsets_ = inp_out_indexer_(wid.get(0));
80 const dpctl::tensor::ssize_t &inp_offset = offsets_.get_first_offset();
81 const dpctl::tensor::ssize_t &out_offset = offsets_.get_second_offset();
82
83 using dpctl::tensor::type_utils::is_complex_v;
84 if constexpr (is_complex_v<T>) {
85 using realT = typename T::value_type;
86 static_assert(std::is_same_v<realT, scT>);
87 T z = inp_[inp_offset];
88 realT x = to_num(z.real(), nan_, posinf_, neginf_);
89 realT y = to_num(z.imag(), nan_, posinf_, neginf_);
90 out_[out_offset] = T{x, y};
91 }
92 else {
93 out_[out_offset] = to_num(inp_[inp_offset], nan_, posinf_, neginf_);
94 }
95 }
96};
97
98template <typename T,
99 typename scT,
100 std::uint8_t vec_sz = 4u,
101 std::uint8_t n_vecs = 2u,
102 bool enable_sg_loadstore = true>
104{
105private:
106 const T *in_ = nullptr;
107 T *out_ = nullptr;
108 std::size_t nelems_;
109 const scT nan_;
110 const scT posinf_;
111 const scT neginf_;
112
113public:
114 NanToNumContigFunctor(const T *in,
115 T *out,
116 const std::size_t n_elems,
117 const scT nan,
118 const scT posinf,
119 const scT neginf)
120 : in_(in), out_(out), nelems_(n_elems), nan_(nan), posinf_(posinf),
121 neginf_(neginf)
122 {
123 }
124
125 void operator()(sycl::nd_item<1> ndit) const
126 {
127 constexpr std::uint8_t elems_per_wi = n_vecs * vec_sz;
128 /* Each work-item processes vec_sz elements, contiguous in memory */
129 /* NOTE: work-group size must be divisible by sub-group size */
130
131 using dpctl::tensor::type_utils::is_complex_v;
132 if constexpr (enable_sg_loadstore && !is_complex_v<T>) {
133 auto sg = ndit.get_sub_group();
134 const std::uint16_t sgSize = sg.get_max_local_range()[0];
135 const std::size_t base =
136 elems_per_wi * (ndit.get_group(0) * ndit.get_local_range(0) +
137 sg.get_group_id()[0] * sgSize);
138
139 if (base + elems_per_wi * sgSize < nelems_) {
140 using dpctl::tensor::sycl_utils::sub_group_load;
141 using dpctl::tensor::sycl_utils::sub_group_store;
142#pragma unroll
143 for (std::uint8_t it = 0; it < elems_per_wi; it += vec_sz) {
144 const std::size_t offset = base + it * sgSize;
145 auto in_multi_ptr = sycl::address_space_cast<
146 sycl::access::address_space::global_space,
147 sycl::access::decorated::yes>(&in_[offset]);
148 auto out_multi_ptr = sycl::address_space_cast<
149 sycl::access::address_space::global_space,
150 sycl::access::decorated::yes>(&out_[offset]);
151
152 sycl::vec<T, vec_sz> arg_vec =
153 sub_group_load<vec_sz>(sg, in_multi_ptr);
154#pragma unroll
155 for (std::uint32_t k = 0; k < vec_sz; ++k) {
156 arg_vec[k] = to_num(arg_vec[k], nan_, posinf_, neginf_);
157 }
158 sub_group_store<vec_sz>(sg, arg_vec, out_multi_ptr);
159 }
160 }
161 else {
162 const std::size_t lane_id = sg.get_local_id()[0];
163 for (std::size_t k = base + lane_id; k < nelems_; k += sgSize) {
164 out_[k] = to_num(in_[k], nan_, posinf_, neginf_);
165 }
166 }
167 }
168 else {
169 const std::uint16_t sgSize =
170 ndit.get_sub_group().get_local_range()[0];
171 const std::size_t gid = ndit.get_global_linear_id();
172 const std::uint16_t elems_per_sg = sgSize * elems_per_wi;
173
174 const std::size_t start =
175 (gid / sgSize) * (elems_per_sg - sgSize) + gid;
176 const std::size_t end = std::min(nelems_, start + elems_per_sg);
177 for (std::size_t offset = start; offset < end; offset += sgSize) {
178 if constexpr (is_complex_v<T>) {
179 using realT = typename T::value_type;
180 static_assert(std::is_same_v<realT, scT>);
181
182 T z = in_[offset];
183 realT x = to_num(z.real(), nan_, posinf_, neginf_);
184 realT y = to_num(z.imag(), nan_, posinf_, neginf_);
185 out_[offset] = T{x, y};
186 }
187 else {
188 out_[offset] = to_num(in_[offset], nan_, posinf_, neginf_);
189 }
190 }
191 }
192 }
193};
194
195template <typename T, typename scT>
196sycl::event nan_to_num_strided_impl(sycl::queue &q,
197 const size_t nelems,
198 const int nd,
199 const dpctl::tensor::ssize_t *shape_strides,
200 const scT nan,
201 const scT posinf,
202 const scT neginf,
203 const char *in_cp,
204 const dpctl::tensor::ssize_t in_offset,
205 char *out_cp,
206 const dpctl::tensor::ssize_t out_offset,
207 const std::vector<sycl::event> &depends)
208{
209 dpctl::tensor::type_utils::validate_type_for_device<T>(q);
210
211 const T *in_tp = reinterpret_cast<const T *>(in_cp);
212 T *out_tp = reinterpret_cast<T *>(out_cp);
213
214 using InOutIndexerT =
215 typename dpctl::tensor::offset_utils::TwoOffsets_StridedIndexer;
216 const InOutIndexerT indexer{nd, in_offset, out_offset, shape_strides};
217
218 sycl::event comp_ev = q.submit([&](sycl::handler &cgh) {
219 cgh.depends_on(depends);
220
221 using NanToNumFunc = NanToNumFunctor<T, scT, InOutIndexerT>;
222 cgh.parallel_for<NanToNumFunc>(
223 {nelems},
224 NanToNumFunc(in_tp, out_tp, indexer, nan, posinf, neginf));
225 });
226 return comp_ev;
227}
228
229template <typename T,
230 typename scT,
231 std::uint8_t vec_sz = 4u,
232 std::uint8_t n_vecs = 2u>
233sycl::event nan_to_num_contig_impl(sycl::queue &exec_q,
234 std::size_t nelems,
235 const scT nan,
236 const scT posinf,
237 const scT neginf,
238 const char *in_cp,
239 char *out_cp,
240 const std::vector<sycl::event> &depends = {})
241{
242 constexpr std::uint8_t elems_per_wi = n_vecs * vec_sz;
243 const std::size_t n_work_items_needed = nelems / elems_per_wi;
244 const std::size_t empirical_threshold = std::size_t(1) << 21;
245 const std::size_t lws = (n_work_items_needed <= empirical_threshold)
246 ? std::size_t(128)
247 : std::size_t(256);
248
249 const std::size_t n_groups =
250 ((nelems + lws * elems_per_wi - 1) / (lws * elems_per_wi));
251 const auto gws_range = sycl::range<1>(n_groups * lws);
252 const auto lws_range = sycl::range<1>(lws);
253
254 const T *in_tp = reinterpret_cast<const T *>(in_cp);
255 T *out_tp = reinterpret_cast<T *>(out_cp);
256
257 sycl::event comp_ev = exec_q.submit([&](sycl::handler &cgh) {
258 cgh.depends_on(depends);
259
260 using dpctl::tensor::kernels::alignment_utils::is_aligned;
261 using dpctl::tensor::kernels::alignment_utils::required_alignment;
262 if (is_aligned<required_alignment>(in_tp) &&
263 is_aligned<required_alignment>(out_tp))
264 {
265 constexpr bool enable_sg_loadstore = true;
266 using NanToNumFunc = NanToNumContigFunctor<T, scT, vec_sz, n_vecs,
267 enable_sg_loadstore>;
268
269 cgh.parallel_for<NanToNumFunc>(
270 sycl::nd_range<1>(gws_range, lws_range),
271 NanToNumFunc(in_tp, out_tp, nelems, nan, posinf, neginf));
272 }
273 else {
274 constexpr bool disable_sg_loadstore = false;
275 using NanToNumFunc = NanToNumContigFunctor<T, scT, vec_sz, n_vecs,
276 disable_sg_loadstore>;
277
278 cgh.parallel_for<NanToNumFunc>(
279 sycl::nd_range<1>(gws_range, lws_range),
280 NanToNumFunc(in_tp, out_tp, nelems, nan, posinf, neginf));
281 }
282 });
283
284 return comp_ev;
285}
286
287} // namespace dpnp::kernels::nan_to_num