1 /* Copyright 2017 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 #include "tensorflow/compiler/tf2xla/literal_util.h" 17 18 #include "tensorflow/compiler/tf2xla/shape_util.h" 19 #include "tensorflow/compiler/tf2xla/type_util.h" 20 #include "tensorflow/compiler/xla/literal.h" 21 #include "tensorflow/core/common_runtime/dma_helper.h" 22 23 namespace tensorflow { 24 25 Status HostTensorToBorrowingLiteral(const Tensor& host_tensor, 26 xla::BorrowingLiteral* literal) { 27 xla::Shape xla_shape; 28 TF_RETURN_IF_ERROR(TensorShapeToXLAShape(host_tensor.dtype(), 29 host_tensor.shape(), &xla_shape)); 30 *literal = xla::BorrowingLiteral( 31 static_cast<const char*>(DMAHelper::base(&host_tensor)), xla_shape); 32 return Status::OK(); 33 } 34 35 xla::StatusOr<xla::Literal> HostTensorToLiteral(const Tensor& host_tensor) { 36 xla::BorrowingLiteral literal; 37 TF_RETURN_IF_ERROR(HostTensorToBorrowingLiteral(host_tensor, &literal)); 38 return literal.Clone(); 39 } 40 41 Status HostTensorToMutableBorrowingLiteral( 42 Tensor* host_tensor, xla::MutableBorrowingLiteral* literal) { 43 xla::Shape xla_shape; 44 TF_RETURN_IF_ERROR(TensorShapeToXLAShape(host_tensor->dtype(), 45 host_tensor->shape(), &xla_shape)); 46 return HostTensorToMutableBorrowingLiteral(xla_shape, host_tensor, literal); 47 } 48 49 Status HostTensorToMutableBorrowingLiteral( 50 const xla::Shape& xla_shape, Tensor* host_tensor, 51 xla::MutableBorrowingLiteral* literal) { 52 *literal = xla::MutableBorrowingLiteral( 53 static_cast<const char*>(DMAHelper::base(host_tensor)), xla_shape); 54 55 return Status::OK(); 56 } 57 58 Status HostTensorsToBorrowingLiteralTuple(absl::Span<const Tensor> host_tensors, 59 xla::BorrowingLiteral* literal) { 60 std::vector<const char*> buf_ptrs; 61 buf_ptrs.reserve(host_tensors.size()); 62 std::vector<xla::Shape> tensor_shapes(host_tensors.size()); 63 64 for (int i = 0; i < host_tensors.size(); i++) { 65 // Validate runtime shapes and fail if it doesn't match the contract. 66 const Tensor* tensor = &host_tensors[i]; 67 buf_ptrs.emplace_back(static_cast<const char*>(DMAHelper::base(tensor))); 68 TF_RETURN_IF_ERROR(TensorShapeToXLAShape(tensor->dtype(), tensor->shape(), 69 &tensor_shapes[i])); 70 } 71 72 *literal = xla::BorrowingLiteral( 73 buf_ptrs, xla::ShapeUtil::MakeTupleShape(tensor_shapes)); 74 75 return Status::OK(); 76 } 77 78 Status CopyLiteralToHostTensor(const xla::LiteralSlice& literal, 79 Tensor* host_tensor) { 80 TF_RET_CHECK(literal.shape().IsArray() && 81 xla::ShapeUtil::ElementsIn(literal.shape()) == 82 host_tensor->NumElements()); 83 xla::PrimitiveType primitive_type; 84 TF_RETURN_IF_ERROR( 85 DataTypeToPrimitiveType(host_tensor->dtype(), &primitive_type)); 86 if (literal.shape().element_type() != primitive_type) { 87 return errors::InvalidArgument( 88 "Cannot convert literal of type ", 89 xla::PrimitiveType_Name(literal.shape().element_type()), 90 " to tensor of type ", DataTypeString(host_tensor->dtype())); 91 } 92 size_t total_bytes = host_tensor->TotalBytes(); 93 if (total_bytes > 0) { 94 const void* src_ptr = literal.untyped_data(); 95 void* dst_ptr = DMAHelper::base(host_tensor); 96 memcpy(dst_ptr, src_ptr, total_bytes); 97 } 98 return Status::OK(); 99 } 100 101 Status LiteralToHostTensor(const xla::LiteralSlice& literal, 102 DataType target_type, Tensor* host_tensor) { 103 TensorShape shape; 104 TF_RETURN_IF_ERROR(XLAShapeToTensorShape(literal.shape(), &shape)); 105 *host_tensor = Tensor(target_type, shape); 106 return CopyLiteralToHostTensor(literal, host_tensor); 107 } 108 109 } // namespace tensorflow 110