1 /* Copyright 2018 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/gpu/outfeed_thunk.h"
17 #include "tensorflow/compiler/xla/literal.h"
18 #include "tensorflow/compiler/xla/service/gpu/hlo_execution_profiler.h"
19 #include "tensorflow/compiler/xla/service/gpu/outfeed_manager.h"
20 #include "tensorflow/compiler/xla/util.h"
21 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
22
23 namespace xla {
24 namespace gpu {
25
OutfeedThunk(ShapeTree<BufferAllocation::Slice> outfeed_slices,const HloInstruction * hlo_instruction)26 OutfeedThunk::OutfeedThunk(ShapeTree<BufferAllocation::Slice> outfeed_slices,
27 const HloInstruction* hlo_instruction)
28 : Thunk(Kind::kOutfeed, hlo_instruction),
29 outfeed_slices_(std::move(outfeed_slices)) {}
30
ExecuteOnStream(const BufferAllocations & buffer_allocations,se::Stream * stream,HloExecutionProfiler * profiler)31 Status OutfeedThunk::ExecuteOnStream(
32 const BufferAllocations& buffer_allocations, se::Stream* stream,
33 HloExecutionProfiler* profiler) {
34 VLOG(2) << "Outfeeding from GPU: " << hlo_instruction()->ToString();
35
36 auto op_profiler = profiler->MakeScopedInstructionProfiler(hlo_instruction());
37 OutfeedManager* outfeed_manager = GetOrCreateOutfeedManager();
38 ShapeTree<std::unique_ptr<OutfeedBuffer>>* outfeed_buffers =
39 outfeed_manager->BlockingGetNextDestination();
40
41 // Nothing to be done for empty tuples.
42 if (ShapeUtil::IsEmptyTuple(hlo_instruction()->operand(0)->shape())) {
43 return Status::OK();
44 }
45 CHECK(ShapeUtil::Compatible(hlo_instruction()->operand(0)->shape(),
46 outfeed_buffers->shape()));
47
48 TF_RETURN_IF_ERROR(outfeed_buffers->ForEachMutableElementWithStatus(
49 [&](const ShapeIndex& index, std::unique_ptr<OutfeedBuffer>* buffer) {
50 if (!*buffer) { // Tuple pointers.
51 return Status::OK();
52 }
53
54 BufferAllocation::Slice slice = outfeed_slices_.element(index);
55 se::DeviceMemoryBase data_address;
56 if (slice.allocation()) {
57 // If we have a static allocation, read it from there. This avoids
58 // synchronizing the host and device just to read a pointer.
59 data_address = buffer_allocations.GetDeviceAddress(slice);
60 } else {
61 // Otherwise we have to read the tuple pointer first.
62 CHECK(!index.empty());
63 // Copy the parent buffer to the host.
64 BufferAllocation::Slice tuple_slice =
65 outfeed_slices_.element(ShapeIndexView(index).ConsumeFront());
66 if (!tuple_slice.allocation()) {
67 return Unimplemented(
68 "Nested dynamic tuples are not supported on GPU");
69 }
70 se::DeviceMemoryBase tuple_address =
71 buffer_allocations.GetDeviceAddress(tuple_slice);
72 CHECK(tuple_slice.size() % sizeof(void*) == 0)
73 << "Tuple size must be a multiple of pointer size";
74 std::vector<void*> tuple_element_buffer_addresses(tuple_slice.size() /
75 sizeof(void*));
76 stream->ThenMemcpy(tuple_element_buffer_addresses.data(),
77 tuple_address, tuple_slice.size());
78 TF_RETURN_IF_ERROR(stream->BlockHostUntilDone());
79 // The data address is specified by the element of the tuple pointer
80 // buffer.
81 data_address =
82 se::DeviceMemoryBase(tuple_element_buffer_addresses[index.back()],
83 (*buffer)->length());
84 }
85
86 // TODO(b/111309141): Run this on a separate stream so it doesn't block
87 // the GPU from doing work during the transfer. This could be handled by
88 // making StreamAssignment do something intelligent with outfeed thunks.
89 stream
90 ->ThenMemcpy((*buffer)->destination()->untyped_data(), data_address,
91 (*buffer)->length())
92 .ThenDoHostCallback([buffer]() { (*buffer)->Done(); });
93 return Status::OK();
94 }));
95
96 Status block_status = stream->BlockHostUntilDone();
97 if (!block_status.ok()) {
98 return InternalError("Failed to complete data transfer on stream %p: %s",
99 stream, block_status.error_message());
100 }
101
102 VLOG(2) << "Outfeeding from GPU complete";
103 return Status::OK();
104 }
105
106 } // namespace gpu
107 } // namespace xla
108