Created by: zhiqiu
PR types
Others
PR changes
OPs
Describe
Add common unary op
Usage
For example, if we want to write a new operator, y = -(x)
.
We can simply write like this,
class TestNegOpMaker : public framework::OpProtoAndCheckerMaker {
public:
void Make() override {
AddInput("X", "input of test op");
AddOutput("Out", "output of test op");
AddComment("Out = -X");
}
};
template <typename T>
class TestNegOpGradMaker : public framework::SingleGradOpMaker<T> {
public:
using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
protected:
void Apply(GradOpPtr<T> op) const override {
op->SetType(this->ForwardOpType() + "_grad");
op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
op->SetAttrMap(this->Attrs());
}
};
} // namespace operators
} // namespace paddle
namespace ops = paddle::operators;
namespace functors = paddle::operators::functors;
REGISTER_OPERATOR(test_neg, ops::UnaryOp, ops::TestNegOpMaker,
ops::TestNegOpGradMaker<paddle::framework::OpDesc>,
ops::TestNegOpGradMaker<paddle::imperative::OpBase>);
REGISTER_OPERATOR(test_neg_grad, ops::UnaryGradOp);
REGISTER_OP_KERNEL_4(test_neg, ops::UnaryOpKernel, CPU, functors::Neg, int,
int64_t, float, double);
REGISTER_OP_KERNEL_4(test_neg_grad, ops::UnaryGradOpKernel, CPU, functors::Neg,
int, int64_t, float, double);
As the example shown, the implementation of neg
op can be easy and simple enough.
Plus, if we want to implement another unary op, we only need to change the OpMaker
, op_name
, and functor
.