-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Add helper function in GradOpDescMakerBase. Make it easier to use. #4551
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
JiayiFeng
merged 9 commits into
PaddlePaddle:develop
from
reyoung:feature/grad_reg_mechanism_cont
Oct 3, 2017
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e3a642e
Extract BaseClass of grad_op_desc_maker and add some common method
reyoung 32fbc6b
Merge branch 'develop' of github.com:baidu/Paddle into feature/grad_r…
reyoung 3723304
Add missing ctor
reyoung 18fa6b9
Merge branch 'develop' of github.com:baidu/Paddle into feature/grad_r…
reyoung d068777
Merge branch 'develop' of github.com:baidu/Paddle into feature/grad_r…
reyoung 3395bf7
Remove duplicated method in OpDesc
reyoung 495a80a
Update design doc
reyoung b280613
Change Interface to unique_ptr
reyoung 703321e
Fix CI
reyoung 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
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 |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. */ | ||
|
|
||
| #pragma once | ||
| #include "paddle/framework/op_desc.h" | ||
| #include "paddle/framework/operator.h" | ||
|
|
||
| namespace paddle { | ||
| namespace framework { | ||
|
|
||
| class GradOpDescMakerBase { | ||
| public: | ||
| explicit GradOpDescMakerBase(const OpDescBind& fwd_op) : fwd_op_(fwd_op) {} | ||
|
|
||
| virtual ~GradOpDescMakerBase() = default; | ||
| virtual std::vector<OpDescBind> operator()() const = 0; | ||
|
|
||
| protected: | ||
| static std::vector<std::string> ToGradNames( | ||
| const std::vector<std::string>& var_names) { | ||
| std::vector<std::string> ret_val; | ||
| ret_val.reserve(var_names.size()); | ||
| std::transform(var_names.begin(), var_names.end(), | ||
| std::back_inserter(ret_val), GradVarName); | ||
| return ret_val; | ||
| } | ||
|
|
||
| std::vector<std::string> InputGrad(const std::string& name) const { | ||
| return ToGradNames(fwd_op_.Input(name)); | ||
| } | ||
|
|
||
| std::vector<std::string> OutputGrad(const std::string& name) const { | ||
| return ToGradNames(fwd_op_.Output(name)); | ||
| } | ||
|
|
||
| std::vector<std::string> InputNames() const { | ||
| return this->fwd_op_.InputNames(); | ||
| } | ||
|
|
||
| std::vector<std::string> OutputNames() const { | ||
| return this->fwd_op_.OutputNames(); | ||
| } | ||
|
|
||
| std::vector<std::string> Input(const std::string& name) const { | ||
| return fwd_op_.Input(name); | ||
| } | ||
|
|
||
| std::vector<std::string> Output(const std::string& name) const { | ||
| return fwd_op_.Output(name); | ||
| } | ||
|
|
||
| const std::unordered_map<std::string, Attribute>& Attrs() const { | ||
| return fwd_op_.GetAttrMap(); | ||
| } | ||
|
|
||
| const Attribute& GetAttr(const std::string& name) const { | ||
| auto& map = fwd_op_.GetAttrMap(); | ||
| auto it = map.find(name); | ||
| PADDLE_ENFORCE(it != map.end(), "Cannot find attribute %s", name); | ||
| return it->second; | ||
| } | ||
|
|
||
| std::string ForwardOpType() const { return this->fwd_op_.Type(); } | ||
|
|
||
| private: | ||
| const OpDescBind& fwd_op_; | ||
| }; | ||
|
|
||
| class SingleGradOpDescMaker : public GradOpDescMakerBase { | ||
| public: | ||
| using GradOpDescMakerBase::GradOpDescMakerBase; | ||
|
|
||
| std::vector<OpDescBind> operator()() const { return {this->Apply()}; } | ||
|
|
||
| protected: | ||
| virtual OpDescBind Apply() const = 0; | ||
| }; | ||
|
|
||
| class DefaultGradOpDescMaker : public SingleGradOpDescMaker { | ||
| public: | ||
| using SingleGradOpDescMaker::SingleGradOpDescMaker; | ||
|
|
||
| protected: | ||
| virtual OpDescBind Apply() const { | ||
| OpDescBind grad; | ||
| grad.SetType(this->GradOpType()); | ||
|
|
||
| for (auto& input_param : this->InputNames()) { | ||
| grad.SetInput(input_param, this->Input(input_param)); | ||
| grad.SetOutput(GradVarName(input_param), this->InputGrad(input_param)); | ||
| } | ||
|
|
||
| for (auto& output_param : this->OutputNames()) { | ||
| grad.SetInput(output_param, this->Output(output_param)); | ||
| grad.SetInput(GradVarName(output_param), this->OutputGrad(output_param)); | ||
| } | ||
|
|
||
| grad.SetAttrMap(this->Attrs()); | ||
|
|
||
| return grad; | ||
| } | ||
|
|
||
| virtual std::string GradOpType() const { | ||
| return this->ForwardOpType() + "_grad"; | ||
| } | ||
| }; | ||
|
|
||
| } // namespace framework | ||
| } // namespace paddle |
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
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.
grad_op_maker_returnsstd::vector<OpDescBind>, while inBlockDescBindops_isstd::deque<std::unique_ptr<OpDescBind>>. So when and how shall we convertOpDescBindtostd::unique_ptr<OpDescBind>?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.
Yes. You are right.
grad_op_maker_should returnvector<unique_ptr<OpDescBind>>.