/* 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 #include #include "paddle/fluid/operators/elementwise/elementwise_mul_op.h" #include "paddle/fluid/operators/elementwise/elementwise_op.h" #include "paddle/fluid/operators/elementwise/elementwise_op_function.h" #include "paddle/fluid/operators/elementwise/elementwise_sub_op.h" #include "paddle/fluid/operators/math/blas.h" #include "paddle/fluid/operators/reduce_ops/reduce_op.h" namespace paddle { namespace operators { template void default_elementwise_div(const framework::ExecutionContext& ctx, const framework::Tensor* x, const framework::Tensor* y, framework::Tensor* z) { int axis = ctx.Attr("axis"); auto x_dims = x->dims(); auto y_dims = y->dims(); if (x_dims.size() >= y_dims.size()) { ElementwiseComputeEx, DeviceContext, T>(ctx, x, y, axis, DivFunctor(), z); } else { ElementwiseComputeEx, DeviceContext, T>( ctx, x, y, axis, InverseDivFunctor(), z); } } template struct SameDimsElemwiseDiv { void operator()(const framework::ExecutionContext& ctx, const framework::Tensor* x, const framework::Tensor* y, framework::Tensor* z); }; template class ElementwiseDivKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { auto* x = ctx.Input("X"); auto* y = ctx.Input("Y"); auto* z = ctx.Output("Out"); z->mutable_data(ctx.GetPlace()); auto dims_equal = x->dims() == y->dims(); if (dims_equal) { SameDimsElemwiseDiv same_dims_div; same_dims_div(ctx, x, y, z); } else { default_elementwise_div(ctx, x, y, z); } } }; template struct DivGradDX { HOSTDEVICE T operator()(T x, T y, T out, T dout) const { return dout / y; } }; template struct DivGradDX> { HOSTDEVICE paddle::platform::complex operator()( paddle::platform::complex x, paddle::platform::complex y, paddle::platform::complex out, paddle::platform::complex dout) const { paddle::platform::complex y_conj(y.real, -y.imag); return dout / y_conj; } }; template struct DivGradDY { HOSTDEVICE T operator()(T x, T y, T out, T dout) const { return -dout * out / y; } }; template struct DivGradDY> { HOSTDEVICE paddle::platform::complex operator()( paddle::platform::complex x, paddle::platform::complex y, paddle::platform::complex out, paddle::platform::complex dout) const { paddle::platform::complex out_div_y_conj((out / y).real, -(out / y).imag); return -dout * out_div_y_conj; } }; template struct DivDoubleDY { HOSTDEVICE T operator()(T x, T y, T out, T dout) const { return y * out * dout - x * dout; } }; template typename std::enable_if< std::is_same::value>::type elementwise_div_grad(const framework::ExecutionContext& ctx, const framework::Tensor* x, const framework::Tensor* y, const framework::Tensor* out, const framework::Tensor* dout, framework::Tensor* dx, framework::Tensor* dy) { int axis = ctx.Attr("axis"); ElemwiseGradCompute, DivGradDY>( ctx, *x, *y, *out, *dout, axis, dx, dy, DivGradDX(), DivGradDY()); } #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) // cuda definition template typename std::enable_if< std::is_same::value>::type elementwise_div_grad(const framework::ExecutionContext& ctx, const framework::Tensor* x, const framework::Tensor* y, const framework::Tensor* out, const framework::Tensor* dout, framework::Tensor* dx, framework::Tensor* dy); #endif template class ElementwiseDivGradKernel : public ElemwiseGradKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { ElemwiseGradKernel::Compute(ctx); using Tensor = framework::Tensor; auto* x = ctx.Input("X"); auto* y = ctx.Input("Y"); auto* out = ctx.Input("Out"); auto* dout = ctx.Input(framework::GradVarName("Out")); auto* dx = ctx.Output(framework::GradVarName("X")); auto* dy = ctx.Output(framework::GradVarName("Y")); int axis = ctx.Attr("axis"); if (dx != nullptr && dy != nullptr && (dx->dims() == dy->dims())) { elementwise_div_grad(ctx, x, y, out, dout, dx, dy); } else { ElemwiseGradCompute, DivGradDY>( ctx, *x, *y, *out, *dout, axis, dx, dy, DivGradDX(), DivGradDY()); } } }; class ElementwiseDivOpDoubleGrad : public framework::OperatorWithKernel { public: using framework::OperatorWithKernel::OperatorWithKernel; using Tensor = framework::Tensor; void InferShape(framework::InferShapeContext* ctx) const override { auto y_grad_name = framework::GradVarName("Y"); if (ctx->HasOutput("DOut")) { ctx->ShareDim("DX", "DOut"); ctx->ShareLoD("DX", "DOut"); } if (ctx->HasOutput(y_grad_name)) { ctx->ShareDim("Y", y_grad_name); ctx->ShareLoD("Y", y_grad_name); } if (ctx->HasOutput("DDOut")) { ctx->ShareDim("DX", "DDOut"); ctx->ShareLoD("DX", "DDOut"); } } framework::OpKernelType GetExpectedKernelType( const framework::ExecutionContext& ctx) const override { auto input_data_type = OperatorWithKernel::IndicateVarDataType(ctx, "Out"); #ifdef PADDLE_WITH_MKLDNN if (this->CanMKLDNNBeUsed(ctx, input_data_type)) { return framework::OpKernelType(input_data_type, ctx.GetPlace(), framework::DataLayout::kMKLDNN, framework::LibraryType::kMKLDNN); } #endif return framework::OpKernelType(input_data_type, ctx.GetPlace()); } framework::OpKernelType GetKernelTypeForVar( const std::string& var_name, const framework::Tensor& tensor, const framework::OpKernelType& expected_kernel_type) const { if (framework::IsComplexType(expected_kernel_type.data_type_)) { // only promote inputs’s types when contains complex input return framework::OpKernelType(tensor.type(), tensor.place(), tensor.layout()); } else { return framework::OpKernelType(expected_kernel_type.data_type_, tensor.place(), tensor.layout()); } } }; template class ElementwiseDivDoubleGradKernel : public framework::OpKernel { using Tensor = framework::Tensor; public: void Compute(const framework::ExecutionContext& ctx) const override { auto* Y = ctx.Input("Y"); auto* Out = ctx.Input("Out"); auto* ddX = ctx.Input("DDX"); auto* ddY = ctx.Input("DDY"); auto* dX = ctx.Input("DX"); auto* dY = ctx.Output(framework::GradVarName("Y")); auto* dOut = ctx.Output("DOut"); auto* ddOut = ctx.Output("DDOut"); int axis = ctx.Attr("axis"); if (dY) dY->mutable_data(Y->dims(), ctx.GetPlace()); if (dOut) dOut->mutable_data(Out->dims(), ctx.GetPlace()); if (ddOut) ddOut->mutable_data(Out->dims(), ctx.GetPlace()); // ddX_safe == null ? 0 : ddX // ddY_safe == null ? 0 : ddY Tensor ddX_safe, ddY_safe; GetDoubleGradSafeTensor(ctx, dX, ddX, &ddX_safe); GetDoubleGradSafeTensor(ctx, Y, ddY, &ddY_safe); // ddOut = ddX / Y - Out * ddY / Y = (ddX - Out * ddY) / Y // dY = Out * dX * ddY / Y - dX * ddX / Y // dOut = - dX * ddY // To save memory, (1) dout can be used as 'tmp' tensor, (2) ddout can // inplace ddx Tensor tmp; if (dOut) { tmp = *dOut; } else { auto& dev_ctx = ctx.template device_context(); tmp = ctx.AllocateTmpTensor(Out->dims(), dev_ctx); } if (dY) { // dX_div_Y = dX / Y; Tensor dX_div_Y = tmp; default_elementwise_div(ctx, dX, Y, &dX_div_Y); // NOTE(dengkaipeng): in the following ElemwiseGradCompute, for the // first output tensor is nullptr, the branch to calculate first // output tensor will not be activated, DivGradDx function will not // be called and can be ignored, the first branch has little effect // on running speed. // dY = Out * dX * ddY / Y - dX * ddX / Y ElemwiseGradCompute, DivDoubleDY>( ctx, ddX_safe, ddY_safe, *Out, dX_div_Y, axis, nullptr, dY, DivGradDX(), DivDoubleDY()); } if (ddOut) { // ddOut = ddX / Y - Out * ddY / Y = (ddX - Out * ddY) / Y default_elementwise_mul(ctx, Out, &ddY_safe, &tmp); default_elementwise_sub(ctx, &ddX_safe, &tmp, &tmp); default_elementwise_div(ctx, &tmp, Y, ddOut); } if (dOut) { // dOut = - dX * ddY default_elementwise_mul(ctx, dX, &ddY_safe, dOut); auto& place = *ctx.template device_context().eigen_device(); auto dout = framework::EigenVector::Flatten(*dOut); dout.device(place) = static_cast(-1) * dout; } } }; } // namespace operators } // namespace paddle