Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions paddle/framework/backward_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ using DeviceContext = platform::DeviceContext;

class EmptyOp : public OperatorBase {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be named NOP?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. We can create another PR to fix it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or we can fix it in following prs.

public:
DEFINE_OPERATOR_CTOR(EmptyOp, OperatorBase)

void InferShape(const Scope &scope) const override {}
void Run(const Scope &scope, const DeviceContext &dev_ctx) const override {}
};
Expand Down
2 changes: 2 additions & 0 deletions paddle/framework/grad_op_builder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace framework {

class NOP : public OperatorBase {
public:
DEFINE_OPERATOR_CTOR(NOP, OperatorBase)

void InferShape(const Scope &scope) const override {}
void Run(const Scope &scope,
const platform::DeviceContext &dev_ctx) const override {}
Expand Down
4 changes: 4 additions & 0 deletions paddle/framework/op_registry_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace paddle {
namespace framework {
class CosineOp : public OperatorBase {
public:
DEFINE_OPERATOR_CTOR(CosineOp, OperatorBase)

void Run(const Scope& scope,
const platform::DeviceContext& dev_ctx) const override {}
void InferShape(const Scope& scope) const override {}
Expand All @@ -27,6 +29,8 @@ class CosineOpProtoAndCheckerMaker : public OpProtoAndCheckerMaker {

class MyTestOp : public OperatorBase {
public:
DEFINE_OPERATOR_CTOR(MyTestOp, OperatorBase)

void InferShape(const Scope& scope) const override {}
void Run(const Scope& scope,
const platform::DeviceContext& dev_ctx) const override {}
Expand Down
32 changes: 32 additions & 0 deletions paddle/framework/operator.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ class ExecutionContext;
*/
class OperatorBase {
public:
OperatorBase() {} // TODO(yi): This constructor is to be removed.
OperatorBase(const std::string& type, const std::vector<std::string>& inputs,
const std::vector<std::string>& outputs,
const AttributeMap& attrs,
std::unordered_map<std::string, int>* in_out_idxs)
: type_(type),
inputs_(inputs),
outputs_(outputs),
attrs_(attrs),
in_out_idxs_(in_out_idxs) {}

virtual ~OperatorBase() {}

template <typename T>
Expand Down Expand Up @@ -109,6 +120,9 @@ class OperatorBase {
const std::vector<std::string> Inputs() const { return inputs_; }
const std::vector<std::string> Outputs() const { return outputs_; }
const AttributeMap& Attrs() const { return attrs_; }
const std::unordered_map<std::string, int>* InOutIdx() const {
return in_out_idxs_.get();
}

public:
std::string type_;
Expand Down Expand Up @@ -286,6 +300,14 @@ class OpKernel {

class OperatorWithKernel : public OperatorBase {
public:
OperatorWithKernel() {} // TODO(yi): This constructor is to be removed.
OperatorWithKernel(const std::string& type,
const std::vector<std::string>& inputs,
const std::vector<std::string>& outputs,
const AttributeMap& attrs,
std::unordered_map<std::string, int>* in_out_idxs)
: OperatorBase(type, inputs, outputs, attrs, in_out_idxs) {}

struct OpKernelKey {
platform::Place place_;

Expand Down Expand Up @@ -335,5 +357,15 @@ class OperatorWithKernel : public OperatorBase {
virtual void InferShape(const InferShapeContext& ctx) const = 0;
};

#define DEFINE_OPERATOR_CTOR(Class, ParentClass) \
public: \
Class() { /* TODO(yi): This constructor is to be removed. */ \
} \
Class(const std::string& type, const std::vector<std::string>& inputs, \
const std::vector<std::string>& outputs, \
const ::paddle::framework::AttributeMap& attrs, \
std::unordered_map<std::string, int>* in_out_idxs) \
: ParentClass(type, inputs, outputs, attrs, in_out_idxs) {}

} // namespace framework
} // namespace paddle
6 changes: 6 additions & 0 deletions paddle/framework/operator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ static int op_run_num = 0;

class OpWithoutKernelTest : public OperatorBase {
public:
DEFINE_OPERATOR_CTOR(OpWithoutKernelTest, OperatorBase)

void Init() override { x = 1; }
void InferShape(const Scope& scope) const override {}
void Run(const Scope& scope,
Expand Down Expand Up @@ -97,6 +99,8 @@ class OpKernelTestProtoAndCheckerMaker : public OpProtoAndCheckerMaker {
static int cpu_kernel_run_num = 0;

class OpWithKernelTest : public OperatorWithKernel {
public:
DEFINE_OPERATOR_CTOR(OpWithKernelTest, OperatorWithKernel)
protected:
void InferShape(const framework::InferShapeContext& ctx) const override {}
};
Expand All @@ -116,6 +120,8 @@ class CPUKernelTest : public OpKernel {
// multiple inputs test
class OperatorMultiInputsTest : public OperatorBase {
public:
DEFINE_OPERATOR_CTOR(OperatorMultiInputsTest, OperatorBase)

void Init() override { x = 1; }
void InferShape(const Scope& scope) const override {}
void Run(const Scope& scope,
Expand Down
2 changes: 2 additions & 0 deletions paddle/operators/add_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace paddle {
namespace operators {

class AddOp : public framework::OperatorWithKernel {
DEFINE_OPERATOR_CTOR(AddOp, framework::OperatorWithKernel)
protected:
void InferShape(const framework::InferShapeContext &ctx) const override {
PADDLE_ENFORCE_EQ(ctx.InputSize(), 2);
Expand Down Expand Up @@ -47,6 +48,7 @@ The equation is: Out = X + Y
};

class AddOpGrad : public framework::OperatorWithKernel {
DEFINE_OPERATOR_CTOR(AddOpGrad, framework::OperatorWithKernel)
protected:
void InferShape(const framework::InferShapeContext &ctx) const override {}
};
Expand Down
3 changes: 3 additions & 0 deletions paddle/operators/cross_entropy_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace paddle {
namespace operators {

class OnehotCrossEntropyOp : public framework::OperatorWithKernel {
DEFINE_OPERATOR_CTOR(OnehotCrossEntropyOp, framework::OperatorWithKernel)
protected:
void InferShape(const framework::InferShapeContext &ctx) const override {
PADDLE_ENFORCE_EQ(ctx.InputSize(), 2,
Expand All @@ -38,6 +39,8 @@ class OnehotCrossEntropyOp : public framework::OperatorWithKernel {
};

class OnehotCrossEntropyGradientOp : public framework::OperatorWithKernel {
DEFINE_OPERATOR_CTOR(OnehotCrossEntropyGradientOp,
framework::OperatorWithKernel)
protected:
void InferShape(const framework::InferShapeContext &ctx) const override {
auto X_grad = ctx.Output<Tensor>(framework::GradVarName("X"));
Expand Down
1 change: 1 addition & 0 deletions paddle/operators/fill_zeros_like_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace paddle {
namespace operators {

class FillZerosLikeOp : public framework::OperatorWithKernel {
DEFINE_OPERATOR_CTOR(FillZerosLikeOp, framework::OperatorWithKernel)
protected:
void InferShape(const framework::InferShapeContext &ctx) const override {
PADDLE_ENFORCE_EQ(ctx.InputSize(), 1UL,
Expand Down
1 change: 1 addition & 0 deletions paddle/operators/gaussian_random_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class GaussianRandomKernel : public framework::OpKernel {
};

class GaussianRandomOp : public framework::OperatorWithKernel {
DEFINE_OPERATOR_CTOR(GaussianRandomOp, framework::OperatorWithKernel)
protected:
void InferShape(const framework::InferShapeContext& context) const override {
auto* tensor = context.Output<framework::Tensor>(0);
Expand Down
2 changes: 2 additions & 0 deletions paddle/operators/mean_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace paddle {
namespace operators {

class MeanOp : public framework::OperatorWithKernel {
DEFINE_OPERATOR_CTOR(MeanOp, framework::OperatorWithKernel)
protected:
void InferShape(const framework::InferShapeContext &ctx) const override {
PADDLE_ENFORCE_EQ(ctx.InputSize(), 1, "Input size of AddOp must be one");
Expand All @@ -39,6 +40,7 @@ class MeanOpMaker : public framework::OpProtoAndCheckerMaker {
};

class MeanGradOp : public framework::OperatorWithKernel {
DEFINE_OPERATOR_CTOR(MeanGradOp, framework::OperatorWithKernel)
protected:
void InferShape(const framework::InferShapeContext &ctx) const override {
ctx.Output<Tensor>(framework::GradVarName("X"))
Expand Down
2 changes: 2 additions & 0 deletions paddle/operators/mul_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace paddle {
namespace operators {

class MulOp : public framework::OperatorWithKernel {
DEFINE_OPERATOR_CTOR(MulOp, framework::OperatorWithKernel)
protected:
void InferShape(const framework::InferShapeContext &ctx) const override {
PADDLE_ENFORCE(ctx.InputSize() == 2, "The mul op must take two inputs");
Expand Down Expand Up @@ -53,6 +54,7 @@ The equation is: Out = X * Y
};

class MulOpGrad : public framework::OperatorWithKernel {
DEFINE_OPERATOR_CTOR(MulOpGrad, framework::OperatorWithKernel)
protected:
void InferShape(const framework::InferShapeContext &ctx) const override {}
std::string DebugString() const override {
Expand Down
2 changes: 2 additions & 0 deletions paddle/operators/net_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ namespace operators {
*/
class NetOp : public framework::OperatorBase {
public:
DEFINE_OPERATOR_CTOR(NetOp, framework::OperatorBase)

/**
* Infer all the operators' input and output variables' shapes, will be called
* before every mini-batch
Expand Down
4 changes: 4 additions & 0 deletions paddle/operators/net_op_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ static int run_cnt = 0;

class TestOp : public framework::OperatorBase {
public:
DEFINE_OPERATOR_CTOR(TestOp, framework::OperatorBase)

void InferShape(const Scope& scope) const override { ++infer_shape_cnt; }
void Run(const Scope& scope,
const platform::DeviceContext& dev_ctx) const override {
Expand All @@ -21,6 +23,8 @@ class TestOp : public framework::OperatorBase {

class EmptyOp : public framework::OperatorBase {
public:
DEFINE_OPERATOR_CTOR(EmptyOp, framework::OperatorBase)

void InferShape(const Scope& scope) const override {}
void Run(const Scope& scope, const DeviceContext& dev_ctx) const override {}
};
Expand Down
1 change: 1 addition & 0 deletions paddle/operators/recurrent_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class RecurrentGradientAlgorithm {
};

class RecurrentOp final : public framework::OperatorBase {
DEFINE_OPERATOR_CTOR(RecurrentOp, framework::OperatorBase)
public:
void Init() override;

Expand Down
1 change: 1 addition & 0 deletions paddle/operators/rowwise_add_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace paddle {
namespace operators {

class RowWiseAddOp : public framework::OperatorWithKernel {
DEFINE_OPERATOR_CTOR(RowWiseAddOp, framework::OperatorWithKernel)
protected:
void InferShape(const framework::InferShapeContext &ctx) const override {
PADDLE_ENFORCE(ctx.InputSize() == 2UL,
Expand Down
1 change: 1 addition & 0 deletions paddle/operators/sgd_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace paddle {
namespace operators {

class SGDOp : public framework::OperatorWithKernel {
DEFINE_OPERATOR_CTOR(SGDOp, framework::OperatorWithKernel)
protected:
void InferShape(const framework::InferShapeContext &ctx) const override {
PADDLE_ENFORCE_EQ(ctx.InputSize(), 2, "Input size of SGDOp must be two");
Expand Down
2 changes: 2 additions & 0 deletions paddle/operators/sigmoid_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace paddle {
namespace operators {

class SigmoidOp : public framework::OperatorWithKernel {
DEFINE_OPERATOR_CTOR(SigmoidOp, framework::OperatorWithKernel)
protected:
void InferShape(const framework::InferShapeContext &ctx) const override {
PADDLE_ENFORCE(ctx.InputSize() == 1, "Sigmoid Op only have one input");
Expand All @@ -38,6 +39,7 @@ class SigmoidOpMaker : public framework::OpProtoAndCheckerMaker {
};

class SigmoidOpGrad : public framework::OperatorWithKernel {
DEFINE_OPERATOR_CTOR(SigmoidOpGrad, framework::OperatorWithKernel)
protected:
void InferShape(const framework::InferShapeContext &ctx) const override {
ctx.Output<Tensor>(0)->Resize(ctx.Input<Tensor>(0)->dims());
Expand Down
2 changes: 2 additions & 0 deletions paddle/operators/softmax_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace paddle {
namespace operators {

class SoftmaxOp : public framework::OperatorWithKernel {
DEFINE_OPERATOR_CTOR(SoftmaxOp, framework::OperatorWithKernel)
protected:
void InferShape(const framework::InferShapeContext &ctx) const override {
PADDLE_ENFORCE_EQ(ctx.InputSize(), 1UL,
Expand All @@ -42,6 +43,7 @@ class SoftmaxOpMaker : public framework::OpProtoAndCheckerMaker {
};

class SoftmaxOpGrad : public framework::OperatorWithKernel {
DEFINE_OPERATOR_CTOR(SoftmaxOpGrad, framework::OperatorWithKernel)
protected:
void InferShape(const framework::InferShapeContext &ctx) const override {
PADDLE_ENFORCE_EQ(ctx.InputSize(), 3UL,
Expand Down
1 change: 1 addition & 0 deletions paddle/operators/uniform_random_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class CPUUniformRandomKernel : public framework::OpKernel {
};

class UniformRandomOp : public framework::OperatorWithKernel {
DEFINE_OPERATOR_CTOR(UniformRandomOp, framework::OperatorWithKernel)
protected:
void InferShape(const framework::InferShapeContext& ctx) const override {
PADDLE_ENFORCE(GetAttr<float>("min") < GetAttr<float>("max"),
Expand Down