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 Status::OK();
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 Status::OK();
43 }
44
SetToDefaultLayout()45 void ShapeLayout::SetToDefaultLayout() {
46 LayoutUtil::SetToDefaultLayout(&shape_);
47 }
48
MatchesLayoutInShape(const Shape & shape,bool minor_to_major_only) const49 bool ShapeLayout::MatchesLayoutInShape(const Shape& shape,
50 bool minor_to_major_only) const {
51 auto equal = Shape::Equal().IgnoreDynamicDimension();
52 if (minor_to_major_only) {
53 equal.MinorToMajorOnlyInLayout();
54 }
55 return equal(shape, shape_);
56 }
57
layout() const58 const Layout& ShapeLayout::layout() const {
59 CHECK(LayoutIsSet());
60 CHECK(!shape_.IsTuple());
61 return shape_.layout();
62 }
63
Clear()64 void ShapeLayout::Clear() { LayoutUtil::ClearLayout(&shape_); }
65
LayoutIsSet() const66 bool ShapeLayout::LayoutIsSet() const { return LayoutUtil::HasLayout(shape_); }
67
ResetLayout(const Layout & layout)68 void ShapeLayout::ResetLayout(const Layout& layout) {
69 CHECK(!shape_.IsTuple());
70 CHECK(!shape_.IsOpaque());
71 *shape_.mutable_layout() = layout;
72 TF_CHECK_OK(ShapeUtil::ValidateShape(shape_));
73 }
74
ResetLayout(const Layout & layout,ShapeIndexView shape_index)75 void ShapeLayout::ResetLayout(const Layout& layout,
76 ShapeIndexView shape_index) {
77 CHECK(shape_.IsTuple());
78 *ShapeUtil::GetMutableSubshape(&shape_, shape_index)->mutable_layout() =
79 layout;
80 TF_CHECK_OK(ShapeUtil::ValidateShape(shape_));
81 }
82
operator ==(const ShapeLayout & other) const83 bool ShapeLayout::operator==(const ShapeLayout& other) const {
84 return Shape::Equal().IgnoreDynamicDimension()(shape_, other.shape_);
85 }
86
operator !=(const ShapeLayout & other) const87 bool ShapeLayout::operator!=(const ShapeLayout& other) const {
88 return !Shape::Equal().IgnoreDynamicDimension()(shape_, other.shape_);
89 }
90
91 } // namespace xla
92