/* Copyright (c) 2016 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. */ #pragma once #include "paddle/fluid/framework/operator.h" #include "paddle/fluid/operators/elementwise/elementwise_op_function.h" #include "paddle/fluid/platform/complex.h" #include "paddle/phi/kernels/funcs/complex_functors.h" namespace paddle { namespace operators { // functors to use with ElementwiseComputeEx template struct RealAndImagToComplexFunctor { inline HOSTDEVICE platform::complex operator()(const T x, const T y) { return platform::complex(x, y); } }; template struct ImagAndRealToComplexFunctor { inline HOSTDEVICE platform::complex operator()(const T y, const T x) { return platform::complex(x, y); } }; template struct ComplexGradForRealFunctor { inline HOSTDEVICE T operator()(const T x, const T y, const platform::complex out, const platform::complex dout) { return dout.real; } }; template struct ComplexGradForImagFunctor { inline HOSTDEVICE T operator()(const T x, const T y, const platform::complex out, const platform::complex dout) { return dout.imag; } }; template class ComplexKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { const auto* x = ctx.Input("X"); const auto* y = ctx.Input("Y"); auto* z = ctx.Output("Out"); using C = platform::complex; z->mutable_data(ctx.GetPlace()); // NOTE(chenfeiyu): be careful of the caveats of calling elementwise-related // facility functions #if defined(__NVCC__) || defined(__HIPCC__) ElementwiseComputeEx, DeviceContext, T, C>( ctx, x, y, /*axis*/ -1, RealAndImagToComplexFunctor(), z); #else auto x_dims = x->dims(); auto y_dims = y->dims(); if (x_dims.size() >= y_dims.size()) { ElementwiseComputeEx, DeviceContext, T, C>( ctx, x, y, /*axis*/ -1, RealAndImagToComplexFunctor(), z); } else { ElementwiseComputeEx, DeviceContext, T, C>( ctx, x, y, /*axis*/ -1, ImagAndRealToComplexFunctor(), z); } #endif } }; template class ComplexGradKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { using Tensor = framework::Tensor; auto* x = ctx.Input("X"); auto* y = ctx.Input("Y"); auto* dout = ctx.Input(framework::GradVarName("Out")); auto* dx = ctx.Output(framework::GradVarName("X")); auto* dy = ctx.Output(framework::GradVarName("Y")); using C = platform::complex; // skip out in a hacky way auto* out = dout; ElemwiseGradCompute, ComplexGradForImagFunctor, C>(ctx, *x, *y, *out, *dout, /*axis*/ -1, dx, dy, ComplexGradForRealFunctor(), ComplexGradForImagFunctor()); } }; } // namespace operators } // namespace paddle