Home | History | Annotate | Download | only in xla
      1 /* Copyright 2017 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/compiler/xla/index_util.h"
     17 
     18 #include <initializer_list>
     19 
     20 #include "tensorflow/compiler/xla/shape_util.h"
     21 #include "tensorflow/compiler/xla/test.h"
     22 #include "tensorflow/compiler/xla/xla_data.pb.h"
     23 
     24 namespace xla {
     25 namespace {
     26 
     27 void SetMinorToMajorLayout(Shape* shape,
     28                            std::initializer_list<int64> dimensions) {
     29   shape->mutable_layout()->clear_minor_to_major();
     30   for (auto dimension : dimensions) {
     31     shape->mutable_layout()->add_minor_to_major(dimension);
     32   }
     33 }
     34 
     35 TEST(IndexUtilTest, VectorIndexing) {
     36   // Vectors are trivially laid out and the linear index should always be the
     37   // same as the "multidimensional" index.
     38   Shape vector_shape = ShapeUtil::MakeShape(F32, {100});
     39   EXPECT_EQ(42,
     40             IndexUtil::MultidimensionalIndexToLinearIndex(vector_shape, {42}));
     41   std::vector<int64> multi_index =
     42       IndexUtil::LinearIndexToMultidimensionalIndex(vector_shape, 42);
     43   EXPECT_EQ(1, multi_index.size());
     44   EXPECT_EQ(42, multi_index[0]);
     45 }
     46 
     47 TEST(IndexUtilTest, MatrixIndexingRowMajor) {
     48   // Set layout to [0, 1]. That is, row major.
     49   Shape matrix_shape_01 = ShapeUtil::MakeShape(F32, {10, 20});
     50   SetMinorToMajorLayout(&matrix_shape_01, {0, 1});
     51 
     52   // If index is {a, b} then linear index should be: a + b * 10
     53   EXPECT_EQ(0, IndexUtil::MultidimensionalIndexToLinearIndex(matrix_shape_01,
     54                                                              {0, 0}));
     55   EXPECT_EQ(199, IndexUtil::MultidimensionalIndexToLinearIndex(matrix_shape_01,
     56                                                                {9, 19}));
     57   EXPECT_EQ(53, IndexUtil::MultidimensionalIndexToLinearIndex(matrix_shape_01,
     58                                                               {3, 5}));
     59   EXPECT_EQ(std::vector<int64>({3, 5}),
     60             IndexUtil::LinearIndexToMultidimensionalIndex(matrix_shape_01, 53));
     61 }
     62 
     63 TEST(IndexUtilTest, MatrixIndexingColumnMajor) {
     64   // Set layout to [1, 0]. That is, column major.
     65   Shape matrix_shape_10 = ShapeUtil::MakeShape(F32, {10, 20});
     66   SetMinorToMajorLayout(&matrix_shape_10, {1, 0});
     67 
     68   // If index is {a, b} then linear index should be: a * 20 + b
     69   EXPECT_EQ(0, IndexUtil::MultidimensionalIndexToLinearIndex(matrix_shape_10,
     70                                                              {0, 0}));
     71   EXPECT_EQ(199, IndexUtil::MultidimensionalIndexToLinearIndex(matrix_shape_10,
     72                                                                {9, 19}));
     73   EXPECT_EQ(65, IndexUtil::MultidimensionalIndexToLinearIndex(matrix_shape_10,
     74                                                               {3, 5}));
     75   EXPECT_EQ(std::vector<int64>({3, 5}),
     76             IndexUtil::LinearIndexToMultidimensionalIndex(matrix_shape_10, 65));
     77 }
     78 
     79 TEST(IndexUtilTest, ThreeDArrayIndexing210) {
     80   // Set layout to [2, 1, 0]. That is, column major.
     81   Shape shape_210 = ShapeUtil::MakeShape(F32, {10, 20, 30});
     82   SetMinorToMajorLayout(&shape_210, {2, 1, 0});
     83 
     84   // If index is {a, b, c} then linear index should be:
     85   // a * 20 * 30 + b * 30 + c
     86   EXPECT_EQ(1957, IndexUtil::MultidimensionalIndexToLinearIndex(shape_210,
     87                                                                 {3, 5, 7}));
     88   EXPECT_EQ(5277, IndexUtil::MultidimensionalIndexToLinearIndex(shape_210,
     89                                                                 {8, 15, 27}));
     90 }
     91 
     92 TEST(IndexUtilTest, ThreeDArrayIndexing120) {
     93   // Set layout to [1, 2, 0]
     94   Shape shape_120 = ShapeUtil::MakeShape(F32, {10, 20, 30});
     95   SetMinorToMajorLayout(&shape_120, {1, 2, 0});
     96 
     97   // If index is {a, b, c} then linear index should be:
     98   // a * 20 * 30 + b + c * 20
     99   EXPECT_EQ(1945, IndexUtil::MultidimensionalIndexToLinearIndex(shape_120,
    100                                                                 {3, 5, 7}));
    101   EXPECT_EQ(5355, IndexUtil::MultidimensionalIndexToLinearIndex(shape_120,
    102                                                                 {8, 15, 27}));
    103 }
    104 
    105 TEST(IndexUtilTest, FourDArrayIndexing3210) {
    106   // Set layout to [3, 2, 1,0]. That is, column major.
    107   Shape shape_3210 = ShapeUtil::MakeShape(F32, {10, 20, 30, 40});
    108   SetMinorToMajorLayout(&shape_3210, {3, 2, 1, 0});
    109 
    110   // If index is {a, b, c, d} then linear index should be:
    111   // a * 20 * 30 * 40 + b * 30 * 40 + c * 40 + d
    112   EXPECT_EQ(78289, IndexUtil::MultidimensionalIndexToLinearIndex(shape_3210,
    113                                                                  {3, 5, 7, 9}));
    114   EXPECT_EQ(211113, IndexUtil::MultidimensionalIndexToLinearIndex(
    115                         shape_3210, {8, 15, 27, 33}));
    116 }
    117 
    118 TEST(IndexUtilTest, LinearToMultiToLinear) {
    119   // Verify that converting a linear index to a multidimensional index and back
    120   // always returns the same value for different crazy shapes.  Shape has
    121   // 1440000000 elements. Inputs are randomly-ish selected.
    122   std::vector<int64> linear_indexes = {0,        1439999999, 1145567336,
    123                                        43883404, 617295214,  1117613654};
    124 
    125   std::vector<std::initializer_list<int64>> minor_to_major_orders;
    126   minor_to_major_orders.push_back({6, 5, 4, 3, 2, 1, 0});
    127   minor_to_major_orders.push_back({0, 1, 2, 3, 4, 5, 6});
    128   minor_to_major_orders.push_back({4, 5, 1, 2, 6, 0, 3});
    129 
    130   for (auto minor_to_major_order : minor_to_major_orders) {
    131     Shape shape = ShapeUtil::MakeShape(F32, {10, 20, 30, 40, 30, 20, 10});
    132     SetMinorToMajorLayout(&shape, minor_to_major_order);
    133     for (auto linear_index : linear_indexes) {
    134       std::vector<int64> multi_index =
    135           IndexUtil::LinearIndexToMultidimensionalIndex(shape, linear_index);
    136       EXPECT_EQ(linear_index, IndexUtil::MultidimensionalIndexToLinearIndex(
    137                                   shape, multi_index));
    138     }
    139   }
    140 }
    141 
    142 TEST(IndexUtilTest, BumpIndices2x2) {
    143   auto shape = ShapeUtil::MakeShape(S32, {2, 2});
    144   std::vector<int64> indices = {0, 0};
    145   EXPECT_TRUE(IndexUtil::BumpIndices(shape, &indices));
    146   EXPECT_THAT(indices, ::testing::ElementsAre(0, 1));
    147   EXPECT_TRUE(IndexUtil::BumpIndices(shape, &indices));
    148   EXPECT_THAT(indices, ::testing::ElementsAre(1, 0));
    149   EXPECT_TRUE(IndexUtil::BumpIndices(shape, &indices));
    150   EXPECT_THAT(indices, ::testing::ElementsAre(1, 1));
    151   EXPECT_FALSE(IndexUtil::BumpIndices(shape, &indices));
    152 }
    153 
    154 }  // namespace
    155 }  // namespace xla
    156