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 "absl/strings/str_cat.h"
21 #include "tensorflow/compiler/xla/service/gpu/cudnn_conv_runner.h"
22 #include "tensorflow/compiler/xla/service/gpu/hlo_execution_profiler.h"
23 #include "tensorflow/compiler/xla/service/gpu/ir_emission_utils.h"
24 #include "tensorflow/compiler/xla/types.h"
25 #include "tensorflow/compiler/xla/util.h"
26 #include "tensorflow/core/platform/logging.h"
27 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
28
29 namespace xla {
30 namespace gpu {
31
ConvolutionThunk(const HloCustomCallInstruction * cudnn_call,std::vector<BufferAllocation::Slice> operand_slices,BufferAllocation::Slice result_slice,BufferAllocation::Slice scratch_slice,BufferAllocation::Slice tuple_result_slice)32 ConvolutionThunk::ConvolutionThunk(
33 const HloCustomCallInstruction* cudnn_call,
34 std::vector<BufferAllocation::Slice> operand_slices,
35 BufferAllocation::Slice result_slice, BufferAllocation::Slice scratch_slice,
36 BufferAllocation::Slice tuple_result_slice)
37 : Thunk(Kind::kConvolution, cudnn_call),
38 cudnn_call_(cudnn_call),
39 operand_buffers_(std::move(operand_slices)),
40 result_buffer_(result_slice),
41 scratch_buffer_(scratch_slice),
42 tuple_result_buffer_(tuple_result_slice) {}
43
ExecuteOnStream(const BufferAllocations & buffer_allocations,se::Stream * stream,HloExecutionProfiler * profiler)44 Status ConvolutionThunk::ExecuteOnStream(
45 const BufferAllocations& buffer_allocations, se::Stream* stream,
46 HloExecutionProfiler* profiler) {
47 std::vector<se::DeviceMemoryBase> operand_se_buffers;
48 for (const auto& buffer : operand_buffers_) {
49 operand_se_buffers.push_back(buffer_allocations.GetDeviceAddress(buffer));
50 }
51
52 se::DeviceMemoryBase result_buffer =
53 buffer_allocations.GetDeviceAddress(result_buffer_);
54
55 se::DeviceMemoryBase scratch =
56 buffer_allocations.GetDeviceAddress(scratch_buffer_);
57
58 auto op_profiler = profiler->MakeScopedInstructionProfiler(hlo_instruction());
59 TF_RETURN_IF_ERROR(RunCudnnConv(cudnn_call_,
60 absl::MakeSpan(operand_se_buffers),
61 result_buffer, scratch, stream));
62
63 void* ptrs[] = {result_buffer.opaque(), scratch.opaque()};
64 se::DeviceMemory<void*> tuple_addr(
65 buffer_allocations.GetDeviceAddress(tuple_result_buffer_));
66 stream->ThenMemcpyH2D<void*>(ptrs, &tuple_addr);
67
68 if (!stream->ok()) {
69 return InternalError("ConvolutionThunk::ExecuteOnStream failed.");
70 }
71 return Status::OK();
72 }
73
74 } // namespace gpu
75 } // namespace xla
76