1 /* Copyright 2016 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 #ifndef TENSORFLOW_CORE_FRAMEWORK_SHAPE_INFERENCE_TESTUTIL_H_ 16 #define TENSORFLOW_CORE_FRAMEWORK_SHAPE_INFERENCE_TESTUTIL_H_ 17 18 #include <vector> 19 20 #include "tensorflow/core/framework/node_def.pb.h" 21 #include "tensorflow/core/framework/shape_inference.h" 22 #include "tensorflow/core/lib/core/status.h" 23 #include "tensorflow/core/lib/core/stringpiece.h" 24 #include "tensorflow/core/lib/strings/str_util.h" 25 #include "tensorflow/core/platform/types.h" 26 #include "tensorflow/core/public/version.h" 27 28 // Contains utilities for writing tests for shape inference functions. 29 30 namespace tensorflow { 31 32 class Tensor; 33 34 struct ShapeInferenceTestOp { 35 typedef std::pair<string, DataType> ShapeAndType; ShapeInferenceTestOpShapeInferenceTestOp36 explicit ShapeInferenceTestOp(StringPiece name) : name(string(name)) {} 37 string name; 38 NodeDef node_def; 39 std::vector<const Tensor*> input_tensors; 40 std::vector<std::vector<ShapeAndType>*> 41 input_resource_handle_shapes_and_types; 42 int graph_def_version = TF_GRAPH_DEF_VERSION; 43 }; 44 45 namespace shape_inference { 46 47 class ShapeInferenceTestutil { 48 public: 49 // Run shape inference for <op.name>, given inputs specified by <ins> 50 // and returns an error if the inferred shape does not match expected_outs. 51 // 52 // <ins> is a semicolon separated list of shapes. Each shape is formatted 53 // according to the formatting per 54 // shape_inference::InferenceContext::InferenceContext. 55 // 56 // <expected_outs> is a semicolon separated list of shapes. Each shape is 57 // formatted as one of: 58 // * ? - an unknown shape, but not matching an input shape 59 // * in0|in2|... - output shape must be the same as one of these input shapes. 60 // * [1,?,d0_0|d0_1] - output shape is of known rank, with comma-separated 61 // dimension values. 62 // Each dimension value is one of: 63 // * a constant, which means that constant not equal to a specific input 64 // * ?, which means an unknown dim size not equal to a specific input 65 // * d0_0|d1_2, indicating that the dim size must be equal to one of 66 // the given input dimensions; the first number is the input # and 67 // the second is which dimension in that input it corresponds to. 68 // <expected_outs> can be "e"; this is used to indicate that shape inference 69 // should have failed. 70 static Status InferShapes(ShapeInferenceTestOp op, const string& ins, 71 const string& expected_outs); 72 73 private: ShapeInferenceTestutil()74 ShapeInferenceTestutil() {} 75 76 // Makes a shape out of 'spec'. 77 static Status MakeShapeFromString(InferenceContext::ShapeManager* manager, 78 const string& spec, ShapeHandle* output); 79 }; 80 81 } // namespace shape_inference 82 83 #define INFER_OK(op, i, o) \ 84 EXPECT_EQ( \ 85 "", ::tensorflow::shape_inference::ShapeInferenceTestutil::InferShapes( \ 86 op, i, o) \ 87 .error_message()) 88 #define INFER_ERROR(error_substring, op, i) \ 89 { \ 90 std::string error_message = \ 91 ::tensorflow::shape_inference::ShapeInferenceTestutil::InferShapes( \ 92 op, i, "e") \ 93 .error_message(); \ 94 const std::string substring = error_substring; \ 95 EXPECT_NE("", error_message); \ 96 EXPECT_TRUE(absl::StrContains(error_message, substring)) \ 97 << "Expected to see '" << substring << "' in '" << error_message \ 98 << "'"; \ 99 } 100 101 } // namespace tensorflow 102 103 #endif // TENSORFLOW_CORE_FRAMEWORK_SHAPE_INFERENCE_TESTUTIL_H_ 104