109 lines
3.9 KiB
C++
109 lines
3.9 KiB
C++
// Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
#ifdef PADDLE_WITH_XPU_FFT
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "paddle/phi/common/type_traits.h"
|
|
#include "paddle/phi/core/kernel_registry.h"
|
|
#include "paddle/phi/kernels/fft_kernel.h"
|
|
|
|
#include "paddle/common/ddim.h"
|
|
#include "paddle/phi/kernels/empty_kernel.h"
|
|
#include "paddle/phi/kernels/full_kernel.h"
|
|
#include "paddle/phi/kernels/funcs/fft.h"
|
|
#include "paddle/phi/kernels/funcs/fft_fill_conj_xpu.h"
|
|
|
|
namespace phi {
|
|
template <typename T, typename Context>
|
|
void FFTC2CKernel(const Context& dev_ctx,
|
|
const DenseTensor& x,
|
|
const std::vector<int64_t>& axes,
|
|
const std::string& normalization,
|
|
bool forward,
|
|
DenseTensor* out) {
|
|
dev_ctx.template Alloc<T>(out);
|
|
if (x.numel() == 0) {
|
|
Full<T, Context>(dev_ctx, out->dims(), 0, out);
|
|
return;
|
|
}
|
|
const auto norm_type = funcs::get_norm_from_string(normalization, forward);
|
|
funcs::FFTC2CFunctor<Context, T, T> fft_c2c_func;
|
|
fft_c2c_func(dev_ctx, x, out, axes, norm_type, forward);
|
|
}
|
|
|
|
template <typename T, typename Context>
|
|
void FFTC2RKernel(const Context& dev_ctx,
|
|
const DenseTensor& x,
|
|
const std::vector<int64_t>& axes,
|
|
const std::string& normalization,
|
|
bool forward,
|
|
int64_t last_dim_size UNUSED,
|
|
DenseTensor* out) {
|
|
using R = typename T::value_type; // get real type
|
|
dev_ctx.template Alloc<R>(out);
|
|
if (x.numel() == 0) {
|
|
Full<R, Context>(dev_ctx, out->dims(), 0, out);
|
|
return;
|
|
}
|
|
const auto norm_type = funcs::get_norm_from_string(normalization, forward);
|
|
funcs::FFTC2RFunctor<Context, T, R> fft_c2r_func;
|
|
fft_c2r_func(dev_ctx, x, out, axes, norm_type, forward);
|
|
}
|
|
|
|
template <typename T, typename Context>
|
|
void FFTR2CKernel(const Context& dev_ctx,
|
|
const DenseTensor& x,
|
|
const std::vector<int64_t>& axes,
|
|
const std::string& normalization,
|
|
bool forward,
|
|
bool onesided,
|
|
DenseTensor* out) {
|
|
using C = phi::dtype::complex<T>;
|
|
dev_ctx.template Alloc<C>(out);
|
|
if (x.numel() == 0) {
|
|
Full<C, Context>(dev_ctx, out->dims(), 0, out);
|
|
return;
|
|
}
|
|
auto norm_type = funcs::get_norm_from_string(normalization, forward);
|
|
funcs::FFTR2CFunctor<Context, T, C> fft_r2c_func;
|
|
|
|
if (onesided) {
|
|
fft_r2c_func(dev_ctx, x, out, axes, norm_type, forward);
|
|
} else {
|
|
DDim onesided_out_shape = x.dims();
|
|
const int64_t last_fft_axis = axes.back();
|
|
const int64_t onesided_last_axis_size =
|
|
out->dims().at(last_fft_axis) / 2 + 1;
|
|
onesided_out_shape[last_fft_axis] = onesided_last_axis_size;
|
|
DenseTensor onesided_out =
|
|
Empty<C, Context>(dev_ctx, vectorize(onesided_out_shape));
|
|
fft_r2c_func(dev_ctx, x, &onesided_out, axes, norm_type, forward);
|
|
funcs::FFTFillConj<Context, C>(dev_ctx, &onesided_out, out, axes);
|
|
}
|
|
}
|
|
} // namespace phi
|
|
|
|
PD_REGISTER_KERNEL(
|
|
fft_c2c, XPU, ALL_LAYOUT, phi::FFTC2CKernel, phi::complex64) {}
|
|
PD_REGISTER_KERNEL(
|
|
fft_c2r, XPU, ALL_LAYOUT, phi::FFTC2RKernel, phi::complex64) {
|
|
kernel->OutputAt(0).SetDataType(phi::dtype::ToReal(kernel_key.dtype()));
|
|
}
|
|
PD_REGISTER_KERNEL(fft_r2c, XPU, ALL_LAYOUT, phi::FFTR2CKernel, float) {
|
|
kernel->OutputAt(0).SetDataType(phi::dtype::ToComplex(kernel_key.dtype()));
|
|
}
|
|
#endif
|