cxx11_tensor_argmax.cpp File Reference
#include "main.h"
#include <Eigen/CXX11/Tensor>

Functions

template<int DataLayout>
static void test_simple_index_pairs ()
 
template<int DataLayout>
static void test_index_pairs_dim ()
 
template<int DataLayout>
static void test_argmax_pair_reducer ()
 
template<int DataLayout>
static void test_argmin_pair_reducer ()
 
template<int DataLayout>
static void test_simple_argmax ()
 
template<int DataLayout>
static void test_simple_argmin ()
 
template<int DataLayout>
static void test_argmax_dim ()
 
template<int DataLayout>
static void test_argmin_dim ()
 
 EIGEN_DECLARE_TEST (cxx11_tensor_argmax)
 

Function Documentation

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( cxx11_tensor_argmax  )
272  {
273  CALL_SUBTEST(test_simple_index_pairs<RowMajor>());
274  CALL_SUBTEST(test_simple_index_pairs<ColMajor>());
275  CALL_SUBTEST(test_index_pairs_dim<RowMajor>());
276  CALL_SUBTEST(test_index_pairs_dim<ColMajor>());
277  CALL_SUBTEST(test_argmax_pair_reducer<RowMajor>());
278  CALL_SUBTEST(test_argmax_pair_reducer<ColMajor>());
279  CALL_SUBTEST(test_argmin_pair_reducer<RowMajor>());
280  CALL_SUBTEST(test_argmin_pair_reducer<ColMajor>());
281  CALL_SUBTEST(test_simple_argmax<RowMajor>());
282  CALL_SUBTEST(test_simple_argmax<ColMajor>());
283  CALL_SUBTEST(test_simple_argmin<RowMajor>());
284  CALL_SUBTEST(test_simple_argmin<ColMajor>());
285  CALL_SUBTEST(test_argmax_dim<RowMajor>());
286  CALL_SUBTEST(test_argmax_dim<ColMajor>());
287  CALL_SUBTEST(test_argmin_dim<RowMajor>());
288  CALL_SUBTEST(test_argmin_dim<ColMajor>());
289 }
#define CALL_SUBTEST(FUNC)
Definition: main.h:382

References CALL_SUBTEST.

◆ test_argmax_dim()

template<int DataLayout>
static void test_argmax_dim ( )
static
151  {
152  Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7);
153  std::vector<int> dims{2, 3, 5, 7};
154 
155  for (int dim = 0; dim < 4; ++dim) {
156  tensor.setRandom();
157  tensor = (tensor + tensor.constant(0.5)).log();
158 
159  Tensor<DenseIndex, 3, DataLayout> tensor_argmax;
161  for (int i = 0; i < 2; ++i) {
162  for (int j = 0; j < 3; ++j) {
163  for (int k = 0; k < 5; ++k) {
164  for (int l = 0; l < 7; ++l) {
165  ix[0] = i;
166  ix[1] = j;
167  ix[2] = k;
168  ix[3] = l;
169  if (ix[dim] != 0) continue;
170  // suppose dim == 1, then for all i, k, l, set tensor(i, 0, k, l) = 10.0
171  tensor(ix) = 10.0;
172  }
173  }
174  }
175  }
176 
177  tensor_argmax = tensor.argmax(dim);
178 
179  VERIFY_IS_EQUAL(tensor_argmax.size(), ptrdiff_t(2 * 3 * 5 * 7 / tensor.dimension(dim)));
180  for (ptrdiff_t n = 0; n < tensor_argmax.size(); ++n) {
181  // Expect max to be in the first index of the reduced dimension
182  VERIFY_IS_EQUAL(tensor_argmax.data()[n], 0);
183  }
184 
185  for (int i = 0; i < 2; ++i) {
186  for (int j = 0; j < 3; ++j) {
187  for (int k = 0; k < 5; ++k) {
188  for (int l = 0; l < 7; ++l) {
189  ix[0] = i;
190  ix[1] = j;
191  ix[2] = k;
192  ix[3] = l;
193  if (ix[dim] != tensor.dimension(dim) - 1) continue;
194  // suppose dim == 1, then for all i, k, l, set tensor(i, 2, k, l) = 20.0
195  tensor(ix) = 20.0;
196  }
197  }
198  }
199  }
200 
201  tensor_argmax = tensor.argmax(dim);
202 
203  VERIFY_IS_EQUAL(tensor_argmax.size(), ptrdiff_t(2 * 3 * 5 * 7 / tensor.dimension(dim)));
204  for (ptrdiff_t n = 0; n < tensor_argmax.size(); ++n) {
205  // Expect max to be in the last index of the reduced dimension
206  VERIFY_IS_EQUAL(tensor_argmax.data()[n], tensor.dimension(dim) - 1);
207  }
208  }
209 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
The tensor class.
Definition: Tensor.h:68
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const
Definition: Tensor.h:101
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar * data()
Definition: Tensor.h:102
char char char int int * k
Definition: level2_impl.h:374
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:367
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 log(const bfloat16 &a)
Definition: BFloat16.h:618
std::array< T, N > array
Definition: EmulateArray.h:231
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::data(), Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::dimension(), i, j, k, Eigen::bfloat16_impl::log(), n, Eigen::TensorBase< Derived, AccessLevel >::setRandom(), Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::size(), and VERIFY_IS_EQUAL.

