• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "tensorflow/core/runtime_fallback/util/tensor_util.h"
16 
17 #include "tensorflow/core/runtime_fallback/util/type_util.h"
18 #include "tfrt/support/error_util.h"  // from @tf_runtime
19 
20 namespace tensorflow {
21 namespace tfd {
22 
23 using tfrt::DType;
24 using tfrt::Expected;
25 using tfrt::HostBuffer;
26 using tfrt::RCReference;
27 using tfrt::StringHostTensor;
28 using tfrt::TensorShape;
29 
30 // Moves one ref on HostBuffer to tensorflow::Tensor.
MoveHostBufferToTfTensor(RCReference<HostBuffer> host_buffer,DType dtype,const tfrt::TensorShape & shape)31 tensorflow::Tensor MoveHostBufferToTfTensor(RCReference<HostBuffer> host_buffer,
32                                             DType dtype,
33                                             const tfrt::TensorShape& shape) {
34   llvm::SmallVector<tfrt::Index, 4> dims;
35   shape.GetDimensions(&dims);
36 
37   auto deallocator = [](void* data, size_t len, void* arg) {
38     auto* host_buffer = reinterpret_cast<HostBuffer*>(arg);
39     host_buffer->DropRef();
40   };
41 
42   CheckBoolCompatibility();
43   // Transfer one HostBuffer ref to TFTensor.
44   auto* data = host_buffer->data();
45   auto size = host_buffer->size();
46   OwnedTFTensor tf_tensor{TF_NewTensor(
47       static_cast<TF_DataType>(GetTfDataType(dtype)), dims.data(), dims.size(),
48       data, size, deallocator, host_buffer.release())};
49   return down_cast<TensorInterface*>(tf_tensor->tensor)->Tensor();
50 }
51 
CopyShtToTfTensor(const StringHostTensor & sht)52 tensorflow::Tensor CopyShtToTfTensor(const StringHostTensor& sht) {
53   llvm::SmallVector<tfrt::Index, 4> dims;
54   sht.shape().GetDimensions(&dims);
55 
56   tensorflow::Tensor tensor(
57       tensorflow::DT_STRING,
58       tensorflow::TensorShape(
59           llvm::SmallVector<int64_t, 4>(dims.begin(), dims.end())));
60 
61   auto len = tensor.NumElements();
62   auto from = sht.strings();
63   auto to = tensor.flat<tensorflow::tstring>();
64 
65   // TODO(tfrt-devs): Consider a more efficient way to pass string
66   // tensors between TFRT and TF.
67   for (int i = 0; i < len; ++i) {
68     to(i).assign(from[i].data(), from[i].size());
69   }
70   return tensor;
71 }
72 
73 }  // namespace tfd
74 }  // namespace tensorflow
75