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_RUNTIME_FALLBACK_UTIL_TENSOR_UTIL_H_
17 #define TENSORFLOW_CORE_RUNTIME_FALLBACK_UTIL_TENSOR_UTIL_H_
18
19 #include "tensorflow/c/tf_tensor.h"
20 #include "tensorflow/c/tf_tensor_internal.h"
21 #include "tensorflow/core/framework/tensor.h"
22 #include "tensorflow/core/platform/status.h"
23 #include "tensorflow/core/runtime_fallback/util/type_util.h"
24 #include "tfrt/core_runtime/tensor_handle.h" // from @tf_runtime
25 #include "tfrt/host_context/host_buffer.h" // from @tf_runtime
26 #include "tfrt/host_context/host_context.h" // from @tf_runtime
27 #include "tfrt/support/forward_decls.h" // from @tf_runtime
28 #include "tfrt/tensor/dense_host_tensor.h" // from @tf_runtime
29 #include "tfrt/tensor/string_host_tensor.h" // from @tf_runtime
30 #include "tfrt/tensor/tensor_shape.h" // from @tf_runtime
31
32 namespace tensorflow {
33 namespace tfd {
34
35 struct TFTensorDeleter {
operatorTFTensorDeleter36 void operator()(TF_Tensor* p) const { TF_DeleteTensor(p); }
37 };
38 using OwnedTFTensor = std::unique_ptr<TF_Tensor, TFTensorDeleter>;
39
40 // Moves one ref on HostBuffer to tensorflow::Tensor.
41 tensorflow::Tensor MoveHostBufferToTfTensor(
42 tfrt::RCReference<tfrt::HostBuffer> host_buffer, tfrt::DType dtype,
43 const tfrt::TensorShape& shape);
44
45 // Creates a tensorflow::Tensor based on StringHostTensor.
46 tensorflow::Tensor CopyShtToTfTensor(const tfrt::StringHostTensor& sht);
47
48 // Converts tfrt shape to tensorflow shape.
GetTfShape(const tfrt::TensorShape & shape)49 inline tensorflow::TensorShape GetTfShape(const tfrt::TensorShape& shape) {
50 llvm::SmallVector<tfrt::Index, 4> dimensions;
51 shape.GetDimensions(&dimensions);
52 llvm::SmallVector<int64_t, 4> dims(dimensions.begin(), dimensions.end());
53 return tensorflow::TensorShape(dims);
54 }
55
56 // Retrieves TFRT TensorMetadata from a tensorflow::Tensor.
GetTensorMetadata(const tensorflow::Tensor & tf_tensor)57 inline tfrt::TensorMetadata GetTensorMetadata(
58 const tensorflow::Tensor& tf_tensor) {
59 auto dtype = tfd::GetTfrtDtype(tf_tensor.dtype());
60 auto dim_sizes = tf_tensor.shape().dim_sizes();
61 static_assert(sizeof(tfrt::Index) == sizeof(dim_sizes.front()),
62 "Invalid dimension type size");
63 auto shape = llvm::makeArrayRef(
64 reinterpret_cast<tfrt::Index*>(dim_sizes.data()), dim_sizes.size());
65 return tfrt::TensorMetadata(dtype, shape);
66 }
67
CheckBoolCompatibility()68 inline void CheckBoolCompatibility() {
69 // sizeof(bool) is implementation defined. The following may only work when
70 // sizeof(bool) is 1.
71 //
72 // TODO(tfrt-devs): It is still undefined behavior to directly cast char*
73 // between bool* and access the data. Consider allocating target objects and
74 // using memcpy instead.
75 static_assert(sizeof(bool) == 1, "Only support when bool is 1 byte.");
76 }
77
78 } // namespace tfd
79 } // namespace tensorflow
80 #endif // TENSORFLOW_CORE_RUNTIME_FALLBACK_UTIL_TENSOR_UTIL_H_
81