forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Loops.h
249 lines (221 loc) · 8.79 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
#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; },
// [](Vec256<float> a, Vec256<float> b) { return a * b; });
//
// See BinaryOpsKernel.cpp for the complete implementation
//
//
#include <stdint.h>
#include <c10/util/C++17.h>
#include <ATen/detail/FunctionTraits.h>
#include <ATen/native/cpu/IsContiguous.h>
#include <ATen/native/TensorIterator.h>
#include <ATen/cpu/vec256/vec256.h>
#ifndef _MSC_VER
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#endif
namespace at { namespace native { namespace {
using namespace vec256;
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(
*(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,
typename std::enable_if<!std::is_void<typename function_traits<func_t>::result_type>::value>::type* = nullptr>
static 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(std::forward<func_t>(op), dereference<traits>(
&data[1],
&strides[1],
i));
}
}
template <typename func_t,
typename std::enable_if<std::is_void<typename function_traits<func_t>::result_type>::value>::type* = nullptr>
static 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(std::forward<func_t>(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>
static 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 (int arg = 0; arg < ntensors; arg++) {
strides[arg] = strides_[arg];
}
execute_op(data, strides, i, n, std::forward<func_t>(op));
}
// 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>
static 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 = Vec256<scalar_t>;
constexpr int ntensors = traits::arity + 1;
char* C10_RESTRICT data[ntensors];
for (int arg = 0; arg < ntensors; arg++) {
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(std::forward<vec_func_t>(vop), std::move(args1));
auto out2 = c10::guts::apply(std::forward<vec_func_t>(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 (int arg = 0; arg < ntensors; arg++) {
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>
static 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>
static 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 func_t>
void cpu_kernel(TensorIterator& iter, func_t&& op) {
using traits = function_traits<func_t>;
TORCH_INTERNAL_ASSERT(iter.ntensors() >= traits::arity + 1);
iter.for_each([&](char** data, const int64_t* strides, int64_t n) {
if (is_contiguous<traits>(strides)) {
basic_loop(data, strides, 0, n, std::forward<func_t>(op));
} else {
using Indices = std::make_index_sequence<traits::arity>;
unroll_contiguous_scalar_checks<traits>(strides, Indices{}, [&](size_t _idx) {
basic_loop(data, strides, 0, n, std::forward<func_t>(op));
});
}
});
iter.cast_outputs();
}
template <typename func_t, typename vec_func_t>
void cpu_kernel_vec(TensorIterator& iter, func_t&& op, vec_func_t&& vop) {
using traits = function_traits<func_t>;
TORCH_INTERNAL_ASSERT(iter.ntensors() >= traits::arity + 1);
iter.for_each([&](char** data, const int64_t* strides, int64_t n) {
if (is_contiguous<traits>(strides)) {
return vectorized_loop(data, n, 0, std::forward<func_t>(op), std::forward<vec_func_t>(vop));
} else {
using Indices = std::make_index_sequence<traits::arity>;
unroll_contiguous_scalar_checks<traits>(strides, Indices{}, [&](size_t idx) {
if (idx) {
vectorized_loop(data, n, idx, std::forward<func_t>(op), std::forward<vec_func_t>(vop));
} else {
basic_loop(data, strides, 0, n, std::forward<func_t>(op));
}
});
}
});
iter.cast_outputs();
}
template <typename func_t>
void cpu_serial_kernel(TensorIterator& iter, func_t&& op) {
using traits = function_traits<func_t>;
TORCH_INTERNAL_ASSERT((std::is_void<typename traits::result_type>::value &&
iter.noutputs() == 0 && iter.ntensors() == traits::arity) || (iter.ntensors() >= traits::arity + 1));
iter.serial_for_each([&](char** data, const int64_t* strides, int64_t n) {
if (is_contiguous<traits>(strides)) {
basic_loop(data, strides, 0, n, std::forward<func_t>(op));
} else {
using Indices = std::make_index_sequence<traits::arity>;
unroll_contiguous_scalar_checks<traits>(strides, Indices{}, [&](size_t _idx) {
basic_loop(data, strides, 0, n, std::forward<func_t>(op));
});
}
}, {0, iter.numel()});
iter.cast_outputs();
}
}}} // namespace at::native::<anonymous>
#ifndef _MSC_VER
#pragma GCC diagnostic pop
#endif