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/xla/service/generic_transfer_manager.h"
17
18 #include <string>
19 #include <utility>
20 #include <vector>
21
22 #include "tensorflow/compiler/xla/layout_util.h"
23 #include "tensorflow/compiler/xla/literal.h"
24 #include "tensorflow/compiler/xla/service/interpreter/platform_id.h"
25 #include "tensorflow/compiler/xla/shape_util.h"
26 #include "tensorflow/compiler/xla/status_macros.h"
27 #include "tensorflow/compiler/xla/types.h"
28 #include "tensorflow/compiler/xla/util.h"
29 #include "tensorflow/core/lib/core/errors.h"
30 #include "tensorflow/core/platform/logging.h"
31 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
32
33 namespace xla {
34
GenericTransferManager(se::Platform::Id platform_id,size_t pointer_size)35 GenericTransferManager::GenericTransferManager(se::Platform::Id platform_id,
36 size_t pointer_size)
37 : platform_id_(platform_id), pointer_size_(pointer_size) {}
38
PlatformId() const39 se::Platform::Id GenericTransferManager::PlatformId() const {
40 return platform_id_;
41 }
42
WriteSingleTupleIndexTable(se::Stream * stream,absl::Span<const se::DeviceMemoryBase> elements,const Shape & shape,se::DeviceMemoryBase * region)43 Status GenericTransferManager::WriteSingleTupleIndexTable(
44 se::Stream* stream, absl::Span<const se::DeviceMemoryBase> elements,
45 const Shape& shape, se::DeviceMemoryBase* region) {
46 TF_RET_CHECK(elements.size() == ShapeUtil::TupleElementCount(shape));
47
48 std::vector<const void*> element_pointers;
49 for (const se::DeviceMemoryBase& element : elements) {
50 element_pointers.push_back(element.opaque());
51 }
52 TF_RETURN_IF_ERROR(TransferBufferToDevice(
53 stream, GetByteSizeRequirement(shape), element_pointers.data(), region));
54 // Ensure the buffer is transferred before we destroy element_pointers.
55 return stream->BlockHostUntilDone();
56 }
57
TransferLiteralFromDevice(se::Stream * stream,const ShapedBuffer & device_buffer,MutableBorrowingLiteral literal,std::function<void (Status)> done,const TransferMetadata *)58 void GenericTransferManager::TransferLiteralFromDevice(
59 se::Stream* stream, const ShapedBuffer& device_buffer,
60 MutableBorrowingLiteral literal, std::function<void(Status)> done,
61 const TransferMetadata* /*transfer_metadata*/) {
62 Status status = stream->BlockHostUntilDone();
63 if (!status.ok()) {
64 return done(status);
65 }
66
67 done(TransferLiteralFromDeviceInternal(stream->parent(), device_buffer,
68 literal));
69 }
70
TransferLiteralFromDeviceInternal(se::StreamExecutor * executor,const ShapedBuffer & device_buffer,MutableBorrowingLiteral literal)71 Status GenericTransferManager::TransferLiteralFromDeviceInternal(
72 se::StreamExecutor* executor, const ShapedBuffer& device_buffer,
73 MutableBorrowingLiteral literal) {
74 VLOG(2) << "transferring literal from device ordinal "
75 << executor->device_ordinal() << "; device buffer: " << device_buffer;
76 TF_RET_CHECK(executor->device_ordinal() == device_buffer.device_ordinal());
77
78 // The on-host and on-device shape should always be the same for the generic
79 // transfer manager.
80 TF_RET_CHECK(ShapeUtil::Equal(device_buffer.on_device_shape(),
81 device_buffer.on_host_shape()));
82
83 TF_RETURN_IF_ERROR(ShapeUtil::ForEachSubshapeWithStatus(
84 device_buffer.on_host_shape(),
85 [&](const Shape& subshape, const ShapeIndex& index) -> Status {
86 if (subshape.IsArray()) {
87 TF_RETURN_IF_ERROR(executor->SynchronousMemcpyD2H(
88 /*source=*/device_buffer.buffer(index),
89 /*size=*/GetByteSizeRequirement(subshape),
90 /*destination=*/
91 literal.untyped_data(index)));
92 }
93
94 return Status::OK();
95 }));
96 return Status::OK();
97 }
98
TransferLiteralToDeviceAsync(se::Stream * stream,const LiteralSlice & literal,const ShapedBuffer & device_buffer,const TransferMetadata *)99 Status GenericTransferManager::TransferLiteralToDeviceAsync(
100 se::Stream* stream, const LiteralSlice& literal,
101 const ShapedBuffer& device_buffer,
102 const TransferMetadata* /*transfer_metadata*/) {
103 const Shape& shape = literal.shape();
104 VLOG(2) << "transferring literal shape to device: "
105 << ShapeUtil::HumanString(shape)
106 << "; device buffer: " << device_buffer;
107
108 // The on-host and on-device shape should always be the same for the generic
109 // transfer manager.
110 TF_RET_CHECK(ShapeUtil::Equal(device_buffer.on_device_shape(),
111 device_buffer.on_host_shape()));
112
113 TF_RET_CHECK(
114 ShapeUtil::Compatible(literal.shape(), device_buffer.on_host_shape()));
115 TF_RET_CHECK(stream->parent()->device_ordinal() ==
116 device_buffer.device_ordinal());
117
118 TF_RETURN_IF_ERROR(WriteTupleIndexTables(stream, device_buffer));
119
120 return ShapeUtil::ForEachSubshapeWithStatus(
121 device_buffer.on_host_shape(),
122 [&](const Shape& device_subshape, const ShapeIndex& index) -> Status {
123 se::DeviceMemoryBase device_memory = device_buffer.buffer(index);
124 if (device_subshape.IsArray()) {
125 TF_RET_CHECK(GetByteSizeRequirement(device_subshape) ==
126 device_memory.size());
127 // Element is array-shaped: transfer array data to device buffer.
128 const auto subliteral = LiteralSlice(literal, index);
129 Literal relayed_out_literal;
130 const void* source;
131 if (LayoutUtil::Equal(device_subshape.layout(),
132 subliteral.shape().layout())) {
133 source = subliteral.untyped_data();
134 return TransferBufferToDevice(
135 stream,
136 /*size=*/GetByteSizeRequirement(device_subshape), source,
137 &device_memory);
138 } else {
139 // Relayout data before transferring.
140 relayed_out_literal = subliteral.Relayout(device_subshape.layout(),
141 /*shape_index=*/{});
142 source = relayed_out_literal.untyped_data();
143 TF_RETURN_IF_ERROR(TransferBufferToDevice(
144 stream,
145 /*size=*/GetByteSizeRequirement(device_subshape), source,
146 &device_memory));
147 return stream->BlockHostUntilDone();
148 }
149 }
150 return Status::OK();
151 });
152 }
153
TransferLiteralToInfeed(se::StreamExecutor * executor,const LiteralSlice & literal)154 Status GenericTransferManager::TransferLiteralToInfeed(
155 se::StreamExecutor* executor, const LiteralSlice& literal) {
156 return Unimplemented("Generic transfer to Infeed");
157 }
158
TransferLiteralFromOutfeed(se::StreamExecutor * executor,const Shape & literal_shape,MutableBorrowingLiteral literal)159 Status GenericTransferManager::TransferLiteralFromOutfeed(
160 se::StreamExecutor* executor, const Shape& literal_shape,
161 MutableBorrowingLiteral literal) {
162 return Unimplemented("Generic transfer from Outfeed");
163 }
164
ResetDevices(absl::Span<se::StreamExecutor * const>)165 Status GenericTransferManager::ResetDevices(
166 absl::Span<se::StreamExecutor* const>
167 /*executors*/) {
168 return Unimplemented(
169 "Device reset is not yet supported on this platform (b/30481585)");
170 }
171
GetByteSizeRequirement(const Shape & shape) const172 int64 GenericTransferManager::GetByteSizeRequirement(const Shape& shape) const {
173 return ShapeUtil::ByteSizeOf(shape, pointer_size_);
174 }
175
176 } // namespace xla
177