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_manager.h" 17 18 #include "absl/memory/memory.h" 19 #include "tensorflow/compiler/xla/map_util.h" 20 #include "tensorflow/compiler/xla/shape_util.h" 21 #include "tensorflow/core/platform/logging.h" 22 23 #if GOOGLE_CUDA || TENSORFLOW_USE_ROCM 24 #include "tensorflow/compiler/xla/service/gpu/xla_executor_state.h" 25 #include "tensorflow/stream_executor/gpu/gpu_executor.h" 26 #endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM 27 28 namespace xla { 29 namespace gpu { 30 GetOrCreateOutfeedManager(se::StreamExecutor * executor)31OutfeedManager *GetOrCreateOutfeedManager(se::StreamExecutor *executor) { 32 #if GOOGLE_CUDA || TENSORFLOW_USE_ROCM 33 stream_executor::gpu::GpuExecutor *gpu_executor = 34 stream_executor::gpu::ExtractGpuExecutor(executor); 35 auto *xla_state = 36 gpu_executor->getOrCreateXLAState<GpuExecutorXLAState>(executor); 37 return xla_state->getOrCreateOutfeedManager(executor); 38 #else // GOOGLE_CUDA || TENSORFLOW_USE_ROCM 39 return nullptr; 40 #endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM 41 } 42 TransferLiteralFromOutfeed(se::StreamExecutor * executor,MutableBorrowingLiteral literal)43Status OutfeedManager::TransferLiteralFromOutfeed( 44 se::StreamExecutor* executor, MutableBorrowingLiteral literal) { 45 ShapeTree<std::unique_ptr<gpu::OutfeedBuffer>> outfeed_buffers( 46 &literal.shape()); 47 48 for (auto& leaf : outfeed_buffers.leaves()) { 49 const Shape& shape = ShapeUtil::GetSubshape(literal.shape(), leaf.first); 50 CHECK(shape.IsArray()) << ShapeUtil::HumanStringWithLayout(shape); 51 leaf.second = 52 absl::make_unique<gpu::OutfeedBuffer>(ShapeUtil::ByteSizeOf(shape)); 53 leaf.second->set_destination( 54 absl::make_unique<MutableBorrowingLiteral>(literal, leaf.first)); 55 } 56 57 // Give the tree of buffers to the outfeed manager. The device will fill it 58 // while we're waiting for it below. 59 gpu::OutfeedManager* outfeed_manager = 60 gpu::GetOrCreateOutfeedManager(executor); 61 outfeed_manager->EnqueueDestination(&outfeed_buffers); 62 63 // Now wait till all the buffers are written. 64 for (auto& leaf : outfeed_buffers.leaves()) { 65 const Shape& shape = ShapeUtil::GetSubshape(literal.shape(), leaf.first); 66 CHECK(shape.IsArray()) << ShapeUtil::HumanStringWithLayout(shape); 67 leaf.second->WaitUntilAvailable(); 68 } 69 70 return Status::OK(); 71 } 72 73 } // namespace gpu 74 } // namespace xla 75