1 /* Copyright 2018 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 #ifndef TENSORFLOW_CORE_GRAPPLER_UTILS_SYMBOLIC_SHAPES_H_ 17 #define TENSORFLOW_CORE_GRAPPLER_UTILS_SYMBOLIC_SHAPES_H_ 18 19 #include "tensorflow/core/framework/tensor_shape.pb.h" 20 #include "tensorflow/core/grappler/costs/op_performance_data.pb.h" 21 #include "tensorflow/core/platform/types.h" 22 23 namespace tensorflow { 24 namespace grappler { 25 26 bool IsKnown(const TensorShapeProto::Dim& dim); 27 bool IsKnownSymbolically(const TensorShapeProto::Dim& dim); 28 bool IsUnknown(const TensorShapeProto::Dim& dim); 29 30 // Shape is symbolically defined, if it has a known rank, and each dimension is 31 // known (dim_size >= 0), or is a symbolic dimension size (dim_size <= -2). 32 bool ShapeIsSymbolicallyDefined(const TensorShapeProto& shape); 33 bool ShapeIsSymbolicallyDefined(const OpInfo::TensorProperties& properties); 34 35 // Returns the rank of the shape ir -1 if unknown 36 int Rank(const TensorShapeProto& shape); 37 38 // Returns the number of coefficients in the shape or -1 if unknown. 39 // TODO(bsteiner) Add a function that computes the minimum size of the tensor, 40 // ie the size assuming all the symbolic dimensions take the value 1. 41 int64 NumCoefficients(const TensorShapeProto& shape); 42 43 // Shapes are symbolically equal, if they have the same rank, they are known or 44 // symbolically defined, and have matching dimensions. 45 bool ShapesSymbolicallyEqual(const TensorShapeProto& left, 46 const TensorShapeProto& right); 47 bool ShapesSymbolicallyEqual(const OpInfo::TensorProperties& left, 48 const OpInfo::TensorProperties& right); 49 50 // Check if two shapes can be broadcasted to each other. Both shapes must be at 51 // least symbolically defined, and the have valid BCast instance. 52 bool ShapesBroadcastable(const TensorShapeProto& left, 53 const TensorShapeProto& right); 54 bool ShapesBroadcastable(const OpInfo::TensorProperties& left, 55 const OpInfo::TensorProperties& right); 56 bool ShapeAfterBroadcast(const TensorShapeProto& left, 57 const TensorShapeProto& right, 58 TensorShapeProto* output_shape); 59 60 // Return true if can prove, that tensor of size 'left' is smaller than tensor 61 // of size 'right'. Return false if it's larger or equal, or it's impossible to 62 // compare because of unknown dimensions, or mismatch in symbolic dimensions. 63 bool CompareSymbolicallyShapedTensorSizes(const TensorShapeProto& left, 64 const TensorShapeProto& right); 65 bool CompareSymbolicallyShapedTensorSizes( 66 const OpInfo::TensorProperties& left, 67 const OpInfo::TensorProperties& right); 68 69 // Returns the ratio of the sizes of the 2 shapes if known statically, or -1 70 // otherwise. 71 int64 ComputeSizeRatio(const TensorShapeProto& numerator, 72 const TensorShapeProto& denominator); 73 74 } // namespace grappler 75 } // end namespace tensorflow 76 77 #endif // TENSORFLOW_CORE_GRAPPLER_UTILS_SYMBOLIC_SHAPES_H_ 78