-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
check duplicate of ProtoAndCheckerMaker #2903
Changes from 1 commit
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 |
---|---|---|
|
@@ -61,7 +61,15 @@ class OpProtoAndCheckerMaker { | |
OpProtoAndCheckerMaker(OpProto* proto, OpAttrChecker* op_checker) | ||
: proto_(proto), op_checker_(op_checker) {} | ||
|
||
~OpProtoAndCheckerMaker() { CheckNoDuplicatedAttrs(); } | ||
~OpProtoAndCheckerMaker() { | ||
PADDLE_ENFORCE(validated_, "should call Validate after build"); | ||
} | ||
|
||
void Validate() { | ||
validated_ = true; | ||
CheckNoDuplicatedAttrs(); | ||
CheckNoDuplicatedInOut(); | ||
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. Not only attributes but also input and output together could not be duplicated. So maybe one function named |
||
} | ||
|
||
protected: | ||
void AddInput(const std::string& name, const std::string& comment, | ||
|
@@ -171,11 +179,27 @@ Add a mark to which output is temporary is helpful for future optimization. | |
++cnt; | ||
} | ||
PADDLE_ENFORCE(names.size() == cnt, | ||
"Cannot register two attribute in same name!"); | ||
"Cannot register two attribute with same name!"); | ||
} | ||
|
||
void CheckNoDuplicatedInOut() { | ||
std::unordered_set<std::string> names; | ||
size_t cnt = 0; | ||
for (auto& input : proto_->inputs()) { | ||
names.insert(input.name()); | ||
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. Why not use: for (auto& input : proto_->inputs()) {
std::string input_name = input.name();
PADDLE_ENFORCE(!names.count(input_name), "%s is duplicated!", input_name);
names.insert(input_name);
} In this way, we are able to be informed about which one is duplicated. 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. ok, it's a little slower~ |
||
++cnt; | ||
} | ||
for (auto& output : proto_->outputs()) { | ||
names.insert(output.name()); | ||
++cnt; | ||
} | ||
PADDLE_ENFORCE(names.size() == cnt, | ||
"Cannot register two Input or Output with same name!"); | ||
} | ||
|
||
OpProto* proto_; | ||
OpAttrChecker* op_checker_; | ||
bool validated_{false}; | ||
bool has_multiple_input_{false}; | ||
bool has_multiple_output_{false}; | ||
bool has_temporary_output_{false}; | ||
|
@@ -190,7 +214,8 @@ class OpRegistry { | |
creators()[op_type] = [] { return new OpType; }; | ||
OpProto& op_proto = protos()[op_type]; | ||
OpAttrChecker& op_checker = op_checkers()[op_type]; | ||
ProtoMakerType(&op_proto, &op_checker); | ||
auto maker = ProtoMakerType(&op_proto, &op_checker); | ||
maker.Validate(); | ||
*op_proto.mutable_type() = op_type; | ||
PADDLE_ENFORCE( | ||
op_proto.IsInitialized(), | ||
|
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.
Why not just push that validation in the destructor?
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.
because Exception in destructor can not be caught, so it can not be tested~