• 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_LAYOUT_UTIL_H_
19 #define TENSORFLOW_COMPILER_XLA_LAYOUT_UTIL_H_
20 
21 #include <string>
22 
23 #include "absl/types/span.h"
24 #include "tensorflow/compiler/xla/layout.h"
25 #include "tensorflow/compiler/xla/shape.h"
26 #include "tensorflow/compiler/xla/status.h"
27 #include "tensorflow/compiler/xla/types.h"
28 #include "tensorflow/compiler/xla/xla_data.pb.h"
29 #include "tensorflow/core/platform/macros.h"
30 #include "tensorflow/core/platform/types.h"
31 
32 namespace xla {
33 
34 // Namespaced collection of (static) Layout utilities.
35 class LayoutUtil {
36  public:
37   // Creates a layout with the given minor-to-major dimension order. (This is a
38   // convenience function for protobuf construction.)
39   static Layout MakeLayout(absl::Span<const int64> minor_to_major,
40                            absl::Span<const Tile> tiles = {},
41                            int64 element_size_in_bits = 0,
42                            int64 memory_space = 0);
43 
44   // Similar to MakeLayout, but take indices in reverse order.
45   static Layout MakeLayoutFromMajorToMinor(
46       absl::Span<const int64> major_to_minor);
47 
48   // Returns a layout with descending ((i.e. {n, n-1, ..., 0}) minor-to-major
49   // dimensions.
50   static Layout MakeDescendingLayout(int64 rank);
51 
52   // Returns default layout for the given shape.
53   static Layout GetDefaultLayoutForShape(const Shape& shape);
54 
55   // Helper functions that create default layouts for various ranks.
56   static Layout GetDefaultLayoutForRank(int64 rank);
57   static Layout GetDefaultLayoutForR2();
58   static Layout GetDefaultLayoutForR3();
59   static Layout GetDefaultLayoutForR4();
60 
61   // Sets the default layout on the Shape.
62   static void SetToDefaultLayout(Shape* shape);
63 
64   // Returns a shape with the same dimensions as `shape` but with the default
65   // layout.
66   static Shape GetWithDefaultLayout(const Shape& shape);
67 
68   // Sets the layouts of all Shapes within the given ProgramShape to the
69   // default.
70   static void SetToDefaultLayout(ProgramShape* program_shape);
71 
72   // Validates that the layout within the given shape is correct. The check
73   // is performed for all subshapes as well. If missing layouts are allowed
74   // the check does not fail on array shapes without layouts.
75   static Status ValidateLayoutInShape(const Shape& shape,
76                                       bool allow_missing_layouts = false);
77 
78   // Validates that the provided layout satisfies invariants for the given
79   // shape.
80   static Status ValidateLayoutForShape(const Layout& layout,
81                                        const Shape& shape);
82 
83   // Clears the layout in the given Shape. After this function is called,
84   // HasLayout will return false for the shape.
85   static void ClearLayout(Shape* shape);
86 
87   // Clears the layout on all Shapes within the given ProgramShape.
88   static void ClearLayout(ProgramShape* program_shape);
89 
90   // Returns whether the given Shape is an array and has a dense format layout.
91   static bool IsDenseArray(const Shape& shape);
92 
93   // Returns whether the given Layout has a dense format.
94   static bool IsDense(const Layout& layout);
95 
96   // Returns whether the layout is monotonic and dim 0 is minor in the layout.
97   // * R0 and R1: this is always trivially true.
98   // * R2+: equivalent to column-major. Dimension 0 is the minor, dimension 1 is
99   //        more major, and so on until dimension N-1 which is the major.
100   static bool IsMonotonicWithDim0Minor(const Layout& layout);
101 
102   // Returns whether the layout is monotonic and dim 0 is major in the layout.
103   // * R0 and R1: this is always trivially true.
104   // * R2+: equivalent to row-major. Dimension 0 is the major, dimension 1 is
105   //        more minor, and so on until dimension N-1 which is the minor.
106   static bool IsMonotonicWithDim0Major(const Layout& layout);
107 
108   // Returns whether the given shape has a layout. For tuple shapes, true is
109   // returned only if all elements have layouts.
110   static bool HasLayout(const Shape& shape);
111 
112   // Returns whether all Shapes within the given ProgramShape have layouts.
113   static bool HasLayout(const ProgramShape& program_shape);
114 
115   // Returns whether lhs and rhs are identical.
116   static bool Equal(const Layout& lhs, const Layout& rhs);
117 
118   // Returns the minor_to_major array for the given Shape.  Requires that the
119   // shape is an array and has a dense layout.
120   static absl::Span<const int64> MinorToMajor(const Shape& shape);
121   static absl::Span<const int64> MinorToMajor(const Layout& layout);
122 
123   // Major(0) is the most major logical dimension number, Major(1) is the
124   // second-most-major logical dimension number and so on.
125   //
126   // This can be used to translate physical dimension numbers to logical
127   // dimension numbers. Assume that we are numbering the physical dimensions so
128   // that the most major physical dimension has physical dimension number 0 and
129   // so on. Then a physical dimension number p corresponds to the logical
130   // dimension number Major(p). So this function could also be called
131   // PhysicalToLogical().
132   //
133   // As an example, consider physical dimension number 0, which by definition is
134   // the most major. Then Major(0) is the most major logical dimension, so Major
135   // maps the physical dimension number 0 to the most major logical dimension
136   // number Major(0).
137   static int64 Major(const Layout& layout, int64 physical_dimension_number);
138 
139   // Minor(0) is the most minor logical dimension number, minor(1) is the
140   // second-most-minor logical dimension number and so on.
141   static int64 Minor(const Layout& layout, int64 physical_dimension_number);
142 
143   // Returns the inverse mapping of the Major() function. More precisely, return
144   // a vector v such that if l == Major(p), then v[l] == p.
145   //
146   // This can be used to translate logical dimension numbers into physical
147   // dimension numbers. Assume that we are numbering the physical dimensions so
148   // that the most major physical dimension has physical dimension number 0 and
149   // so on. Then a logical dimension number l corresponds to the physical
150   // dimension number MakeLogicalToPhysical(layout)[l].
151   //
152   // In the returned vector, the first element represents the most major logical
153   // dimension. The element whose contents are 0 represents the most major
154   // physical dimension, and the element with contents (rank - 1) represents
155   // the most minor physical dimension.
156   static std::vector<int64> MakeLogicalToPhysical(const Layout& layout);
157 
158   // Returns a human-readable string that represents the given layout.
159   static string HumanString(const Layout& layout);
160 
161   // Copies the layout from 'src' to 'dst'. Recursively copies layouts of
162   // tuples.  'src' and 'dst' need not be compatible but the two shapes must
163   // have the same tuple structure (if any) and arrays must have the same
164   // rank. within the shapes must have the same number of dimensions.
165   static Status CopyLayoutBetweenShapes(const Shape& src, Shape* dst);
166 
167   // Returns true if the layouts of lhs and rhs are equal, false
168   // otherwise. Recursively compares layouts of tuples.
169   //
170   // lhs and rhs need not be compatible to have the same layout but the two
171   // shapes must have the same tuple structure (if any) and arrays must have the
172   // same rank. Element type is ignored.
173   static bool LayoutsInShapesEqual(const Shape& lhs, const Shape& rhs);
174 
175   // Returns whether the given dimensions are consecutive in the given layout,
176   // not necessarily in the order given.
177   static bool AreDimensionsConsecutive(const Layout& layout,
178                                        absl::Span<const int64> dims);
179 
180   // Compute a hash for `layout`.
181   static size_t Hash(const Layout& layout);
182 
183  private:
184   TF_DISALLOW_COPY_AND_ASSIGN(LayoutUtil);
185 };
186 
187 }  // namespace xla
188 
189 #endif  // TENSORFLOW_COMPILER_XLA_LAYOUT_UTIL_H_
190