-
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
[ Dy2Static ] Add closure analysis for control flow and add some unittest #43713
[ Dy2Static ] Add closure analysis for control flow and add some unittest #43713
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
@@ -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 |
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.
如果foo(a.val.x) case的话,node应该是一个Argument->Attribute->Attribute ,这种会处理么?或者说不需要特殊考虑?
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.
不应该处理 foo(a.val.x) 的情况,这个情况是函数调用,我们需要处理的是函数定义,当定义的时候 func(x, y) 一定是一个普通的名字。
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.
Greater Job
@@ -108,6 +108,113 @@ def create_while_nodes(condition_name, body_name, loop_var_names): | |||
return ret | |||
|
|||
|
|||
class NameScope: |
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我们可以把类似的公用类、分析函数统一放到一个文件里,这样可以被其他transofmer导入使用
2. nonlocal variable is stored in node.var_nonlocals. | ||
3. arguments is stored in node.var_args. | ||
|
||
for example: |
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.
for example: | |
For example: | |
空行 |
def _current_funcdef_scope(self): | ||
return self.funcdef_stack[-1].pd_scope | ||
|
||
def get_funcion_create_var_names(self, node): |
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.
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" |
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.
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()), |
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.
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 |
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.
import语句注意要清理下,很多import语句是没有在这个单测里用到的
import inspect | ||
|
||
SEED = 2020 | ||
np.random.seed(SEED) |
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.
单测里应该不需要np设置seed操作
def __init__(self, ans): | ||
self.ans = ans | ||
|
||
def visit(self, node): |
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.
这个visit应该也不需要重新定义?
JudgeVisitor(ans).visit(gast_root) | ||
|
||
|
||
class TestClosureAnalysis_Attribute(unittest.TestCase): |
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.
这里应该要继承 TestClosureAnalysis ?
|
||
class TestClosureAnalysis_Attribute(unittest.TestCase): | ||
|
||
def setUp(self): |
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.
如果要继承 TestClosureAnalysis,这里的setUp的函数也可以不写的
…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
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,就可以确保索引正确。