forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Loops.h
395 lines (347 loc) · 14.5 KB
/
Loops.h
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#pragma once
// This file provides two functions to help write elementwise kernels:
//
// cpu_kernel(TensorIterator iter, <lambda>)
// cpu_kernel_vec(TensorIterator iter, <lambda>, <vec_lambda>)
//
// Both functions may generate vectorized code. The cpu_kernel implementation
// relies on the compiler's auto-vectorization. The cpu_kernel_vec
// implementation uses x86 SIMD intrinsics when available. These functions
// are only intended to be used in the ATen/native/cpu subdirectory, since files
// in other directories are not compiled with AVX/AVX2 enabled. See README.md
// for more details.
//
// For example, to write a multiplication kernel for float:
//
// cpu_kernel(iter, [](float a, float b) { return a * b; });
//
// Or you may write:
//
// cpu_kernel_vec(iter,
// [](float a, float b) { return a * b; },
// [](Vectorized<float> a, Vectorized<float> b) { return a * b; });
//
// See BinaryOpsKernel.cpp for the complete implementation
//
//
#include <cstdint>
#include <c10/util/C++17.h>
#include <c10/util/Load.h>
#include <c10/util/irange.h>
#include <ATen/detail/FunctionTraits.h>
#include <ATen/native/cpu/IsContiguous.h>
#include <ATen/native/TensorIterator.h>
#include <ATen/native/TensorIteratorDynamicCasting.h>
#include <ATen/cpu/vec/vec.h>
#include <tuple>
#include <utility>
namespace at::native { inline namespace CPU_CAPABILITY {
using namespace vec;
template <typename traits, std::size_t... INDEX>
typename traits::ArgsTuple
dereference_impl(char* C10_RESTRICT data[], const int64_t* strides, int64_t i,
std::index_sequence<INDEX...>) {
return std::make_tuple(
c10::load<typename traits::template arg<INDEX>::type>(
data[INDEX] + i * strides[INDEX])...);
}
template <typename traits>
typename traits::ArgsTuple
dereference(char* C10_RESTRICT data[], const int64_t* strides, int64_t i) {
using Indices = std::make_index_sequence<traits::arity>;
return dereference_impl<traits>(data, strides, i, Indices{});
}
template <typename traits, std::size_t... INDEX>
typename traits::ArgsTuple
dereference_vec_impl(char* C10_RESTRICT data[],
const typename traits::result_type& opt_scalar,
size_t S,
int64_t i,
std::index_sequence<INDEX...>) {
using Vec = typename traits::result_type;
using scalar_t = typename Vec::value_type;
return std::make_tuple(
S == INDEX + 1 ?
opt_scalar :
Vec::loadu(data[INDEX] + i * sizeof(scalar_t))...);
}
template <typename traits>
typename traits::ArgsTuple
dereference_vec(char* C10_RESTRICT data[], const typename traits::result_type& opt_scalar, size_t S, int64_t i) {
using Indices = std::make_index_sequence<traits::arity>;
return dereference_vec_impl<traits>(data, opt_scalar, S, i, Indices{});
}
template <typename func_t,
std::enable_if_t<!std::is_void_v<typename function_traits<func_t>::result_type>>* = nullptr>
inline void
execute_op(char* C10_RESTRICT data[], const int64_t* strides, int64_t i, int64_t n, func_t&& op) {
using traits = function_traits<func_t>;
using result_type = typename traits::result_type;
for (; i < n; i++) {
result_type* out_ptr = (result_type*)(data[0] + i * strides[0]);
*out_ptr = c10::guts::apply(op, dereference<traits>(
&data[1],
&strides[1],
i));
}
}
template <typename func_t,
std::enable_if_t<std::is_void_v<typename function_traits<func_t>::result_type>>* = nullptr>
inline void
execute_op(char* C10_RESTRICT data[], const int64_t* strides, int64_t i, int64_t n, func_t&& op) {
using traits = function_traits<func_t>;
for (; i < n; i++) {
c10::guts::apply(op, dereference<traits>(
&data[0],
&strides[0],
i));
}
}
// Basic loop operation (one output, N inputs). May be auto-vectorized
// by the compiler. Supports inputs and outputs of different types.
template <typename func_t>
inline void
basic_loop(char* C10_RESTRICT data[], const int64_t* strides_, int64_t i, int64_t n, func_t&& op) {
using traits = function_traits<func_t>;
constexpr int ntensors = traits::arity + 1;
// Copying strides to temporary array helps auto vectorization in older GCC
// versions.
int64_t strides[ntensors];
for (const auto arg : c10::irange(ntensors)) {
strides[arg] = strides_[arg];
}
execute_op(data, strides, i, n, std::forward<func_t>(op));
}
// the recursive variadic template for iterating over the returned tuple
template<class T, size_t N>
struct TupleOutput {
static void handle(char *C10_RESTRICT data[], const int64_t *strides, int64_t i,
const T &tuple) {
TupleOutput<T, N - 1>::handle(data, strides, i, tuple);
auto output = std::get<N - 1>(tuple);
using output_type = decltype(output);
output_type * out_ptr = (output_type *)(data[N - 1] + i * strides[N - 1]);
*out_ptr = output;
}
};
// Base case for the above recursive template
template<class T>
struct TupleOutput<T, 1> {
static void handle(char *C10_RESTRICT data[], const int64_t *strides, int64_t i,
const T &tuple) {
auto output = std::get<0>(tuple);
using output_type = decltype(output);
output_type* out_ptr = (output_type *)(data[0] + i * strides[0]);
*out_ptr = output;
}
};
template<class... Args>
void handle_tuple_outputs(char* C10_RESTRICT data[],
const int64_t* strides,
int64_t i,
const std::tuple<Args...> &tuple) {
TupleOutput<decltype(tuple), sizeof...(Args)>::handle(data, strides, i, tuple);
}
// Loop operation for `cpu_kernel_multiple_outputs`.
// 1. Use `c10::guts::apply` to make dynamic method invocation
// for the lambda passed in `cpu_kernel_multiple_outputs`.
// 2. Iterate over the members of the returned tuple, set the corresponding
// output tensor by the tuple member in `handle_tuple_outputs` function.
template <typename func_t>
inline void
multiple_outputs_loop(char* C10_RESTRICT data[], const int64_t* strides_, int64_t i, int64_t n, func_t&& op) {
using traits = function_traits<func_t>;
using result_type = typename traits::result_type;
constexpr int num_outputs = std::tuple_size_v<result_type>;
constexpr int ntensors = traits::arity + num_outputs;
// Copying strides to temporary array helps auto vectorization in older GCC
// versions.
int64_t strides[ntensors];
for (const auto arg : c10::irange(ntensors)) {
strides[arg] = strides_[arg];
}
for (; i < n; i++) {
auto output = c10::guts::apply(op, dereference<traits>(
&data[num_outputs],
&strides[num_outputs],
i));
handle_tuple_outputs(data, strides, i, output);
}
}
// Explicitly vectorized loop implementation. All inputs and outputs must be
// the same type and contiguous with one exception: a single input may be
// a scalar (stride 0). It's position is indicated by the argument `S`. If `S`
// is 0, then there are no scalar inputs.
template <typename func_t, typename vec_func_t>
inline void
vectorized_loop(char** C10_RESTRICT data_, int64_t n, int64_t S, func_t&& op, vec_func_t&& vop) {
using traits = function_traits<vec_func_t>;
using scalar_t = typename function_traits<func_t>::result_type;
using Vec = Vectorized<scalar_t>;
constexpr int ntensors = traits::arity + 1;
char* C10_RESTRICT data[ntensors];
for (const auto arg : c10::irange(ntensors)) {
data[arg] = data_[arg];
}
Vec opt_scalar = Vec(S > 0 ? *(scalar_t*)data[S] : scalar_t(0));
int64_t i = 0;
for (; i <= n - 2 * Vec::size(); i += 2 * Vec::size()) {
auto args1 = dereference_vec<traits>(&data[1], opt_scalar, S, i);
auto args2 = dereference_vec<traits>(&data[1], opt_scalar, S, i + Vec::size());
auto out1 = c10::guts::apply(vop, std::move(args1));
auto out2 = c10::guts::apply(vop, std::move(args2));
out1.store(data[0] + i * sizeof(scalar_t));
out2.store(data[0] + (i + Vec::size()) * sizeof(scalar_t));
}
if (i < n) {
int64_t strides[ntensors];
for (const auto arg : c10::irange(ntensors)) {
strides[arg] = (S > 0 && arg == S) ? 0 : sizeof(scalar_t);
}
basic_loop(data, strides, i, n, std::forward<func_t>(op));
}
}
template <typename traits, typename cb_t>
inline void unroll_contiguous_scalar_checks(
const int64_t* /*strides*/,
std::index_sequence<>,
cb_t&& cb) {
cb(0);
}
template <typename traits, typename cb_t, size_t INDEX0, size_t ...INDEX>
inline void unroll_contiguous_scalar_checks(
const int64_t* strides,
std::index_sequence<INDEX0, INDEX...>,
cb_t&& cb) {
if (is_contiguous_scalar<traits, INDEX0 + 1>(strides)) {
cb(INDEX0 + 1);
} else {
unroll_contiguous_scalar_checks<traits>(strides, std::index_sequence<INDEX...>{}, std::forward<cb_t>(cb));
}
}
template <typename op_t, typename vop_t>
struct VectorizedLoop2d {
op_t op;
vop_t vop;
using traits = function_traits<op_t>;
static constexpr int ntensors = traits::arity + 1;
using data_t = std::array<char*, ntensors>;
VectorizedLoop2d(op_t op, vop_t vop):
op(std::move(op)), vop(std::move(vop)) {}
static void advance(data_t &data, const int64_t *outer_strides) {
for (const auto arg : c10::irange(data.size())) {
data[arg] += outer_strides[arg];
}
}
void operator()(char** base, const int64_t *strides, int64_t size0, int64_t size1) {
data_t data;
std::copy_n(base, ntensors, data.data());
const int64_t *outer_strides = &strides[ntensors];
if (is_contiguous<traits>(strides)) {
for ([[maybe_unused]] const auto i : c10::irange(size1)) {
vectorized_loop(data.data(), size0, 0, op, vop);
advance(data, outer_strides);
}
} else {
using Indices = std::make_index_sequence<traits::arity>;
unroll_contiguous_scalar_checks<traits>(strides, Indices{}, [&](size_t idx) {
if (idx) {
for ([[maybe_unused]] const auto i : c10::irange(size1)) {
vectorized_loop(data.data(), size0, idx, op, vop);
advance(data, outer_strides);
}
} else {
for ([[maybe_unused]] const auto i : c10::irange(size1)) {
basic_loop(data.data(), strides, 0, size0, op);
advance(data, outer_strides);
}
}
});
}
}
};
template <typename op_t, typename vop_t>
VectorizedLoop2d<op_t, vop_t> make_vectorized_loop2d(
op_t &&op, vop_t &&vop) {
return VectorizedLoop2d<op_t, vop_t>(std::forward<op_t>(op), std::forward<vop_t>(vop));
}
template <typename func_t>
void cpu_kernel(TensorIteratorBase& iter, func_t&& op, int64_t grain_size = at::internal::GRAIN_SIZE) {
using traits = function_traits<func_t>;
// this could be extended to work with void return types
TORCH_INTERNAL_ASSERT(iter.ninputs() == traits::arity);
TORCH_INTERNAL_ASSERT(iter.noutputs() == 1);
// dynamic casting not currently supported on CPU
TORCH_INTERNAL_ASSERT(!needs_dynamic_casting<func_t>::check(iter));
iter.for_each([&](char** data, const int64_t* strides, int64_t n) {
// basic loop can handle 1d slices with arbitrary strides, and 1d slices is all that
// iter.for_each is ever sending to the loop lambda
basic_loop(data, strides, 0, n, op);
}, grain_size);
iter.cast_outputs();
}
// This function helps write elementwise kernels that requires multiple outputs.
// It follows the similar structure of cpu_kernel.
// Instead of `basic_loop` function, a new `multiple_outputs_loop` function is
// manipulated to handle multiple return values.
// For now `needs_dynamic_casting` check is not added as the passed lambda (`func_t`)
// of `multiple_outputs_loop` returns `std::tuple` instead of `scalar_t`.
// The `gpu_kernel_multiple_outputs` is also implemented without this check,
// We could extend `needs_dynamic_casting` to support both `std::tuple` and
// `thrust::tuple` in the future.
template <typename func_t>
void cpu_kernel_multiple_outputs(TensorIteratorBase& iter, func_t&& op, int64_t grain_size = at::internal::GRAIN_SIZE) {
using traits = function_traits<func_t>;
TORCH_INTERNAL_ASSERT(iter.ninputs() == traits::arity);
iter.for_each([&](char** data, const int64_t* strides, int64_t n) {
multiple_outputs_loop(data, strides, 0, n, op);
}, grain_size);
iter.cast_outputs();
}
template <bool check_dynamic_cast=true, typename func_t, typename vec_func_t>
void cpu_kernel_vec(TensorIteratorBase& iter, func_t&& op, vec_func_t&& vop, int64_t grain_size = at::internal::GRAIN_SIZE) {
using traits = function_traits<func_t>;
// this could be extended to work with void return types
TORCH_INTERNAL_ASSERT(iter.ninputs() == traits::arity);
TORCH_INTERNAL_ASSERT(iter.noutputs() == 1);
// dynamic casting not currently supported on CPU, but some kernels (like Fill)
// explicitly dynamic_cast, so we give the opt-out of checking.
if constexpr (check_dynamic_cast) {
TORCH_INTERNAL_ASSERT(!needs_dynamic_casting<func_t>::check(iter));
}
iter.for_each(make_vectorized_loop2d(std::forward<func_t>(op), std::forward<vec_func_t>(vop)), grain_size);
iter.cast_outputs();
}
template <typename func_t>
void cpu_serial_kernel(TensorIteratorBase& iter, func_t&& op, const Range& range) {
using traits = function_traits<func_t>;
constexpr bool result_void = std::is_void_v<typename traits::result_type>;
TORCH_INTERNAL_ASSERT(iter.ninputs() == traits::arity &&
((result_void && iter.noutputs() == 0) || (!result_void && iter.noutputs() == 1)));
// dynamic casting not currently supported on CPU
TORCH_INTERNAL_ASSERT(!needs_dynamic_casting<func_t>::check(iter));
iter.serial_for_each([&](char** data, const int64_t* strides, int64_t n) {
basic_loop(data, strides, 0, n, op);
}, range);
iter.cast_outputs();
}
template <typename func_t>
void cpu_serial_kernel(TensorIteratorBase& iter, func_t&& op) {
cpu_serial_kernel(iter, std::forward<func_t>(op), {0, iter.numel()});
}
template <typename func_t, typename vec_func_t>
void cpu_serial_kernel_vec(TensorIteratorBase& iter, func_t&& op, vec_func_t&& vop, const Range& range) {
using traits = function_traits<func_t>;
// this could be extended to work with void return types
TORCH_INTERNAL_ASSERT(iter.ninputs() == traits::arity);
TORCH_INTERNAL_ASSERT(iter.noutputs() == 1);
// dynamic casting not currently supported on CPU
TORCH_INTERNAL_ASSERT(!needs_dynamic_casting<func_t>::check(iter));
iter.serial_for_each(make_vectorized_loop2d(std::forward<func_t>(op), std::forward<vec_func_t>(vop)), range);
iter.cast_outputs();
}
template <typename func_t, typename vec_func_t>
void cpu_serial_kernel_vec(TensorIteratorBase& iter, func_t&& op, vec_func_t&& vop) {
cpu_serial_kernel_vec(iter, std::forward<func_t>(op), std::forward<vec_func_t>(vop), {0, iter.numel()});
}
}} // namespace at::native::<anonymous>