xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/gpu/convolution_thunk.h (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 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_GPU_CONVOLUTION_THUNK_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_GPU_CONVOLUTION_THUNK_H_
18 
19 #include <optional>
20 
21 #include "absl/container/flat_hash_map.h"
22 #include "tensorflow/compiler/xla/service/buffer_assignment.h"
23 #include "tensorflow/compiler/xla/service/gpu/buffer_allocations.h"
24 #include "tensorflow/compiler/xla/service/gpu/gpu_conv_runner.h"
25 #include "tensorflow/compiler/xla/service/gpu/gpu_executable.h"
26 #include "tensorflow/compiler/xla/service/gpu/thunk.h"
27 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
28 #include "tensorflow/compiler/xla/service/hlo_instructions.h"
29 #include "tensorflow/compiler/xla/types.h"
30 #include "tensorflow/compiler/xla/xla_data.pb.h"
31 #include "tensorflow/core/lib/core/status.h"
32 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
33 
34 namespace xla {
35 namespace gpu {
36 
37 // This class stores everything that StreamExecutor needs to launch a DNN
38 // convolution. It is generated by IrEmitter.
39 //
40 // This is thread-compatible.
41 class ConvolutionThunk : public Thunk {
42  public:
43   // Constructs a thunk for launching a DNN convolution.
44   //
45   // operand_slices should be in the same order as cudnn_call->operands().
46   ConvolutionThunk(ThunkInfo thunk_info, GpuConvConfig config,
47                    std::vector<BufferAllocation::Slice> operand_slices,
48                    BufferAllocation::Slice result_slice,
49                    BufferAllocation::Slice scratch_slice);
50 
51   ConvolutionThunk(const ConvolutionThunk&) = delete;
52   ConvolutionThunk& operator=(const ConvolutionThunk&) = delete;
53 
54   Status ExecuteOnStream(const ExecuteParams& params) override;
55 
56  private:
57   std::vector<BufferAllocation::Slice> operand_buffers_;
58   BufferAllocation::Slice result_buffer_;
59   BufferAllocation::Slice scratch_buffer_;
60   MaybeFusedConvRunner& GetOrCreateRunner(
61       const stream_executor::Stream* stream);
62 
63   // Convolution config
64   const GpuConvConfig config_;
65   absl::Mutex mu_;
66   absl::flat_hash_map<const stream_executor::Stream*,
67                       std::unique_ptr<MaybeFusedConvRunner>>
68       runner_cache_ ABSL_GUARDED_BY(mu_);
69 };
70 
71 }  // namespace gpu
72 }  // namespace xla
73 
74 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_CONVOLUTION_THUNK_H_
75