-
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
Add Sync Machanism for Scope and VaraibleScope. Fix test_fetch_var #37085
Add Sync Machanism for Scope and VaraibleScope. Fix test_fetch_var #37085
Conversation
Thanks for your contribution! |
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.
LGTM
// ScopeListener is the callback to sync changes in Original Scope. We can make | ||
// it | ||
// a membership of VariableScope. Here we use inherent. | ||
class VariableScope : public ScopeBase, public ScopeListener { |
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.
It seems no need to inherent.
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.
在下个PR 会移除继承
void SetScope(Scope* outer_scope) { | ||
// We don't consider SetScope twice currently. | ||
PADDLE_ENFORCE_EQ( | ||
outer_scope_, nullptr, | ||
platform::errors::PreconditionNotMet( | ||
"You have been SetScope(), please don't call SetScope twice.")); | ||
outer_scope_ = outer_scope; | ||
outer_scope->AddListener(this); | ||
} |
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.
U can directly initialize outer_scope_ in constructor, and do not expose a SetScope
method.
paddle/fluid/framework/scope.h
Outdated
@@ -164,6 +184,7 @@ class Scope : public ScopeBase { | |||
// Scope in `kids_` are owned by this class. | |||
mutable std::list<Scope*> kids_; | |||
const Scope* parent_{nullptr}; | |||
mutable std::list<ScopeListener*> listeners_; |
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.
is mutable needed?
// machanism. | ||
// ScopeListener is the callback to sync changes in Original Scope. We can make | ||
// it | ||
// a membership of VariableScope. Here we use inherent. |
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.
这里的注释要手动调整下,pre-commit有时候只关注长度是否满足,样式上不是很好看
const Scope* GetScope() const { return scope_ptr_.get(); } | ||
|
||
~VariableScope() { | ||
outer_scope_->DelListener(this); // don't listen anymore. |
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.
outer_scope_->DelListener(this); // don't listen anymore. | |
if(nullptr!=outer_scope_){ | |
outer_scope_->DelListener(this); // don't listen anymore. | |
} |
VLOG(4) << "Add variable: " << name << " through AddVar()"; | ||
PADDLE_ENFORCE_NE(outer_scope_, nullptr, | ||
platform::errors::PreconditionNotMet( | ||
"the outer_scope_ of Variable is nullptr. please " |
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.
"the outer_scope_ of Variable is nullptr. please " | |
"Reqiured the outer_scope_ of Variable is nullptr. please " |
PADDLE_ENFORCE_NE(outer_scope_, nullptr, | ||
platform::errors::PreconditionNotMet( | ||
"the outer_scope_ of Variable is nullptr. please " | ||
"SetScope() first")); |
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.
"SetScope() first")); | |
"SetScope() firstly")); |
// map. | ||
PADDLE_ENFORCE_NE(outer_scope_, nullptr, | ||
platform::errors::PreconditionNotMet( | ||
"the outer_scope_ of Variable is nullptr. please " |
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.
"the outer_scope_ of Variable is nullptr. please " | |
"Required the outer_scope_ of Variable is nullptr. please " |
PADDLE_ENFORCE_NE(outer_scope_, nullptr, | ||
platform::errors::PreconditionNotMet( | ||
"the outer_scope_ of Variable is nullptr. please " | ||
"SetScope() first")); |
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.
"SetScope() first")); | |
"SetScope() firstly")); |
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.
LGTM
PR types
Others
PR changes
Others
Describe
Add Sync Machanism for Scope and VaraibleScope. Fix test_fetch_var
背景
放弃了使用继承Scope的方式来实现同步,相反的,将同步机制和Scope的变量存储逻辑进行了分离。使用Listener的模式设置回调函数实现了Scope和VaraibelScope的同步,这样Scope可以作为VariableScope的一个成员,就不需要引入额外的tmpScope和globalScope的同步。
主要改进
TODO
其他的同步逻辑,比如Delete和SubScope。但是目前VariableScope的行为还没完全定义。比如VariableScope目前还不支持Delete。
将ScopeListener作为一个成员。