// Copyright (c) 2022 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. #include "paddle/phi/kernels/elementwise_kernel.h" #include "paddle/phi/kernels/legacy/elementwise_kernel.h" #include "paddle/phi/kernels/xpu/elementwise.h" #include "paddle/phi/backends/xpu/xpu_context.h" #include "paddle/phi/core/kernel_registry.h" #ifdef PADDLE_WITH_XPU_FFT #include "fft/cuComplex.h" #include "paddle/phi/kernels/complex_kernel.h" #include "paddle/phi/kernels/expand_kernel.h" #include "paddle/phi/kernels/funcs/common_infer_shape_functions.h" namespace xfft_internal::xpu { template // T supports float2, double2 int RemainderFunctor(const XPUStream stream, int N, const T* input_x, const T* input_y, T* output); } #endif namespace phi { template void FloorDivideKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& y, DenseTensor* out) { int axis = -1; FloorDivideRawKernel(dev_ctx, x, y, axis, out); } template void MaximumKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& y, DenseTensor* out) { int axis = -1; MaximumRawKernel(dev_ctx, x, y, axis, out); } template void MinimumKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& y, DenseTensor* out) { int axis = -1; MinimumRawKernel(dev_ctx, x, y, axis, out); } template void RemainderKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& y, DenseTensor* out) { using XPUType = typename XPUTypeTrait::Type; if (out && out->numel() == 0) { dev_ctx.template Alloc(out); return; } auto f = [](xpu::Context* xpu_ctx, const XPUType* x, const XPUType* y, XPUType* z, const std::vector& xshape, const std::vector& yshape) { return xpu::broadcast_mod(xpu_ctx, x, y, z, xshape, yshape); }; XPUElementwise(dev_ctx, x, y, -1, out, f); } template void ElementwisePowKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& y, DenseTensor* out) { int axis = -1; ElementwisePowRawKernel(dev_ctx, x, y, axis, out); } #ifdef PADDLE_WITH_XPU_FFT template <> void RemainderKernel(const XPUContext& dev_ctx, const DenseTensor& x, const DenseTensor& y, DenseTensor* out) { using T = phi::complex64; if (out && out->numel() == 0) { dev_ctx.template Alloc(out); return; } const auto& x_dims = x.dims(); const auto& y_dims = y.dims(); auto out_dims = funcs::BroadcastTwoDims(x_dims, y_dims); std::vector out_dims_vec = vectorize(out_dims); auto complex_expand = [](const XPUContext& dev_ctx, const DenseTensor& x, const std::vector& out_dims_vec, DenseTensor* out) { DenseTensor real_out, imag_out; real_out.Resize(out->dims()); imag_out.Resize(out->dims()); dev_ctx.template Alloc(&real_out); dev_ctx.template Alloc(&imag_out); const DenseTensor real = Real(dev_ctx, x); const DenseTensor imag = Imag(dev_ctx, x); ExpandKernel( dev_ctx, real, phi::IntArray(out_dims_vec), &real_out); ExpandKernel( dev_ctx, imag, phi::IntArray(out_dims_vec), &imag_out); phi::ComplexKernel(dev_ctx, real_out, imag_out, out); }; DenseTensor broadcasted_x, broadcasted_y; const T* x_data = nullptr; const T* y_data = nullptr; if (x_dims == out_dims) { x_data = x.data(); } else { broadcasted_x.Resize(out_dims); dev_ctx.template Alloc(&broadcasted_x); complex_expand(dev_ctx, x, out_dims_vec, &broadcasted_x); x_data = broadcasted_x.data(); } if (y_dims == out_dims) { y_data = y.data(); } else { broadcasted_y.Resize(out_dims); dev_ctx.template Alloc(&broadcasted_y); complex_expand(dev_ctx, y, out_dims_vec, &broadcasted_y); y_data = broadcasted_y.data(); } dev_ctx.template Alloc(out); int r = xfft_internal::xpu::RemainderFunctor( dev_ctx.x_context()->xpu_stream, out->numel(), reinterpret_cast(x_data), reinterpret_cast(y_data), reinterpret_cast(out->data())); PADDLE_ENFORCE_XPU_SUCCESS(r); } #endif } // namespace phi PD_REGISTER_KERNEL(floor_divide, XPU, ALL_LAYOUT, phi::FloorDivideKernel, float, phi::bfloat16, phi::float16, int32_t, int64_t) {} PD_REGISTER_KERNEL(maximum, XPU, ALL_LAYOUT, phi::MaximumKernel, float, phi::bfloat16, phi::float16, int32_t, int64_t) {} PD_REGISTER_KERNEL(minimum, XPU, ALL_LAYOUT, phi::MinimumKernel, float, phi::bfloat16, phi::float16, int32_t, int64_t) {} PD_REGISTER_KERNEL(remainder, XPU, ALL_LAYOUT, phi::RemainderKernel, float, phi::float16, #ifdef PADDLE_WITH_XPU_FFT phi::complex64, #endif int32_t, int64_t) { } PD_REGISTER_KERNEL(elementwise_pow, XPU, ALL_LAYOUT, phi::ElementwisePowKernel, float, phi::float16, phi::bfloat16) {}