◆ test_argmax_pair_reducer()

template<int DataLayout>
static void test_argmax_pair_reducer ( )
static
53  {
54  Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7);
55  tensor.setRandom();
56  tensor = (tensor + tensor.constant(0.5)).log();
57 
58  Tensor<Pair<DenseIndex, float>, 4, DataLayout> index_pairs(2, 3, 5, 7);
59  index_pairs = tensor.index_pairs();
60 
63  reduced = index_pairs.reduce(dims, internal::ArgMaxPairReducer<Pair<DenseIndex, float> >());
64 
65  Tensor<float, 0, DataLayout> maxi = tensor.maximum();
66 
67  VERIFY_IS_EQUAL(maxi(), reduced(0).second);
68 
69  array<DenseIndex, 3> reduce_dims;
70  for (int d = 0; d < 3; ++d) reduce_dims[d] = d;
71  Tensor<Pair<DenseIndex, float>, 1, DataLayout> reduced_by_dims(7);
72  reduced_by_dims = index_pairs.reduce(reduce_dims, internal::ArgMaxPairReducer<Pair<DenseIndex, float> >());
73 
74  Tensor<float, 1, DataLayout> max_by_dims = tensor.maximum(reduce_dims);
75 
76  for (int l = 0; l < 7; ++l) {
77  VERIFY_IS_EQUAL(max_by_dims(l), reduced_by_dims(l).second);
78  }
79 }
static const int DataLayout
Definition: cxx11_tensor_image_patch_sycl.cpp:24
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T maxi(const T &x, const T &y)
Definition: MathFunctions.h:926
Definition: TensorDimensionList.h:29
Definition: TensorMeta.h:205

References DataLayout, Eigen::bfloat16_impl::log(), Eigen::numext::maxi(), Eigen::TensorBase< Derived, AccessLevel >::setRandom(), and VERIFY_IS_EQUAL.

◆ test_argmin_dim()

