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) const49 bool ShapeLayout::MatchesLayoutInShape(const Shape& shape) const {
50 return ShapeUtil::Equal(shape, shape_);
51 }
52
layout() const53 const Layout& ShapeLayout::layout() const {
54 CHECK(LayoutIsSet());
55 CHECK(!shape_.IsTuple());
56 return shape_.layout();
57 }
58
Clear()59 void ShapeLayout::Clear() { LayoutUtil::ClearLayout(&shape_); }
60
LayoutIsSet() const61 bool ShapeLayout::LayoutIsSet() const { return LayoutUtil::HasLayout(shape_); }
62
ResetLayout(const Layout & layout)63 void ShapeLayout::ResetLayout(const Layout& layout) {
64 CHECK(!shape_.IsTuple());
65 CHECK(!shape_.IsOpaque());
66 *shape_.mutable_layout() = layout;
67 TF_CHECK_OK(ShapeUtil::ValidateShape(shape_));
68 }
69
ResetLayout(const Layout & layout,ShapeIndexView shape_index)70 void ShapeLayout::ResetLayout(const Layout& layout,
71 ShapeIndexView shape_index) {
72 CHECK(shape_.IsTuple());
73 *ShapeUtil::GetMutableSubshape(&shape_, shape_index)->mutable_layout() =
74 layout;
75 TF_CHECK_OK(ShapeUtil::ValidateShape(shape_));
76 }
77
operator ==(const ShapeLayout & other) const78 bool ShapeLayout::operator==(const ShapeLayout& other) const {
79 return ShapeUtil::Equal(shape_, other.shape_);
80 }
81
operator !=(const ShapeLayout & other) const82 bool ShapeLayout::operator!=(const ShapeLayout& other) const {
83 return !ShapeUtil::Equal(shape_, other.shape_);
84 }
85
86 } // namespace xla
87