forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReduceOpsKernel.cpp
216 lines (194 loc) · 6.69 KB
/
ReduceOpsKernel.cpp
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
#include <numeric>
#include <iterator>
#include <algorithm>
#include <limits>
#include <ATen/Dispatch.h>
#include <ATen/cpu/vec256/vec256.h>
#include <ATen/native/ReduceOps.h>
#include <ATen/native/TensorIterator.h>
#include <ATen/native/SharedReduceOps.h>
#include <ATen/native/cpu/Reduce.h>
#include <c10/util/Optional.h>
namespace at { namespace native { namespace {
using namespace vec256;
static void sum_kernel_impl(TensorIterator& iter) {
AT_DISPATCH_ALL_TYPES_AND_COMPLEX_AND2(
ScalarType::BFloat16, ScalarType::Bool, iter.dtype(), "sum_cpu", [&] {
binary_kernel_reduce_vec(
iter, [=](scalar_t a, scalar_t b) -> scalar_t { return a + b; },
[=](Vec256<scalar_t> a, Vec256<scalar_t> b) { return a + b; });
});
}
static void mean_kernel_impl(TensorIterator& iter) {
AT_DISPATCH_ALL_TYPES_AND_COMPLEX(iter.dtype(), "mean_cpu", [&] {
scalar_t factor = scalar_t(iter.num_output_elements()) / scalar_t(iter.numel());
binary_kernel_reduce(
iter,
MeanOps<scalar_t, scalar_t> {factor},
scalar_t(0)
);
});
}
static void std_var_kernel_impl(TensorIterator &iter, bool unbiased, bool take_sqrt) {
AT_DISPATCH_FLOATING_TYPES_AND_HALF(iter.dtype(), "std_cpu", [&] {
binary_kernel_reduce(
iter,
WelfordOps<scalar_t, double, int64_t, double, std::tuple<scalar_t, scalar_t>> { unbiased, take_sqrt },
WelfordData<double, int64_t, double>()
);
});
}
static void prod_kernel_impl(TensorIterator& iter) {
AT_DISPATCH_ALL_TYPES_AND_COMPLEX(iter.dtype(), "prod_cpu", [&] {
binary_kernel_reduce_vec(
iter,
[=](scalar_t a, scalar_t b) -> scalar_t { return a * b; },
[=](Vec256<scalar_t> a, Vec256<scalar_t> b) { return a * b; },
/*identity=*/1);
});
}
static void norm_kernel_tensor_iterator_impl(
TensorIterator& iter,
Scalar p) {
float val;
if (p.isIntegral(false)) {
val = p.to<int64_t>();
} else if (p.isFloatingPoint()) {
val = p.to<float>();
} else {
AT_ERROR("norm_kernel_tensor_iterator_impl expects norm to be integer or float");
}
if (val == 0) {
AT_DISPATCH_FLOATING_AND_COMPLEX_TYPES(iter.dtype(), "norm_cpu", [&] {
binary_kernel_reduce(
iter,
NormZeroOps<scalar_t>(),
scalar_t(0)
);
});
} else if (val == 1) {
AT_DISPATCH_FLOATING_AND_COMPLEX_TYPES(iter.dtype(), "norm_cpu", [&] {
binary_kernel_reduce(
iter,
NormOneOps<scalar_t>(),
scalar_t(0)
);
});
} else if (val == INFINITY) {
AT_DISPATCH_FLOATING_AND_COMPLEX_TYPES(iter.dtype(), "norm_cpu", [&] {
binary_kernel_reduce(
iter,
AbsMaxOps<scalar_t>(),
scalar_t(std::numeric_limits<scalar_t>::min())
);
});
} else if (val == -INFINITY) {
AT_DISPATCH_FLOATING_AND_COMPLEX_TYPES(iter.dtype(), "norm_cpu", [&] {
binary_kernel_reduce(
iter,
AbsMinOps<scalar_t>(),
scalar_t(std::numeric_limits<scalar_t>::max())
);
});
} else {
AT_DISPATCH_FLOATING_AND_COMPLEX_TYPES(iter.dtype(), "norm_cpu", [&] {
binary_kernel_reduce(
iter,
NormOps<scalar_t> { scalar_t(val) },
scalar_t(0)
);
});
}
}
static void and_kernel_impl(TensorIterator& iter) {
binary_kernel_reduce_vec(
iter,
[=](uint8_t a, uint8_t b) -> uint8_t { return a && b; },
[=](Vec256<uint8_t> a, Vec256<uint8_t> b) {
// Adding the implementation here instead of in vec256_base to avoid
// return value inconsistency. Other comparison operators in vec256_base
// return -1/0 (all bit 1 / all bit 0) as true/false to follow the AVX2
// convention. This would be convenient when combined with other
// vectorized operations. For example, one can use the logical operation
// results as a mask for a bit operation to retrieve/reset multiple
// elements in a vector.
//
// In this method, users would expect, e.g., all(), to return 1/0 as
// true/false.
Vec256<uint8_t> c = Vec256<uint8_t>();
for (int i = 0; i != Vec256<uint8_t>::size(); i++) {
c[i] = a[i] && b[i];
}
return c;
},
/*ident=*/true);
}
static void or_kernel_impl(TensorIterator& iter) {
binary_kernel_reduce_vec(
iter,
[=](uint8_t a, uint8_t b) -> uint8_t { return a || b; },
[=](Vec256<uint8_t> a, Vec256<uint8_t> b) {
Vec256<uint8_t> c = Vec256<uint8_t>();
for (int i = 0; i != Vec256<uint8_t>::size(); i++) {
c[i] = a[i] || b[i];
}
return c;
},
/*ident=*/false);
}
static void min_values_kernel_impl(TensorIterator& iter) {
AT_DISPATCH_ALL_TYPES_AND_COMPLEX(iter.dtype(), "min_values_cpu", [&iter] {
binary_kernel_reduce_vec(
iter,
[](scalar_t a, scalar_t b) -> scalar_t { return min_impl(a, b); },
[](Vec256<scalar_t> a, Vec256<scalar_t> b) { return minimum(a, b); });
});
}
static void max_values_kernel_impl(TensorIterator& iter) {
AT_DISPATCH_ALL_TYPES_AND_COMPLEX(iter.dtype(), "max_values_cpu", [&iter] {
binary_kernel_reduce_vec(
iter,
[](scalar_t a, scalar_t b) -> scalar_t { return max_impl(a, b); },
[](Vec256<scalar_t> a, Vec256<scalar_t> b) { return maximum(a, b); });
});
}
// Maximum and minimum possible scalar values, including infinities
template <typename scalar_t>
constexpr scalar_t upper_bound() {
using lim = std::numeric_limits<scalar_t>;
return lim::has_infinity ? lim::infinity() : lim::max();
}
template <typename scalar_t>
constexpr scalar_t lower_bound() {
using lim = std::numeric_limits<scalar_t>;
return lim::has_infinity ? -lim::infinity() : lim::lowest();
}
static void argmax_kernel_impl(TensorIterator &iter) {
AT_DISPATCH_ALL_TYPES(iter.dtype(1), "argmax_cpu", [&] {
binary_kernel_reduce(
iter,
ArgMaxOps<scalar_t>{},
std::pair<scalar_t, int64_t>(lower_bound<scalar_t>(), -1));
});
}
static void argmin_kernel_impl(TensorIterator &iter) {
AT_DISPATCH_ALL_TYPES(iter.dtype(1), "argmin_cpu", [&] {
binary_kernel_reduce(
iter,
ArgMinOps<scalar_t>{},
std::pair<scalar_t, int64_t>(upper_bound<scalar_t>(), -1));
});
}
} // anonymous namespace
REGISTER_DISPATCH(sum_stub, &sum_kernel_impl);
REGISTER_DISPATCH(std_var_stub, &std_var_kernel_impl);
REGISTER_DISPATCH(prod_stub, &prod_kernel_impl);
REGISTER_DISPATCH(mean_stub, &mean_kernel_impl);
REGISTER_DISPATCH(norm_stub, &norm_kernel_tensor_iterator_impl);
REGISTER_DISPATCH(and_stub, &and_kernel_impl);
REGISTER_DISPATCH(or_stub, &or_kernel_impl);
REGISTER_DISPATCH(min_values_stub, &min_values_kernel_impl);
REGISTER_DISPATCH(max_values_stub, &max_values_kernel_impl);
REGISTER_DISPATCH(argmax_stub, &argmax_kernel_impl);
REGISTER_DISPATCH(argmin_stub, &argmin_kernel_impl);
}} // namespace at::native