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