Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #include "tensorflow/core/framework/tensor.h"
     17 #include "tensorflow/core/framework/tensor_testutil.h"
     18 #include "tensorflow/core/kernels/transpose_functor.h"
     19 #include "tensorflow/core/platform/test.h"
     20 
     21 namespace tensorflow {
     22 
     23 class TransposeUtilTest : public ::testing::Test {
     24  protected:
     25   void TestDimensionReduction(const TensorShape& shape,
     26                               const gtl::ArraySlice<int32>& perm,
     27                               const gtl::ArraySlice<int32>& expected_perm,
     28                               const gtl::ArraySlice<int64>& expected_dims) {
     29     internal::TransposePermsVec new_perm;
     30     internal::TransposeDimsVec new_dims;
     31     internal::ReduceTransposeDimensions(shape, perm, &new_perm, &new_dims);
     32 
     33     gtl::ArraySlice<int32> computed_perm(new_perm);
     34     gtl::ArraySlice<int64> computed_dims(new_dims);
     35     EXPECT_EQ(computed_perm, expected_perm);
     36     EXPECT_EQ(computed_dims, expected_dims);
     37   }
     38 };
     39 
     40 TEST_F(TransposeUtilTest, NormalDimensionReduction) {
     41   TestDimensionReduction({2, 3, 4}, {0, 2, 1}, {0, 2, 1}, {2, 3, 4});
     42 
     43   TestDimensionReduction({2, 3, 4}, {1, 0, 2}, {1, 0, 2}, {2, 3, 4});
     44 
     45   TestDimensionReduction({2, 3, 4}, {2, 1, 0}, {2, 1, 0}, {2, 3, 4});
     46 
     47   TestDimensionReduction({2, 3, 4, 5}, {0, 2, 3, 1}, {0, 2, 1}, {2, 3, 20});
     48 
     49   TestDimensionReduction({2, 3, 4, 5}, {0, 3, 1, 2}, {0, 2, 1}, {2, 12, 5});
     50 
     51   TestDimensionReduction({2, 3, 4, 5}, {3, 1, 2, 0}, {2, 1, 0}, {2, 12, 5});
     52 
     53   TestDimensionReduction({2, 3, 4, 5}, {2, 3, 1, 0}, {2, 1, 0}, {2, 3, 20});
     54 
     55   TestDimensionReduction({2, 3, 4, 5, 6}, {0, 2, 3, 4, 1}, {0, 2, 1},
     56                          {2, 3, 120});
     57 
     58   TestDimensionReduction({2, 3, 4, 5, 6}, {0, 4, 1, 2, 3}, {0, 2, 1},
     59                          {2, 60, 6});
     60 
     61   TestDimensionReduction({2, 3, 4, 5, 6}, {4, 1, 2, 3, 0}, {2, 1, 0},
     62                          {2, 60, 6});
     63 
     64   TestDimensionReduction({2, 3, 4, 5, 6}, {3, 4, 1, 2, 0}, {2, 1, 0},
     65                          {2, 12, 30});
     66 
     67   TestDimensionReduction({2, 3}, {1, 0}, {1, 0}, {2, 3});
     68 
     69   TestDimensionReduction({2, 3, 4}, {2, 0, 1}, {1, 0}, {6, 4});
     70 
     71   TestDimensionReduction({2, 3, 4}, {1, 2, 0}, {1, 0}, {2, 12});
     72 
     73   TestDimensionReduction({2, 3, 4, 5}, {2, 3, 0, 1}, {1, 0}, {6, 20});
     74 
     75   TestDimensionReduction({2, 3, 4, 5}, {1, 2, 3, 0}, {1, 0}, {2, 60});
     76 
     77   TestDimensionReduction({2, 3, 4, 5, 6}, {2, 3, 4, 0, 1}, {1, 0}, {6, 120});
     78 
     79   TestDimensionReduction({2, 3, 4, 5, 6}, {4, 0, 1, 2, 3}, {1, 0}, {120, 6});
     80 
     81   TestDimensionReduction({2, 3, 4, 5, 6}, {0, 1, 2, 3, 4}, {0}, {720});
     82 
     83   TestDimensionReduction({2, 3, 4, 5}, {0, 1, 2, 3}, {0}, {120});
     84 
     85   TestDimensionReduction({2, 3, 4}, {0, 1, 2}, {0}, {24});
     86 
     87   TestDimensionReduction({2, 3}, {0, 1}, {0}, {6});
     88 }
     89 
     90 TEST_F(TransposeUtilTest, LargeDimensionReduction) {
     91   TestDimensionReduction({2, 3, 4, 5, 6, 7, 8, 9, 10, 20},
     92                          {0, 2, 3, 4, 5, 6, 7, 8, 9, 1}, {0, 2, 1},
     93                          {2, 3, 12096000});
     94   TestDimensionReduction({2, 3, 4, 5, 6, 7, 8, 9, 10, 20},
     95                          {0, 1, 2, 3, 4, 5, 6, 7, 9, 8}, {0, 2, 1},
     96                          {362880, 10, 20});
     97   TestDimensionReduction({2, 3, 4, 5, 6, 7, 8, 9, 10, 20},
     98                          {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, {0}, {72576000});
     99 }
    100 
    101 TEST_F(TransposeUtilTest, NonSingletonDimensionAlignment) {
    102   // Non-singleton dims 0, 2
    103   EXPECT_TRUE(internal::NonSingletonDimensionsAlign({2, 1, 2}, {1, 0, 2}));
    104   EXPECT_TRUE(internal::NonSingletonDimensionsAlign({2, 1, 2}, {0, 2, 1}));
    105   EXPECT_FALSE(internal::NonSingletonDimensionsAlign({2, 1, 2}, {2, 0, 1}));
    106   EXPECT_FALSE(internal::NonSingletonDimensionsAlign({2, 1, 2}, {2, 1, 0}));
    107 
    108   // Non-singleton dims 0, 2, 4
    109   EXPECT_TRUE(
    110       internal::NonSingletonDimensionsAlign({2, 1, 2, 1, 2}, {0, 2, 4, 1, 3}));
    111   EXPECT_TRUE(
    112       internal::NonSingletonDimensionsAlign({2, 1, 2, 1, 2}, {0, 2, 1, 4, 3}));
    113   EXPECT_TRUE(
    114       internal::NonSingletonDimensionsAlign({2, 1, 2, 1, 2}, {1, 3, 0, 2, 4}));
    115   EXPECT_TRUE(
    116       internal::NonSingletonDimensionsAlign({2, 1, 2, 1, 2}, {3, 0, 1, 2, 4}));
    117   EXPECT_FALSE(
    118       internal::NonSingletonDimensionsAlign({2, 1, 2, 1, 2}, {3, 2, 0, 1, 4}));
    119 
    120   // Non-singleton dims 2, 4, 5
    121   EXPECT_TRUE(internal::NonSingletonDimensionsAlign({1, 1, 2, 1, 2, 2},
    122                                                     {3, 2, 1, 4, 0, 5}));
    123   EXPECT_TRUE(internal::NonSingletonDimensionsAlign({1, 1, 2, 1, 2, 2},
    124                                                     {3, 1, 0, 2, 4, 5}));
    125   EXPECT_TRUE(internal::NonSingletonDimensionsAlign({1, 1, 2, 1, 2, 2},
    126                                                     {2, 4, 5, 0, 3, 1}));
    127   EXPECT_FALSE(internal::NonSingletonDimensionsAlign({1, 1, 2, 1, 2, 2},
    128                                                      {0, 1, 5, 2, 4, 3}));
    129   EXPECT_FALSE(internal::NonSingletonDimensionsAlign({1, 1, 2, 1, 2, 2},
    130                                                      {0, 1, 2, 5, 4, 3}));
    131 }
    132 
    133 }  // namespace tensorflow
    134