template<int DataLayout>
static void test_argmin_dim ( )
static
212  {
213  Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7);
214  std::vector<int> dims{2, 3, 5, 7};
215 
216  for (int dim = 0; dim < 4; ++dim) {
217  tensor.setRandom();
218  tensor = (tensor + tensor.constant(0.5)).log();
219 
220  Tensor<DenseIndex, 3, DataLayout> tensor_argmin;
222  for (int i = 0; i < 2; ++i) {
223  for (int j = 0; j < 3; ++j) {
224  for (int k = 0; k < 5; ++k) {
225  for (int l = 0; l < 7; ++l) {
226  ix[0] = i;
227  ix[1] = j;
228  ix[2] = k;
229  ix[3] = l;
230  if (ix[dim] != 0) continue;
231  // suppose dim == 1, then for all i, k, l, set tensor(i, 0, k, l) = -10.0
232  tensor(ix) = -10.0;
233  }
234  }
235  }
236  }
237 
238  tensor_argmin = tensor.argmin(dim);
239 
240  VERIFY_IS_EQUAL(tensor_argmin.size(), ptrdiff_t(2 * 3 * 5 * 7 / tensor.dimension(dim)));
241  for (ptrdiff_t n = 0; n < tensor_argmin.size(); ++n) {
242  // Expect min to be in the first index of the reduced dimension
243  VERIFY_IS_EQUAL(tensor_argmin.data()[n], 0);
244  }
245 
246  for (int i = 0; i < 2; ++i) {
247  for (int j = 0; j < 3; ++j) {
248  for (int k = 0; k < 5; ++k) {
249  for (int l = 0; l < 7; ++l) {
250  ix[0] = i;
251  ix[1] = j;
252  ix[2] = k;
253  ix[3] = l;
254  if (ix[dim] != tensor.dimension(dim) - 1) continue;
255  // suppose dim == 1, then for all i, k, l, set tensor(i, 2, k, l) = -20.0
256  tensor(ix) = -20.0;
257  }
258  }
259  }
260  }
261 
262  tensor_argmin = tensor.argmin(dim);
263 
264  VERIFY_IS_EQUAL(tensor_argmin.size(), ptrdiff_t(2 * 3 * 5 * 7 / tensor.dimension(dim)));
265  for (ptrdiff_t n = 0; n < tensor_argmin.size(); ++n) {
266  // Expect min to be in the last index of the reduced dimension
267  VERIFY_IS_EQUAL(tensor_argmin.data()[n], tensor.dimension(dim) - 1);
268  }
269  }
270 }

References Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::data(), Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::dimension(), i, j, k, Eigen::bfloat16_impl::log(), n, Eigen::TensorBase< Derived, AccessLevel >::setRandom(), Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::size(), and VERIFY_IS_EQUAL.

◆ test_argmin_pair_reducer()

template<int DataLayout>
static void test_argmin_pair_reducer ( )
static
82  {
83  Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7);
84  tensor.setRandom();
85  tensor = (tensor + tensor.constant(0.5)).log();
86 
87  Tensor<Pair<DenseIndex, float>, 4, DataLayout> index_pairs(2, 3, 5, 7);
88  index_pairs = tensor.index_pairs();
89 
92  reduced = index_pairs.reduce(dims, internal::ArgMinPairReducer<Pair<DenseIndex, float> >());
93 
94  Tensor<float, 0, DataLayout> mini = tensor.minimum();
95 
96  VERIFY_IS_EQUAL(mini(), reduced(0).second);
97 
98  array<DenseIndex, 3> reduce_dims;
99  for (int d = 0; d < 3; ++d) reduce_dims[d] = d;
100  Tensor<Pair<DenseIndex, float>, 1, DataLayout> reduced_by_dims(7);
101  reduced_by_dims = index_pairs.reduce(reduce_dims, internal::ArgMinPairReducer<Pair<DenseIndex, float> >());
102 
103  Tensor<float, 1, DataLayout> min_by_dims = tensor.minimum(reduce_dims);
104 
105  for (int l = 0; l < 7; ++l) {
106  VERIFY_IS_EQUAL(min_by_dims(l), reduced_by_dims(l).second);
107  }
108 }
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T mini(const T &x, const T &y)
Definition: MathFunctions.h:920

References DataLayout, Eigen::bfloat16_impl::log(), Eigen::numext::mini(), Eigen::TensorBase< Derived, AccessLevel >::setRandom(), and VERIFY_IS_EQUAL.

