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 "absl/container/flat_hash_map.h" 20 #include "absl/types/optional.h" 21 #include "tensorflow/compiler/xla/service/buffer_assignment.h" 22 #include "tensorflow/compiler/xla/service/gpu/buffer_allocations.h" 23 #include "tensorflow/compiler/xla/service/gpu/gpu_executable.h" 24 #include "tensorflow/compiler/xla/service/gpu/hlo_execution_profiler.h" 25 #include "tensorflow/compiler/xla/service/gpu/thunk.h" 26 #include "tensorflow/compiler/xla/service/hlo_instruction.h" 27 #include "tensorflow/compiler/xla/types.h" 28 #include "tensorflow/compiler/xla/xla_data.pb.h" 29 #include "tensorflow/core/lib/core/status.h" 30 #include "tensorflow/core/platform/stream_executor_no_cuda.h" 31 32 namespace xla { 33 namespace gpu { 34 35 // A one-time scratch allocator for FFT. The scratch buffers allocated are 36 // released on destruction. 37 // 38 // Not thread-safe in that AllocateBytes, destructor are not locked. 39 class FftScratchAllocator : public se::ScratchAllocator { 40 public: 41 FftScratchAllocator(int device_ordinal, 42 se::DeviceMemoryAllocator* memory_allocator); 43 44 int64 GetMemoryLimitInBytes() override; 45 TotalAllocatedBytes()46 int64 TotalAllocatedBytes() { return total_allocated_bytes_; } 47 48 se::port::StatusOr<se::DeviceMemory<uint8>> AllocateBytes( 49 int64 byte_size) override; 50 51 private: 52 const int device_ordinal_; 53 se::DeviceMemoryAllocator* memory_allocator_; 54 std::vector<se::OwningDeviceMemory> allocated_buffers_; 55 int64 total_allocated_bytes_ = 0; 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> 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> fft_length_; 81 82 float scale_factor_; 83 84 // One plan per device ordinal. 85 absl::Mutex mu_; 86 struct FftPlan { 87 absl::Mutex mu; 88 std::unique_ptr<se::fft::Plan> plan; 89 }; 90 absl::flat_hash_map<int, std::unique_ptr<FftPlan>> fft_plans_ 91 ABSL_GUARDED_BY(mu_); 92 93 const BufferAllocation::Slice input_buffer_; 94 const BufferAllocation::Slice output_buffer_; 95 96 const Shape input_shape_; 97 const Shape output_shape_; 98 }; 99 100 } // namespace gpu 101 } // namespace xla 102 103 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_FFT_THUNK_H_ 104