xref: /aosp_15_r20/external/tensorflow/tensorflow/core/ir/importexport/convert_attributes.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2021 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_CORE_IR_IMPORTEXPORT_EXPORT_UTILS_H_
17 #define TENSORFLOW_CORE_IR_IMPORTEXPORT_EXPORT_UTILS_H_
18 
19 #include <string>
20 
21 #include "absl/strings/string_view.h"
22 #include "mlir/IR/Attributes.h"  // from @llvm-project
23 #include "mlir/IR/Builders.h"  // from @llvm-project
24 #include "mlir/IR/BuiltinTypes.h"  // from @llvm-project
25 #include "tensorflow/core/framework/node_def_util.h"
26 #include "tensorflow/core/framework/op_def.pb.h"
27 #include "tensorflow/core/framework/resource_handle.pb.h"
28 #include "tensorflow/core/ir/dialect.h"
29 #include "tensorflow/core/platform/statusor.h"
30 
31 namespace mlir {
32 namespace tfg {
33 
34 // Convert the list of MLIR Attributes `attrs` to the `tensorflow::AttrValueMap`
35 // `values`.
36 tensorflow::Status ConvertAttributes(ArrayRef<NamedAttribute> attrs,
37                                      ArrayRef<StringRef> attrs_to_ignore,
38                                      bool remove_ref_type,
39                                      tensorflow::AttrValueMap* values);
40 
41 // Convert the MLIR attribute `attr` and return a `tensorflow::AttrValue`.
42 tensorflow::StatusOr<tensorflow::AttrValue> ConvertAttribute(Attribute attr);
43 
44 tensorflow::Status SetShapeAttribute(absl::string_view name,
45                                      ShapedType shaped_type,
46                                      tensorflow::AttrValueMap* values);
47 
48 // Converts an MLIR shaped type to a TensorFlow shape attribute.
49 ShapeAttr ConvertTypeToTensorShapeAttr(const Type& type);
50 
51 /// Import from TensorFlow to MLIR
52 
53 // Converts non func AttrValue proto into an MLIR attribute. Func attribute is
54 // exclused in this function because the function might be renamed when the
55 // function definition is imported.
56 tensorflow::StatusOr<Attribute> ConvertNonFuncAttributeValue(
57     const tensorflow::AttrValue& value, Builder& builder);
58 
59 // Converts all kinds of AttrValue proto into an MLIR attribute.
60 tensorflow::StatusOr<Attribute> ConvertAttributeValue(
61     const tensorflow::AttrValue& value, Builder& builder);
62 
63 // Convert the MLIR FullTyoe attribute `attr` and return a
64 // `tensorflow::FullTypeDef`.
65 tensorflow::StatusOr<tensorflow::FullTypeDef> ConvertAttribute(
66     tf_type::FullTypeAttr full_type);
67 
68 // Converts fulltype proto to attribute.
69 tensorflow::StatusOr<::mlir::tf_type::FullTypeAttr> ConvertAttribute(
70     const tensorflow::FullTypeDef& full_type, Builder& builder);
71 
72 // Convert an array of handle data (pairs of data types and shapes) to an array
73 // attribute of tensor types.
74 tensorflow::StatusOr<ArrayAttr> ConvertHandleData(
75     Builder builder,
76     const tensorflow::protobuf::RepeatedPtrField<
77         tensorflow::ResourceHandleProto_DtypeAndShape>& handle_data);
78 
79 // Convert an array of handle data into the `handle_data` field of the provided
80 // ArgDef. Each entry of the array is expected to be a TensorType.
81 tensorflow::Status ConvertHandleData(ArrayAttr handle_data_arr,
82                                      tensorflow::OpDef::ArgDef* arg);
83 
84 }  // namespace tfg
85 }  // namespace mlir
86 
87 #endif  // TENSORFLOW_CORE_IR_IMPORTEXPORT_EXPORT_UTILS_H_
88