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
18 #include "tensorflow/compiler/xla/service/buffer_assignment.h"
19 #include "tensorflow/compiler/xla/service/gpu/hlo_execution_profiler.h"
20 #include "tensorflow/compiler/xla/service/gpu/infeed_manager.h"
21 #include "tensorflow/compiler/xla/shape_util.h"
22 #include "tensorflow/compiler/xla/status_macros.h"
23 #include "tensorflow/compiler/xla/util.h"
24 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
25
26 namespace xla {
27 namespace gpu {
28
InfeedThunk(ThunkInfo thunk_info,std::vector<ShapedSlice> dest_slices)29 InfeedThunk::InfeedThunk(ThunkInfo thunk_info,
30 std::vector<ShapedSlice> dest_slices)
31 : Thunk(Kind::kInfeed, thunk_info), dest_slices_(std::move(dest_slices)) {}
32
ExecuteOnStream(const ExecuteParams & params)33 Status InfeedThunk::ExecuteOnStream(const ExecuteParams& params) {
34 auto& stream = *params.stream;
35 auto& buffer_allocations = *params.buffer_allocations;
36
37 VLOG(2) << "Infeeding to GPU";
38
39 auto op_profiler =
40 params.profiler->MakeScopedInstructionProfiler(profile_index());
41 ShapeTree<InfeedBuffer> source_buffers =
42 GetOrCreateInfeedManager()->BlockingGetNextDestination();
43
44 size_t index = 0;
45 for (auto& source : source_buffers.leaves()) {
46 // Assert that the shapes are compatible.
47 const ShapeIndex& shape_index = source.first;
48 InfeedBuffer& buffer = source.second;
49 const Shape& source_shape =
50 ShapeUtil::GetSubshape(source_buffers.shape(), shape_index);
51 TF_RET_CHECK(ShapeUtil::Equal(dest_slices_[index].shape, source_shape))
52 << "Mismatch between infeed source buffer shape "
53 << ShapeUtil::HumanStringWithLayout(source_shape)
54 << " and infeed dest buffer shape "
55 << ShapeUtil::HumanStringWithLayout(dest_slices_[index].shape);
56 se::DeviceMemoryBase dest_address =
57 buffer_allocations.GetDeviceAddress(dest_slices_[index++].slice);
58 stream.ThenMemcpy(&dest_address, *buffer.device_memory(), buffer.length());
59 }
60
61 // Make sure that all dest slices have been copied into.
62 CHECK_EQ(index, dest_slices_.size())
63 << "Infeed did not populate all destination buffers";
64
65 Status block_status = stream.BlockHostUntilDone();
66 if (!block_status.ok()) {
67 return InternalError("Failed to complete data transfer on stream %p: %s",
68 &stream, block_status.error_message());
69 }
70
71 VLOG(2) << "Infeeding to GPU complete";
72 return Status::OK();
73 }
74
75 } // namespace gpu
76 } // namespace xla
77