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 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_GPU_CONVOLUTION_THUNK_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_GPU_CONVOLUTION_THUNK_H_ 18 19 #include "tensorflow/compiler/xla/service/buffer_assignment.h" 20 #include "tensorflow/compiler/xla/service/gpu/buffer_allocations.h" 21 #include "tensorflow/compiler/xla/service/gpu/cudnn_convolution_runner.h" 22 #include "tensorflow/compiler/xla/service/gpu/gpu_executable.h" 23 #include "tensorflow/compiler/xla/service/gpu/thunk.h" 24 #include "tensorflow/compiler/xla/service/hlo_instruction.h" 25 #include "tensorflow/compiler/xla/types.h" 26 #include "tensorflow/compiler/xla/xla_data.pb.h" 27 #include "tensorflow/core/lib/core/status.h" 28 #include "tensorflow/core/lib/gtl/optional.h" 29 #include "tensorflow/core/platform/stream_executor_no_cuda.h" 30 31 namespace xla { 32 namespace gpu { 33 34 // This class stores everything that StreamExecutor needs to launch a BNN 35 // convolution. It is generated by IrEmitter. 36 // 37 // This is thread-compatible. 38 class ConvolutionThunk : public Thunk { 39 public: 40 // Constructs a thunk for launching a DNN convolution. When run, it will 41 // write a tuple (result, scratch_memory) into `tuple_result_buffer`. 42 // 43 // `algorithm` is a cudnn algorithm number. `algorithm == -1` indicates that 44 // we should use the default (i.e. baseline) cudnn algorithm. 45 // 46 // Note that "output" here doesn't refer to the output from running this 47 // thunk, but rather to the "output" of a hypothetical forward convolution 48 // that corresponds to this input+filter+output triple. That is, the result 49 // generated by this thunk is "output" for forward convs, "input" for 50 // backward-input convs, and "filter" for backward-filter convs. 51 // 52 // Semantics of null hlo_instruction argument are as in Thunk. 53 ConvolutionThunk(CudnnConvKind convolution_kind, 54 const BufferAllocation::Slice& input_buffer, 55 const BufferAllocation::Slice& filter_buffer, 56 const BufferAllocation::Slice& output_buffer, 57 const BufferAllocation::Slice& tuple_result_buffer, 58 const BufferAllocation::Slice& scratch_buffer, 59 const Shape& input_shape, const Shape& filter_shape, 60 const Shape& output_shape, const Window& window, 61 const ConvolutionDimensionNumbers& dim_nums, int64 algorithm, 62 bool tensor_ops_enabled, const HloInstruction* hlo); 63 64 ConvolutionThunk(const ConvolutionThunk&) = delete; 65 ConvolutionThunk& operator=(const ConvolutionThunk&) = delete; 66 67 // Does the convolution for the thunk on "stream". 68 Status ExecuteOnStream(const BufferAllocations& buffer_allocations, 69 perftools::gputools::Stream* stream) override; 70 71 private: 72 class ScratchAllocator; 73 74 Status Convolve( 75 const perftools::gputools::dnn::BatchDescriptor& input_descriptor, 76 perftools::gputools::DeviceMemory<float> input_data, 77 const perftools::gputools::dnn::FilterDescriptor& filter_descriptor, 78 perftools::gputools::DeviceMemory<float> filter_data, 79 const perftools::gputools::dnn::BatchDescriptor& output_descriptor, 80 perftools::gputools::DeviceMemory<float> output_data, 81 const perftools::gputools::dnn::ConvolutionDescriptor& 82 convolution_descriptor, 83 const perftools::gputools::dnn::AlgorithmConfig& algorithm_config, 84 perftools::gputools::Stream* stream, ScratchAllocator* scratch_allocator, 85 perftools::gputools::dnn::ProfileResult* profile_result); 86 87 const CudnnConvKind convolution_kind_; 88 89 const BufferAllocation::Slice input_buffer_; 90 const BufferAllocation::Slice filter_buffer_; 91 const BufferAllocation::Slice output_buffer_; 92 const BufferAllocation::Slice tuple_result_buffer_; 93 const BufferAllocation::Slice scratch_buffer_; 94 95 const Shape input_shape_; 96 const Shape filter_shape_; 97 const Shape output_shape_; 98 99 const Window window_; 100 const ConvolutionDimensionNumbers dim_nums_; 101 int64 algorithm_; 102 bool tensor_ops_enabled_; 103 }; 104 105 } // namespace gpu 106 } // namespace xla 107 108 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_CONVOLUTION_THUNK_H_ 109