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" // TF:llvm-project 22 #include "mlir/IR/Builders.h" // TF: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/stream_executor/lib/statusor.h" 27 28 namespace tensorflow { 29 30 using stream_executor::port::StatusOr; 31 32 // Converts an TensorFlow tensor proto into an MLIR elements attribute. 33 StatusOr<mlir::ElementsAttr> ConvertTensorProto(const TensorProto& input_tensor, 34 mlir::Builder* builder); 35 36 // Converts an TensorFlow tensor into an MLIR elements attribute. 37 StatusOr<mlir::ElementsAttr> ConvertTensor(const Tensor& input_tensor, 38 mlir::Builder* builder); 39 40 // Converts a shape from MLIR to a TensorFlow tensor shape proto. 41 void ConvertToTensorShapeProto(llvm::ArrayRef<int64_t> shape, 42 TensorShapeProto* output_shape); 43 44 // Converts an MLIR type to a TensorFlow tensor shape. 45 PartialTensorShape ConvertTypeToTensorShape(const mlir::Type& type); 46 47 // Converts an MLIR elements attribute to a TensorFlow tensor proto. 48 Status ConvertToTensorProto(mlir::ElementsAttr attr, 49 TensorProto* output_tensor); 50 51 // Converts an MLIR elements attribute to a TensorFlow tensor. 52 Status ConvertToTensor(mlir::ElementsAttr attr, Tensor* output_tensor); 53 54 // Decodes the given opaque elements attribute holding tensor content into a 55 // human-readable elements attribute. 56 StatusOr<mlir::ElementsAttr> DecodeOpaqueTensor( 57 mlir::OpaqueElementsAttr input_attr, mlir::Builder builder); 58 59 } // namespace tensorflow 60 61 #endif // TENSORFLOW_COMPILER_MLIR_TENSORFLOW_UTILS_CONVERT_TENSOR_H_ 62