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

[ Dy2Static ] Add closure analysis for control flow and add some unittest #43713

Merged
merged 5 commits into from
Jun 24, 2022

Conversation

2742195759
Copy link
Contributor

PR types

Others

PR changes

Others

Describe

Add closure analysis for control flow and add some unittest :
可以通过 NameVisitor 的 get_funcion_create_var_names 函数来获取当前Function的所有的需要Create的函数。然后配合所有的控制流的修改操作都变为 non-local,就可以确保索引正确。

@paddle-bot-old
Copy link

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@@ -353,6 +389,14 @@ def _is_call_func_name_node(self, node):
return True
return False

def _is_argument_name(self, node):
parent_node = self._get_parent_node(node)
if isinstance(parent_node, gast.arguments): return True
Copy link
Contributor

@Aurelius84 Aurelius84 Jun 22, 2022

Choose a reason for hiding this comment

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

如果foo(a.val.x) case的话,node应该是一个Argument->Attribute->Attribute ,这种会处理么?或者说不需要特殊考虑?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

不应该处理 foo(a.val.x) 的情况,这个情况是函数调用,我们需要处理的是函数定义,当定义的时候 func(x, y) 一定是一个普通的名字。

Copy link
Contributor

@Aurelius84 Aurelius84 left a comment

Choose a reason for hiding this comment

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

Greater Job

@@ -108,6 +108,113 @@ def create_while_nodes(condition_name, body_name, loop_var_names):
return ret


class NameScope:
Copy link
Contributor

Choose a reason for hiding this comment

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

下个PR我们可以把类似的公用类、分析函数统一放到一个文件里,这样可以被其他transofmer导入使用

2. nonlocal variable is stored in node.var_nonlocals.
3. arguments is stored in node.var_args.

for example:
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
for example:
For example:
空行

def _current_funcdef_scope(self):
return self.funcdef_stack[-1].pd_scope

def get_funcion_create_var_names(self, node):
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
def get_funcion_create_var_names(self, node):
def get_function_create_var_names(self, node):


def get_funcion_create_var_names(self, node):
assert isinstance(
node, gast.FunctionDef), "Input node is not function define node"
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
node, gast.FunctionDef), "Input node is not function define node"
node, gast.FunctionDef), "Input node is not a FunctionDef node"

self.generic_visit(node)
write_context = {
type(gast.Store()),
type(gast.AugStore()),
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
type(gast.AugStore()),
gast.AugStore,

from paddle.fluid.layers.utils import map_structure
from paddle.fluid.dygraph.dygraph_to_static.loop_transformer import FunctionNameLivenessAnalysis
from paddle.utils import gast
import inspect
Copy link
Contributor

Choose a reason for hiding this comment

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

import语句注意要清理下,很多import语句是没有在这个单测里用到的

import inspect

SEED = 2020
np.random.seed(SEED)
Copy link
Contributor

Choose a reason for hiding this comment

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

单测里应该不需要np设置seed操作

def __init__(self, ans):
self.ans = ans

def visit(self, node):
Copy link
Contributor

Choose a reason for hiding this comment

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

这个visit应该也不需要重新定义?

JudgeVisitor(ans).visit(gast_root)


class TestClosureAnalysis_Attribute(unittest.TestCase):
Copy link
Contributor

Choose a reason for hiding this comment

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

这里应该要继承 TestClosureAnalysis ?


class TestClosureAnalysis_Attribute(unittest.TestCase):

def setUp(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

如果要继承 TestClosureAnalysis,这里的setUp的函数也可以不写的

@2742195759 2742195759 merged commit 6971771 into PaddlePaddle:develop Jun 24, 2022
@2742195759 2742195759 deleted the dy2static-static-analysis branch June 24, 2022 09:06
sneaxiy pushed a commit to sneaxiy/Paddle that referenced this pull request Jun 27, 2022
…test (PaddlePaddle#43713)

* add closure analysis for control flow and add some unittest

* finetune the design of FunctionScopeVisitor

* fix

* fix python check

* fix code by code review
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.

2 participants