1 /* Copyright 2019 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_COMPILER_MLIR_TENSORFLOW_TRANSLATE_MLIR_ROUNDTRIP_FLAGS_H_
17 #define TENSORFLOW_COMPILER_MLIR_TENSORFLOW_TRANSLATE_MLIR_ROUNDTRIP_FLAGS_H_
18 
19 #include "absl/container/flat_hash_set.h"
20 #include "llvm/ADT/MapVector.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "tensorflow/core/framework/tensor_shape.pb.h"
23 #include "tensorflow/core/framework/types.h"
24 #include "tensorflow/core/framework/types.pb.h"
25 #include "tensorflow/core/lib/core/status.h"
26 
27 namespace tensorflow {
28 
29 struct ArrayInfo {
30   // The node type when the input node is imported. Typically needs to be
31   // specified when passing arbitrary nodes (some node attributes are removed).
32   DataType imported_dtype;
33 
34   // Node "shape" attribute value.
35   TensorShapeProto shape;
36 };
37 
38 struct GraphImportConfig {
39   using InputArrays =
40       llvm::MapVector<string, ArrayInfo, llvm::StringMap<unsigned>>;
41   // Maps input node names to node data types and shapes.
42   InputArrays inputs;
43   // name:index strings for the data outputs.
44   std::vector<string> outputs;
45   // name strings for the control outputs.
46   std::vector<string> control_outputs;
47   // Setting prune_unused_nodes to true, would prune unreachable nodes if
48   // output_arrays is specified.
49   bool prune_unused_nodes = false;
50   // If true, inputs of type LegacyFedInput are replaced with Placeholder ops.
51   // LegacyFedInput ops have two outputs unlike Placeholder which has only one
52   // output, so if both outputs of the LegacyFedInput ops are used then returns
53   // an error.
54   bool convert_legacy_fed_inputs = false;
55   // If true, the main graph will be treated as a function.
56   bool graph_as_function = false;
57   // If true, upgrade legacy features of the graph (for instance, functionalize
58   // control-flow).
59   bool upgrade_legacy = false;
60   // If true, functionalization is restricted to TPU nodes. This is only needed
61   // if upgrade_legacy is true and if upgrading legacy features of the graph
62   // (which includes functionalization) runs before TPU cluster extraction, as
63   // for example in the MLIR-based TPU bridge. Otherwise, this parameter should
64   // stay false.
65   bool restrict_functionalization_to_tpu_nodes = false;
66   // If true, enables shape inference on input.
67   // TODO(jpienaar): This will be removed shortly.
68   bool enable_shape_inference = true;
69 };
70 
71 struct GraphExportConfig {
72   // Whether to export shape attribute for the NodeDefs in the GraphDef.
73   bool export_shapes = true;
74   // Whether to export library field in the GraphDef.
75   bool export_library = true;
76   // Whether to export debug original node name in the GraphDef.
77   bool export_debug_info = true;
78 };
79 
80 // Parses the command line flag strings to the specification of nodes in
81 // the Graph.
82 Status ParseOutputArrayInfo(absl::string_view array_names,
83                             std::vector<string>* outputs);
84 
85 Status ParseOutputArrayInfo(const std::vector<string>& output_names,
86                             std::vector<string>* outputs);
87 
88 // Parses the command line flag strings to the specification of nodes in
89 // the Graph. `data_types` input string can be empty since the flag is optional.
90 Status ParseInputArrayInfo(absl::string_view array_names,
91                            absl::string_view data_types,
92                            absl::string_view shapes,
93                            GraphImportConfig::InputArrays* inputs);
94 
95 Status ParseInputArrayInfo(
96     const std::vector<string>& node_names,
97     const std::vector<string>& node_dtypes,
98     const std::vector<llvm::Optional<std::vector<int>>>& node_shapes,
99     GraphImportConfig::InputArrays* inputs);
100 
101 // Parses shapes from the given string into shapes_vector which is a structured
102 // format.
103 // NOTE: If shapes_str is empty, shapes_vector will also be empty.
104 Status ParseNodeShapes(
105     absl::string_view shapes_str,
106     std::vector<llvm::Optional<std::vector<int>>>& shapes_vector);
107 
108 // Parses names from the given string into the names_vector.
109 // NOTE: If names_str is empty, names_vector will also be empty.
110 Status ParseNodeNames(absl::string_view names_str,
111                       std::vector<std::string>& names_vector);
112 
113 // Parses data types from the given string into the data_type_vector.
114 // NOTE: If data_types_str is empty, data_type_vector will also be empty.
115 Status ParseNodeDataTypes(absl::string_view data_types_str,
116                           std::vector<std::string>& data_type_vector);
117 
118 }  // namespace tensorflow
119 
120 #endif  // TENSORFLOW_COMPILER_MLIR_TENSORFLOW_TRANSLATE_MLIR_ROUNDTRIP_FLAGS_H_
121