diff --git a/paddle/fluid/operators/elementwise/elementwise_pow_op.cc b/paddle/fluid/operators/elementwise/elementwise_pow_op.cc index 6335e67a8a48c8702f0cb14ce947275d47e01d17..59ec9a2d4a5dd6751f5ea5c6124f49b2e99d057e 100644 --- a/paddle/fluid/operators/elementwise/elementwise_pow_op.cc +++ b/paddle/fluid/operators/elementwise/elementwise_pow_op.cc @@ -1,11 +1,8 @@ /* Copyright (c) 2018 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. @@ -13,11 +10,30 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/fluid/operators/elementwise/elementwise_pow_op.h" +#include #include #include "paddle/fluid/operators/elementwise/elementwise_op.h" namespace paddle { namespace operators { + +class ElementwisePowOpGradDescMaker : public framework::SingleGradOpDescMaker { + public: + using framework::SingleGradOpDescMaker::SingleGradOpDescMaker; + + protected: + std::unique_ptr Apply() const override { + std::unique_ptr op(new framework::OpDesc()); + op->SetType("elementwise_pow_grad"); + op->SetInput("X", Input("X")); + op->SetInput("Y", Input("Y")); + op->SetInput(framework::GradVarName("Out"), OutputGrad("Out")); + op->SetAttrMap(Attrs()); + op->SetOutput(framework::GradVarName("X"), InputGrad("X")); + op->SetOutput(framework::GradVarName("Y"), InputGrad("Y")); + return op; + } +}; class ElementwisePowOpMaker : public ElementwiseOpMaker { protected: std::string GetName() const override { return "Pow"; } @@ -27,9 +43,20 @@ class ElementwisePowOpMaker : public ElementwiseOpMaker { } // namespace paddle namespace ops = paddle::operators; -REGISTER_OP_WITHOUT_GRADIENT(elementwise_pow, ops::ElementwiseOp, - ops::ElementwisePowOpMaker); +REGISTER_OPERATOR(elementwise_pow, ops::ElementwiseOp, + ops::ElementwisePowOpMaker, ops::ElementwiseOpInferVarType, + ops::ElementwisePowOpGradDescMaker); +REGISTER_OPERATOR(elementwise_pow_grad, ops::ElementwiseOpGrad); + REGISTER_OP_CPU_KERNEL( elementwise_pow, ops::ElementwisePowKernel, - ops::ElementwisePowKernel); + ops::ElementwisePowKernel, + ops::ElementwisePowKernel, + ops::ElementwisePowKernel); +REGISTER_OP_CPU_KERNEL( + elementwise_pow_grad, + ops::ElementwisePowGradKernel, + ops::ElementwisePowGradKernel, + ops::ElementwisePowGradKernel, + ops::ElementwisePowGradKernel); diff --git a/paddle/fluid/operators/elementwise/elementwise_pow_op.cu b/paddle/fluid/operators/elementwise/elementwise_pow_op.cu index 9263dbfebfd00451f3e67c3ca9d2081b5b4904bd..320d1e7b38da8e4f77015ef2b7bcc73e5db7675f 100644 --- a/paddle/fluid/operators/elementwise/elementwise_pow_op.cu +++ b/paddle/fluid/operators/elementwise/elementwise_pow_op.cu @@ -15,4 +15,13 @@ namespace ops = paddle::operators; REGISTER_OP_CUDA_KERNEL( elementwise_pow, ops::ElementwisePowKernel, - ops::ElementwisePowKernel); + ops::ElementwisePowKernel, + ops::ElementwisePowKernel, + ops::ElementwisePowKernel); +REGISTER_OP_CUDA_KERNEL( + elementwise_pow_grad, + ops::ElementwisePowGradKernel, + ops::ElementwisePowGradKernel, + ops::ElementwisePowGradKernel, + ops::ElementwisePowGradKernel); diff --git a/paddle/fluid/operators/elementwise/elementwise_pow_op.h b/paddle/fluid/operators/elementwise/elementwise_pow_op.h index dc584b4c32fc3063da0c6de50577d28afcb63b83..1363485ced4e12bd7c67c04037ea3a2cd27b0e54 100644 --- a/paddle/fluid/operators/elementwise/elementwise_pow_op.h +++ b/paddle/fluid/operators/elementwise/elementwise_pow_op.h @@ -1,11 +1,8 @@ /* Copyright (c) 2018 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. @@ -15,6 +12,7 @@ limitations under the License. */ #pragma once #include +#include "paddle/fluid/operators/elementwise/elementwise_op.h" #include "paddle/fluid/operators/elementwise/elementwise_op_function.h" namespace paddle { @@ -29,9 +27,11 @@ template class ElementwisePowKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { - using Tensor = framework::Tensor; - + using Tensor = framework::LoDTensor; auto* x = ctx.Input("X"); + PADDLE_ENFORCE(x != nullptr, + "Cannot get input Variable X, variable name = %s", + ctx.op().Input("X")); auto* y = ctx.Input("Y"); auto* z = ctx.Output("Out"); z->mutable_data(ctx.GetPlace()); @@ -41,5 +41,36 @@ class ElementwisePowKernel : public framework::OpKernel { } }; +template +struct PowGradDX { + HOSTDEVICE T operator()(T x, T y, T out, T dout) const { + return dout * y * std::pow(x, y - 1); + } +}; + +template +struct PowGradDY { + HOSTDEVICE T operator()(T x, T y, T out, T dout) const { + return dout * std::log(x) * std::pow(x, y); + } +}; + +template +class ElementwisePowGradKernel : 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* dout = ctx.Input(framework::GradVarName("Out")); + auto* out = dout; + auto* dx = ctx.Output(framework::GradVarName("X")); + auto* dy = ctx.Output(framework::GradVarName("Y")); + int axis = ctx.Attr("axis"); + ElemwiseGradCompute, PowGradDY>( + ctx, *x, *y, *out, *dout, axis, dx, dy, PowGradDX(), PowGradDY()); + } +}; } // namespace operators } // namespace paddle diff --git a/python/paddle/fluid/tests/unittests/test_elementwise_pow_op.py b/python/paddle/fluid/tests/unittests/test_elementwise_pow_op.py index 7bf642f03f480b1eeec68298f9d453deb1fa2ac3..0b0c7c5ecb872e0a1c6ef0e722a95858801e2d56 100644 --- a/python/paddle/fluid/tests/unittests/test_elementwise_pow_op.py +++ b/python/paddle/fluid/tests/unittests/test_elementwise_pow_op.py @@ -22,24 +22,87 @@ class TestElementwisePowOp(OpTest): def setUp(self): self.op_type = "elementwise_pow" self.inputs = { - 'X': np.random.uniform(0.1, 1, [13, 17]).astype("float32"), - 'Y': np.random.uniform(0.1, 1, [13, 17]).astype("float32") + 'X': np.random.uniform(0.1, 1, [2, 3]).astype("float32"), + 'Y': np.random.uniform(0.1, 1, [2, 3]).astype("float32") } self.outputs = {'Out': np.power(self.inputs['X'], self.inputs['Y'])} def test_check_output(self): self.check_output() + def test_check_grad_normal(self): + self.check_grad(['X', 'Y'], 'Out') + class TestElementwisePowOp_scalar(TestElementwisePowOp): def setUp(self): self.op_type = "elementwise_pow" self.inputs = { - 'X': np.random.rand(2, 3, 4).astype('float32'), - 'Y': np.random.rand(1).astype('float32') + 'X': np.random.uniform(0.1, 1, [3, 3, 4]).astype(np.float32), + 'Y': np.random.uniform(0.1, 1, [1]).astype(np.float32) + } + self.outputs = {'Out': np.power(self.inputs['X'], self.inputs['Y'])} + + +class TestElementwisePowOp_tensor(TestElementwisePowOp): + def setUp(self): + self.op_type = "elementwise_pow" + self.inputs = { + 'X': np.random.uniform(0.1, 1, [32]).astype("float32"), + 'Y': np.random.uniform(0.1, 1, [32]).astype("float32") + } + self.outputs = {'Out': np.power(self.inputs['X'], self.inputs['Y'])} + + +class TestElementwisePowOp_broadcast_0(TestElementwisePowOp): + def setUp(self): + self.op_type = "elementwise_pow" + self.inputs = { + 'X': np.random.uniform(0.1, 1, [2, 3, 4]).astype("float32"), + 'Y': np.random.uniform(0.1, 1, [4]).astype("float32") } self.outputs = {'Out': np.power(self.inputs['X'], self.inputs['Y'])} +class TestElementwisePowOp_broadcast_1(TestElementwisePowOp): + def setUp(self): + self.op_type = "elementwise_pow" + self.inputs = { + 'X': np.random.uniform(0.1, 1, [2, 3, 4]).astype("float32"), + 'Y': np.random.uniform(0.1, 1, [3]).astype("float32") + } + self.attrs = {'axis': 1} + self.outputs = { + 'Out': np.power(self.inputs['X'], self.inputs['Y'].reshape(3, 1)) + } + + +class TestElementwisePowOp_broadcast_2(TestElementwisePowOp): + def setUp(self): + self.op_type = "elementwise_pow" + self.inputs = { + 'X': np.random.uniform(0.1, 1, [2, 3, 4]).astype("float32"), + 'Y': np.random.uniform(0.1, 1, [2]).astype("float32") + } + self.attrs = {'axis': 0} + self.outputs = { + 'Out': np.power(self.inputs['X'], self.inputs['Y'].reshape(2, 1, 1)) + } + + +class TestElementwisePowOp_broadcast_3(TestElementwisePowOp): + def setUp(self): + self.op_type = "elementwise_pow" + self.inputs = { + 'X': np.random.uniform(0.1, 1, [2, 3, 4, 5]).astype("float32"), + 'Y': np.random.uniform(0.1, 1, [3, 4]).astype("float32") + } + self.attrs = {'axis': 1} + self.outputs = { + 'Out': np.power(self.inputs['X'], self.inputs['Y'].reshape(1, 3, 4, + 1)) + } + + if __name__ == '__main__': unittest.main()