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/convolution_thunk.h"
     17 
     18 #include <string>
     19 
     20 #include "tensorflow/compiler/xla/service/gpu/cudnn_convolution_runner.h"
     21 #include "tensorflow/compiler/xla/types.h"
     22 #include "tensorflow/compiler/xla/util.h"
     23 #include "tensorflow/core/lib/strings/strcat.h"
     24 #include "tensorflow/core/lib/strings/stringprintf.h"
     25 #include "tensorflow/core/platform/logging.h"
     26 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
     27 
     28 namespace se = ::perftools::gputools;
     29 
     30 namespace xla {
     31 namespace gpu {
     32 
     33 using se::dnn::AlgorithmDesc;
     34 using se::dnn::BatchDescriptor;
     35 using se::dnn::ConvolutionDescriptor;
     36 using se::dnn::DataLayout;
     37 using se::dnn::FilterDescriptor;
     38 using se::dnn::FilterLayout;
     39 
     40 ConvolutionThunk::ConvolutionThunk(
     41     CudnnConvKind convolution_kind, const BufferAllocation::Slice& input_buffer,
     42     const BufferAllocation::Slice& filter_buffer,
     43     const BufferAllocation::Slice& output_buffer,
     44     const BufferAllocation::Slice& tuple_result_buffer,
     45     const BufferAllocation::Slice& scratch_buffer, const Shape& input_shape,
     46     const Shape& filter_shape, const Shape& output_shape, const Window& window,
     47     const ConvolutionDimensionNumbers& dim_nums, int64 algorithm,
     48     bool tensor_ops_enabled, const HloInstruction* hlo)
     49     : Thunk(Kind::kConvolution, hlo),
     50       convolution_kind_(convolution_kind),
     51       input_buffer_(input_buffer),
     52       filter_buffer_(filter_buffer),
     53       output_buffer_(output_buffer),
     54       tuple_result_buffer_(tuple_result_buffer),
     55       scratch_buffer_(scratch_buffer),
     56       input_shape_(input_shape),
     57       filter_shape_(filter_shape),
     58       output_shape_(output_shape),
     59       window_(window),
     60       dim_nums_(dim_nums),
     61       algorithm_(algorithm),
     62       tensor_ops_enabled_(tensor_ops_enabled) {}
     63 
     64 Status ConvolutionThunk::ExecuteOnStream(
     65     const BufferAllocations& buffer_allocations, se::Stream* stream) {
     66   se::DeviceMemoryBase input_data =
     67       buffer_allocations.GetDeviceAddress(input_buffer_);
     68   se::DeviceMemoryBase filter_data =
     69       buffer_allocations.GetDeviceAddress(filter_buffer_);
     70   se::DeviceMemoryBase output_data =
     71       buffer_allocations.GetDeviceAddress(output_buffer_);
     72   se::DeviceMemoryBase scratch =
     73       buffer_allocations.GetDeviceAddress(scratch_buffer_);
     74 
     75   se::dnn::AlgorithmConfig algorithm_config(
     76       se::dnn::AlgorithmDesc(algorithm_, tensor_ops_enabled_));
     77 
     78   TF_RETURN_IF_ERROR(RunCudnnConvolution(
     79       convolution_kind_, input_shape_, filter_shape_, output_shape_, input_data,
     80       filter_data, output_data, scratch, window_, dim_nums_, algorithm_config,
     81       stream));
     82 
     83   // Figure out which of output/input/filter is the result produced by
     84   // this op, and write the result tuple.
     85   void* result_ptr = [&] {
     86     switch (convolution_kind_) {
     87       case CudnnConvKind::kForward:
     88         return output_data.opaque();
     89       case CudnnConvKind::kBackwardInput:
     90         return input_data.opaque();
     91       case CudnnConvKind::kBackwardFilter:
     92         return filter_data.opaque();
     93     }
     94   }();
     95   void* ptrs[] = {result_ptr, scratch.opaque()};
     96   se::DeviceMemory<void*> tuple_addr(
     97       buffer_allocations.GetDeviceAddress(tuple_result_buffer_));
     98   stream->ThenMemcpyH2D<void*>(ptrs, &tuple_addr);
     99 
    100   if (!stream->ok()) {
    101     return InternalError("ConvolutionThunk::ExecuteOnStream failed.");
    102   }
    103   return Status::OK();
    104 }
    105 
    106 }  // namespace gpu
    107 }  // namespace xla
    108