- Notifications
You must be signed in to change notification settings - Fork 5.9k
Let OpProto support multiple and temporary #2860
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
Merged
jacquesqiao merged 2 commits into PaddlePaddle:develop from reyoung:feature/op_proto_modified Jul 14, 2017
Merged
Changes from all commits
Commits
Show all changes
2 commits Select commit Hold shift + click to select a range
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
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -2,6 +2,8 @@ | |
| | ||
| #include <algorithm> | ||
| #include <type_traits> | ||
| #include <unordered_map> | ||
| #include <unordered_set> | ||
| #include "paddle/framework/attr_checker.h" | ||
| #include "paddle/framework/op_desc.pb.h" | ||
| #include "paddle/framework/op_proto.pb.h" | ||
| | @@ -59,25 +61,52 @@ class OpProtoAndCheckerMaker { | |
| OpProtoAndCheckerMaker(OpProto* proto, OpAttrChecker* op_checker) | ||
| : proto_(proto), op_checker_(op_checker) {} | ||
| | ||
| ~OpProtoAndCheckerMaker() { CheckNoDuplicatedAttrs(); } | ||
| | ||
| protected: | ||
| void AddInput(const std::string& name, const std::string& comment) { | ||
| void AddInput(const std::string& name, const std::string& comment, | ||
| bool multiple = false) { | ||
| auto input = proto_->mutable_inputs()->Add(); | ||
| *input->mutable_name() = name; | ||
| *input->mutable_comment() = comment; | ||
| input->set_multiple(multiple); | ||
| if (multiple) { | ||
| SetHasMultipleInput(); | ||
| } | ||
| } | ||
| | ||
| void AddInputs(const std::string& name, const std::string& comment) { | ||
| AddInput(name, comment, true); | ||
| } | ||
| | ||
| void AddOutput(const std::string& name, const std::string& comment) { | ||
| void AddOutput(const std::string& name, const std::string& comment, | ||
| bool temporary = false, bool multiple = false) { | ||
| auto output = proto_->mutable_outputs()->Add(); | ||
| *output->mutable_name() = name; | ||
| *output->mutable_comment() = comment; | ||
| output->set_multiple(multiple); | ||
| if (multiple) { | ||
| SetHasMultipleOutput(); | ||
| } | ||
| output->set_temporary(temporary); | ||
| if (temporary) { | ||
| SetHasTemporaryOutput(); | ||
| } | ||
| } | ||
| | ||
| void AddOutputs(const std::string& name, const std::string& comment, | ||
| bool temporary = false) { | ||
| AddOutput(name, comment, temporary, true); | ||
| } | ||
| | ||
| template <typename T> | ||
| TypedAttrChecker<T>& AddAttr(const std::string& name, | ||
| const std::string& comment) { | ||
| const std::string& comment, | ||
| bool generated = false) { | ||
| auto attr = proto_->mutable_attrs()->Add(); | ||
| *attr->mutable_name() = name; | ||
| *attr->mutable_comment() = comment; | ||
| attr->set_generated(generated); | ||
| AttrTypeHelper::SetAttrType<T>(attr); | ||
| return op_checker_->AddAttrChecker<T>(name); | ||
| } | ||
| | @@ -86,8 +115,70 @@ class OpProtoAndCheckerMaker { | |
| *(proto_->mutable_comment()) = comment; | ||
| } | ||
| | ||
| private: | ||
| void SetHasMultiple(const std::string& in_out, bool* flag) { | ||
| if (!*flag) { | ||
| AddAttr<std::vector<int>>(in_out + "_format", | ||
| 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. each variable length input will has a 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. The | ||
| "The multiple index of " + in_out + | ||
| "\n" | ||
| R"DOC( | ||
| This attribute is used by Paddle core framework. Paddle's Op support each input | ||
| or output could be a list of variable. This attribute is used to show how that | ||
| list organized. | ||
| | ||
| e.g. | ||
| input = ["a", "b", "c", "d", "e", "f"] | ||
| input_format = [0, 4, 5, 6] | ||
| | ||
| means | ||
| The number of all input variables this op is six, and they are segmented into | ||
| three inputs. | ||
| | ||
| The first input is input[0:4], second is input[4:5], third is input[5:6]. | ||
| )DOC", | ||
| /*generated*/ true); | ||
| *flag = true; | ||
| } | ||
| } | ||
| | ||
| void SetHasMultipleInput() { SetHasMultiple("input", &has_multiple_input_); } | ||
| void SetHasMultipleOutput() { | ||
| SetHasMultiple("output", &has_multiple_output_); | ||
| } | ||
| | ||
| void SetHasTemporaryOutput() { | ||
| if (!has_temporary_output_) { | ||
| AddAttr<std::vector<int>>("temporary_index", | ||
| R"DOC(The temporary index of output. | ||
| | ||
| Not all output of Paddle Op is used by user. For faster computation, each op | ||
| could output some its internal state to other op, other op could take that | ||
| output to make compute faster. | ||
| | ||
| Add a mark to which output is temporary is helpful for future optimization. | ||
| )DOC", | ||
| /*generated*/ true) | ||
| .SetDefault(std::vector<int>()); | ||
| has_temporary_output_ = true; | ||
| } | ||
| } | ||
| | ||
| void CheckNoDuplicatedAttrs() { | ||
| std::unordered_set<std::string> names; | ||
| size_t cnt = 0; | ||
| for (auto& attr : proto_->attrs()) { | ||
| names.insert(attr.name()); | ||
| ++cnt; | ||
| } | ||
| PADDLE_ENFORCE(names.size() == cnt, | ||
| "Cannot register two attribute in same name!"); | ||
| } | ||
| | ||
| OpProto* proto_; | ||
| OpAttrChecker* op_checker_; | ||
| bool has_multiple_input_{false}; | ||
| bool has_multiple_output_{false}; | ||
| bool has_temporary_output_{false}; | ||
| }; | ||
| | ||
| class OpRegistry { | ||
| | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
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.
if
repeateda better name?