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 Sync Machanism for Scope and VaraibleScope. Fix test_fetch_var #37085

Merged
merged 2 commits into from
Nov 11, 2021

Conversation

2742195759
Copy link
Contributor

@2742195759 2742195759 commented Nov 10, 2021

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的同步。

主要改进

  1. 给Scope添加了Listener机制。
  2. 给VariableScope和Scope之间的CreateVaraible添加了同步机制。
  3. Scope作为VariableScope的直接成员。顺便解决了FetchVar的问题。
  4. 改写了VariableScope::AddVar逻辑,将映射逻辑放到了 onCreateVariable 后面。代码更加整洁。注意调用scope->Var 会调用 onCreateVariable。
  5. 目前通过了所有已知单测
  6. 保证了所有的Variable都只Add了一次。

TODO

  1. 其他的同步逻辑,比如Delete和SubScope。但是目前VariableScope的行为还没完全定义。比如VariableScope目前还不支持Delete。

  2. 将ScopeListener作为一个成员。

@paddle-bot-old
Copy link

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

wanghuancoder
wanghuancoder previously approved these changes Nov 10, 2021
Copy link
Contributor

@wanghuancoder wanghuancoder left a 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 {
Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在下个PR 会移除继承

Comment on lines 500 to 508
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);
}
Copy link
Contributor

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.

@@ -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_;
Copy link
Contributor

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.
Copy link
Contributor

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"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"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"SetScope() first"));
"SetScope() firstly"));

// map.
PADDLE_ENFORCE_NE(outer_scope_, nullptr,
platform::errors::PreconditionNotMet(
"the outer_scope_ of Variable is nullptr. please "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"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"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"SetScope() first"));
"SetScope() firstly"));

paddle/fluid/framework/scope.cc Show resolved Hide resolved
Copy link
Contributor

@XiaoguangHu01 XiaoguangHu01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@wanghuancoder wanghuancoder merged commit 32c3e61 into PaddlePaddle:develop Nov 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants