xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/mlir/tensorflow/utils/convert_tensor.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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_UTILS_CONVERT_TENSOR_H_
17 #define TENSORFLOW_COMPILER_MLIR_TENSORFLOW_UTILS_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/compiler/mlir/tensorflow/ir/tf_attributes.h"
24 #include "tensorflow/core/framework/tensor.h"
25 #include "tensorflow/core/framework/tensor.pb.h"
26 #include "tensorflow/core/framework/tensor_shape.pb.h"
27 #include "tensorflow/stream_executor/lib/statusor.h"
28 
29 namespace tensorflow {
30 
31 using stream_executor::port::StatusOr;
32 
33 // Converts an TensorFlow tensor proto into an MLIR elements attribute.
34 StatusOr<mlir::ElementsAttr> ConvertTensorProto(const TensorProto& input_tensor,
35                                                 mlir::Builder* builder);
36 
37 // Converts an TensorFlow tensor into an MLIR elements attribute.
38 StatusOr<mlir::ElementsAttr> ConvertTensor(const Tensor& input_tensor,
39                                            mlir::Builder* builder);
40 
41 // Converts a shape from MLIR to a TensorFlow tensor shape proto.
42 void ConvertToTensorShapeProto(llvm::ArrayRef<int64_t> shape,
43                                TensorShapeProto* output_shape);
44 
45 // Converts an MLIR type to a TensorFlow tensor shape.
46 PartialTensorShape ConvertTypeToTensorShape(const mlir::Type& type);
47 
48 // Converts an MLIR shaped type to a TensorFlow shape attribute.
49 mlir::TF::ShapeAttr ConvertTypeToTensorShapeAttr(const mlir::Type& type);
50 
51 // Converts a TensorFlow shape attribute to an MLIR shape attribute.
52 StatusOr<mlir::Attribute> ConvertTensorShapeProto(const TensorShapeProto& shape,
53                                                   mlir::MLIRContext* context);
54 
55 // Converts an MLIR elements attribute to a TensorFlow tensor proto.
56 Status ConvertToTensorProto(mlir::ElementsAttr attr,
57                             TensorProto* output_tensor);
58 
59 // Converts an MLIR elements attribute to a TensorFlow tensor.
60 Status ConvertToTensor(mlir::ElementsAttr attr, Tensor* output_tensor);
61 
62 }  // namespace tensorflow
63 
64 #endif  // TENSORFLOW_COMPILER_MLIR_TENSORFLOW_UTILS_CONVERT_TENSOR_H_
65