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