• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Utility functions related to layouts of Shapes.
17 
18 #ifndef TENSORFLOW_COMPILER_XLA_INDEX_UTIL_H_
19 #define TENSORFLOW_COMPILER_XLA_INDEX_UTIL_H_
20 
21 #include <vector>
22 
23 #include "absl/types/span.h"
24 #include "tensorflow/compiler/xla/shape.h"
25 #include "tensorflow/compiler/xla/types.h"
26 #include "tensorflow/compiler/xla/xla_data.pb.h"
27 #include "tensorflow/core/platform/macros.h"
28 
29 namespace xla {
30 
31 // Namespaced collection of (static) utilities related to indexing into
32 // multidimensional arrays.
33 class IndexUtil {
34  public:
35   // Converts a multidimensional index (eg {x, y, z}) into a linear index based
36   // on the shape and its layout. The first index in the multi_index is
37   // dimension 0.
38   static int64 MultidimensionalIndexToLinearIndex(
39       const Shape& shape, absl::Span<const int64> multi_index);
40 
41   // Converts a linear index into multidimensional index (eg {x, y, z}) based on
42   // the shape and its layout. The first index in the returned multidimensional
43   // index is dimension 0.
44   static std::vector<int64> LinearIndexToMultidimensionalIndex(
45       const Shape& shape, int64 linear_index);
46 
47   // Bumps a sequence of indices; e.g. {0,0,0,0} up by one index value; e.g. to
48   // {0,0,0,1}. This is akin to std::next_permutation. If the index hits a limit
49   // for the provided shape, the next most significant index is bumped, in a
50   // counting-up process.
51   //
52   // E.g. for shape f32[2,3]
53   //  {0,0}=>{0,1}
54   //  {0,1}=>{0,2}
55   //  {0,2}=>{1,0}
56   //  etc.
57   //
58   // This is useful for traversing the indices in a literal.
59   //
60   // Returns true iff the indices were successfully bumped; false if we've hit
61   // the limit where it can no longer be bumped in-bounds.
62   static bool BumpIndices(const Shape& shape, absl::Span<int64> indices);
63 
64   // Calculates the stride size (in number of elements, not byte size) of a
65   // given logical shape dimension (from 0 to rank-1).
66   // Example:
67   //  GetDimensionStride(F32[5,8,10,4]{3,2,1,0}, 1) ==
68   //    sizeof(dimension(3)) * sizeof(dimension(2)) == 4 * 10
69   static int64 GetDimensionStride(const Shape& shape, int64 dimension);
70 
71   // Returns true iff the given multi-index is contained in the bounds for the
72   // shape.
73   static bool IndexInBounds(const Shape& shape, absl::Span<const int64> index);
74 
75   // Compares the given indices in lexicographic order.  lhs[0] and rhs[0] are
76   // compared first, and lhs[rank-1] and rhs[rank-1] last.  If lhs is larger,
77   // then -1 is returned. If rhs is larger, then 1 is returned.  Otherwise, 0 is
78   // returned.
79   static int CompareIndices(absl::Span<const int64> lhs,
80                             absl::Span<const int64> rhs);
81 
82  private:
83   TF_DISALLOW_COPY_AND_ASSIGN(IndexUtil);
84 };
85 
86 }  // namespace xla
87 
88 #endif  // TENSORFLOW_COMPILER_XLA_INDEX_UTIL_H_
89