diff options
author | Yi Kong <yikong@google.com> | 2022-02-25 16:41:05 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2022-02-25 16:41:05 +0000 |
commit | bc0f5df265caa21a2120c22453655a7fcc941991 (patch) | |
tree | fb979fb4cf4f8052c8cc66b1ec9516d91fcd859b /unsupported/test/cxx11_tensor_morphing.cpp | |
parent | 8fd413e275f78a4c240f1442ce5cf77c73a20a55 (diff) | |
parent | 7cb50013986f04dce5fac87bebf319bb8db37a36 (diff) | |
download | eigen-android13-qpr3-s14-release.tar.gz |
Merge changes Iee153445,Iee274471 am: 79df15ea88 am: 10f298fc41 am: 7cb5001398t_frc_odp_330442040t_frc_odp_330442000t_frc_ase_330444010android-wear-13.0.0-gpl_r3android-wear-13.0.0-gpl_r2android-wear-13.0.0-gpl_r1android-vts-13.0_r8android-vts-13.0_r7android-vts-13.0_r6android-vts-13.0_r5android-vts-13.0_r4android-vts-13.0_r3android-vts-13.0_r2android-t-qpr3-beta-3-gplandroid-t-qpr3-beta-1-gplandroid-t-qpr2-beta-3-gplandroid-t-qpr2-beta-2-gplandroid-t-qpr1-beta-3-gplandroid-t-qpr1-beta-1-gplandroid-cts-13.0_r8android-cts-13.0_r7android-cts-13.0_r6android-cts-13.0_r5android-cts-13.0_r4android-cts-13.0_r3android-cts-13.0_r2android-13.0.0_r83android-13.0.0_r82android-13.0.0_r81android-13.0.0_r80android-13.0.0_r79android-13.0.0_r78android-13.0.0_r77android-13.0.0_r76android-13.0.0_r75android-13.0.0_r74android-13.0.0_r73android-13.0.0_r72android-13.0.0_r71android-13.0.0_r70android-13.0.0_r69android-13.0.0_r68android-13.0.0_r67android-13.0.0_r66android-13.0.0_r65android-13.0.0_r64android-13.0.0_r63android-13.0.0_r62android-13.0.0_r61android-13.0.0_r60android-13.0.0_r59android-13.0.0_r58android-13.0.0_r57android-13.0.0_r56android-13.0.0_r55android-13.0.0_r54android-13.0.0_r53android-13.0.0_r52android-13.0.0_r51android-13.0.0_r50android-13.0.0_r49android-13.0.0_r48android-13.0.0_r47android-13.0.0_r46android-13.0.0_r45android-13.0.0_r44android-13.0.0_r43android-13.0.0_r42android-13.0.0_r41android-13.0.0_r40android-13.0.0_r39android-13.0.0_r38android-13.0.0_r37android-13.0.0_r36android-13.0.0_r35android-13.0.0_r34android-13.0.0_r33android-13.0.0_r32android-13.0.0_r30android-13.0.0_r29android-13.0.0_r28android-13.0.0_r27android-13.0.0_r24android-13.0.0_r23android-13.0.0_r22android-13.0.0_r21android-13.0.0_r20android-13.0.0_r19android-13.0.0_r18android-13.0.0_r17android-13.0.0_r16aml_go_odp_330912000aml_go_ads_330915100aml_go_ads_330915000aml_go_ads_330913000android13-tests-releaseandroid13-tests-devandroid13-qpr3-s9-releaseandroid13-qpr3-s8-releaseandroid13-qpr3-s7-releaseandroid13-qpr3-s6-releaseandroid13-qpr3-s5-releaseandroid13-qpr3-s4-releaseandroid13-qpr3-s3-releaseandroid13-qpr3-s2-releaseandroid13-qpr3-s14-releaseandroid13-qpr3-s13-releaseandroid13-qpr3-s12-releaseandroid13-qpr3-s11-releaseandroid13-qpr3-s10-releaseandroid13-qpr3-s1-releaseandroid13-qpr3-releaseandroid13-qpr3-c-s8-releaseandroid13-qpr3-c-s7-releaseandroid13-qpr3-c-s6-releaseandroid13-qpr3-c-s5-releaseandroid13-qpr3-c-s4-releaseandroid13-qpr3-c-s3-releaseandroid13-qpr3-c-s2-releaseandroid13-qpr3-c-s12-releaseandroid13-qpr3-c-s11-releaseandroid13-qpr3-c-s10-releaseandroid13-qpr3-c-s1-releaseandroid13-qpr2-s9-releaseandroid13-qpr2-s8-releaseandroid13-qpr2-s7-releaseandroid13-qpr2-s6-releaseandroid13-qpr2-s5-releaseandroid13-qpr2-s3-releaseandroid13-qpr2-s2-releaseandroid13-qpr2-s12-releaseandroid13-qpr2-s11-releaseandroid13-qpr2-s10-releaseandroid13-qpr2-s1-releaseandroid13-qpr2-releaseandroid13-qpr2-b-s1-releaseandroid13-qpr1-s8-releaseandroid13-qpr1-s7-releaseandroid13-qpr1-s6-releaseandroid13-qpr1-s5-releaseandroid13-qpr1-s4-releaseandroid13-qpr1-s3-releaseandroid13-qpr1-s2-releaseandroid13-qpr1-s1-releaseandroid13-qpr1-releaseandroid13-mainline-go-adservices-releaseandroid13-frc-odp-releaseandroid13-devandroid13-d4-s2-releaseandroid13-d4-s1-releaseandroid13-d4-releaseandroid13-d3-s1-releaseandroid13-d2-releaseandroid-wear-13.0.0-gpl_r1
Original change: https://android-review.googlesource.com/c/platform/external/eigen/+/1999079
Change-Id: I4c76dc5ddc7fb0ae9fc42436f28bd8bf9de50a97
Diffstat (limited to 'unsupported/test/cxx11_tensor_morphing.cpp')
-rw-r--r-- | unsupported/test/cxx11_tensor_morphing.cpp | 198 |
1 files changed, 139 insertions, 59 deletions
diff --git a/unsupported/test/cxx11_tensor_morphing.cpp b/unsupported/test/cxx11_tensor_morphing.cpp index f7de43110..ed5d5ade3 100644 --- a/unsupported/test/cxx11_tensor_morphing.cpp +++ b/unsupported/test/cxx11_tensor_morphing.cpp @@ -41,7 +41,29 @@ static void test_simple_reshape() } } -template<typename> +template <typename> +static void test_static_reshape() { +#if defined(EIGEN_HAS_INDEX_LIST) + using Eigen::type2index; + + Tensor<float, 5> tensor(2, 3, 1, 7, 1); + tensor.setRandom(); + + // New dimensions: [2, 3, 7] + Eigen::IndexList<type2index<2>, type2index<3>, type2index<7>> dim; + Tensor<float, 3> reshaped = tensor.reshape(static_cast<Eigen::DSizes<ptrdiff_t,3>>(dim)); + + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 3; ++j) { + for (int k = 0; k < 7; ++k) { + VERIFY_IS_EQUAL(tensor(i, j, 0, k, 0), reshaped(i, j, k)); + } + } + } +#endif +} + +template <typename> static void test_reshape_in_expr() { MatrixXf m1(2,3*5*7*11); MatrixXf m2(3*5*7*11,13); @@ -90,19 +112,19 @@ static void test_reshape_as_lvalue() } } -template<int DataLayout> +template<typename T, int DataLayout> static void test_simple_slice() { - Tensor<float, 5, DataLayout> tensor(2,3,5,7,11); + Tensor<T, 5, DataLayout> tensor(2,3,5,7,11); tensor.setRandom(); - Tensor<float, 5, DataLayout> slice1(1,1,1,1,1); + Tensor<T, 5, DataLayout> slice1(1,1,1,1,1); Eigen::DSizes<ptrdiff_t, 5> indices(1,2,3,4,5); Eigen::DSizes<ptrdiff_t, 5> sizes(1,1,1,1,1); slice1 = tensor.slice(indices, sizes); VERIFY_IS_EQUAL(slice1(0,0,0,0,0), tensor(1,2,3,4,5)); - Tensor<float, 5, DataLayout> slice2(1,1,2,2,3); + Tensor<T, 5, DataLayout> slice2(1,1,2,2,3); Eigen::DSizes<ptrdiff_t, 5> indices2(1,1,3,4,5); Eigen::DSizes<ptrdiff_t, 5> sizes2(1,1,2,2,3); slice2 = tensor.slice(indices2, sizes2); @@ -115,20 +137,20 @@ static void test_simple_slice() } } -template<typename=void> +template<typename T> static void test_const_slice() { - const float b[1] = {42}; - TensorMap<Tensor<const float, 1> > m(b, 1); + const T b[1] = {42}; + TensorMap<Tensor<const T, 1> > m(b, 1); DSizes<DenseIndex, 1> offsets; offsets[0] = 0; - TensorRef<Tensor<const float, 1> > slice_ref(m.slice(offsets, m.dimensions())); + TensorRef<Tensor<const T, 1> > slice_ref(m.slice(offsets, m.dimensions())); VERIFY_IS_EQUAL(slice_ref(0), 42); } -template<int DataLayout> +template<typename T, int DataLayout> static void test_slice_in_expr() { - typedef Matrix<float, Dynamic, Dynamic, DataLayout> Mtx; + typedef Matrix<T, Dynamic, Dynamic, DataLayout> Mtx; Mtx m1(7,7); Mtx m2(3,3); m1.setRandom(); @@ -136,10 +158,10 @@ static void test_slice_in_expr() { Mtx m3 = m1.block(1, 2, 3, 3) * m2.block(0, 2, 3, 1); - TensorMap<Tensor<float, 2, DataLayout>> tensor1(m1.data(), 7, 7); - TensorMap<Tensor<float, 2, DataLayout>> tensor2(m2.data(), 3, 3); - Tensor<float, 2, DataLayout> tensor3(3,1); - typedef Tensor<float, 1>::DimensionPair DimPair; + TensorMap<Tensor<T, 2, DataLayout>> tensor1(m1.data(), 7, 7); + TensorMap<Tensor<T, 2, DataLayout>> tensor2(m2.data(), 3, 3); + Tensor<T, 2, DataLayout> tensor3(3,1); + typedef typename Tensor<T, 1>::DimensionPair DimPair; array<DimPair, 1> contract_along{{DimPair(1, 0)}}; Eigen::DSizes<ptrdiff_t, 2> indices1(1,2); @@ -156,28 +178,28 @@ static void test_slice_in_expr() { } // Take an arbitrary slice of an arbitrarily sized tensor. - TensorMap<Tensor<const float, 2, DataLayout>> tensor4(m1.data(), 7, 7); - Tensor<float, 1, DataLayout> tensor6 = tensor4.reshape(DSizes<ptrdiff_t, 1>(7*7)).exp().slice(DSizes<ptrdiff_t, 1>(0), DSizes<ptrdiff_t, 1>(35)); + TensorMap<Tensor<const T, 2, DataLayout>> tensor4(m1.data(), 7, 7); + Tensor<T, 1, DataLayout> tensor6 = tensor4.reshape(DSizes<ptrdiff_t, 1>(7*7)).exp().slice(DSizes<ptrdiff_t, 1>(0), DSizes<ptrdiff_t, 1>(35)); for (int i = 0; i < 35; ++i) { VERIFY_IS_APPROX(tensor6(i), expf(tensor4.data()[i])); } } -template<int DataLayout> +template<typename T, int DataLayout> static void test_slice_as_lvalue() { - Tensor<float, 3, DataLayout> tensor1(2,2,7); + Tensor<T, 3, DataLayout> tensor1(2,2,7); tensor1.setRandom(); - Tensor<float, 3, DataLayout> tensor2(2,2,7); + Tensor<T, 3, DataLayout> tensor2(2,2,7); tensor2.setRandom(); - Tensor<float, 3, DataLayout> tensor3(4,3,5); + Tensor<T, 3, DataLayout> tensor3(4,3,5); tensor3.setRandom(); - Tensor<float, 3, DataLayout> tensor4(4,3,2); + Tensor<T, 3, DataLayout> tensor4(4,3,2); tensor4.setRandom(); - Tensor<float, 3, DataLayout> tensor5(10,13,12); + Tensor<T, 3, DataLayout> tensor5(10,13,12); tensor5.setRandom(); - Tensor<float, 3, DataLayout> result(4,5,7); + Tensor<T, 3, DataLayout> result(4,5,7); Eigen::DSizes<ptrdiff_t, 3> sizes12(2,2,7); Eigen::DSizes<ptrdiff_t, 3> first_slice(0,0,0); result.slice(first_slice, sizes12) = tensor1; @@ -223,10 +245,10 @@ static void test_slice_as_lvalue() } } -template<int DataLayout> +template<typename T, int DataLayout> static void test_slice_raw_data() { - Tensor<float, 4, DataLayout> tensor(3,5,7,11); + Tensor<T, 4, DataLayout> tensor(3,5,7,11); tensor.setRandom(); Eigen::DSizes<ptrdiff_t, 4> offsets(1,2,3,4); @@ -253,7 +275,7 @@ static void test_slice_raw_data() extents = Eigen::DSizes<ptrdiff_t, 4>(1,2,1,1); auto slice3 = SliceEvaluator(tensor.slice(offsets, extents), DefaultDevice()); VERIFY_IS_EQUAL(slice3.dimensions().TotalSize(), 2); - VERIFY_IS_EQUAL(slice3.data(), static_cast<float*>(0)); + VERIFY_IS_EQUAL(slice3.data(), static_cast<T*>(0)); if (DataLayout == ColMajor) { offsets = Eigen::DSizes<ptrdiff_t, 4>(0,2,3,4); @@ -318,15 +340,15 @@ static void test_slice_raw_data() } -template<int DataLayout> +template<typename T, int DataLayout> static void test_strided_slice() { - typedef Tensor<float, 5, DataLayout> Tensor5f; + typedef Tensor<T, 5, DataLayout> Tensor5f; typedef Eigen::DSizes<Eigen::DenseIndex, 5> Index5; - typedef Tensor<float, 2, DataLayout> Tensor2f; + typedef Tensor<T, 2, DataLayout> Tensor2f; typedef Eigen::DSizes<Eigen::DenseIndex, 2> Index2; - Tensor<float, 5, DataLayout> tensor(2,3,5,7,11); - Tensor<float, 2, DataLayout> tensor2(7,11); + Tensor<T, 5, DataLayout> tensor(2,3,5,7,11); + Tensor<T, 2, DataLayout> tensor2(7,11); tensor.setRandom(); tensor2.setRandom(); @@ -412,13 +434,13 @@ static void test_strided_slice() } } -template<int DataLayout> +template<typename T, int DataLayout> static void test_strided_slice_write() { - typedef Tensor<float, 2, DataLayout> Tensor2f; + typedef Tensor<T, 2, DataLayout> Tensor2f; typedef Eigen::DSizes<Eigen::DenseIndex, 2> Index2; - Tensor<float, 2, DataLayout> tensor(7,11),tensor2(7,11); + Tensor<T, 2, DataLayout> tensor(7,11),tensor2(7,11); tensor.setRandom(); tensor2=tensor; Tensor2f slice(2,3); @@ -438,15 +460,14 @@ static void test_strided_slice_write() } } - -template<int DataLayout> +template<typename T, int DataLayout> static void test_composition() { - Eigen::Tensor<float, 2, DataLayout> matrix(7, 11); + Eigen::Tensor<T, 2, DataLayout> matrix(7, 11); matrix.setRandom(); const DSizes<ptrdiff_t, 3> newDims(1, 1, 11); - Eigen::Tensor<float, 3, DataLayout> tensor = + Eigen::Tensor<T, 3, DataLayout> tensor = matrix.slice(DSizes<ptrdiff_t, 2>(2, 0), DSizes<ptrdiff_t, 2>(1, 11)).reshape(newDims); VERIFY_IS_EQUAL(tensor.dimensions().TotalSize(), 11); @@ -458,28 +479,87 @@ static void test_composition() } } +template<typename T, int DataLayout> +static void test_empty_slice() +{ + Tensor<T, 3, DataLayout> tensor(2,3,5); + tensor.setRandom(); + Tensor<T, 3, DataLayout> copy = tensor; + + // empty size in first dimension + Eigen::DSizes<ptrdiff_t, 3> indices1(1,2,3); + Eigen::DSizes<ptrdiff_t, 3> sizes1(0,1,2); + Tensor<T, 3, DataLayout> slice1(0,1,2); + slice1.setRandom(); + tensor.slice(indices1, sizes1) = slice1; + + // empty size in second dimension + Eigen::DSizes<ptrdiff_t, 3> indices2(1,2,3); + Eigen::DSizes<ptrdiff_t, 3> sizes2(1,0,2); + Tensor<T, 3, DataLayout> slice2(1,0,2); + slice2.setRandom(); + tensor.slice(indices2, sizes2) = slice2; + + // empty size in third dimension + Eigen::DSizes<ptrdiff_t, 3> indices3(1,2,3); + Eigen::DSizes<ptrdiff_t, 3> sizes3(1,1,0); + Tensor<T, 3, DataLayout> slice3(1,1,0); + slice3.setRandom(); + tensor.slice(indices3, sizes3) = slice3; + + // empty size in first and second dimension + Eigen::DSizes<ptrdiff_t, 3> indices4(1,2,3); + Eigen::DSizes<ptrdiff_t, 3> sizes4(0,0,2); + Tensor<T, 3, DataLayout> slice4(0,0,2); + slice4.setRandom(); + tensor.slice(indices4, sizes4) = slice4; + + // empty size in second and third dimension + Eigen::DSizes<ptrdiff_t, 3> indices5(1,2,3); + Eigen::DSizes<ptrdiff_t, 3> sizes5(1,0,0); + Tensor<T, 3, DataLayout> slice5(1,0,0); + slice5.setRandom(); + tensor.slice(indices5, sizes5) = slice5; + + // empty size in all dimensions + Eigen::DSizes<ptrdiff_t, 3> indices6(1,2,3); + Eigen::DSizes<ptrdiff_t, 3> sizes6(0,0,0); + Tensor<T, 3, DataLayout> slice6(0,0,0); + slice6.setRandom(); + tensor.slice(indices6, sizes6) = slice6; + + // none of these operations should change the tensor's components + // because all of the rvalue slices have at least one zero dimension + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 3; ++j) { + for (int k = 0; k < 5; ++k) { + VERIFY_IS_EQUAL(tensor(i,j,k), copy(i,j,k)); + } + } + } +} + +#define CALL_SUBTEST_PART(PART) \ + CALL_SUBTEST_##PART + +#define CALL_SUBTESTS_TYPES_LAYOUTS(PART, NAME) \ + CALL_SUBTEST_PART(PART)((NAME<float, ColMajor>())); \ + CALL_SUBTEST_PART(PART)((NAME<float, RowMajor>())); \ + CALL_SUBTEST_PART(PART)((NAME<bool, ColMajor>())); \ + CALL_SUBTEST_PART(PART)((NAME<bool, RowMajor>())) -void test_cxx11_tensor_morphing() +EIGEN_DECLARE_TEST(cxx11_tensor_morphing) { CALL_SUBTEST_1(test_simple_reshape<void>()); - CALL_SUBTEST_1(test_reshape_in_expr<void>()); + CALL_SUBTEST_1(test_static_reshape<void>()); CALL_SUBTEST_1(test_reshape_as_lvalue<void>()); - - CALL_SUBTEST_1(test_simple_slice<ColMajor>()); - CALL_SUBTEST_1(test_simple_slice<RowMajor>()); - CALL_SUBTEST_1(test_const_slice()); - CALL_SUBTEST_2(test_slice_in_expr<ColMajor>()); - CALL_SUBTEST_3(test_slice_in_expr<RowMajor>()); - CALL_SUBTEST_4(test_slice_as_lvalue<ColMajor>()); - CALL_SUBTEST_4(test_slice_as_lvalue<RowMajor>()); - CALL_SUBTEST_5(test_slice_raw_data<ColMajor>()); - CALL_SUBTEST_5(test_slice_raw_data<RowMajor>()); - - CALL_SUBTEST_6(test_strided_slice_write<ColMajor>()); - CALL_SUBTEST_6(test_strided_slice<ColMajor>()); - CALL_SUBTEST_6(test_strided_slice_write<RowMajor>()); - CALL_SUBTEST_6(test_strided_slice<RowMajor>()); - - CALL_SUBTEST_7(test_composition<ColMajor>()); - CALL_SUBTEST_7(test_composition<RowMajor>()); + CALL_SUBTEST_1(test_reshape_in_expr<void>()); + CALL_SUBTEST_1(test_const_slice<float>()); + + CALL_SUBTESTS_TYPES_LAYOUTS(2, test_simple_slice); + CALL_SUBTESTS_TYPES_LAYOUTS(3, test_slice_as_lvalue); + CALL_SUBTESTS_TYPES_LAYOUTS(4, test_slice_raw_data); + CALL_SUBTESTS_TYPES_LAYOUTS(5, test_strided_slice_write); + CALL_SUBTESTS_TYPES_LAYOUTS(6, test_strided_slice); + CALL_SUBTESTS_TYPES_LAYOUTS(7, test_composition); } |