xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/gpu/kernel_thunk.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 #include "absl/container/flat_hash_map.h"
2 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
3 
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7 
8     http://www.apache.org/licenses/LICENSE-2.0
9 
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 ==============================================================================*/
16 
17 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_GPU_KERNEL_THUNK_H_
18 #define TENSORFLOW_COMPILER_XLA_SERVICE_GPU_KERNEL_THUNK_H_
19 
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include "absl/types/span.h"
25 #include "tensorflow/compiler/xla/service/buffer_assignment.h"
26 #include "tensorflow/compiler/xla/service/gpu/buffer_allocations.h"
27 #include "tensorflow/compiler/xla/service/gpu/launch_dimensions.h"
28 #include "tensorflow/compiler/xla/service/gpu/thunk.h"
29 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
30 #include "tensorflow/compiler/xla/types.h"
31 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
32 
33 namespace xla {
34 namespace gpu {
35 
36 class GpuExecutable;
37 
38 // This class stores everything that StreamExecutor needs for launching a
39 // kernel. It implements the ExecuteOnStream interface for GpuExecutable to
40 // invoke the corresponding kernel.
41 //
42 // This is thread-compatible.
43 class KernelThunk : public Thunk {
44  public:
45   // Constructs a thunk for the given kernel.
46   //
47   // `hlo_instruction` is as in Thunk. Other arguments are as the class members.
48   KernelThunk(ThunkInfo thunk_info,
49               absl::Span<const BufferAllocation* const> args,
50               const std::string& kernel_name,
51               const LaunchDimensions& launch_dimensions);
52   KernelThunk(const KernelThunk&) = delete;
53   KernelThunk& operator=(const KernelThunk&) = delete;
54   ~KernelThunk() override = default;
55 
56   std::string ToStringExtra(int indent) const override;
57 
58   Status Initialize(const GpuExecutable& executable,
59                     se::StreamExecutor* executor) override;
60   Status ExecuteOnStream(const ExecuteParams& params) override;
61 
arguments()62   const std::vector<const BufferAllocation*>& arguments() const {
63     return args_;
64   }
kernel_name()65   const std::string& kernel_name() const { return kernel_name_; }
launch_dimensions()66   const LaunchDimensions& launch_dimensions() const {
67     return launch_dimensions_;
68   }
69 
70  private:
71   // Buffers passed to the kernel as arguments.
72   const std::vector<const BufferAllocation*> args_;
73 
74   // Entry kernel name for the computation.
75   const std::string kernel_name_;
76 
77   // The thread and block dimension used to launch the kernel.
78   const LaunchDimensions launch_dimensions_;
79 
80   mutable absl::Mutex mutex_;
81 
82   // Loaded kernels for each `StreamExecutor`.  Requires pointer stability of
83   // values.
84   absl::flat_hash_map<se::StreamExecutor*, std::unique_ptr<se::KernelBase>>
85       kernel_cache_ ABSL_GUARDED_BY(mutex_);
86 };
87 
88 }  // namespace gpu
89 }  // namespace xla
90 
91 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_KERNEL_THUNK_H_
92