xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/jit/test_util.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 // Helper functions for tests.
17 
18 #ifndef TENSORFLOW_COMPILER_JIT_TEST_UTIL_H_
19 #define TENSORFLOW_COMPILER_JIT_TEST_UTIL_H_
20 
21 #include <map>
22 #include <unordered_map>
23 #include <vector>
24 
25 #include "tensorflow/compiler/jit/shape_inference.h"
26 #include "tensorflow/core/common_runtime/optimization_registry.h"
27 #include "tensorflow/core/framework/function.h"
28 #include "tensorflow/core/framework/op.h"
29 #include "tensorflow/core/framework/partial_tensor_shape.h"
30 #include "tensorflow/core/graph/graph.h"
31 #include "tensorflow/core/lib/core/status.h"
32 #include "tensorflow/core/public/session_options.h"
33 
34 namespace tensorflow {
35 
36 // Tests that the shapes in 'shape_info' for the nodes in `graph` match
37 // `expected_shapes`. Returns an error if there are nodes in `expected_shapes`
38 // that do not have shape information. Ignores nodes in `graph` that do not have
39 // `expected_shapes` entries.
40 Status ShapeAnnotationsMatch(
41     const Graph& graph, const GraphShapeInfo& shape_info,
42     std::map<string, std::vector<PartialTensorShape>> expected_shapes);
43 
44 // A helper object to create GraphOptimizationPassOptions.
45 struct GraphOptimizationPassWrapper {
GraphOptimizationPassWrapperGraphOptimizationPassWrapper46   explicit GraphOptimizationPassWrapper() : library(OpRegistry::Global(), {}) {
47     session_options.env = Env::Default();
48   }
49 
50   // Create GraphOptimizationPassOptions with a graph passed in constructor and
51   // sensible options.
CreateGraphOptimizationPassOptionsGraphOptimizationPassWrapper52   GraphOptimizationPassOptions CreateGraphOptimizationPassOptions(
53       std::unique_ptr<Graph>* graph) {
54     GraphOptimizationPassOptions options;
55     options.session_options = &session_options;
56     options.flib_def = &library;
57     options.graph = graph;
58     return options;
59   }
60 
61   FunctionLibraryDefinition library;
62   SessionOptions session_options;
63 };
64 
65 }  // namespace tensorflow
66 
67 
68 #endif  // TENSORFLOW_COMPILER_JIT_TEST_UTIL_H_
69