◆ test_index_pairs_dim()

template<int DataLayout>
static void test_index_pairs_dim ( )
static
36  {
37  Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7);
38  tensor.setRandom();
39  tensor = (tensor + tensor.constant(0.5)).log();
40 
41  Tensor<Pair<DenseIndex, float>, 4, DataLayout> index_pairs(2, 3, 5, 7);
42 
43  index_pairs = tensor.index_pairs();
44 
45  for (Eigen::DenseIndex n = 0; n < tensor.size(); ++n) {
46  const Pair<DenseIndex, float>& v = index_pairs(n); //(i, j, k, l);
47  VERIFY_IS_EQUAL(v.first, n);
48  VERIFY_IS_EQUAL(v.second, tensor(n));
49  }
50 }
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
EIGEN_DEFAULT_DENSE_INDEX_TYPE DenseIndex
Definition: Meta.h:75

References DataLayout, Eigen::bfloat16_impl::log(), n, Eigen::TensorBase< Derived, AccessLevel >::setRandom(), Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::size(), v, and VERIFY_IS_EQUAL.

◆ test_simple_argmax()

template<int DataLayout>
static void test_simple_argmax ( )
static
111  {
112  Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7);
113  tensor.setRandom();
114  tensor = (tensor + tensor.constant(0.5)).log();
115  tensor(0, 0, 0, 0) = 10.0;
116 
117  Tensor<DenseIndex, 0, DataLayout> tensor_argmax;
118 
119  tensor_argmax = tensor.argmax();
120 
121  VERIFY_IS_EQUAL(tensor_argmax(0), 0);
122 
123  tensor(1, 2, 4, 6) = 20.0;
124 
125  tensor_argmax = tensor.argmax();
126 
127  VERIFY_IS_EQUAL(tensor_argmax(0), 2 * 3 * 5 * 7 - 1);
128 }

References Eigen::bfloat16_impl::log(), Eigen::TensorBase< Derived, AccessLevel >::setRandom(), and VERIFY_IS_EQUAL.

◆ test_simple_argmin()

template<int DataLayout>
static void test_simple_argmin ( )
static
131  {
132  Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7);
133  tensor.setRandom();
134  tensor = (tensor + tensor.constant(0.5)).log();
135  tensor(0, 0, 0, 0) = -10.0;
136 
137  Tensor<DenseIndex, 0, DataLayout> tensor_argmin;
138 
139  tensor_argmin = tensor.argmin();
140 
141  VERIFY_IS_EQUAL(tensor_argmin(0), 0);
142 
143  tensor(1, 2, 4, 6) = -20.0;
144 
145  tensor_argmin = tensor.argmin();
146 
147  VERIFY_IS_EQUAL(tensor_argmin(0), 2 * 3 * 5 * 7 - 1);
148 }

References Eigen::bfloat16_impl::log(), Eigen::TensorBase< Derived, AccessLevel >::setRandom(), and VERIFY_IS_EQUAL.

◆ test_simple_index_pairs()

template<int DataLayout>
static void test_simple_index_pairs ( )
static
20  {
21  Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7);
22  tensor.setRandom();
23  tensor = (tensor + tensor.constant(0.5)).log();
24 
25  Tensor<Pair<DenseIndex, float>, 4, DataLayout> index_pairs(2, 3, 5, 7);
26  index_pairs = tensor.index_pairs();
27 
28  for (DenseIndex n = 0; n < 2 * 3 * 5 * 7; ++n) {
29  const Pair<DenseIndex, float>& v = index_pairs.coeff(n);
30  VERIFY_IS_EQUAL(v.first, n);
31  VERIFY_IS_EQUAL(v.second, tensor.coeff(n));
32  }
33 }

References Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::coeff(), DataLayout, Eigen::bfloat16_impl::log(), n, Eigen::TensorBase< Derived, AccessLevel >::setRandom(), v, and VERIFY_IS_EQUAL.