Home | History | Annotate | Download | only in gpu
      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 #include "tensorflow/compiler/xla/service/gpu/infeed_thunk.h"
     18 #include "tensorflow/compiler/xla/util.h"
     19 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
     20 
     21 namespace xla {
     22 namespace gpu {
     23 
     24 InfeedThunk::InfeedThunk(
     25     tensorflow::gtl::ArraySlice<BufferAllocation::Slice> tuple_element_buffers,
     26     const BufferAllocation::Slice& destination_buffer,
     27     const HloInstruction* hlo_instruction)
     28     : Thunk(Kind::kInfeed, hlo_instruction),
     29       tuple_element_buffers_(tuple_element_buffers.begin(),
     30                              tuple_element_buffers.end()),
     31       destination_buffer_(destination_buffer) {}
     32 
     33 Status InfeedThunk::ExecuteOnStream(const BufferAllocations& buffer_allocations,
     34                                     perftools::gputools::Stream* stream) {
     35   VLOG(2) << "Infeeding to GPU ";
     36 
     37   perftools::gputools::DeviceMemoryBase destination_address =
     38       buffer_allocations.GetDeviceAddress(destination_buffer_);
     39 
     40   InfeedManager* infeed_manager = GetOrCreateInfeedManager();
     41   std::vector<InfeedBuffer*> infeed_buffers;
     42   if (ShapeUtil::IsTuple(hlo_instruction()->shape())) {
     43     CHECK(!ShapeUtil::IsNestedTuple(hlo_instruction()->shape()));
     44     // Transfer the tuple elements first.
     45     std::vector<void*> tuple_element_addresses;
     46     for (BufferAllocation::Slice tuple_element_buffer :
     47          tuple_element_buffers_) {
     48       perftools::gputools::DeviceMemoryBase tuple_element_address =
     49           buffer_allocations.GetDeviceAddress(tuple_element_buffer);
     50 
     51       InfeedBuffer* buffer = infeed_manager->BlockingDequeueBuffer();
     52       infeed_buffers.push_back(buffer);
     53       stream->ThenMemcpy(&tuple_element_address, *(buffer->device_memory()),
     54                          buffer->length());
     55       tuple_element_addresses.push_back(tuple_element_address.opaque());
     56     }
     57     // Transfer the tuple outer buffer.
     58     auto host_size = tuple_element_addresses.size() * sizeof(void*);
     59     stream->ThenMemcpy(&destination_address, tuple_element_addresses.data(),
     60                        host_size);
     61   } else {
     62     InfeedBuffer* buffer = infeed_manager->BlockingDequeueBuffer();
     63     infeed_buffers.push_back(buffer);
     64     stream->ThenMemcpy(&destination_address, *(buffer->device_memory()),
     65                        buffer->length());
     66   }
     67 
     68   Status block_status = stream->BlockHostUntilDone();
     69   if (!block_status.ok()) {
     70     return InternalError("Failed to complete data transfer on stream %p: %s",
     71                          stream, block_status.error_message().c_str());
     72   }
     73 
     74   infeed_manager->ReleaseBuffers(infeed_buffers);
     75 
     76   VLOG(2) << "Infeeding to GPU complete";
     77   return Status::OK();
     78 }
     79 
     80 }  // namespace gpu
     81 }  // namespace xla
     82