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/gpu_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/service/hlo_casting_utils.h"
25 #include "tensorflow/compiler/xla/types.h"
26 #include "tensorflow/compiler/xla/util.h"
27 #include "tensorflow/core/platform/logging.h"
28 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
29
30 namespace xla {
31 namespace gpu {
32
ConvolutionThunk(ThunkInfo thunk_info,GpuConvConfig config,std::vector<BufferAllocation::Slice> operand_slices,BufferAllocation::Slice result_slice,BufferAllocation::Slice scratch_slice)33 ConvolutionThunk::ConvolutionThunk(
34 ThunkInfo thunk_info, GpuConvConfig config,
35 std::vector<BufferAllocation::Slice> operand_slices,
36 BufferAllocation::Slice result_slice, BufferAllocation::Slice scratch_slice)
37 : Thunk(Kind::kConvolution, thunk_info),
38 operand_buffers_(std::move(operand_slices)),
39 result_buffer_(result_slice),
40 scratch_buffer_(scratch_slice),
41 config_(std::move(config)) {}
42
ExecuteOnStream(const ExecuteParams & params)43 Status ConvolutionThunk::ExecuteOnStream(const ExecuteParams& params) {
44 const auto& buffer_allocations = *params.buffer_allocations;
45
46 std::vector<se::DeviceMemoryBase> operand_se_buffers;
47 for (const auto& buffer : operand_buffers_) {
48 operand_se_buffers.push_back(buffer_allocations.GetDeviceAddress(buffer));
49 }
50
51 se::DeviceMemoryBase result_buffer =
52 buffer_allocations.GetDeviceAddress(result_buffer_);
53
54 se::DeviceMemoryBase scratch =
55 buffer_allocations.GetDeviceAddress(scratch_buffer_);
56
57 auto op_profiler =
58 params.profiler->MakeScopedInstructionProfiler(profile_index());
59 TF_RETURN_IF_ERROR(RunGpuConv(config_, absl::MakeSpan(operand_se_buffers),
60 result_buffer, scratch, params.stream));
61
62 // Note: Convolution has a tuple buffer as an output, but we don't need to
63 // populate it as no one should be reading from the tuple directly.
64 if (!params.stream->ok()) {
65 return InternalError("ConvolutionThunk::ExecuteOnStream failed.");
66 }
67 return Status::OK();
68 }
69
70 } // namespace gpu
71 } // namespace xla
72