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_manager.h"
17
18 #include <memory>
19
20 #include "tensorflow/compiler/xla/shape_util.h"
21
22 #if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
23 #include "tensorflow/compiler/xla/service/gpu/xla_executor_state.h"
24 #include "tensorflow/stream_executor/gpu/gpu_executor.h"
25 #endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
26
27 namespace xla {
28 namespace gpu {
29
30 constexpr int kMaxInfeedsInFlight = 8;
31
InfeedManager(se::StreamExecutor * executor)32 InfeedManager::InfeedManager(se::StreamExecutor* executor)
33 : BlockingXfeedQueue(/*max_pending_xfeeds=*/kMaxInfeedsInFlight),
34 stream_(std::make_unique<se::Stream>(executor)) {
35 stream_->Init();
36 }
37
CopyBufferToDevice(se::Stream * stream,int64_t size,const void * source)38 static StatusOr<se::ScopedDeviceMemory<uint8_t>> CopyBufferToDevice(
39 se::Stream* stream, int64_t size, const void* source) {
40 if (size > std::numeric_limits<int32_t>::max()) {
41 return InvalidArgument("GPU infeed of %d bytes exceeds maximum of %d bytes",
42 size, std::numeric_limits<int32_t>::max());
43 }
44
45 if (size == 0) {
46 return InvalidArgument("Infeed shape needs 0 bytes");
47 }
48
49 se::StreamExecutor* executor = stream->parent();
50 se::ScopedDeviceMemory<uint8_t> buffer(
51 executor, executor->AllocateArray<uint8_t>(size));
52 stream->ThenMemcpy(buffer.ptr(), source, size);
53
54 return std::move(buffer);
55 }
56
TransferLiteralToInfeed(se::StreamExecutor * executor,const LiteralSlice & literal)57 Status InfeedManager::TransferLiteralToInfeed(se::StreamExecutor* executor,
58 const LiteralSlice& literal) {
59 const Shape& literal_shape = literal.shape();
60 VLOG(2) << "Transferring literal to infeed with shape: "
61 << ShapeUtil::HumanString(literal_shape);
62
63 BlockUntilEnqueueSlotAvailable();
64
65 // For a tuple, we transfer each of its elements to the device and enqueue the
66 // resulting destination device addresses with the infeed manager.
67 ShapeTree<se::ScopedDeviceMemory<uint8_t>> buffer_tree(literal_shape);
68 for (auto& leaf : buffer_tree.leaves()) {
69 const Shape& sub_shape = ShapeUtil::GetSubshape(literal_shape, leaf.first);
70 CHECK(sub_shape.IsArray()) << ShapeUtil::HumanStringWithLayout(sub_shape);
71 TF_ASSIGN_OR_RETURN(
72 leaf.second,
73 CopyBufferToDevice(stream(), ShapeUtil::ByteSizeOf(sub_shape),
74 literal.untyped_data(leaf.first)));
75 }
76
77 // TODO(b/30467474): Since this stream is shared across different infeed
78 // requests, blocking on the stream might be heavy-handed. Figure out if
79 // finer-grained acknowledgement is possible.
80 Status block_status = stream()->BlockHostUntilDone();
81 if (!block_status.ok()) {
82 return InternalError("Failed to complete data transfer on stream %p: %s",
83 stream(), block_status.error_message());
84 }
85
86 EnqueueDestination(std::move(buffer_tree));
87 return OkStatus();
88 }
89
GetOrCreateInfeedManager(se::StreamExecutor * executor)90 InfeedManager* GetOrCreateInfeedManager(se::StreamExecutor* executor) {
91 #if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
92 stream_executor::gpu::GpuExecutor* gpu_executor =
93 stream_executor::gpu::ExtractGpuExecutor(executor);
94 auto* xla_state =
95 gpu_executor->getOrCreateXLAState<GpuExecutorXLAState>(executor);
96 return xla_state->getOrCreateInfeedManager(executor);
97 #else // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
98 return nullptr;
99 #endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
100 }
101
102 } // namespace gpu
103 } // namespace xla
104