13 #include <Eigen/CXX11/Tensor>
17 template <
int DataLayout>
35 for (
int i = 0;
i < 7; ++
i) {
48 for (
int i = 0;
i < 2; ++
i) {
49 for (
int j = 0;
j < 3; ++
j) {
56 template <
typename Scalar,
int DataLayout>
61 tensor += tensor.constant(
Scalar(0.5
f));
63 reduction_axis2[0] = 1;
64 reduction_axis2[1] = 3;
69 for (
int i = 0;
i < 2; ++
i) {
70 for (
int j = 0;
j < 5; ++
j) {
72 for (
int k = 0; k < 3; ++k) {
73 for (
int l = 0;
l < 7; ++
l) {
74 sum += tensor(
i, k,
j,
l);
86 reduction_axis4[0] = 0;
87 reduction_axis4[1] = 1;
88 reduction_axis4[2] = 2;
89 reduction_axis4[3] = 3;
96 reduction_axis2[0] = 0;
97 reduction_axis2[1] = 2;
98 result = tensor.prod(reduction_axis2);
101 for (
int i = 0;
i < 3; ++
i) {
102 for (
int j = 0;
j < 7; ++
j) {
104 for (
int k = 0; k < 2; ++k) {
105 for (
int l = 0;
l < 5; ++
l) {
118 reduction_axis4[0] = 0;
119 reduction_axis4[1] = 1;
120 reduction_axis4[2] = 2;
121 reduction_axis4[3] = 3;
128 reduction_axis2[0] = 0;
129 reduction_axis2[1] = 2;
130 result = tensor.maximum(reduction_axis2);
133 for (
int i = 0;
i < 3; ++
i) {
134 for (
int j = 0;
j < 7; ++
j) {
135 Scalar max_val = std::numeric_limits<Scalar>::lowest();
136 for (
int k = 0; k < 2; ++k) {
137 for (
int l = 0;
l < 5; ++
l) {
150 reduction_axis4[0] = 0;
151 reduction_axis4[1] = 1;
152 reduction_axis4[2] = 2;
153 reduction_axis4[3] = 3;
160 reduction_axis2[0] = 0;
161 reduction_axis2[1] = 1;
162 result = tensor.minimum(reduction_axis2);
165 for (
int i = 0;
i < 5; ++
i) {
166 for (
int j = 0;
j < 7; ++
j) {
168 for (
int k = 0; k < 2; ++k) {
169 for (
int l = 0;
l < 3; ++
l) {
182 reduction_axis4[0] = 0;
183 reduction_axis4[1] = 1;
184 reduction_axis4[2] = 2;
185 reduction_axis4[3] = 3;
192 reduction_axis2[0] = 0;
193 reduction_axis2[1] = 1;
194 result = tensor.mean(reduction_axis2);
197 for (
int i = 0;
i < 5; ++
i) {
198 for (
int j = 0;
j < 7; ++
j) {
201 for (
int k = 0; k < 2; ++k) {
202 for (
int l = 0;
l < 3; ++
l) {
203 sum += tensor(k,
l,
i,
j);
216 reduction_axis4[0] = 0;
217 reduction_axis4[1] = 1;
218 reduction_axis4[2] = 2;
219 reduction_axis4[3] = 3;
233 all_ = (ints >= ints.constant(0)).
all();
237 any = (ints > ints.constant(10)).any();
239 any = (ints < ints.constant(1)).any();
245 template <
int DataLayout>
250 reduction_axis2[0] = 1;
251 reduction_axis2[1] = 3;
254 result =
result.constant(1.0
f) - tensor.sum(reduction_axis2);
257 for (
int i = 0;
i < 2; ++
i) {
258 for (
int j = 0;
j < 5; ++
j) {
260 for (
int k = 0; k < 3; ++k) {
261 for (
int l = 0;
l < 7; ++
l) {
262 sum += tensor(
i, k,
j,
l);
271 template <
int DataLayout>
276 reduction_axis[0] = 0;
277 reduction_axis[1] = 1;
283 for (
int i = 0;
i < 2; ++
i) {
284 for (
int j = 0;
j < 3; ++
j) {
290 result = tensor.square().sum(reduction_axis).sqrt();
294 for (
int i = 0;
i < 2; ++
i) {
295 for (
int j = 0;
j < 3; ++
j) {
296 sum += tensor(
i,
j) * tensor(
i,
j);
305 void reduce(
const float val,
float* accum) { *accum += val * val; }
313 template <
int DataLayout>
318 reduction_axis[0] = 1;
323 for (
int i = 0;
i < 5; ++
i) {
325 for (
int j = 0;
j < 7; ++
j) {
333 template <
int DataLayout>
335 int inputs[2 * 3 * 5 * 7];
342 tensor_map.setRandom();
344 reduction_axis[0] = 1;
345 reduction_axis[1] = 3;
350 tensor_map_const_const.sum(reduction_axis);
352 for (
int i = 0;
i < 2; ++
i) {
353 for (
int j = 0;
j < 5; ++
j) {
355 for (
int k = 0; k < 3; ++k) {
356 for (
int l = 0;
l < 7; ++
l) {
357 sum += tensor_map(
i, k,
j,
l);
367 template <
int DataLayout>
373 #if !EIGEN_HAS_CONSTEXPR
375 reduction_axis[0] = 1;
376 reduction_axis[1] = 3;
378 Eigen::IndexList<Eigen::type2index<1>, Eigen::type2index<3> > reduction_axis;
381 out = in.maximum(reduction_axis);
383 for (
int i = 0;
i < 72; ++
i) {
384 for (
int j = 0;
j < 97; ++
j) {
386 for (
int k = 0; k < 53; ++k) {
387 for (
int l = 0;
l < 113; ++
l) {
396 template <
int DataLayout>
403 #if !EIGEN_HAS_CONSTEXPR
405 reduction_axis[0] = 0;
406 reduction_axis[1] = 1;
409 Eigen::IndexList<Eigen::type2index<0>, Eigen::type2index<1> > reduction_axis;
412 out = in.maximum(reduction_axis);
414 for (
int i = 0;
i < 97; ++
i) {
415 for (
int j = 0;
j < 113; ++
j) {
417 for (
int k = 0; k < 53; ++k) {
418 for (
int l = 0;
l < 72; ++
l) {
427 template <
int DataLayout>
434 #if !EIGEN_HAS_CONSTEXPR
436 reduction_axis[0] = 2;
437 reduction_axis[1] = 3;
440 Eigen::IndexList<Eigen::type2index<2>, Eigen::type2index<3>> reduction_axis;
443 out = in.maximum(reduction_axis);
445 for (
int i = 0;
i < 72; ++
i) {
446 for (
int j = 0;
j < 53; ++
j) {
448 for (
int k = 0; k < 97; ++k) {
449 for (
int l = 0;
l < 113; ++
l) {
458 template <
int DataLayout>
465 #if !EIGEN_HAS_CONSTEXPR
467 reduction_axis[0] = 1;
468 reduction_axis[1] = 2;
471 Eigen::IndexList<Eigen::type2index<1>, Eigen::type2index<2>> reduction_axis;
474 out = in.maximum(reduction_axis);
476 for (
int i = 0;
i < 72; ++
i) {
477 for (
int j = 0;
j < 113; ++
j) {
479 for (
int k = 0; k < 53; ++k) {
480 for (
int l = 0;
l < 97; ++
l) {
491 for (
float prescribed_mean : {1.0f, 10.0f, 100.0f, 1000.0f, 10000.0f}) {
493 tensor += tensor.constant(prescribed_mean);
496 double expected_sum = 0.0;
497 for (
int i = 0;
i < 101; ++
i) {
498 for (
int j = 0;
j < 101; ++
j) {
499 for (
int k = 0; k < 101; ++k) {
500 expected_sum +=
static_cast<double>(tensor(
i,
j, k));
511 CALL_SUBTEST(( test_simple_reductions<float,ColMajor>() ));
512 CALL_SUBTEST(( test_simple_reductions<float,RowMajor>() ));
513 CALL_SUBTEST(( test_simple_reductions<Eigen::half,ColMajor>() ));
514 CALL_SUBTEST(( test_simple_reductions<Eigen::bfloat16,ColMajor>() ));