xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/gpu/fft_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_FFT_THUNK_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_GPU_FFT_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/thunk.h"
25 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
26 #include "tensorflow/compiler/xla/types.h"
27 #include "tensorflow/compiler/xla/xla_data.pb.h"
28 #include "tensorflow/core/lib/core/status.h"
29 #include "tensorflow/core/platform/stream_executor_no_cuda.h"
30 
31 namespace xla {
32 namespace gpu {
33 
34 struct FftPlan {
35   // CuFFT thread-safety requires that separate host threads not share plans;
36   // protect each plan with a mutex.
37   absl::Mutex mu;
38   std::unique_ptr<se::fft::Plan> plan ABSL_GUARDED_BY(mu);
39   float scale_factor ABSL_GUARDED_BY(mu);
40 };
41 
42 class FftPlanCache {
43  public:
44   // Returnes Fft plan cached for the given device ordinal or creates a new one.
GetOrCreate(int device_ordinal)45   FftPlan* GetOrCreate(int device_ordinal) {
46     absl::MutexLock lock(&mu_);
47     std::unique_ptr<FftPlan>& plan = fft_plans_[device_ordinal];
48     if (!plan) plan = std::make_unique<FftPlan>();
49     return plan.get();
50   }
51 
52  private:
53   absl::Mutex mu_;
54   absl::flat_hash_map<int, std::unique_ptr<FftPlan>> fft_plans_
55       ABSL_GUARDED_BY(mu_);
56 };
57 
58 // This class stores everything that StreamExecutor needs to launch an FFT.
59 // It is generated by IrEmitter.
60 //
61 // This is thread-compatible.
62 class FftThunk : public Thunk {
63  public:
64   // Constructs a thunk for launching an FFT on a stream.
65   // Semantics of null hlo_instruction argument are as in Thunk.
66   FftThunk(ThunkInfo thunk_info, FftType fft_type,
67            absl::Span<const int64_t> fft_length,
68            const BufferAllocation::Slice& input_buffer,
69            const BufferAllocation::Slice& output_buffer,
70            const Shape& input_shape, const Shape& output_shape);
71 
72   FftThunk(const FftThunk&) = delete;             // Cannot share fft_plan_
73   FftThunk& operator=(const FftThunk&) = delete;  // Cannot share fft_plan_
74 
75   // Does the FFT for the thunk on "stream".
76   Status ExecuteOnStream(const ExecuteParams& params) override;
77 
78  private:
79   const se::fft::Type fft_type_;
80   const std::vector<int64_t> fft_length_;
81 
82   FftPlanCache fft_plan_cache_;
83 
84   const BufferAllocation::Slice input_buffer_;
85   const BufferAllocation::Slice output_buffer_;
86 
87   const Shape input_shape_;
88   const Shape output_shape_;
89 };
90 
91 Status RunFft(se::DeviceMemoryBase input, const Shape& input_shape,
92               se::DeviceMemoryBase output, const Shape& output_shape,
93               se::fft::Type fft_type, absl::Span<const int64_t> fft_length,
94               int device_ordinal, FftPlanCache* fft_plan_cache,
95               se::Stream* stream, se::DeviceMemoryAllocator* memory_allocator);
96 
97 }  // namespace gpu
98 }  // namespace xla
99 
100 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_FFT_THUNK_H_
101