Home | History | Annotate | Download | only in xla
      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 
     25 tensorflow::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).c_str(),
     29                            ShapeUtil::HumanString(shape()).c_str());
     30   }
     31   shape_ = other_shape;
     32   return tensorflow::Status::OK();
     33 }
     34 
     35 tensorflow::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).c_str(),
     39                            ShapeUtil::HumanString(shape()).c_str());
     40   }
     41   *to_shape = shape_;
     42   return tensorflow::Status::OK();
     43 }
     44 
     45 void ShapeLayout::SetToDefaultLayout() {
     46   LayoutUtil::SetToDefaultLayout(&shape_);
     47 }
     48 
     49 bool ShapeLayout::MatchesLayoutInShape(const Shape& shape) const {
     50   return ShapeUtil::Equal(shape, shape_);
     51 }
     52 
     53 const Layout& ShapeLayout::layout() const {
     54   CHECK(LayoutIsSet());
     55   CHECK(!ShapeUtil::IsTuple(shape_));
     56   return shape_.layout();
     57 }
     58 
     59 void ShapeLayout::Clear() { LayoutUtil::ClearLayout(&shape_); }
     60 
     61 bool ShapeLayout::LayoutIsSet() const { return LayoutUtil::HasLayout(shape_); }
     62 
     63 void ShapeLayout::ResetLayout(const Layout& layout) {
     64   CHECK(!ShapeUtil::IsTuple(shape_));
     65   CHECK(!ShapeUtil::IsOpaque(shape_));
     66   *shape_.mutable_layout() = layout;
     67   TF_CHECK_OK(ShapeUtil::ValidateShape(shape_));
     68 }
     69 
     70 bool ShapeLayout::operator==(const ShapeLayout& other) const {
     71   return ShapeUtil::Equal(shape_, other.shape_);
     72 }
     73 
     74 bool ShapeLayout::operator!=(const ShapeLayout& other) const {
     75   return !ShapeUtil::Equal(shape_, other.shape_);
     76 }
     77 
     78 }  // namespace xla
     79