xref: /aosp_15_r20/external/tensorflow/tensorflow/core/ir/importexport/convert_tensor.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_CONVERT_TENSOR_H_
17 #define TENSORFLOW_CORE_IR_IMPORTEXPORT_CONVERT_TENSOR_H_
18 
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "mlir/IR/Attributes.h"  // from @llvm-project
22 #include "mlir/IR/Builders.h"  // from @llvm-project
23 #include "tensorflow/core/framework/tensor.h"
24 #include "tensorflow/core/framework/tensor.pb.h"
25 #include "tensorflow/core/framework/tensor_shape.pb.h"
26 #include "tensorflow/core/ir/dialect.h"
27 #include "tensorflow/core/ir/types/dialect.h"
28 #include "tensorflow/stream_executor/lib/statusor.h"
29 
30 namespace mlir {
31 namespace tfg {
32 
33 // Converts an TensorFlow tensor proto into an MLIR elements attribute.
34 tensorflow::StatusOr<ElementsAttr> ConvertTensorProto(
35     const tensorflow::TensorProto& input_tensor, Builder builder);
36 
37 // Converts an TensorFlow tensor into an MLIR elements attribute.
38 tensorflow::StatusOr<ElementsAttr> ConvertTensor(
39     const tensorflow::Tensor& input_tensor, Builder builder);
40 
41 // Converts a shape from MLIR to a TensorFlow tensor shape proto.
42 void ConvertToTensorShapeProto(ArrayRef<int64_t> shape,
43                                tensorflow::TensorShapeProto* output_shape);
44 
45 // Converts an MLIR type to a TensorFlow tensor shape.
46 tensorflow::PartialTensorShape ConvertTypeToTensorShape(const Type& type);
47 
48 // Converts a TensorFlow shape attribute to an MLIR shape attribute.
49 tensorflow::StatusOr<ShapeAttr> ConvertTensorShapeProto(
50     const tensorflow::TensorShapeProto& shape, MLIRContext* context);
51 
52 // Fill in the contents of TensorShapeProto for the given shape.
53 // ShapeContainerT is any type with the following methods:
54 //   bool hasRank()
55 //   ArrayRef<int64_t> getShape()
56 // This includes TF::ShapeAttr and ShapedType.
57 template <typename ShapeContainerT>
SetTensorShapeProto(ShapeContainerT shape,tensorflow::TensorShapeProto * proto)58 void SetTensorShapeProto(ShapeContainerT shape,
59                          tensorflow::TensorShapeProto* proto) {
60   if (shape.hasRank()) {
61     for (int64_t dim : shape.getShape()) {
62       proto->add_dim()->set_size(dim);
63     }
64   } else {
65     proto->set_unknown_rank(true);
66   }
67 }
68 
69 // Converts an MLIR elements attribute to a TensorFlow tensor proto.
70 tensorflow::Status ConvertToTensorProto(ElementsAttr attr,
71                                         tensorflow::TensorProto* output_tensor);
72 
73 // Converts an MLIR elements attribute to a TensorFlow tensor.
74 tensorflow::Status ConvertToTensor(ElementsAttr attr,
75                                    tensorflow::Tensor* output_tensor);
76 
77 }  // namespace tfg
78 }  // namespace mlir
79 
80 #endif  // TENSORFLOW_CORE_IR_IMPORTEXPORT_CONVERT_TENSOR_H_
81