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/gpu/infeed_thunk.h"
17 #include "tensorflow/compiler/xla/service/gpu/hlo_execution_profiler.h"
18 #include "tensorflow/compiler/xla/service/gpu/infeed_manager.h"
19 #include "tensorflow/compiler/xla/util.h"
20 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
21
22 namespace xla {
23 namespace gpu {
24
InfeedThunk(const ShapeTree<BufferAllocation::Slice> & infeed_slices,const HloInstruction * hlo_instruction)25 InfeedThunk::InfeedThunk(
26 const ShapeTree<BufferAllocation::Slice>& infeed_slices,
27 const HloInstruction* hlo_instruction)
28 : Thunk(Kind::kInfeed, hlo_instruction), infeed_slices_(infeed_slices) {}
29
ExecuteOnStream(const BufferAllocations & buffer_allocations,se::Stream * stream,HloExecutionProfiler * profiler)30 Status InfeedThunk::ExecuteOnStream(const BufferAllocations& buffer_allocations,
31 se::Stream* stream,
32 HloExecutionProfiler* profiler) {
33 VLOG(2) << "Infeeding to GPU: " << hlo_instruction()->ToString();
34
35 auto op_profiler = profiler->MakeScopedInstructionProfiler(hlo_instruction());
36 ShapeTree<InfeedBuffer> infeed_buffers =
37 GetOrCreateInfeedManager()->BlockingGetNextDestination();
38
39 // infeed_slices_'s shape should be a tuple of shape (buffers, token).
40 const auto& infeed_shape = infeed_slices_.shape();
41 TF_RET_CHECK(infeed_shape.IsTuple())
42 << ShapeUtil::HumanStringWithLayout(infeed_shape);
43 TF_RET_CHECK(infeed_shape.tuple_shapes().size() == 2)
44 << ShapeUtil::HumanStringWithLayout(infeed_shape);
45 TF_RET_CHECK(infeed_shape.tuple_shapes(1).IsToken())
46 << ShapeUtil::HumanStringWithLayout(infeed_shape);
47 TF_RET_CHECK(
48 ShapeUtil::Equal(infeed_buffers.shape(), infeed_shape.tuple_shapes(0)))
49 << "Expected infeed of shape "
50 << ShapeUtil::HumanStringWithLayout(infeed_shape.tuple_shapes(0))
51 << " but was "
52 << ShapeUtil::HumanStringWithLayout(infeed_buffers.shape());
53
54 {
55 // The infeed buffer has an extra outer tuple with a token. Adjust the index
56 // accordingly.
57 ShapeIndex index = {0};
58 std::function<void(std::vector<void*>*)> copy_tuple_contents =
59 [&](std::vector<void*>* tuple_element_addresses) {
60 const Shape& shape = ShapeUtil::GetSubshape(infeed_buffers.shape(),
61 ShapeIndexView(index, 1));
62 // For the leaf buffers of the tuple copy the elements directly.
63 if (shape.IsArray()) {
64 const BufferAllocation::Slice& tuple_element_buffer =
65 infeed_slices_.element(index);
66 se::DeviceMemoryBase tuple_element_address =
67 buffer_allocations.GetDeviceAddress(tuple_element_buffer);
68
69 InfeedBuffer* buffer =
70 infeed_buffers.mutable_element(ShapeIndexView(index, 1));
71 stream->ThenMemcpy(&tuple_element_address,
72 *(buffer->device_memory()), buffer->length());
73 tuple_element_addresses->push_back(tuple_element_address.opaque());
74 return;
75 }
76
77 const int64 tuple_element_count = ShapeUtil::TupleElementCount(shape);
78 index.push_back(0);
79 std::vector<void*> inner_tuple_element_addresses;
80 for (int64 i = 0; i < tuple_element_count; ++i) {
81 index.back() = i;
82 copy_tuple_contents(&inner_tuple_element_addresses);
83 }
84 index.pop_back();
85
86 // Create a buffer of pointers for non-leaf buffers.
87 CHECK_EQ(tuple_element_count, inner_tuple_element_addresses.size());
88 auto host_size = inner_tuple_element_addresses.size() * sizeof(void*);
89 se::DeviceMemoryBase tuple_address =
90 buffer_allocations.GetDeviceAddress(
91 infeed_slices_.element(index));
92 stream->ThenMemcpy(&tuple_address,
93 inner_tuple_element_addresses.data(), host_size);
94 tuple_element_addresses->push_back(tuple_address.opaque());
95 };
96
97 std::vector<void*> tuple_element_addresses;
98 copy_tuple_contents(&tuple_element_addresses);
99 CHECK_EQ(1, tuple_element_addresses.size());
100 }
101
102 // Construct top-level tuple of infeed containing the data and the token. Use
103 // a nullptr for the token, it should never be dereferenced.
104 se::DeviceMemoryBase data_address =
105 buffer_allocations.GetDeviceAddress(infeed_slices_.element({0}));
106 void* infeed_addresses[] = {data_address.opaque(), nullptr};
107 se::DeviceMemoryBase top_level_address =
108 buffer_allocations.GetDeviceAddress(infeed_slices_.element({}));
109 stream->ThenMemcpy(&top_level_address, infeed_addresses, 2 * sizeof(void*));
110
111 Status block_status = stream->BlockHostUntilDone();
112 if (!block_status.ok()) {
113 return InternalError("Failed to complete data transfer on stream %p: %s",
114 stream, block_status.error_message());
115 }
116
117 VLOG(2) << "Infeeding to GPU complete";
118 return Status::OK();
119 }
120
121 } // namespace gpu
122 } // namespace xla
123