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