13 #include <Eigen/CXX11/Tensor> 17 template <
int DataLayout>
22 array<ptrdiff_t, 0> reduction_axis;
25 VERIFY_IS_EQUAL(result(), tensor());
31 array<ptrdiff_t, 0> reduction_axis;
35 for (
int i = 0; i < 7; ++i) {
36 VERIFY_IS_EQUAL(result(i), tensor(i));
43 array<ptrdiff_t, 0> reduction_axis;
48 for (
int i = 0; i < 2; ++i) {
49 for (
int j = 0; j < 3; ++j) {
50 VERIFY_IS_EQUAL(result(i, j), tensor(i, j));
56 template <
int DataLayout>
60 array<ptrdiff_t, 2> reduction_axis2;
61 reduction_axis2[0] = 1;
62 reduction_axis2[1] = 3;
67 for (
int i = 0; i < 2; ++i) {
68 for (
int j = 0; j < 5; ++j) {
70 for (
int k = 0; k < 3; ++k) {
71 for (
int l = 0; l < 7; ++l) {
72 sum += tensor(i, k, j, l);
75 VERIFY_IS_APPROX(result(i, j), sum);
81 VERIFY_IS_EQUAL(sum1.
rank(), 0);
83 array<ptrdiff_t, 4> reduction_axis4;
84 reduction_axis4[0] = 0;
85 reduction_axis4[1] = 1;
86 reduction_axis4[2] = 2;
87 reduction_axis4[3] = 3;
89 VERIFY_IS_EQUAL(sum2.
rank(), 0);
91 VERIFY_IS_APPROX(sum1(), sum2());
94 reduction_axis2[0] = 0;
95 reduction_axis2[1] = 2;
96 result = tensor.prod(reduction_axis2);
99 for (
int i = 0; i < 3; ++i) {
100 for (
int j = 0; j < 7; ++j) {
102 for (
int k = 0; k < 2; ++k) {
103 for (
int l = 0; l < 5; ++l) {
104 prod *= tensor(k, i, l, j);
107 VERIFY_IS_APPROX(result(i, j), prod);
113 VERIFY_IS_EQUAL(prod1.
rank(), 0);
115 array<ptrdiff_t, 4> reduction_axis4;
116 reduction_axis4[0] = 0;
117 reduction_axis4[1] = 1;
118 reduction_axis4[2] = 2;
119 reduction_axis4[3] = 3;
121 VERIFY_IS_EQUAL(prod2.
rank(), 0);
123 VERIFY_IS_APPROX(prod1(), prod2());
126 reduction_axis2[0] = 0;
127 reduction_axis2[1] = 2;
128 result = tensor.maximum(reduction_axis2);
131 for (
int i = 0; i < 3; ++i) {
132 for (
int j = 0; j < 7; ++j) {
133 float max_val = std::numeric_limits<float>::lowest();
134 for (
int k = 0; k < 2; ++k) {
135 for (
int l = 0; l < 5; ++l) {
136 max_val = (
std::max)(max_val, tensor(k, i, l, j));
139 VERIFY_IS_APPROX(result(i, j), max_val);
145 VERIFY_IS_EQUAL(max1.
rank(), 0);
147 array<ptrdiff_t, 4> reduction_axis4;
148 reduction_axis4[0] = 0;
149 reduction_axis4[1] = 1;
150 reduction_axis4[2] = 2;
151 reduction_axis4[3] = 3;
153 VERIFY_IS_EQUAL(max2.
rank(), 0);
155 VERIFY_IS_APPROX(max1(), max2());
158 reduction_axis2[0] = 0;
159 reduction_axis2[1] = 1;
160 result = tensor.minimum(reduction_axis2);
163 for (
int i = 0; i < 5; ++i) {
164 for (
int j = 0; j < 7; ++j) {
166 for (
int k = 0; k < 2; ++k) {
167 for (
int l = 0; l < 3; ++l) {
168 min_val = (std::min)(min_val, tensor(k, l, i, j));
171 VERIFY_IS_APPROX(result(i, j), min_val);
177 VERIFY_IS_EQUAL(min1.
rank(), 0);
179 array<ptrdiff_t, 4> reduction_axis4;
180 reduction_axis4[0] = 0;
181 reduction_axis4[1] = 1;
182 reduction_axis4[2] = 2;
183 reduction_axis4[3] = 3;
185 VERIFY_IS_EQUAL(min2.
rank(), 0);
187 VERIFY_IS_APPROX(min1(), min2());
190 reduction_axis2[0] = 0;
191 reduction_axis2[1] = 1;
192 result = tensor.mean(reduction_axis2);
195 for (
int i = 0; i < 5; ++i) {
196 for (
int j = 0; j < 7; ++j) {
199 for (
int k = 0; k < 2; ++k) {
200 for (
int l = 0; l < 3; ++l) {
201 sum += tensor(k, l, i, j);
205 VERIFY_IS_APPROX(result(i, j), sum / count);
211 VERIFY_IS_EQUAL(mean1.
rank(), 0);
213 array<ptrdiff_t, 4> reduction_axis4;
214 reduction_axis4[0] = 0;
215 reduction_axis4[1] = 1;
216 reduction_axis4[2] = 2;
217 reduction_axis4[3] = 3;
219 VERIFY_IS_EQUAL(mean2.
rank(), 0);
221 VERIFY_IS_APPROX(mean1(), mean2());
228 TensorFixedSize<bool, Sizes<> > all;
231 all = (ints >= ints.constant(0)).all();
234 TensorFixedSize<bool, Sizes<> > any;
235 any = (ints > ints.constant(10)).any();
237 any = (ints < ints.constant(1)).any();
243 template <
int DataLayout>
247 array<ptrdiff_t, 2> reduction_axis2;
248 reduction_axis2[0] = 1;
249 reduction_axis2[1] = 3;
252 result = result.constant(1.0
f) - tensor.sum(reduction_axis2);
255 for (
int i = 0; i < 2; ++i) {
256 for (
int j = 0; j < 5; ++j) {
258 for (
int k = 0; k < 3; ++k) {
259 for (
int l = 0; l < 7; ++l) {
260 sum += tensor(i, k, j, l);
263 VERIFY_IS_APPROX(result(i, j), 1.0
f - sum);
269 template <
int DataLayout>
273 array<ptrdiff_t, 2> reduction_axis;
274 reduction_axis[0] = 0;
275 reduction_axis[1] = 1;
278 VERIFY_IS_EQUAL(result.
rank(), 0);
281 for (
int i = 0; i < 2; ++i) {
282 for (
int j = 0; j < 3; ++j) {
286 VERIFY_IS_APPROX(result(0), sum);
288 result = tensor.square().sum(reduction_axis).sqrt();
289 VERIFY_IS_EQUAL(result.
rank(), 0);
292 for (
int i = 0; i < 2; ++i) {
293 for (
int j = 0; j < 3; ++j) {
294 sum += tensor(i, j) * tensor(i, j);
297 VERIFY_IS_APPROX(result(), sqrtf(sum));
303 void reduce(
const float val,
float* accum) { *accum += val * val; }
311 template <
int DataLayout>
315 array<ptrdiff_t, 1> reduction_axis;
316 reduction_axis[0] = 1;
321 for (
int i = 0; i < 5; ++i) {
322 float expected = 10.0f;
323 for (
int j = 0; j < 7; ++j) {
324 expected += tensor(i, j) * tensor(i, j);
326 expected = 1.0f / expected;
327 VERIFY_IS_APPROX(result(i), expected);
331 template <
int DataLayout>
333 int inputs[2 * 3 * 5 * 7];
334 TensorMap<Tensor<int, 4, DataLayout> > tensor_map(inputs, 2, 3, 5, 7);
335 TensorMap<Tensor<const int, 4, DataLayout> > tensor_map_const(inputs, 2, 3, 5,
337 const TensorMap<Tensor<const int, 4, DataLayout> > tensor_map_const_const(
340 tensor_map.setRandom();
341 array<ptrdiff_t, 2> reduction_axis;
342 reduction_axis[0] = 1;
343 reduction_axis[1] = 3;
348 tensor_map_const_const.sum(reduction_axis);
350 for (
int i = 0; i < 2; ++i) {
351 for (
int j = 0; j < 5; ++j) {
353 for (
int k = 0; k < 3; ++k) {
354 for (
int l = 0; l < 7; ++l) {
355 sum += tensor_map(i, k, j, l);
358 VERIFY_IS_EQUAL(result(i, j), sum);
359 VERIFY_IS_EQUAL(result2(i, j), sum);
360 VERIFY_IS_EQUAL(result3(i, j), sum);
365 template <
int DataLayout>
371 #if !EIGEN_HAS_CONSTEXPR 372 array<int, 2> reduction_axis;
373 reduction_axis[0] = 1;
374 reduction_axis[1] = 3;
376 Eigen::IndexList<Eigen::type2index<1>, Eigen::type2index<3> > reduction_axis;
379 out = in.maximum(reduction_axis);
381 for (
int i = 0; i < 72; ++i) {
382 for (
int j = 0; j < 97; ++j) {
383 float expected = -1e10f;
384 for (
int k = 0; k < 53; ++k) {
385 for (
int l = 0; l < 113; ++l) {
386 expected = (
std::max)(expected, in(i, k, j, l));
389 VERIFY_IS_APPROX(out(i, j), expected);
394 template <
int DataLayout>
401 #if !EIGEN_HAS_CONSTEXPR 402 array<int, 2> reduction_axis;
403 reduction_axis[0] = 0;
404 reduction_axis[1] = 1;
407 Eigen::IndexList<Eigen::type2index<0>, Eigen::type2index<1> > reduction_axis;
410 out = in.maximum(reduction_axis);
412 for (
int i = 0; i < 97; ++i) {
413 for (
int j = 0; j < 113; ++j) {
414 float expected = -1e10f;
415 for (
int k = 0; k < 53; ++k) {
416 for (
int l = 0; l < 72; ++l) {
417 expected = (
std::max)(expected, in(l, k, i, j));
420 VERIFY_IS_APPROX(out(i, j), expected);
425 template <
int DataLayout>
432 #if !EIGEN_HAS_CONSTEXPR 433 array<int, 2> reduction_axis;
434 reduction_axis[0] = 2;
435 reduction_axis[1] = 3;
438 Eigen::IndexList<Eigen::type2index<2>, Eigen::type2index<3>> reduction_axis;
441 out = in.maximum(reduction_axis);
443 for (
int i = 0; i < 72; ++i) {
444 for (
int j = 0; j < 53; ++j) {
445 float expected = -1e10f;
446 for (
int k = 0; k < 97; ++k) {
447 for (
int l = 0; l < 113; ++l) {
448 expected = (
std::max)(expected, in(i, j, k, l));
451 VERIFY_IS_APPROX(out(i, j), expected);
456 template <
int DataLayout>
463 #if !EIGEN_HAS_CONSTEXPR 464 array<int, 2> reduction_axis;
465 reduction_axis[0] = 1;
466 reduction_axis[1] = 2;
469 Eigen::IndexList<Eigen::type2index<1>, Eigen::type2index<2>> reduction_axis;
472 out = in.maximum(reduction_axis);
474 for (
int i = 0; i < 72; ++i) {
475 for (
int j = 0; j < 113; ++j) {
476 float expected = -1e10f;
477 for (
int k = 0; k < 53; ++k) {
478 for (
int l = 0; l < 97; ++l) {
479 expected = (
std::max)(expected, in(i, k, l, j));
482 VERIFY_IS_APPROX(out(i, j), expected);
488 CALL_SUBTEST(test_trivial_reductions<ColMajor>());
489 CALL_SUBTEST(test_trivial_reductions<RowMajor>());
490 CALL_SUBTEST(test_simple_reductions<ColMajor>());
491 CALL_SUBTEST(test_simple_reductions<RowMajor>());
492 CALL_SUBTEST(test_reductions_in_expr<ColMajor>());
493 CALL_SUBTEST(test_reductions_in_expr<RowMajor>());
494 CALL_SUBTEST(test_full_reductions<ColMajor>());
495 CALL_SUBTEST(test_full_reductions<RowMajor>());
496 CALL_SUBTEST(test_user_defined_reductions<ColMajor>());
497 CALL_SUBTEST(test_user_defined_reductions<RowMajor>());
498 CALL_SUBTEST(test_tensor_maps<ColMajor>());
499 CALL_SUBTEST(test_tensor_maps<RowMajor>());
500 CALL_SUBTEST(test_static_dims<ColMajor>());
501 CALL_SUBTEST(test_static_dims<RowMajor>());
502 CALL_SUBTEST(test_innermost_last_dims<ColMajor>());
503 CALL_SUBTEST(test_innermost_last_dims<RowMajor>());
504 CALL_SUBTEST(test_innermost_first_dims<ColMajor>());
505 CALL_SUBTEST(test_innermost_first_dims<RowMajor>());
506 CALL_SUBTEST(test_reduce_middle_dims<ColMajor>());
507 CALL_SUBTEST(test_reduce_middle_dims<RowMajor>());
static void test_trivial_reductions()
static void test_reductions_in_expr()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(std::size_t n) const
static void test_tensor_maps()
void test_cxx11_tensor_reduction()
static int f(const TensorMap< Tensor< int, 3 > > &tensor)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor< Scalar_, NumIndices_, Options_, IndexType_ > & setRandom()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank() const
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half() max(const half &a, const half &b)
float finalize(const float accum) const
static void test_innermost_last_dims()
static void test_full_reductions()
const mpreal sum(const mpreal tab[], const unsigned long int n, int &status, mp_rnd_t mode=mpreal::get_default_rnd())
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar * data()
void reduce(const float val, float *accum)
static const bool PacketAccess
UserReducer(float offset)
static void test_innermost_first_dims()
static void test_static_dims()
static void test_reduce_middle_dims()
static void test_user_defined_reductions()
static void test_simple_reductions()