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 // XLA-specific Ops for FFT. 17 18 #include "tensorflow/compiler/tf2xla/xla_helpers.h" 19 #include "tensorflow/compiler/tf2xla/xla_op_kernel.h" 20 #include "tensorflow/compiler/tf2xla/xla_op_registry.h" 21 #include "tensorflow/compiler/xla/literal_util.h" 22 #include "tensorflow/core/framework/numeric_op.h" 23 #include "tensorflow/core/framework/op_kernel.h" 24 #include "tensorflow/core/framework/tensor.h" 25 #include "tensorflow/core/framework/tensor_shape.h" 26 #include "tensorflow/core/framework/tensor_slice.h" 27 #include "tensorflow/core/kernels/bounds_check.h" 28 #include "tensorflow/core/kernels/conv_grad_ops.h" 29 #include "tensorflow/core/kernels/ops_util.h" 30 #include "tensorflow/core/util/padding.h" 31 #include "tensorflow/core/util/tensor_format.h" 32 33 namespace tensorflow { 34 35 namespace { 36 37 using xla::FftType; 38 39 class GenericFftOp : public XlaOpKernel { 40 public: 41 explicit GenericFftOp(OpKernelConstruction* ctx, FftType fft_type, 42 int fft_rank) 43 : XlaOpKernel(ctx), fft_type_(fft_type), fft_rank_(fft_rank) {} 44 45 void Compile(XlaOpKernelContext* ctx) override { 46 const TensorShape input_shape = ctx->InputShape(0); 47 OP_REQUIRES( 48 ctx, TensorShapeUtils::IsVectorOrHigher(input_shape), 49 errors::InvalidArgument("input must be at least 1 dimensional")); 50 51 std::vector<int64> fft_length; 52 if (fft_type_ == FftType::RFFT || fft_type_ == FftType::IRFFT) { 53 OP_REQUIRES_OK(ctx, ctx->ConstantInputAsIntVector(1, &fft_length)); 54 OP_REQUIRES(ctx, fft_length.size() == fft_rank_, 55 errors::InvalidArgument("fft_length must be length ", 56 fft_rank_, " vector")); 57 } else { 58 // Innermost axis provides the FFT length. 59 for (int i = 0; i < fft_rank_; i++) { 60 fft_length.push_back( 61 input_shape.dim_size(input_shape.dims() - fft_rank_ + i)); 62 } 63 } 64 65 xla::ComputationBuilder* b = ctx->builder(); 66 xla::ComputationDataHandle fft = 67 b->Fft(ctx->Input(0), fft_type_, fft_length); 68 ctx->SetOutput(0, fft); 69 } 70 71 protected: 72 const FftType fft_type_; 73 const int fft_rank_; 74 75 private: 76 TF_DISALLOW_COPY_AND_ASSIGN(GenericFftOp); 77 }; 78 79 template <int FFTRank> 80 class FFTOp : public GenericFftOp { 81 public: 82 explicit FFTOp(OpKernelConstruction* ctx) 83 : GenericFftOp(ctx, /*fft_type=*/FftType::FFT, /*fft_rank=*/FFTRank) {} 84 }; 85 REGISTER_XLA_OP(Name("FFT"), FFTOp<1>); 86 REGISTER_XLA_OP(Name("FFT2D"), FFTOp<2>); 87 REGISTER_XLA_OP(Name("FFT3D"), FFTOp<3>); 88 89 template <int FFTRank> 90 class IFFTOp : public GenericFftOp { 91 public: 92 explicit IFFTOp(OpKernelConstruction* ctx) 93 : GenericFftOp(ctx, /*fft_type=*/FftType::IFFT, /*fft_rank=*/FFTRank) {} 94 }; 95 REGISTER_XLA_OP(Name("IFFT"), IFFTOp<1>); 96 REGISTER_XLA_OP(Name("IFFT2D"), IFFTOp<2>); 97 REGISTER_XLA_OP(Name("IFFT3D"), IFFTOp<3>); 98 99 template <int FFTRank> 100 class RFFTOp : public GenericFftOp { 101 public: 102 explicit RFFTOp(OpKernelConstruction* ctx) 103 : GenericFftOp(ctx, /*fft_type=*/FftType::RFFT, /*fft_rank=*/FFTRank) {} 104 }; 105 REGISTER_XLA_OP(Name("RFFT").CompileTimeConstInput("fft_length"), RFFTOp<1>); 106 REGISTER_XLA_OP(Name("RFFT2D").CompileTimeConstInput("fft_length"), RFFTOp<2>); 107 REGISTER_XLA_OP(Name("RFFT3D").CompileTimeConstInput("fft_length"), RFFTOp<3>); 108 109 template <int FFTRank> 110 class IRFFTOp : public GenericFftOp { 111 public: 112 explicit IRFFTOp(OpKernelConstruction* ctx) 113 : GenericFftOp(ctx, /*fft_type=*/FftType::IRFFT, /*fft_rank=*/FFTRank) {} 114 }; 115 REGISTER_XLA_OP(Name("IRFFT").CompileTimeConstInput("fft_length"), IRFFTOp<1>); 116 REGISTER_XLA_OP(Name("IRFFT2D").CompileTimeConstInput("fft_length"), 117 IRFFTOp<2>); 118 REGISTER_XLA_OP(Name("IRFFT3D").CompileTimeConstInput("fft_length"), 119 IRFFTOp<3>); 120 121 } // namespace 122 } // namespace tensorflow 123