xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/shape_layout.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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/shape_layout.h"
17 
18 #include "tensorflow/compiler/xla/layout_util.h"
19 #include "tensorflow/compiler/xla/shape_util.h"
20 #include "tensorflow/compiler/xla/util.h"
21 #include "tensorflow/core/platform/logging.h"
22 
23 namespace xla {
24 
CopyLayoutFromShape(const Shape & other_shape)25 Status ShapeLayout::CopyLayoutFromShape(const Shape& other_shape) {
26   if (!ShapeUtil::Compatible(other_shape, shape_)) {
27     return InvalidArgument("Shape %s is not compatible with shape %s",
28                            ShapeUtil::HumanString(other_shape),
29                            ShapeUtil::HumanString(shape()));
30   }
31   shape_ = other_shape;
32   return OkStatus();
33 }
34 
AssignLayoutToShape(Shape * to_shape) const35 Status ShapeLayout::AssignLayoutToShape(Shape* to_shape) const {
36   if (!ShapeUtil::Compatible(*to_shape, shape_)) {
37     return InvalidArgument("Shape %s is not compatible with shape %s",
38                            ShapeUtil::HumanString(*to_shape),
39                            ShapeUtil::HumanString(shape()));
40   }
41   *to_shape = shape_;
42   return OkStatus();
43 }
44 
SetToDefaultLayout()45 void ShapeLayout::SetToDefaultLayout() {
46   LayoutUtil::SetToDefaultLayout(&shape_);
47 }
48 
MatchesLayoutInShape(const Shape & shape,bool minor_to_major_only,bool ignore_fully_empty_tiling) const49 bool ShapeLayout::MatchesLayoutInShape(const Shape& shape,
50                                        bool minor_to_major_only,
51                                        bool ignore_fully_empty_tiling) const {
52   auto equal = Shape::Equal().IgnoreDynamicDimension();
53   if (ignore_fully_empty_tiling) {
54     bool fully_empty_tiling = true;
55     auto check_tiling = [&fully_empty_tiling](const Shape& subshape,
56                                               const xla::ShapeIndex& index) {
57       if (!fully_empty_tiling) {
58         return;
59       }
60       if (subshape.IsArray() && !subshape.layout().tiles().empty()) {
61         fully_empty_tiling = false;
62       }
63     };
64     ShapeUtil::ForEachSubshape(shape, check_tiling);
65     if (fully_empty_tiling) {
66       equal.MinorToMajorOnlyInLayout();
67     } else {
68       fully_empty_tiling = true;
69       // Check the other shape.
70       ShapeUtil::ForEachSubshape(shape_, check_tiling);
71       if (fully_empty_tiling) {
72         equal.MinorToMajorOnlyInLayout();
73       }
74     }
75   }
76   if (minor_to_major_only) {
77     equal.MinorToMajorOnlyInLayout();
78   }
79   return equal(shape, shape_);
80 }
81 
layout() const82 const Layout& ShapeLayout::layout() const {
83   CHECK(LayoutIsSet());
84   CHECK(!shape_.IsTuple());
85   return shape_.layout();
86 }
87 
Clear()88 void ShapeLayout::Clear() { LayoutUtil::ClearLayout(&shape_); }
89 
LayoutIsSet() const90 bool ShapeLayout::LayoutIsSet() const { return LayoutUtil::HasLayout(shape_); }
91 
ResetLayout(const Layout & layout)92 void ShapeLayout::ResetLayout(const Layout& layout) {
93   CHECK(!shape_.IsTuple());
94   CHECK(!shape_.IsOpaque());
95   *shape_.mutable_layout() = layout;
96   TF_CHECK_OK(ShapeUtil::ValidateShape(shape_));
97 }
98 
ResetLayout(const Layout & layout,ShapeIndexView shape_index)99 void ShapeLayout::ResetLayout(const Layout& layout,
100                               ShapeIndexView shape_index) {
101   *ShapeUtil::GetMutableSubshape(&shape_, shape_index)->mutable_layout() =
102       layout;
103   TF_CHECK_OK(ShapeUtil::ValidateShape(shape_));
104 }
105 
operator ==(const ShapeLayout & other) const106 bool ShapeLayout::operator==(const ShapeLayout& other) const {
107   return Shape::Equal().IgnoreDynamicDimension()(shape_, other.shape_);
108 }
109 
operator !=(const ShapeLayout & other) const110 bool ShapeLayout::operator!=(const ShapeLayout& other) const {
111   return !Shape::Equal().IgnoreDynamicDimension()(shape_, other.shape_);
112 }
113 
114 }  // namespace xla
115