xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/gpu/convolution_thunk.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 <memory>
19 #include <string>
20 
21 #include "absl/strings/str_cat.h"
22 #include "tensorflow/compiler/xla/service/gpu/gpu_conv_runner.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 
GetOrCreateRunner(const stream_executor::Stream * stream)43 MaybeFusedConvRunner& ConvolutionThunk::GetOrCreateRunner(
44     const stream_executor::Stream* stream) {
45   absl::MutexLock lock(&mu_);
46   auto it = runner_cache_.find(stream);
47   if (it == runner_cache_.end()) {
48     it = runner_cache_
49              .insert({stream, std::make_unique<MaybeFusedConvRunner>(config_)})
50              .first;
51   }
52   return *it->second;
53 }
54 
ExecuteOnStream(const ExecuteParams & params)55 Status ConvolutionThunk::ExecuteOnStream(const ExecuteParams& params) {
56   const auto& buffer_allocations = *params.buffer_allocations;
57 
58   std::vector<se::DeviceMemoryBase> operand_se_buffers;
59   for (const auto& buffer : operand_buffers_) {
60     operand_se_buffers.push_back(buffer_allocations.GetDeviceAddress(buffer));
61   }
62 
63   se::DeviceMemoryBase result_buffer =
64       buffer_allocations.GetDeviceAddress(result_buffer_);
65 
66   se::DeviceMemoryBase scratch =
67       buffer_allocations.GetDeviceAddress(scratch_buffer_);
68 
69   RunConvOptions opts;
70   opts.runner_cache = &GetOrCreateRunner(params.stream);
71 
72   TF_RETURN_IF_ERROR(RunGpuConv(config_, absl::MakeSpan(operand_se_buffers),
73                                 result_buffer, scratch, params.stream, opts));
74 
75   // Note: Convolution has a tuple buffer as an output, but we don't need to
76   // populate it as no one should be reading from the tuple directly.
77   if (!params.stream->ok()) {
78     return InternalError("ConvolutionThunk::ExecuteOnStream failed.");
79   }
80   return OkStatus();
81 }
82 
83 }  // namespace gpu
84 }  // namespace xla
85