Skip to content
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 constructors to OperatorBase and all sub-classes #3430

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions paddle/framework/backward_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ 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:
EmptyOp(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) {}

void InferShape(const Scope &scope) const override {}
void Run(const Scope &scope, const DeviceContext &dev_ctx) const override {}
};
Expand Down
5 changes: 5 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,11 @@ namespace framework {

class NOP : public OperatorBase {
public:
NOP(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) {}

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

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

class MyTestOp : public OperatorBase {
public:
MyTestOp(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) {}

void InferShape(const Scope& scope) const override {}
void Run(const Scope& scope,
const platform::DeviceContext& dev_ctx) const override {}
Expand Down
20 changes: 20 additions & 0 deletions paddle/framework/operator.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ class ExecutionContext;
*/
class OperatorBase {
public:
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_(input),
outputs_(output),
attrs_(attrs),
in_out_idxs_(in_out_idxs) {}

virtual ~OperatorBase() {}

template <typename T>
Expand Down Expand Up @@ -109,6 +119,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 +299,13 @@ class OpKernel {

class OperatorWithKernel : public OperatorBase {
public:
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
14 changes: 14 additions & 0 deletions paddle/framework/operator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ static int op_run_num = 0;

class OpWithoutKernelTest : public OperatorBase {
public:
OpWithoutKernelTest(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) {}

void Init() override { x = 1; }
void InferShape(const Scope& scope) const override {}
void Run(const Scope& scope,
Expand Down Expand Up @@ -116,6 +123,13 @@ class CPUKernelTest : public OpKernel {
// multiple inputs test
class OperatorMultiInputsTest : public OperatorBase {
public:
OperatorMultiInputsTest(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) {}

void Init() override { x = 1; }
void InferShape(const Scope& scope) const override {}
void Run(const Scope& scope,
Expand Down