-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Add digamma_op and unittest #33278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add digamma_op and unittest #33278
Changes from 3 commits
125f7fe
78e1fd9
2d6c3b9
31759be
6c45985
47a0592
0ec2ca4
37f8f81
66f9ab0
bfc12ba
58a486b
2c3899a
469bd9d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| /* Copyright (c) 2020 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/fluid/operators/digamma_op.h" | ||
|
|
||
| namespace paddle { | ||
| namespace operators { | ||
|
|
||
| class DigammaOp : public framework::OperatorWithKernel { | ||
| public: | ||
| DigammaOp(const std::string &type, const framework::VariableNameMap &inputs, | ||
| const framework::VariableNameMap &outputs, | ||
| const framework::AttributeMap &attrs) | ||
| : OperatorWithKernel(type, inputs, outputs, attrs) {} | ||
|
|
||
| void InferShape(framework::InferShapeContext *ctx) const override { | ||
| OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "Digamma"); | ||
| OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "Digamma"); | ||
|
|
||
| auto in_dims = ctx->GetInputDim("X"); | ||
|
|
||
| ctx->SetOutputDim("Out", in_dims); | ||
| ctx->ShareLoD("X", "Out"); | ||
| } | ||
| }; | ||
|
|
||
| class DigammaOpMaker : public framework::OpProtoAndCheckerMaker { | ||
| public: | ||
| void Make() override { | ||
| AddInput("X", "(Tensor), The input tensor of digamma operator."); | ||
| AddOutput("Out", "(Tensor), The output tensor of digamma operator."); | ||
| AddComment(R"DOC( | ||
| Digamma Operator. | ||
|
|
||
| This operator is used to perform elementwise digamma for input $X$. | ||
| $$out = \Psi(x) = \frac{ \Gamma^{'}(x) }{ \Gamma(x) }$$ | ||
|
|
||
| )DOC"); | ||
| } | ||
| }; | ||
|
|
||
| class DigammaGradOp : public framework::OperatorWithKernel { | ||
| public: | ||
| using framework::OperatorWithKernel::OperatorWithKernel; | ||
| void InferShape(framework::InferShapeContext *ctx) const override { | ||
| OP_INOUT_CHECK(ctx->HasInput(framework::GradVarName("Out")), "Input", | ||
| "Out@Grad", "DigammaGrad"); | ||
| OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "DigammaGrad"); | ||
| OP_INOUT_CHECK(ctx->HasOutput(framework::GradVarName("X")), "Output", | ||
| "X@Grad", "DigammaGrad"); | ||
|
|
||
| auto dout_dims = ctx->GetInputDim(framework::GradVarName("Out")); | ||
| ctx->SetOutputDim(framework::GradVarName("X"), dout_dims); | ||
| ctx->ShareLoD(framework::GradVarName("Out"), framework::GradVarName("X")); | ||
| } | ||
| }; | ||
|
|
||
| template <typename T> | ||
| class DigammaGradOpMaker : public framework::SingleGradOpMaker<T> { | ||
| public: | ||
| using framework::SingleGradOpMaker<T>::SingleGradOpMaker; | ||
|
|
||
| void Apply(GradOpPtr<T> retv) const override { | ||
| retv->SetType("digamma_grad"); | ||
| retv->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out")); | ||
| retv->SetInput("X", this->Input("X")); | ||
| retv->SetAttrMap(this->Attrs()); | ||
| retv->SetOutput(framework::GradVarName("X"), this->InputGrad("X")); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace operators | ||
| } // namespace paddle | ||
|
|
||
| namespace ops = paddle::operators; | ||
|
|
||
| REGISTER_OPERATOR(digamma, ops::DigammaOp, ops::DigammaOpMaker, | ||
| ops::DigammaGradOpMaker<paddle::framework::OpDesc>, | ||
| ops::DigammaGradOpMaker<paddle::imperative::OpBase>); | ||
| REGISTER_OPERATOR(digamma_grad, ops::DigammaGradOp); | ||
|
|
||
| REGISTER_OP_CPU_KERNEL( | ||
| digamma, ops::DigammaKernel<paddle::platform::CPUDeviceContext, float>, | ||
| ops::DigammaKernel<paddle::platform::CPUDeviceContext, double>); | ||
|
|
||
| REGISTER_OP_CPU_KERNEL( | ||
| digamma_grad, | ||
| ops::DigammaGradKernel<paddle::platform::CPUDeviceContext, float>, | ||
| ops::DigammaGradKernel<paddle::platform::CPUDeviceContext, double>); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* Copyright (c) 2020 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 <unsupported/Eigen/SpecialFunctions> | ||
| #include "paddle/fluid/operators/digamma_op.h" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here we only need the header
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
| #include "paddle/fluid/operators/elementwise/elementwise_op_impl.cu.h" | ||
| #include "paddle/fluid/operators/math/complex_functors.h" | ||
|
|
||
| namespace paddle { | ||
| namespace operators { | ||
|
|
||
| template <typename T, typename Enable = void> | ||
| struct CudaDigammaFunctor; | ||
|
|
||
| template <typename T> | ||
| struct CudaDigammaFunctor<T, math::NoComplex<T, math::Real<T>>> { | ||
| __device__ __forceinline__ T operator()(const T* args) const { | ||
| return Eigen::numext::digamma(args[0]); | ||
| } | ||
| }; | ||
|
|
||
| template <typename T> | ||
| class DigammaKernel<platform::CUDADeviceContext, T> | ||
|
||
| : public framework::OpKernel<T> { | ||
| public: | ||
| void Compute(const framework::ExecutionContext& context) const override { | ||
| const Tensor* x = context.Input<Tensor>("X"); | ||
| Tensor* out = context.Output<Tensor>("Out"); | ||
| out->mutable_data<math::Real<T>>(context.GetPlace()); | ||
|
|
||
| auto& dev_ctx = context.device_context<platform::CUDADeviceContext>(); | ||
| std::vector<const framework::Tensor*> ins = {x}; | ||
| std::vector<framework::Tensor*> outs = {out}; | ||
| auto functor = CudaDigammaFunctor<T>(); | ||
| LaunchSameDimsElementwiseCudaKernel<ElementwiseType::kUnary, T, | ||
| math::Real<T>>(dev_ctx, ins, &outs, | ||
| functor); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace operators | ||
| } // namespace paddle | ||
|
|
||
| namespace ops = paddle::operators; | ||
|
|
||
| REGISTER_OP_CUDA_KERNEL( | ||
| digamma, ops::DigammaKernel<paddle::platform::CUDADeviceContext, float>, | ||
| ops::DigammaKernel<paddle::platform::CUDADeviceContext, double>); | ||
|
|
||
| REGISTER_OP_CUDA_KERNEL( | ||
| digamma_grad, | ||
| ops::DigammaGradKernel<paddle::platform::CUDADeviceContext, float>, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need special DigammaGradKernel here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. register a CudaKernel for digamma grad is necessary |
||
| ops::DigammaGradKernel<paddle::platform::CUDADeviceContext, double>); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /* Copyright (c) 2020 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 <unsupported/Eigen/SpecialFunctions> | ||
| #include "paddle/fluid/framework/op_registry.h" | ||
| #include "paddle/fluid/framework/operator.h" | ||
| #include "paddle/fluid/platform/for_range.h" | ||
|
|
||
| namespace paddle { | ||
| namespace operators { | ||
|
|
||
| template <typename T> | ||
| struct DigammaFunctor { | ||
| DigammaFunctor(const T* input, T* output, int64_t numel) | ||
| : input_(input), output_(output), numel_(numel) {} | ||
|
|
||
| HOSTDEVICE void operator()(int64_t idx) const { | ||
| output_[idx] = Eigen::numext::digamma(input_[idx]); | ||
| } | ||
|
|
||
| private: | ||
| const T* input_; | ||
| T* output_; | ||
| int64_t numel_; | ||
| }; | ||
|
|
||
| template <typename T> | ||
| struct DigammaGradFunctor { | ||
| DigammaGradFunctor(const T* dout, const T* x, T* output, int64_t numel) | ||
| : dout_(dout), x_(x), output_(output), numel_(numel) {} | ||
|
|
||
| HOSTDEVICE void operator()(int64_t idx) const { | ||
| output_[idx] = dout_[idx] * Eigen::numext::polygamma(T(1), x_[idx]); | ||
| } | ||
|
|
||
| private: | ||
| const T* dout_; | ||
| const T* x_; | ||
| T* output_; | ||
| int64_t numel_; | ||
| }; | ||
|
|
||
| using Tensor = framework::Tensor; | ||
|
|
||
| template <typename DeviceContext, typename T> | ||
| class DigammaKernel : public framework::OpKernel<T> { | ||
| public: | ||
| void Compute(const framework::ExecutionContext& context) const override { | ||
| const Tensor* x = context.Input<Tensor>("X"); | ||
| Tensor* out = context.Output<Tensor>("Out"); | ||
|
|
||
| auto numel = x->numel(); | ||
| auto* x_data = x->data<T>(); | ||
| auto* out_data = out->mutable_data<T>(context.GetPlace(), | ||
| size_t(x->numel() * sizeof(T))); | ||
|
|
||
| auto& dev_ctx = context.template device_context<DeviceContext>(); | ||
| platform::ForRange<DeviceContext> for_range(dev_ctx, numel); | ||
| DigammaFunctor<T> functor(x_data, out_data, numel); | ||
| for_range(functor); | ||
| } | ||
| }; | ||
|
|
||
| template <typename DeviceContext, typename T> | ||
| class DigammaGradKernel : public framework::OpKernel<T> { | ||
| public: | ||
| void Compute(const framework::ExecutionContext& context) const override { | ||
| const Tensor* d_out = context.Input<Tensor>(framework::GradVarName("Out")); | ||
| const Tensor* x = context.Input<Tensor>("X"); | ||
| auto* d_x = context.Output<Tensor>(framework::GradVarName("X")); | ||
|
|
||
| auto numel = d_out->numel(); | ||
| auto* dout_data = d_out->data<T>(); | ||
| auto* x_data = x->data<T>(); | ||
| auto* dx_data = d_x->mutable_data<T>( | ||
| context.GetPlace(), static_cast<size_t>(numel * sizeof(T))); | ||
|
|
||
| auto& dev_ctx = context.template device_context<DeviceContext>(); | ||
| platform::ForRange<DeviceContext> for_range(dev_ctx, numel); | ||
| DigammaGradFunctor<T> functor(dout_data, x_data, dx_data, numel); | ||
| for_range(functor); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace operators | ||
| } // namespace paddle |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2020 -> 2021
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done