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 // Utilities for working with XLA shapes. 17 18 #ifndef TENSORFLOW_COMPILER_TF2XLA_SHAPE_UTIL_H_ 19 #define TENSORFLOW_COMPILER_TF2XLA_SHAPE_UTIL_H_ 20 21 #include <vector> 22 23 #include "tensorflow/compiler/xla/shape.h" 24 #include "tensorflow/compiler/xla/statusor.h" 25 #include "tensorflow/compiler/xla/xla_data.pb.h" 26 #include "tensorflow/core/framework/tensor_shape.h" 27 #include "tensorflow/core/framework/types.pb.h" 28 29 namespace tensorflow { 30 31 // Convert an XLA Shape into the equivalent TensorFlow shape. May fail since 32 // not all XLA shapes can be represented as TensorShapes. 33 Status XLAShapeToTensorShape(const xla::Shape& shape, 34 TensorShape* tensor_shape); 35 36 // Convert a TensorShape into the equivalent XLA Shape proto. Unlike Tensorflow, 37 // XLA shapes include the type. Not all `dtype` values can be represented by 38 // XLA, so this conversion may fail. 39 Status TensorShapeToXLAShape(DataType dtype, const TensorShape& tensor_shape, 40 xla::Shape* shape); 41 42 // Converts a TensorShape into the equivalent XLA Shape proto, taking an 43 // xla::PrimitiveType to specify the element type. This never fails. 44 xla::Shape TensorShapeToXLAShape(xla::PrimitiveType type, 45 const TensorShape& tensor_shape); 46 47 // Convert a PartialTensorShape into the equivalent XLA Shape proto. An shape 48 // with unknown rank is represented by an r1 with empty dimension. 49 Status TensorShapeToXLAShape(DataType dtype, 50 const PartialTensorShape& tensor_shape, 51 xla::Shape* shape); 52 53 // Convert a PartialTensorShape into the equivalent XLA Shape proto. An shape 54 // with unknown rank is represented by an r1 with empty dimension. 55 xla::Shape TensorShapeToXLAShape(xla::PrimitiveType type, 56 const PartialTensorShape& tensor_shape); 57 58 // Given an XLA shape with layouts, builds a layout vector in the form able to 59 // be fed to ops like InfeedEnqueue/InfeedEnqueueTuple/XRTAllocateV2/.... 60 // THe returned vector is a linearized sequence of the minor-to-major values of 61 // the layouts held within the input shape. 62 // In case the input shape is a tuple, the minor-to-major values will be in the 63 // order of the tuple elements within the tuple shape. 64 // If a shape (or a subshape of a tuple shape) has missing layout, a rank long 65 // sequence of -1 values will be emitted. 66 StatusOr<std::vector<int>> GetShapeLayoutVector(const xla::Shape& shape); 67 68 // Given the input shape and a linearized sequence of the minor-to-major values 69 // of the layouts, create the output shape by rewriting the input shape layouts. 70 // If a layout is missing (has -1 values) for a matching tuple subshape, the 71 // layout_func will be called, if not nullptr. 72 Status GetShapeWithLayout( 73 const xla::Shape& input_shape, absl::Span<const int64> minor_to_major, 74 const std::function<xla::Layout(const xla::Shape&)>& layout_func, 75 xla::Shape* output_shape); 76 77 } // namespace tensorflow 78 79 #endif // TENSORFLOW_COMPILER_TF2XLA_SHAPE_UTIL_H_ 80