Repository navigation
Expand file tree
/
Copy pathcumulative_prod.cpp
More file actions
353 lines (311 loc) · 13.6 KB
/
Copy pathcumulative_prod.cpp
File metadata and controls
353 lines (311 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//===-- ------------ Implementation of _tensor_impl module ----*-C++-*-/===//
//
// Data Parallel Control (dpctl)
//
// Copyright 2020-2025 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 functions of dpctl.tensor._tensor_impl extensions
//===--------------------------------------------------------------------===//
#include "dpctl4pybind11.hpp"
#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <sycl/sycl.hpp>
#include <type_traits>
#include <vector>
#include "accumulate_over_axis.hpp"
#include "kernels/accumulators.hpp"
#include "utils/sycl_utils.hpp"
#include "utils/type_dispatch_building.hpp"
namespace py = pybind11;
namespace dpctl
{
namespace tensor
{
namespace py_internal
{
namespace su_ns = dpctl::tensor::sycl_utils;
namespace td_ns = dpctl::tensor::type_dispatch;
namespace impl
{
using dpctl::tensor::kernels::accumulators::accumulate_1d_contig_impl_fn_ptr_t;
static accumulate_1d_contig_impl_fn_ptr_t
cumprod_1d_contig_dispatch_table[td_ns::num_types][td_ns::num_types];
using dpctl::tensor::kernels::accumulators::accumulate_strided_impl_fn_ptr_t;
static accumulate_strided_impl_fn_ptr_t
cumprod_strided_dispatch_table[td_ns::num_types][td_ns::num_types];
static accumulate_1d_contig_impl_fn_ptr_t
cumprod_1d_include_initial_contig_dispatch_table[td_ns::num_types]
[td_ns::num_types];
static accumulate_strided_impl_fn_ptr_t
cumprod_include_initial_strided_dispatch_table[td_ns::num_types]
[td_ns::num_types];
template <typename argTy, typename outTy>
struct TypePairSupportDataForProdAccumulation
{
static constexpr bool is_defined = std::disjunction<
td_ns::TypePairDefinedEntry<argTy, bool, outTy, bool>,
td_ns::TypePairDefinedEntry<argTy, bool, outTy, std::int32_t>,
td_ns::TypePairDefinedEntry<argTy, bool, outTy, std::int64_t>,
// input int8_t
td_ns::TypePairDefinedEntry<argTy, std::int8_t, outTy, std::int8_t>,
td_ns::TypePairDefinedEntry<argTy, std::int8_t, outTy, std::int32_t>,
td_ns::TypePairDefinedEntry<argTy, std::int8_t, outTy, std::int64_t>,
// input uint8_t
td_ns::TypePairDefinedEntry<argTy, std::uint8_t, outTy, std::uint8_t>,
td_ns::TypePairDefinedEntry<argTy, std::uint8_t, outTy, std::uint32_t>,
td_ns::TypePairDefinedEntry<argTy, std::uint8_t, outTy, std::uint64_t>,
// input int16_t
td_ns::TypePairDefinedEntry<argTy, std::int16_t, outTy, std::int16_t>,
td_ns::TypePairDefinedEntry<argTy, std::int16_t, outTy, std::int32_t>,
td_ns::TypePairDefinedEntry<argTy, std::int16_t, outTy, std::int64_t>,
// input uint16_t
td_ns::TypePairDefinedEntry<argTy, std::uint16_t, outTy, std::uint16_t>,
td_ns::TypePairDefinedEntry<argTy, std::uint16_t, outTy, std::uint32_t>,
td_ns::TypePairDefinedEntry<argTy, std::uint16_t, outTy, std::uint64_t>,
// input int32_t
td_ns::TypePairDefinedEntry<argTy, std::int32_t, outTy, std::int32_t>,
td_ns::TypePairDefinedEntry<argTy, std::int32_t, outTy, std::int64_t>,
// input uint32_t
td_ns::TypePairDefinedEntry<argTy, std::uint32_t, outTy, std::uint32_t>,
td_ns::TypePairDefinedEntry<argTy, std::uint32_t, outTy, std::uint64_t>,
// input int64_t
td_ns::TypePairDefinedEntry<argTy, std::int64_t, outTy, std::int64_t>,
// input uint64_t
td_ns::TypePairDefinedEntry<argTy, std::uint64_t, outTy, std::uint64_t>,
// input half
td_ns::TypePairDefinedEntry<argTy, sycl::half, outTy, sycl::half>,
// input float
td_ns::TypePairDefinedEntry<argTy, float, outTy, float>,
// input double
td_ns::TypePairDefinedEntry<argTy, double, outTy, double>,
// input std::complex
td_ns::TypePairDefinedEntry<argTy,
std::complex<float>,
outTy,
std::complex<float>>,
td_ns::TypePairDefinedEntry<argTy,
std::complex<double>,
outTy,
std::complex<double>>,
// fall-through
td_ns::NotDefinedEntry>::is_defined;
};
template <typename T>
using CumProdScanOpT = std::conditional_t<std::is_same_v<T, bool>,
sycl::logical_and<T>,
sycl::multiplies<T>>;
template <typename fnT, typename srcTy, typename dstTy>
struct CumProd1DContigFactory
{
fnT get()
{
if constexpr (TypePairSupportDataForProdAccumulation<srcTy,
dstTy>::is_defined)
{
using ScanOpT = CumProdScanOpT<dstTy>;
constexpr bool include_initial = false;
if constexpr (std::is_same_v<srcTy, dstTy>) {
using dpctl::tensor::kernels::accumulators::NoOpTransformer;
fnT fn = dpctl::tensor::kernels::accumulators::
accumulate_1d_contig_impl<srcTy, dstTy,
NoOpTransformer<dstTy>, ScanOpT,
include_initial>;
return fn;
}
else {
using dpctl::tensor::kernels::accumulators::CastTransformer;
fnT fn = dpctl::tensor::kernels::accumulators::
accumulate_1d_contig_impl<srcTy, dstTy,
CastTransformer<srcTy, dstTy>,
ScanOpT, include_initial>;
return fn;
}
}
else {
return nullptr;
}
}
};
template <typename fnT, typename srcTy, typename dstTy>
struct CumProd1DIncludeInitialContigFactory
{
fnT get()
{
if constexpr (TypePairSupportDataForProdAccumulation<srcTy,
dstTy>::is_defined)
{
using ScanOpT = CumProdScanOpT<dstTy>;
constexpr bool include_initial = true;
if constexpr (std::is_same_v<srcTy, dstTy>) {
using dpctl::tensor::kernels::accumulators::NoOpTransformer;
fnT fn = dpctl::tensor::kernels::accumulators::
accumulate_1d_contig_impl<srcTy, dstTy,
NoOpTransformer<dstTy>, ScanOpT,
include_initial>;
return fn;
}
else {
using dpctl::tensor::kernels::accumulators::CastTransformer;
fnT fn = dpctl::tensor::kernels::accumulators::
accumulate_1d_contig_impl<srcTy, dstTy,
CastTransformer<srcTy, dstTy>,
ScanOpT, include_initial>;
return fn;
}
}
else {
return nullptr;
}
}
};
template <typename fnT, typename srcTy, typename dstTy>
struct CumProdStridedFactory
{
fnT get()
{
if constexpr (TypePairSupportDataForProdAccumulation<srcTy,
dstTy>::is_defined)
{
using ScanOpT = CumProdScanOpT<dstTy>;
constexpr bool include_initial = false;
if constexpr (std::is_same_v<srcTy, dstTy>) {
using dpctl::tensor::kernels::accumulators::NoOpTransformer;
fnT fn = dpctl::tensor::kernels::accumulators::
accumulate_strided_impl<srcTy, dstTy,
NoOpTransformer<dstTy>, ScanOpT,
include_initial>;
return fn;
}
else {
using dpctl::tensor::kernels::accumulators::CastTransformer;
fnT fn = dpctl::tensor::kernels::accumulators::
accumulate_strided_impl<srcTy, dstTy,
CastTransformer<srcTy, dstTy>,
ScanOpT, include_initial>;
return fn;
}
}
else {
return nullptr;
}
}
};
template <typename fnT, typename srcTy, typename dstTy>
struct CumProdIncludeInitialStridedFactory
{
fnT get()
{
if constexpr (TypePairSupportDataForProdAccumulation<srcTy,
dstTy>::is_defined)
{
using ScanOpT = CumProdScanOpT<dstTy>;
constexpr bool include_initial = true;
if constexpr (std::is_same_v<srcTy, dstTy>) {
using dpctl::tensor::kernels::accumulators::NoOpTransformer;
fnT fn = dpctl::tensor::kernels::accumulators::
accumulate_strided_impl<srcTy, dstTy,
NoOpTransformer<dstTy>, ScanOpT,
include_initial>;
return fn;
}
else {
using dpctl::tensor::kernels::accumulators::CastTransformer;
fnT fn = dpctl::tensor::kernels::accumulators::
accumulate_strided_impl<srcTy, dstTy,
CastTransformer<srcTy, dstTy>,
ScanOpT, include_initial>;
return fn;
}
}
else {
return nullptr;
}
}
};
void populate_cumprod_dispatch_tables(void)
{
td_ns::DispatchTableBuilder<accumulate_1d_contig_impl_fn_ptr_t,
CumProd1DContigFactory, td_ns::num_types>
dtb1;
dtb1.populate_dispatch_table(cumprod_1d_contig_dispatch_table);
td_ns::DispatchTableBuilder<accumulate_strided_impl_fn_ptr_t,
CumProdStridedFactory, td_ns::num_types>
dtb2;
dtb2.populate_dispatch_table(cumprod_strided_dispatch_table);
td_ns::DispatchTableBuilder<accumulate_1d_contig_impl_fn_ptr_t,
CumProd1DIncludeInitialContigFactory,
td_ns::num_types>
dtb3;
dtb3.populate_dispatch_table(
cumprod_1d_include_initial_contig_dispatch_table);
td_ns::DispatchTableBuilder<accumulate_strided_impl_fn_ptr_t,
CumProdIncludeInitialStridedFactory,
td_ns::num_types>
dtb4;
dtb4.populate_dispatch_table(
cumprod_include_initial_strided_dispatch_table);
return;
}
} // namespace impl
void init_cumulative_prod(py::module_ m)
{
using arrayT = dpctl::tensor::usm_ndarray;
using event_vecT = std::vector<sycl::event>;
using impl::populate_cumprod_dispatch_tables;
populate_cumprod_dispatch_tables();
using impl::cumprod_1d_contig_dispatch_table;
using impl::cumprod_strided_dispatch_table;
auto cumprod_pyapi = [&](const arrayT &src, int trailing_dims_to_accumulate,
const arrayT &dst, sycl::queue &exec_q,
const event_vecT &depends = {}) {
using dpctl::tensor::py_internal::py_accumulate_over_axis;
return py_accumulate_over_axis(
src, trailing_dims_to_accumulate, dst, exec_q, depends,
cumprod_strided_dispatch_table, cumprod_1d_contig_dispatch_table);
};
m.def("_cumprod_over_axis", cumprod_pyapi, "", py::arg("src"),
py::arg("trailing_dims_to_accumulate"), py::arg("dst"),
py::arg("sycl_queue"), py::arg("depends") = py::list());
using impl::cumprod_1d_include_initial_contig_dispatch_table;
using impl::cumprod_include_initial_strided_dispatch_table;
auto cumprod_include_initial_pyapi =
[&](const arrayT &src, const arrayT &dst, sycl::queue &exec_q,
const event_vecT &depends = {}) {
using dpctl::tensor::py_internal::
py_accumulate_final_axis_include_initial;
return py_accumulate_final_axis_include_initial(
src, dst, exec_q, depends,
cumprod_include_initial_strided_dispatch_table,
cumprod_1d_include_initial_contig_dispatch_table);
};
m.def("_cumprod_final_axis_include_initial", cumprod_include_initial_pyapi,
"", py::arg("src"), py::arg("dst"), py::arg("sycl_queue"),
py::arg("depends") = py::list());
auto cumprod_dtype_supported = [&](const py::dtype &input_dtype,
const py::dtype &output_dtype) {
using dpctl::tensor::py_internal::py_accumulate_dtype_supported;
return py_accumulate_dtype_supported(input_dtype, output_dtype,
cumprod_strided_dispatch_table);
};
m.def("_cumprod_dtype_supported", cumprod_dtype_supported, "",
py::arg("arg_dtype"), py::arg("out_dtype"));
}
} // namespace py_internal
} // namespace tensor
} // namespace dpctl