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